|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530 |
- <?php
-
- /**
- * Copyright distrib (2018)
- *
- * contact@opendistrib.net
- *
- * Ce logiciel est un programme informatique servant à aider les producteurs
- * à distribuer leur production en circuits courts.
- *
- * Ce logiciel est régi par la licence CeCILL soumise au droit français et
- * respectant les principes de diffusion des logiciels libres. Vous pouvez
- * utiliser, modifier et/ou redistribuer ce programme sous les conditions
- * de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
- * sur le site "http://www.cecill.info".
- *
- * En contrepartie de l'accessibilité au code source et des droits de copie,
- * de modification et de redistribution accordés par cette licence, il n'est
- * offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons,
- * seule une responsabilité restreinte pèse sur l'auteur du programme, le
- * titulaire des droits patrimoniaux et les concédants successifs.
- *
- * A cet égard l'attention de l'utilisateur est attirée sur les risques
- * associés au chargement, à l'utilisation, à la modification et/ou au
- * développement et à la reproduction du logiciel par l'utilisateur étant
- * donné sa spécificité de logiciel libre, qui peut le rendre complexe à
- * manipuler et qui le réserve donc à des développeurs et des professionnels
- * avertis possédant des connaissances informatiques approfondies. Les
- * utilisateurs sont donc invités à charger et tester l'adéquation du
- * logiciel à leurs besoins dans des conditions permettant d'assurer la
- * sécurité de leurs systèmes et ou de leurs données et, plus généralement,
- * à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
- *
- * Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
- * pris connaissance de la licence CeCILL, et que vous en avez accepté les
- * termes.
- */
-
- namespace common\models;
-
- use common\helpers\GlobalParam;
- use kartik\mpdf\Pdf;
- use Symfony\Component\Finder\Exception\DirectoryNotFoundException;
- use yii\base\ErrorException;
-
- class Document extends ActiveRecordCommon
- {
- const STATUS_DRAFT = 'draft';
- const STATUS_VALID = 'valid';
-
- const TAX_CALCULATION_METHOD_SUM_OF_ROUNDINGS = 'sum-roundings';
- const TAX_CALCULATION_METHOD_ROUNDING_OF_THE_SUM = 'rounding-sum';
- const TAX_CALCULATION_METHOD_DEFAULT = self::TAX_CALCULATION_METHOD_ROUNDING_OF_THE_SUM;
-
- public static $taxCalculationMethodArray = [
- self::TAX_CALCULATION_METHOD_ROUNDING_OF_THE_SUM => 'Arrondi de la somme des lignes',
- self::TAX_CALCULATION_METHOD_SUM_OF_ROUNDINGS => 'Somme des arrondis de chaque ligne'
- ];
-
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['name', 'id_user'], 'required'],
- [['date'], 'safe'],
- [['comment', 'address', 'tax_calculation_method'], 'string'],
- [['id_user', 'id_producer'], 'integer'],
- ['is_sent', 'boolean'],
- [['name', 'reference', 'status'], 'string', 'max' => 255],
- [['deliveryNotes'], 'safe']
- ];
- }
-
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'name' => 'Nom',
- 'reference' => 'Référence',
- 'date' => 'Date',
- 'comment' => 'Commentaire',
- 'id_user' => 'Utilisateur',
- 'address' => 'Adresse',
- 'id_producer' => 'Producteur',
- 'status' => 'Statut',
- 'tax_calculation_method' => 'Méthode de calcul de la TVA',
- 'is_sent' => 'Envoyé'
- ];
- }
-
- /*
- * Relations
- */
-
- public function getUser()
- {
- return $this->hasOne(User::className(), ['id' => 'id_user']);
- }
-
- public function getProducer()
- {
- return $this->hasOne(Producer::className(), ['id' => 'id_producer']);
- }
-
- public function relationOrders($fieldIdDocument)
- {
- $defaultOptionsSearch = Order::defaultOptionsSearch();
-
- return $this->hasMany(Order::className(), [$fieldIdDocument => 'id'])
- ->with($defaultOptionsSearch['with'])
- ->joinWith($defaultOptionsSearch['join_with'])
- ->orderBy('distribution.date ASC');
- }
-
- /*
- * Méthodes
- */
-
- public function getAmount($type = Order::AMOUNT_TOTAL, $format = false)
- {
- return $this->_getAmountGeneric($type, false, $format);
- }
-
- public function getAmountWithTax($type = Order::AMOUNT_TOTAL, $format = false)
- {
- return $this->_getAmountGeneric($type, true, $format);
- }
-
- protected function _getAmountGeneric($type = Order::AMOUNT_TOTAL, $withTax = true, $format = false)
- {
- $amount = 0;
- $totalVat = 0;
- $ordersArray = $this->orders;
-
- // Méthode de calcul via les commandes liées
- /*foreach ($ordersArray as $order) {
- $order->init($this->tax_calculation_method);
- $amount += $order->getAmount($type);
- $totalVat += $order->getTotalVat($type);
- }*/
-
- // Méthode de calcul via getProductOrders()
- foreach($this->getProductsOrders() as $productOrderArray) {
- foreach($productOrderArray as $productOrder) {
- $priceLine = $productOrder->getPriceByTypeTotal($type) * $productOrder->quantity;
- $amount += $priceLine;
- $totalVat += Price::getVat($priceLine, $productOrder->taxRate->value, $this->tax_calculation_method);
- }
- }
-
- if ($this->isTaxCalculationMethodRoundingOfTheSum()) {
- $totalVat = Price::round($totalVat);
- }
-
- if ($withTax) {
- $amount += $totalVat;
- }
-
- if ($format) {
- return Price::format($amount);
- } else {
- return $amount;
- }
- }
-
- public function getTotalVatArray($typeTotal)
- {
- $totalVatArray = [];
-
- // Méthode de calcul via les commandes liées
- /*$ordersArray = $this->orders;
- foreach ($ordersArray as $order) {
- $order->init($this->tax_calculation_method);
- $fieldNameVat = $order->getFieldNameAmount($typeTotal, 'vat');
- foreach ($order->$fieldNameVat as $idTaxRate => $vat) {
- if (!isset($totalVatArray[$idTaxRate])) {
- $totalVatArray[$idTaxRate] = 0;
- }
- $totalVatArray[$idTaxRate] += $vat;
- }
- }*/
-
- // Méthode de calcul via getProductOrders()
- foreach($this->getProductsOrders() as $productOrderArray) {
- foreach ($productOrderArray as $productOrder) {
-
- $idTaxRate = $productOrder->taxRate->id;
- if (!isset($totalVatArray[$idTaxRate])) {
- $totalVatArray[$idTaxRate] = 0;
- }
-
- $totalVatArray[$idTaxRate] += Price::getVat(
- $productOrder->getPriceByTypeTotal($typeTotal) * $productOrder->quantity,
- $productOrder->taxRate->value,
- $this->tax_calculation_method
- );
- }
- }
-
- return $totalVatArray;
- }
-
- public function getPointSale()
- {
- if (isset($this->orders) && isset($this->orders[0])) {
- return $this->orders[0]->pointSale;
- } else {
- return '';
- }
- }
-
- public function getDistribution()
- {
- if (isset($this->orders) && isset($this->orders[0])) {
- return $this->orders[0]->distribution;
- } else {
- return '';
- }
- }
-
- public function getClass()
- {
- return str_replace('common\models\\', '', get_class($this));
- }
-
- public function getType()
- {
- $class = $this->getClass();
-
- if ($class == 'Invoice') {
- $documentType = 'Facture';
- } elseif ($class == 'DeliveryNote') {
- $documentType = 'Bon de livraison';
- } elseif ($class == 'Quotation') {
- $documentType = 'Devis';
- }
-
- if (isset($documentType)) {
- return $documentType;
- }
-
- return '';
- }
-
- public function isValidClass($typeDocument)
- {
- return in_array($typeDocument, ['Invoice', 'DeliveryNote', 'Quotation']);
- }
-
- public function generateReference()
- {
- $class = $this->getClass();
- $classLower = strtolower($class);
- if ($classLower == 'deliverynote') {
- $classLower = 'delivery_note';
- }
-
- $prefix = Producer::getConfig('document_' . $classLower . '_prefix');
- $oneDocumentExist = $class::searchOne(['status' => Document::STATUS_VALID], ['orderby' => 'reference DESC']);
-
- if ($oneDocumentExist) {
- $reference = $oneDocumentExist->reference;
- $pattern = '#([A-Z]+)?([0-9]+)#';
- preg_match($pattern, $reference, $matches, PREG_OFFSET_CAPTURE);
- $sizeNumReference = strlen($matches[2][0]);
- $numReference = ((int)$matches[2][0]) + 1;
- $numReference = str_pad($numReference, $sizeNumReference, '0', STR_PAD_LEFT);
-
- return $prefix . $numReference;
- } else {
- $firstReference = Producer::getConfig('document_' . $classLower . '_first_reference');
-
- if (strlen($firstReference) > 0) {
- return $firstReference;
- } else {
- return $prefix . '00001';
- }
- }
- }
-
- public function downloadPdf()
- {
- $filenameComplete = $this->getFilenameComplete();
-
- /*if (!file_exists($filenameComplete)) {
- $this->generatePdf(Pdf::DEST_FILE);
- }*/
- $this->generatePdf(Pdf::DEST_FILE);
-
- if (file_exists($filenameComplete)) {
- return Yii::$app->response->sendFile($filenameComplete, $this->getFilename(), ['inline' => true]);
- } else {
- throw new ErrorException('File ' . $filenameComplete . ' not found');
- }
- }
-
- public function generatePdf($destination)
- {
- $producer = GlobalParam::getCurrentProducer();
- $content = Yii::$app->controller->renderPartial('/document/download', [
- 'producer' => $producer,
- 'document' => $this
- ]);
-
- $contentFooter = '<div id="footer">';
- $contentFooter .= '<div class="infos-bottom">' . Html::encode($producer->document_infos_bottom) . '</div>';
- if ($this->isStatusValid() || $this->isStatusDraft()) {
- $contentFooter .= '<div class="reference-document">';
- if ($this->isStatusValid()) {
- $contentFooter .= $this->getType() . ' N°' . $this->reference;
- }
- if ($this->isStatusDraft()) {
- $contentFooter .= $this->getType() . ' non validé';
- if ($this->getType() == 'Facture') {
- $contentFooter .= 'e';
- }
- }
- $contentFooter .= '</div>';
- }
- $contentFooter .= '</div>';
-
- $marginBottom = 10;
- if (strlen(Producer::getConfig('document_infos_bottom')) > 0) {
- $marginBottom = 40;
- }
-
- $this->initDirectoryPdf();
-
- $pdf = new Pdf([
- 'mode' => Pdf::MODE_UTF8,
- 'format' => Pdf::FORMAT_A4,
- 'orientation' => Pdf::ORIENT_PORTRAIT,
- 'destination' => $destination,
- 'content' => $content,
- 'filename' => $this->getFilenameComplete(),
- 'cssFile' => Yii::getAlias('@webroot/css/document/download.css'),
- 'marginBottom' => $marginBottom,
- 'methods' => [
- 'SetHTMLFooter' => $contentFooter
- ]
- ]);
-
- return $pdf->render();
- }
-
- public function send()
- {
- if (isset($this->user) && strlen($this->user->email) > 0) {
- $producer = GlobalParam::getCurrentProducer();
-
- $subjectEmail = $this->getType();
- if ($this->isStatusValid()) {
- $subjectEmail .= ' N°' . $this->reference;
- }
-
- $email = Yii::$app->mailer->compose(
- [
- 'html' => 'sendDocument-html',
- 'text' => 'sendDocument-text'
- ], [
- 'document' => $this,
- ])
- ->setTo($this->user->email)
- ->setFrom([$producer->getEmailOpendistrib() => $producer->name])
- ->setSubject('[' . $producer->name . '] ' . $subjectEmail);
-
- $this->generatePdf(Pdf::DEST_FILE);
- $email->attach($this->getFilenameComplete());
-
- return $email->send();
- }
-
- return false;
- }
-
- public function changeStatus($status)
- {
- if ($status == Document::STATUS_VALID) {
- $this->status = $status;
- $this->reference = $this->generateReference();
- }
- }
-
- public function getStatusWording()
- {
- return ($this->status == self::STATUS_DRAFT) ? 'Brouillon' : 'Validé';
- }
-
- public function getStatusCssClass()
- {
- return ($this->status == self::STATUS_DRAFT) ? 'default' : 'success';
- }
-
- public function getHtmlLabel()
- {
- $label = $this->getStatusWording();
- $classLabel = $this->getStatusCssClass();
- return '<span class="label label-' . $classLabel . '">' . $label . '</span>';
- }
-
- public function isStatus($status)
- {
- return $this->status == $status;
- }
-
- public function isStatusDraft()
- {
- return $this->isStatus(self::STATUS_DRAFT);
- }
-
- public function isStatusValid()
- {
- return $this->isStatus(self::STATUS_VALID);
- }
-
- public function getProductsOrders()
- {
- $productsOrdersArray = [];
- $ordersArray = $this->orders;
- if ($ordersArray && count($ordersArray)) {
- foreach ($ordersArray as $order) {
- foreach ($order->productOrder as $productOrder) {
- if ($productOrder->product) {
- $indexProductOrder = $productOrder->product->order;
- $newProductOrder = clone $productOrder;
-
- if (!isset($productsOrdersArray[$indexProductOrder])) {
- $productsOrdersArray[$indexProductOrder] = [$newProductOrder];
- } else {
- $productOrderMatch = false;
- foreach ($productsOrdersArray[$indexProductOrder] as &$theProductOrder) {
- if ($theProductOrder->unit == $productOrder->unit
- && ((!$this->isInvoicePrice() && $theProductOrder->price == $productOrder->price)
- || ($this->isInvoicePrice() && $theProductOrder->invoice_price == $productOrder->invoice_price)
- )) {
-
- $theProductOrder->quantity += $productOrder->quantity;
- $productOrderMatch = true;
- }
- }
- if (!$productOrderMatch) {
- $productsOrdersArray[$indexProductOrder][] = $newProductOrder;
- }
- }
- }
- }
- }
- }
-
- // tri des orderProduct par product.order
- ksort($productsOrdersArray);
-
- return $productsOrdersArray;
- }
-
- public function isDisplayOrders()
- {
- $displayOrders = ($this->getClass() == 'Invoice') ?
- Producer::getConfig('document_display_orders_invoice') :
- Producer::getConfig('document_display_orders_delivery_note');
-
- return $displayOrders;
- }
-
- public function getAliasDirectoryBase()
- {
- return '@app/web/pdf/' . $this->id_producer . '/';
- }
-
- public function initDirectoryPdf()
- {
- $aliasDirectoryBase = $this->getAliasDirectoryBase();
- $directoryPdf = Yii::getAlias($aliasDirectoryBase);
- if (!file_exists($directoryPdf)) {
- mkdir($directoryPdf, 0755);
- }
- }
-
- public function getFilename()
- {
- $filename = $this->getType() . '-';
-
- if($this->isStatusValid()) {
- $filename .= $this->reference;
- }
- elseif($this->isStatusDraft()) {
- $filename .= 'Brouillon-'.$this->id;
- }
-
- $filename .= '.pdf';
-
- return $filename;
- }
-
- public function getFilenameComplete()
- {
- return Yii::getAlias($this->getAliasDirectoryBase() . $this->getFilename());
- }
-
- public function isInvoicePrice()
- {
- return $this->getClass() == 'Invoice' || $this->getClass() == 'DeliveryNote';
- }
-
- public function isTaxCalculationMethodSumOfRoundings()
- {
- return $this->tax_calculation_method == self::TAX_CALCULATION_METHOD_SUM_OF_ROUNDINGS;
- }
-
- public function isTaxCalculationMethodRoundingOfTheSum()
- {
- return $this->tax_calculation_method == self::TAX_CALCULATION_METHOD_ROUNDING_OF_THE_SUM;
- }
-
- public function initTaxCalculationMethod()
- {
- $producerTaxCalculationMethod = Producer::getConfig('option_tax_calculation_method');
-
- if ($producerTaxCalculationMethod) {
- $this->tax_calculation_method = $producerTaxCalculationMethod;
- } else {
- $this->tax_calculation_method = self::TAX_CALCULATION_METHOD_DEFAULT;
- }
- }
- }
|