Browse Source

Documents : gestion référence

feature/export_comptable
Guillaume 4 years ago
parent
commit
d91bf98257
4 changed files with 135 additions and 27 deletions
  1. +20
    -1
      ShopBundle/Model/Document.php
  2. +6
    -6
      ShopBundle/Repository/OrderShopRepository.php
  3. +94
    -0
      ShopBundle/Services/DocumentUtils.php
  4. +15
    -20
      ShopBundle/Services/OrderUtils.php

+ 20
- 1
ShopBundle/Model/Document.php View File

@@ -5,17 +5,24 @@ namespace Lc\ShopBundle\Model;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Lc\ShopBundle\Context\FilterMerchantInterface;

/**
* @ORM\MappedSuperclass()
*/
abstract class Document extends AbstractDocumentEntity
abstract class Document extends AbstractDocumentEntity implements FilterMerchantInterface
{
const TYPE_INVOICE = 'invoice' ;
const TYPE_QUOTATION = 'quotation' ;
const TYPE_PURCHASE_ORDER = 'purchase-order' ;
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)
*/
@@ -73,6 +80,18 @@ abstract class Document extends AbstractDocumentEntity
$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()
{
if($this->getType() == self::TYPE_INVOICE) {

+ 6
- 6
ShopBundle/Repository/OrderShopRepository.php View File

@@ -30,8 +30,7 @@ class OrderShopRepository extends BaseRepository implements DefaultRepositoryInt
$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() ;

@@ -55,7 +54,7 @@ class OrderShopRepository extends BaseRepository implements DefaultRepositoryInt
}

if(!isset($params['isCart'])) {
$query = $this->filterOrderCart($query) ;
$this->filterOrderCart($query) ;
}

if(isset($params['user'])) {
@@ -82,9 +81,10 @@ class OrderShopRepository extends BaseRepository implements DefaultRepositoryInt

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()

+ 94
- 0
ShopBundle/Services/DocumentUtils.php View File

@@ -0,0 +1,94 @@
<?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 ;
}
}

+ 15
- 20
ShopBundle/Services/OrderUtils.php View File

@@ -29,9 +29,11 @@ class OrderUtils
protected $orderShopRepo;
protected $priceUtils;
protected $productFamilyUtils;
protected $documentUtils ;

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->security = $security;
@@ -40,6 +42,7 @@ class OrderUtils
$this->orderShopRepo = $this->em->getRepository($this->em->getClassMetadata(OrderShopInterface::class)->getName());
$this->priceUtils = $priceUtils;
$this->productFamilyUtils = $productFamilyUtils;
$this->documentUtils = $documentUtils ;
}


@@ -370,27 +373,19 @@ class OrderUtils

public function createDocumentInvoice(OrderShopInterface $orderShop)
{
$documentClass = $this->em->getClassMetadata(DocumentInterface::class)->getName();
$invoice = new $documentClass ;

$merchantAddress = $orderShop->getMerchant()->getAddress() ;
$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 ;
}
}

Loading…
Cancel
Save