@@ -546,7 +546,7 @@ class DocumentController extends BackendController | |||
$document = $this->findModel($id); | |||
if ($document) { | |||
$documentModule->validateDocument($document); | |||
$documentModule->getManager()->validateDocument($document); | |||
// @TODO : gérer via un événement | |||
$documentModule->generatePdf($document, Pdf::DEST_FILE); |
@@ -453,12 +453,13 @@ $this->addBreadcrumb($this->getTitle()); | |||
0 => 'Non', | |||
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_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_delivery_note_prefix'); ?> | |||
<?= $form->field($model, 'document_delivery_note_prefix')->hint($hintKeywordsPrefix);; ?> | |||
<?= $form->field($model, 'document_delivery_note_first_reference'); ?> | |||
<?= $form->field($model, 'option_invoice_only_based_on_delivery_notes')->dropDownList([ | |||
0 => 'Non', |
@@ -79,4 +79,9 @@ abstract class AbstractRepository extends AbstractService implements RepositoryI | |||
$this->query->orderBy($defaultOptions['orderby']); | |||
} | |||
} | |||
public function findAll() | |||
{ | |||
return $this->createQuery()->find(); | |||
} | |||
} |
@@ -67,7 +67,7 @@ class Document extends ActiveRecordCommon implements DocumentInterface | |||
[['name', 'id_user'], 'required'], | |||
[['date'], 'safe'], | |||
[['comment', 'address', 'tax_calculation_method'], 'string'], | |||
[['id_user', 'id_producer'], 'integer'], | |||
[['id_user', 'id_producer', 'reference_number'], 'integer'], | |||
[['is_sent'], 'boolean'], | |||
[['name', 'reference', 'status'], 'string', 'max' => 255], | |||
[['deliveryNotes', 'ordersOnCreate'], 'safe'] |
@@ -26,57 +26,6 @@ class DocumentBuilder extends AbstractBuilder | |||
$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 | |||
{ | |||
$producerTaxCalculationMethod = $this->producerSolver->getConfig('option_tax_calculation_method'); |
@@ -3,6 +3,7 @@ | |||
namespace common\logic\Document\Document\Service; | |||
use common\logic\AbstractManager; | |||
use common\logic\Document\Document\Model\Document; | |||
use common\logic\Document\Document\Model\DocumentInterface; | |||
use common\logic\Producer\Producer\Service\ProducerSolver; | |||
use kartik\mpdf\Pdf; | |||
@@ -14,12 +15,32 @@ class DocumentManager extends AbstractManager | |||
protected DocumentSolver $documentSolver; | |||
protected DocumentBuilder $documentBuilder; | |||
protected ProducerSolver $producerSolver; | |||
protected DocumentReferenceGenerator $documentReferenceGenerator; | |||
public function loadDependencies(): void | |||
{ | |||
$this->documentSolver = $this->loadService(DocumentSolver::class); | |||
$this->documentBuilder = $this->loadService(DocumentBuilder::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 |
@@ -0,0 +1,85 @@ | |||
<?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; | |||
} | |||
} | |||
} |
@@ -6,6 +6,7 @@ use common\logic\AbstractManager; | |||
use common\logic\Document\DeliveryNote\Model\DeliveryNote; | |||
use common\logic\Document\DeliveryNote\Repository\DeliveryNoteRepository; | |||
use common\logic\Document\DeliveryNote\Service\DeliveryNoteBuilder; | |||
use common\logic\Document\Document\Service\DocumentManager; | |||
use common\logic\Document\Document\Service\DocumentSolver; | |||
use common\logic\Order\Order\Model\Order; | |||
use common\logic\Order\Order\Repository\OrderRepository; | |||
@@ -18,6 +19,7 @@ class OrderDocumentManager extends AbstractManager | |||
protected DocumentSolver $documentSolver; | |||
protected DeliveryNoteRepository $deliveryNoteRepository; | |||
protected DeliveryNoteBuilder $deliveryNoteBuilder; | |||
protected DocumentManager $documentManager; | |||
public function loadDependencies(): void | |||
{ | |||
@@ -26,6 +28,7 @@ class OrderDocumentManager extends AbstractManager | |||
$this->documentSolver = $this->loadService(DocumentSolver::class); | |||
$this->deliveryNoteRepository = $this->loadService(DeliveryNoteRepository::class); | |||
$this->deliveryNoteBuilder = $this->loadService(DeliveryNoteBuilder::class); | |||
$this->documentManager = $this->loadService(DocumentManager::class); | |||
} | |||
public function generateDeliveryNoteForPointSale(array $idOrders = []): ?DeliveryNote | |||
@@ -84,7 +87,7 @@ class OrderDocumentManager extends AbstractManager | |||
if ($this->orderSolver->isOrderFromProducer($order)) { | |||
$deliveryNote = $this->deliveryNoteRepository->findOneDeliveryNoteById((int) $order->id_delivery_note); | |||
if($deliveryNote) { | |||
$this->deliveryNoteBuilder->validateDocument($deliveryNote); | |||
$this->documentManager->validateDocument($deliveryNote); | |||
$oneDeliveryNoteValidated = true; | |||
} | |||
} |
@@ -194,14 +194,14 @@ class Producer extends ActiveRecordCommon | |||
} | |||
} | |||
], | |||
[ | |||
/*[ | |||
['document_quotation_prefix', 'document_invoice_prefix', 'document_delivery_note_prefix'], | |||
function ($attribute, $params) { | |||
if (!ctype_upper($this->$attribute)) { | |||
$this->addError($attribute, 'Ne doit contenir que des majuscules'); | |||
} | |||
} | |||
], | |||
],*/ | |||
[ | |||
[ | |||
'description', |
@@ -35,6 +35,21 @@ class UserSolver extends AbstractService implements SolverInterface | |||
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 | |||
{ | |||
$username = ''; | |||
@@ -72,7 +87,6 @@ class UserSolver extends AbstractService implements SolverInterface | |||
public function getUsername(User $user, $withType = false): string | |||
{ | |||
$username = ''; | |||
if (isset($user->name_legal_person) && strlen($user->name_legal_person)) { | |||
$username = $user->name_legal_person; | |||
} else { |
@@ -0,0 +1,61 @@ | |||
<?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; | |||
} | |||
} |