add contract controller(s) changes
This commit is contained in:
164
console/controllers/ContractController.php
Normal file
164
console/controllers/ContractController.php
Normal file
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
|
||||
namespace console\controllers;
|
||||
|
||||
use common\models\Card;
|
||||
use common\models\Contract;
|
||||
use common\models\Customer;
|
||||
use common\models\ShoppingCart;
|
||||
use common\models\Ticket;
|
||||
use common\models\TicketInstallmentRequest;
|
||||
use common\models\Transfer;
|
||||
use yii\console\Controller;
|
||||
use yii\console\Exception;
|
||||
|
||||
class ContractController extends Controller
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $cardNumber
|
||||
* @throws Exception
|
||||
* @throws \yii\db\Exception
|
||||
*/
|
||||
public function actionRestoreCanceled($cardNumber)
|
||||
{
|
||||
|
||||
$card = Card::findOne(['number' => $cardNumber]);
|
||||
if (!isset($card)) {
|
||||
throw new Exception("Card not found: " . $cardNumber);
|
||||
}
|
||||
|
||||
$this->info("Card loaded");
|
||||
$this->info($card->id_card);
|
||||
$this->info($card->number);
|
||||
|
||||
$customer = Customer::findOne(['id_customer_card' => $card->id_card]);
|
||||
if (!isset($customer)) {
|
||||
throw new Exception("Customer not found for card: " . $cardNumber);
|
||||
}
|
||||
|
||||
$this->info("Customer loaded");
|
||||
$this->info($customer->id_customer);
|
||||
$this->info($customer->name);
|
||||
|
||||
|
||||
// find latest contract for customer
|
||||
/** @var \common\models\Contract $contract */
|
||||
$contract = Contract::find()
|
||||
->andWhere(['id_customer' => $customer->id_customer])
|
||||
->orderBy(['created_at' => SORT_DESC])
|
||||
->limit(1)
|
||||
->one();
|
||||
|
||||
if (!isset($contract)) {
|
||||
throw new Exception("Contract not found for customer: " . $customer->id_customer);
|
||||
}
|
||||
|
||||
$this->info("Contract loaded");
|
||||
$this->info($contract->id_contract);
|
||||
$this->info(Contract::toFlangName($contract->flag));
|
||||
|
||||
$parts = TicketInstallmentRequest::findAll(['id_contract' => $contract->id_contract]);
|
||||
$this->info("Parts loaded");
|
||||
|
||||
$tx = \Yii::$app->db->beginTransaction();
|
||||
try {
|
||||
if (!$contract->isFlagActive()) {
|
||||
$contract->flag = Contract::$FLAG_ACTIVE;
|
||||
$I_CONTRACT = "Contract - change flag " . Contract::toStatusName($contract->flag) . "=>" .
|
||||
Contract::toStatusName(Contract::$FLAG_ACTIVE);
|
||||
$contract->save(false);
|
||||
$this->info($I_CONTRACT);
|
||||
}
|
||||
|
||||
foreach ($parts as $part) {
|
||||
$this->info("Part #" . $part->priority);
|
||||
if ($part->status == TicketInstallmentRequest::$STATUS_CANCELED) {
|
||||
$part->status = TicketInstallmentRequest::$STATUS_PENDING;
|
||||
$part->request_processed_at = null;
|
||||
$part->save(false);
|
||||
$this->info( "Change status: "
|
||||
. TicketInstallmentRequest::toStatusName(TicketInstallmentRequest::$STATUS_CANCELED)
|
||||
. "->"
|
||||
. TicketInstallmentRequest::toStatusName(TicketInstallmentRequest::$STATUS_PENDING)
|
||||
);
|
||||
} else if ($part->isStatusAccepted()) {
|
||||
/** @var \common\models\Ticket $ticket */
|
||||
$ticket = $part->ticket;
|
||||
if ($ticket->status == Ticket::STATUS_DELETED) {
|
||||
$ticket->status = Ticket::STATUS_ACTIVE;
|
||||
$ticket->save(false);
|
||||
$this->info( "Ticket restored: ");
|
||||
$this->info( $ticket->id_ticket );
|
||||
$this->info( Ticket::toStatusName(Ticket::STATUS_DELETED)
|
||||
. "->"
|
||||
. Ticket::toStatusName(Ticket::STATUS_ACTIVE)
|
||||
);
|
||||
|
||||
}
|
||||
/** @var \common\models\Transfer $transfer */
|
||||
$transfer = $ticket->transfer;
|
||||
if ($transfer->status == Transfer::STATUS_STORNO) {
|
||||
$transfer->status = Transfer::STATUS_PAID;
|
||||
$transfer->save(false);
|
||||
|
||||
$this->info( "Restore transfer: ");
|
||||
$this->info( $transfer->id_transfer);
|
||||
$this->info( Transfer::toStatusName(Transfer::STATUS_STORNO)
|
||||
. "->"
|
||||
. Ticket::toStatusName(Transfer::STATUS_PAID)
|
||||
);
|
||||
|
||||
if (!isset($transfer->paid_at)) {
|
||||
$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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$tx->commit();
|
||||
$this->info("Ready");
|
||||
} catch (\Throwable $e) {
|
||||
$tx->rollBack();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function info($msg)
|
||||
{
|
||||
echo $msg . "\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \common\models\Contract $contract
|
||||
* @param \common\models\TicketInstallmentRequest[] $parts
|
||||
*/
|
||||
private function log($contract, $parts)
|
||||
{
|
||||
|
||||
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
|
||||
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
|
||||
|
||||
foreach ($contract->getAttributes() as $key => $value) {
|
||||
echo $key . "=" . $value . ";";
|
||||
}
|
||||
echo "\n";
|
||||
echo "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
|
||||
foreach ($parts as $part) {
|
||||
echo "----------------------------------------------------------------------\n";
|
||||
foreach ($part->getAttributes() as $key => $value) {
|
||||
echo $key . "=" . $value . ";";
|
||||
}
|
||||
echo "\n";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user