[ '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 `order`.date_delete IS NULL " . $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)) . '', ]; 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; } }