[ 'class' => AccessControl::class, 'rules' => [ [ 'allow' => true, 'roles' => ['@'], 'matchCallback' => function ($rule, $action) { return $this->getUserModule() ->getAuthorizationChecker() ->isGrantedAsProducer($this->getUserCurrent()); } ] ], ], ]; } public function actionIndex() { $this->checkProductsPointsSale(); return $this->render('index'); } public function actionAjaxInit() { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $userModule = $this->getUserModule(); $pointSaleModule = $this->getPointSaleModule(); $distributionModule = $this-> getDistributionModule(); $usersArray = $userModule->findUsers(); $pointsSaleArray = $pointSaleModule->findPointSales(); $firstDistribution = $distributionModule->findOneFirstDistribution(); $lastDistribution = $distributionModule->findOneLastDistribution(); $firstYear = date('Y', strtotime($firstDistribution->date)); $lastYear = date('Y', strtotime($lastDistribution->date)); $distributionYearsArray = []; for ($year = $firstYear; $year <= $lastYear; $year++) { $distributionYearsArray[] = $year; } $distributionsByMonthArray = []; $distributionsArray = $distributionModule->findDistributionsWithOrders(); foreach ($distributionsArray as $distribution) { $month = date('Y-m', strtotime($distribution->date)); if (!isset($distributionsByMonthArray[$month])) { $distributionsByMonthArray[$month] = [ 'display' => 0, 'year' => date('Y', strtotime($distribution->date)), 'month' => strftime('%B', strtotime($distribution->date)), 'distributions' => [] ]; } $distribution->date = strftime('%A %d %B %Y', strtotime($distribution->date)); $distributionsByMonthArray[$month]['distributions'][] = $distribution; } return [ 'usersArray' => $usersArray, 'pointsSaleArray' => $pointsSaleArray, 'distributionYearsArray' => $distributionYearsArray, 'distributionsByMonthArray' => $distributionsByMonthArray ]; } public function actionAjaxReport() { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $posts = Yii::$app->request->post(); $resArray = []; $conditionUsers = $this->_generateConditionSqlReport($posts, 'users', 'id_user'); $conditionPointsSale = $this->_generateConditionSqlReport($posts, 'pointsSale', 'id_point_sale'); $conditionDistributions = $this->_generateConditionSqlReport($posts, 'distributions', 'id_distribution'); $res = Yii::$app->db->createCommand("SELECT product.name, SUM(product_order.quantity) AS quantity, SUM(product_order.price * product_order.quantity) AS total FROM `order`, product_order, product WHERE `order`.id = product_order.id_order AND product.id_producer = " . ((int)GlobalParam::getCurrentProducerId()) . " AND product_order.id_product = product.id AND ".OrderRepositoryQuery::getSqlFilterIsValid()." " . $conditionUsers . " " . $conditionPointsSale . " " . $conditionDistributions . " GROUP BY product.id ORDER BY product.order ASC ") ->queryAll(); $totalGlobal = 0; foreach ($res as $line) { $total = Price::format(round($line['total'], 2)); if ($line['quantity'] > 0) { $resArray[] = [ 'name' => $line['name'], 'quantity' => $line['quantity'], 'total' => $total, ]; $totalGlobal += $line['total']; } } $resArray[] = [ 'name' => '', 'quantity' => '', 'total' => '' . Price::format(round($totalGlobal, 2)) . ' HT', ]; return $resArray; } public function _generateConditionSqlReport($posts, $name, $fieldOrder) { $condition = ''; if (isset($posts[$name]) && strlen($posts[$name])) { $idsArray = explode(',', $posts[$name]); for ($i = 0; $i < count($idsArray); $i++) { $idsArray[$i] = (int)$idsArray[$i]; } $condition = 'AND `order`.' . $fieldOrder . ' IN (' . implode(',', $idsArray) . ') '; } return $condition; } public function actionPayments(string $dateStart, string $dateEnd) { $orderModule = $this->getOrderModule(); $ordersArray = $orderModule->getRepository() ->findOrdersByPeriod( new \DateTime($dateStart), new \DateTime($dateEnd) ); $datas = [ [ 'Date', 'Client', 'Montant', 'Facture', 'Crédit', 'Espèce', 'Chèque', 'Virement', 'Carte bancaire', 'Total' ] ]; $sumAmountTotalSpentByCredit = 0; $sumAmountTotalSpentByMoney = 0; $sumAmountTotalSpentByCheque = 0; $sumAmountTotalSpentByTransfer = 0; $sumAmountTotalSpentByCreditCard = 0; foreach($ordersArray as $order) { $orderModule->getBuilder()->initOrder($order); $hasInvoice = false; $referenceInvoice = 'Non'; if($order->invoice) { $hasInvoice = true; $referenceInvoice = $order->invoice->reference.""; } $amountTotalSpentByCredit = $orderModule->getSolver()->getAmountTotalSpentByMeanPayment($order, MeanPayment::CREDIT); $sumAmountTotalSpentByCredit += $amountTotalSpentByCredit; $amountTotalSpentByMoney = $orderModule->getSolver()->getAmountTotalSpentByMeanPayment($order, MeanPayment::MONEY); $sumAmountTotalSpentByMoney += $amountTotalSpentByMoney; $amountTotalSpentByCheque = $orderModule->getSolver()->getAmountTotalSpentByMeanPayment($order, MeanPayment::CHEQUE); $sumAmountTotalSpentByCheque += $amountTotalSpentByCheque; $amountTotalSpentByTransfer = $orderModule->getSolver()->getAmountTotalSpentByMeanPayment($order, MeanPayment::TRANSFER); $sumAmountTotalSpentByTransfer += $amountTotalSpentByTransfer; $amountTotalSpentByCreditCard = $orderModule->getSolver()->getAmountTotalSpentByMeanPayment($order, MeanPayment::CREDIT_CARD); $sumAmountTotalSpentByCreditCard += $amountTotalSpentByCreditCard; $datas[] = [ $order->distribution->date, $orderModule->getSolver()->getOrderUsername($order), CSV::formatNumber($orderModule->getSolver()->getOrderAmountWithTax($order)), $referenceInvoice, $hasInvoice ? '' : CSV::formatNumber($amountTotalSpentByCredit), $hasInvoice ? '' : CSV::formatNumber($amountTotalSpentByMoney), $hasInvoice ? '' : CSV::formatNumber($amountTotalSpentByCheque), $hasInvoice ? '' : CSV::formatNumber($amountTotalSpentByTransfer), $hasInvoice ? '' : CSV::formatNumber($amountTotalSpentByCreditCard) ]; } $datas[] = [ '', '', '', 'Totaux paiements', CSV::formatNumber($sumAmountTotalSpentByCredit), CSV::formatNumber($sumAmountTotalSpentByMoney), CSV::formatNumber($sumAmountTotalSpentByCheque), CSV::formatNumber($sumAmountTotalSpentByTransfer), CSV::formatNumber($sumAmountTotalSpentByCreditCard), CSV::formatNumber($sumAmountTotalSpentByCredit + $sumAmountTotalSpentByMoney + $sumAmountTotalSpentByCheque + $sumAmountTotalSpentByTransfer + $sumAmountTotalSpentByCreditCard) ]; CSV::send('commandes.csv', $datas); } }