|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871 |
- <?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 backend\controllers;
-
- use common\helpers\Ajax;
- use common\helpers\CSV;
- use common\helpers\GlobalParam;
- use common\helpers\MeanPayment;
- use common\helpers\Price;
- use common\logic\Document\DeliveryNote\Model\DeliveryNote;
- use common\logic\Document\Document\Model\Document;
- use common\logic\Document\Invoice\Model\Invoice;
- use common\logic\Document\Quotation\Model\Quotation;
- use common\logic\Order\Order\Model\Order;
- use common\logic\Order\ProductOrder\Model\ProductOrder;
- use common\logic\Product\Product\Model\Product;
- use kartik\mpdf\Pdf;
- use yii\base\UserException;
- use yii;
- use yii\filters\VerbFilter;
- use yii\filters\AccessControl;
- use yii\web\NotFoundHttpException;
- use yii\helpers\Html;
- use common\logic\Payment\Model\Payment;
-
- class DocumentController extends BackendController
- {
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::class,
- 'rules' => [
- [
- 'allow' => true,
- 'roles' => ['@'],
- 'matchCallback' => function ($rule, $action) {
- return $this->getUserModule()
- ->getAuthorizationChecker()
- ->isGrantedAsProducer($this->getUserCurrent());
- }
- ]
- ],
- ],
- ];
- }
-
- public function actionGeneratePdfValidatedDocuments()
- {
- set_time_limit(0);
-
- $validatedDocumentsArray = array_merge(
- Quotation::find()->where(['status' => Document::STATUS_VALID])->all(),
- DeliveryNote::find()->where(['status' => Document::STATUS_VALID])->all(),
- Invoice::find()->where(['status' => Document::STATUS_VALID])->all()
- );
-
- foreach ($validatedDocumentsArray as $document) {
- if (!file_exists($document->getFilenameComplete())) {
- $document->generatePdf(Pdf::DEST_FILE);
- }
- }
- }
-
- public function actionCreate()
- {
- $documentManager = $this->getDocumentManager();
-
- $class = $this->getClass();
- $class = 'common\\logic\\Document\\'.$class.'\\Model\\'.$class;
- $model = new $class();
-
- $documentManager->initTaxCalculationMethod($model);
-
- if ($model->load(\Yii::$app->request->post())) {
- $model->id_producer = GlobalParam::getCurrentProducerId();
-
- if ($model->save()) {
- $this->processInvoiceViaDeliveryNotes($model);
- $this->processInvoiceViaOrders($model);
-
- $this->setFlash('success', $this->getFlashMessage('create', $model));
- return $this->redirect(['/' . $this->getControllerUrl() . '/update', 'id' => $model->id]);
- } else {
- $this->setFlash('error', 'Un problème est survenu lors de la création du document.');
- }
- }
-
- return $this->render('/document/create', [
- 'title' => $this->getTitle('Ajouter'),
- 'typeDocument' => $this->getDocumentType(),
- 'model' => $model,
- ]);
- }
-
- public function processInvoiceViaDeliveryNotes($model)
- {
- $orderManager = $this->getOrderManager();
- $documentManager = $this->getDocumentManager();
- $deliveryNoteManager = $this->getDeliveryNoteManager();
- if ($documentManager->getClass($model) == 'Invoice') {
- if ($model->deliveryNotes && is_array($model->deliveryNotes) && count($model->deliveryNotes)) {
- foreach ($model->deliveryNotes as $key => $idDeliveryNote) {
- $deliveryNote = $deliveryNoteManager->findOneDeliveryNoteById($idDeliveryNote);
- $orderManager->assignAllOrdersInvoiceByDeliveryNote($model, $deliveryNote);
- }
- }
- }
- }
-
- public function processInvoiceViaOrders($model)
- {
- $orderManager = $this->getOrderManager();
- $documentManager = $this->getDocumentManager();
- if ($documentManager->getClass($model) == 'Invoice') {
- if ($model->ordersOnCreate && is_array($model->ordersOnCreate) && count($model->ordersOnCreate)) {
- foreach ($model->ordersOnCreate as $key => $idOrder) {
- $order = $orderManager->findOneOrderById($idOrder);
- $orderManager->updateOrderInvoice($order, $model);
- }
- }
- }
- }
-
- public function actionUpdate($id)
- {
- $documentManager = $this->getDocumentManager();
- $paymentManager = $this->getPaymentManager();
- $document = $this->findModel($id);
-
- if (!$document) {
- throw new yii\web\NotFoundHttpException('Le document n\'a pas été trouvé.');
- }
-
- if ($document && $document->load(\Yii::$app->request->post()) && $document->save()) {
- $this->setFlash('success', $this->getFlashMessage('update', $document));
- }
-
- $payment = null;
- if($documentManager->isDocumentInvoice($document) && $documentManager->isStatusValid($document)) {
- $payment = $paymentManager->instanciatePayment(
- Payment::TYPE_PAYMENT,
- number_format($documentManager->getAmountWithTax($document, Order::INVOICE_AMOUNT_TOTAL), 2),
- $this->getProducerCurrent(),
- null,
- null,
- MeanPayment::TRANSFER,
- null,
- null,
- $document
- );
- $payment->amount = number_format($payment->amount, 2);
-
- $posts = \Yii::$app->request->post();
- if(isset($posts['Payment']['date_transaction']) && $posts['Payment']['date_transaction']) {
- $posts['Payment']['date_transaction'] = date('Y-m-d', strtotime(str_replace('/', '-', $posts['Payment']['date_transaction'])));
- }
-
- if ($payment->load($posts) && $payment->save()) {
- $this->setFlash('success', 'Le règlement a bien été ajouté.');
- return $this->redirect(['invoice/update', 'id' => $document->id]);
- }
- else {
- if($payment->date_transaction) {
- $payment->date_transaction = date('d/m/Y', strtotime($payment->date_transaction));
- }
- }
- }
-
- return $this->render('/document/update', [
- 'title' => $this->getTitle('Modifier'),
- 'typeDocument' => $this->getDocumentType(),
- 'model' => $document,
- 'payment' => $payment
- ]);
- }
-
- public function actionDelete($id)
- {
- $documentManager = $this->getDocumentManager();
- $model = $this->findModel($id);
-
- if ($documentManager->isStatusValid($model)) {
- throw new UserException('Vous ne pouvez pas supprimer un document validé.');
- }
-
- $documentManager->delete($model);
-
- if ($this->getClass() == 'DeliveryNote') {
- Order::updateAll([
- 'order.id_delivery_note' => null
- ], [
- 'order.id_delivery_note' => $id
- ]);
- }
-
- if ($this->getClass() == 'Quotation') {
- Order::updateAll([
- 'order.id_quotation' => null
- ], [
- 'order.id_quotation' => $id
- ]);
- }
-
- if ($this->getClass() == 'Invoice') {
- Order::updateAll([
- 'order.id_invoice' => null
- ], [
- 'order.id_invoice' => $id
- ]);
- }
-
- $this->setFlash('success', $this->getFlashMessage('delete', $model));
-
- $this->redirect([$this->getControllerUrl() . '/index']);
- }
-
- public function actionExportCsvEvoliz(int $id)
- {
- $documentManager = $this->getDocumentManager();
- $productOrderManager = $this->getProductOrderManager();
-
- $datas = [];
- $document = $this->findModel($id);
-
- // données
- $datas[] = [
- 'N° facture externe *',
- 'Date facture *',
- 'Client',
- 'Code client *',
- 'Total TVA',
- 'Total HT',
- 'Total TTC',
- 'Total réglé',
- 'Etat',
- 'Date Etat',
- 'Date de création',
- 'Objet',
- 'Date d\'échéance',
- 'Date d\'exécution',
- 'Taux de pénalité',
- 'Frais de recouvrement',
- 'Taux d\'escompte',
- 'Conditions de règlement *',
- 'Mode de paiement',
- 'Remise globale',
- 'Acompte',
- 'Nombre de relance',
- 'Commentaires',
- 'N° facture',
- 'Annulé',
- 'Catalogue',
- 'Réf.',
- 'Désignation *',
- 'Qté *',
- 'Unité',
- 'PU HT *',
- 'Remise',
- 'TVA',
- 'Total TVA',
- 'Total HT',
- 'Classification vente',
- 'Code Classification vente',
- ];
-
- foreach ($documentManager->getProductsOrders($document) as $productOrderArray) {
- foreach ($productOrderArray as $productOrder) {
-
- $price = $productOrder->getPrice();
- if ($documentManager->isInvoicePrice($document) && $productOrder->getInvoicePrice()) {
- $price = $productOrder->getInvoicePrice();
- }
-
- $typeTotal = $documentManager->isInvoicePrice($document) ? Order::INVOICE_AMOUNT_TOTAL : Order::AMOUNT_TOTAL;
- $priceTotal = $productOrderManager->getPriceByTypeTotal($productOrder, $typeTotal) * $productOrder->quantity;
- $tva = Price::getVat(
- $priceTotal,
- $productOrder->taxRate->value,
- $document->tax_calculation_method
- );
-
- $datas[] = [
- $document->reference, // N° facture externe *
- date('d/m/Y', strtotime($document->date)), // Date facture *
- '', // Client
- $document->user->evoliz_code, // Code client *
- '', // Total TVA
- '', // Total HT
- '', // Total TTC
- '', // Total réglé
- '', // Etat
- '', // Date Etat
- '', // Date de création
- $document->name, // Objet
- '', // Date d'échéance
- '', // Date d'exécution
- '', // Taux de pénalité
- '', // Frais de recouvrement
- '', // Taux d\'escompte
- 'A réception', // Conditions de règlement *
- '', // Mode de paiement
- '', // Remise globale
- '', // Acompte
- '', // Nombre de relance
- '', // Commentaires
- '', // N° facture
- '', // Annulé
- 'Non', // Catalogue
- '', // Réf.
- $productOrder->product->name, // Désignation *
- $productOrder->quantity, // Qté *
- '', // Product::strUnit($productOrder->unit, 'wording'), // Unité
- $price, // PU HT *
- '', // Remise
- $productOrder->taxRate->value * 100, // TVA
- $tva, // Total TVA
- $priceTotal, // Total HT
- '', // Classification vente
- '01', // Code Classification vente
- ];
- }
- }
-
- // nom fichier
- $reference = $document->id;
- if ($document->reference && strlen($document->reference)) {
- $reference = $document->reference;
- }
-
- // status
- $status = '';
- if ($documentManager->isStatusDraft($document)) {
- $status = 'brouillon_';
- }
-
- CSV::downloadSendHeaders(strtolower($this->getDocumentType()) . '_' . $status . $reference . '.csv');
- echo CSV::array2csv($datas);
- die();
- }
-
- public function actionDownload($id)
- {
- $documentManager = $this->getDocumentManager();
- $document = $this->findModel($id);
-
- return $documentManager->downloadPdf($document);
- }
-
- public function actionRegenerate($id)
- {
- $documentManager = $this->getDocumentManager();
- $document = $this->findModel($id);
- $documentManager->downloadPdf($document, true);
- $this->setFlash('success', 'Le document PDF a bien été regénéré.');
-
- return $this->redirect([$this->getControllerUrl() . '/update', 'id' => $id]);
- }
-
- public function actionSend(int $id, $backUpdateForm = false)
- {
- $documentManager = $this->getDocumentManager();
- $document = $this->findModel($id);
- if ($documentManager->sendDocument($document)) {
- $this->setFlash('success', $this->getFlashMessage('send', $document));
- } else {
- $this->setFlash('danger', $this->getFlashMessage('send', $document));
- }
-
- if ($backUpdateForm) {
- return $this->redirect([$this->getControllerUrl() . '/update', 'id' => $id]);
- } else {
- return $this->redirectReferer();
- }
- }
-
- public function actionAjaxUserInfos($typeAction, $idUser, $classDocument, $idDocument = false)
- {
- $userModule = $this->getUserModule();
- $documentManager = $this->getDocumentManager();
- $orderManager = $this->getOrderManager();
- $producerManager = $this->getProducerManager();
- $producerCurrent = $this->getProducerCurrent();
-
- \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
-
- if ($idUser > 0) {
- $user = $userModule->findOneUserById($idUser);
-
- if ($user) {
- $document = null;
- if ($documentManager->isValidClass($classDocument) && $idDocument) {
- $document = $this->findModel($idDocument, $classDocument);
- }
-
- if ($document && $document->id_user == $user->id) {
- $address = $document->address;
- } else {
- $address = $userModule->getFullAddress($user);
- }
-
- $json = [
- 'return' => 'success',
- 'address' => $address
- ];
-
- if ($classDocument == 'Invoice') {
- $options = [
- 'orderby' => 'distribution.date ASC',
- 'join_with' => ['user AS user_delivery_note', 'orders', 'producer']
- ];
- $deliveryNotesCreateArray = DeliveryNote::searchAll([
- 'id_user' => $user->id,
- 'status' => Document::STATUS_VALID,
- 'ignore_when_invoicing' => null
- ], $options);
- $deliveryNotesUpdateArray = DeliveryNote::searchAll([
- 'id_user' => $user->id,
- 'status' => Document::STATUS_VALID,
- 'order.id_invoice' => $idDocument,
- 'ignore_when_invoicing' => null
- ], $options);
- $json['delivery_note_create_array'] = $this->initDeliveryNoteArray('create', $deliveryNotesCreateArray);
- $json['delivery_note_update_array'] = $this->initDeliveryNoteArray('update', $deliveryNotesUpdateArray);
-
-
- $json['orders_create_array'] = [];
- $json['orders_update_array'] = [];
- if(!$producerManager->getConfig('option_invoice_only_based_on_delivery_notes')) {
- $json['orders_create_array'] = $this->initOrdersArray($orderManager->findOrdersByUserNotInvoiced($user));
- $json['orders_update_array'] = $document ? $this->initOrdersArray($orderManager->findOrdersByUserAndInvoice($user, $document)) : [];
- }
- }
-
- if ($classDocument == 'DeliveryNote') {
- $json['orders_create_array'] = $this->initOrdersArray($orderManager->findOrdersByUserNotLinkedDeliveryNote($user));
- $json['orders_update_array'] = $document ? $this->initOrdersArray($orderManager->findOrdersByUserAndDeliveryNote($user, $document)) : [];
- }
-
- return $json;
- }
- }
-
- return ['return' => 'error'];
- }
-
- public function initOrdersArray(array $ordersArray)
- {
- $orderManager = $this->getOrderManager();
- $ordersReturnArray = [];
-
- foreach($ordersArray as &$order) {
- $orderManager->initOrder($order);
- $ordersReturnArray[] = [
- 'id' => $order->id,
- 'date' => $order->distribution->date,
- 'name' => date('d/m/Y', strtotime($order->distribution->date)),
- 'amount_with_tax' => $orderManager->getOrderAmountWithTax($order, Order::INVOICE_AMOUNT_TOTAL)
- ];
- }
-
- return $ordersReturnArray;
- }
-
- public function initDeliveryNoteArray($type, $deliveryNoteArrayResults)
- {
- $deliveryNoteArray = [];
- $isCreate = false;
- if ($type == 'create') {
- $isCreate = true;
- }
-
- if ($deliveryNoteArrayResults) {
- foreach ($deliveryNoteArrayResults as $deliveryNote) {
- $deliveryNoteData = $this->addDeliveryNoteToArray($deliveryNote, $isCreate);
- if ($deliveryNoteData) {
- $deliveryNoteArray[] = $deliveryNoteData;
- }
- }
- }
-
- return $deliveryNoteArray;
- }
-
- public function addDeliveryNoteToArray($deliveryNote, $isCreate = true)
- {
- $deliveryNoteManager = $this->getDeliveryNoteManager();
-
- $deliveryNoteData = array_merge(
- $deliveryNote->getAttributes(),
- [
- 'url' => $this->getUrlManagerBackend()->createUrl(['delivery-note/update', 'id' => $deliveryNote->id]),
- 'total' => $deliveryNoteManager->getAmountWithTax($deliveryNote, Order::INVOICE_AMOUNT_TOTAL)
- ]
- );
-
- if (($isCreate && !$deliveryNoteManager->isInvoiced($deliveryNote)) || !$isCreate) {
- return $deliveryNoteData;
- }
-
- return false;
- }
-
- public function actionValidate($id, $backUpdateForm = false)
- {
- $documentManager = $this->getDocumentManager();
- $classDocument = $this->getClass();
-
- if ($id > 0 && $documentManager->isValidClass($classDocument)) {
- $document = $this->findModel($id);
-
- if ($document) {
- $documentManager->validateDocument($document);
-
- // @TODO : gérer via un événement
- $documentManager->generatePdf($document, Pdf::DEST_FILE);
-
- $this->setFlash('success', $this->getFlashMessage('validate', $document));
-
- if ($backUpdateForm) {
- return $this->redirect([$this->getControllerUrl() . '/update', 'id' => $id]);
- } else {
- return $this->redirect([$this->getControllerUrl() . '/index']);
- }
- }
- }
-
- $this->setFlash('danger', 'Une erreur est survenue lors de la validation du document.');
-
- return $this->redirect([$this->getControllerUrl() . '/index']);
- }
-
- public function actionAjaxInit($idDocument, $classDocument)
- {
- \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
-
- $orderManager = $this->getOrderManager();
- $productManager = $this->getProductManager();
- $documentManager = $this->getDocumentManager();
- $deliveryNoteManager = $this->getDeliveryNoteManager();
- $userProducerManager = $this->getUserProducerManager();
- $pointSaleManager = $this->getPointSaleManager();
-
- if ($idDocument > 0 && $documentManager->isValidClass($classDocument)) {
-
- $document = $this->findModel($idDocument, $classDocument);
-
- if ($document) {
- $ordersArray = [];
- $productsArray = $productManager->findProducts(false);
-
- foreach ($document->orders as $order) {
- $orderManager->initOrder($order);
- $productsOrderArray = [];
-
- foreach ($order->productOrder as $productOrder) {
- $productsOrderArray[$productOrder->id] = array_merge($productOrder->getAttributes(), [
- 'url_order' => $this->getUrlManagerBackend()->createUrl(['distribution/index', 'idOrderUpdate' => $productOrder->id_order])
- ]);
- }
- $ordersArray[$order->id] = array_merge(
- $order->getAttributes(),
- [
- 'username' => $orderManager->getOrderUsername($order),
- 'distribution_date' => isset($order->distribution) ? date(
- 'd/m/Y',
- strtotime(
- $order->distribution->date
- )
- ) : null,
- 'point_sale_name' => isset($order->pointSale) ? $order->pointSale->name : null,
- 'productOrder' => $productsOrderArray,
- ]
- );
- }
-
- $userProducer = $userProducerManager->findOneUserProducer($document->user);
- $pointSale = $pointSaleManager->findOnePointSaleByIdUser($document->user->id);
-
- $productsArray = yii\helpers\ArrayHelper::map(
- $productsArray,
- 'order',
- function ($product) use ($document, $userProducer, $pointSale, $productManager) {
- return array_merge($product->getAttributes(), [
- 'unit_coefficient' => Product::$unitsArray[$product->unit]['coefficient'],
- 'prices' => $productManager->getPriceArray($product, $userProducer->user, $pointSale),
- 'wording_unit' => $product->wording_unit,
- 'tax_rate' => $product->taxRate->value
- ]);
- }
- );
-
- return [
- 'return' => 'success',
- 'tax_rate_producer' => GlobalParam::getCurrentProducer()->taxRate->value,
- 'document' => array_merge($document->getAttributes(), [
- 'html_label' => $documentManager->getHtmlLabel($document),
- 'class' => $documentManager->getClass($document)
- ]),
- 'id_user' => $document->user->id,
- 'products' => $productsArray,
- 'orders' => $ordersArray,
- 'total' => ($documentManager->getClass($document) == 'Invoice' || $documentManager->getClass($document) == 'DeliveryNote') ? $documentManager->getAmount(
- $document,
- Order::INVOICE_AMOUNT_TOTAL
- ) : $documentManager->getAmount($document, Order::AMOUNT_TOTAL),
- 'total_with_tax' => ($documentManager->getClass($document) == 'Invoice' || $documentManager->getClass($document) == 'DeliveryNote') ? $documentManager->getAmountWithTax(
- $document,
- Order::INVOICE_AMOUNT_TOTAL
- ) : $documentManager->getAmountWithTax($document, Order::AMOUNT_TOTAL),
- 'invoice_url' => ($documentManager->getClass($document) == 'DeliveryNote' && $deliveryNoteManager->getInvoice($document)) ? $this->getUrlManagerBackend()->createUrl(['invoice/update', 'id' => $deliveryNoteManager->getInvoice($document)->id]) : null
- ];
- }
- }
-
- return ['return' => 'error'];
- }
-
- public function actionAjaxAddProduct($idDocument, $classDocument, $idProduct, $quantity, $price)
- {
- $documentManager = $this->getDocumentManager();
- $productManager = $this->getProductManager();
-
- if ($documentManager->isValidClass($classDocument)) {
- $document = $this->findModel($idDocument, $classDocument);
- $classDocumentComplete = $documentManager->getClass($document, true);
- $product = $productManager->findOneProductById($idProduct);
-
- if ($document && $product) {
- if (count($document->orders) == 0) {
- $order = new Order;
- $order->id_user = $document->id_user;
- $order->id_point_sale = null;
- $order->id_distribution = null;
- $order->status = 'tmp-order';
- $order->origin = Order::ORIGIN_ADMIN;
- $order->date = date('Y-m-d H:i:s');
- $fieldIdDocument = 'id_' . $classDocumentComplete::tableName();
- $order->$fieldIdDocument = $document->id;
- $order->save();
- } else {
- $order = $document->orders[0];
- }
-
- if ($order) {
- $productOrder = new ProductOrder();
- $productOrder->id_order = $order->id;
- $productOrder->id_product = $idProduct;
- $quantity = $quantity / Product::$unitsArray[$product->unit]['coefficient'];
- $productOrder->quantity = $quantity;
- $productOrder->price = (float)$price;
- $productOrder->unit = $product->unit;
- $productOrder->step = $product->step;
- $productOrder->id_tax_rate = $product->taxRate->id;
- $productOrder->save();
-
- return Ajax::responseSuccess('Produit ajouté');
- }
- }
- }
-
- return Ajax::responseError('Une erreur est survenue lors de la suppression du produit.');
- }
-
- public function actionAjaxDeleteProductOrder($idProductOrder)
- {
- $productOrderManager = $this->getProductOrderManager();
-
- $productOrder = $productOrderManager->findOneProductOrderById($idProductOrder);
-
- if ($productOrder) {
- $productOrderManager->delete($productOrder);
-
- return Ajax::responseSuccess('Produit supprimé');
- }
-
- return Ajax::responseError('Une erreur est survenue lors de la suppression du produit.');
- }
-
- public function actionAjaxAddOrder($idDocument, $classDocument, $idOrder)
- {
- $documentManager = $this->getDocumentManager();
- $orderManager = $this->getOrderManager();
-
- $document = $documentManager->findOneDocumentByIdAndClass($idDocument, $classDocument);
- $order = $orderManager->findOneOrderById($idOrder);
-
- if ($document && $documentManager->isStatusDraft($document) && $order) {
- $orderManager->updateOrderDocument($order, $document);
-
- return Ajax::responseSuccess("Commande ajoutée à la facture.");
- }
- else {
- return Ajax::responseError("Une erreur est survenue lors de l'ajout de la commande.");
- }
- }
-
- public function actionAjaxDeleteOrder($idDocument, $classDocument, $idOrder)
- {
- $documentManager = $this->getDocumentManager();
- $orderManager = $this->getOrderManager();
-
- $document = $documentManager->findOneDocumentByIdAndClass($idDocument, $classDocument);
- $order = $orderManager->findOneOrderById($idOrder);
-
- if ($document && $documentManager->isStatusDraft($document)) {
- if($documentManager->isDocumentDeliveryNote($document)) {
- $orderManager->updateOrderDeliveryNote($order, null);
- }
- elseif($documentManager->isDocumentInvoice($document)) {
- $orderManager->updateOrderInvoice($order, null);
- }
-
- return Ajax::responseSuccess('Commande supprimée de la facture.');
- }
- else {
- return Ajax::responseError('Une erreur est survenue lors de la suppression de la commande.');
- }
- }
-
- public function actionAjaxIgnoreOrderWhenInvoicing($idDocument, $classDocument, $idOrder)
- {
- $documentManager = $this->getDocumentManager();
- $orderManager = $this->getOrderManager();
-
- $document = $documentManager->findOneDocumentByIdAndClass($idDocument, $classDocument);
- $order = $orderManager->findOneOrderById($idOrder);
-
- if ($document && $documentManager->isStatusDraft($document) && $order) {
- $orderManager->updateOrderIgnoreWhenInvoicing($order, true);
- return Ajax::responseSuccess("La commande sera maintenant ignorée au moment de la facturation.");
- }
- else {
- return Ajax::responseError("Une erreur est survenue.");
- }
- }
-
- public function getClass()
- {
- $class = get_class($this);
- $class = str_replace('Controller', '', $class);
- $class = str_replace('backend\controllers\\', '', $class);
- return $class;
- }
-
- public function getDocumentType()
- {
- $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 getFlashMessage(string $type, $model)
- {
- $class = $this->getClass();
-
- $message = $this->getDocumentType();
- $message .= ' <strong>' . Html::encode($model->name) . '</strong> ';
-
- if ($type == 'create') {
- $message .= 'ajouté';
- } elseif ($type == 'update') {
- $message .= 'modifié';
- } elseif ($type == 'delete') {
- $message .= 'supprimé';
- } elseif ($type == 'validate') {
- $message .= 'validé';
- } elseif ($type == 'send') {
- $message .= 'envoyé';
- }
-
- if ($class == 'Invoice') {
- $message .= 'e';
- }
-
- return $message;
- }
-
- protected function getTitle($prepend)
- {
- $class = $this->getClass();
-
- switch ($class) {
- case 'Invoice' :
- $title = $prepend . ' une facture';
- break;
- case 'DeliveryNote' :
- $title = $prepend . ' un bon de livraison';
- break;
- case 'Quotation' :
- $title = $prepend . ' un devis';
- break;
- }
-
- return $title;
- }
-
- public function getControllerUrl()
- {
- $path = strtolower($this->getClass());
- $path = str_replace('deliverynote', 'delivery-note', $path);
- return $path;
- }
-
- /**
- * Recherche un Document en fonction de son ID.
- *
- * @param integer $id
- * @return Document
- * @throws NotFoundHttpException si le modèle n'est pas trouvé
- */
- protected function findModel($idDocument, $classDocument = null)
- {
- $documentManager = $this->getDocumentManager();
- $model = $documentManager->findOneDocumentByIdAndClass($idDocument, $classDocument ?? $this->getClass());
-
- if ($model) {
- return $model;
- } else {
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- }
- }
|