You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OrderController.php 25KB

6 年之前
6 年之前
6 年之前
6 年之前
6 年之前
6 年之前
6 年之前
6 年之前
6 年之前
6 年之前
6 年之前
6 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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 producer\controllers;
  32. use common\models\ProductDistribution ;
  33. use common\models\User ;
  34. use common\models\Producer ;
  35. use common\models\Order ;
  36. use common\models\UserPointSale ;
  37. use common\models\Product ;
  38. use DateTime;
  39. class OrderController extends ProducerBaseController
  40. {
  41. var $enableCsrfValidation = false;
  42. public function behaviors()
  43. {
  44. return [
  45. 'access' => [
  46. 'class' => AccessControl::className(),
  47. 'rules' => [
  48. [
  49. 'allow' => true,
  50. 'roles' => ['@'],
  51. ]
  52. ],
  53. ],
  54. ];
  55. }
  56. public function actionOrder($id = 0, $date = '')
  57. {
  58. $params = [] ;
  59. if($id) {
  60. $order = Order::searchOne([
  61. 'id' => $id
  62. ]) ;
  63. if($order) {
  64. if($order->getState() == Order::STATE_OPEN) {
  65. $params['order'] = $order ;
  66. }
  67. }
  68. }
  69. if(strlen($date)) {
  70. $distribution = Distribution::searchOne([
  71. 'date' => $date,
  72. 'id_producer' => Producer::getId()
  73. ]) ;
  74. if($distribution) {
  75. $params['date'] = $date ;
  76. }
  77. }
  78. return $this->render('order', $params) ;
  79. }
  80. /**
  81. * Affiche l'historique des commandes de l'utilisateur
  82. *
  83. * @return ProducerView
  84. */
  85. public function actionHistory($type = 'incoming')
  86. {
  87. $query = Order::find()
  88. ->with('productOrder', 'pointSale', 'creditHistory')
  89. ->joinWith('distribution', 'distribution.producer')
  90. ->where([
  91. 'id_user' => Yii::$app->user->id,
  92. 'distribution.id_producer' => Producer::getId()
  93. ])
  94. ->params([':date_today' => date('Y-m-d')]);
  95. $queryIncoming = clone $query ;
  96. $queryIncoming->andWhere('distribution.date >= :date_today')->orderBy('distribution.date ASC');
  97. $queryPassed = clone $query ;
  98. $queryPassed->andWhere('distribution.date < :date_today')->orderBy('distribution.date DESC');
  99. $dataProviderOrders = new ActiveDataProvider([
  100. 'query' => ($type == 'incoming') ? $queryIncoming : $queryPassed,
  101. 'pagination' => [
  102. 'pageSize' => 10,
  103. ],
  104. ]);
  105. return $this->render('history', [
  106. 'dataProviderOrders' => $dataProviderOrders,
  107. 'orderOk' => Yii::$app->getRequest()->get('orderOk', false),
  108. 'cancelOk' => Yii::$app->getRequest()->get('cancelOk', false),
  109. 'type' => $type,
  110. 'countIncoming' => $queryIncoming->count(),
  111. 'countPassed' => $queryPassed->count(),
  112. ]);
  113. }
  114. /**
  115. * Supprime un producteur.
  116. *
  117. * @param integer $id
  118. */
  119. public function actionRemoveProducer($id = 0)
  120. {
  121. $userProducer = UserProducer::find()
  122. ->where(['id_producer' => $id, 'id_user' => User::getCurrentId()])
  123. ->one();
  124. $userProducer->active = 0;
  125. $userProducer->save();
  126. $this->redirect(['order/index']);
  127. }
  128. /**
  129. * Crée une commande.
  130. *
  131. * @return mixed
  132. */
  133. public function actionAjaxProcess()
  134. {
  135. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  136. $order = new Order ;
  137. $idProducer = $this->getProducer()->id ;
  138. $posts = Yii::$app->request->post();
  139. if ($idProducer) {
  140. $this->_verifyProducerActive($idProducer);
  141. }
  142. if ($order->load($posts)) {
  143. $order = Order::find()
  144. ->where('id_distribution = :id_distribution')
  145. ->andWhere('id_user = :id_user')
  146. ->params([
  147. ':id_distribution' => $posts['Order']['id_distribution'],
  148. ':id_user' => User::getCurrentId()
  149. ])
  150. ->one();
  151. if (!$order) {
  152. $order = new Order;
  153. $order->load(Yii::$app->request->post());
  154. $order->id_user = User::getCurrentId();
  155. $order->date = date('Y-m-d H:i:s');
  156. $order->origin = Order::ORIGIN_USER;
  157. }
  158. $errors = $this->processForm($order);
  159. if(count($errors)) {
  160. return ['status' => 'error', 'errors' => $errors] ;
  161. }
  162. }
  163. return ['status' => 'success', 'idOrder' => $order->id] ;
  164. }
  165. /**
  166. * Vérifie si un producteur est actif.
  167. *
  168. * @param integer $idProducer
  169. * @throws NotFoundHttpException
  170. */
  171. public function _verifyProducerActive($idProducer)
  172. {
  173. $producer = Producer::findOne($idProducer);
  174. if ($producer && !$producer->active) {
  175. throw new NotFoundHttpException('Ce producteur est actuellement hors ligne.');
  176. }
  177. }
  178. /**
  179. * Traite le formulaire de création/modification de commande.
  180. *
  181. * @param Commande $order
  182. */
  183. public function processForm($order)
  184. {
  185. $posts = Yii::$app->request->post();
  186. $productsArray = [];
  187. $totalQuantity = 0;
  188. $producer = $this->getProducer() ;
  189. foreach ($posts['products'] as $key => $quantity) {
  190. $product = Product::find()->where(['id' => (int) $key])->one();
  191. $totalQuantity += $quantity;
  192. if ($product && $quantity) {
  193. $productsArray[] = $product;
  194. }
  195. }
  196. // date
  197. $errorDate = false;
  198. if (isset($order->id_distribution)) {
  199. // date de commande
  200. $distribution = Distribution::find()->where(['id' => $order->id_distribution])->one();
  201. $date = $this->getProducer()->getEarliestDateOrder() ;
  202. if($order->getState() != Order::STATE_OPEN) {
  203. $errorDate = true;
  204. }
  205. }
  206. // point de vente
  207. $errorPointSale = false;
  208. if (isset($distribution) && $distribution) {
  209. $pointSaleDistribution = PointSaleDistribution::searchOne([
  210. 'id_distribution' => $distribution->id,
  211. 'id_point_sale' => $posts['Order']['id_point_sale']
  212. ]) ;
  213. if (!$pointSaleDistribution || !$pointSaleDistribution->delivery) {
  214. $errorPointSale = true;
  215. }
  216. $pointSale = PointSale::findOne($posts['Order']['id_point_sale']);
  217. if ($pointSale) {
  218. if (strlen($pointSale->code) && !$pointSale->validateCode($posts['code_point_sale'])) {
  219. $errorPointSale = true;
  220. }
  221. } else {
  222. $errorPointSale = true;
  223. }
  224. $userPointSale = UserPointSale::searchOne([
  225. 'id_user' => User::getCurrentId(),
  226. 'id_point_sale' => $pointSale->id
  227. ]) ;
  228. if($pointSale->restricted_access && !$userPointSale) {
  229. $errorPointSale = true;
  230. }
  231. }
  232. $errors = [] ;
  233. if ($order->validate() && count($productsArray) && !$errorDate && !$errorPointSale) {
  234. $userProducer = UserProducer::searchOne([
  235. 'id_producer' => $order->distribution->id_producer,
  236. 'id_user' => User::getCurrentId()
  237. ]) ;
  238. // gestion point de vente
  239. $pointSale = PointSale::searchOne([
  240. 'id' => $order->id_point_sale
  241. ]) ;
  242. $order->comment_point_sale = ($pointSale && strlen($pointSale->getComment())) ?
  243. $pointSale->getComment() : '' ;
  244. // la commande est automatiquement réactivée lors d'une modification
  245. $order->date_delete = null ;
  246. // sauvegarde de la commande
  247. $order->save();
  248. // ajout de l'utilisateur à l'établissement
  249. Producer::addUser(User::getCurrentId(), $distribution->id_producer) ;
  250. // suppression de tous les enregistrements ProductOrder
  251. if (!is_null($order)) {
  252. ProductOrder::deleteAll(['id_order' => $order->id]);
  253. $stepsArray = [] ;
  254. if(isset($order->productOrder)) {
  255. foreach($order->productOrder as $productOrder) {
  256. $unitsArray[$productOrder->id_product] = $productOrder->unit;
  257. }
  258. }
  259. }
  260. // produits dispos
  261. $availableProducts = ProductDistribution::searchByDistribution($distribution->id) ;
  262. // sauvegarde des produits
  263. foreach ($productsArray as $product) {
  264. if (isset($availableProducts[$product->id])) {
  265. $productOrder = new ProductOrder();
  266. $productOrder->id_order = $order->id;
  267. $productOrder->id_product = $product->id;
  268. $productOrder->price = $product->price;
  269. $unit = (!is_null($order) && isset($unitsArray[$product->id])) ? $unitsArray[$product->id] : $product->unit ;
  270. $coefficient = Product::$unitsArray[$unit]['coefficient'] ;
  271. $quantity = ((float) $posts['products'][$product->id]) / $coefficient ;
  272. if ($availableProducts[$product->id]['quantity_max'] && $quantity > $availableProducts[$product->id]['quantity_remaining']) {
  273. $quantity = $availableProducts[$product->id]['quantity_remaining'] ;
  274. }
  275. $productOrder->quantity = $quantity;
  276. $productOrder->unit = $product->unit ;
  277. $productOrder->step = $product->step ;
  278. $productOrder->save();
  279. }
  280. }
  281. // lien utilisateur / point de vente
  282. $pointSale->linkUser(User::getCurrentId()) ;
  283. // credit
  284. $credit = Producer::getConfig('credit');
  285. $creditLimit = Producer::getConfig('credit_limit');
  286. $creditFunctioning = $pointSale->getCreditFunctioning() ;
  287. $creditUser = Yii::$app->user->identity->getCredit($distribution->id_producer);
  288. $order = Order::searchOne([
  289. 'id' => $order->id
  290. ]) ;
  291. $amountPaid = $order->getAmount(Order::AMOUNT_PAID);
  292. $amountRemaining = $order->getAmount(Order::AMOUNT_REMAINING) ;
  293. if($credit && $pointSale->credit &&
  294. ( ($creditFunctioning == Producer::CREDIT_FUNCTIONING_OPTIONAL && $posts['use_credit']) ||
  295. $creditFunctioning == Producer::CREDIT_FUNCTIONING_MANDATORY ||
  296. ($creditFunctioning == Producer::CREDIT_FUNCTIONING_USER && $userProducer->credit_active)
  297. )) {
  298. // à payer
  299. if ($order->getPaymentStatus() == Order::PAYMENT_UNPAID) {
  300. if(!is_null($creditLimit) && $amountRemaining > $creditUser - $creditLimit) {
  301. $amountRemaining = $creditUser - $creditLimit ;
  302. }
  303. if ($amountRemaining > 0) {
  304. $order->saveCreditHistory(
  305. CreditHistory::TYPE_PAYMENT,
  306. $amountRemaining,
  307. $distribution->id_producer,
  308. User::getCurrentId(),
  309. User::getCurrentId()
  310. );
  311. }
  312. }
  313. // surplus à rembourser
  314. elseif ($order->getPaymentStatus() == Order::PAYMENT_SURPLUS) {
  315. $amountSurplus = $order->getAmount(Order::AMOUNT_SURPLUS) ;
  316. $order->saveCreditHistory(
  317. CreditHistory::TYPE_REFUND,
  318. $amountSurplus,
  319. $distribution->id_producer,
  320. User::getCurrentId(),
  321. User::getCurrentId()
  322. );
  323. }
  324. }
  325. }
  326. if (!count($productsArray)) {
  327. $errors[] = "Vous n'avez choisi aucun produit" ;
  328. }
  329. if ($errorDate) {
  330. $errors[] = "Vous ne pouvez pas commander pour cette date." ;
  331. }
  332. if ($errorPointSale) {
  333. $errors[] = "Point de vente invalide." ;
  334. }
  335. return $errors ;
  336. }
  337. /**
  338. * Annule une commande.
  339. *
  340. * @param integer $id
  341. * @throws \yii\web\NotFoundHttpException
  342. * @throws UserException
  343. */
  344. public function actionCancel($id)
  345. {
  346. $order = Order::searchOne([
  347. 'id' => $id
  348. ]) ;
  349. if(!$order) {
  350. throw new \yii\web\NotFoundHttpException('Commande introuvable');
  351. }
  352. if ($order->getState() != Order::STATE_OPEN) {
  353. throw new UserException('Vous ne pouvez plus annuler cette commande.');
  354. }
  355. if ($order && User::getCurrentId() == $order->id_user) {
  356. $order->delete() ;
  357. Yii::$app->session->setFlash('success','Votre commande a bien été annulée.') ;
  358. }
  359. $this->redirect(Yii::$app->urlManager->createUrl(['order/history']));
  360. }
  361. /**
  362. * Vérifie le code saisi pour un point de vente.
  363. *
  364. * @param integer $idPointSale
  365. * @param string $code
  366. * @return boolean
  367. */
  368. public function actionAjaxValidateCodePointSale($idPointSale, $code)
  369. {
  370. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  371. $pointSale = PointSale::findOne($idPointSale);
  372. if ($pointSale) {
  373. if ($pointSale->validateCode($code)) {
  374. return 1;
  375. }
  376. }
  377. return 0;
  378. }
  379. public function actionAjaxInfos($date = '')
  380. {
  381. \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  382. $json = [] ;
  383. $format = 'Y-m-d' ;
  384. $dateObject = DateTime::createFromFormat($format, $date);
  385. // Producteur
  386. $producer = Producer::searchOne([
  387. 'id' => $this->getProducer()->id
  388. ]) ;
  389. $json['producer'] = [
  390. 'order_infos' => $producer->order_infos,
  391. 'credit' => $producer->credit,
  392. 'credit_functioning' => $producer->credit_functioning,
  393. 'use_credit_checked_default' => $producer->use_credit_checked_default,
  394. 'credit_limit' => is_numeric($producer->credit_limit) ? $producer->credit_limit : null
  395. ] ;
  396. // Distributions
  397. $dateMini = $producer->getEarliestDateOrder() ;
  398. $distributionsArray = Distribution::searchAll([
  399. 'active' => 1
  400. ], [
  401. 'conditions' => ['date > :date'],
  402. 'params' => [':date' => $dateMini],
  403. ]) ;
  404. $json['distributions'] = $distributionsArray ;
  405. // Commandes de l'utilisateur
  406. $ordersUserArray = Order::searchAll([
  407. 'id_user' => User::getCurrentId()
  408. ], [
  409. 'conditions' => [
  410. 'distribution.date > :date'
  411. ],
  412. 'params' => [
  413. ':date' => $dateMini
  414. ]
  415. ]);
  416. if(is_array($ordersUserArray) && count($ordersUserArray)) {
  417. foreach($ordersUserArray as &$order) {
  418. $order = array_merge($order->getAttributes(), [
  419. 'amount_total' => $order->getAmount(Order::AMOUNT_TOTAL),
  420. 'date_distribution' => $order->distribution->date,
  421. 'pointSale' => $order->pointSale->getAttributes()
  422. ]) ;
  423. }
  424. $json['orders'] = $ordersUserArray;
  425. }
  426. // User
  427. $userProducer = UserProducer::searchOne([
  428. 'id_producer' => $producer->id,
  429. 'id_user' => User::getCurrentId()
  430. ]) ;
  431. $json['user'] = [
  432. 'credit' => $userProducer->credit,
  433. 'credit_active' => $userProducer->credit_active,
  434. ] ;
  435. if($dateObject && $dateObject->format($format) === $date) {
  436. // Commande de l'utilisateur
  437. $orderUser = Order::searchOne([
  438. 'distribution.date' => $date,
  439. 'id_user' => User::getCurrentId(),
  440. ]);
  441. if($orderUser) {
  442. $json['order'] = array_merge($orderUser->getAttributes(), [
  443. 'amount_total' => $orderUser->getAmount(Order::AMOUNT_TOTAL),
  444. 'amount_paid' => $orderUser->getAmount(Order::AMOUNT_PAID),
  445. ]) ;
  446. }
  447. // distribution
  448. $distribution = Distribution::initDistribution($date) ;
  449. $json['distribution'] = $distribution ;
  450. $pointsSaleArray = PointSale::find()
  451. ->joinWith(['pointSaleDistribution' => function($query) use ($distribution) {
  452. $query->where(['id_distribution' => $distribution->id]);
  453. }
  454. ])
  455. ->with(['userPointSale' => function($query) {
  456. $query->onCondition(['id_user' => User::getCurrentId()]) ;
  457. }])
  458. ->where(['id_producer' => $distribution->id_producer])
  459. ->andWhere('restricted_access = 0 OR (restricted_access = 1 AND (SELECT COUNT(*) FROM user_point_sale WHERE point_sale.id = user_point_sale.id_point_sale AND user_point_sale.id_user = :id_user) > 0)')
  460. ->params([':id_user' => User::getCurrentId()])
  461. ->all();
  462. $creditFunctioningProducer = Producer::getConfig('credit_functioning') ;
  463. foreach($pointsSaleArray as &$pointSale) {
  464. $pointSale = array_merge($pointSale->getAttributes(),[
  465. 'pointSaleDistribution' => [
  466. 'id_distribution' => $pointSale->pointSaleDistribution[0]->id_distribution,
  467. 'id_point_sale' => $pointSale->pointSaleDistribution[0]->id_point_sale,
  468. 'delivery' => $pointSale->pointSaleDistribution[0]->delivery
  469. ],
  470. 'userPointSale' => ($pointSale->userPointSale ? $pointSale->userPointSale[0] : '')
  471. ]) ;
  472. if($pointSale['code'] && strlen($pointSale['code'])) {
  473. $pointSale['code'] = '***' ;
  474. }
  475. if(!strlen($pointSale['credit_functioning'])) {
  476. $pointSale['credit_functioning'] = $creditFunctioningProducer ;
  477. }
  478. }
  479. $favoritePointSale = User::getCurrent()->getFavoritePointSale() ;
  480. if($favoritePointSale) {
  481. for( $i = 0; $i < count($pointsSaleArray) ; $i++) {
  482. if($pointsSaleArray[$i]['id'] == $favoritePointSale->id) {
  483. $theFavoritePointSale = $pointsSaleArray[$i] ;
  484. unset($pointsSaleArray[$i]) ;
  485. }
  486. }
  487. if(isset($theFavoritePointSale)) {
  488. $pointsSaleArray = array_reverse($pointsSaleArray, false) ;
  489. $pointsSaleArray[] = $theFavoritePointSale ;
  490. $pointsSaleArray = array_reverse($pointsSaleArray, false) ;
  491. }
  492. }
  493. $json['points_sale'] = $pointsSaleArray ;
  494. // Commandes totales
  495. $ordersArray = Order::searchAll([
  496. 'distribution.date' => $date,
  497. ]);
  498. // Produits
  499. if(Producer::getConfig('option_allow_user_gift')) {
  500. $productsArray = Product::find()
  501. ->orWhere(['id_producer' => $this->getProducer()->id,])
  502. ->orWhere(['id_producer' => 0,]) ; // produit "Don";
  503. }
  504. else {
  505. $productsArray = Product::find()
  506. ->where(['id_producer' => $this->getProducer()->id,]) ;
  507. }
  508. $productsArray = $productsArray->joinWith(['productDistribution' => function($query) use($distribution) {
  509. $query->andOnCondition('product_distribution.id_distribution = '.$distribution->id) ;
  510. }])
  511. ->orderBy('product_distribution.active DESC, order ASC')
  512. ->asArray()
  513. ->all();
  514. $indexProduct = 0 ;
  515. foreach($productsArray as &$product) {
  516. if(!isset($product['productDistribution'][0])) {
  517. $product['productDistribution'][0] = $distribution->linkProduct($product) ;
  518. }
  519. $coefficient_unit = Product::$unitsArray[$product['unit']]['coefficient'] ;
  520. if(is_null($product['photo'])) {
  521. $product['photo'] = '' ;
  522. }
  523. $product['quantity_max'] = $product['productDistribution'][0]['quantity_max'] ;
  524. $quantityOrder = Order::getProductQuantity($product['id'], $ordersArray) ;
  525. $product['quantity_ordered'] = $quantityOrder ;
  526. $product['quantity_remaining'] = $product['quantity_max'] - $quantityOrder ;
  527. if($orderUser) {
  528. $quantityOrderUser = Order::getProductQuantity($product['id'], [$orderUser], true) ;
  529. $product['quantity_ordered'] = $quantityOrder ;
  530. $product['quantity_remaining'] = $product['quantity_max'] - $quantityOrder + $quantityOrderUser ;
  531. $product['quantity_form'] = $quantityOrderUser * $coefficient_unit ;
  532. foreach($orderUser->productOrder as $productOrder) {
  533. if($productOrder->id_product == $product['id']) {
  534. $product['wording_unit'] = Product::strUnit($productOrder->unit,'wording_unit', true) ;
  535. $product['step'] = $productOrder->step ;
  536. }
  537. }
  538. }
  539. else {
  540. $product['quantity_form'] = 0 ;
  541. $product['wording_unit'] = Product::strUnit($product['unit'],'wording_unit', true) ;
  542. }
  543. $product['coefficient_unit'] = $coefficient_unit ;
  544. if($product['quantity_remaining'] < 0) $product['quantity_remaining'] = 0 ;
  545. $product['index'] = $indexProduct ++ ;
  546. }
  547. $json['products'] = $productsArray;
  548. }
  549. return $json ;
  550. }
  551. public function actionConfirm($idOrder)
  552. {
  553. $order = Order::searchOne(['id' => $idOrder]) ;
  554. if(!$order || $order->id_user != User::getCurrentId()) {
  555. throw new \yii\base\UserException('Commande introuvable.') ;
  556. }
  557. return $this->render('confirm',[
  558. 'order' => $order
  559. ]) ;
  560. }
  561. }