Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

DistributionController.php 69KB

pirms 6 gadiem
pirms 6 gadiem
pirms 6 gadiem
pirms 6 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  1. <?php
  2. /**
  3. * Copyright distrib (2018)
  4. *
  5. * contact@opendistrib.net
  6. *
  7. * Ce logiciel est un programme informatique servant à aider les producteurs
  8. * à distribuer leur production en circuits courts.
  9. *
  10. * Ce logiciel est régi par la licence CeCILL soumise au droit français et
  11. * respectant les principes de diffusion des logiciels libres. Vous pouvez
  12. * utiliser, modifier et/ou redistribuer ce programme sous les conditions
  13. * de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
  14. * sur le site "http://www.cecill.info".
  15. *
  16. * En contrepartie de l'accessibilité au code source et des droits de copie,
  17. * de modification et de redistribution accordés par cette licence, il n'est
  18. * offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons,
  19. * seule une responsabilité restreinte pèse sur l'auteur du programme, le
  20. * titulaire des droits patrimoniaux et les concédants successifs.
  21. *
  22. * A cet égard l'attention de l'utilisateur est attirée sur les risques
  23. * associés au chargement, à l'utilisation, à la modification et/ou au
  24. * développement et à la reproduction du logiciel par l'utilisateur étant
  25. * donné sa spécificité de logiciel libre, qui peut le rendre complexe à
  26. * manipuler et qui le réserve donc à des développeurs et des professionnels
  27. * avertis possédant des connaissances informatiques approfondies. Les
  28. * utilisateurs sont donc invités à charger et tester l'adéquation du
  29. * logiciel à leurs besoins dans des conditions permettant d'assurer la
  30. * sécurité de leurs systèmes et ou de leurs données et, plus généralement,
  31. * à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
  32. *
  33. * Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
  34. * pris connaissance de la licence CeCILL, et que vous en avez accepté les
  35. * termes.
  36. */
  37. namespace backend\controllers;
  38. use common\helpers\GlobalParam;
  39. use common\helpers\Price;
  40. use DateTime;
  41. class DistributionController extends BackendController
  42. {
  43. public function behaviors()
  44. {
  45. return [
  46. 'access' => [
  47. 'class' => AccessControl::className(),
  48. 'rules' => [
  49. [
  50. 'actions' => ['report-cron', 'report-terredepains'],
  51. 'allow' => true,
  52. 'roles' => ['?']
  53. ],
  54. [
  55. 'allow' => true,
  56. 'roles' => ['@'],
  57. 'matchCallback' => function ($rule, $action) {
  58. return User::getCurrentStatus() == User::STATUS_ADMIN
  59. || User::getCurrentStatus() == User::STATUS_PRODUCER;
  60. }
  61. ]
  62. ],
  63. ],
  64. ];
  65. }
  66. public function actionIndex($date = '', $idOrderUpdate = 0)
  67. {
  68. $this->checkProductsPointsSale();
  69. $format = 'Y-m-d';
  70. $theDate = '';
  71. $dateObject = DateTime::createFromFormat($format, $date);
  72. if ($dateObject && $dateObject->format($format) === $date) {
  73. $theDate = $date;
  74. }
  75. $orderUpdate = null;
  76. if ($idOrderUpdate) {
  77. $orderUpdate = Order::searchOne(['id' => $idOrderUpdate]);
  78. }
  79. return $this->render('index', [
  80. 'date' => $theDate,
  81. 'orderUpdate' => $orderUpdate
  82. ]);
  83. }
  84. public function actionAjaxInfos($date = '')
  85. {
  86. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  87. $creditHistoryService = \Yii::$app->logic->getCreditHistoryContainer()->getService();
  88. $json = [
  89. 'distribution' => [],
  90. 'products' => []
  91. ];
  92. $format = 'Y-m-d';
  93. $dateObject = DateTime::createFromFormat($format, $date);
  94. $numberOfLoadedMonthes = '3 month';
  95. if (is_object($dateObject)) {
  96. $dateBegin = strtotime('-' . $numberOfLoadedMonthes, $dateObject->getTimestamp());
  97. $dateEnd = strtotime('+' . $numberOfLoadedMonthes, $dateObject->getTimestamp());
  98. } else {
  99. $dateBegin = strtotime('-' . $numberOfLoadedMonthes);
  100. $dateEnd = strtotime('+' . $numberOfLoadedMonthes);
  101. }
  102. $producer = GlobalParam::getCurrentProducer();
  103. $json['producer'] = [
  104. 'credit' => $producer->credit,
  105. 'tiller' => $producer->tiller,
  106. 'option_display_export_grid' => $producer->option_display_export_grid
  107. ];
  108. $json['means_payment'] = MeanPayment::getAll();
  109. $distributionsArray = DistributionModel::searchAll([
  110. 'active' => 1
  111. ], [
  112. 'conditions' => [
  113. 'date > :date_begin',
  114. 'date < :date_end'
  115. ],
  116. 'params' => [
  117. ':date_begin' => date(
  118. 'Y-m-d',
  119. $dateBegin
  120. ),
  121. ':date_end' => date(
  122. 'Y-m-d',
  123. $dateEnd
  124. ),
  125. ],
  126. ]);
  127. $json['distributions'] = $distributionsArray;
  128. if ($dateObject && $dateObject->format($format) === $date) {
  129. // distribution
  130. $distribution = DistributionModel::initDistribution($date);
  131. $json['distribution'] = [
  132. 'id' => $distribution->id,
  133. 'active' => $distribution->active,
  134. 'url_report' => Yii::$app->urlManagerBackend->createUrl(
  135. ['distribution/report', 'date' => $distribution->date]
  136. ),
  137. 'url_report_grid' => Yii::$app->urlManagerBackend->createUrl(
  138. ['distribution/report-grid', 'date' => $distribution->date]
  139. ),
  140. ];
  141. // commandes
  142. $ordersArray = Order::searchAll([
  143. 'distribution.id' => $distribution->id,
  144. ], [
  145. 'orderby' => 'user.lastname ASC, user.name ASC'
  146. ]);
  147. // montant et poids des commandes
  148. $revenues = 0;
  149. $weight = 0;
  150. if ($ordersArray) {
  151. foreach ($ordersArray as $order) {
  152. if (is_null($order->date_delete)) {
  153. $revenues += $order->getAmountWithTax();
  154. $weight += $order->weight;
  155. }
  156. }
  157. }
  158. $json['distribution']['revenues'] = Price::format($revenues);
  159. $json['distribution']['weight'] = number_format($weight, 2);
  160. // products
  161. $productsQuery = Product::find()
  162. ->orWhere(['id_producer' => GlobalParam::getCurrentProducerId(),])
  163. ->joinWith([
  164. 'taxRate',
  165. 'productDistribution' => function ($query) use ($distribution) {
  166. $query->andOnCondition(
  167. 'product_distribution.id_distribution = ' . $distribution->id
  168. );
  169. }
  170. ])
  171. ->orderBy('product_distribution.active DESC, order ASC');
  172. $productsArray = $productsQuery->asArray()->all();
  173. $potentialRevenues = 0;
  174. $potentialWeight = 0;
  175. foreach ($productsArray as &$theProduct) {
  176. $quantityOrder = Order::getProductQuantity($theProduct['id'], $ordersArray);
  177. $theProduct['quantity_ordered'] = $quantityOrder;
  178. if (!isset($theProduct['productDistribution'][0])) {
  179. $theProductObject = (object)$theProduct;
  180. $theProduct['productDistribution'][0] = $distribution->linkProduct($theProductObject);
  181. }
  182. if (!is_numeric($theProduct['productDistribution'][0]['quantity_max'])) {
  183. $theProduct['quantity_remaining'] = null;
  184. } else {
  185. $theProduct['quantity_remaining'] = $theProduct['productDistribution'][0]['quantity_max'] - $quantityOrder;
  186. }
  187. $theProduct['quantity_form'] = 0;
  188. if ($theProduct['productDistribution'][0]['active'] && $theProduct['productDistribution'][0]['quantity_max']) {
  189. $potentialRevenues += $theProduct['productDistribution'][0]['quantity_max'] * $theProduct['price'];
  190. $potentialWeight += $theProduct['productDistribution'][0]['quantity_max'] * $theProduct['weight'] / 1000;
  191. }
  192. if (!isset($theProduct['taxRate'])) {
  193. $theProduct['taxRate'] = $producer->taxRate;
  194. }
  195. }
  196. $json['distribution']['potential_revenues'] = Price::format($potentialRevenues);
  197. $json['distribution']['potential_weight'] = number_format($potentialWeight, 2);
  198. $json['products'] = $productsArray;
  199. // orders as array
  200. $ordersArrayObject = $ordersArray;
  201. if ($ordersArray) {
  202. foreach ($ordersArray as &$order) {
  203. $productOrderArray = [];
  204. foreach ($order->productOrder as $productOrder) {
  205. $productOrderArray[$productOrder->id_product] = [
  206. 'quantity' => $productOrder->quantity * Product::$unitsArray[$productOrder->unit]['coefficient'],
  207. 'unit' => $productOrder->unit,
  208. 'price' => number_format($productOrder->price, 3),
  209. 'invoice_price' => number_format($productOrder->invoice_price, 2),
  210. 'price_with_tax' => Price::getPriceWithTax($productOrder->price, $productOrder->taxRate->value),
  211. ];
  212. }
  213. foreach ($productsArray as $product) {
  214. if (!isset($productOrderArray[$product['id']])) {
  215. $productOrderArray[$product['id']] = [
  216. 'quantity' => 0,
  217. 'unit' => $product['unit'],
  218. 'price' => number_format($product['price'], 3),
  219. 'price_with_tax' => Price::getPriceWithTax($product['price'], $product['taxRate']['value']),
  220. ];
  221. }
  222. }
  223. $creditHistoryArray = [];
  224. foreach ($order->creditHistory as $creditHistory) {
  225. $creditHistoryArray[] = [
  226. 'date' => date('d/m/Y H:i:s', strtotime($creditHistory->date)),
  227. 'user' => $creditHistory->getUserObject()->getUsername(),
  228. 'user_action' => $creditHistoryService->getStrUserAction($creditHistory),
  229. 'wording' => $creditHistoryService->getStrWording($creditHistory),
  230. 'debit' => ($creditHistoryService->isTypeDebit($creditHistory) ? '-&nbsp;' . $creditHistoryService->getAmount(
  231. $creditHistory,
  232. Order::AMOUNT_TOTAL,
  233. true
  234. ) : ''),
  235. 'credit' => ($creditHistoryService->isTypeCredit($creditHistory) ? '+&nbsp;' . $creditHistoryService->getAmount(
  236. $creditHistory,
  237. Order::AMOUNT_TOTAL,
  238. true
  239. ) : '')
  240. ];
  241. }
  242. $arrayCreditUser = [];
  243. if (isset($order->user) && isset($order->user->userProducer) && isset($order->user->userProducer[0])) {
  244. $arrayCreditUser['credit'] = $order->user->userProducer[0]->credit;
  245. }
  246. $oneProductUnactivated = false;
  247. foreach ($order->productOrder as $productOrder) {
  248. foreach ($productsArray as $product) {
  249. if ($productOrder->id_product == $product['id'] && !$product['productDistribution'][0]['active']) {
  250. $oneProductUnactivated = true;
  251. }
  252. }
  253. }
  254. $order = array_merge($order->getAttributes(), [
  255. 'selected' => false,
  256. 'weight' => $order->weight,
  257. 'amount' => Price::numberTwoDecimals($order->getAmountWithTax(Order::AMOUNT_TOTAL)),
  258. 'amount_paid' => Price::numberTwoDecimals($order->getAmount(Order::AMOUNT_PAID)),
  259. 'amount_remaining' => Price::numberTwoDecimals($order->getAmount(Order::AMOUNT_REMAINING)),
  260. 'amount_surplus' => Price::numberTwoDecimals($order->getAmount(Order::AMOUNT_SURPLUS)),
  261. 'user' => (isset($order->user)) ? array_merge(
  262. $order->user->getAttributes(),
  263. $arrayCreditUser
  264. ) : null,
  265. 'pointSale' => $order->pointSale ? ['id' => $order->pointSale->id, 'name' => $order->pointSale->name] : null,
  266. 'productOrder' => $productOrderArray,
  267. 'creditHistory' => $creditHistoryArray,
  268. 'oneProductUnactivated' => $oneProductUnactivated,
  269. 'isLinkedToValidDocument' => $order->isLinkedToValidDocument(),
  270. ]);
  271. }
  272. }
  273. $json['orders'] = $ordersArray;
  274. // points de vente
  275. $pointsSaleArray = PointSale::find()
  276. ->joinWith([
  277. 'pointSaleDistribution' => function ($q) use ($distribution) {
  278. $q->where(['id_distribution' => $distribution->id]);
  279. }
  280. ])
  281. ->where([
  282. 'id_producer' => GlobalParam::getCurrentProducerId(),
  283. ])
  284. ->asArray()
  285. ->all();
  286. $idPointSaleDefault = 0;
  287. foreach ($pointsSaleArray as $pointSale) {
  288. if ($pointSale['default']) {
  289. $idPointSaleDefault = $pointSale['id'];
  290. }
  291. }
  292. $json['points_sale'] = $pointsSaleArray;
  293. // bons de livraison
  294. $deliveryNotesArray = DeliveryNote::searchAll([
  295. 'distribution.date' => $date,
  296. ], [
  297. 'join_with' => ['user AS user_delivery_note', 'orders', 'producer']
  298. ]);
  299. $deliveryNotesByPointSaleArray = [];
  300. foreach ($deliveryNotesArray as $deliveryNote) {
  301. if (isset($deliveryNote->orders[0])) {
  302. $deliveryNotesByPointSaleArray[$deliveryNote->orders[0]->id_point_sale] =
  303. $deliveryNote->getAttributes();
  304. }
  305. }
  306. $json['delivery_notes'] = $deliveryNotesByPointSaleArray;
  307. // order create
  308. $productOrderArray = [];
  309. foreach ($productsArray as $product) {
  310. $productOrderArray[$product['id']] = [
  311. 'quantity' => 0,
  312. 'unit' => $product['unit'],
  313. 'price' => Price::getPriceWithTax($product['price'], $product['taxRate']['value']),
  314. ];
  315. }
  316. $json['order_create'] = [
  317. 'id_point_sale' => $idPointSaleDefault,
  318. 'id_user' => 0,
  319. 'username' => '',
  320. 'comment' => '',
  321. 'productOrder' => $productOrderArray
  322. ];
  323. // utilisateurs
  324. $usersArray = User::findBy()->all();
  325. $json['users'] = $usersArray;
  326. // une production de la semaine activée ou non
  327. $oneDistributionWeekActive = false;
  328. $week = sprintf('%02d', date('W', strtotime($date)));
  329. $start = strtotime(date('Y', strtotime($date)) . 'W' . $week);
  330. $dateMonday = date('Y-m-d', strtotime('Monday', $start));
  331. $dateTuesday = date('Y-m-d', strtotime('Tuesday', $start));
  332. $dateWednesday = date('Y-m-d', strtotime('Wednesday', $start));
  333. $dateThursday = date('Y-m-d', strtotime('Thursday', $start));
  334. $dateFriday = date('Y-m-d', strtotime('Friday', $start));
  335. $dateSaturday = date('Y-m-d', strtotime('Saturday', $start));
  336. $dateSunday = date('Y-m-d', strtotime('Sunday', $start));
  337. $weekDistribution = DistributionModel::find()
  338. ->andWhere([
  339. 'id_producer' => GlobalParam::getCurrentProducerId(),
  340. 'active' => 1,
  341. ])
  342. ->andWhere([
  343. 'or',
  344. ['date' => $dateMonday],
  345. ['date' => $dateTuesday],
  346. ['date' => $dateWednesday],
  347. ['date' => $dateThursday],
  348. ['date' => $dateFriday],
  349. ['date' => $dateSaturday],
  350. ['date' => $dateSunday],
  351. ])
  352. ->one();
  353. if ($weekDistribution) {
  354. $oneDistributionWeekActive = true;
  355. }
  356. $json['one_distribution_week_active'] = $oneDistributionWeekActive;
  357. // tiller
  358. if ($producer->tiller) {
  359. $tiller = new Tiller();
  360. $json['tiller_is_synchro'] = (int)$tiller->isSynchro($date);
  361. }
  362. // abonnements manquants
  363. $arraySubscriptions = Subscription::searchByDate($date);
  364. $json['missing_subscriptions'] = [];
  365. if ($distribution->active) {
  366. foreach ($arraySubscriptions as $subscription) {
  367. if (!$subscription->hasOrderAlreadyExist($ordersArrayObject)
  368. && $subscription->productSubscription && count($subscription->productSubscription)
  369. && $subscription->id_point_sale && $subscription->id_point_sale > 0) {
  370. $json['missing_subscriptions'][] = [
  371. 'username' => $subscription->getUsername()
  372. ];
  373. }
  374. }
  375. }
  376. }
  377. return $json;
  378. }
  379. public function actionAjaxPointSaleFavorite($idUser)
  380. {
  381. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  382. $user = User::findOne(['id' => $idUser]);
  383. $favoritePointSale = $user->getFavoritePointSale();
  384. $idFavoritePointSale = 0;
  385. if ($favoritePointSale) {
  386. $idFavoritePointSale = $favoritePointSale->id;
  387. }
  388. return [
  389. 'id_favorite_point_sale' => $idFavoritePointSale
  390. ];
  391. }
  392. public function actionAjaxUpdateProductOrder(
  393. $idDistribution,
  394. $idUser = false,
  395. $idPointSale = false,
  396. $idOrder = false
  397. )
  398. {
  399. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  400. $order = Order::searchOne(['id' => $idOrder]);
  401. $distribution = DistributionModel::findOne($idDistribution);
  402. $user = User::findOne($idUser);
  403. $pointSale = PointSale::findOne($idPointSale);
  404. $productsArray = Product::find()
  405. ->where([
  406. 'id_producer' => GlobalParam::getCurrentProducerId(),
  407. ])->joinWith([
  408. 'productPrice',
  409. 'productDistribution' => function ($q) use ($distribution) {
  410. $q->where(['id_distribution' => $distribution->id]);
  411. }
  412. ])->all();
  413. $productOrderArray = [];
  414. foreach ($productsArray as $product) {
  415. $priceArray = $product->getPriceArray($user, $pointSale);
  416. $quantity = 0;
  417. $invoicePrice = null;
  418. if(isset($order->productOrder)) {
  419. foreach ($order->productOrder as $productOrder) {
  420. if ($productOrder->id_product == $product['id']) {
  421. if ($productOrder->invoice_price) {
  422. $invoicePrice = number_format($productOrder->invoice_price, 2);
  423. } else {
  424. $invoicePrice = number_format($productOrder->price, 3);
  425. }
  426. $quantity = $productOrder->quantity;
  427. }
  428. }
  429. }
  430. $productOrderArray[$product['id']] = [
  431. 'quantity' => $quantity,
  432. 'unit' => $product->unit,
  433. 'unit_coefficient' => Product::$unitsArray[$product->unit]['coefficient'],
  434. 'prices' => $priceArray,
  435. 'active' => $product->productDistribution[0]->active
  436. && (!$pointSale || $product->isAvailableOnPointSale($pointSale)),
  437. 'invoice_price' => $invoicePrice
  438. ];
  439. }
  440. return $productOrderArray;
  441. }
  442. public function actionAjaxUpdateInvoicePrices($idOrder)
  443. {
  444. $order = Order::searchOne([
  445. 'id' => (int)$idOrder
  446. ]);
  447. if ($order && $order->distribution->id_producer == GlobalParam::getCurrentProducerId()) {
  448. $userProducer = null;
  449. if ($order->id_user) {
  450. $userProducer = UserProducer::searchOne([
  451. 'id_user' => $order->id_user,
  452. 'id_producer' => GlobalParam::getCurrentProducerId()
  453. ]);
  454. }
  455. foreach ($order->productOrder as $productOrder) {
  456. $invoicePrice = $productOrder->product->getPrice([
  457. 'user' => $order->user ?: null,
  458. 'point_sale' => $order->pointSale,
  459. 'user_producer' => $userProducer,
  460. 'quantity' => $productOrder->quantity
  461. ]);
  462. if ($invoicePrice != $productOrder->price) {
  463. $productOrder->invoice_price = $invoicePrice;
  464. }
  465. else {
  466. $productOrder->invoice_price = null;
  467. }
  468. $productOrder->save();
  469. }
  470. }
  471. }
  472. /**
  473. * Génére un PDF récapitulatif des des commandes d'un producteur pour une
  474. * date donnée (Méthode appelable via CRON)
  475. *
  476. * @param string $date
  477. * @param boolean $save
  478. * @param integer $idProducer
  479. * @param string $key
  480. * @return PDF|null
  481. */
  482. public function actionReportCron($date = '', $save = false, $idProducer = 0, $key = '')
  483. {
  484. if ($key == '64ac0bdab7e9f5e48c4d991ec5201d57') {
  485. $this->actionReport($date, $save, $idProducer);
  486. }
  487. }
  488. /**
  489. * Génére un PDF récapitulatif des commandes d'un producteur pour une
  490. * date donnée.
  491. *
  492. * @param string $date
  493. * @param boolean $save
  494. * @param integer $idProducer
  495. * @return PDF|null
  496. */
  497. public function actionReport($date = '', $save = false, $idProducer = 0, $type = "pdf")
  498. {
  499. if (!Yii::$app->user->isGuest) {
  500. $idProducer = GlobalParam::getCurrentProducerId();
  501. }
  502. $ordersArray = Order::searchAll(
  503. [
  504. 'distribution.date' => $date,
  505. 'distribution.id_producer' => $idProducer
  506. ],
  507. [
  508. 'orderby' => 'user.lastname ASC, user.name ASC, comment_point_sale ASC',
  509. 'conditions' => 'date_delete IS NULL'
  510. ]
  511. );
  512. $distribution = DistributionModel::searchOne([
  513. 'id_producer' => $idProducer
  514. ], [
  515. 'conditions' => 'date LIKE :date',
  516. 'params' => [':date' => $date]
  517. ]);
  518. if ($distribution) {
  519. $selectedProductsArray = ProductDistributionModel::searchByDistribution($distribution->id);
  520. $pointsSaleArray = PointSale::searchAll([
  521. 'point_sale.id_producer' => $idProducer
  522. ]);
  523. foreach ($pointsSaleArray as $pointSale) {
  524. $pointSale->initOrders($ordersArray);
  525. }
  526. // produits
  527. $productsArray = Product::find()
  528. ->joinWith([
  529. 'productDistribution' => function ($q) use ($distribution) {
  530. $q->where(['id_distribution' => $distribution->id]);
  531. }
  532. ])
  533. ->where([
  534. 'id_producer' => $idProducer,
  535. ])
  536. ->orderBy('order ASC')
  537. ->all();
  538. if ($type == 'pdf') {
  539. $viewPdf = 'report';
  540. $orientationPdf = Pdf::ORIENT_PORTRAIT;
  541. $producer = GlobalParam::getCurrentProducer();
  542. if ($producer->slug == 'bourlinguepacotille') {
  543. $viewPdf = 'report-bourlingue';
  544. $orientationPdf = Pdf::ORIENT_LANDSCAPE;
  545. }
  546. // get your HTML raw content without any layouts or scripts
  547. $content = $this->renderPartial($viewPdf, [
  548. 'date' => $date,
  549. 'distribution' => $distribution,
  550. 'selectedProductsArray' => $selectedProductsArray,
  551. 'pointsSaleArray' => $pointsSaleArray,
  552. 'productsArray' => $productsArray,
  553. 'ordersArray' => $ordersArray,
  554. 'producer' => Producer::searchOne(['id' => $idProducer])
  555. ]);
  556. $dateStr = date('d/m/Y', strtotime($date));
  557. if ($save) {
  558. $destination = Pdf::DEST_FILE;
  559. } else {
  560. $destination = Pdf::DEST_BROWSER;
  561. }
  562. $pdf = new Pdf([
  563. // set to use core fonts only
  564. 'mode' => Pdf::MODE_UTF8,
  565. // A4 paper format
  566. 'format' => Pdf::FORMAT_A4,
  567. // portrait orientation
  568. 'orientation' => $orientationPdf,
  569. // stream to browser inline
  570. 'destination' => $destination,
  571. 'filename' => Yii::getAlias(
  572. '@app/web/pdf/Commandes-' . $date . '-' . $idProducer . '.pdf'
  573. ),
  574. // your html content input
  575. 'content' => $content,
  576. // format content from your own css file if needed or use the
  577. // enhanced bootstrap css built by Krajee for mPDF formatting
  578. //'cssFile' => Yii::getAlias('@web/css/distribution/report.css'),
  579. // any css to be embedded if required
  580. 'cssInline' => '
  581. table {
  582. border-spacing : 0px ;
  583. border-collapse : collapse ;
  584. width: 100% ;
  585. }
  586. table tr th,
  587. table tr td {
  588. padding: 0px ;
  589. margin: 0px ;
  590. border: solid 1px #e0e0e0 ;
  591. padding: 3px 8px ;
  592. vertical-align : top;
  593. page-break-inside: avoid !important;
  594. color: black;
  595. }
  596. table tr th {
  597. font-size: 13px ;
  598. }
  599. table tr td {
  600. font-size: 13px ;
  601. }
  602. ',
  603. // set mPDF properties on the fly
  604. //'options' => ['title' => 'Krajee Report Title'],
  605. // call mPDF methods on the fly
  606. 'methods' => [
  607. 'SetHeader' => ['Commandes du ' . $dateStr],
  608. 'SetFooter' => ['{PAGENO}'],
  609. ]
  610. ]);
  611. // return the pdf output as per the destination setting
  612. return $pdf->render();
  613. } elseif ($type == 'csv') {
  614. $datas = [];
  615. $optionCsvExportAllProducts = Producer::getConfig('option_csv_export_all_products');
  616. $optionCsvExportByPiece = Producer::getConfig('option_csv_export_by_piece');
  617. // produits en colonne
  618. $productsNameArray = ['', 'Commentaire'];
  619. $productsIndexArray = [];
  620. $productsHasQuantity = [];
  621. $cpt = 2;
  622. foreach ($productsArray as $product) {
  623. $productsHasQuantity[$product->id] = 0;
  624. foreach ( Product::$unitsArray as $unit => $dataUnit) {
  625. $quantity = Order::getProductQuantity($product->id, $ordersArray, true, $unit);
  626. if ($quantity) {
  627. $productsHasQuantity[$product->id] += $quantity;
  628. }
  629. }
  630. if ($productsHasQuantity[$product->id] > 0 || $optionCsvExportAllProducts) {
  631. $productName = $product->getNameExport();
  632. if ($optionCsvExportByPiece) {
  633. $productUnit = 'piece';
  634. } else {
  635. $productUnit = $product->unit;
  636. }
  637. $productName .= ' (' . Product::strUnit($productUnit, 'wording_short', true) . ')';
  638. $productsNameArray[] = $productName;
  639. $productsIndexArray[$product->id] = $cpt++;
  640. }
  641. }
  642. $datas[] = $productsNameArray;
  643. // points de vente
  644. foreach ($pointsSaleArray as $pointSale) {
  645. if (count($pointSale->orders)) {
  646. // listing commandes
  647. $datas[] = ['> ' . $pointSale->name];
  648. foreach ($pointSale->orders as $order) {
  649. $orderLine = [$order->getStrUser(), $order->getCommentReport()];
  650. if ($optionCsvExportByPiece) {
  651. foreach ($order->productOrder as $productOrder) {
  652. $orderLine[$productsIndexArray[$productOrder->id_product]] = Order::getProductQuantityPieces(
  653. $productOrder->id_product,
  654. [$order]
  655. );
  656. }
  657. } else {
  658. foreach ($productsIndexArray as $idProduct => $indexProduct) {
  659. $orderLine[$indexProduct] = '';
  660. }
  661. foreach ($order->productOrder as $productOrder) {
  662. if (strlen($orderLine[$productsIndexArray[$productOrder->id_product]])) {
  663. $orderLine[$productsIndexArray[$productOrder->id_product]] .= ' + ';
  664. }
  665. $orderLine[$productsIndexArray[$productOrder->id_product]] .= $productOrder->quantity;
  666. if ($productOrder->product->unit != $productOrder->unit) {
  667. $orderLine[$productsIndexArray[$productOrder->id_product]] .= Product::strUnit(
  668. $productOrder->unit,
  669. 'wording_short',
  670. true
  671. );
  672. }
  673. }
  674. }
  675. $datas[] = $this->_lineOrderReportCSV($orderLine, $cpt);
  676. }
  677. // total point de vente
  678. if ($optionCsvExportByPiece) {
  679. $totalsPointSaleArray = $this->_totalReportPiecesCSV(
  680. 'Total',
  681. $pointSale->orders,
  682. $productsArray,
  683. $productsIndexArray
  684. );
  685. } else {
  686. $totalsPointSaleArray = $this->_totalReportCSV(
  687. 'Total',
  688. $pointSale->orders,
  689. $productsArray,
  690. $productsIndexArray
  691. );
  692. }
  693. $datas[] = $this->_lineOrderReportCSV($totalsPointSaleArray, $cpt);
  694. $datas[] = [];
  695. }
  696. }
  697. // global
  698. if ($optionCsvExportByPiece) {
  699. $totalsGlobalArray = $this->_totalReportPiecesCSV(
  700. '> Totaux',
  701. $ordersArray,
  702. $productsArray,
  703. $productsIndexArray
  704. );
  705. } else {
  706. $totalsGlobalArray = $this->_totalReportCSV(
  707. '> Totaux',
  708. $ordersArray,
  709. $productsArray,
  710. $productsIndexArray
  711. );
  712. }
  713. $datas[] = $this->_lineOrderReportCSV($totalsGlobalArray, $cpt);
  714. CSV::downloadSendHeaders('Commandes_' . $date . '.csv');
  715. echo CSV::array2csv($datas);
  716. die();
  717. }
  718. }
  719. return null;
  720. }
  721. public function actionReportGrid($date = '', $save = false, $idProducer = 0, $type = "pdf")
  722. {
  723. if (!Yii::$app->user->isGuest) {
  724. $idProducer = GlobalParam::getCurrentProducerId();
  725. }
  726. $distribution = DistributionModel::searchOne([
  727. 'id_producer' => $idProducer
  728. ], [
  729. 'conditions' => 'date LIKE :date',
  730. 'params' => [':date' => $date]
  731. ]);
  732. if ($distribution) {
  733. $ordersArray = Order::searchAll(
  734. [
  735. 'distribution.date' => $date,
  736. 'distribution.id_producer' => $idProducer
  737. ],
  738. [
  739. 'orderby' => 'user.lastname ASC, user.name ASC, comment_point_sale ASC',
  740. 'conditions' => 'date_delete IS NULL'
  741. ]
  742. );
  743. $selectedProductsArray = ProductDistributionModel::searchByDistribution($distribution->id);
  744. $pointsSaleArray = PointSale::searchAll([
  745. 'point_sale.id_producer' => $idProducer
  746. ]);
  747. foreach ($pointsSaleArray as $pointSale) {
  748. $pointSale->initOrders($ordersArray);
  749. }
  750. $ordersByPage = 22;
  751. $nbPages = ceil(count($ordersArray) / $ordersByPage);
  752. $ordersArrayPaged = [];
  753. foreach ($pointsSaleArray as $pointSale) {
  754. $index = 0;
  755. $indexPage = 0;
  756. foreach ($pointSale->orders as $order) {
  757. if (!isset($ordersArrayPaged[$pointSale->id])) {
  758. $ordersArrayPaged[$pointSale->id] = [];
  759. }
  760. if (!isset($ordersArrayPaged[$pointSale->id][$indexPage])) {
  761. $ordersArrayPaged[$pointSale->id][$indexPage] = [];
  762. }
  763. $ordersArrayPaged[$pointSale->id][$indexPage][] = $order;
  764. $index++;
  765. if ($index == $ordersByPage) {
  766. $index = 0;
  767. $indexPage++;
  768. }
  769. }
  770. }
  771. // catégories
  772. $categoriesArray = ProductCategory::searchAll(
  773. ['id_producer' => $idProducer],
  774. ['orderby' => 'product_category.position ASC']
  775. );
  776. array_unshift($categoriesArray, null);
  777. // produits
  778. $productsArray = Product::find()
  779. ->joinWith([
  780. 'productDistribution' => function ($q) use ($distribution) {
  781. $q->where(['id_distribution' => $distribution->id]);
  782. }
  783. ])
  784. ->where([
  785. 'id_producer' => $idProducer,
  786. ])
  787. ->orderBy('order ASC')
  788. ->all();
  789. $viewPdf = 'report-grid';
  790. $orientationPdf = Pdf::ORIENT_PORTRAIT;
  791. $producer = GlobalParam::getCurrentProducer();
  792. if ($producer->slug == 'bourlinguepacotille') {
  793. $viewPdf = 'report-bourlingue';
  794. $orientationPdf = Pdf::ORIENT_LANDSCAPE;
  795. }
  796. // get your HTML raw content without any layouts or scripts
  797. $content = $this->renderPartial($viewPdf, [
  798. 'date' => $date,
  799. 'distribution' => $distribution,
  800. 'selectedProductsArray' => $selectedProductsArray,
  801. 'pointsSaleArray' => $pointsSaleArray,
  802. 'categoriesArray' => $categoriesArray,
  803. 'productsArray' => $productsArray,
  804. 'ordersArray' => $ordersArrayPaged,
  805. 'producer' => Producer::searchOne(['id' => $idProducer])
  806. ]);
  807. $dateStr = date('d/m/Y', strtotime($date));
  808. if ($save) {
  809. $destination = Pdf::DEST_FILE;
  810. } else {
  811. $destination = Pdf::DEST_BROWSER;
  812. }
  813. $pdf = new Pdf([
  814. // set to use core fonts only
  815. 'mode' => Pdf::MODE_UTF8,
  816. // A4 paper format
  817. 'format' => Pdf::FORMAT_A4,
  818. // portrait orientation
  819. 'orientation' => $orientationPdf,
  820. // stream to browser inline
  821. 'destination' => $destination,
  822. 'filename' => Yii::getAlias(
  823. '@app/web/pdf/Commandes-' . $date . '-' . $idProducer . '.pdf'
  824. ),
  825. // your html content input
  826. 'content' => $content,
  827. // format content from your own css file if needed or use the
  828. // enhanced bootstrap css built by Krajee for mPDF formatting
  829. //'cssFile' => Yii::getAlias('@web/css/distribution/report.css'),
  830. // any css to be embedded if required
  831. 'cssInline' => '
  832. table {
  833. border-spacing : 0px ;
  834. border-collapse : collapse ;
  835. width: 100% ;
  836. }
  837. table tr th,
  838. table tr td {
  839. padding: 0px ;
  840. margin: 0px ;
  841. border: solid 1px #e0e0e0 ;
  842. padding: 3px ;
  843. vertical-align : top;
  844. page-break-inside: avoid !important;
  845. }
  846. table tr th {
  847. font-size: 10px ;
  848. }
  849. table tr td {
  850. font-size: 10px ;
  851. }
  852. table thead tr {
  853. line-height: 220px;
  854. text-align:left;
  855. }
  856. .th-user,
  857. .td-nb-products {
  858. /* width: 35px ; */
  859. text-align: center ;
  860. }
  861. .th-user {
  862. padding: 10px ;
  863. }
  864. .category-name {
  865. font-weight: bold ;
  866. }
  867. ',
  868. // set mPDF properties on the fly
  869. //'options' => ['title' => 'Krajee Report Title'],
  870. // call mPDF methods on the fly
  871. 'methods' => [
  872. 'SetHeader' => ['Commandes du ' . $dateStr],
  873. 'SetFooter' => ['{PAGENO}'],
  874. ]
  875. ]);
  876. // return the pdf output as per the destination setting
  877. return $pdf->render();
  878. }
  879. }
  880. /**
  881. * Génère un export des commandes au format CSV à destination du Google Drive
  882. * de Terre de pains.
  883. *
  884. * @param type $date
  885. * @return CSV
  886. */
  887. public function actionReportTerredepains($date, $key)
  888. {
  889. if ($key == 'ef572cc148c001f0180c4a624189ed30') {
  890. $producer = Producer::searchOne([
  891. 'producer.slug' => 'terredepains'
  892. ]);
  893. $idProducer = $producer->id;
  894. $ordersArray = Order::searchAll(
  895. [
  896. 'distribution.date' => $date,
  897. 'distribution.id_producer' => $idProducer
  898. ],
  899. [
  900. 'orderby' => 'user.lastname ASC, user.name ASC, comment_point_sale ASC',
  901. 'conditions' => 'date_delete IS NULL'
  902. ]
  903. );
  904. $distribution = DistributionModel::searchOne([
  905. 'distribution.id_producer' => $idProducer
  906. ], [
  907. 'conditions' => 'date LIKE :date',
  908. 'params' => [
  909. ':date' => $date,
  910. ]
  911. ]);
  912. if ($distribution) {
  913. $selectedProductsArray = ProductDistributionModel::searchByDistribution($distribution->id);
  914. $pointsSaleArray = PointSale::searchAll([
  915. 'point_sale.id_producer' => $idProducer
  916. ]);
  917. foreach ($pointsSaleArray as $pointSale) {
  918. $pointSale->initOrders($ordersArray);
  919. }
  920. // produits
  921. $productsArray = Product::find()
  922. ->joinWith([
  923. 'productDistribution' => function ($q) use ($distribution) {
  924. $q->where(['id_distribution' => $distribution->id]);
  925. }
  926. ])
  927. ->where([
  928. 'id_producer' => $idProducer,
  929. ])
  930. ->orderBy('order ASC')
  931. ->all();
  932. $datas = [];
  933. // produits en colonne
  934. $productsNameArray = [''];
  935. $productsIndexArray = [];
  936. $productsHasQuantity = [];
  937. $cpt = 1;
  938. foreach ($productsArray as $product) {
  939. $theUnit = Product::strUnit($product->unit, 'wording_short', true);
  940. $theUnit = ($theUnit == 'p.') ? '' : ' (' . $theUnit . ')';
  941. $productsNameArray[] = $product->name . $theUnit;
  942. $productsIndexArray[$product->id] = $cpt++;
  943. }
  944. $productsNameArray[] = 'Total';
  945. $datas[] = $productsNameArray;
  946. // global
  947. $totalsGlobalArray = $this->_totalReportCSV(
  948. '> Totaux',
  949. $ordersArray,
  950. $productsArray,
  951. $productsIndexArray
  952. );
  953. $datas[] = $this->_lineOrderReportCSV($totalsGlobalArray, $cpt - 1, true);
  954. $datas[] = [];
  955. // points de vente
  956. foreach ($pointsSaleArray as $pointSale) {
  957. if (count($pointSale->orders)) {
  958. // listing commandes
  959. /*foreach ($pointSale->orders as $order) {
  960. $orderLine = [$order->getStrUser()];
  961. foreach ($productsIndexArray as $idProduct => $indexProduct) {
  962. $orderLine[$indexProduct] = '';
  963. }
  964. foreach ($order->productOrder as $productOrder) {
  965. if (strlen($orderLine[$productsIndexArray[$productOrder->id_product]])) {
  966. $orderLine[$productsIndexArray[$productOrder->id_product]] .= ' + ';
  967. }
  968. $orderLine[$productsIndexArray[$productOrder->id_product]] .= $productOrder->quantity;
  969. if ($productOrder->product->unit != $productOrder->unit) {
  970. $orderLine[$productsIndexArray[$productOrder->id_product]] .= Product::strUnit($productOrder->unit, 'wording_short', true);
  971. }
  972. }
  973. $datas[] = $this->_lineOrderReportCSV($orderLine, $cpt - 1, true);
  974. }*/
  975. // total point de vente
  976. $totalsPointSaleArray = $this->_totalReportCSV(
  977. '> ' . $pointSale->name,
  978. $pointSale->orders,
  979. $productsArray,
  980. $productsIndexArray
  981. );
  982. $datas[] = $this->_lineOrderReportCSV($totalsPointSaleArray, $cpt - 1, true);
  983. }
  984. }
  985. CSV::downloadSendHeaders('Commandes_' . $date . '.csv');
  986. echo CSV::array2csv($datas);
  987. die();
  988. }
  989. return null;
  990. }
  991. }
  992. public function _totalReportCSV($label, $ordersArray, $productsArray, $productsIndexArray)
  993. {
  994. $totalsPointSaleArray = [$label];
  995. foreach ($productsArray as $product) {
  996. foreach ( Product::$unitsArray as $unit => $dataUnit) {
  997. $quantity = Order::getProductQuantity($product->id, $ordersArray, false, $unit);
  998. if ($quantity) {
  999. $index = $productsIndexArray[$product->id];
  1000. if (!isset($totalsPointSaleArray[$index])) {
  1001. $totalsPointSaleArray[$index] = '';
  1002. }
  1003. if (strlen($totalsPointSaleArray[$index])) {
  1004. $totalsPointSaleArray[$index] .= ' + ';
  1005. }
  1006. $totalsPointSaleArray[$index] .= $quantity;
  1007. if ($product->unit != $unit) {
  1008. $totalsPointSaleArray[$index] .= '' . Product::strUnit($unit, 'wording_short', true);
  1009. }
  1010. }
  1011. }
  1012. }
  1013. return $totalsPointSaleArray;
  1014. }
  1015. public function _totalReportPiecesCSV($label, $ordersArray, $productsArray, $productsIndexArray)
  1016. {
  1017. $totalsPointSaleArray = [$label];
  1018. foreach ($productsArray as $product) {
  1019. $quantity = 0;
  1020. foreach ( Product::$unitsArray as $unit => $dataUnit) {
  1021. $quantityProduct = Order::getProductQuantity($product->id, $ordersArray, false, $unit);
  1022. if ($unit == 'piece') {
  1023. $quantity += $quantityProduct;
  1024. } else {
  1025. if ($product->weight > 0) {
  1026. $quantity += ($quantityProduct * $dataUnit['coefficient']) / $product->weight;
  1027. }
  1028. }
  1029. }
  1030. if ($quantity) {
  1031. $index = $productsIndexArray[$product->id];
  1032. $totalsPointSaleArray[$index] = $quantity;
  1033. }
  1034. }
  1035. return $totalsPointSaleArray;
  1036. }
  1037. public function _lineOrderReportCSV($orderLine, $cptMax, $showTotal = false)
  1038. {
  1039. $line = [];
  1040. $cptTotal = 0;
  1041. for ($i = 0; $i <= $cptMax; $i++) {
  1042. if (isset($orderLine[$i]) && $orderLine[$i]) {
  1043. $line[] = $orderLine[$i];
  1044. if (is_numeric($orderLine[$i])) {
  1045. $cptTotal += $orderLine[$i];
  1046. }
  1047. } else {
  1048. $line[] = '';
  1049. }
  1050. }
  1051. if ($cptTotal > 0 && $showTotal) {
  1052. $line[] = $cptTotal;
  1053. }
  1054. return $line;
  1055. }
  1056. public function actionAjaxProcessProductQuantityMax($idDistribution, $idProduct, $quantityMax)
  1057. {
  1058. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  1059. $productDistribution = ProductDistributionModel::searchOne([
  1060. 'id_distribution' => $idDistribution,
  1061. 'id_product' => $idProduct,
  1062. ]);
  1063. $productDistribution->quantity_max = (!$quantityMax) ? null : (float)$quantityMax;
  1064. $productDistribution->save();
  1065. return ['success'];
  1066. }
  1067. public function actionAjaxProcessActiveProduct($idDistribution, $idProduct, $active)
  1068. {
  1069. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  1070. $productDistribution = ProductDistributionModel::searchOne([
  1071. 'id_distribution' => $idDistribution,
  1072. 'id_product' => $idProduct,
  1073. ]);
  1074. $productDistribution->active = $active;
  1075. $productDistribution->save();
  1076. return ['success'];
  1077. }
  1078. public function actionAjaxProcessActivePointSale($idDistribution, $idPointSale, $delivery)
  1079. {
  1080. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  1081. $pointSaleDistribution = PointSaleDistributionModel::searchOne([
  1082. 'id_distribution' => $idDistribution,
  1083. 'id_point_sale' => $idPointSale,
  1084. ]);
  1085. $pointSaleDistribution->delivery = $delivery;
  1086. $pointSaleDistribution->save();
  1087. return ['success'];
  1088. }
  1089. /**
  1090. * Active/désactive un jour de distribution.
  1091. *
  1092. * @param integer $idDistribution
  1093. * @param string $date
  1094. * @param boolean $active
  1095. * @return array
  1096. */
  1097. public function actionAjaxProcessActiveDistribution($idDistribution = 0, $date = '', $active)
  1098. {
  1099. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  1100. if ($idDistribution) {
  1101. $distribution = DistributionModel::searchOne([
  1102. 'id' => $idDistribution
  1103. ]);
  1104. }
  1105. $format = 'Y-m-d';
  1106. $dateObject = DateTime::createFromFormat($format, $date);
  1107. if ($dateObject && $dateObject->format($format) === $date) {
  1108. $distribution = DistributionModel::initDistribution($date);
  1109. }
  1110. if ($distribution) {
  1111. $distribution->active($active);
  1112. return ['success'];
  1113. }
  1114. return ['error'];
  1115. }
  1116. /**
  1117. * Change l'état d'une semaine de production (activé, désactivé).
  1118. *
  1119. * @param string $date
  1120. * @param integer $active
  1121. */
  1122. public function actionAjaxProcessActiveWeekDistribution($date, $active)
  1123. {
  1124. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  1125. $week = sprintf('%02d', date('W', strtotime($date)));
  1126. $start = strtotime(date('Y', strtotime($date)) . 'W' . $week);
  1127. $dateMonday = date('Y-m-d', strtotime('Monday', $start));
  1128. $dateTuesday = date('Y-m-d', strtotime('Tuesday', $start));
  1129. $dateWednesday = date('Y-m-d', strtotime('Wednesday', $start));
  1130. $dateThursday = date('Y-m-d', strtotime('Thursday', $start));
  1131. $dateFriday = date('Y-m-d', strtotime('Friday', $start));
  1132. $dateSaturday = date('Y-m-d', strtotime('Saturday', $start));
  1133. $dateSunday = date('Y-m-d', strtotime('Sunday', $start));
  1134. $pointsSaleArray = PointSale::searchAll();
  1135. $activeMonday = false;
  1136. $activeTuesday = false;
  1137. $activeWednesday = false;
  1138. $activeThursday = false;
  1139. $activeFriday = false;
  1140. $activeSaturday = false;
  1141. $activeSunday = false;
  1142. foreach ($pointsSaleArray as $pointSale) {
  1143. if ($pointSale->delivery_monday) {
  1144. $activeMonday = true;
  1145. }
  1146. if ($pointSale->delivery_tuesday) {
  1147. $activeTuesday = true;
  1148. }
  1149. if ($pointSale->delivery_wednesday) {
  1150. $activeWednesday = true;
  1151. }
  1152. if ($pointSale->delivery_thursday) {
  1153. $activeThursday = true;
  1154. }
  1155. if ($pointSale->delivery_friday) {
  1156. $activeFriday = true;
  1157. }
  1158. if ($pointSale->delivery_saturday) {
  1159. $activeSaturday = true;
  1160. }
  1161. if ($pointSale->delivery_sunday) {
  1162. $activeSunday = true;
  1163. }
  1164. }
  1165. if ($activeMonday || !$active) {
  1166. $this->actionAjaxProcessActiveDistribution(0, $dateMonday, $active);
  1167. }
  1168. if ($activeTuesday || !$active) {
  1169. $this->actionAjaxProcessActiveDistribution(0, $dateTuesday, $active);
  1170. }
  1171. if ($activeWednesday || !$active) {
  1172. $this->actionAjaxProcessActiveDistribution(0, $dateWednesday, $active);
  1173. }
  1174. if ($activeThursday || !$active) {
  1175. $this->actionAjaxProcessActiveDistribution(0, $dateThursday, $active);
  1176. }
  1177. if ($activeFriday || !$active) {
  1178. $this->actionAjaxProcessActiveDistribution(0, $dateFriday, $active);
  1179. }
  1180. if ($activeSaturday || !$active) {
  1181. $this->actionAjaxProcessActiveDistribution(0, $dateSaturday, $active);
  1182. }
  1183. if ($activeSunday || !$active) {
  1184. $this->actionAjaxProcessActiveDistribution(0, $dateSunday, $active);
  1185. }
  1186. return ['success'];
  1187. }
  1188. /**
  1189. * Ajoute les commandes récurrentes pour une date donnée.
  1190. *
  1191. * @param string $date
  1192. */
  1193. public function actionAjaxProcessAddSubscriptions($date)
  1194. {
  1195. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  1196. Subscription::addAll($date, true);
  1197. return ['success'];
  1198. }
  1199. /**
  1200. * Synchronise les commandes avec la plateforme Tiller pour une date donnée.
  1201. *
  1202. * @param string $date
  1203. */
  1204. public function actionAjaxProcessSynchroTiller($date)
  1205. {
  1206. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  1207. $return = [];
  1208. $producerTiller = Producer::getConfig('tiller');
  1209. if ($producerTiller) {
  1210. $tiller = new Tiller();
  1211. $isSynchro = $tiller->isSynchro($date);
  1212. if (!$isSynchro) {
  1213. $orders = Order::searchAll([
  1214. 'distribution.date' => $date,
  1215. 'order.tiller_synchronization' => 1
  1216. ], [
  1217. 'conditions' => 'date_delete IS NULL'
  1218. ]);
  1219. $strDate = date('Y-m-d\T12:i:s+0000', strtotime($date) + 1);
  1220. if ($orders && count($orders)) {
  1221. foreach ($orders as $order) {
  1222. $lines = [];
  1223. foreach ($order->productOrder as $productOrder) {
  1224. $lines[] = [
  1225. 'name' => $productOrder->product->name,
  1226. 'price' => $productOrder->getPriceWithTax() * 100 * $productOrder->quantity,
  1227. 'tax' => $productOrder->taxRate->value * 100,
  1228. 'date' => $strDate,
  1229. 'quantity' => $productOrder->quantity
  1230. ];
  1231. }
  1232. $typePaymentTiller = '';
  1233. if ($order->mean_payment == MeanPayment::MONEY) {
  1234. $typePaymentTiller = 'CASH';
  1235. }
  1236. if ($order->mean_payment == MeanPayment::CREDIT_CARD
  1237. || $order->mean_payment == MeanPayment::CREDIT
  1238. || $order->mean_payment == MeanPayment::TRANSFER
  1239. || $order->mean_payment == MeanPayment::OTHER) {
  1240. $typePaymentTiller = 'CARD';
  1241. }
  1242. if ($order->mean_payment == MeanPayment::CHEQUE) {
  1243. $typePaymentTiller = 'BANK_CHECK';
  1244. }
  1245. if (!strlen($typePaymentTiller) || !$order->mean_payment) {
  1246. $typePaymentTiller = 'CASH';
  1247. }
  1248. $returnTiller = $tiller->postOrder([
  1249. 'externalId' => $order->id,
  1250. 'type' => 1,
  1251. 'status' => 'IN_PROGRESS',
  1252. 'openDate' => $strDate,
  1253. 'closeDate' => $strDate,
  1254. 'lines' => $lines,
  1255. 'payments' => [
  1256. [
  1257. 'type' => $typePaymentTiller,
  1258. 'amount' => $order->getAmountWithTax(
  1259. Order::AMOUNT_TOTAL
  1260. ) * 100,
  1261. 'status' => 'ACCEPTED',
  1262. 'date' => $strDate
  1263. ]
  1264. ]
  1265. ]);
  1266. $returnTillerObject = json_decode($returnTiller);
  1267. $order->tiller_external_id = '' . $returnTillerObject->id;
  1268. $order->save();
  1269. $return[] = $returnTiller;
  1270. }
  1271. }
  1272. }
  1273. }
  1274. return $return;
  1275. }
  1276. public function actionAjaxValidateDeliveryNotes($idOrders)
  1277. {
  1278. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  1279. if (strlen($idOrders)) {
  1280. $idOrders = json_decode($idOrders, true);
  1281. if (is_array($idOrders) && count($idOrders) > 0) {
  1282. foreach ($idOrders as $idOrder) {
  1283. $order = Order::searchOne([
  1284. 'id' => (int)$idOrder
  1285. ]);
  1286. if ($order && $order->distribution->id_producer == GlobalParam::getCurrentProducerId()) {
  1287. $deliveryNote = DeliveryNote::searchOne([
  1288. 'id' => (int)$order->id_delivery_note
  1289. ]);
  1290. if ($deliveryNote && $deliveryNote->isStatusDraft()) {
  1291. $deliveryNote->changeStatus(Document::STATUS_VALID);
  1292. $deliveryNote->save();
  1293. }
  1294. }
  1295. }
  1296. return [
  1297. 'return' => 'success',
  1298. 'alert' => [
  1299. 'type' => 'success',
  1300. 'message' => 'Bon(s) de livraison validé(s)'
  1301. ]
  1302. ];
  1303. }
  1304. }
  1305. return [
  1306. 'return' => 'error',
  1307. 'alert' => [
  1308. 'type' => 'danger',
  1309. 'message' => 'Une erreur est survenue lors de la validation des bons de livraison'
  1310. ]
  1311. ];
  1312. }
  1313. public function actionAjaxGenerateDeliveryNoteEachUser($idOrders)
  1314. {
  1315. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  1316. if (strlen($idOrders)) {
  1317. $idOrders = json_decode($idOrders, true);
  1318. if (is_array($idOrders) && count($idOrders) > 0) {
  1319. foreach ($idOrders as $idOrder) {
  1320. $order = Order::searchOne([
  1321. 'id' => (int)$idOrder
  1322. ]);
  1323. if ($order && $order->distribution->id_producer == GlobalParam::getCurrentProducerId() && $order->id_user) {
  1324. $deliveryNote = null;
  1325. $idDeliveryNote = $order->id_delivery_note;
  1326. if ($idDeliveryNote) {
  1327. $deliveryNote = DeliveryNote::searchOne([
  1328. 'id' => (int)$idDeliveryNote
  1329. ]);
  1330. }
  1331. // on regénére le document si c'est un brouillon
  1332. if ($deliveryNote && $deliveryNote->isStatusDraft()) {
  1333. $deliveryNote->delete();
  1334. $deliveryNote = null;
  1335. }
  1336. if (!$deliveryNote) {
  1337. $deliveryNote = new DeliveryNote();
  1338. $deliveryNote->initTaxCalculationMethod();
  1339. $deliveryNote->id_producer = GlobalParam::getCurrentProducerId();
  1340. $deliveryNote->id_user = $order->id_user;
  1341. $deliveryNote->name = 'Bon de livraison ' . $order->getUsername() . ' (' . date(
  1342. 'd/m/Y',
  1343. strtotime(
  1344. $order->distribution->date
  1345. )
  1346. ) . ')';
  1347. $deliveryNote->address = $order->user->getFullAddress();
  1348. $deliveryNote->save();
  1349. }
  1350. if ($deliveryNote) {
  1351. $order->id_delivery_note = $deliveryNote->id;
  1352. $order->save();
  1353. // init invoice prices
  1354. $user = User::searchOne([
  1355. 'id' => $deliveryNote->id_user
  1356. ]);
  1357. $userProducer = UserProducer::searchOne([
  1358. 'id_user' => $deliveryNote->id_user,
  1359. 'id_producer' => GlobalParam::getCurrentProducerId()
  1360. ]);
  1361. $order->initInvoicePrices([
  1362. 'user' => $user,
  1363. 'user_producer' => $userProducer,
  1364. 'point_sale' => $order->pointSale
  1365. ]);
  1366. }
  1367. }
  1368. }
  1369. }
  1370. return [
  1371. 'return' => 'success',
  1372. 'alert' => [
  1373. 'type' => 'success',
  1374. 'message' => 'Bon(s) de livraison généré(s)'
  1375. ]
  1376. ];
  1377. }
  1378. return [
  1379. 'return' => 'error',
  1380. 'alert' => [
  1381. 'type' => 'danger',
  1382. 'message' => 'Une erreur est survenue lors de la génération du bon de livraison.'
  1383. ]
  1384. ];
  1385. }
  1386. public function actionAjaxGenerateDeliveryNote($idOrders)
  1387. {
  1388. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  1389. if (strlen($idOrders)) {
  1390. $idOrders = json_decode($idOrders, true);
  1391. if (is_array($idOrders) && count($idOrders) > 0) {
  1392. // récupération première commande pour obtenir des infos
  1393. reset($idOrders);
  1394. $firstOrder = Order::searchOne([
  1395. 'id' => (int)$idOrders[key($idOrders)]
  1396. ]);
  1397. // deliveryNote existant
  1398. $deliveryNote = null;
  1399. $isUpdate = false;
  1400. $i = 0;
  1401. $ordersArray = Order::searchAll([
  1402. 'id' => $idOrders,
  1403. ]);
  1404. do {
  1405. $order = $ordersArray[$i];
  1406. if ($order->distribution->id_producer == GlobalParam::getCurrentProducerId() && $order->id_delivery_note > 0) {
  1407. $deliveryNote = DeliveryNote::searchOne([
  1408. 'id' => $order->id_delivery_note
  1409. ]);
  1410. $isUpdate = true;
  1411. }
  1412. $i++;
  1413. } while ($deliveryNote == null && isset($ordersArray[$i]));
  1414. if ($deliveryNote && $deliveryNote->status == Document::STATUS_VALID) {
  1415. return [
  1416. 'return' => 'error',
  1417. 'alert' => [
  1418. 'type' => 'danger',
  1419. 'message' => 'Vous ne pouvez pas modifier un bon de livraison déjà validé.'
  1420. ]
  1421. ];
  1422. }
  1423. if ($firstOrder) {
  1424. // génération du BL
  1425. if (!$deliveryNote) {
  1426. $deliveryNote = new DeliveryNote;
  1427. $deliveryNote->initTaxCalculationMethod();
  1428. $deliveryNote->name = 'Bon de livraison ' . $firstOrder->pointSale->name . ' (' . date(
  1429. 'd/m/Y',
  1430. strtotime(
  1431. $firstOrder->distribution->date
  1432. )
  1433. ) . ')';
  1434. $deliveryNote->id_producer = GlobalParam::getCurrentProducerId();
  1435. if ($firstOrder->pointSale->id_user) {
  1436. $deliveryNote->id_user = $firstOrder->pointSale->id_user;
  1437. $user = User::searchOne([
  1438. 'id' => $deliveryNote->id_user
  1439. ]);
  1440. $userProducer = UserProducer::searchOne([
  1441. 'id_user' => $deliveryNote->id_user,
  1442. 'id_producer' => GlobalParam::getCurrentProducerId()
  1443. ]);
  1444. } else {
  1445. $user = new User;
  1446. $user->type = User::TYPE_LEGAL_PERSON;
  1447. $user->name_legal_person = $firstOrder->pointSale->name;
  1448. $user->address = $firstOrder->pointSale->address;
  1449. $user->id_producer = 0;
  1450. $user->setPassword(Password::generate());
  1451. $user->generateAuthKey();
  1452. $user->email = '';
  1453. if (!strlen($user->email)) {
  1454. $user->username = 'inconnu@opendistrib.net';
  1455. }
  1456. $user->save();
  1457. $userProducer = new UserProducer;
  1458. $userProducer->id_user = $user->id;
  1459. $userProducer->id_producer = GlobalParam::getCurrentProducerId();
  1460. $userProducer->credit = 0;
  1461. $userProducer->active = 1;
  1462. $userProducer->save();
  1463. $firstOrder->pointSale->id_user = $user->id;
  1464. $firstOrder->pointSale->save();
  1465. $deliveryNote->id_user = $user->id;
  1466. }
  1467. $deliveryNote->address = $user->getFullAddress();
  1468. $deliveryNote->save();
  1469. } else {
  1470. // réinitialisation des order.id_delivery_note
  1471. Order::updateAll([
  1472. 'id_delivery_note' => null
  1473. ], [
  1474. 'id_delivery_note' => $deliveryNote->id
  1475. ]);
  1476. }
  1477. if (!isset($user) || !$user) {
  1478. $user = User::searchOne([
  1479. 'id' => $deliveryNote->id_user
  1480. ]);
  1481. $userProducer = UserProducer::searchOne([
  1482. 'id_user' => $deliveryNote->id_user,
  1483. 'id_producer' => GlobalParam::getCurrentProducerId()
  1484. ]);
  1485. }
  1486. // affectation du BL aux commandes
  1487. foreach ($idOrders as $idOrder) {
  1488. $order = Order::searchOne([
  1489. 'id' => (int)$idOrder
  1490. ]);
  1491. if ($order && $order->distribution->id_producer == GlobalParam::getCurrentProducerId()) {
  1492. $order->id_delivery_note = $deliveryNote->id;
  1493. $order->save();
  1494. }
  1495. // init invoice price
  1496. $order = Order::searchOne(['id' => $idOrder]);
  1497. if ($order) {
  1498. $order->initInvoicePrices([
  1499. 'user' => $user,
  1500. 'user_producer' => $userProducer,
  1501. 'point_sale' => $firstOrder->pointSale
  1502. ]);
  1503. }
  1504. }
  1505. return [
  1506. 'return' => 'success',
  1507. 'alert' => [
  1508. 'type' => 'success',
  1509. 'message' => 'Bon de livraison ' . ($isUpdate ? 'modifié' : 'généré')
  1510. ]
  1511. ];
  1512. }
  1513. }
  1514. }
  1515. return [
  1516. 'return' => 'error',
  1517. 'alert' => [
  1518. 'type' => 'danger',
  1519. 'message' => 'Une erreur est survenue lors de la génération du bon de livraison.'
  1520. ]
  1521. ];
  1522. }
  1523. }