130 lines
3.9 KiB
PHP
130 lines
3.9 KiB
PHP
<?php
|
|
namespace common\components;
|
|
|
|
|
|
use common\models\Transfer;
|
|
use common\models\Account;
|
|
use yii\base\Exception;
|
|
use common\models\ShoppingCart;
|
|
use common\models\UserSoldItem;
|
|
use common\models\Ticket;
|
|
|
|
|
|
|
|
class TransferPayout extends \yii\base\Object{
|
|
|
|
/**Current user*/
|
|
public $idUser = null;
|
|
/**id of transfers which we want to payout*/
|
|
public $idTransfers = [];
|
|
/** The loaded transfers*/
|
|
public $transfers = [];
|
|
|
|
/**valamilyen kosár kifizetés ( user|customer ) ? */
|
|
public $cartType;
|
|
/**Vásárló kosár kifizetése esetén a vásárló azonosító*/
|
|
public $idCustomer;
|
|
|
|
/**Az alapértelmezett kassza!*/
|
|
public $idAccount;
|
|
|
|
/**Ha a fizetési módot meg szeretnél változtatni*/
|
|
public $overridePaymentMethod = null;
|
|
|
|
|
|
|
|
|
|
|
|
public function payout(){
|
|
|
|
$this->readTransfers();
|
|
|
|
|
|
if ( count($this->transfers ) ==0 ){
|
|
\Yii::error("TransferPayout - üres lista kifizetés");
|
|
throw new Exception("Nem található kifizethető tranzakció a listában. ");
|
|
}
|
|
|
|
|
|
if ( !$this->isTransferCountMatchWithRequestesTransferCount() ){
|
|
\Yii::error("TransferPayout - input tranzakció azonosítók száma nem egyezik a betöltött tranzakciók számával");
|
|
throw new Exception("A kifizetni kívánt tranzakciók száma nem egyezik a kifizehtető tranzakciók számával. ");
|
|
}
|
|
|
|
foreach ($this->transfers as $transfer){
|
|
$this->payoutTransfer($transfer);
|
|
}
|
|
}
|
|
|
|
public function readTransfers(){
|
|
$query = Transfer::find();
|
|
$query->andWhere([ 'in' , 'transfer.id_transfer' , $this->idTransfers ]);
|
|
$query->andWhere( ['transfer.status' => Transfer::STATUS_NOT_PAID]) ;
|
|
|
|
if ( Helper::isUserCartVisibilityUser()){
|
|
$query->innerJoin("user" ,"user.id = transfer.id_user");
|
|
}
|
|
|
|
$this->transfers = $query->all();
|
|
\Yii::info("TransferPayout - betöltött tranzakciók száma:" . count($this->transfers) );
|
|
}
|
|
|
|
/**
|
|
* Ellenőrizzük, hogy annyi tranzakciót töltöttünk e be, mint amennyit parameterként kaptunkk
|
|
* */
|
|
public function isTransferCountMatchWithRequestesTransferCount(){
|
|
return count($this->transfers) == count($this->idTransfers);
|
|
}
|
|
|
|
/**
|
|
* @param \common\models\Transfer $transfer the transfer to payout
|
|
* @throws Exception
|
|
*/
|
|
protected function payoutTransfer($transfer){
|
|
|
|
$transfer->paid_by = $this->idUser;
|
|
$transfer->status = Transfer::STATUS_PAID;
|
|
$transfer->paid_at = date('Y-m-d H:i:s' );
|
|
|
|
$account = $transfer->account;
|
|
|
|
if ( isset($account )){
|
|
/**Ha a tranzakció eredet kasszája látható típusu, akkor változtathatjuk az atuális kasszára. Különben nem*/
|
|
if ($account->type == Account::TYPE_ALL){
|
|
$transfer->id_account = Account::readDefault();
|
|
}
|
|
}
|
|
|
|
\Yii::info("fizetési mód: " . $this->overridePaymentMethod);
|
|
if ( isset($this->overridePaymentMethod ) && array_search($this->overridePaymentMethod, array_keys( Transfer::paymentMethods()) ) ){
|
|
\Yii::info("fizetési mód beállítva: " .$this->overridePaymentMethod );
|
|
$transfer->payment_method = $this->overridePaymentMethod;
|
|
}
|
|
|
|
if ( $transfer->save(false) == false ){
|
|
\Yii::error("Tranzakció kifizetése sikertelen volt: " . $transfer->id_transfer);
|
|
throw new Exception("Tranzakció fizetése sikertelen volt!");
|
|
}
|
|
|
|
if ( $transfer->type == Transfer::TYPE_TICKET){
|
|
$ticket = $transfer->ticket;
|
|
$ticket->status = Ticket::STATUS_ACTIVE;
|
|
|
|
if ( $ticket->save(false) == false ){
|
|
\Yii::error("Tranzakció kifizetése sikertelen volt: ");
|
|
throw new Exception("Tranzakció fizetése sikertelen volt!");
|
|
}
|
|
}
|
|
|
|
try{
|
|
ShoppingCart::deleteAll( ['id_transfer' => $transfer->id_transfer] );
|
|
UserSoldItem::deleteAll( ['id_transfer' => $transfer->id_transfer] );
|
|
}catch (\Exception $e){
|
|
\Yii::error("TransferPayout - tranzakció törlése a kosarakból sikertelen volt. Tranzakció: " . $transfer->id_transfer);
|
|
throw new Exception("Tranzakció fizetése sikertelen volt!");
|
|
}
|
|
|
|
}
|
|
|
|
|
|
} |