[ '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(); foreach($usersArray as $key => $user) { $usersArray[$key]['username'] = $userModule->getSolver()->getUsernameFromArray($user, true); } $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() { $posts = Yii::$app->request->post(); $isDownload = $posts['isDownload'] == 1; $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) { $roundedTotal = round($line['total'], 2); $total = $isDownload ? $roundedTotal : Price::format($roundedTotal); $quantity = $line['quantity']; if((int) $quantity != $quantity) { $quantity = round($quantity, 3); } if ($line['quantity'] > 0) { $resArray[] = [ 'name' => $line['name'], 'quantity' => $quantity, 'total' => $total, ]; $totalGlobal += $line['total']; } } $roundedTotalGlobal = round($totalGlobal, 2); $totalGlobalFormat = $isDownload ? $roundedTotalGlobal : Price::format($roundedTotalGlobal).' HT'; $resArray[] = [ 'name' => '', 'quantity' => '', 'total' => $totalGlobalFormat, ]; if($isDownload) { CSV::send('rapport.csv', $resArray); } else { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; 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() { $dateStart = $dateEnd = null; $reportPaymentsForm = new ReportPaymentsForm(); if($reportPaymentsForm->load(Yii::$app->request->post()) && $reportPaymentsForm->validate()) { $dateStart = date('Y-m-d', strtotime(str_replace('/', '-', $reportPaymentsForm->date_start))); $dateEnd = date('Y-m-d', strtotime(str_replace('/', '-', $reportPaymentsForm->date_end))); } if($dateStart && $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); } return $this->render('payments', [ 'reportPaymentsForm' => $reportPaymentsForm ]); } }