namespace backend\controllers; | namespace backend\controllers; | ||||
use common\helpers\CSV; | |||||
use common\helpers\GlobalParam; | use common\helpers\GlobalParam; | ||||
use common\helpers\MeanPayment; | |||||
use common\helpers\Price; | use common\helpers\Price; | ||||
use Yii; | use Yii; | ||||
use yii\filters\AccessControl; | use yii\filters\AccessControl; | ||||
return $condition; | return $condition; | ||||
} | } | ||||
public function actionPayments(string $dateStart, string $dateEnd) | |||||
{ | |||||
$orderModule = $this->getOrderModule(); | |||||
$paymentModule = $this->getPaymentModule(); | |||||
$ordersArray = $orderModule->getRepository() | |||||
->findOrdersByPeriod( | |||||
new \DateTime($dateStart), | |||||
new \DateTime($dateEnd) | |||||
); | |||||
$datas = [ | |||||
[ | |||||
'Date', | |||||
'Client', | |||||
'Montant', | |||||
'Facture', | |||||
'Crédit', | |||||
'Espèce', | |||||
'Chèque', | |||||
'Virement', | |||||
'Carte bancaire', | |||||
'count paiements' | |||||
] | |||||
]; | |||||
foreach($ordersArray as $order) { | |||||
$orderModule->getBuilder()->initOrder($order); | |||||
$strPayment = ''; | |||||
foreach($order->payment as $payment) { | |||||
$strPayment .= $paymentModule->getSolver()->getStrMeanPayment($payment).', '; | |||||
} | |||||
$datas[] = [ | |||||
$order->distribution->date, | |||||
$orderModule->getSolver()->getOrderUsername($order), | |||||
$orderModule->getSolver()->getOrderAmountWithTax($order), | |||||
$order->invoice ? $order->invoice->reference : 'Non', | |||||
$orderModule->getSolver()->getAmountTotalByMeanPayment($order, MeanPayment::CREDIT), | |||||
$orderModule->getSolver()->getAmountTotalByMeanPayment($order, MeanPayment::MONEY), | |||||
$orderModule->getSolver()->getAmountTotalByMeanPayment($order, MeanPayment::CHEQUE), | |||||
$orderModule->getSolver()->getAmountTotalByMeanPayment($order, MeanPayment::TRANSFER), | |||||
$orderModule->getSolver()->getAmountTotalByMeanPayment($order, MeanPayment::CREDIT_CARD), | |||||
count($order->payment) . ' : '.$strPayment | |||||
]; | |||||
//print_r($order->payment); | |||||
//die(); | |||||
} | |||||
CSV::send('commandes.csv', $datas); | |||||
} | |||||
} | } |
return $query->find(); | return $query->find(); | ||||
} | } | ||||
public function findOrdersByPeriod(\DateTime $dateStart, \DateTime $dateEnd) | |||||
{ | |||||
$query = $this->createDefaultQuery() | |||||
->filterByDistributionDateStartEnd($dateStart, $dateEnd) | |||||
->filterIsValid() | |||||
->orderBy('distribution.date ASC'); | |||||
return $query->find(); | |||||
} | |||||
public function findOrdersByUserAndInvoice(User $user, Invoice $invoice) | public function findOrdersByUserAndInvoice(User $user, Invoice $invoice) | ||||
{ | { | ||||
return $this | return $this |
return $this; | return $this; | ||||
} | } | ||||
public function filterByDistributionDateStartEnd(\DateTime $dateStart, \DateTime $dateEnd): self | |||||
{ | |||||
$this | |||||
->andWhere('distribution.date >= \'' .$dateStart->format('Y-m-d').'\'') | |||||
->andWhere('distribution.date <= \'' .$dateEnd->format('Y-m-d').'\''); | |||||
return $this; | |||||
} | |||||
public function filterIsValid(): self | public function filterIsValid(): self | ||||
{ | { | ||||
$this->andWhere('date_delete IS NULL'); | $this->andWhere('date_delete IS NULL'); |
return ''; | return ''; | ||||
} | } | ||||
public function getAmountTotalByMeanPayment(Order $order, string $meanPayment) | |||||
{ | |||||
$amount = 0; | |||||
foreach($order->payment as $payment) { | |||||
if($this->paymentSolver->isMeanPayment($payment, $meanPayment)) { | |||||
$amount += $payment->getAmount(); | |||||
} | |||||
} | |||||
return $amount; | |||||
} | |||||
} | } |
return $payment->mean_payment == MeanPayment::CREDIT; | return $payment->mean_payment == MeanPayment::CREDIT; | ||||
} | } | ||||
public function isMeanPayment(Payment $payment, string $meanPayment): bool | |||||
{ | |||||
return $payment->mean_payment == $payment; | |||||
} | |||||
public function getStrMeanPayment(Payment $payment): string | public function getStrMeanPayment(Payment $payment): string | ||||
{ | { | ||||
return MeanPayment::getStrBy($payment->getMeanPayment()); | return MeanPayment::getStrBy($payment->getMeanPayment()); |