|
- <?php
-
- namespace Lc\CaracoleBundle\Generator;
-
- use Lc\CaracoleBundle\Definition\SectionSettingDefinition;
- use Lc\CaracoleBundle\Model\File\DocumentModel;
- use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
- use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
- use Lc\CaracoleBundle\Model\Section\SectionModel;
- use Lc\CaracoleBundle\Repository\File\DocumentStore;
-
- class DocumentReferenceGenerator
- {
- protected DocumentStore $documentStore;
-
- public function __construct(DocumentStore $documentStore)
- {
- $this->documentStore = $documentStore;
- }
-
- public function buildReference(MerchantInterface $merchant, string $documentType, OrderShopInterface $orderShop = null)
- {
- $prefix = '';
- if ($documentType == DocumentModel::TYPE_DELIVERY_NOTE) {
- $prefix = 'BL';
- } elseif ($documentType == DocumentModel::TYPE_PURCHASE_ORDER) {
- $prefix = 'BC';
- } elseif ($documentType == DocumentModel::TYPE_INVOICE) {
- $prefix = 'FA';
- } elseif ($documentType == DocumentModel::TYPE_QUOTATION) {
- $prefix = 'DE';
- }
-
- $oneDocumentExist = $this->documentStore
- ->setMerchant($merchant)
- ->getOneLatestByType($documentType) ;
-
- if ($oneDocumentExist) {
- $reference = $oneDocumentExist->getReference();
- $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 {
- return $prefix . '00001';
- }
- }
-
- }
|