use Doctrine\Common\Collections\ArrayCollection; | use Doctrine\Common\Collections\ArrayCollection; | ||||
use Doctrine\Common\Collections\Collection; | use Doctrine\Common\Collections\Collection; | ||||
use Doctrine\ORM\Mapping as ORM; | use Doctrine\ORM\Mapping as ORM; | ||||
use Lc\ShopBundle\Context\FilterMerchantInterface; | |||||
/** | /** | ||||
* @ORM\MappedSuperclass() | * @ORM\MappedSuperclass() | ||||
*/ | */ | ||||
abstract class Document extends AbstractDocumentEntity | |||||
abstract class Document extends AbstractDocumentEntity implements FilterMerchantInterface | |||||
{ | { | ||||
const TYPE_INVOICE = 'invoice' ; | const TYPE_INVOICE = 'invoice' ; | ||||
const TYPE_QUOTATION = 'quotation' ; | const TYPE_QUOTATION = 'quotation' ; | ||||
const TYPE_PURCHASE_ORDER = 'purchase-order' ; | const TYPE_PURCHASE_ORDER = 'purchase-order' ; | ||||
const TYPE_DELIVERY_NOTE = 'delivery-note' ; | const TYPE_DELIVERY_NOTE = 'delivery-note' ; | ||||
/** | |||||
* @ORM\ManyToOne(targetEntity="Lc\ShopBundle\Context\MerchantInterface") | |||||
* @ORM\JoinColumn(nullable=false) | |||||
*/ | |||||
protected $merchant; | |||||
/** | /** | ||||
* @ORM\Column(type="string", length=64) | * @ORM\Column(type="string", length=64) | ||||
*/ | */ | ||||
$this->orderShops = new ArrayCollection(); | $this->orderShops = new ArrayCollection(); | ||||
} | } | ||||
public function getMerchant(): ?Merchant | |||||
{ | |||||
return $this->merchant; | |||||
} | |||||
public function setMerchant(?Merchant $merchant): self | |||||
{ | |||||
$this->merchant = $merchant; | |||||
return $this; | |||||
} | |||||
public function getLabel() | public function getLabel() | ||||
{ | { | ||||
if($this->getType() == self::TYPE_INVOICE) { | if($this->getType() == self::TYPE_INVOICE) { |
$query->andWhere('e.visitor = :visitor')->setParameter('visitor', $params['visitor']) ; | $query->andWhere('e.visitor = :visitor')->setParameter('visitor', $params['visitor']) ; | ||||
} | } | ||||
$query->leftJoin('e.orderStatusHistories', 'orderStatusHistories') | |||||
->andWhere('SIZE(e.orderStatusHistories) = 0') ; | |||||
$this->filterOrderCart($query, true) ; | |||||
$results = $query->getQuery()->getResult() ; | $results = $query->getQuery()->getResult() ; | ||||
} | } | ||||
if(!isset($params['isCart'])) { | if(!isset($params['isCart'])) { | ||||
$query = $this->filterOrderCart($query) ; | |||||
$this->filterOrderCart($query) ; | |||||
} | } | ||||
if(isset($params['user'])) { | if(isset($params['user'])) { | ||||
public function filterOrderCart($query, $isCart = false) | public function filterOrderCart($query, $isCart = false) | ||||
{ | { | ||||
$operator = $isCart ? '=' : '>' ; | |||||
return $query->leftJoin('e.orderStatusHistories', 'orderStatusHistories') | |||||
->andWhere('SIZE(e.orderStatusHistories) '.$operator.' 0') ; | |||||
$operator = $isCart ? '=' : '!=' ; | |||||
return $query->leftJoin('e.orderStatus', 'orderStatus') | |||||
->andWhere('orderStatus.alias '.$operator.' :orderStatus') | |||||
->setParameter('orderStatus', 'cart'); | |||||
} | } | ||||
//getValidOrders() | //getValidOrders() |
<?php | |||||
namespace Lc\ShopBundle\Services ; | |||||
use Doctrine\ORM\EntityManagerInterface; | |||||
use Lc\ShopBundle\Context\DocumentInterface; | |||||
use Lc\ShopBundle\Context\MerchantUtilsInterface; | |||||
use Lc\ShopBundle\Model\Document ; | |||||
class DocumentUtils | |||||
{ | |||||
protected $em ; | |||||
protected $merchantUtils ; | |||||
protected $documentRepository ; | |||||
public function __construct(EntityManagerInterface $em, MerchantUtilsInterface $merchantUtils) | |||||
{ | |||||
$this->em = $em ; | |||||
$this->documentRepository = $this->em->getRepository($this->em->getClassMetadata(DocumentInterface::class)->getName()) ; | |||||
$this->merchantUtils = $merchantUtils ; | |||||
} | |||||
public function generateReference($documentType) | |||||
{ | |||||
$prefix = ''; | |||||
if($documentType == Document::TYPE_DELIVERY_NOTE) { | |||||
$prefix = 'BL'; | |||||
} | |||||
elseif($documentType == Document::TYPE_PURCHASE_ORDER) { | |||||
$prefix = 'BC'; | |||||
} | |||||
elseif($documentType == Document::TYPE_INVOICE) { | |||||
$prefix = 'FA'; | |||||
} | |||||
elseif($documentType == Document::TYPE_QUOTATION) { | |||||
$prefix = 'DE'; | |||||
} | |||||
$oneDocumentExist = $this->documentRepository->findOneBy([ | |||||
'status' => 1, | |||||
'type' => $documentType, | |||||
'merchant' => $this->merchantUtils->getMerchantCurrent() | |||||
], [ | |||||
'reference' => 'DESC' | |||||
]); | |||||
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'; | |||||
} | |||||
} | |||||
public function createDocument($params = []) | |||||
{ | |||||
$documentClass = $this->em->getClassMetadata(DocumentInterface::class)->getName(); | |||||
$document = new $documentClass ; | |||||
if(isset($params['merchant'])) { | |||||
$document->setMerchant($params['merchant']) ; | |||||
} | |||||
else { | |||||
$document->setMerchant($params['order_shops'][0]->getMerchant()) ; | |||||
} | |||||
foreach($params['order_shops'] as $orderShop) { | |||||
$document->addOrderShop($orderShop) ; | |||||
} | |||||
$document->setType($params['type']) ; | |||||
$document->setTitle($params['title']) ; | |||||
$document->setStatus((isset($params['status'])) ? $params['status'] : 1) ; | |||||
$document->setReference($this->generateReference($params['type'])) ; | |||||
$document->setMerchantAddress($params['merchant_address']) ; | |||||
$document->setBuyerAddress($params['buyer_address']) ; | |||||
$document->setMerchantAddressText($params['merchant_address']->getSummary()) ; | |||||
$document->setBuyerAddressText($params['buyer_address']->getSummary()) ; | |||||
$document->setCreatedBy($params['created_by']) ; | |||||
$document->setUpdatedBy($params['created_by']) ; | |||||
$this->em->persist($document); | |||||
$this->em->flush() ; | |||||
return $document ; | |||||
} | |||||
} |
protected $orderShopRepo; | protected $orderShopRepo; | ||||
protected $priceUtils; | protected $priceUtils; | ||||
protected $productFamilyUtils; | protected $productFamilyUtils; | ||||
protected $documentUtils ; | |||||
public function __construct(EntityManagerInterface $em, Security $security, UserUtils $userUtils, | public function __construct(EntityManagerInterface $em, Security $security, UserUtils $userUtils, | ||||
MerchantUtilsInterface $merchantUtils, PriceUtils $priceUtils, ProductFamilyUtilsInterface $productFamilyUtils) | |||||
MerchantUtilsInterface $merchantUtils, PriceUtils $priceUtils, ProductFamilyUtilsInterface $productFamilyUtils, | |||||
DocumentUtils $documentUtils) | |||||
{ | { | ||||
$this->em = $em; | $this->em = $em; | ||||
$this->security = $security; | $this->security = $security; | ||||
$this->orderShopRepo = $this->em->getRepository($this->em->getClassMetadata(OrderShopInterface::class)->getName()); | $this->orderShopRepo = $this->em->getRepository($this->em->getClassMetadata(OrderShopInterface::class)->getName()); | ||||
$this->priceUtils = $priceUtils; | $this->priceUtils = $priceUtils; | ||||
$this->productFamilyUtils = $productFamilyUtils; | $this->productFamilyUtils = $productFamilyUtils; | ||||
$this->documentUtils = $documentUtils ; | |||||
} | } | ||||
public function createDocumentInvoice(OrderShopInterface $orderShop) | public function createDocumentInvoice(OrderShopInterface $orderShop) | ||||
{ | { | ||||
$documentClass = $this->em->getClassMetadata(DocumentInterface::class)->getName(); | |||||
$invoice = new $documentClass ; | |||||
$merchantAddress = $orderShop->getMerchant()->getAddress() ; | $merchantAddress = $orderShop->getMerchant()->getAddress() ; | ||||
$buyerAddress = $orderShop->getBillingAddress() ; | $buyerAddress = $orderShop->getBillingAddress() ; | ||||
$invoice->addOrderShop($orderShop) ; | |||||
$invoice->setType(Document::TYPE_INVOICE) ; | |||||
$invoice->setTitle('Test facture') ; | |||||
$invoice->setStatus(1) ; | |||||
$invoice->setReference('0001') ; | |||||
$invoice->setMerchantAddress($merchantAddress) ; | |||||
$invoice->setBuyerAddress($buyerAddress) ; | |||||
$invoice->setMerchantAddressText($merchantAddress->getSummary()) ; | |||||
$invoice->setBuyerAddressText($buyerAddress->getSummary()) ; | |||||
$invoice->setCreatedBy($orderShop->getUser()) ; | |||||
$invoice->setUpdatedBy($orderShop->getUser()) ; | |||||
$this->em->persist($invoice); | |||||
$this->em->flush() ; | |||||
return $invoice ; | |||||
$document = $this->documentUtils->createDocument([ | |||||
'type' => Document::TYPE_INVOICE, | |||||
'title' => '', | |||||
'status' => 1, | |||||
'order_shops' => [$orderShop], | |||||
'merchant_address' => $merchantAddress, | |||||
'buyer_address' => $buyerAddress, | |||||
'created_by' => $orderShop->getUser() | |||||
]) ; | |||||
return $document ; | |||||
} | } | ||||
} | } |