You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 line
1.8KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Generator;
  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, OrderShopInterface $orderShop = null)
  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. }