Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

DistributionController.php 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. <?php
  2. /**
  3. Copyright distrib (2018)
  4. contact@opendistrib.net
  5. Ce logiciel est un programme informatique servant à aider les producteurs
  6. à distribuer leur production en circuits courts.
  7. Ce logiciel est régi par la licence CeCILL soumise au droit français et
  8. respectant les principes de diffusion des logiciels libres. Vous pouvez
  9. utiliser, modifier et/ou redistribuer ce programme sous les conditions
  10. de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
  11. sur le site "http://www.cecill.info".
  12. En contrepartie de l'accessibilité au code source et des droits de copie,
  13. de modification et de redistribution accordés par cette licence, il n'est
  14. offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons,
  15. seule une responsabilité restreinte pèse sur l'auteur du programme, le
  16. titulaire des droits patrimoniaux et les concédants successifs.
  17. A cet égard l'attention de l'utilisateur est attirée sur les risques
  18. associés au chargement, à l'utilisation, à la modification et/ou au
  19. développement et à la reproduction du logiciel par l'utilisateur étant
  20. donné sa spécificité de logiciel libre, qui peut le rendre complexe à
  21. manipuler et qui le réserve donc à des développeurs et des professionnels
  22. avertis possédant des connaissances informatiques approfondies. Les
  23. utilisateurs sont donc invités à charger et tester l'adéquation du
  24. logiciel à leurs besoins dans des conditions permettant d'assurer la
  25. sécurité de leurs systèmes et ou de leurs données et, plus généralement,
  26. à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
  27. Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
  28. pris connaissance de la licence CeCILL, et que vous en avez accepté les
  29. termes.
  30. */
  31. namespace backend\controllers;
  32. use backend\controllers\BackendController ;
  33. use common\models\Distribution ;
  34. use common\models\Product ;
  35. use common\models\Producer ;
  36. use common\models\Order ;
  37. use common\models\User ;
  38. use common\models\Subscription ;
  39. use common\helpers\Price ;
  40. use common\models\PointSaleDistribution ;
  41. use DateTime;
  42. class DistributionController extends BackendController
  43. {
  44. public function behaviors()
  45. {
  46. return [
  47. 'access' => [
  48. 'class' => AccessControl::className(),
  49. 'rules' => [
  50. [
  51. 'actions' => ['report-cron'],
  52. 'allow' => true,
  53. 'roles' => ['?']
  54. ],
  55. [
  56. 'allow' => true,
  57. 'roles' => ['@'],
  58. 'matchCallback' => function ($rule, $action) {
  59. return User::getCurrentStatus() == USER::STATUS_ADMIN
  60. || User::getCurrentStatus() == USER::STATUS_PRODUCER;
  61. }
  62. ]
  63. ],
  64. ],
  65. ];
  66. }
  67. public function actionIndex($date = '')
  68. {
  69. $this->checkProductsPointsSale() ;
  70. $format = 'Y-m-d' ;
  71. $theDate = '' ;
  72. $dateObject = DateTime::createFromFormat($format, $date);
  73. if($dateObject && $dateObject->format($format) === $date) {
  74. $theDate = $date ;
  75. }
  76. return $this->render('index', [
  77. 'date' => $theDate
  78. ]) ;
  79. }
  80. public function actionAjaxInfos($date = '')
  81. {
  82. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  83. $json = [
  84. 'distribution' => [],
  85. 'products' => []
  86. ] ;
  87. $format = 'Y-m-d' ;
  88. $dateObject = DateTime::createFromFormat($format, $date);
  89. $producer = Producer::getCurrent() ;
  90. $json['producer'] = [
  91. 'credit' => $producer->credit
  92. ];
  93. $distributionsArray = Distribution::searchAll([
  94. 'active' => 1
  95. ], [
  96. 'conditions' => ['date > :date_begin','date < :date_end'],
  97. 'params' => [':date_begin' => date('Y-m-d', strtotime('-1 month')), ':date_end' => date('Y-m-d', strtotime('+3 month')), ],
  98. ]) ;
  99. $json['distributions'] = $distributionsArray ;
  100. if($dateObject && $dateObject->format($format) === $date) {
  101. // distribution
  102. $distribution = Distribution::initDistribution($date) ;
  103. $json['distribution'] = [
  104. 'id' => $distribution->id,
  105. 'active' => $distribution->active,
  106. 'url_report' => Yii::$app->urlManagerBackend->createUrl(['distribution/report','date' => $distribution->date])
  107. ] ;
  108. // commandes
  109. $ordersArray = Order::searchAll([
  110. 'distribution.id' => $distribution->id,
  111. ],[
  112. 'orderby' => 'user.lastname ASC, user.name ASC'
  113. ]);
  114. // montant et poids des commandes
  115. $revenues = 0;
  116. $weight = 0 ;
  117. if($ordersArray) {
  118. foreach ($ordersArray as $order) {
  119. if(is_null($order->date_delete)) {
  120. $revenues += $order->amount;
  121. $weight += $order->weight;
  122. }
  123. }
  124. }
  125. $json['distribution']['revenues'] = Price::format($revenues);
  126. $json['distribution']['weight'] = number_format($weight, 2);
  127. // products
  128. $productsArray = Product::find()
  129. ->orWhere(['id_producer' => Producer::getId(),])
  130. ->joinWith(['productDistribution' => function($query) use($distribution) {
  131. $query->andOnCondition('product_distribution.id_distribution = '.$distribution->id) ;
  132. }])
  133. ->orderBy('product_distribution.active DESC, order ASC')
  134. ->asArray()
  135. ->all();
  136. $potentialRevenues = 0;
  137. $potentialWeight = 0;
  138. foreach($productsArray as &$theProduct) {
  139. $quantityOrder = Order::getProductQuantity($theProduct['id'], $ordersArray) ;
  140. $theProduct['quantity_ordered'] = $quantityOrder ;
  141. if(!isset($theProduct['productDistribution'][0])) {
  142. $theProduct['productDistribution'][0] = $distribution->linkProduct($theProduct) ;
  143. }
  144. if(!is_numeric($theProduct['productDistribution'][0]['quantity_max'])) {
  145. $theProduct['quantity_remaining'] = null ;
  146. }
  147. else {
  148. $theProduct['quantity_remaining'] = $theProduct['productDistribution'][0]['quantity_max'] - $quantityOrder ;
  149. }
  150. $theProduct['quantity_form'] = 0 ;
  151. if($theProduct['productDistribution'][0]['active'] && $theProduct['productDistribution'][0]['quantity_max']) {
  152. $potentialRevenues += $theProduct['productDistribution'][0]['quantity_max'] * $theProduct['price'];
  153. $potentialWeight += $theProduct['productDistribution'][0]['quantity_max'] * $theProduct['weight'] / 1000;
  154. }
  155. }
  156. $json['distribution']['potential_revenues'] = Price::format($potentialRevenues) ;
  157. $json['distribution']['potential_weight'] = number_format($potentialWeight, 2) ;
  158. $json['products'] = $productsArray ;
  159. // orders as array
  160. if($ordersArray) {
  161. foreach($ordersArray as &$order) {
  162. $productOrderArray = [] ;
  163. foreach($order->productOrder as $productOrder) {
  164. $productOrderArray[$productOrder->id_product] = [
  165. 'quantity' => $productOrder->quantity * Product::$unitsArray[$productOrder->unit]['coefficient'],
  166. 'unit' => $productOrder->unit,
  167. ] ;
  168. }
  169. /*$productOrderArray = \yii\helpers\ArrayHelper::map($order->productOrder, 'id_product', 'quantity') ;
  170. */
  171. foreach($productsArray as $product) {
  172. if(!isset($productOrderArray[$product['id']])) {
  173. $productOrderArray[$product['id']] = [
  174. 'quantity' => 0,
  175. 'unit' => $product['unit']
  176. ] ;
  177. }
  178. }
  179. $creditHistoryArray = [] ;
  180. foreach($order->creditHistory as $creditHistory) {
  181. $creditHistoryArray[] = [
  182. 'date' => date('d/m/Y H:i:s', strtotime($creditHistory->date)),
  183. 'user_action' => $creditHistory->strUserAction(),
  184. 'wording' => $creditHistory->getStrWording(),
  185. 'debit' => ($creditHistory->isTypeDebit() ? '-&nbsp;'.$creditHistory->getAmount(Order::AMOUNT_TOTAL,true) : ''),
  186. 'credit' => ($creditHistory->isTypeCredit() ? '+&nbsp;'.$creditHistory->getAmount(Order::AMOUNT_TOTAL,true) : '')
  187. ] ;
  188. }
  189. $arrayCreditUser = [] ;
  190. if(isset($order->user) && isset($order->user->userProducer)) {
  191. $arrayCreditUser['credit'] = $order->user->userProducer[0]->credit ;
  192. }
  193. $oneProductUnactivated = false ;
  194. foreach($order->productOrder as $productOrder) {
  195. foreach($productsArray as $product) {
  196. if($productOrder->id_product == $product['id'] && !$product['productDistribution'][0]['active']) {
  197. $oneProductUnactivated = true ;
  198. }
  199. }
  200. }
  201. $order = array_merge($order->getAttributes(), [
  202. 'amount' => $order->getAmount(Order::AMOUNT_TOTAL),
  203. 'amount_paid' => $order->getAmount(Order::AMOUNT_PAID),
  204. 'amount_remaining' => $order->getAmount(Order::AMOUNT_REMAINING),
  205. 'amount_surplus' => $order->getAmount(Order::AMOUNT_SURPLUS),
  206. 'user' => (isset($order->user)) ? array_merge($order->user->getAttributes(), $arrayCreditUser) : null,
  207. 'pointSale' => ['id' => $order->pointSale->id, 'name' => $order->pointSale->name],
  208. 'productOrder' => $productOrderArray,
  209. 'creditHistory' => $creditHistoryArray,
  210. 'oneProductUnactivated' => $oneProductUnactivated
  211. ]) ;
  212. }
  213. }
  214. $json['orders'] = $ordersArray ;
  215. // points de vente
  216. $pointsSaleArray = PointSale::find()
  217. ->joinWith(['pointSaleDistribution' => function($q) use ($distribution) {
  218. $q->where(['id_distribution' => $distribution->id]);
  219. }])
  220. ->where([
  221. 'id_producer' => Producer::getId(),
  222. ])
  223. ->asArray()
  224. ->all();
  225. $idPointSaleDefault = 0 ;
  226. foreach($pointsSaleArray as $pointSale) {
  227. if($pointSale['default']) {
  228. $idPointSaleDefault = $pointSale['id'] ;
  229. }
  230. }
  231. $json['points_sale'] = $pointsSaleArray ;
  232. // order create
  233. $productOrderArray = [] ;
  234. foreach($productsArray as $product) {
  235. $productOrderArray[$product['id']] = [
  236. 'quantity' => 0,
  237. 'unit' => $product['unit']
  238. ] ;
  239. }
  240. $json['order_create'] = [
  241. 'id_point_sale' => $idPointSaleDefault,
  242. 'id_user' => 0,
  243. 'username' => '',
  244. 'comment' => '',
  245. 'productOrder' => $productOrderArray
  246. ] ;
  247. // utilisateurs
  248. $usersArray = User::findBy()->all() ;
  249. $json['users'] = $usersArray ;
  250. // une production de la semaine activée ou non
  251. $oneDistributionWeekActive = false ;
  252. $week = sprintf('%02d',date('W',strtotime($date)));
  253. $start = strtotime(date('Y',strtotime($date)).'W'.$week);
  254. $dateMonday = date('Y-m-d',strtotime('Monday',$start)) ;
  255. $dateTuesday = date('Y-m-d',strtotime('Tuesday',$start)) ;
  256. $dateWednesday = date('Y-m-d',strtotime('Wednesday',$start)) ;
  257. $dateThursday = date('Y-m-d',strtotime('Thursday',$start)) ;
  258. $dateFriday = date('Y-m-d',strtotime('Friday',$start)) ;
  259. $dateSaturday = date('Y-m-d',strtotime('Saturday',$start)) ;
  260. $dateSunday = date('Y-m-d',strtotime('Sunday',$start)) ;
  261. $weekDistribution = Distribution::find()
  262. ->andWhere([
  263. 'id_producer' => Producer::getId(),
  264. 'active' => 1,
  265. ])
  266. ->andWhere(['or',
  267. ['date' => $dateMonday],
  268. ['date' => $dateTuesday],
  269. ['date' => $dateWednesday],
  270. ['date' => $dateThursday],
  271. ['date' => $dateFriday],
  272. ['date' => $dateSaturday],
  273. ['date' => $dateSunday],
  274. ])
  275. ->one();
  276. if($weekDistribution) {
  277. $oneDistributionWeekActive = true ;
  278. }
  279. $json['one_distribution_week_active'] = $oneDistributionWeekActive ;
  280. }
  281. return $json ;
  282. }
  283. /**
  284. * Génére un PDF récapitulatif des des commandes d'un producteur pour une
  285. * date donnée (Méthode appelable via CRON)
  286. *
  287. * @param string $date
  288. * @param boolean $save
  289. * @param integer $idProducer
  290. * @param string $key
  291. * @return PDF|null
  292. */
  293. public function actionReportCron($date = '', $save = false, $idProducer = 0, $key = '')
  294. {
  295. if($key == '64ac0bdab7e9f5e48c4d991ec5201d57') {
  296. $this->actionReport($date, $save, $idProducer) ;
  297. }
  298. }
  299. /**
  300. * Génére un PDF récapitulatif des commandes d'un producteur pour une
  301. * date donnée.
  302. *
  303. * @param string $date
  304. * @param boolean $save
  305. * @param integer $idProducer
  306. * @return PDF|null
  307. */
  308. public function actionReport($date = '', $save = false, $idProducer = 0, $type = "pdf")
  309. {
  310. if (!Yii::$app->user->isGuest) {
  311. $idProducer = Producer::getId() ;
  312. }
  313. $ordersArray = Order::searchAll([
  314. 'distribution.date' => $date,
  315. ],
  316. [
  317. 'orderby' => 'comment_point_sale ASC, user.name ASC',
  318. 'conditions' => 'date_delete IS NULL'
  319. ]) ;
  320. $distribution = Distribution::searchOne([],[
  321. 'conditions' => 'date LIKE :date',
  322. 'params' => [':date' => $date]
  323. ]) ;
  324. if ($distribution) {
  325. $selectedProductsArray = ProductDistribution::searchByDistribution($distribution->id) ;
  326. $pointsSaleArray = PointSale::searchAll() ;
  327. foreach ($pointsSaleArray as $pointSale) {
  328. $pointSale->initOrders($ordersArray) ;
  329. }
  330. // produits
  331. $productsArray = Product::find()
  332. ->joinWith(['productDistribution' => function($q) use ($distribution) {
  333. $q->where(['id_distribution' => $distribution->id]);
  334. }])
  335. ->where([
  336. 'id_producer' => Producer::getId(),
  337. ])
  338. ->orderBy('order ASC')
  339. ->all();
  340. if($type == 'pdf') {
  341. // get your HTML raw content without any layouts or scripts
  342. $content = $this->renderPartial('report', [
  343. 'date' => $date,
  344. 'distribution' => $distribution,
  345. 'selectedProductsArray' => $selectedProductsArray,
  346. 'pointsSaleArray' => $pointsSaleArray,
  347. 'productsArray' => $productsArray,
  348. 'ordersArray' => $ordersArray
  349. ]);
  350. $dateStr = date('d/m/Y', strtotime($date));
  351. if ($save) {
  352. $destination = Pdf::DEST_FILE;
  353. } else {
  354. $destination = Pdf::DEST_BROWSER;
  355. }
  356. $pdf = new Pdf([
  357. // set to use core fonts only
  358. 'mode' => Pdf::MODE_UTF8,
  359. // A4 paper format
  360. 'format' => Pdf::FORMAT_A4,
  361. // portrait orientation
  362. 'orientation' => Pdf::ORIENT_PORTRAIT,
  363. // stream to browser inline
  364. 'destination' => $destination,
  365. 'filename' => Yii::getAlias('@app/web/pdf/Commandes-' . $date . '-' . $idProducer . '.pdf'),
  366. // your html content input
  367. 'content' => $content,
  368. // format content from your own css file if needed or use the
  369. // enhanced bootstrap css built by Krajee for mPDF formatting
  370. //'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
  371. // any css to be embedded if required
  372. //'cssInline' => '.kv-heading-1{font-size:18px}',
  373. // set mPDF properties on the fly
  374. //'options' => ['title' => 'Krajee Report Title'],
  375. // call mPDF methods on the fly
  376. 'methods' => [
  377. 'SetHeader' => ['Commandes du ' . $dateStr],
  378. 'SetFooter' => ['{PAGENO}'],
  379. ]
  380. ]);
  381. // return the pdf output as per the destination setting
  382. return $pdf->render();
  383. }
  384. elseif($type == 'csv') {
  385. $datas = [];
  386. // produits en colonne
  387. $productsNameArray = [''] ;
  388. $productsIndexArray = [] ;
  389. $cpt = 1 ;
  390. foreach ($productsArray as $product) {
  391. $quantity = Order::getProductQuantity($product->id, $ordersArray);
  392. if($quantity) {
  393. $productsNameArray[] = $product->name ;
  394. $productsIndexArray[$product->id] = $cpt ++ ;
  395. }
  396. }
  397. $datas[] = $productsNameArray ;
  398. // points de vente
  399. foreach ($pointsSaleArray as $pointSale) {
  400. if (count($pointSale->orders)) {
  401. // listing commandes
  402. $datas[] = ['> '.$pointSale->name] ;
  403. foreach($pointSale->orders as $order) {
  404. $orderLine = [$order->getStrUser()] ;
  405. foreach($order->productOrder as $productOrder) {
  406. $orderLine[$productsIndexArray[$productOrder->id_product]] = $productOrder->quantity . ' '.Product::strUnit($productOrder->unit, 'wording_short', true);
  407. }
  408. $datas[] = $this->_lineOrderReportCSV($orderLine, $cpt) ;
  409. }
  410. // total point de vente
  411. $totalsPointSaleArray = ['Total'] ;
  412. foreach ($productsArray as $product) {
  413. foreach(Product::$unitsArray as $unit => $dataUnit) {
  414. $quantity = Order::getProductQuantity($product->id, $pointSale->orders, false, $unit);
  415. if ($quantity) {
  416. $index = $productsIndexArray[$product->id] ;
  417. if(!isset($totalsPointSaleArray[$index])) {
  418. $totalsPointSaleArray[$index] = '' ;
  419. }
  420. $totalsPointSaleArray[$index] .= $quantity . ' '.Product::strUnit($unit, 'wording_short', true).' ';
  421. }
  422. }
  423. }
  424. $datas[] = $this->_lineOrderReportCSV($totalsPointSaleArray, $cpt) ;
  425. $datas[] = [] ;
  426. }
  427. }
  428. // global
  429. $totalsGlobalArray = ['> Totaux'] ;
  430. foreach ($productsArray as $product) {
  431. foreach(Product::$unitsArray as $unit => $dataUnit) {
  432. $quantity = Order::getProductQuantity($product->id, $ordersArray, false, $unit);
  433. if ($quantity) {
  434. $index = $productsIndexArray[$product->id] ;
  435. if(!isset($totalsGlobalArray[$index])) {
  436. $totalsGlobalArray[$index] = '' ;
  437. }
  438. $totalsGlobalArray[$index] .= $quantity . ' '.Product::strUnit($unit, 'wording_short', true).' ';
  439. }
  440. }
  441. }
  442. $datas[] = $this->_lineOrderReportCSV($totalsGlobalArray, $cpt) ;
  443. CSV::downloadSendHeaders('Commandes_'.$date.'.csv');
  444. echo CSV::array2csv($datas);
  445. die();
  446. }
  447. }
  448. return null ;
  449. }
  450. public function _lineOrderReportCSV($orderLine, $cptMax)
  451. {
  452. $line = [] ;
  453. for($i = 0; $i <= $cptMax ; $i++) {
  454. if(isset($orderLine[$i]) && $orderLine[$i]) {
  455. $line[] = $orderLine[$i] ;
  456. }
  457. else {
  458. $line[] = '' ;
  459. }
  460. }
  461. return $line ;
  462. }
  463. public function actionAjaxProcessProductQuantityMax($idDistribution, $idProduct, $quantityMax)
  464. {
  465. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  466. $productDistribution = ProductDistribution::searchOne([
  467. 'id_distribution' => $idDistribution,
  468. 'id_product' => $idProduct,
  469. ]) ;
  470. $productDistribution->quantity_max = (!$quantityMax) ? null : (int) $quantityMax ;
  471. $productDistribution->save() ;
  472. return ['success'] ;
  473. }
  474. public function actionAjaxProcessActiveProduct($idDistribution, $idProduct, $active)
  475. {
  476. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  477. $productDistribution = ProductDistribution::searchOne([
  478. 'id_distribution' => $idDistribution,
  479. 'id_product' => $idProduct,
  480. ]) ;
  481. $productDistribution->active = $active ;
  482. $productDistribution->save() ;
  483. return ['success'] ;
  484. }
  485. public function actionAjaxProcessActivePointSale($idDistribution, $idPointSale, $delivery)
  486. {
  487. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  488. $pointSaleDistribution = PointSaleDistribution::searchOne([
  489. 'id_distribution' => $idDistribution,
  490. 'id_point_sale' => $idPointSale,
  491. ]) ;
  492. $pointSaleDistribution->delivery = $delivery ;
  493. $pointSaleDistribution->save() ;
  494. return ['success'] ;
  495. }
  496. /**
  497. * Active/désactive un jour de distribution.
  498. *
  499. * @param integer $idDistribution
  500. * @param string $date
  501. * @param boolean $active
  502. * @return array
  503. */
  504. public function actionAjaxProcessActiveDistribution($idDistribution = 0, $date = '', $active)
  505. {
  506. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  507. if($idDistribution) {
  508. $distribution = Distribution::searchOne([
  509. 'id' => $idDistribution
  510. ]) ;
  511. }
  512. $format = 'Y-m-d' ;
  513. $dateObject = DateTime::createFromFormat($format, $date);
  514. if($dateObject && $dateObject->format($format) === $date) {
  515. $distribution = Distribution::initDistribution($date) ;
  516. }
  517. if($distribution) {
  518. $distribution->active($active) ;
  519. return ['success'] ;
  520. }
  521. return ['error'] ;
  522. }
  523. /**
  524. * Change l'état d'une semaine de production (activé, désactivé).
  525. *
  526. * @param string $date
  527. * @param integer $active
  528. */
  529. public function actionAjaxProcessActiveWeekDistribution($date, $active)
  530. {
  531. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  532. $week = sprintf('%02d',date('W',strtotime($date)));
  533. $start = strtotime(date('Y',strtotime($date)).'W'.$week);
  534. $dateMonday = date('Y-m-d',strtotime('Monday',$start)) ;
  535. $dateTuesday = date('Y-m-d',strtotime('Tuesday',$start)) ;
  536. $dateWednesday = date('Y-m-d',strtotime('Wednesday',$start)) ;
  537. $dateThursday = date('Y-m-d',strtotime('Thursday',$start)) ;
  538. $dateFriday = date('Y-m-d',strtotime('Friday',$start)) ;
  539. $dateSaturday = date('Y-m-d',strtotime('Saturday',$start)) ;
  540. $dateSunday = date('Y-m-d',strtotime('Sunday',$start)) ;
  541. $pointsSaleArray = PointSale::searchAll() ;
  542. $activeMonday = false ;
  543. $activeTuesday = false ;
  544. $activeWednesday = false ;
  545. $activeThursday = false ;
  546. $activeFriday = false ;
  547. $activeSaturday = false ;
  548. $activeSunday = false ;
  549. foreach($pointsSaleArray as $pointSale) {
  550. if($pointSale->delivery_monday) $activeMonday = true ;
  551. if($pointSale->delivery_tuesday) $activeTuesday = true ;
  552. if($pointSale->delivery_wednesday) $activeWednesday = true ;
  553. if($pointSale->delivery_thursday) $activeThursday = true ;
  554. if($pointSale->delivery_friday) $activeFriday = true ;
  555. if($pointSale->delivery_saturday) $activeSaturday = true ;
  556. if($pointSale->delivery_sunday) $activeSunday = true ;
  557. }
  558. if($activeMonday || !$active) {
  559. $this->actionAjaxProcessActiveDistribution(0, $dateMonday, $active) ;
  560. }
  561. if($activeTuesday || !$active) {
  562. $this->actionAjaxProcessActiveDistribution(0, $dateTuesday, $active) ;
  563. }
  564. if($activeWednesday || !$active) {
  565. $this->actionAjaxProcessActiveDistribution(0, $dateWednesday, $active) ;
  566. }
  567. if($activeThursday || !$active) {
  568. $this->actionAjaxProcessActiveDistribution(0, $dateThursday, $active) ;
  569. }
  570. if($activeFriday || !$active) {
  571. $this->actionAjaxProcessActiveDistribution(0, $dateFriday, $active) ;
  572. }
  573. if($activeSaturday || !$active) {
  574. $this->actionAjaxProcessActiveDistribution(0, $dateSaturday, $active) ;
  575. }
  576. if($activeSunday || !$active) {
  577. $this->actionAjaxProcessActiveDistribution(0, $dateSunday, $active) ;
  578. }
  579. return ['success'] ;
  580. }
  581. /**
  582. * Ajoute les commandes récurrentes pour une date donnée.
  583. *
  584. * @param string $date
  585. */
  586. public function actionAjaxProcessAddSubscriptions($date)
  587. {
  588. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  589. Subscription::addAll($date, true);
  590. return ['success'] ;
  591. }
  592. }