|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
-
-
-
- namespace backend\controllers;
-
- use common\helpers\GlobalParam;
- use Yii;
- use yii\filters\AccessControl;
- use common\models\User;
- use common\models\Distribution ;
-
- class ReportController extends BackendController
- {
- var $enableCsrfValidation = false;
-
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::className(),
- 'rules' => [
- [
- 'allow' => true,
- 'roles' => ['@'],
- 'matchCallback' => function ($rule, $action) {
- return User::hasAccessBackend() ;
- }
- ]
- ],
- ],
- ];
- }
-
- public function actionIndex()
- {
- $this->checkProductsPointsSale() ;
- return $this->render('index') ;
- }
-
- public function actionAjaxInit()
- {
- \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
-
- $usersArray = User::findBy()->all() ;
- $pointsSaleArray = PointSale::searchAll() ;
-
-
- $firstDistribution = Distribution::searchOne([], [
- 'orderby' => 'id ASC'
- ]) ;
- $lastDistribution = Distribution::searchOne([], [
- 'orderby' => 'id DESC'
- ]) ;
-
- $firstYear = date('Y',strtotime($firstDistribution->date)) ;
- $lastYear = date('Y',strtotime($lastDistribution->date)) ;
- $distributionYearsArray = [] ;
- for($year = $firstYear; $year <= $lastYear; $year ++) {
- $distributionYearsArray[] = $year ;
- }
-
- $distributionsArray = Distribution::searchAll([
- 'distribution.active' => 1
- ], [
- 'orderby' => 'date ASC',
- ]) ;
- $distributionsByMonthArray = [] ;
- 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' => '<strong>'.Price::format(round($totalGlobal, 2)).'</strong>',
- ] ;
-
- 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 ;
- }
-
- }
|