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 = Distribution::initDistribution($date) ; $json['distribution'] = [ 'id' => $distribution->id, 'active' => $distribution->active, 'url_report' => Yii::$app->urlManagerBackend->createUrl(['distribution/report','date' => $distribution->date]) ] ; // commandes $ordersArray = Order::searchAll([ 'distribution.date' => $date, ]); // montant et poids des commandes $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'] = number_format($revenues, 2); $json['distribution']['weight'] = number_format($weight, 2); // products $productsArray = Product::find() ->where([ 'id_producer' => Producer::getId(), ]) ->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']) { $potentialRevenues += $product['quantity_max'] * $product['price']; $potentialWeight += $product['quantity_max'] * $product['weight'] / 1000; } } $json['distribution']['potential_revenues'] = $potentialRevenues ; $json['distribution']['potential_weight'] = $potentialWeight ; $json['products'] = $productsArray ; // orders as array 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) : '') ] ; } $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)) ? $order->user->getAttributes() : null, 'pointSale' => ['id' => $order->pointSale->id, 'name' => $order->pointSale->name], 'productOrder' => $productOrderArray, 'creditHistory' => $creditHistoryArray ]) ; } } $json['orders'] = $ordersArray ; // order create $productOrderArray = [] ; foreach($productsArray as $product) { $productOrderArray[$product['id']] = 0 ; } $json['order_create'] = [ 'id_point_sale' => 0, 'id_user' => 0, 'username' => '', 'comment' => '', 'productOrder' => $productOrderArray ] ; // points de vente $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 ; // utilisateurs $usersArray = User::findBy()->all() ; $json['users'] = $usersArray ; } return $json ; } /** * 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 $idProducer * @return PDF|null */ 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) ; } // produits $productsArray = Product::searchAll() ; // get your HTML raw content without any layouts or scripts $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([ // 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 . '-' . $idProducer . '.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 ; } 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 = (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, $active) { \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $distribution = Distribution::searchOne([ 'id' => $idDistribution ]) ; $distribution->active = (int) $active ; $distribution->save() ; if ($active) { // ajout des abonnements Subscription::addAll($distribution->date); } return ['success'] ; } }