Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DocumentUtils.php 3.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. else {
  61. $document->setMerchant($params['order_shops'][0]->getMerchant()) ;
  62. }
  63. foreach($params['order_shops'] as $orderShop) {
  64. $document->addOrderShop($orderShop) ;
  65. }
  66. $document->setType($params['type']) ;
  67. $document->setTitle($params['title']) ;
  68. $document->setStatus((isset($params['status'])) ? $params['status'] : 1) ;
  69. $document->setReference($this->generateReference($params['type'])) ;
  70. $document->setMerchantAddress($params['merchant_address']) ;
  71. $document->setBuyerAddress($params['buyer_address']) ;
  72. $document->setMerchantAddressText($params['merchant_address']->getSummary()) ;
  73. $document->setBuyerAddressText($params['buyer_address']->getSummary()) ;
  74. $document->setCreatedBy($params['created_by']) ;
  75. $document->setUpdatedBy($params['created_by']) ;
  76. $this->em->persist($document);
  77. $this->em->flush() ;
  78. return $document ;
  79. }
  80. }