add hidden account support add delete/payout buttons to carts add backend product sales with pdf export add frontend product sales with pdf export add frontend ticket sales with pdf export
183 lines
4.7 KiB
PHP
183 lines
4.7 KiB
PHP
<?php
|
|
namespace frontend\models;
|
|
|
|
use common\models\Ticket;
|
|
use common\models\TicketType;
|
|
use common\models\Account;
|
|
use common\models\Discount;
|
|
use common\models\Transfer;
|
|
use common\models\UserSoldItem;
|
|
use common\models\ShoppingCart;
|
|
use yii\base\Object;
|
|
|
|
/**
|
|
* @property $cart string name of cart, into we put the ticket
|
|
* @property $userCart common\models\Transfer[] items in user cart
|
|
* @property $customerCart common\models\Transfer[] items in customer cart
|
|
* @property $customer common\models\Customer selected customer
|
|
*
|
|
* */
|
|
class TicketCreate extends Ticket{
|
|
|
|
public $_currency;
|
|
public $_account;
|
|
public $_discount;
|
|
public $_transfer;
|
|
|
|
|
|
public $customer;
|
|
public $userCart;
|
|
public $customerCart;
|
|
|
|
|
|
public $cart;
|
|
|
|
public function rules()
|
|
{
|
|
return [
|
|
/////////////////////
|
|
//ticket type
|
|
/////////////////////
|
|
[[ 'id_ticket_type'], 'required'],
|
|
[[ 'id_ticket_type'], 'integer'],
|
|
[[ 'id_ticket_type'], 'validateTicketType'],
|
|
/////////////////////
|
|
//id_account
|
|
/////////////////////
|
|
[[ 'id_account'], 'required'],
|
|
[[ 'id_account'], 'integer'],
|
|
[[ 'id_account'], 'validateAccount'],
|
|
/////////////////////
|
|
//id_discount
|
|
/////////////////////
|
|
[[ 'id_discount'], 'integer'],
|
|
[[ 'id_discount'], 'validateDiscount'],
|
|
/////////////////////
|
|
// start and end date
|
|
/////////////////////
|
|
[['start', 'end' ], 'required'],
|
|
[['start', 'end' ], 'date'],
|
|
/////////////////////
|
|
//id_account
|
|
/////////////////////
|
|
[[ 'max_usage_count'], 'required'],
|
|
[[ 'max_usage_count'], 'integer'],
|
|
/////////////////////
|
|
//price
|
|
/////////////////////
|
|
[[ 'price_brutto'], 'required'],
|
|
[[ 'price_brutto'], 'integer'],
|
|
/////////////////////
|
|
//comment
|
|
/////////////////////
|
|
[['comment'], 'string', 'max' => 255],
|
|
/////////////////////
|
|
//cart
|
|
/////////////////////
|
|
[['cart'], 'string', 'max' => 10]
|
|
];
|
|
}
|
|
|
|
|
|
public function validateTicketType($attribute,$params){
|
|
$type = TicketType::findOne($this->id_ticket_type);
|
|
if ( !isset($type)) {
|
|
$this->addError($attribute,Yii::t('frontend/ticket' , 'Invalid ticket type' ));
|
|
}
|
|
}
|
|
|
|
public function validateAccount($attribute,$params){
|
|
$this->_account = Account::findOne($this->id_account);
|
|
if ( !isset($this->_account )) {
|
|
$this->addError($attribute,Yii::t('frontend/ticket' , 'Invalid transfer' ));
|
|
}
|
|
}
|
|
|
|
public function validateDiscount($attribute,$params){
|
|
$this->_discount = Discount::findOne($this->id_discount);
|
|
if ( !isset($this->_discount)) {
|
|
$this->addError($attribute,Yii::t('frontend/ticket' , 'Invalid discount' ));
|
|
}
|
|
}
|
|
|
|
public function afterSave($insert, $changedAttributes){
|
|
$this->addTransfer();
|
|
$this->appendToUserCart();
|
|
$this->appendToCustomerCart();
|
|
|
|
|
|
}
|
|
|
|
protected function addTransfer(){
|
|
$transfer = Transfer::createTicketTransfer($this->_account, $this->_discount, null, 1, $this);
|
|
|
|
$status = Transfer::STATUS_PAID;
|
|
if ( $this->isAppendToUserCart() ){
|
|
$status = Transfer::STATUS_NOT_PAID;
|
|
}else if ( $this->isAppendToCustomerCart() ){
|
|
$status = Transfer::STATUS_NOT_PAID;
|
|
$customer = $this->customer;
|
|
}else {
|
|
$status = Transfer::STATUS_PAID;
|
|
$transfer->paid_at = date('Y-m-d H:i:s' ) ;
|
|
}
|
|
$transfer->status = $status;
|
|
|
|
|
|
if ( isset($this->comment)){
|
|
$transfer->comment = $this->comment;
|
|
}
|
|
$transfer->id_user = \Yii::$app->user->id;
|
|
$transfer->id_customer = $this->customer->id_customer;
|
|
$transfer->save();
|
|
$this->_transfer = $transfer;
|
|
}
|
|
|
|
protected function addToCart(){
|
|
if ( $this->isAddToCart()){
|
|
$item = new UserSoldItem();
|
|
$item->id_transfer = $this->_transfer->id_transfer;
|
|
$item->id_user = \Yii::$app->user->id;
|
|
$item->save(false);
|
|
}
|
|
}
|
|
|
|
|
|
public function isAppendToUserCart(){
|
|
$result = false;
|
|
if ( isset( $this->cart ) && $this->cart == 'user' ){
|
|
$result = true;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
public function isAppendToCustomerCart(){
|
|
$result = false;
|
|
if ( isset( $this->cart ) && $this->cart == 'customer' ){
|
|
$result = true;
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
|
|
public function appendToUserCart(){
|
|
if ( $this->isAppendToUserCart() ){
|
|
$item = new UserSoldItem();
|
|
$item->id_transfer = $this->_transfer->id_transfer;
|
|
$item->id_user = \Yii::$app->user->id;
|
|
$item->save(false);
|
|
}
|
|
}
|
|
|
|
public function appendToCustomerCart(){
|
|
if ( $this->isAppendToCustomerCart() && isset($this->customer) ){
|
|
$item = new ShoppingCart();
|
|
$item->id_customer = $this->customer->id_customer;
|
|
$item->id_transfer = $this->_transfer->id_transfer;
|
|
$item->save(false);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
} |