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.

531 line
17KB

  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. use kartik\mpdf\Pdf;
  40. use Symfony\Component\Finder\Exception\DirectoryNotFoundException;
  41. use yii\base\ErrorException;
  42. class Document extends ActiveRecordCommon
  43. {
  44. const STATUS_DRAFT = 'draft';
  45. const STATUS_VALID = 'valid';
  46. const TAX_CALCULATION_METHOD_SUM_OF_ROUNDINGS = 'sum-roundings';
  47. const TAX_CALCULATION_METHOD_ROUNDING_OF_THE_SUM = 'rounding-sum';
  48. const TAX_CALCULATION_METHOD_DEFAULT = self::TAX_CALCULATION_METHOD_ROUNDING_OF_THE_SUM;
  49. public static $taxCalculationMethodArray = [
  50. self::TAX_CALCULATION_METHOD_ROUNDING_OF_THE_SUM => 'Arrondi de la somme des lignes',
  51. self::TAX_CALCULATION_METHOD_SUM_OF_ROUNDINGS => 'Somme des arrondis de chaque ligne'
  52. ];
  53. /**
  54. * @inheritdoc
  55. */
  56. public function rules()
  57. {
  58. return [
  59. [['name', 'id_user'], 'required'],
  60. [['date'], 'safe'],
  61. [['comment', 'address', 'tax_calculation_method'], 'string'],
  62. [['id_user', 'id_producer'], 'integer'],
  63. ['is_sent', 'boolean'],
  64. [['name', 'reference', 'status'], 'string', 'max' => 255],
  65. [['deliveryNotes'], 'safe']
  66. ];
  67. }
  68. /**
  69. * @inheritdoc
  70. */
  71. public function attributeLabels()
  72. {
  73. return [
  74. 'id' => 'ID',
  75. 'name' => 'Nom',
  76. 'reference' => 'Référence',
  77. 'date' => 'Date',
  78. 'comment' => 'Commentaire',
  79. 'id_user' => 'Utilisateur',
  80. 'address' => 'Adresse',
  81. 'id_producer' => 'Producteur',
  82. 'status' => 'Statut',
  83. 'tax_calculation_method' => 'Méthode de calcul de la TVA',
  84. 'is_sent' => 'Envoyé'
  85. ];
  86. }
  87. /*
  88. * Relations
  89. */
  90. public function getUser()
  91. {
  92. return $this->hasOne(User::className(), ['id' => 'id_user']);
  93. }
  94. public function getProducer()
  95. {
  96. return $this->hasOne(Producer::className(), ['id' => 'id_producer']);
  97. }
  98. public function relationOrders($fieldIdDocument)
  99. {
  100. $defaultOptionsSearch = Order::defaultOptionsSearch();
  101. return $this->hasMany(Order::className(), [$fieldIdDocument => 'id'])
  102. ->with($defaultOptionsSearch['with'])
  103. ->joinWith($defaultOptionsSearch['join_with'])
  104. ->orderBy('distribution.date ASC');
  105. }
  106. /*
  107. * Méthodes
  108. */
  109. public function getAmount($type = Order::AMOUNT_TOTAL, $format = false)
  110. {
  111. return $this->_getAmountGeneric($type, false, $format);
  112. }
  113. public function getAmountWithTax($type = Order::AMOUNT_TOTAL, $format = false)
  114. {
  115. return $this->_getAmountGeneric($type, true, $format);
  116. }
  117. protected function _getAmountGeneric($type = Order::AMOUNT_TOTAL, $withTax = true, $format = false)
  118. {
  119. $amount = 0;
  120. $totalVat = 0;
  121. $ordersArray = $this->orders;
  122. // Méthode de calcul via les commandes liées
  123. /*foreach ($ordersArray as $order) {
  124. $order->init($this->tax_calculation_method);
  125. $amount += $order->getAmount($type);
  126. $totalVat += $order->getTotalVat($type);
  127. }*/
  128. // Méthode de calcul via getProductOrders()
  129. foreach($this->getProductsOrders() as $productOrderArray) {
  130. foreach($productOrderArray as $productOrder) {
  131. $priceLine = $productOrder->getPriceByTypeTotal($type) * $productOrder->quantity;
  132. $amount += $priceLine;
  133. $totalVat += Price::getVat($priceLine, $productOrder->taxRate->value, $this->tax_calculation_method);
  134. }
  135. }
  136. if ($this->isTaxCalculationMethodRoundingOfTheSum()) {
  137. $totalVat = Price::round($totalVat);
  138. }
  139. if ($withTax) {
  140. $amount += $totalVat;
  141. }
  142. if ($format) {
  143. return Price::format($amount);
  144. } else {
  145. return $amount;
  146. }
  147. }
  148. public function getTotalVatArray($typeTotal)
  149. {
  150. $totalVatArray = [];
  151. // Méthode de calcul via les commandes liées
  152. /*$ordersArray = $this->orders;
  153. foreach ($ordersArray as $order) {
  154. $order->init($this->tax_calculation_method);
  155. $fieldNameVat = $order->getFieldNameAmount($typeTotal, 'vat');
  156. foreach ($order->$fieldNameVat as $idTaxRate => $vat) {
  157. if (!isset($totalVatArray[$idTaxRate])) {
  158. $totalVatArray[$idTaxRate] = 0;
  159. }
  160. $totalVatArray[$idTaxRate] += $vat;
  161. }
  162. }*/
  163. // Méthode de calcul via getProductOrders()
  164. foreach($this->getProductsOrders() as $productOrderArray) {
  165. foreach ($productOrderArray as $productOrder) {
  166. $idTaxRate = $productOrder->taxRate->id;
  167. if (!isset($totalVatArray[$idTaxRate])) {
  168. $totalVatArray[$idTaxRate] = 0;
  169. }
  170. $totalVatArray[$idTaxRate] += Price::getVat(
  171. $productOrder->getPriceByTypeTotal($typeTotal) * $productOrder->quantity,
  172. $productOrder->taxRate->value,
  173. $this->tax_calculation_method
  174. );
  175. }
  176. }
  177. return $totalVatArray;
  178. }
  179. public function getPointSale()
  180. {
  181. if (isset($this->orders) && isset($this->orders[0])) {
  182. return $this->orders[0]->pointSale;
  183. } else {
  184. return '';
  185. }
  186. }
  187. public function getDistribution()
  188. {
  189. if (isset($this->orders) && isset($this->orders[0])) {
  190. return $this->orders[0]->distribution;
  191. } else {
  192. return '';
  193. }
  194. }
  195. public function getClass()
  196. {
  197. return str_replace('common\models\\', '', get_class($this));
  198. }
  199. public function getType()
  200. {
  201. $class = $this->getClass();
  202. if ($class == 'Invoice') {
  203. $documentType = 'Facture';
  204. } elseif ($class == 'DeliveryNote') {
  205. $documentType = 'Bon de livraison';
  206. } elseif ($class == 'Quotation') {
  207. $documentType = 'Devis';
  208. }
  209. if (isset($documentType)) {
  210. return $documentType;
  211. }
  212. return '';
  213. }
  214. public function isValidClass($typeDocument)
  215. {
  216. return in_array($typeDocument, ['Invoice', 'DeliveryNote', 'Quotation']);
  217. }
  218. public function generateReference()
  219. {
  220. $class = $this->getClass();
  221. $classLower = strtolower($class);
  222. if ($classLower == 'deliverynote') {
  223. $classLower = 'delivery_note';
  224. }
  225. $prefix = Producer::getConfig('document_' . $classLower . '_prefix');
  226. $oneDocumentExist = $class::searchOne(['status' => Document::STATUS_VALID], ['orderby' => 'reference DESC']);
  227. if ($oneDocumentExist) {
  228. $reference = $oneDocumentExist->reference;
  229. $pattern = '#([A-Z]+)?([0-9]+)#';
  230. preg_match($pattern, $reference, $matches, PREG_OFFSET_CAPTURE);
  231. $sizeNumReference = strlen($matches[2][0]);
  232. $numReference = ((int)$matches[2][0]) + 1;
  233. $numReference = str_pad($numReference, $sizeNumReference, '0', STR_PAD_LEFT);
  234. return $prefix . $numReference;
  235. } else {
  236. $firstReference = Producer::getConfig('document_' . $classLower . '_first_reference');
  237. if (strlen($firstReference) > 0) {
  238. return $firstReference;
  239. } else {
  240. return $prefix . '00001';
  241. }
  242. }
  243. }
  244. public function downloadPdf()
  245. {
  246. $filenameComplete = $this->getFilenameComplete();
  247. /*if (!file_exists($filenameComplete)) {
  248. $this->generatePdf(Pdf::DEST_FILE);
  249. }*/
  250. $this->generatePdf(Pdf::DEST_FILE);
  251. if (file_exists($filenameComplete)) {
  252. return Yii::$app->response->sendFile($filenameComplete, $this->getFilename(), ['inline' => true]);
  253. } else {
  254. throw new ErrorException('File ' . $filenameComplete . ' not found');
  255. }
  256. }
  257. public function generatePdf($destination)
  258. {
  259. $producer = GlobalParam::getCurrentProducer();
  260. $content = Yii::$app->controller->renderPartial('/document/download', [
  261. 'producer' => $producer,
  262. 'document' => $this
  263. ]);
  264. $contentFooter = '<div id="footer">';
  265. $contentFooter .= '<div class="infos-bottom">' . Html::encode($producer->document_infos_bottom) . '</div>';
  266. if ($this->isStatusValid() || $this->isStatusDraft()) {
  267. $contentFooter .= '<div class="reference-document">';
  268. if ($this->isStatusValid()) {
  269. $contentFooter .= $this->getType() . ' N°' . $this->reference;
  270. }
  271. if ($this->isStatusDraft()) {
  272. $contentFooter .= $this->getType() . ' non validé';
  273. if ($this->getType() == 'Facture') {
  274. $contentFooter .= 'e';
  275. }
  276. }
  277. $contentFooter .= '</div>';
  278. }
  279. $contentFooter .= '</div>';
  280. $marginBottom = 10;
  281. if (strlen(Producer::getConfig('document_infos_bottom')) > 0) {
  282. $marginBottom = 40;
  283. }
  284. $this->initDirectoryPdf();
  285. $pdf = new Pdf([
  286. 'mode' => Pdf::MODE_UTF8,
  287. 'format' => Pdf::FORMAT_A4,
  288. 'orientation' => Pdf::ORIENT_PORTRAIT,
  289. 'destination' => $destination,
  290. 'content' => $content,
  291. 'filename' => $this->getFilenameComplete(),
  292. 'cssFile' => Yii::getAlias('@webroot/css/document/download.css'),
  293. 'marginBottom' => $marginBottom,
  294. 'methods' => [
  295. 'SetHTMLFooter' => $contentFooter
  296. ]
  297. ]);
  298. return $pdf->render();
  299. }
  300. public function send()
  301. {
  302. if (isset($this->user) && strlen($this->user->email) > 0) {
  303. $producer = GlobalParam::getCurrentProducer();
  304. $subjectEmail = $this->getType();
  305. if ($this->isStatusValid()) {
  306. $subjectEmail .= ' N°' . $this->reference;
  307. }
  308. $email = Yii::$app->mailer->compose(
  309. [
  310. 'html' => 'sendDocument-html',
  311. 'text' => 'sendDocument-text'
  312. ], [
  313. 'document' => $this,
  314. ])
  315. ->setTo($this->user->email)
  316. ->setFrom([$producer->getEmailOpendistrib() => $producer->name])
  317. ->setSubject('[' . $producer->name . '] ' . $subjectEmail);
  318. $this->generatePdf(Pdf::DEST_FILE);
  319. $email->attach($this->getFilenameComplete());
  320. return $email->send();
  321. }
  322. return false;
  323. }
  324. public function changeStatus($status)
  325. {
  326. if ($status == Document::STATUS_VALID) {
  327. $this->status = $status;
  328. $this->reference = $this->generateReference();
  329. }
  330. }
  331. public function getStatusWording()
  332. {
  333. return ($this->status == self::STATUS_DRAFT) ? 'Brouillon' : 'Validé';
  334. }
  335. public function getStatusCssClass()
  336. {
  337. return ($this->status == self::STATUS_DRAFT) ? 'default' : 'success';
  338. }
  339. public function getHtmlLabel()
  340. {
  341. $label = $this->getStatusWording();
  342. $classLabel = $this->getStatusCssClass();
  343. return '<span class="label label-' . $classLabel . '">' . $label . '</span>';
  344. }
  345. public function isStatus($status)
  346. {
  347. return $this->status == $status;
  348. }
  349. public function isStatusDraft()
  350. {
  351. return $this->isStatus(self::STATUS_DRAFT);
  352. }
  353. public function isStatusValid()
  354. {
  355. return $this->isStatus(self::STATUS_VALID);
  356. }
  357. public function getProductsOrders()
  358. {
  359. $productsOrdersArray = [];
  360. $ordersArray = $this->orders;
  361. if ($ordersArray && count($ordersArray)) {
  362. foreach ($ordersArray as $order) {
  363. foreach ($order->productOrder as $productOrder) {
  364. if ($productOrder->product) {
  365. $indexProductOrder = $productOrder->product->order;
  366. $newProductOrder = clone $productOrder;
  367. if (!isset($productsOrdersArray[$indexProductOrder])) {
  368. $productsOrdersArray[$indexProductOrder] = [$newProductOrder];
  369. } else {
  370. $productOrderMatch = false;
  371. foreach ($productsOrdersArray[$indexProductOrder] as &$theProductOrder) {
  372. if ($theProductOrder->unit == $productOrder->unit
  373. && ((!$this->isInvoicePrice() && $theProductOrder->price == $productOrder->price)
  374. || ($this->isInvoicePrice() && $theProductOrder->invoice_price == $productOrder->invoice_price)
  375. )) {
  376. $theProductOrder->quantity += $productOrder->quantity;
  377. $productOrderMatch = true;
  378. }
  379. }
  380. if (!$productOrderMatch) {
  381. $productsOrdersArray[$indexProductOrder][] = $newProductOrder;
  382. }
  383. }
  384. }
  385. }
  386. }
  387. }
  388. // tri des orderProduct par product.order
  389. ksort($productsOrdersArray);
  390. return $productsOrdersArray;
  391. }
  392. public function isDisplayOrders()
  393. {
  394. $displayOrders = ($this->getClass() == 'Invoice') ?
  395. Producer::getConfig('document_display_orders_invoice') :
  396. Producer::getConfig('document_display_orders_delivery_note');
  397. return $displayOrders;
  398. }
  399. public function getAliasDirectoryBase()
  400. {
  401. return '@app/web/pdf/' . $this->id_producer . '/';
  402. }
  403. public function initDirectoryPdf()
  404. {
  405. $aliasDirectoryBase = $this->getAliasDirectoryBase();
  406. $directoryPdf = Yii::getAlias($aliasDirectoryBase);
  407. if (!file_exists($directoryPdf)) {
  408. mkdir($directoryPdf, 0755);
  409. }
  410. }
  411. public function getFilename()
  412. {
  413. $filename = $this->getType() . '-';
  414. if($this->isStatusValid()) {
  415. $filename .= $this->reference;
  416. }
  417. elseif($this->isStatusDraft()) {
  418. $filename .= 'Brouillon-'.$this->id;
  419. }
  420. $filename .= '.pdf';
  421. return $filename;
  422. }
  423. public function getFilenameComplete()
  424. {
  425. return Yii::getAlias($this->getAliasDirectoryBase() . $this->getFilename());
  426. }
  427. public function isInvoicePrice()
  428. {
  429. return $this->getClass() == 'Invoice' || $this->getClass() == 'DeliveryNote';
  430. }
  431. public function isTaxCalculationMethodSumOfRoundings()
  432. {
  433. return $this->tax_calculation_method == self::TAX_CALCULATION_METHOD_SUM_OF_ROUNDINGS;
  434. }
  435. public function isTaxCalculationMethodRoundingOfTheSum()
  436. {
  437. return $this->tax_calculation_method == self::TAX_CALCULATION_METHOD_ROUNDING_OF_THE_SUM;
  438. }
  439. public function initTaxCalculationMethod()
  440. {
  441. $producerTaxCalculationMethod = Producer::getConfig('option_tax_calculation_method');
  442. if ($producerTaxCalculationMethod) {
  443. $this->tax_calculation_method = $producerTaxCalculationMethod;
  444. } else {
  445. $this->tax_calculation_method = self::TAX_CALCULATION_METHOD_DEFAULT;
  446. }
  447. }
  448. }