$document = $this->findModel($id); | $document = $this->findModel($id); | ||||
if ($document) { | if ($document) { | ||||
$documentModule->validateDocument($document); | |||||
$documentModule->getManager()->validateDocument($document); | |||||
// @TODO : gérer via un événement | // @TODO : gérer via un événement | ||||
$documentModule->generatePdf($document, Pdf::DEST_FILE); | $documentModule->generatePdf($document, Pdf::DEST_FILE); |
0 => 'Non', | 0 => 'Non', | ||||
1 => 'Oui' | 1 => 'Oui' | ||||
]); ?> | ]); ?> | ||||
<?= $form->field($model, 'document_quotation_prefix'); ?> | |||||
<?php $hintKeywordsPrefix = "Saisissez [ANNEE] pour intégrer l'année courante"; ?> | |||||
<?= $form->field($model, 'document_quotation_prefix')->hint($hintKeywordsPrefix); ?> | |||||
<?= $form->field($model, 'document_quotation_first_reference'); ?> | <?= $form->field($model, 'document_quotation_first_reference'); ?> | ||||
<?= $form->field($model, 'document_quotation_duration'); ?> | <?= $form->field($model, 'document_quotation_duration'); ?> | ||||
<?= $form->field($model, 'document_invoice_prefix'); ?> | |||||
<?= $form->field($model, 'document_invoice_prefix')->hint($hintKeywordsPrefix);; ?> | |||||
<?= $form->field($model, 'document_invoice_first_reference'); ?> | <?= $form->field($model, 'document_invoice_first_reference'); ?> | ||||
<?= $form->field($model, 'document_delivery_note_prefix'); ?> | |||||
<?= $form->field($model, 'document_delivery_note_prefix')->hint($hintKeywordsPrefix);; ?> | |||||
<?= $form->field($model, 'document_delivery_note_first_reference'); ?> | <?= $form->field($model, 'document_delivery_note_first_reference'); ?> | ||||
<?= $form->field($model, 'option_invoice_only_based_on_delivery_notes')->dropDownList([ | <?= $form->field($model, 'option_invoice_only_based_on_delivery_notes')->dropDownList([ | ||||
0 => 'Non', | 0 => 'Non', |
$this->query->orderBy($defaultOptions['orderby']); | $this->query->orderBy($defaultOptions['orderby']); | ||||
} | } | ||||
} | } | ||||
public function findAll() | |||||
{ | |||||
return $this->createQuery()->find(); | |||||
} | |||||
} | } |
[['name', 'id_user'], 'required'], | [['name', 'id_user'], 'required'], | ||||
[['date'], 'safe'], | [['date'], 'safe'], | ||||
[['comment', 'address', 'tax_calculation_method'], 'string'], | [['comment', 'address', 'tax_calculation_method'], 'string'], | ||||
[['id_user', 'id_producer'], 'integer'], | |||||
[['id_user', 'id_producer', 'reference_number'], 'integer'], | |||||
[['is_sent'], 'boolean'], | [['is_sent'], 'boolean'], | ||||
[['name', 'reference', 'status'], 'string', 'max' => 255], | [['name', 'reference', 'status'], 'string', 'max' => 255], | ||||
[['deliveryNotes', 'ordersOnCreate'], 'safe'] | [['deliveryNotes', 'ordersOnCreate'], 'safe'] |
$document->id_producer = $this->getProducerContextId(); | $document->id_producer = $this->getProducerContextId(); | ||||
} | } | ||||
public function generateReference(DocumentInterface $document): void | |||||
{ | |||||
$class = $this->documentSolver->getClass($document); | |||||
$classComplete = $this->documentSolver->getClass($document, true); | |||||
$classLower = strtolower($class); | |||||
if ($classLower == 'deliverynote') { | |||||
$classLower = 'delivery_note'; | |||||
} | |||||
$prefix = $this->producerSolver->getConfig('document_' . $classLower . '_prefix'); | |||||
$oneDocumentExist = $classComplete::searchOne(['status' => Document::STATUS_VALID], ['orderby' => 'reference DESC']); | |||||
if ($oneDocumentExist) { | |||||
$referenceDocument = $oneDocumentExist->reference; | |||||
$pattern = '#([A-Z]+)?([0-9]+)#'; | |||||
preg_match($pattern, $referenceDocument, $matches, PREG_OFFSET_CAPTURE); | |||||
$sizeNumReference = strlen($matches[2][0]); | |||||
$numReference = ((int)$matches[2][0]) + 1; | |||||
$numReference = str_pad($numReference, $sizeNumReference, '0', STR_PAD_LEFT); | |||||
$reference = $prefix . $numReference; | |||||
} else { | |||||
$firstReference = $this->producerSolver->getConfig('document_' . $classLower . '_first_reference'); | |||||
if (strlen($firstReference) > 0) { | |||||
$reference = $firstReference; | |||||
} else { | |||||
$reference = $prefix . '00001'; | |||||
} | |||||
} | |||||
$document->reference = $reference; | |||||
} | |||||
public function changeStatus(DocumentInterface $document, string $status): void | |||||
{ | |||||
$document->status = $status; | |||||
if ($status == Document::STATUS_VALID) { | |||||
$this->generateReference($document); | |||||
} | |||||
} | |||||
public function validateDocument(Document $document): void | |||||
{ | |||||
if($this->documentSolver->isStatusDraft($document)) { | |||||
$this->changeStatus($document, Document::STATUS_VALID); | |||||
$this->update($document); | |||||
} | |||||
} | |||||
public function initTaxCalculationMethod(DocumentInterface $document): void | public function initTaxCalculationMethod(DocumentInterface $document): void | ||||
{ | { | ||||
$producerTaxCalculationMethod = $this->producerSolver->getConfig('option_tax_calculation_method'); | $producerTaxCalculationMethod = $this->producerSolver->getConfig('option_tax_calculation_method'); |
namespace common\logic\Document\Document\Service; | namespace common\logic\Document\Document\Service; | ||||
use common\logic\AbstractManager; | use common\logic\AbstractManager; | ||||
use common\logic\Document\Document\Model\Document; | |||||
use common\logic\Document\Document\Model\DocumentInterface; | use common\logic\Document\Document\Model\DocumentInterface; | ||||
use common\logic\Producer\Producer\Service\ProducerSolver; | use common\logic\Producer\Producer\Service\ProducerSolver; | ||||
use kartik\mpdf\Pdf; | use kartik\mpdf\Pdf; | ||||
protected DocumentSolver $documentSolver; | protected DocumentSolver $documentSolver; | ||||
protected DocumentBuilder $documentBuilder; | protected DocumentBuilder $documentBuilder; | ||||
protected ProducerSolver $producerSolver; | protected ProducerSolver $producerSolver; | ||||
protected DocumentReferenceGenerator $documentReferenceGenerator; | |||||
public function loadDependencies(): void | public function loadDependencies(): void | ||||
{ | { | ||||
$this->documentSolver = $this->loadService(DocumentSolver::class); | $this->documentSolver = $this->loadService(DocumentSolver::class); | ||||
$this->documentBuilder = $this->loadService(DocumentBuilder::class); | $this->documentBuilder = $this->loadService(DocumentBuilder::class); | ||||
$this->producerSolver = $this->loadService(ProducerSolver::class); | $this->producerSolver = $this->loadService(ProducerSolver::class); | ||||
$this->documentReferenceGenerator = $this->loadService(DocumentReferenceGenerator::class); | |||||
} | |||||
public function changeStatus(DocumentInterface $document, string $status): void | |||||
{ | |||||
$document->status = $status; | |||||
if ($status == Document::STATUS_VALID) { | |||||
$this->documentReferenceGenerator->generateReference($document); | |||||
} | |||||
$this->documentBuilder->update($document); | |||||
} | |||||
public function validateDocument(Document $document): void | |||||
{ | |||||
if($this->documentSolver->isStatusDraft($document)) { | |||||
$this->changeStatus($document, Document::STATUS_VALID); | |||||
} | |||||
} | } | ||||
public function generatePdf(DocumentInterface $document, string $destination): ?string | public function generatePdf(DocumentInterface $document, string $destination): ?string |
<?php | |||||
namespace common\logic\Document\Document\Service; | |||||
use common\logic\AbstractGenerator; | |||||
use common\logic\Document\Document\Model\Document; | |||||
use common\logic\Document\Document\Model\DocumentInterface; | |||||
use common\logic\Producer\Producer\Service\ProducerSolver; | |||||
use common\logic\User\User\Model\User; | |||||
use common\logic\User\User\Service\UserSolver; | |||||
class DocumentReferenceGenerator extends AbstractGenerator | |||||
{ | |||||
protected DocumentSolver $documentSolver; | |||||
protected ProducerSolver $producerSolver; | |||||
protected UserSolver $userSolver; | |||||
public function loadDependencies(): void | |||||
{ | |||||
$this->documentSolver = $this->loadService(DocumentSolver::class); | |||||
$this->producerSolver = $this->loadService(ProducerSolver::class); | |||||
$this->userSolver = $this->loadService(UserSolver::class); | |||||
} | |||||
public function generateReference(DocumentInterface $document): void | |||||
{ | |||||
$class = $this->documentSolver->getClass($document); | |||||
$classComplete = $this->documentSolver->getClass($document, true); | |||||
$classLower = strtolower($class); | |||||
if ($classLower == 'deliverynote') { | |||||
$classLower = 'delivery_note'; | |||||
} | |||||
$prefix = $this->producerSolver->getConfig('document_' . $classLower . '_prefix'); | |||||
$oneDocumentExist = $classComplete::searchOne(['status' => Document::STATUS_VALID], ['orderby' => 'reference DESC']); | |||||
if ($oneDocumentExist) { | |||||
$referenceNumberDocumentExist = $oneDocumentExist->reference_number; | |||||
$referenceNumber = $referenceNumberDocumentExist + 1; | |||||
$reference = $this->buildReference($prefix, $referenceNumber, $document); | |||||
} else { | |||||
$firstReference = $this->producerSolver->getConfig('document_' . $classLower . '_first_reference'); | |||||
if (strlen($firstReference) > 0) { | |||||
$referenceNumber = (int) filter_var(str_replace('-', '', $firstReference), FILTER_SANITIZE_NUMBER_INT); | |||||
$reference = $firstReference; | |||||
} else { | |||||
$referenceNumber = 1; | |||||
$reference = $this->buildReference($prefix, 1, $document); | |||||
} | |||||
} | |||||
$document->reference = $reference; | |||||
$document->reference_number = $referenceNumber; | |||||
} | |||||
public function buildReference(string $prefix, int $referenceNumber, DocumentInterface $document) | |||||
{ | |||||
$prefix = $this->replaceKeywords($prefix, $document); | |||||
return $prefix . $this->strPadReference($referenceNumber); | |||||
} | |||||
public function replaceKeywords(string $prefix, DocumentInterface $document): string | |||||
{ | |||||
$prefix = str_replace('[ANNEE]', date('Y'), $prefix); | |||||
$prefix = str_replace('[CLIENT]', strtoupper($this->getMainName($document->user)), $prefix); | |||||
return $prefix; | |||||
} | |||||
public function strPadReference(int $referenceNumber): string | |||||
{ | |||||
return str_pad($referenceNumber, 6, '0', STR_PAD_LEFT); | |||||
} | |||||
private function getMainName(User $user): string | |||||
{ | |||||
if($this->userSolver->isTypeLegalPerson($user) && strlen($user->name_legal_person)) { | |||||
return $user->name_legal_person; | |||||
} | |||||
else { | |||||
return $user->lastname; | |||||
} | |||||
} | |||||
} |
use common\logic\Document\DeliveryNote\Model\DeliveryNote; | use common\logic\Document\DeliveryNote\Model\DeliveryNote; | ||||
use common\logic\Document\DeliveryNote\Repository\DeliveryNoteRepository; | use common\logic\Document\DeliveryNote\Repository\DeliveryNoteRepository; | ||||
use common\logic\Document\DeliveryNote\Service\DeliveryNoteBuilder; | use common\logic\Document\DeliveryNote\Service\DeliveryNoteBuilder; | ||||
use common\logic\Document\Document\Service\DocumentManager; | |||||
use common\logic\Document\Document\Service\DocumentSolver; | use common\logic\Document\Document\Service\DocumentSolver; | ||||
use common\logic\Order\Order\Model\Order; | use common\logic\Order\Order\Model\Order; | ||||
use common\logic\Order\Order\Repository\OrderRepository; | use common\logic\Order\Order\Repository\OrderRepository; | ||||
protected DocumentSolver $documentSolver; | protected DocumentSolver $documentSolver; | ||||
protected DeliveryNoteRepository $deliveryNoteRepository; | protected DeliveryNoteRepository $deliveryNoteRepository; | ||||
protected DeliveryNoteBuilder $deliveryNoteBuilder; | protected DeliveryNoteBuilder $deliveryNoteBuilder; | ||||
protected DocumentManager $documentManager; | |||||
public function loadDependencies(): void | public function loadDependencies(): void | ||||
{ | { | ||||
$this->documentSolver = $this->loadService(DocumentSolver::class); | $this->documentSolver = $this->loadService(DocumentSolver::class); | ||||
$this->deliveryNoteRepository = $this->loadService(DeliveryNoteRepository::class); | $this->deliveryNoteRepository = $this->loadService(DeliveryNoteRepository::class); | ||||
$this->deliveryNoteBuilder = $this->loadService(DeliveryNoteBuilder::class); | $this->deliveryNoteBuilder = $this->loadService(DeliveryNoteBuilder::class); | ||||
$this->documentManager = $this->loadService(DocumentManager::class); | |||||
} | } | ||||
public function generateDeliveryNoteForPointSale(array $idOrders = []): ?DeliveryNote | public function generateDeliveryNoteForPointSale(array $idOrders = []): ?DeliveryNote | ||||
if ($this->orderSolver->isOrderFromProducer($order)) { | if ($this->orderSolver->isOrderFromProducer($order)) { | ||||
$deliveryNote = $this->deliveryNoteRepository->findOneDeliveryNoteById((int) $order->id_delivery_note); | $deliveryNote = $this->deliveryNoteRepository->findOneDeliveryNoteById((int) $order->id_delivery_note); | ||||
if($deliveryNote) { | if($deliveryNote) { | ||||
$this->deliveryNoteBuilder->validateDocument($deliveryNote); | |||||
$this->documentManager->validateDocument($deliveryNote); | |||||
$oneDeliveryNoteValidated = true; | $oneDeliveryNoteValidated = true; | ||||
} | } | ||||
} | } |
} | } | ||||
} | } | ||||
], | ], | ||||
[ | |||||
/*[ | |||||
['document_quotation_prefix', 'document_invoice_prefix', 'document_delivery_note_prefix'], | ['document_quotation_prefix', 'document_invoice_prefix', 'document_delivery_note_prefix'], | ||||
function ($attribute, $params) { | function ($attribute, $params) { | ||||
if (!ctype_upper($this->$attribute)) { | if (!ctype_upper($this->$attribute)) { | ||||
$this->addError($attribute, 'Ne doit contenir que des majuscules'); | $this->addError($attribute, 'Ne doit contenir que des majuscules'); | ||||
} | } | ||||
} | } | ||||
], | |||||
],*/ | |||||
[ | [ | ||||
[ | [ | ||||
'description', | 'description', |
return in_array($type, User::$types); | return in_array($type, User::$types); | ||||
} | } | ||||
public function isTypeLegalPerson(User $user): bool | |||||
{ | |||||
return $user->type == User::TYPE_LEGAL_PERSON; | |||||
} | |||||
public function isTypeIndividual(User $user): bool | |||||
{ | |||||
return $user->type == User::TYPE_INDIVIDUAL; | |||||
} | |||||
public function isTypeGuest(User $user): bool | |||||
{ | |||||
return $user->type == User::TYPE_GUEST; | |||||
} | |||||
public function getUsernameFromArray(array $modelArray, $withType = false): string | public function getUsernameFromArray(array $modelArray, $withType = false): string | ||||
{ | { | ||||
$username = ''; | $username = ''; | ||||
public function getUsername(User $user, $withType = false): string | public function getUsername(User $user, $withType = false): string | ||||
{ | { | ||||
$username = ''; | |||||
if (isset($user->name_legal_person) && strlen($user->name_legal_person)) { | if (isset($user->name_legal_person) && strlen($user->name_legal_person)) { | ||||
$username = $user->name_legal_person; | $username = $user->name_legal_person; | ||||
} else { | } else { |
<?php | |||||
use common\logic\Document\DeliveryNote\Module\DeliveryNoteModule; | |||||
use common\logic\Document\Invoice\Module\InvoiceModule; | |||||
use common\logic\Document\Quotation\Module\QuotationModule; | |||||
use yii\db\Migration; | |||||
use yii\db\Schema; | |||||
/** | |||||
* Class m231018_074957_add_column_document_reference_number | |||||
*/ | |||||
class m231018_074957_add_column_document_reference_number extends Migration | |||||
{ | |||||
/** | |||||
* {@inheritdoc} | |||||
*/ | |||||
public function safeUp() | |||||
{ | |||||
$this->addColumn('quotation', 'reference_number', Schema::TYPE_INTEGER); | |||||
$this->addColumn('delivery_note', 'reference_number', Schema::TYPE_INTEGER); | |||||
$this->addColumn('invoice', 'reference_number', Schema::TYPE_INTEGER); | |||||
$quotationArray = QuotationModule::getInstance()->getRepository()->findAll(); | |||||
$deliveryNoteArray = DeliveryNoteModule::getInstance()->getRepository()->findAll(); | |||||
$invoiceArray = InvoiceModule::getInstance()->getRepository()->findAll(); | |||||
$this->updateReferenceNumber($quotationArray); | |||||
$this->updateReferenceNumber($deliveryNoteArray); | |||||
$this->updateReferenceNumber($invoiceArray); | |||||
} | |||||
/** | |||||
* {@inheritdoc} | |||||
*/ | |||||
public function safeDown() | |||||
{ | |||||
$this->dropColumn('quotation', 'reference_number'); | |||||
$this->dropColumn('delivery_note', 'reference_number'); | |||||
$this->dropColumn('invoice', 'reference_number'); | |||||
} | |||||
public function updateReferenceNumber(array $documentArray) | |||||
{ | |||||
foreach($documentArray as $document) { | |||||
$document->reference_number = $this->getReferenceNumber($document); | |||||
$document->save(); | |||||
} | |||||
} | |||||
public function getReferenceNumber($document) | |||||
{ | |||||
$referenceDocument = $document->reference; | |||||
$pattern = '#([A-Z]+)?([0-9]+)#'; | |||||
preg_match($pattern, $referenceDocument, $matches, PREG_OFFSET_CAPTURE); | |||||
if(isset($matches[2])) { | |||||
return (int) $matches[2][0]; | |||||
} | |||||
return null; | |||||
} | |||||
} |