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

387 lines
15KB

  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\GlobalParam;
  39. class Document extends ActiveRecordCommon
  40. {
  41. const STATUS_DRAFT = 'draft';
  42. const STATUS_VALID = 'valid';
  43. /**
  44. * @inheritdoc
  45. */
  46. public function rules()
  47. {
  48. return [
  49. [['name', 'id_user'], 'required'],
  50. [['date'], 'safe'],
  51. [['comment', 'address'], 'string'],
  52. [['id_user', 'id_producer'], 'integer'],
  53. [['name', 'reference', 'status'], 'string', 'max' => 255],
  54. [['deliveryNotes'], 'safe']
  55. ];
  56. }
  57. /**
  58. * @inheritdoc
  59. */
  60. public function attributeLabels()
  61. {
  62. return [
  63. 'id' => 'ID',
  64. 'name' => 'Nom',
  65. 'reference' => 'Référence',
  66. 'date' => 'Date',
  67. 'comment' => 'Commentaire',
  68. 'id_user' => 'Utilisateur',
  69. 'address' => 'Adresse',
  70. 'id_producer' => 'Producteur',
  71. 'status' => 'Statut',
  72. ];
  73. }
  74. /*
  75. * Relations
  76. */
  77. public function getUser()
  78. {
  79. return $this->hasOne(User::className(), ['id' => 'id_user']);
  80. }
  81. public function getProducer()
  82. {
  83. return $this->hasOne(Producer::className(), ['id' => 'id_producer']);
  84. }
  85. public function relationOrders($fieldIdDocument)
  86. {
  87. $defaultOptionsSearch = Order::defaultOptionsSearch();
  88. return $this->hasMany(Order::className(), [$fieldIdDocument => 'id'])
  89. ->with($defaultOptionsSearch['with'])
  90. ->joinWith($defaultOptionsSearch['join_with'])
  91. ->orderBy('user.lastname ASC, user.name ASC, comment_point_sale ASC');
  92. }
  93. /*
  94. * Méthodes
  95. */
  96. public function getAmount($type = Order::AMOUNT_TOTAL, $format = false)
  97. {
  98. return $this->_getAmountGeneric($type, false, $format);
  99. }
  100. public function getAmountWithTax($type = Order::AMOUNT_TOTAL, $format = false)
  101. {
  102. return $this->_getAmountGeneric($type, true, $format);
  103. }
  104. protected function _getAmountGeneric($type = Order::AMOUNT_TOTAL, $withTax = true, $format = false)
  105. {
  106. $amount = 0;
  107. $ordersArray = $this->orders;
  108. foreach ($ordersArray as $order) {
  109. $order->init();
  110. if ($withTax) {
  111. $amount += $order->getAmountWithTax($type);
  112. }
  113. else {
  114. $amount += $order->getAmount($type);
  115. }
  116. }
  117. if ($format) {
  118. return Price::format($amount);
  119. } else {
  120. return $amount;
  121. }
  122. }
  123. public function getPointSale()
  124. {
  125. if (isset($this->orders) && isset($this->orders[0])) {
  126. return $this->orders[0]->pointSale;
  127. } else {
  128. return '';
  129. }
  130. }
  131. public function getDistribution()
  132. {
  133. if (isset($this->orders) && isset($this->orders[0])) {
  134. return $this->orders[0]->distribution;
  135. } else {
  136. return '';
  137. }
  138. }
  139. public function getClass()
  140. {
  141. return str_replace('common\models\\', '', get_class($this));
  142. }
  143. public function getType()
  144. {
  145. $class = $this->getClass();
  146. if ($class == 'Invoice') {
  147. $documentType = 'Facture';
  148. } elseif ($class == 'DeliveryNote') {
  149. $documentType = 'Bon de livraison';
  150. } elseif ($class == 'Quotation') {
  151. $documentType = 'Devis';
  152. }
  153. if (isset($documentType)) {
  154. return $documentType;
  155. }
  156. return '';
  157. }
  158. public function isValidClass($typeDocument)
  159. {
  160. return in_array($typeDocument, ['Invoice', 'DeliveryNote', 'Quotation']);
  161. }
  162. public function generateReference()
  163. {
  164. $class = $this->getClass();
  165. $classLower = strtolower($class);
  166. if ($classLower == 'deliverynote') {
  167. $classLower = 'delivery_note';
  168. }
  169. $prefix = Producer::getConfig('document_' . $classLower . '_prefix');
  170. $oneDocumentExist = $class::searchOne(['status' => Document::STATUS_VALID] , ['orderby' => 'reference DESC']);
  171. if ($oneDocumentExist) {
  172. $reference = $oneDocumentExist->reference;
  173. $pattern = '#([A-Z]+)?([0-9]+)#';
  174. preg_match($pattern, $reference, $matches, PREG_OFFSET_CAPTURE);
  175. $sizeNumReference = strlen($matches[2][0]);
  176. $numReference = ((int)$matches[2][0]) + 1;
  177. $numReference = str_pad($numReference, $sizeNumReference, '0', STR_PAD_LEFT);
  178. return $prefix . $numReference;
  179. } else {
  180. $firstReference = Producer::getConfig('document_' . $classLower . '_first_reference');
  181. if (strlen($firstReference) > 0) {
  182. return $firstReference;
  183. } else {
  184. return $prefix . '00001';
  185. }
  186. }
  187. }
  188. public function generatePdf($destination)
  189. {
  190. $producer = GlobalParam::getCurrentProducer();
  191. $content = Yii::$app->controller->renderPartial('/document/download', [
  192. 'producer' => $producer,
  193. 'document' => $this
  194. ]);
  195. $contentFooter = '<div id="footer">';
  196. $contentFooter .= '<div class="infos-bottom">' . Html::encode($producer->document_infos_bottom) . '</div>';
  197. if ($this->isStatusValid() || $this->isStatusDraft()) {
  198. $contentFooter .= '<div class="reference-document">';
  199. if ($this->isStatusValid()) {
  200. $contentFooter .= $this->getType() . ' N°' . $this->reference;
  201. }
  202. if ($this->isStatusDraft()) {
  203. $contentFooter .= $this->getType() . ' non validé';
  204. if($this->getType() == 'Facture') {
  205. $contentFooter .= 'e' ;
  206. }
  207. }
  208. $contentFooter .= '</div>';
  209. }
  210. $contentFooter .= '</div>';
  211. $marginBottom = 10 ;
  212. if(strlen(Producer::getConfig('document_infos_bottom')) > 0) {
  213. $marginBottom = 40 ;
  214. }
  215. $pdf = new Pdf([
  216. 'mode' => Pdf::MODE_UTF8,
  217. 'format' => Pdf::FORMAT_A4,
  218. 'orientation' => Pdf::ORIENT_PORTRAIT,
  219. 'destination' => $destination,
  220. 'content' => $content,
  221. 'filename' => $this->getFilename(),
  222. 'cssFile' => Yii::getAlias('@webroot/css/document/download.css'),
  223. 'marginBottom' => $marginBottom,
  224. 'methods' => [
  225. 'SetHTMLFooter' => $contentFooter
  226. ]
  227. ]);
  228. return $pdf->render();
  229. }
  230. public function send()
  231. {
  232. if(isset($this->user) && strlen($this->user->email) > 0) {
  233. $producer = GlobalParam::getCurrentProducer();
  234. $subjectEmail = $this->getType() ;
  235. if($this->isStatusValid()) {
  236. $subjectEmail .= ' N°'.$this->reference ;
  237. }
  238. $email = Yii::$app->mailer->compose(
  239. [
  240. 'html' => 'sendDocument-html',
  241. 'text' => 'sendDocument-text'
  242. ], [
  243. 'document' => $this,
  244. ])
  245. ->setTo($this->user->email)
  246. ->setFrom([$producer->getEmailOpendistrib() => $producer->name])
  247. ->setSubject('['.$producer->name.'] '.$subjectEmail) ;
  248. $this->generatePdf(Pdf::DEST_FILE) ;
  249. $email->attach($this->getFilename());
  250. return $email->send() ;
  251. }
  252. return false ;
  253. }
  254. public function changeStatus($status)
  255. {
  256. if ($status == Document::STATUS_VALID) {
  257. $this->status = $status;
  258. $this->reference = $this->generateReference();
  259. }
  260. }
  261. public function getStatusWording()
  262. {
  263. return ($this->status == self::STATUS_DRAFT) ? 'Brouillon' : 'Validé';
  264. }
  265. public function getStatusCssClass()
  266. {
  267. return ($this->status == self::STATUS_DRAFT) ? 'default' : 'success';
  268. }
  269. public function getHtmlLabel()
  270. {
  271. $label = $this->getStatusWording();
  272. $classLabel = $this->getStatusCssClass();
  273. return '<span class="label label-' . $classLabel . '">' . $label . '</span>';
  274. }
  275. public function isStatus($status)
  276. {
  277. return $this->status == $status;
  278. }
  279. public function isStatusDraft()
  280. {
  281. return $this->isStatus(self::STATUS_DRAFT);
  282. }
  283. public function isStatusValid()
  284. {
  285. return $this->isStatus(self::STATUS_VALID);
  286. }
  287. public function getProductsOrders()
  288. {
  289. $productsOrdersArray = [];
  290. $ordersArray = $this->orders ;
  291. if ($ordersArray && count($ordersArray)) {
  292. foreach ($ordersArray as $order) {
  293. foreach ($order->productOrder as $productOrder) {
  294. if (!isset($productsOrdersArray[$productOrder->id_product])) {
  295. $newProductOrder = clone $productOrder ;
  296. $productsOrdersArray[$productOrder->id_product] = [$newProductOrder];
  297. } else {
  298. $productOrderMatch = false;
  299. foreach ($productsOrdersArray[$productOrder->id_product] as &$theProductOrder) {
  300. if ($theProductOrder->unit == $productOrder->unit && $theProductOrder->price == $productOrder->price) {
  301. $theProductOrder->quantity += $productOrder->quantity;
  302. $productOrderMatch = true;
  303. }
  304. }
  305. if (!$productOrderMatch) {
  306. $productsOrdersArray[$productOrder->id_product][] = $productOrder;
  307. }
  308. }
  309. }
  310. }
  311. }
  312. return $productsOrdersArray;
  313. }
  314. public function isDisplayOrders()
  315. {
  316. $displayOrders = ($this->getClass() == 'Invoice') ?
  317. Producer::getConfig('document_display_orders_invoice') :
  318. Producer::getConfig('document_display_orders_delivery_note') ;
  319. return $displayOrders ;
  320. }
  321. public function getFilename()
  322. {
  323. return Yii::getAlias('@app/web/pdf/'.$this->getType().'-' . $this->reference. '.pdf') ;
  324. }
  325. public function isInvoicePrice()
  326. {
  327. return $this->getClass() == 'Invoice' || $this->getClass() == 'DeliveryNote' ;
  328. }
  329. }