Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

DocumentReferenceGenerator.php 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace Lc\CaracoleBundle\Generator\Reference;
  3. use Lc\CaracoleBundle\Definition\SectionSettingDefinition;
  4. use Lc\CaracoleBundle\Model\File\DocumentModel;
  5. use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
  6. use Lc\CaracoleBundle\Model\Order\OrderShopInterface;
  7. use Lc\CaracoleBundle\Model\Section\SectionModel;
  8. use Lc\CaracoleBundle\Repository\File\DocumentStore;
  9. class DocumentReferenceGenerator
  10. {
  11. protected DocumentStore $documentStore;
  12. public function __construct(DocumentStore $documentStore)
  13. {
  14. $this->documentStore = $documentStore;
  15. }
  16. public function buildReference(MerchantInterface $merchant, string $documentType)
  17. {
  18. $prefix = '';
  19. if ($documentType == DocumentModel::TYPE_DELIVERY_NOTE) {
  20. $prefix = 'BL';
  21. } elseif ($documentType == DocumentModel::TYPE_PURCHASE_ORDER) {
  22. $prefix = 'BC';
  23. } elseif ($documentType == DocumentModel::TYPE_INVOICE) {
  24. $prefix = 'FA';
  25. } elseif ($documentType == DocumentModel::TYPE_QUOTATION) {
  26. $prefix = 'DE';
  27. }
  28. $oneDocumentExist = $this->documentStore
  29. ->setMerchant($merchant)
  30. ->getOneLatestByType($documentType) ;
  31. if ($oneDocumentExist) {
  32. $reference = $oneDocumentExist->getReference();
  33. $pattern = '#([A-Z]+)?([0-9]+)#';
  34. preg_match($pattern, $reference, $matches, PREG_OFFSET_CAPTURE);
  35. $sizeNumReference = strlen($matches[2][0]);
  36. $numReference = ((int)$matches[2][0]) + 1;
  37. $numReference = str_pad($numReference, $sizeNumReference, '0', STR_PAD_LEFT);
  38. return $prefix . $numReference;
  39. } else {
  40. return $prefix . '00001';
  41. }
  42. }
  43. }