|
- <?php
-
-
-
- namespace backend\controllers;
-
- use backend\controllers\BackendController ;
- use common\models\Distribution ;
- use common\models\Product ;
- use common\models\Producer ;
- use common\models\Order ;
- use common\models\User ;
- use common\models\Subscription ;
- use common\helpers\Price ;
- use DateTime;
-
- class DistributionController extends BackendController
- {
- public function actionIndex($date = '')
- {
- $format = 'Y-m-d' ;
- $theDate = '' ;
- $dateObject = DateTime::createFromFormat($format, $date);
-
- if($dateObject && $dateObject->format($format) === $date) {
- $theDate = $date ;
- }
-
- return $this->render('index', [
- 'date' => $theDate
- ]) ;
- }
-
- public function actionAjaxInfos($date = '')
- {
- \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
-
- $json = [
- 'distribution' => [],
- 'products' => []
- ] ;
-
- $format = 'Y-m-d' ;
- $dateObject = DateTime::createFromFormat($format, $date);
-
- $distributionsArray = Distribution::searchAll([
- 'active' => 1
- ], [
- 'conditions' => ['date > :date_begin','date < :date_end'],
- 'params' => [':date_begin' => date('Y-m-d', strtotime('-1 month')), ':date_end' => date('Y-m-d', strtotime('+3 month')), ],
- ]) ;
- $json['distributions'] = $distributionsArray ;
-
- if($dateObject && $dateObject->format($format) === $date) {
-
- $distribution = Distribution::initDistribution($date) ;
- $json['distribution'] = [
- 'id' => $distribution->id,
- 'active' => $distribution->active,
- 'url_report' => Yii::$app->urlManagerBackend->createUrl(['distribution/report','date' => $distribution->date])
- ] ;
-
-
- $ordersArray = Order::searchAll([
- 'distribution.date' => $date,
- ],[
- 'orderby' => 'user.lastname ASC, user.name ASC'
- ]);
-
-
- $revenues = 0;
- $weight = 0 ;
- if($ordersArray) {
- foreach ($ordersArray as $order) {
- if(is_null($order->date_delete)) {
- $revenues += $order->amount;
- $weight += $order->weight;
- }
- }
- }
- $json['distribution']['revenues'] = Price::format($revenues);
- $json['distribution']['weight'] = number_format($weight, 2);
-
-
- $productsArray = Product::find()
- ->orWhere(['id_producer' => Producer::getId(),])
- ->orWhere(['id_producer' => 0,])
- ->joinWith(['productDistribution' => function($query) use($distribution) {
- $query->andOnCondition('product_distribution.id_distribution = '.$distribution->id) ;
- }])
- ->orderBy('product_distribution.active DESC, order ASC')
- ->asArray()
- ->all();
-
- $potentialRevenues = 0;
- $potentialWeight = 0;
-
- foreach($productsArray as &$product) {
- $quantityOrder = Order::getProductQuantity($product['id'], $ordersArray) ;
- $product['quantity_ordered'] = $quantityOrder ;
- $product['quantity_remaining'] = $product['quantity_max'] - $quantityOrder ;
- if($product['quantity_remaining'] < 0) $product['quantity_remaining'] = 0 ;
- $product['quantity_form'] = 0 ;
-
- if($product['productDistribution'][0]['active'] && $product['productDistribution'][0]['quantity_max']) {
- $potentialRevenues += $product['productDistribution'][0]['quantity_max'] * $product['price'];
- $potentialWeight += $product['productDistribution'][0]['quantity_max'] * $product['weight'] / 1000;
- }
- }
-
- $json['distribution']['potential_revenues'] = Price::format($potentialRevenues) ;
- $json['distribution']['potential_weight'] = number_format($potentialWeight, 2) ;
-
- $json['products'] = $productsArray ;
-
-
- if($ordersArray) {
- foreach($ordersArray as &$order) {
- $productOrderArray = \yii\helpers\ArrayHelper::map($order->productOrder, 'id_product', 'quantity') ;
- foreach($productsArray as $product) {
- if(!isset($productOrderArray[$product['id']])) {
- $productOrderArray[$product['id']] = 0 ;
- }
- }
-
- $creditHistoryArray = [] ;
-
- foreach($order->creditHistory as $creditHistory) {
- $creditHistoryArray[] = [
- 'date' => date('d/m/Y H:i:s', strtotime($creditHistory->date)),
- 'user_action' => $creditHistory->strUserAction(),
- 'wording' => $creditHistory->getStrWording(),
- 'debit' => ($creditHistory->isTypeDebit() ? '- '.$creditHistory->getAmount(Order::AMOUNT_TOTAL,true) : ''),
- 'credit' => ($creditHistory->isTypeCredit() ? '+ '.$creditHistory->getAmount(Order::AMOUNT_TOTAL,true) : '')
- ] ;
- }
-
- $arrayCreditUser = [] ;
- if(isset($order->user) && isset($order->user->userProducer)) {
- $arrayCreditUser['credit'] = $order->user->userProducer[0]->credit ;
- }
-
- $order = array_merge($order->getAttributes(), [
- 'amount' => $order->getAmount(Order::AMOUNT_TOTAL),
- 'amount_paid' => $order->getAmount(Order::AMOUNT_PAID),
- 'amount_remaining' => $order->getAmount(Order::AMOUNT_REMAINING),
- 'amount_surplus' => $order->getAmount(Order::AMOUNT_SURPLUS),
- 'user' => (isset($order->user)) ? array_merge($order->user->getAttributes(), $arrayCreditUser) : null,
- 'pointSale' => ['id' => $order->pointSale->id, 'name' => $order->pointSale->name],
- 'productOrder' => $productOrderArray,
- 'creditHistory' => $creditHistoryArray
- ]) ;
-
-
- }
- }
-
- $json['orders'] = $ordersArray ;
-
-
- $productOrderArray = [] ;
- foreach($productsArray as $product) {
- $productOrderArray[$product['id']] = 0 ;
- }
- $json['order_create'] = [
- 'id_point_sale' => 0,
- 'id_user' => 0,
- 'username' => '',
- 'comment' => '',
- 'productOrder' => $productOrderArray
- ] ;
-
-
- $pointsSaleArray = PointSale::find()
- ->joinWith(['pointSaleDistribution' => function($q) use ($distribution) {
- $q->where(['id_distribution' => $distribution->id]);
- }])
- ->where([
- 'id_producer' => Producer::getId(),
- ])
- ->asArray()
- ->all();
-
- $json['points_sale'] = $pointsSaleArray ;
-
-
- $usersArray = User::findBy()->all() ;
- $json['users'] = $usersArray ;
-
-
- $oneDistributionWeekActive = false ;
- $week = sprintf('%02d',date('W',strtotime($date)));
- $start = strtotime(date('Y',strtotime($date)).'W'.$week);
- $dateMonday = date('Y-m-d',strtotime('Monday',$start)) ;
- $dateTuesday = date('Y-m-d',strtotime('Tuesday',$start)) ;
- $dateWednesday = date('Y-m-d',strtotime('Wednesday',$start)) ;
- $dateThursday = date('Y-m-d',strtotime('Thursday',$start)) ;
- $dateFriday = date('Y-m-d',strtotime('Friday',$start)) ;
- $dateSaturday = date('Y-m-d',strtotime('Saturday',$start)) ;
- $dateSunday = date('Y-m-d',strtotime('Sunday',$start)) ;
-
- $weekDistribution = Distribution::find()
- ->andWhere([
- 'id_producer' => Producer::getId(),
- 'active' => 1,
- ])
- ->andWhere(['or',
- ['date' => $dateMonday],
- ['date' => $dateTuesday],
- ['date' => $dateWednesday],
- ['date' => $dateThursday],
- ['date' => $dateFriday],
- ['date' => $dateSaturday],
- ['date' => $dateSunday],
- ])
- ->one();
- if($weekDistribution) {
- $oneDistributionWeekActive = true ;
- }
-
- $json['one_distribution_week_active'] = $oneDistributionWeekActive ;
-
- }
-
- return $json ;
- }
-
-
-
- public function actionReport($date = '', $save = false, $idProducer = 0)
- {
- if (!Yii::$app->user->isGuest) {
- $idProducer = Producer::getId() ;
- }
-
- $ordersArray = Order::searchAll([
- 'distribution.date' => $date,
- ],
- [
- 'orderby' => 'comment_point_sale ASC, user.name ASC',
- 'conditions' => 'date_delete IS NULL'
- ]) ;
-
- $distribution = Distribution::searchOne([],[
- 'conditions' => 'date LIKE :date',
- 'params' => [':date' => $date]
- ]) ;
-
- if ($distribution) {
- $selectedProductsArray = ProductDistribution::searchByDistribution($distribution->id) ;
- $pointsSaleArray = PointSale::searchAll() ;
-
- foreach ($pointsSaleArray as $pointSale) {
- $pointSale->initOrders($ordersArray) ;
- }
-
-
- $productsArray = Product::searchAll() ;
-
-
- $content = $this->renderPartial('report', [
- 'date' => $date,
- 'distribution' => $distribution,
- 'selectedProductsArray' => $selectedProductsArray,
- 'pointsSaleArray' => $pointsSaleArray,
- 'productsArray' => $productsArray,
- 'ordersArray' => $ordersArray
- ]);
-
- $dateStr = date('d/m/Y', strtotime($date));
-
- if ($save) {
- $destination = Pdf::DEST_FILE;
- } else {
- $destination = Pdf::DEST_BROWSER;
- }
-
- $pdf = new Pdf([
-
- 'mode' => Pdf::MODE_UTF8,
-
- 'format' => Pdf::FORMAT_A4,
-
- 'orientation' => Pdf::ORIENT_PORTRAIT,
-
- 'destination' => $destination,
- 'filename' => Yii::getAlias('@app/web/pdf/Commandes-' . $date . '-' . $idProducer . '.pdf'),
-
- 'content' => $content,
-
-
-
-
-
-
-
-
- 'methods' => [
- 'SetHeader' => ['Commandes du ' . $dateStr],
- 'SetFooter' => ['{PAGENO}'],
- ]
- ]);
-
-
- return $pdf->render();
- }
-
- return null ;
- }
-
- public function actionAjaxProcessProductQuantityMax($idDistribution, $idProduct, $quantityMax)
- {
- \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
-
- $productDistribution = ProductDistribution::searchOne([
- 'id_distribution' => $idDistribution,
- 'id_product' => $idProduct,
- ]) ;
-
- $productDistribution->quantity_max = (!$quantityMax) ? null : (int) $quantityMax ;
-
- $productDistribution->save() ;
-
- return ['success'] ;
- }
-
- public function actionAjaxProcessActiveProduct($idDistribution, $idProduct, $active)
- {
- \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
-
- $productDistribution = ProductDistribution::searchOne([
- 'id_distribution' => $idDistribution,
- 'id_product' => $idProduct,
- ]) ;
- $productDistribution->active = $active ;
- $productDistribution->save() ;
-
- return ['success'] ;
- }
-
- public function actionAjaxProcessActivePointSale($idDistribution, $idPointSale, $delivery)
- {
- \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
-
- $pointSaleDistribution = PointSaleDistribution::searchOne([
- 'id_distribution' => $idDistribution,
- 'id_point_sale' => $idPointSale,
- ]) ;
- $pointSaleDistribution->delivery = $delivery ;
- $pointSaleDistribution->save() ;
-
- return ['success'] ;
- }
-
-
-
- public function actionAjaxProcessActiveDistribution($idDistribution = 0, $date = '', $active)
- {
- \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
-
- if($idDistribution) {
- $distribution = Distribution::searchOne([
- 'id' => $idDistribution
- ]) ;
- }
-
- $format = 'Y-m-d' ;
- $dateObject = DateTime::createFromFormat($format, $date);
- if($dateObject && $dateObject->format($format) === $date) {
- $distribution = Distribution::initDistribution($date) ;
-
- }
-
- if($distribution) {
- $distribution->active = (int) $active ;
- $distribution->save() ;
-
- if ($active) {
-
- Subscription::addAll($distribution->date);
- }
-
- return ['success'] ;
- }
-
- return ['error'] ;
- }
-
-
-
- public function actionAjaxProcessActiveWeekDistribution($date, $active)
- {
- $week = sprintf('%02d',date('W',strtotime($date)));
- $start = strtotime(date('Y',strtotime($date)).'W'.$week);
- $dateMonday = date('Y-m-d',strtotime('Monday',$start)) ;
- $dateTuesday = date('Y-m-d',strtotime('Tuesday',$start)) ;
- $dateWednesday = date('Y-m-d',strtotime('Wednesday',$start)) ;
- $dateThursday = date('Y-m-d',strtotime('Thursday',$start)) ;
- $dateFriday = date('Y-m-d',strtotime('Friday',$start)) ;
- $dateSaturday = date('Y-m-d',strtotime('Saturday',$start)) ;
- $dateSunday = date('Y-m-d',strtotime('Sunday',$start)) ;
-
- $pointsSaleArray = PointSale::searchAll() ;
-
- $activeMonday = false ;
- $activeTuesday = false ;
- $activeWednesday = false ;
- $activeThursday = false ;
- $activeFriday = false ;
- $activeSaturday = false ;
- $activeSunday = false ;
-
- foreach($pointsSaleArray as $pointSale) {
- if($pointSale->delivery_monday) $activeMonday = true ;
- if($pointSale->delivery_tuesday) $activeTuesday = true ;
- if($pointSale->delivery_wednesday) $activeWednesday = true ;
- if($pointSale->delivery_thursday) $activeThursday = true ;
- if($pointSale->delivery_friday) $activeFriday = true ;
- if($pointSale->delivery_saturday) $activeSaturday = true ;
- if($pointSale->delivery_sunday) $activeSunday = true ;
- }
-
- if($activeMonday || !$active) {
- $this->actionAjaxProcessActiveDistribution(0, $dateMonday, $active) ;
- }
- if($activeTuesday || !$active) {
- $this->actionAjaxProcessActiveDistribution(0, $dateTuesday, $active) ;
- }
- if($activeWednesday || !$active) {
- $this->actionAjaxProcessActiveDistribution(0, $dateWednesday, $active) ;
- }
- if($activeThursday || !$active) {
- $this->actionAjaxProcessActiveDistribution(0, $dateThursday, $active) ;
- }
- if($activeFriday || !$active) {
- $this->actionAjaxProcessActiveDistribution(0, $dateFriday, $active) ;
- }
- if($activeSaturday || !$active) {
- $this->actionAjaxProcessActiveDistribution(0, $dateSaturday, $active) ;
- }
- if($activeSunday || !$active) {
- $this->actionAjaxProcessActiveDistribution(0, $dateSunday, $active) ;
- }
-
- return ['success'] ;
- }
-
- }
|