Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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 common\models;
  38. use common\helpers\Debug;
  39. use common\helpers\GlobalParam;
  40. use common\helpers\Price;
  41. use common\models\Producer;
  42. use Yii;
  43. use yii\helpers\Html;
  44. use common\components\ActiveRecordCommon;
  45. use yii\web\NotFoundHttpException;
  46. /**
  47. * This is the model class for table "order".
  48. *
  49. * @property integer $id
  50. * @property integer $id_user
  51. * @property string $date
  52. * @property string $date_update
  53. * @property integer $id_point_sale
  54. * @property integer $id_distribution
  55. * @property boolean $auto_payment
  56. * @property integer $id_subscription
  57. */
  58. class Order extends ActiveRecordCommon
  59. {
  60. var $amount = 0;
  61. var $amount_with_tax = 0;
  62. var $invoice_amount = 0;
  63. var $invoice_amount_with_tax = 0;
  64. var $paid_amount = 0;
  65. var $weight = 0;
  66. const ORIGIN_AUTO = 'auto';
  67. const ORIGIN_USER = 'user';
  68. const ORIGIN_ADMIN = 'admin';
  69. const PAYMENT_PAID = 'paid';
  70. const PAYMENT_UNPAID = 'unpaid';
  71. const PAYMENT_SURPLUS = 'surplus';
  72. const INVOICE_AMOUNT_TOTAL = 'invoice-total';
  73. const AMOUNT_TOTAL = 'total';
  74. const AMOUNT_PAID = 'paid';
  75. const AMOUNT_REMAINING = 'remaining';
  76. const AMOUNT_SURPLUS = 'surplus';
  77. const STATE_OPEN = 'open';
  78. const STATE_PREPARATION = 'preparation';
  79. const STATE_DELIVERED = 'delivered';
  80. /**
  81. * @inheritdoc
  82. */
  83. public static function tableName()
  84. {
  85. return 'order';
  86. }
  87. /**
  88. * @inheritdoc
  89. */
  90. public function rules()
  91. {
  92. return [
  93. [['id_user', 'date', 'status'], 'required', 'message' => ''],
  94. [
  95. [
  96. 'id_user',
  97. 'id_point_sale',
  98. 'id_distribution',
  99. 'id_subscription',
  100. 'id_invoice',
  101. 'id_quotation',
  102. 'id_delivery_note'
  103. ],
  104. 'integer'
  105. ],
  106. [['auto_payment', 'tiller_synchronization', 'delivery_home'], 'boolean'],
  107. [['status', 'reference', 'delivery_address', 'online_payment_url', 'tiller_external_id'], 'string'],
  108. [['date', 'date_update', 'comment', 'comment_point_sale', 'mean_payment', 'tiller_external_id'], 'safe']
  109. ];
  110. }
  111. /**
  112. * @inheritdoc
  113. */
  114. public function attributeLabels()
  115. {
  116. return [
  117. 'id' => 'ID',
  118. 'id_user' => 'Id User',
  119. 'date' => 'Date',
  120. 'date_update' => 'Date de modification',
  121. 'id_point_sale' => 'Point de vente',
  122. 'id_distribution' => 'Date de distribution',
  123. 'id_subscription' => 'Abonnement',
  124. 'status' => 'Statut',
  125. 'id_invoice' => 'Facture',
  126. 'id_quotation' => 'Devis',
  127. 'id_delivery_note' => 'Bon de livraison',
  128. 'reference' => 'Référence',
  129. 'delivery_home' => 'Livraison à domicile',
  130. 'delivery_address' => 'Adresse de livraison',
  131. 'online_payment_url' => 'URL de paiement',
  132. 'tiller_external_id' => 'Tiller : externalId',
  133. ];
  134. }
  135. /*
  136. * Relations
  137. */
  138. public function getUser()
  139. {
  140. return $this->hasOne(User::className(), ['id' => 'id_user']);
  141. }
  142. public function getProductOrder()
  143. {
  144. return $this->hasMany(ProductOrder::className(), ['id_order' => 'id'])
  145. ->orderBy(['product.order' => SORT_ASC])
  146. ->joinWith('product');
  147. }
  148. public function getDistribution()
  149. {
  150. return $this->hasOne(Distribution::className(), ['id' => 'id_distribution'])
  151. ->with('producer');
  152. }
  153. public function getPointSale()
  154. {
  155. return $this->hasOne(PointSale::className(), ['id' => 'id_point_sale'])
  156. ->with('userPointSale');
  157. }
  158. public function getCreditHistory()
  159. {
  160. return $this->hasMany(CreditHistory::className(), ['id_order' => 'id']);
  161. }
  162. public function getSubscription()
  163. {
  164. return $this->hasOne(Subscription::className(), ['id' => 'id_subscription'])
  165. ->with('productSubscription');
  166. }
  167. public function getInvoice()
  168. {
  169. return $this->hasOne(Invoice::className(), ['id' => 'id_invoice']);
  170. }
  171. public function getQuotation()
  172. {
  173. return $this->hasOne(Quotation::className(), ['id' => 'id_quotation']);
  174. }
  175. public function getDeliveryNote()
  176. {
  177. return $this->hasOne(DeliveryNote::className(), ['id' => 'id_delivery_note']);
  178. }
  179. /**
  180. * Retourne les options de base nécessaires à la fonction de recherche.
  181. *
  182. * @return array
  183. */
  184. public static function defaultOptionsSearch()
  185. {
  186. return [
  187. 'with' => [
  188. 'productOrder',
  189. 'productOrder.product',
  190. 'creditHistory',
  191. 'creditHistory.userAction',
  192. 'pointSale'
  193. ],
  194. 'join_with' => ['distribution', 'user', 'user.userProducer'],
  195. 'orderby' => 'order.date ASC',
  196. 'attribute_id_producer' => 'distribution.id_producer'
  197. ];
  198. }
  199. /**
  200. * Initialise le montant total, le montant déjà payé et le poids de la
  201. * commande.
  202. */
  203. public function init()
  204. {
  205. $this->initAmount();
  206. $this->initPaidAmount();
  207. return $this;
  208. }
  209. /**
  210. * Initialise le montant de la commande.
  211. *
  212. */
  213. public function initAmount()
  214. {
  215. $this->amount = 0;
  216. $this->amount_with_tax = 0;
  217. $this->invoice_amount = 0;
  218. $this->invoice_amount_with_tax = 0;
  219. $this->weight = 0;
  220. if (isset($this->productOrder)) {
  221. foreach ($this->productOrder as $productOrder) {
  222. $this->amount += $productOrder->price * $productOrder->quantity;
  223. $this->amount_with_tax += Price::getPriceWithTax(
  224. $productOrder->price,
  225. $productOrder->taxRate->value
  226. ) * $productOrder->quantity;
  227. $invoicePrice = $productOrder->invoice_price ? $productOrder->invoice_price : $productOrder->price;
  228. $this->invoice_amount += $invoicePrice * $productOrder->quantity;
  229. $this->invoice_amount_with_tax += Price::getPriceWithTax(
  230. $invoicePrice,
  231. $productOrder->taxRate->value
  232. ) * $productOrder->quantity;
  233. if ($productOrder->unit == 'piece') {
  234. if (isset($productOrder->product)) {
  235. $this->weight += ($productOrder->quantity * $productOrder->product->weight) / 1000;
  236. }
  237. } else {
  238. $this->weight += $productOrder->quantity;
  239. }
  240. }
  241. }
  242. }
  243. /**
  244. * Initialise le montant payé de la commande et le retourne.
  245. *
  246. * @return float
  247. */
  248. public function initPaidAmount()
  249. {
  250. if (isset($this->creditHistory)) {
  251. $history = $this->creditHistory;
  252. } else {
  253. $history = CreditHistory::find()
  254. ->where(['id_order' => $this->id])
  255. ->all();
  256. }
  257. $this->paid_amount = 0;
  258. if (count($history)) {
  259. foreach ($history as $ch) {
  260. if ($ch->type == CreditHistory::TYPE_PAYMENT) {
  261. $this->paid_amount += $ch->amount;
  262. } elseif ($ch->type == CreditHistory::TYPE_REFUND) {
  263. $this->paid_amount -= $ch->amount;
  264. }
  265. }
  266. }
  267. }
  268. public function delete()
  269. {
  270. // remboursement si l'utilisateur a payé pour cette commande
  271. $amountPaid = $this->getAmount(Order::AMOUNT_PAID);
  272. if ($amountPaid > 0.01) {
  273. $this->saveCreditHistory(
  274. CreditHistory::TYPE_REFUND,
  275. $amountPaid,
  276. GlobalParam::getCurrentProducerId(),
  277. $this->id_user,
  278. User::getCurrentId()
  279. );
  280. }
  281. // delete
  282. if (Producer::getConfig('option_behavior_cancel_order') == Producer::BEHAVIOR_DELETE_ORDER_DELETE ||
  283. (Producer::getConfig(
  284. 'option_behavior_cancel_order'
  285. ) == Producer::BEHAVIOR_DELETE_ORDER_STATUS && strlen($this->date_delete))) {
  286. ProductOrder::deleteAll(['id_order' => $this->id]);
  287. return parent::delete();
  288. } // status 'delete'
  289. elseif (Producer::getConfig('option_behavior_cancel_order') == Producer::BEHAVIOR_DELETE_ORDER_STATUS) {
  290. $this->date_delete = date('Y-m-d H:i:s');
  291. return $this->save();
  292. }
  293. }
  294. /**
  295. * Changement de statut d'une commande
  296. *
  297. * @param $newStatus
  298. */
  299. public function changeOrderStatus($newStatus, $origin)
  300. {
  301. $orderStatusArray = GlobalParam::get('orderStatus');
  302. switch ($newStatus) {
  303. case 'new-order' :
  304. $this->addOrderStatusHistory($newStatus, $origin);
  305. $this->status = $newStatus;
  306. $this->save();
  307. break;
  308. case 'waiting-paiement-on-delivery':
  309. if (in_array($newStatus, $orderStatusArray[$this->status]['nextStatusAllow'])) {
  310. $this->addOrderStatusHistory($newStatus, $origin);
  311. $this->status = $newStatus;
  312. $this->save();
  313. }
  314. break;
  315. case 'waiting-paiement-by-credit':
  316. if (in_array($newStatus, $orderStatusArray[$this->status]['nextStatusAllow'])) {
  317. $this->addOrderStatusHistory($newStatus, $origin);
  318. $this->status = $newStatus;
  319. $this->save();
  320. }
  321. break;
  322. case 'paid-by-credit':
  323. if (in_array($newStatus, $orderStatusArray[$this->status]['nextStatusAllow'])) {
  324. $this->addOrderStatusHistory($newStatus, $origin);
  325. $this->status = $newStatus;
  326. $this->save();
  327. }
  328. break;
  329. case 'waiting-delevery' :
  330. if (in_array($newStatus, $orderStatusArray[$this->status]['nextStatusAllow'])) {
  331. $this->addOrderStatusHistory($newStatus, $origin);
  332. $this->status = $newStatus;
  333. $this->save();
  334. }
  335. break;
  336. case 'delivered':
  337. if (in_array($newStatus, $orderStatusArray[$this->status]['nextStatusAllow'])) {
  338. $this->addOrderStatusHistory($newStatus, $origin);
  339. $this->status = $newStatus;
  340. $this->save();
  341. }
  342. break;
  343. case 'refunded':
  344. if (in_array($newStatus, $orderStatusArray[$this->status]['nextStatusAllow'])) {
  345. $this->addOrderStatusHistory($newStatus, $origin);
  346. $this->status = $newStatus;
  347. $this->save();
  348. }
  349. break;
  350. case 'cancel':
  351. if (in_array($newStatus, $orderStatusArray[$this->status]['nextStatusAllow'])) {
  352. $this->addOrderStatusHistory($newStatus, $origin);
  353. $this->status = $newStatus;
  354. $this->save();
  355. }
  356. break;
  357. default:
  358. throw new NotFoundHttpException('Statut de commande inconnu.');
  359. break;
  360. }
  361. }
  362. public function addOrderStatusHistory($newStatus, $origin)
  363. {
  364. $orderStatusHistory = new OrderStatusHistory();
  365. $orderStatusHistory->id_user = User::getCurrentId();
  366. $orderStatusHistory->id_order = $this->id;
  367. $orderStatusHistory->status = $newStatus;
  368. $orderStatusHistory->origin = $origin;
  369. $orderStatusHistory->date = date('Y-m-d H:i:s');
  370. $orderStatusHistory->save();
  371. }
  372. /**
  373. * Retourne le montant de la commande (total, payé, restant, ou en surplus).
  374. *
  375. * @param boolean $format
  376. * @return float
  377. */
  378. public function getAmount($type = self::AMOUNT_TOTAL, $format = false)
  379. {
  380. $amount = $this->amount;
  381. if ($type == self::INVOICE_AMOUNT_TOTAL) {
  382. $amount = $this->invoice_amount;
  383. }
  384. return $this->_getAmountGeneric($type, $amount, $format);
  385. }
  386. public function getAmountWithTax($type = self::AMOUNT_TOTAL, $format = false)
  387. {
  388. $amount = $this->amount_with_tax;
  389. if ($type == self::INVOICE_AMOUNT_TOTAL) {
  390. $amount = $this->invoice_amount_with_tax;
  391. }
  392. return $this->_getAmountGeneric($type, $amount, $format);
  393. }
  394. protected function _getAmountGeneric($type, $amountOrder, $format)
  395. {
  396. switch ($type) {
  397. case self::AMOUNT_TOTAL :
  398. case self::INVOICE_AMOUNT_TOTAL :
  399. $amount = $amountOrder;
  400. break;
  401. case self::AMOUNT_PAID :
  402. $this->initPaidAmount();
  403. $amount = $this->paid_amount;
  404. break;
  405. case self::AMOUNT_REMAINING :
  406. $amount = $this->getAmountWithTax(self::AMOUNT_TOTAL)
  407. - $this->getAmountWithTax(self::AMOUNT_PAID);
  408. break;
  409. case self::AMOUNT_SURPLUS :
  410. $amount = $this->getAmountWithTax(self::AMOUNT_PAID)
  411. - $this->getAmountWithTax(self::AMOUNT_TOTAL);
  412. break;
  413. /*default:
  414. throw new NotFoundHttpException('Type de montant inconnu : '.$type) ;*/
  415. }
  416. if ($format) {
  417. return Price::format($amount);
  418. } else {
  419. return $amount;
  420. }
  421. }
  422. /**
  423. * Retourne les informations relatives à la commande au format JSON.
  424. *
  425. * @return string
  426. */
  427. public function getDataJson()
  428. {
  429. $order = Order::searchOne(['order.id' => $this->id]);
  430. $jsonOrder = [];
  431. if ($order) {
  432. $jsonOrder = [
  433. 'products' => [],
  434. 'amount' => $order->amount,
  435. 'str_amount' => $order->getAmountWithTax(self::AMOUNT_TOTAL, true),
  436. 'paid_amount' => $order->getAmount(self::AMOUNT_PAID),
  437. 'comment' => $order->comment,
  438. ];
  439. foreach ($order->productOrder as $productOrder) {
  440. $jsonOrder['products'][$productOrder->id_product] = $productOrder->quantity;
  441. }
  442. }
  443. return json_encode($jsonOrder);
  444. }
  445. /**
  446. * Enregistre un modèle de type CreditHistory.
  447. *
  448. * @param string $type
  449. * @param float $montant
  450. * @param integer $idProducer
  451. * @param integer $idUser
  452. * @param integer $idUserAction
  453. */
  454. public function saveCreditHistory($type, $amount, $idProducer, $idUser, $idUserAction)
  455. {
  456. $creditHistory = new CreditHistory;
  457. $creditHistory->id_user = $this->id_user;
  458. $creditHistory->id_order = $this->id;
  459. $creditHistory->amount = $amount;
  460. $creditHistory->type = $type;
  461. $creditHistory->id_producer = $idProducer;
  462. $creditHistory->id_user_action = $idUserAction;
  463. $creditHistory->populateRelation('order', $this);
  464. $creditHistory->populateRelation('user', User::find()->where(['id' => $this->id_user])->one());
  465. $creditHistory->save();
  466. }
  467. /**
  468. * Ajuste le crédit pour que la commande soit payée.
  469. *
  470. * @return boolean
  471. */
  472. public function processCredit()
  473. {
  474. if ($this->id_user) {
  475. $paymentStatus = $this->getPaymentStatus();
  476. if ($paymentStatus == self::PAYMENT_PAID) {
  477. return true;
  478. } elseif ($paymentStatus == self::PAYMENT_SURPLUS) {
  479. $type = CreditHistory::TYPE_REFUND;
  480. $amount = $this->getAmount(self::AMOUNT_SURPLUS);
  481. } elseif ($paymentStatus == self::PAYMENT_UNPAID) {
  482. $type = CreditHistory::TYPE_PAYMENT;
  483. $amount = $this->getAmount(self::AMOUNT_REMAINING);
  484. }
  485. $this->saveCreditHistory(
  486. $type,
  487. $amount,
  488. GlobalParam::getCurrentProducerId(),
  489. $this->id_user,
  490. User::getCurrentId()
  491. );
  492. }
  493. }
  494. public function setTillerSynchronization()
  495. {
  496. $order = Order::searchOne(['id' => $this->id]);
  497. $paymentStatus = $order->getPaymentStatus();
  498. if ($paymentStatus == self::PAYMENT_PAID) {
  499. $order->tiller_synchronization = 1;
  500. } else {
  501. $order->tiller_synchronization = 0;
  502. }
  503. $order->save();
  504. return $order;
  505. }
  506. /**
  507. * Retourne le statut de paiement de la commande (payée, surplus, ou impayée).
  508. *
  509. * @return string
  510. */
  511. public function getPaymentStatus()
  512. {
  513. // payé
  514. if ($this->getAmountWithtax() - $this->getAmount(self::AMOUNT_PAID) < 0.01 &&
  515. $this->getAmountWithtax() - $this->getAmount(self::AMOUNT_PAID) > -0.01) {
  516. return self::PAYMENT_PAID;
  517. } // à rembourser
  518. elseif ($this->getAmountWithtax() - $this->getAmount(self::AMOUNT_PAID) <= -0.01) {
  519. return self::PAYMENT_SURPLUS;
  520. } // reste à payer
  521. elseif ($this->getAmountWithtax() - $this->getAmount(self::AMOUNT_PAID) >= 0.01) {
  522. return self::PAYMENT_UNPAID;
  523. }
  524. }
  525. /**
  526. * Retourne le résumé du panier au format HTML.
  527. *
  528. * @return string
  529. */
  530. public function getCartSummary($htmlFormat = true)
  531. {
  532. if (!isset($this->productOrder)) {
  533. $this->productOrder = productOrder::find()->where(['id_order' => $this->id])->all();
  534. }
  535. $html = '';
  536. $count = count($this->productOrder);
  537. $i = 0;
  538. foreach ($this->productOrder as $p) {
  539. if (isset($p->product)) {
  540. $html .= Html::encode($p->product->name) . ' (' . $p->quantity . '&nbsp;' . Product::strUnit(
  541. $p->unit,
  542. 'wording_short',
  543. true
  544. ) . ')';
  545. if (++$i != $count) {
  546. if ($htmlFormat) {
  547. $html .= '<br />';
  548. } else {
  549. $html .= "\n";
  550. }
  551. }
  552. }
  553. }
  554. return $html;
  555. }
  556. /**
  557. * Retourne le résumé du point de vente lié à la commande au format HTML.
  558. *
  559. * @return string
  560. */
  561. public function getPointSaleSummary()
  562. {
  563. $html = '';
  564. if (isset($this->pointSale)) {
  565. $html .= '<span class="name-point-sale">' .
  566. Html::encode($this->pointSale->name) .
  567. '</span>' .
  568. '<br /><span class="locality">'
  569. . Html::encode($this->pointSale->locality)
  570. . '</span>';
  571. if (strlen($this->comment_point_sale)) {
  572. $html .= '<div class="comment"><span>'
  573. . Html::encode($this->comment_point_sale)
  574. . '</span></div>';
  575. }
  576. } else {
  577. $html .= 'Point de vente supprimé';
  578. }
  579. return $html;
  580. }
  581. /**
  582. * Retourne le résumé du paiement (montant, statut).
  583. *
  584. * @return string
  585. */
  586. public function getAmountSummary()
  587. {
  588. $html = '';
  589. $creditActive = Producer::getConfig('credit');
  590. $html .= $this->getAmountWithTax(self::AMOUNT_TOTAL, true);
  591. if ($creditActive) {
  592. $html .= '<br />';
  593. if ($this->paid_amount) {
  594. if ($this->getPaymentStatus() == Order::PAYMENT_PAID) {
  595. $html .= '<span class="label label-success">Payée</span>';
  596. } elseif ($this->getPaymentStatus() == Order::PAYMENT_UNPAID) {
  597. $html .= '<span class="label label-danger">Non payée</span><br />
  598. Reste <strong>' . $this->getAmount(
  599. Order::AMOUNT_REMAINING,
  600. true
  601. ) . '</strong> à payer';
  602. } elseif ($this->getPaymentStatus() == Order::PAYMENT_SURPLUS) {
  603. $html .= '<span class="label label-success">Payée</span>';
  604. }
  605. } else {
  606. $html .= '<span class="label label-default">Non réglé</span>';
  607. }
  608. }
  609. return $html;
  610. }
  611. /**
  612. * Retourne une chaine de caractère décrivant l'utilisateur lié à la commande.
  613. *
  614. * @return string
  615. */
  616. public function getStrUser()
  617. {
  618. if (isset($this->user)) {
  619. if (isset($this->user->name_legal_person) && strlen($this->user->name_legal_person)) {
  620. return Html::encode($this->user->name_legal_person);
  621. } else {
  622. $strUser = $this->user->lastname;
  623. if($this->user->lastname && $this->user->name) {
  624. $strUser .= ' '.$this->user->name;
  625. }
  626. return Html::encode($strUser);
  627. }
  628. } elseif (strlen($this->username)) {
  629. return Html::encode($this->username);
  630. } else {
  631. return 'Client introuvable';
  632. }
  633. }
  634. /**
  635. * Retourne l'état de la commande (livrée, modifiable ou en préparation)
  636. *
  637. * @return string
  638. */
  639. public function getState()
  640. {
  641. $orderDate = strtotime($this->distribution->date);
  642. $today = strtotime(date('Y-m-d'));
  643. $todayHour = date('G');
  644. $dayDistribution = strtolower(date('l', strtotime($this->distribution->date)));
  645. $orderDelay = Producer::getConfig(
  646. 'order_delay',
  647. $this->distribution->id_producer
  648. );
  649. $orderDelaySpecific = Producer::getConfig(
  650. 'order_delay_' . $dayDistribution,
  651. $this->distribution->id_producer
  652. );
  653. if ($orderDelaySpecific) {
  654. $orderDelay = $orderDelaySpecific;
  655. }
  656. $orderDeadline = Producer::getConfig(
  657. 'order_deadline',
  658. $this->distribution->id_producer
  659. );
  660. $orderDeadlineSpecific = Producer::getConfig(
  661. 'order_deadline_' . $dayDistribution,
  662. $this->distribution->id_producer
  663. );
  664. if ($orderDeadlineSpecific) {
  665. $orderDeadline = $orderDeadlineSpecific;
  666. }
  667. $nbDays = (int)round((($orderDate - $today) / (24 * 60 * 60)));
  668. if ($nbDays <= 0) {
  669. return self::STATE_DELIVERED;
  670. } elseif ($nbDays >= $orderDelay &&
  671. ($nbDays != $orderDelay ||
  672. ($nbDays == $orderDelay && $todayHour < $orderDeadline))) {
  673. return self::STATE_OPEN;
  674. }
  675. return self::STATE_PREPARATION;
  676. }
  677. /**
  678. * Retourne l'origine de la commande (client, automatique ou admin) sous forme
  679. * texte ou HTML.
  680. *
  681. * @param boolean $with_label
  682. * @return string
  683. */
  684. public function getStrOrigin($withLabel = false)
  685. {
  686. $classLabel = '';
  687. $str = '';
  688. if ($this->origin == self::ORIGIN_USER) {
  689. $classLabel = 'success';
  690. $str = 'Client';
  691. } elseif ($this->origin == self::ORIGIN_AUTO) {
  692. $classLabel = 'default';
  693. $str = 'Auto';
  694. } elseif ($this->origin == self::ORIGIN_ADMIN) {
  695. $classLabel = 'warning';
  696. $str = 'Vous';
  697. }
  698. if ($withLabel) {
  699. return '<span class="label label-' . $classLabel . '">'
  700. . $str . '</span>';
  701. } else {
  702. return $str;
  703. }
  704. }
  705. /**
  706. * Retourne l'historique de la commande (ajoutée, modifiée, supprimée) au
  707. * format HTML.
  708. *
  709. * @return string
  710. */
  711. public function getStrHistory()
  712. {
  713. $arr = [
  714. 'class' => 'create',
  715. 'glyphicon' => 'plus',
  716. 'str' => 'Ajoutée',
  717. 'date' => $this->date
  718. ];
  719. if (!is_null($this->date_update)) {
  720. $arr = [
  721. 'class' => 'update',
  722. 'glyphicon' => 'pencil',
  723. 'str' => 'Modifiée',
  724. 'date' => $this->date_update
  725. ];
  726. }
  727. if (!is_null($this->date_delete)) {
  728. $arr = [
  729. 'class' => 'delete',
  730. 'glyphicon' => 'remove',
  731. 'str' => 'Annulée',
  732. 'date' => $this->date_delete
  733. ];
  734. }
  735. $html = '<div class="small"><span class="' . $arr['class'] . '">'
  736. . '<span class="glyphicon glyphicon-' . $arr['glyphicon'] . '"></span> '
  737. . $arr['str'] . '</span> le <strong>'
  738. . date('d/m/Y à G\hi', strtotime($arr['date'])) . '</strong></div>';
  739. return $html;
  740. }
  741. /**
  742. * Retourne une classe identifiant l'historique de la commande (ajoutée,
  743. * modifiée, supprimée).
  744. *
  745. * @return string
  746. */
  747. public function getClassHistory()
  748. {
  749. if (!is_null($this->date_delete)) {
  750. return 'commande-delete';
  751. }
  752. if (!is_null($this->date_update)) {
  753. return 'commande-update';
  754. }
  755. return 'commande-create';
  756. }
  757. /**
  758. * Retourne la quantité d'un produit donné de plusieurs commandes.
  759. *
  760. * @param integer $idProduct
  761. * @param array $orders
  762. * @param boolean $ignoreCancel
  763. *
  764. * @return integer
  765. */
  766. public static function getProductQuantity($idProduct, $orders, $ignoreCancel = false, $unit = null)
  767. {
  768. $quantity = 0;
  769. if (isset($orders) && is_array($orders) && count($orders)) {
  770. foreach ($orders as $c) {
  771. if (is_null($c->date_delete) || $ignoreCancel) {
  772. foreach ($c->productOrder as $po) {
  773. if ($po->id_product == $idProduct &&
  774. ((is_null($unit) && $po->product->unit == $po->unit) || (!is_null($unit) && strlen(
  775. $unit
  776. ) && $po->unit == $unit))) {
  777. $quantity += $po->quantity;
  778. }
  779. }
  780. }
  781. }
  782. }
  783. return $quantity;
  784. }
  785. public static function getProductQuantityPieces($idProduct, $orders)
  786. {
  787. $quantity = 0;
  788. if (isset($orders) && is_array($orders) && count($orders)) {
  789. foreach ($orders as $c) {
  790. if (is_null($c->date_delete)) {
  791. foreach ($c->productOrder as $po) {
  792. if ($po->id_product == $idProduct) {
  793. if ($po->unit == 'piece') {
  794. $quantity += $po->quantity;
  795. } else {
  796. if (isset($po->product) && $po->product->weight > 0) {
  797. $quantity += ($po->quantity * Product::$unitsArray[$po->unit]['coefficient']) / $po->product->weight;
  798. }
  799. }
  800. }
  801. }
  802. }
  803. }
  804. }
  805. return $quantity;
  806. }
  807. /**
  808. * Recherche et initialise des commandes.
  809. *
  810. * @param array $params
  811. * @param array $conditions
  812. * @param string $orderby
  813. * @param integer $limit
  814. * @return array
  815. */
  816. public static function searchBy($params = [], $options = [])
  817. {
  818. $orders = parent::searchBy($params, $options);
  819. /*
  820. * Initialisation des commandes
  821. */
  822. if (is_array($orders)) {
  823. if (count($orders)) {
  824. foreach ($orders as $order) {
  825. if (is_a($order, 'common\models\Order')) {
  826. $order->init();
  827. }
  828. }
  829. return $orders;
  830. }
  831. } else {
  832. $order = $orders;
  833. if (is_a($order, 'common\models\Order')) {
  834. return $order->init();
  835. } // count
  836. else {
  837. return $order;
  838. }
  839. }
  840. return false;
  841. }
  842. /**
  843. * Retourne le nombre de produits commandés
  844. *
  845. * @return integer
  846. */
  847. public function countProducts()
  848. {
  849. $count = 0;
  850. if ($this->productOrder && is_array($this->productOrder)) {
  851. return count($this->productOrder);
  852. }
  853. return 0;
  854. }
  855. /**
  856. * Retourne un bloc html présentant une date.
  857. *
  858. * @return string
  859. */
  860. public function getBlockDate()
  861. {
  862. return '<div class="block-date">
  863. <div class="day">' . strftime('%A', strtotime($this->distribution->date)) . '</div>
  864. <div class="num">' . date('d', strtotime($this->distribution->date)) . '</div>
  865. <div class="month">' . strftime('%B', strtotime($this->distribution->date)) . '</div>
  866. </div>';
  867. }
  868. public function getUsername()
  869. {
  870. $username = '';
  871. if ($this->user) {
  872. $username = $this->user->getUsername();
  873. }
  874. if (strlen($this->username)) {
  875. $username = $this->username;
  876. }
  877. return $username;
  878. }
  879. public function initInvoicePrices($params = [])
  880. {
  881. foreach ($this->productOrder as $productOrder) {
  882. if ($productOrder->product) {
  883. $productOrder->invoice_price = $productOrder->product->getPrice([
  884. 'user' => isset($params['user']) ? $params['user'] : null,
  885. 'user_producer' => isset($params['user_producer']) ? $params['user_producer'] : null,
  886. 'point_sale' => isset($params['point_sale']) ? $params['point_sale'] : null
  887. ]);
  888. $productOrder->save();
  889. }
  890. }
  891. }
  892. public function initReference()
  893. {
  894. $idProducer = GlobalParam::getCurrentProducerId();
  895. $producer = Producer::findOne($idProducer);
  896. if (!$this->reference && $producer->option_order_reference_type == Producer::ORDER_REFERENCE_TYPE_YEARLY) {
  897. $lastOrder = Order::find()->innerJoinWith('distribution', true)
  898. ->where(['>=', 'distribution.date', date('Y') . '-01-01'])
  899. ->andWhere([
  900. 'distribution.id_producer' => $producer->id
  901. ])
  902. ->andWhere(['not', ['order.reference' => null]])
  903. ->orderBy('order.reference DESC')
  904. ->one();
  905. if ($lastOrder && $lastOrder->reference && strlen($lastOrder->reference) > 0) {
  906. $pattern = '#A([0-9]+)C([0-9]+)#';
  907. preg_match($pattern, $lastOrder->reference, $matches, PREG_OFFSET_CAPTURE);
  908. $sizeNumReference = strlen($matches[2][0]);
  909. $numReference = ((int)$matches[2][0]) + 1;
  910. $numReference = str_pad($numReference, $sizeNumReference, '0', STR_PAD_LEFT);
  911. $this->reference = 'A' . $matches[1][0] . 'C' . $numReference;
  912. } else {
  913. $this->reference = 'A' . date('y') . 'C0001';
  914. }
  915. $this->save();
  916. }
  917. }
  918. public function getCommentReport()
  919. {
  920. $comment = '';
  921. $hasComment = false;
  922. if ($this->comment && strlen($this->comment) > 0) {
  923. $hasComment = true;
  924. $comment .= $this->comment;
  925. }
  926. if ($this->delivery_home && $this->delivery_address && strlen($this->delivery_address) > 0) {
  927. if ($hasComment) {
  928. $comment .= '<br /><br />';
  929. }
  930. $comment .= '<strong>Livraison à domicile :</strong><br />';
  931. $comment .= nl2br($this->delivery_address);
  932. }
  933. return $comment;
  934. }
  935. }