1 line
5.6 KiB
PHP
1 line
5.6 KiB
PHP
<?php
|
|
namespace common\components;
|
|
use common\models\ShoppingCart;
|
|
use frontend\models\CustomerCartForm;
|
|
use yii\base\Object;
|
|
use common\models\TicketType;
|
|
use common\models\Transfer;
|
|
use common\models\Ticket;
|
|
use common\models\Discount;
|
|
|
|
|
|
class TicketSale extends Object{
|
|
|
|
/**
|
|
* A bérlet típusa. Kötelező
|
|
* */
|
|
public $ticketType;
|
|
/**
|
|
* A vendég . Kötelező
|
|
* */
|
|
public $customer;
|
|
/**
|
|
* A kassza. Kötelező
|
|
* */
|
|
public $account;
|
|
/**
|
|
* A bérletkártya. Kötelező
|
|
* */
|
|
public $card;
|
|
/**
|
|
* A kedvezmény. Opcionális
|
|
* */
|
|
public $discount;
|
|
/*
|
|
* Bérlet érvényesség első napja. Opcionális.
|
|
* Ha nincs megadva, a mai nap
|
|
* */
|
|
public $start;
|
|
/**
|
|
* A bérlet érvényességének utolsó napja. OPcionális.
|
|
* Ha nincs megadva a bérlet típusból számoljuk ki.
|
|
* Ha van megadva megbízás, akkor a megbízásból számoljuk ki
|
|
*
|
|
* */
|
|
public $end;
|
|
/**
|
|
* A szerződs. Opcionális
|
|
* */
|
|
public $contract;
|
|
/**
|
|
* Tranzakció állapota. Default: paid
|
|
* */
|
|
public $transferStatus = Transfer::STATUS_PAID;
|
|
/**
|
|
* A fizetési mód. Default : transfer
|
|
* */
|
|
public $paymentMethod = Transfer::PAYMENT_METHOD_TRANSFER;
|
|
/**
|
|
* if this is set, money and start date will be read from this object
|
|
* */
|
|
public $ticketInstallmentRequest;
|
|
|
|
/**
|
|
* A létrehozott bérlet
|
|
* */
|
|
public $ticket;
|
|
/**
|
|
* A létrehozott tranzakció
|
|
* */
|
|
public $transfer;
|
|
/**
|
|
* A bérlet ára. Opcionális
|
|
* Ha nincs megadva a bérlet típusból számoljuk ki
|
|
* Ha van megbízás, a megbízásból számoljuk ki
|
|
* */
|
|
public $money;
|
|
|
|
|
|
|
|
public function doSale(){
|
|
|
|
$this->readMoney();
|
|
$this->readStartDate();
|
|
$this->readEndDate();
|
|
|
|
$this->mkTicket();
|
|
$this->mkTransfer();
|
|
$this->addToCustomerCart();
|
|
}
|
|
|
|
protected function mkTicket( ) {
|
|
|
|
$this->ticket = new Ticket ();
|
|
|
|
$this->ticket->id_user = \Yii::$app->user->id;
|
|
$this->ticket->id_ticket_type = $this->ticketType->id_ticket_type; // save to contract
|
|
$this->ticket->id_account = $this->account->id_account;
|
|
$this->ticket->id_discount = isset($this->discount) ? $this->discount->id_discount : null; // contract.id_discount
|
|
$this->ticket->start = $this->start;
|
|
$this->ticket->end = $this->end;
|
|
$this->ticket->max_usage_count = $this->ticketType->max_usage_count;
|
|
$this->ticket->usage_count = 0;
|
|
$this->ticket->status =
|
|
$this->transferStatus == Transfer::STATUS_PAID ? Ticket::STATUS_ACTIVE : Ticket::STATUS_INACTIVE ;
|
|
$this->ticket->price_brutto = $this->money;
|
|
$this->ticket->id_card = $this->card->id_card;
|
|
|
|
if ( isset( $this->ticketInstallmentRequest ) ){
|
|
$this->ticket->part = $this->ticketInstallmentRequest->priority;
|
|
}
|
|
|
|
if (isset($this->contract)){
|
|
$this->ticket->id_contract = $this->contract->id_contract;
|
|
}
|
|
|
|
if ( !$this->ticket->save ( false ) ){
|
|
\Yii::error("Nem sikerült menteni a bérletet!");
|
|
throw new \Exception("Bérlet mentése nem sikerült");
|
|
}
|
|
|
|
\Yii::info("Bérlet elmentve: id=" . $this->ticket->id_ticket);
|
|
|
|
}
|
|
protected function mkTransfer( ) {
|
|
$this->transfer = new Transfer ();
|
|
|
|
$this->transfer->status = $this->transferStatus;
|
|
$this->transfer->type = Transfer::TYPE_TICKET;
|
|
$this->transfer->direction = Transfer::DIRECTION_IN;
|
|
$this->transfer->id_object = $this->ticket->id_ticket;
|
|
$this->transfer->item_price = $this->ticketType->price_brutto;
|
|
$this->transfer->money = $this->money;
|
|
$this->transfer->id_account = $this->account->id_account;
|
|
$this->transfer->count = 1;
|
|
|
|
if ($this->transferStatus == Transfer::STATUS_PAID) {
|
|
$this->transfer->paid_at = date ( 'Y-m-d H:i:s' );
|
|
$this->transfer->paid_by = \Yii::$app->user->id;
|
|
}
|
|
|
|
$this->transfer->payment_method = $this->paymentMethod;
|
|
|
|
$this->transfer->comment = "";
|
|
if ( isset($this->ticketInstallmentRequest)){
|
|
$this->transfer->comment = "Csoportos megbízással";
|
|
}
|
|
$this->transfer->id_user = \Yii::$app->user->id;
|
|
$this->transfer->id_customer = $this->customer->id_customer;
|
|
|
|
|
|
|
|
if ( !$this->transfer->save (false) ){
|
|
\Yii::error("Nem sikerült menteni a tranzakciót!");
|
|
throw new \Exception("Tranzakció mentése nem sikerült");
|
|
}
|
|
|
|
\Yii::info("tranzakció elmentve: id=" . $this->transfer->id_transfer);
|
|
|
|
}
|
|
protected function addToCustomerCart( ) {
|
|
if ( isset( $this->customer ) ){
|
|
if ( $this->transferStatus == Transfer::STATUS_NOT_PAID ){
|
|
$cartItem = new ShoppingCart();
|
|
$cartItem->id_customer = $this->customer->id_customer;
|
|
$cartItem->id_transfer = $this->transfer->id_transfer;
|
|
$cartItem->save(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function readMoney(){
|
|
if ( !isset($this->money)){
|
|
$this->money = $this->ticketType->price_brutto;
|
|
|
|
if (isset($this->discount)){
|
|
$this->money = Discount::applyDiscount($this->money, $this->discount);
|
|
}
|
|
|
|
if ( isset($this->ticketInstallmentRequest)){
|
|
$this->money = $this->ticketInstallmentRequest->money;
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function readStartDate(){
|
|
if ( !isset($this->start)){
|
|
$this->start = Helper::getDateString();
|
|
if ( isset($this->ticketInstallmentRequest)){
|
|
$this->start = $this->ticketInstallmentRequest->request_target_time_at;
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function readEndDate(){
|
|
if ( !isset($this->end)){
|
|
$unit = "month";
|
|
if ( $this->ticketType->time_unit_type == TicketType::TIME_UNIT_DAY ) {
|
|
$unit = "day";
|
|
}
|
|
$count = $this->ticketType->time_unit_count;
|
|
$this->end = date( 'Y-m-d', strtotime( $this->start . " +$count $unit -1 day"));
|
|
if ( isset($this->ticketInstallmentRequest)){
|
|
$this->end = date( 'Y-m-d', strtotime( $this->ticketInstallmentRequest->request_target_time_at . " +1 month -1 day"));
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
} |