From 8de73c058513db22576ea9a287b866bf7f471d1a Mon Sep 17 00:00:00 2001 From: Roland Schneider Date: Tue, 26 Mar 2019 07:29:47 +0100 Subject: [PATCH] implement feature 'put to cart ticket installment request' --- backend/components/ContractManager.php | 162 ++++++++ backend/controllers/ContractController.php | 29 +- .../TicketInstallmentRequestController.php | 45 ++- .../_index_view.php | 351 +++++++++--------- common/config/params.php | 1 + common/models/Inventory.php | 26 +- common/models/Ticket.php | 2 + common/models/TicketInstallmentRequest.php | 9 +- console/controllers/ContractController.php | 22 +- 9 files changed, 444 insertions(+), 203 deletions(-) create mode 100644 backend/components/ContractManager.php diff --git a/backend/components/ContractManager.php b/backend/components/ContractManager.php new file mode 100644 index 0000000..652e2be --- /dev/null +++ b/backend/components/ContractManager.php @@ -0,0 +1,162 @@ + $id_ticket_installment_request]); + + if (!isset($request)) { + throw new NotFoundHttpException("ticket_installment_request not found #" . $id_ticket_installment_request); + } + + // if status does not support open, return + if (!( + $request->status == TicketInstallmentRequest::$STATUS_ACCEPTED_MANUAL + || $request->status == TicketInstallmentRequest::$STATUS_CANCELED + || $request->status == TicketInstallmentRequest::$STATUS_REJECTED + ) + ) { + $this->info("request can't be opened with status " . TicketInstallmentRequest::toStatusName($request->status)); + return; + } + + $contract = Contract::findOne($request->id_contract); + + if (!isset($contract)) { + throw new NotFoundHttpException("contract not found #" . $id_ticket_installment_request); + } + + $this->openTicketInstallmentRequest($contract, $request); + + $this->putToCart($contract, $request); + + } + + /** + * If status is + * TicketInstallmentRequest::$STATUS_ACCEPTED: nothing to do, automatic purchase was successful + * TicketInstallmentRequest::$STATUS_PENDING: nothing to do, automatic purchase was successful + * TicketInstallmentRequest::$STATUS_MARKED_TO_SEND: nothing to do, automatic purchase was successful + * TicketInstallmentRequest::$STATUS_SENT: nothing to do, automatic purchase was successful + * TicketInstallmentRequest::$STATUS_SENT: nothing to do, automatic purchase was successful + * + * @param $contract \common\models\Contract + * @param $part \common\models\TicketInstallmentRequest + * @throws \Exception + * @throws \Throwable + */ + public function openTicketInstallmentRequest($contract, $part) + { + $statusChanged = false; + if ($part->status == TicketInstallmentRequest::$STATUS_CANCELED) { + $part->status = TicketInstallmentRequest::$STATUS_PENDING; + $part->request_processed_at = null; + $statusChanged = true; + } + + if ($part->status == TicketInstallmentRequest::$STATUS_REJECTED) { + $part->status = TicketInstallmentRequest::$STATUS_ACCEPTED_MANUAL; + $statusChanged = true; + } + + + if ($statusChanged) { + $part->save(false); + if (!$contract->isFlagActive()) { + $contract->flag = Contract::$FLAG_ACTIVE; + $contract->save(false); + } + } + + } + + /** + * @param $contract + * @param $part + * @throws \Exception + */ + private function putToCart($contract, $part) + { + /** @var \common\models\Ticket $ticket */ + $ticket = Ticket::findOne(['id_ticket' => $part->id_ticket]); + + if (isset($ticket)) { + $this->info("Ticket found: " . $ticket->id_ticket . "/" . $ticket->ticketType->name); + + // inactivate ticket + if ($ticket->status != Ticket::STATUS_INACTIVE) { + $ticket->status = Ticket::STATUS_INACTIVE; + $ticket->save(false); + } + + // set transfer status to not paid + /** @var \common\models\Transfer $transfer */ + $transfer = $ticket->transfer; + if ($transfer->status != Transfer::STATUS_NOT_PAID) { + // clean up transfer paid status + $statusOriginal = $transfer->status; + $transfer->status = Transfer::STATUS_NOT_PAID; + $transfer->paid_at = null; + $transfer->paid_by = null; + $transfer->payment_method = null; + $transfer->save(false); + + $this->info("Restore transfer: "); + $this->info($transfer->id_transfer); + $this->info(Transfer::toStatusName($statusOriginal) . "->" . Ticket::toStatusName($transfer->status)); + } + + // check shopping cart + $cart = ShoppingCart::findOne(['id_transfer' => $transfer->id_transfer]); + // put transfer into cart if it is not there + if (!isset($cart)) { + $cart = new ShoppingCart(); + $cart->id_customer = $contract->customer->id_customer; + $cart->id_transfer = $transfer->id_transfer; + if (!$cart->save(false)) { + /** @noinspection SpellCheckingInspection */ + $E_FAILED_TO_SAVE = "Vendég kosár hozzárendelés sikertelen!"; + \Yii::error($E_FAILED_TO_SAVE); + throw new \Exception($E_FAILED_TO_SAVE); + } + } + + } + } + + private function info($message) + { + \Yii::info($message); + } + +} diff --git a/backend/controllers/ContractController.php b/backend/controllers/ContractController.php index 090a8e2..d181de4 100644 --- a/backend/controllers/ContractController.php +++ b/backend/controllers/ContractController.php @@ -2,15 +2,14 @@ namespace backend\controllers; +use backend\components\ContractManager; use Yii; use common\models\Contract; use backend\models\ContractSearch; use yii\web\Controller; use yii\web\NotFoundHttpException; -use yii\filters\VerbFilter; use backend\models\ContractCustomerSearch; use common\models\Customer; -use yii\db\Query; use backend\models\ContractRequestSearch; use common\models\TicketInstallmentRequest; use common\models\Ticket; @@ -64,6 +63,7 @@ class ContractController extends Controller { /** * Lists all Contract models. * + * @param $id * @return mixed * @throws NotFoundHttpException */ @@ -197,17 +197,21 @@ class ContractController extends Controller { ], [ 'id_contract' => $model->id_contract ] ); - $ticket = Ticket::find ()->andWhere ( [ + + /** @var \common\models\Ticket $ticket */ + $ticket = Ticket::find ()->andWhere ( [ 'id_contract' => $model->id_contract ] )->one (); - - $transfer = Transfer::find ()->andWhere ( [ + + + /** @var \common\models\Transfer $transfer */ + $transfer = Transfer::find ()->andWhere ( [ 'type' => Transfer::TYPE_TICKET ] )->andWhere ( [ 'id_object' => $ticket->id_ticket ] )->one (); - $transfer->storno (); + $transfer->storno(); $tx->commit (); // $tx->rollBack(); @@ -239,8 +243,7 @@ class ContractController extends Controller { 'contract' => $contract ] ); - // $searchModel->contract = $contract; - + $dataProvider = $searchModel->search ( Yii::$app->request->queryParams ); return $this->render ( 'details', [ @@ -249,6 +252,16 @@ class ContractController extends Controller { 'dataProvider' => $dataProvider ] ); } + + /** + * @param $id + * @throws NotFoundHttpException + * @throws \Throwable + */ + public function actionRequestToCart($id){ + $contractManager = new ContractManager(); + $contractManager->openRequestAndPutItItoCart($id); + } /** * Finds the Contract model based on its primary key value. diff --git a/backend/controllers/TicketInstallmentRequestController.php b/backend/controllers/TicketInstallmentRequestController.php index 6352b33..1821ee3 100644 --- a/backend/controllers/TicketInstallmentRequestController.php +++ b/backend/controllers/TicketInstallmentRequestController.php @@ -2,9 +2,11 @@ namespace backend\controllers; +use backend\components\ContractManager; use Yii; use common\models\TicketInstallmentRequest; use backend\models\TicketInstallmentRequestSearch; +use yii\helpers\Url; use yii\web\Controller; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; @@ -58,7 +60,7 @@ class TicketInstallmentRequestController extends Controller { $searchModel = new TicketInstallmentRequestSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); - + Url::remember('',"ticket-installment-request/index"); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, @@ -84,29 +86,31 @@ class TicketInstallmentRequestController extends Controller 'dataProvider' => $dataProvider, ]); } - - - public function actionAccept( $id ) + + + /** + * @param $id + * @return \yii\web\Response + * @throws NotFoundHttpException + */ + public function actionAccept($id ) { $model = $this->findModel($id); - + if ( !$model->isStatusAccepted() ){ $model->applyStatus(TicketInstallmentRequest::$STATUS_ACCEPTED_MANUAL,true); \Yii::$app->session->setFlash('success',"Megbízás teljesítve"); } -// else{ - -// } - -// echo "asdff"; - + return $this->redirect(['ticket-installment-request/view', 'id' => $model->id_ticket_installment_request, ]); } + /** * Lists pending TicketInstallmentRequest models. * @return mixed + * @throws \Exception */ public function actionDownloadGiro() { @@ -132,6 +136,7 @@ class TicketInstallmentRequestController extends Controller * Displays a single TicketInstallmentRequest model. * @param integer $id * @return mixed + * @throws NotFoundHttpException */ public function actionView($id) { @@ -163,6 +168,7 @@ class TicketInstallmentRequestController extends Controller * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed + * @throws NotFoundHttpException */ public function actionUpdate($id) { @@ -182,6 +188,8 @@ class TicketInstallmentRequestController extends Controller * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed + * @throws NotFoundHttpException + * @throws \yii\db\StaleObjectException */ public function actionDelete($id) { @@ -192,10 +200,23 @@ class TicketInstallmentRequestController extends Controller public function actionTest( ) { - return $this->render('test'); } + /** + * @param $id + * @return string + * @throws NotFoundHttpException + * @throws \Throwable + */ + public function actionPutToCart($id ) + { + + $contractManager = new ContractManager(); + $contractManager->openRequestAndPutItItoCart($id); + return $this->redirect( Url::previous("ticket-installment-request/index")); + } + /** * Finds the TicketInstallmentRequest model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. diff --git a/backend/views/ticket-installment-request/_index_view.php b/backend/views/ticket-installment-request/_index_view.php index f07aac2..5adf002 100644 --- a/backend/views/ticket-installment-request/_index_view.php +++ b/backend/views/ticket-installment-request/_index_view.php @@ -1,181 +1,192 @@ - - - + + +
-
-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- Megbízás azonosító - - - - Megbízás státusza - - - - Megbízás összege - - -
- Megbízás esedékességének dátuma - - formatter->asDatetime( $model['request_request_target_time_at'] );?> - - Megbízás elindításának ideje - - formatter->asDatetime( $model['request_sent_at'] );?> - - Megbízás feldoglozásának ideje - - formatter->asDatetime( $model['request_processed_at'] );?> -
- Megbízás prioritása - - - - Köteg azonosító - - - - Szerződés azonosító - - -
- Vendég azonosító - - - - Vendég neve - - - - Kártyaszám - - -
- Bérlet azonosító - - - - Bérlet típus - - - - Bérlet státusza - - -
- Bérlet érvényességének kezdete - - formatter->asDate( $model['ticket_start'] ) ;?> - - Bérlet érvényességének vége - - formatter->asDate( $model['ticket_end'] ) ;?> - - -
- Tétel sorszám - - - - - - -
-
- $model['ugiro_id_ugiro']] ,[ 'class' => 'btn btn-primary']); - } - echo Html::a("Megbízás részletek",['ticket-installment-request/view', 'id' => $model['request_id_ticket_installment_request']] ,[ 'class' => 'btn btn-primary']); - if ( !empty( $model['ticket_id_ticket'] )){ - echo Html::a("Szerződés megbízásai",['ticket-installment-request/index', 'TicketInstallmentRequestSearch[id_contract]' => $model['request_id_contract']] ,[ 'class' => 'btn btn-primary']); - } - ?> +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ Megbízás azonosító + + + + Megbízás státusza + + + + Megbízás összege + + +
+ Megbízás esedékességének dátuma + + formatter->asDatetime($model['request_request_target_time_at']); ?> + + Megbízás elindításának ideje + + formatter->asDatetime($model['request_sent_at']); ?> + + Megbízás feldoglozásának ideje + + formatter->asDatetime($model['request_processed_at']); ?> +
+ Megbízás prioritása + + + + Köteg azonosító + + + + Szerződés azonosító + + +
+ Vendég azonosító + + + + Vendég neve + + + + Kártyaszám + + +
+ Bérlet azonosító + + + + Bérlet típus + + + + Bérlet státusza + + +
+ Bérlet érvényességének kezdete + + formatter->asDate($model['ticket_start']); ?> + + Bérlet érvényességének vége + + formatter->asDate($model['ticket_end']); ?> + + +
+ Tétel sorszám + + + + + + +
+
+ $model['ugiro_id_ugiro']], ['class' => 'btn btn-primary']); + } + echo Html::a("Megbízás részletek", ['ticket-installment-request/view', 'id' => $model['request_id_ticket_installment_request']], ['class' => 'btn btn-primary']); + if (!empty($model['ticket_id_ticket'])) { + echo Html::a("Szerződés megbízásai", ['ticket-installment-request/index', 'TicketInstallmentRequestSearch[id_contract]' => $model['request_id_contract']], ['class' => 'btn btn-primary']); + } + if (TicketInstallmentRequest::canBePutToCustomerCart($model['request_status'])) { + echo Html::a("Kosárba helyez", ['ticket-installment-request/put-to-cart' , 'id' => $model['request_id_ticket_installment_request']], + [ + 'class' => 'btn btn-danger', + 'data-method' => 'POST' + ] + ); + } + ?> +
+
+
-
-
-
\ No newline at end of file diff --git a/common/config/params.php b/common/config/params.php index 4f6683b..7ae6114 100644 --- a/common/config/params.php +++ b/common/config/params.php @@ -64,4 +64,5 @@ return [ 'ticket_type_door_allowed_check_on' => false, 'warn_ticket_expire_in_days_count' => 3, 'warn_ticket_expire_in_usage_count' => 3, + 'inventory.products.only.active' => true ]; diff --git a/common/models/Inventory.php b/common/models/Inventory.php index 8fadb3e..460fd2c 100644 --- a/common/models/Inventory.php +++ b/common/models/Inventory.php @@ -71,7 +71,7 @@ class Inventory extends \common\models\BaseFitnessActiveRecord } - public function validateOnlyClosed($attribute,$params){ + public function validateOnlyClosed( ){ $opened = Inventory::find()->andWhere(['status' => Inventory::$STATUS_OPEN])->all(); if ( count($opened) > 0 ){ $this->addError("name","Kérem előbb zárjon le minden másik leltárt"); @@ -93,7 +93,12 @@ class Inventory extends \common\models\BaseFitnessActiveRecord ] ], parent::behaviors()); } - + + /** + * @param $insert + * @param $changedAttributes + * @throws \Exception + */ public function afterSave($insert, $changedAttributes){ if ( $insert ){ @@ -101,12 +106,18 @@ class Inventory extends \common\models\BaseFitnessActiveRecord if ( isset($this->id_account) && is_numeric($this->id_account)){ $query->andWhere(['id_account' => $this->id_account]); } + + if ( \Yii::$app->params['inventory.products.only.active']) { + $query->andWhere( ['status' => Product::STATUS_ACTIVE] ); + } + $products = $query->all(); $inventoryGroups = InventoryGroup::find()->all(); - - - foreach ($products as $product){ + + + /** @var \common\models\Product $product */ + foreach ($products as $product){ $form = new InventoryItemForm( [ 'inventory' => $this, @@ -117,8 +128,9 @@ class Inventory extends \common\models\BaseFitnessActiveRecord ); $form->save(); } - - foreach ($inventoryGroups as $group){ + + /** @var \common\models\InventoryGroup $group */ + foreach ($inventoryGroups as $group){ $form = new InventoryItemForm( [ 'inventory' => $this, diff --git a/common/models/Ticket.php b/common/models/Ticket.php index 99ad375..31d8b72 100644 --- a/common/models/Ticket.php +++ b/common/models/Ticket.php @@ -36,6 +36,8 @@ use common\components\Helper; * * @property \common\models\Card card * @property \common\models\Ticket transfer + * @property \common\models\TicketType ticketType + * @property \common\models\Discount discount */ class Ticket extends \common\models\BaseFitnessActiveRecord { diff --git a/common/models/TicketInstallmentRequest.php b/common/models/TicketInstallmentRequest.php index ffacd06..a55e5f4 100644 --- a/common/models/TicketInstallmentRequest.php +++ b/common/models/TicketInstallmentRequest.php @@ -342,5 +342,12 @@ class TicketInstallmentRequest extends \yii\db\ActiveRecord public function getStatusName(){ return static::toStatusName($this->status); } - + + public static function canBePutToCustomerCart($status){ + return $status == TicketInstallmentRequest::$STATUS_CANCELED + || + $status == TicketInstallmentRequest::$STATUS_REJECTED + || + $status == TicketInstallmentRequest::$STATUS_ACCEPTED_MANUAL; + } } diff --git a/console/controllers/ContractController.php b/console/controllers/ContractController.php index 316f244..306b0fd 100644 --- a/console/controllers/ContractController.php +++ b/console/controllers/ContractController.php @@ -2,6 +2,7 @@ namespace console\controllers; +use backend\components\ContractManager; use common\models\Card; use common\models\Contract; use common\models\Customer; @@ -15,6 +16,17 @@ use yii\console\Exception; class ContractController extends Controller { + + /** + * @param $requestId + * @throws \Throwable + * @throws \yii\web\NotFoundHttpException + */ + public function actionRequestToCart($requestId){ + $contractManager = new ContractManager(); + $contractManager->openRequestAndPutItItoCart($requestId); + } + /** * @param $cardNumber * @throws Exception @@ -84,15 +96,15 @@ class ContractController extends Controller ); } else if ($part->isStatusAccepted()) { $this->info("loading ticket: #" .$part->id_ticket); + /** @var \common\models\Ticket $ticket */ $ticket = null; - try{ - /** @var \common\models\Ticket $ticket */ - // $ticket = Ticket::findOne(['id_ticket' => $part->id_ticket]); + try { + $ticket = Ticket::findOne(['id_ticket' => $part->id_ticket]); }catch (\Throwable $e){ \Yii::info("Failed to load ticket: " . $e->getMessage()); } if (isset($ticket)) { - $this->info("Ticket found: " . $ticket->id_ticket . "/" . $ticket->ticetType->name); + $this->info("Ticket found: " . $ticket->id_ticket . "/" . $ticket->ticketType->name); if ($ticket->status == Ticket::STATUS_DELETED) { $ticket->status = Ticket::STATUS_ACTIVE; $ticket->save(false); @@ -148,4 +160,4 @@ class ContractController extends Controller echo $msg . "\n"; } -} \ No newline at end of file +}