[ 'class' => AccessControl::className(), 'rules' => [ [ 'actions' => ['report-cron'], 'allow' => true, 'roles' => ['?'] ], [ 'allow' => true, 'roles' => ['@'], 'matchCallback' => function ($rule, $action) { return User::getStatus() == USER::STATUS_ADMIN || User::getStatus() == USER::STATUS_PRODUCER; } ] ], ], ]; } /** * Génére un PDF récapitulatif des commandes d'un producteur pour une * date donnée. * * @param string $date * @param boolean $save * @param integer $id_etablissement * @return PDF|null */ public function actionReport($date = '', $save = false, $idProducer = 0) { if (!Yii::$app->user->isGuest) { $idProducer = Producer::getId() ; } $arrayOrders = Order::searchAll([ 'date' => $date, 'date_delete' => 'NULL' ], [ 'orderby' => 'comment_point_sale ASC, user.name ASC' ]) ; $distribution = Distribution::searchOne([],[ 'conditions' => 'date LIKE :date', 'params' => [':date' => $date] ]) ; if ($distribution) { $arraySelectedProducts = ProductDistribution::searchProducts($distribution->id) ; $arrayPointsSale = PointSale::searchAll() ; foreach ($arrayPointsSale as $pointSale) { $pointSale->initOrders($orders) ; } // produits $arrayProducts = Product::searchAll() ; // get your HTML raw content without any layouts or scripts $content = $this->renderPartial('report', [ 'distribution' => $distribution, 'date' => $date, 'arraySelectedProducts' => $arraySelectedProducts, 'arrayPointsSale' => $arrayPointsSale, 'arrayProducts' => $arrayProducts, 'arrayOrders' => $arrayOrders ]); $dateStr = date('d/m/Y', strtotime($date)); if ($save) { $destination = Pdf::DEST_FILE; } else { $destination = Pdf::DEST_BROWSER; } $pdf = new Pdf([ // set to use core fonts only 'mode' => Pdf::MODE_UTF8, // A4 paper format 'format' => Pdf::FORMAT_A4, // portrait orientation 'orientation' => Pdf::ORIENT_PORTRAIT, // stream to browser inline 'destination' => $destination, 'filename' => Yii::getAlias('@app/web/pdf/Commandes-' . $date . '-' . $idProducert . '.pdf'), // your html content input 'content' => $content, // format content from your own css file if needed or use the // enhanced bootstrap css built by Krajee for mPDF formatting //'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css', // any css to be embedded if required //'cssInline' => '.kv-heading-1{font-size:18px}', // set mPDF properties on the fly //'options' => ['title' => 'Krajee Report Title'], // call mPDF methods on the fly 'methods' => [ 'SetHeader' => ['Commandes du ' . $dateStr], 'SetFooter' => ['{PAGENO}'], ] ]); // return the pdf output as per the destination setting return $pdf->render(); } return null ; } /** * Supprime une commande. * * @param string $date * @param integer $id_commande */ public function actionDeleteOrder($date, $idOrder) { $order = Order::searchOne(['id' =>$idOrder]) ; if ($order) { // remboursement de la commande if ($order->id_user && $order->getAmount(Order::AMOUNT_PAID) && Producer::getConfig('credit')) { $order->creditHistory( CreditHistory::TYPE_REFUND, $order->getAmount(Order::AMOUNT_PAID), $order->distribution->id_producer, $order->id_user, User::getId() ); } $order->delete(); ProductOrder::deleteAll(['id_order' => $idOrder]); } $this->redirect(['index', 'date' => $date]); } /** * Traite le formulaire d'ajout/modification de commande. * * @param Production $production * @param string $date * @param array $points_vente * @param array $produits * @param array $users */ public function processOrderForm( $distribution, $date, $pointsSale, $products, $users) { if ($date != '') { // commandes $orders = Order::searchAll([ 'production.date' => $date ]) ; foreach ($pointsSale as $point) { $point->initOrder($orders); if (isset($_POST['submit_pv']) && $_POST['submit_pv']) { // modifs foreach ($point->orders as $order) { // suppression des product_order ProductOrder::deleteAll(['id_order' => $order->id]) ; // création des commande_produit modifiés foreach ($products as $product) { $quantity = Yii::$app->getRequest()->post('product_' . $point->id . '_' . $product->id, 0); if ($quantity) { $productOrder = new ProductOrder; $productOrder->id_order = $order->id; $productOrder->id_product = $product->id; $productOrder->quantity = $quantity; $productOrder->price = $p->price; $productOrder->save(); } } } $username = Yii::$app->getRequest()->post('username_point_sale_' . $point->id, 0); $date = Yii::$app->getRequest()->post('date_order_point_sale_' . $point->id, 0); $oneProduct = false; foreach ($products as $product) { $quantity = Yii::$app->getRequest()->post('product_point_sale_' . $point->id . '_' . $product->id, 0); if ($quantity) { $oneProduct = true; } } if (strlen($username) && $date && $oneProduct) { $order = new Order; $order->id_point_sale = $point->id; $order->id_production = $distribution->id; $order->id_user = 0; $order->username = $username; $arrayDate = explode('/', $date); $order->date = $arrayDate[2] . '-' . $arrayDate[1] . '-' . $arrayDate[0] . ' 00:00:00'; $order->save(); foreach ($products as $product) { $quantity = Yii::$app->getRequest()->post('product_point_sale_' . $point->id . '_' . $product->id, 0); if ($quantity) { $productOrder = new ProductOrder; $productOrder->id_order = $order->id; $productOrder->id_product = $product->id; $productOrder->quantity = $quantity; $productOrder->price = $p->price; $productOrder->save(); } } } } } } } /** * Page principale de la gestion des commandes. * * @param string $date * @param boolean $returnData * @return string */ public function actionIndex($date = '', $returnData = false) { if (!Product::count() && !PointSale::count()) { $this->redirect(['site/index', 'error_products_points_sale' => 1]); } $orders = []; // users $arrayUsers = [0 => '--']; $users = User::searchAll([],['orderby' => 'lastname, name ASC']) ; foreach ($users as $user) { $arrayUsers[$user->id] = $user->name . ' ' . $user->lastname; } // création du jour de distribution $distribution = Distribution::initDistribution($date) ; // points de vente if ($distribution) { $arrayPointsSale = PointSale::find() ->joinWith(['pointSaleDistribution' => function($q) use ($distribution) { $q->where(['id_distribution' => $distribution->id]); }]) ->where([ 'id_producer' => Producer::getId(), ]) ->all(); } else { $arrayPointsSale = PointSale::searchAll() ; } // produits $arrayProducts = Product::searchAll(); // gestion des commandes $this->processOrderForm($distribution, $date, $arrayPointsSale, $arrayProducts, $users); // commandes $arrayOrders = Commande::searchAll([ 'date' => $date, 'date_delete' => 'NULL' ]); $revenues = 0; $weight = 0 ; $revenuesDelivered = 0; foreach ($arrayOrders as $order) { if(is_null($order->date_delete)) { $revenues += $order->amount; if ($order->id_point_sale != 1) { $revenuesDelivered += $order->amount; } $weight += $order->weight; } } $revenues = number_format($revenues, 2); // init commandes point de vente foreach ($arrayPointsSale as $pointSale) { $pointSale->initOrders($arrayOrders); $dataSelectOrders = []; $dataOptionsOrders = []; foreach ($pointSale->orders as $order) { if ($order->user) { $dataSelectOrders[$order->id] = $order->user->name . ' ' . $c->user->lastname; } else { $dataSelectOrders[$order->id] = $order->username; } $dataOptionsOrders[$order->id] = []; $arrayOptions = []; $arrayOptions[$order->id]['amount'] = $c->montant; $arrayOptions[$order->id]['str_amount'] = number_format($c->montant, 2, ',', '') . ' €'; $arrayOptions[$order->id]['amount_paid'] = $c->montant_paye; $arrayOptions[$order->id]['products'] = []; $arrayOptions[$order->id]['comment'] = Html::encode($c->commentaire); foreach ($order->productOrder as $productOrder) { $arrayOptions[$order->id]['products'][$productOrder->id_product] = $productOrder->quantity; } $dataOptionsOrders[$order->id]['data-commande'] = json_encode($arrayOptions[$order->id]); $dataOptionsOrders[$order->id]['value'] = $order->id; } $pointSale->data_select_orders = $dataSelectOrders ; $pointSale->data_options_orders = $dataOptionsOrders ; } // gestion produits selec if (isset($_POST['validate_product_selec'])) { if (isset($_POST['Product'])) { foreach ($arrayProducts as $product) { $productDistribution = ProductDistribution::searchOne([ 'id_distribution' => $distribution->id, 'id_product' => $product->id ]) ; if (!$productDistribution) { $productDistribution = new ProductDistribution(); $productDistribution->id_distribution = $distribution->id; $productDistribution->id_product = $product->id; $productDistribution->active = 0; if (isset($product->quantity_max)) { $productDistribution->quantity_max = $product->quantity_max; } else { $productDistribution->quantity_max = null; } $productDistribution->save(); } if (isset($_POST['Product'][$product->id]['active'])) { $productDistribution->active = 1; } else { $productDistribution->active = 0; } if ((isset($_POST['Product'][$product->id]['quantity_max']) && $_POST['Product'][$product->id]['quantity_max'] != '')) { $productDistribution->quantity_max = (int) $_POST['Product'][$product->id]['quantity_max']; } else { $productDistribution->quantity_max = null; } $productDistribution->save(); } } } $arrayProductsSelected = [] ; if($distribution) { // produits selec pour production $arrayProductsSelected = ProductDistribution::searchByDistribution($distribution->id) ; } // produits if ($distribution) { $arrayProducts = Product::searchByDistribution($distribution->id) ; } // poids total de la production et CA potentiel $potentialTurnover = 0; $totalWeight = 0; foreach ($arrayProductsSelected as $idSelectedProduct => $selectedProduct) { if ($selectedProduct['active']) { foreach ($arrayProducts as $product) { if ($product->id == $idSelectedProduct) { $potentialTurnover += $selectedProduct['quantity_max'] * $product->price; $totalWeight += $selectedProduct['quantity_max'] * $product->weight / 1000; } } } } // jours de distribution $arrayDistributionDays = Distribution::searchAll([ 'active' => 1 ]) ; // commandes auto $subscriptionForm = new SubscriptionForm; // productions point vente $pointSaleDistribution = new PointSaleDistribution; $oointsSaleDistribution = []; if ($distribution) { $oointsSaleDistribution = PointSaleDistribution::searchAll() ; } $arrayPointsSaleDistribution = []; foreach ($arrayPointsSaleDistribution as $pointSaleDistribution) { $key = $pointSaleDistribution->id_distribution . '-' . $pointSaleDistribution->id_point_sale; if ($pointSaleDistribution->delivery == 1) { $pointSaleDistribution->points_sale_distribution[] = $key; } if(isset($pointSaleDistribution->pointSale) && strlen($pointSaleDistribution->pointSale->name)) { $arrayPointsSaleDistribution[$key] = Html::encode($pointSaleDistribution->pointSale->name); } } // une production de la semaine activée ou non $oneDistributionWeekActive = false ; $week = sprintf('%02d',date('W',strtotime($date))); $start = strtotime(date('Y',strtotime($date)).'W'.$week); $date_lundi = date('Y-m-d',strtotime('Monday',$start)) ; $date_mardi = date('Y-m-d',strtotime('Tuesday',$start)) ; $date_mercredi = date('Y-m-d',strtotime('Wednesday',$start)) ; $date_jeudi = date('Y-m-d',strtotime('Thursday',$start)) ; $date_vendredi = date('Y-m-d',strtotime('Friday',$start)) ; $date_samedi = date('Y-m-d',strtotime('Saturday',$start)) ; $date_dimanche = date('Y-m-d',strtotime('Sunday',$start)) ; $production_semaine = Distribution::find() ->andWhere([ 'id_producer' => Producer::getId(), 'active' => 1, ]) ->andWhere(['or', ['date' => $date_lundi], ['date' => $date_mardi], ['date' => $date_mercredi], ['date' => $date_jeudi], ['date' => $date_vendredi], ['date' => $date_samedi], ['date' => $date_dimanche], ]) ->one(); if($production_semaine) { $oneDistributionWeekActive = true ; } $datas = [ 'products' => $arrayProducts, 'pointsSale' => $arrayPointsSale, 'orders' => $arrayOrders, 'date' => $date, 'distribution' => $distribution, 'distributionDays' => $arrayDistributionDays, 'selectedProducts' => $arrayProductsSelected, 'users' => $arrayUsers, 'revenues' => $revenues, 'revenuesDelivered' => $revenuesDelivered, 'weight' => $weight, 'potentialTurnover' => $potentialTurnover, 'totalWeight' => $totalWeight, 'subscriptionForm' => $subscriptionForm, 'pointSaleDistribution' => $pointSaleDistribution, 'arrayPointSaleDistribution' => $arrayPointsSaleDistribution, 'arrayPointsSaleDistribution' => $arrayPointsSaleDistribution, 'oneDistributionWeekActive' => $oneDistributionWeekActive ]; if ($return_data) { return $datas; } else { return $this->render('index', $datas); } } /** * Génère un fichier d'export des commandes au format CSV. * * @param string $date * @param integer $id_point_vente * @param boolean $global */ public function actionDownload($date = '', $id_point_vente = 0, $global = 0) { // commandes $commandes = Order::searchAll([ 'production.date' => $date ]) ; foreach ($commandes as $c) $c->init(); // points de vente $points_vente = PointSale::searchAll() ; foreach ($points_vente as $pv) $pv->initOrders($commandes); // produits $produits = Product::find()->orderBy('order ASC')->all(); $production = Distribution::find()->where('date LIKE \'' . $date . '\'')->one(); $produits_selec = ProductDistribution::searchByDistribution($production->id); /* * export global */ if ($global) { $data = []; $filename = 'export_' . $date . '_global'; $num_jour_semaine = date('w', strtotime($date)); $arr_jour_semaine = [0 => 'sunday', 1 => 'monday', 2 => 'tuesday', 3 => 'wednesday', 4 => 'thursday', 5 => 'friday', 6 => 'saterday']; $champs_horaires_point_vente = 'infos_' . $arr_jour_semaine[$num_jour_semaine]; // par point de vente foreach ($points_vente as $pv) { if (count($pv->orders) && strlen($pv->$champs_horaires_point_vente)) { $line = [$pv->nom, 'Produits', 'Montant', 'Commentaire']; $data[] = $line; $res = $this->contentPointSaleCSV($date, $produits, $points_vente, $pv->id); foreach ($res['data'] as $line) { $data[] = $line; } } if (count($pv->orders) && strlen($pv->$champs_horaires_point_vente)) { $line = ['Total pain']; $str_produits = ''; foreach ($produits as $p) { if (!$p->vrac && isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) { $quantite = Order::getProductQuantity($p->id, $pv->commandes); $str_quantite = ''; if ($quantite) { $str_quantite = $quantite; $str_produits .= $str_quantite . $p->diminutif . ', '; } } } $line[] = substr($str_produits, 0, strlen($str_produits) - 2); $line[] = number_format($pv->revenues, 2) . ' €'; $data[] = $line; $line = ['Total vrac']; $str_produits = ''; foreach ($produits as $p) { if ($p->vrac && isset($produits_selec[$p->id]['active']) && $produits_selec[$p->id]['active']) { $quantite = Order::getProductQuantity($p->id, $pv->orders); $str_quantite = ''; if ($quantite) { $str_quantite = $quantite; $str_produits .= $str_quantite . $p->diminutif . ', '; } } } $line[] = substr($str_produits, 0, strlen($str_produits) - 2); $line[] = number_format($pv->revenues, 2) . ' €'; $data[] = $line; $data[] = []; } } $line = ['Total pain']; $str_produits = ''; foreach ($produits as $p) { if (!$p->vrac && isset($produits_selec[$p->id]['active']) && $produits_selec[$p->id]['active']) { $quantite = Order::getProductQuantity($p->id, $commandes); $str_quantite = ''; if ($quantite) { $str_quantite = $quantite; $str_produits .= $str_quantite . '' . $p->diminutif . ', '; } } } $line[] = substr($str_produits, 0, strlen($str_produits) - 2); $data[] = $line; // vrac $line = ['Total vrac']; $str_produits = ''; foreach ($produits as $p) { if ($p->vrac && isset($produits_selec[$p->id]['active']) && $produits_selec[$p->id]['active']) { $quantite = Order::getProductQuantity($p->id, $commandes); $str_quantite = ''; if ($quantite) { $str_quantite = $quantite; $str_produits .= $str_quantite . '' . $p->diminutif . ', '; } } } $line[] = substr($str_produits, 0, strlen($str_produits) - 2); $data[] = $line; $infos = $this->actionIndex($date, true); CSV::downloadSendHeaders($filename . '.csv'); echo CSV::array2csv($data); die(); } /* * export individuel */ else { if ($commandes && count($commandes)) { $data = []; // par point de vente if ($id_point_vente) { $res = $this->contentPointSaleCSV($date, $produits, $points_vente, $id_point_vente); $data = $res['data']; $filename = $res['filename']; } // récapitulatif else { $res = $this->contentRecapCSV($date, $produits, $points_vente, $commandes); $filename = 'recapitulatif_' . $date; $data = $res['data']; } CSV::downloadSendHeaders($filename . '.csv'); echo CSV::array2csv($data); die(); } } } /** * Génère le contenu nécessaire aux exports au format CSV. * * @see CommandeController::actionDownload() * @param string $date * @param array $produits * @param array $points_vente * @param array $commandes * @return array */ public function contentRecapCSV($date, $produits, $points_vente, $commandes) { $data = []; $filename = 'recapitulatif_' . $date; $production = Distribution::find()->where('date LIKE \'' . $date . '\'')->one(); $produits_selec = ProductDistribution::searchByDistribution($production->id); // head $data[0] = ['Lieu']; foreach ($produits as $p) { if (isset($produits_selec[$p->id]['active']) && $produits_selec[$p->id]['active']) { $data[0][] = $p->description; } } $num_jour_semaine = date('w', strtotime($date)); $arr_jour_semaine = [0 => 'sunday', 1 => 'monday', 2 => 'tuesday', 3 => 'wednesday', 4 => 'thursday', 5 => 'friday', 6 => 'saterday']; $champs_horaires_point_vente = 'infos_' . $arr_jour_semaine[$num_jour_semaine]; // datas foreach ($points_vente as $pv) { if (count($pv->orders) && strlen($pv->$champs_horaires_point_vente)) { $data_add = [$pv->name]; foreach ($produits as $p) { if (isset($produits_selec[$p->id]['active']) && $produits_selec[$p->id]['active']) { $data_add[] = Order::getProductQuantity($p->id, $pv->orders); } } $data[] = $data_add; } } $data_add = ['Total']; foreach ($produits as $p) { if (isset($produits_selec[$p->id]['active']) && $produits_selec[$p->id]['active']) { $data_add[] = Order::getProductQuantity($p->id, $commandes); } } $data[] = $data_add; return [ 'data' => $data, 'filename' => $filename ]; } /** * Génère le contenu relatif aux points de vente nécessaires aux exports au * format CSV. * * @param string $date * @param array $produits * @param array $points_vente * @param integer $id_point_vente * @return array */ public function contentPointSaleCSV($date, $produits, $points_vente, $id_point_vente) { $data = []; $production = Distribution::find()->where('date LIKE \'' . $date . '\'')->one(); $produits_selec = ProductDistribution::searchByDistribution($production->id); // head /* $data[0] = ['Client', 'Date commande'] ; foreach($produits as $p) { if(isset($produits_selec[$p->id]['actif']) && $produits_selec[$p->id]['actif']) { $data[0][] = $p->description ; } } */ // datas foreach ($points_vente as $pv) { if ($pv->id == $id_point_vente) { $filename = 'export_' . $date . '_' . strtolower(str_replace(' ', '-', $pv->nom)); foreach ($pv->orders as $c) { $str_user = ''; // username if ($c->user) { $str_user = $c->user->name . " " . $c->user->lastname; //.' - '.date('d/m', strtotime($c->date)) ; } else { $str_user = $c->username; //.' - '.date('d/m', strtotime($c->date)) ; } // téléphone if (isset($c->user) && strlen($c->user->phone)) { $str_user .= ' (' . $c->user->phone . ')'; } $data_add = [$str_user]; // produits $str_produits = ''; foreach ($produits as $p) { if (isset($produits_selec[$p->id]['active']) && $produits_selec[$p->id]['active']) { $add = false; foreach ($c->productOrder as $cp) { if ($p->id == $cp->id_produit) { $str_produits .= $cp->quantity ; $add = true; } } } } $data_add[] = substr($str_produits, 0, strlen($str_produits) - 2); $data_add[] = number_format($c->amount, 2) . ' €'; $data_add[] = $c->comment; $data[] = $data_add; } } } return [ 'data' => $data, 'filename' => $filename ]; } /** * Ajoute les commandes récurrentes pour une date donnée. * * @param string $date */ public function actionAddSubscriptions($date) { Subscription::addAll($date, true); $this->redirect(['index', 'date' => $date]); } /** * Change l'état d'un jour de production (activé, désactivé). * * @param string $date * @param integer $actif * @param boolean $redirect */ public function actionChangeState($date, $active, $redirect = true) { // changement état $distribution = Distribution::initDistribution($date) ; $distribution->active = $active; $distribution->save(); if ($active) { // add commandes automatiques Subscription::addAll($date); } if($redirect) { $this->redirect(['index', 'date' => $date]); } } /** * Change l'état d'une semaine de production (activé, désactivé). * * @param string $date * @param integer $actif */ public function actionChangeStateWeek($date, $active) { $week = sprintf('%02d',date('W',strtotime($date))); $start = strtotime(date('Y',strtotime($date)).'W'.$week); $date_lundi = date('Y-m-d',strtotime('Monday',$start)) ; $date_mardi = date('Y-m-d',strtotime('Tuesday',$start)) ; $date_mercredi = date('Y-m-d',strtotime('Wednesday',$start)) ; $date_jeudi = date('Y-m-d',strtotime('Thursday',$start)) ; $date_vendredi = date('Y-m-d',strtotime('Friday',$start)) ; $date_samedi = date('Y-m-d',strtotime('Saturday',$start)) ; $date_dimanche = date('Y-m-d',strtotime('Sunday',$start)) ; $points_vente = PointSale::searchAll() ; $lundi_active = false ; $mardi_active = false ; $mercredi_active = false ; $jeudi_active = false ; $vendredi_active = false ; $samedi_active = false ; $dimanche_active = false ; foreach($points_vente as $pv) { if($pv->livraison_lundi) $lundi_active = true ; if($pv->livraison_mardi) $mardi_active = true ; if($pv->livraison_mercredi) $mercredi_active = true ; if($pv->livraison_jeudi) $jeudi_active = true ; if($pv->livraison_vendredi) $vendredi_active = true ; if($pv->livraison_samedi) $samedi_active = true ; if($pv->livraison_dimanche) $dimanche_active = true ; } if($lundi_active || !$active) $this->actionChangeState($date_lundi, $active, false) ; if($mardi_active || !$active) $this->actionChangeState($date_mardi, $active, false) ; if($mercredi_active || !$active) $this->actionChangeState($date_mercredi, $active, false) ; if($jeudi_active || !$active) $this->actionChangeState($date_jeudi, $active, false) ; if($vendredi_active || !$active) $this->actionChangeState($date_vendredi, $active, false) ; if($samedi_active || !$active) $this->actionChangeState($date_samedi, $active, false) ; if($dimanche_active || !$active) $this->actionChangeState($date_dimanche, $active, false) ; $this->redirect(['index', 'date' => $date]); } /** * Met à jour une commande via une requête AJAX. * * @param integer $id_commande * @param array $produits * @param string $date * @param string $commentaire */ public function actionAjaxUpdate( $idOrder, $products, $date, $comment) { $commande = Order::searchOne(['id' => $idOrder]) ; if ($commande && $commande->distribution->id_producer == Producer::getId()) { $produits = json_decode($produits); foreach ($produits as $key => $quantite) { $commande_produit = ProductOrder::findOne([ 'id_commande' => $id_commande, 'id_produit' => $key ]); if ($quantite) { if ($commande_produit) { $commande_produit->quantity = $quantite; } else { $produit = Product::findOne($key); if ($produit) { $commande_produit = new ProductOrder; $commande_produit->id_order = $idOrder; $commande_produit->id_product = $key; $commande_produit->quantity = $quantite; $commande_produit->price = $produit->price; } } $commande_produit->save(); } else { if ($commande_produit) $commande_produit->delete(); } } $commande->date_update = date('Y-m-d H:i:s'); $commande->comment = $comment; $commande->save(); // data commande $json_commande = $commande->getDataJson(); // total point de vente $point_vente = PointSale::findOne($commande->id_point_sale); $commandes = Order::searchAll([ 'distribution.date' => $date ]) ; $point_vente->initOrders($commandes); echo json_encode([ 'total_pv' => number_format($point_vente->revenues, 2) . ' €', 'json_commande' => $json_commande ]); die(); } } /** * Supprime une commande via une requête AJAX. * * @param string $date * @param integer $id_commande */ public function actionAjaxDelete($date, $idOrder) { $commande = Order::searchOne([ 'id' => $idOrder ]) ; // delete if ($commande) { // remboursement si l'utilisateur a payé pour cette commande $montant_paye = $commande->getAmount(Order::AMOUNT_PAID); if ($montant_paye > 0.01) { $commande->creditHistory( CreditHistory::TYPE_REFUND, $montant_paye, Producer::getId(), $commande->id_user, User::getId() ); } $commande->delete(); ProductOrder::deleteAll(['id_order' => $idOrder]); } // total point de vente $point_vente = PointSale::findOne($commande->id_point_sale); $commandes = Order::searchAll([ 'distribution.date' => $date, ]) ; $point_vente->initOrders($commandes); echo json_encode([ 'total_pv' => number_format($point_vente->revenues, 2) . ' €', ]); die(); } /** * Crée une commande via une requête AJAX. * * @param string $date * @param integer $id_pv * @param integer $id_user * @param string $username * @param array $produits * @param string $commentaire */ public function actionAjaxCreate( $date, $id_pv, $id_user, $username, $produits, $commentaire) { $produits = json_decode($produits); $point_vente = PointSale::findOne($id_pv); $production = Distribution::searchOne([ 'date' => $date ]); if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/", $date) && ($id_user || strlen($username)) && $point_vente && count($produits) && $production) { $commande = new Order; $commande->date = date('Y-m-d H:i:s', strtotime($date . ' ' . date('H:i:s'))); $commande->id_point_sale = $id_pv; $commande->id_distribution = $production->id; $commande->type = Order::ORIGIN_ADMIN; $commande->comment = $commentaire; if ($id_user) { $commande->id_user = $id_user; // commentaire du point de vente $point_vente_user = UserPointSale::searchOne([ 'id_point_sale' => $id_pv, 'id_user' => $id_user ]) ; if ($point_vente_user && strlen($point_vente_user->comment)) { $commande->point_sale_comment = $point_vente_user->comment; } } else { $commande->username = $username; $commande->id_user = 0; } $commande->save(); foreach ($produits as $key => $quantite) { $produit = Product::findOne($key); if ($produit) { $commande_produit = new ProductOrder; $commande_produit->id_order = $commande->id; $commande_produit->id_product = $key; $commande_produit->quantity = $quantite; $commande_produit->price = $produit->price; $commande_produit->save(); } } // total point de vente $point_vente = PointSale::findOne($commande->id_point_sale); $commandes = Order::searchAll([ 'distribution.date' => $date ]) ; $point_vente->initOrders($commandes); // json commande $commande = Order::searchOne([ 'id' => $commande->id ]) ; $produits = []; foreach ($commande->productOrder as $cp) { $produits[$cp->id_product] = $cp->quantity; } $json_commande = json_encode(['montant' => number_format($commande->amount, 2), 'produits' => $produits]); $json_commande = $commande->getDataJson(); $str_user = ''; if ($commande->user) $str_user = $commande->user->name . ' ' . $commande->user->lastname; else $str_user = $commande->username; $str_commentaire = ''; if (strlen($commande->comment)) { $str_commentaire = ' '; } $str_label_type_commande = ''; if ($commande->type) { $str_label_type_commande = ' vous'; } echo json_encode([ 'id_commande' => $commande->id, 'total_pv' => number_format($point_vente->revenues, 2) . ' €', 'commande' => '
  • ' . '' . '' . number_format($commande->amount, 2) . ' €' . '' . $str_label_type_commande . ' ' . $str_user . '' . $str_commentaire . '
  • ', ]); die(); } } /** * Retourne un récapitulatif du total des commandes (potentiel, commandé et * par produits) au format HTML; * * @param string $date */ public function actionAjaxTotalOrders($date) { $production = Distribution::searchOne([ 'date' => $date ]) ; if ($production) { // produits $produits = Product::searchOne() ; // commandes $commandes = Order::searchAll([ 'distribution.date' => $date ]) ; $recettes = 0; $poids_pain = 0; foreach ($commandes as $c) { $c->init(); if(is_null($c->date_delete)) { $recettes += $c->amount; $poids_pain += $c->weight; } } // produits selec pour production $produits_selec = ProductDistribution::searchByDistribution($production->id); $ca_potentiel = 0; $poids_total = 0; foreach ($produits_selec as $id_produit_selec => $produit_selec) { if ($produit_selec['active']) { foreach ($produits as $produit) { if ($produit->id == $id_produit_selec) { $ca_potentiel += $produit_selec['quantity_max'] * $produit->price; $poids_total += $produit_selec['quantity_max'] * $produit->weight / 1000; } } } } $html_totaux = $this->renderPartial('_total_commandes.php', [ 'produits' => $produits, 'commandes' => $commandes, 'produits_selec' => $produits_selec, 'recettes' => $recettes, 'poids_total' => $poids_total, 'ca_potentiel' => $ca_potentiel, 'poids_pain' => $poids_pain, ]); echo json_encode([ 'html_totaux' => $html_totaux, ]); } die(); } /** * Active ou désactive la livraison dans un point de vente. * * @param integer $id_production * @param integer $id_point_vente * @param boolean $bool_livraison */ public function actionAjaxPointSaleDelivery( $id_production, $id_point_vente, $bool_livraison) { $production_point_vente = PointSaleDistribution::searchOne([ 'id_production' => $id_production, 'id_point_vente' => $id_point_vente, ]) ; if ($production_point_vente) { $production_point_vente->delivery = $bool_livraison; $production_point_vente->save(); } die(); } /** * Retourne l'état du paiement (historique, crédit) d'une commande donnée. * * @param integer $id_commande */ public function actionPaymentStatus($id_commande) { $commande = Commande::searchOne(['id' => $id_commande]) ; if ($commande) { $commande->init(); $html = ''; if ($commande->id_user) { $user_etablissement = UserProducer::find() ->where([ 'id_user' => $commande->id_user, 'id_producer' => $commande->distribution->id_producer ]) ->one(); $montant_paye = $commande->getAmount(); //$html .= $commande->montant.' | '.$montant_paye ; if (abs($commande->amount - $montant_paye) < 0.0001) { $html .= 'Payé'; $buttons_credit = Html::a('Rembourser ' . $commande->getAmount(Order::AMOUNT_TOTAL, true), 'javascript:void(0);', ['class' => 'btn btn-default btn-xs rembourser', 'data-montant' => $commande->amount, 'data-type' => 'remboursement']); } elseif ($commande->amount > $montant_paye) { $montant_payer = $commande->amount - $montant_paye; $html .= 'Non payé reste ' . number_format($montant_payer, 2) . ' € à payer'; $buttons_credit = Html::a('Payer ' . number_format($montant_payer, 2) . ' €', 'javascript:void(0);', ['class' => 'btn btn-default btn-xs payer', 'data-montant' => $montant_payer, 'data-type' => 'paiement']); } elseif ($commande->amount < $montant_paye) { $montant_rembourser = $montant_paye - $commande->amount; $html .= ' Payé ' . number_format($montant_rembourser, 2) . ' € à rembourser'; $buttons_credit = Html::a('Rembourser ' . number_format($montant_rembourser, 2) . ' €', 'javascript:void(0);', ['class' => 'btn btn-default btn-xs rembourser', 'data-montant' => $montant_rembourser, 'data-type' => 'remboursement']); } $html .= '' . 'Crédit pain : ' . number_format($user_etablissement->credit, 2) . ' €
    ' . $buttons_credit . '
    '; // historique $historique = CreditHistory::find() ->with('userAction') ->where(['id_order' => $id_commande]) ->all(); $html .= '

    Historique
    ' . '' . ''; if ($historique && is_array($historique) && count($historique)) { foreach ($historique as $h) { $html .= '' . '' . '' . '' . '' . '' . ''; } } else { $html .= ''; } $html .= '
    DateUtilisateurAction- Débit+ Crédit
    ' . date('d/m/Y H:i:s', strtotime($h->date)) . '' . Html::encode($h->strUserAction()) . '' . $h->getStrLibelle() . '' . ($h->isTypeDebit() ? '- '.$h->getAmount(Order::AMOUNT_TOTAL,true) : '') . '' . ($h->isTypeCredit() ? '+ '.$h->getAmount(Order::AMOUNT_TOTAL,true) : '') . '
    Aucun résultat
    '; } else { $html .= '
    Pas de gestion de crédit pain pour cette commande car elle n\'est pas liée à un compte utilisateur.
    '; } echo json_encode([ 'html_statut_paiement' => $html, 'json_commande' => $commande->getDataJson() ]); } die(); } /** * Effectue le paiement/remboursement d'une commande. * * @param integer $id_commande * @param string $type * @param float $montant * @return string */ public function actionPayment($id_commande, $type, $montant) { $commande = Order::searchOne([ 'id' => $id_order ]) ; if ($commande) { $commande->creditHistory( $type, $montant, Producer::getId(), $commande->id_user, User::getId() ); } return $this->actionPaymentStatus($id_commande); } }