97 lines
3.8KB

  1. <?php
  2. namespace Lc\ShopBundle\Services ;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Lc\ShopBundle\Context\DocumentInterface;
  5. use Lc\ShopBundle\Context\MerchantUtilsInterface;
  6. use Lc\ShopBundle\Model\Document ;
  7. class DocumentUtils
  8. {
  9. protected $em ;
  10. protected $merchantUtils ;
  11. protected $documentRepository ;
  12. public function __construct(EntityManagerInterface $em, MerchantUtilsInterface $merchantUtils)
  13. {
  14. $this->em = $em ;
  15. $this->documentRepository = $this->em->getRepository($this->em->getClassMetadata(DocumentInterface::class)->getName()) ;
  16. $this->merchantUtils = $merchantUtils ;
  17. }
  18. public function generateReference($documentType)
  19. {
  20. $prefix = '';
  21. if($documentType == Document::TYPE_DELIVERY_NOTE) {
  22. $prefix = 'BL';
  23. }
  24. elseif($documentType == Document::TYPE_PURCHASE_ORDER) {
  25. $prefix = 'BC';
  26. }
  27. elseif($documentType == Document::TYPE_INVOICE) {
  28. $prefix = 'FA';
  29. }
  30. elseif($documentType == Document::TYPE_QUOTATION) {
  31. $prefix = 'DE';
  32. }
  33. $oneDocumentExist = $this->documentRepository->findOneBy([
  34. 'status' => 1,
  35. 'type' => $documentType,
  36. 'merchant' => $this->merchantUtils->getMerchantCurrent()
  37. ], [
  38. 'reference' => 'DESC'
  39. ]);
  40. if ($oneDocumentExist) {
  41. $reference = $oneDocumentExist->getReference();
  42. $pattern = '#([A-Z]+)?([0-9]+)#';
  43. preg_match($pattern, $reference, $matches, PREG_OFFSET_CAPTURE);
  44. $sizeNumReference = strlen($matches[2][0]);
  45. $numReference = ((int)$matches[2][0]) + 1;
  46. $numReference = str_pad($numReference, $sizeNumReference, '0', STR_PAD_LEFT);
  47. return $prefix . $numReference;
  48. }
  49. else {
  50. return $prefix . '00001';
  51. }
  52. }
  53. public function createDocument($params = [])
  54. {
  55. $documentClass = $this->em->getClassMetadata(DocumentInterface::class)->getName();
  56. $document = new $documentClass ;
  57. if(isset($params['merchant'])) {
  58. $document->setMerchant($params['merchant']) ;
  59. }
  60. elseif(isset($params['order_shops'])) {
  61. $document->setMerchant($params['order_shops'][0]->getMerchant()) ;
  62. }
  63. if(isset($params['order_shops'])) {
  64. foreach ($params['order_shops'] as $orderShop) {
  65. $document->addOrderShop($orderShop);
  66. }
  67. }
  68. $document->setType($params['type']) ;
  69. $document->setTitle($params['title']) ;
  70. $document->setStatus((isset($params['status'])) ? $params['status'] : 1) ;
  71. $document->setReference($this->generateReference($params['type'])) ;
  72. $document->setMerchantAddress($params['merchant_address']) ;
  73. $document->setBuyerAddress($params['buyer_address']) ;
  74. $document->setMerchantAddressText($params['merchant_address']->getSummary()) ;
  75. $document->setBuyerAddressText($params['buyer_address']->getSummary()) ;
  76. $document->setCreatedBy($params['created_by']) ;
  77. $document->setUpdatedBy($params['created_by']) ;
  78. $this->em->persist($document);
  79. $this->em->flush() ;
  80. return $document ;
  81. }
  82. }