@@ -0,0 +1,34 @@ | |||
<?php | |||
namespace Lc\CaracoleBundle\Builder\File; | |||
use Lc\CaracoleBundle\Model\File\DocumentInterface; | |||
use Lc\CaracoleBundle\Model\Order\OrderShopInterface; | |||
use Lc\CaracoleBundle\Resolver\Reference\DocumentReferenceResolver; | |||
class DocumentBuilder | |||
{ | |||
protected DocumentReferenceResolver $documentReferenceResolver; | |||
public function __construct(DocumentReferenceResolver $documentReferenceResolver) | |||
{ | |||
$this->documentReferenceResolver = $documentReferenceResolver; | |||
} | |||
public function initFromOrderShop(DocumentInterface $document, OrderShopInterface $orderShop) | |||
{ | |||
$merchantAddress = $orderShop->getMerchant()->getAddress(); | |||
$buyerAddress = $orderShop->getInvoiceAddress(); | |||
$document->setReference($this->documentReferenceResolver->buildReference($orderShop->getMerchant())) ; | |||
$document->setMerchantAddress($merchantAddress); | |||
$document->setBuyerAddress($buyerAddress); | |||
$document->setMerchantAddressText($merchantAddress->getSummary()); | |||
$document->setBuyerAddressText($buyerAddress->getSummary()); | |||
$document->addOrderShop($orderShop); | |||
$document->setCreatedBy($orderShop->getUser()); | |||
return $document; | |||
} | |||
} |
@@ -2,21 +2,22 @@ | |||
namespace Lc\CaracoleBundle\Builder\Order; | |||
use App\Entity\Order\OrderProductReductionCatalog; | |||
use Doctrine\ORM\EntityManagerInterface; | |||
use Lc\CaracoleBundle\Builder\File\DocumentBuilder; | |||
use Lc\CaracoleBundle\Event\Order\OrderShopChangeStatusEvent; | |||
use Lc\CaracoleBundle\Factory\File\DocumentFactory; | |||
use Lc\CaracoleBundle\Factory\Order\OrderPaymentFactory; | |||
use Lc\CaracoleBundle\Factory\Order\OrderProductReductionCatalogFactory; | |||
use Lc\CaracoleBundle\Factory\Order\OrderShopFactory; | |||
use Lc\CaracoleBundle\Factory\Order\OrderStatusHistoryFactory; | |||
use Lc\CaracoleBundle\Model\Merchant\MerchantInterface; | |||
use Lc\CaracoleBundle\Model\File\DocumentModel; | |||
use Lc\CaracoleBundle\Model\Order\OrderProductInterface; | |||
use Lc\CaracoleBundle\Model\Order\OrderShopInterface; | |||
use Lc\CaracoleBundle\Model\Order\OrderStatusHistoryModel; | |||
use Lc\CaracoleBundle\Model\Order\OrderStatusModel; | |||
use Lc\CaracoleBundle\Model\Section\SectionInterface; | |||
use Lc\CaracoleBundle\Model\User\VisitorInterface; | |||
use Lc\CaracoleBundle\Repository\Order\OrderProductStore; | |||
use Lc\CaracoleBundle\Repository\Order\OrderShopStore; | |||
use Lc\CaracoleBundle\Repository\Order\OrderStatusStore; | |||
use Lc\CaracoleBundle\Repository\Product\ProductFamilyStore; | |||
use Lc\CaracoleBundle\Resolver\Price\PriceResolver; | |||
@@ -30,37 +31,61 @@ class OrderShopBuilder | |||
protected ProductFamilyStore $productFamilyStore; | |||
protected PriceResolver $priceResolver; | |||
protected OrderProductBuilder $orderProductBuilder; | |||
protected DocumentBuilder $documentBuilder; | |||
public function __construct( | |||
EntityManagerInterface $entityManager, | |||
OrderShopStore $orderShopStore, | |||
OrderStatusStore $orderStatusStore, | |||
OrderProductStore $orderProductStore, | |||
ProductFamilyStore $productFamilyStore, | |||
OrderProductBuilder $orderProductBuilder, | |||
DocumentBuilder $documentBuilder, | |||
PriceResolver $priceResolver | |||
) { | |||
$this->entityManager = $entityManager; | |||
$this->orderShopStore = $orderShopStore; | |||
$this->orderStatusStore = $orderStatusStore; | |||
$this->orderProductStore = $orderProductStore; | |||
$this->productFamilyStore = $productFamilyStore; | |||
$this->orderProductBuilder = $orderProductBuilder; | |||
$this->documentBuilder = $documentBuilder; | |||
$this->priceResolver = $priceResolver; | |||
} | |||
public function create( | |||
MerchantInterface $merchant, | |||
SectionInterface $section, | |||
UserInterface $user = null, | |||
VisitorInterface $visitor = null | |||
) { | |||
$orderShopFactory = new OrderShopFactory(); | |||
$orderShop = $orderShopFactory->create($merchant, $section, $user, $visitor); | |||
$orderShop = $orderShopFactory->create($section, $user, $visitor); | |||
$this->changeOrderStatus($orderShop, OrderStatusModel::ALIAS_CART); | |||
return $orderShop; | |||
} | |||
public function createIfNotExist( | |||
SectionInterface $section, | |||
UserInterface $user = null, | |||
VisitorInterface $visitor = null | |||
) { | |||
$cart = $this->orderShopStore->getCartBy( | |||
[ | |||
'section' => $section, | |||
'user' => $user, | |||
'visitor' => $visitor | |||
] | |||
); | |||
if (!$cart) { | |||
$cart = $this->create($section, $user, $visitor); | |||
} | |||
return $cart; | |||
} | |||
public function changeOrderStatus(OrderShopInterface $orderShop, string $alias) | |||
{ | |||
$orderStatus = $this->orderStatusStore->getRepositoryQuery()->findOneByAlias($alias); | |||
@@ -239,4 +264,12 @@ class OrderShopBuilder | |||
return $orderShop; | |||
} | |||
public function createDocumentInvoice(OrderShopInterface $orderShop) | |||
{ | |||
$documentFactory = new DocumentFactory(); | |||
$document = $documentFactory->create(DocumentModel::TYPE_INVOICE); | |||
$this->documentBuilder->initFromOrderShop($orderShop); | |||
return $document; | |||
} | |||
} |
@@ -4,18 +4,23 @@ namespace Lc\CaracoleBundle\Factory\File; | |||
use App\Entity\File\Document; | |||
use Lc\CaracoleBundle\Factory\MerchantFactoryTrait; | |||
use Lc\CaracoleBundle\Model\Address\AddressInterface; | |||
use Lc\CaracoleBundle\Model\File\DocumentInterface; | |||
use Lc\SovBundle\Factory\AbstractFactory; | |||
use Lc\SovBundle\Model\User\UserInterface; | |||
class DocumentFactory extends AbstractFactory | |||
{ | |||
use MerchantFactoryTrait; | |||
public function create(): DocumentInterface | |||
public function create(string $type): DocumentInterface | |||
{ | |||
$document = new Document(); | |||
$document->setMerchant($this->merchant); | |||
$document->setType($type); | |||
$document->setTitle(''); | |||
$document->setStatus(1); | |||
return $document; | |||
} |
@@ -27,7 +27,6 @@ class OrderShopFactory extends AbstractFactory | |||
} | |||
public function create( | |||
MerchantInterface $merchant, | |||
SectionInterface $section, | |||
UserInterface $user = null, | |||
VisitorInterface $visitor = null | |||
@@ -35,7 +34,6 @@ class OrderShopFactory extends AbstractFactory | |||
$orderShop = new OrderShop(); | |||
$orderShop->setMerchant($merchant); | |||
$orderShop->setSection($section); | |||
$orderShopBelongTo = false; |
@@ -2,14 +2,22 @@ | |||
namespace Lc\CaracoleBundle\Repository\File; | |||
use Lc\CaracoleBundle\Repository\MerchantStoreTrait; | |||
use Lc\SovBundle\Repository\AbstractStore; | |||
class DocumentStore extends AbstractStore | |||
{ | |||
use MerchantStoreTrait; | |||
protected DocumentRepositoryQuery $query; | |||
public function __construct(DocumentRepositoryQuery $query) | |||
{ | |||
$this->query = $query; | |||
} | |||
public function getOneLatestByType(string $documentType) | |||
{ | |||
// @TODO : à écrire | |||
} | |||
} |
@@ -2,21 +2,31 @@ | |||
namespace Lc\CaracoleBundle\Repository\Order; | |||
use Lc\CaracoleBundle\Builder\File\DocumentBuilder; | |||
use Lc\CaracoleBundle\Factory\File\DocumentFactory; | |||
use Lc\CaracoleBundle\Model\File\DocumentModel; | |||
use Lc\CaracoleBundle\Model\Order\OrderShopInterface; | |||
use Lc\CaracoleBundle\Resolver\Price\PriceResolver; | |||
use Lc\CaracoleBundle\Resolver\Reference\DocumentReferenceResolver; | |||
use Lc\SovBundle\Repository\AbstractStore; | |||
class OrderShopStore extends AbstractStore | |||
{ | |||
protected OrderShopRepositoryQuery $query; | |||
protected PriceResolver $priceResolver; | |||
protected DocumentReferenceResolver $documentReferenceResolver; | |||
protected DocumentBuilder $documentBuilder; | |||
public function __construct( | |||
OrderShopRepositoryQuery $query, | |||
PriceResolver $priceResolver | |||
PriceResolver $priceResolver, | |||
DocumentReferenceResolver $documentReferenceResolver, | |||
DocumentBuilder $documentBuilder | |||
) { | |||
$this->query = $query; | |||
$this->priceResolver = $priceResolver; | |||
$this->documentReferenceResolver = $documentReferenceResolver; | |||
$this->documentBuilder = $documentBuilder; | |||
} | |||
public function getDatas(OrderShopInterface $orderShop = null): array | |||
@@ -139,4 +149,77 @@ class OrderShopStore extends AbstractStore | |||
{ | |||
return $this->getTotalRemainingToBePaid($orderShop) > 0; | |||
} | |||
public function getCartByUserOrCreateIt($user) | |||
{ | |||
$newOrderShop = $this->em->getRepository(OrderShopInterface::class)->findCartCurrent(['user' => $user]); | |||
if ($newOrderShop === null) { | |||
$newOrderShop = $this->createOrderShop( | |||
array( | |||
'user' => $user, | |||
'merchant' => $this->merchantUtils->getMerchantUser() | |||
) | |||
); | |||
} | |||
return $newOrderShop; | |||
} | |||
public function isCartAllowToBeOrder(OrderShopInterface $orderShop) | |||
{ | |||
return true; | |||
} | |||
/* | |||
public function getCartCurrent(SectionInterface $section, UserInterface $user = null, VisitorInterface $visitor = null) | |||
{ | |||
$paramsSearchOrderShop = []; | |||
$user = $this->security->getUser(); | |||
$visitor = $this->userUtils->getVisitorCurrent(); | |||
$orderShop = null; | |||
$orderShopUser = null; | |||
$orderShopVisitor = null; | |||
if ($user) { | |||
$orderShopUser = $this->orderShopRepo->findCartCurrent( | |||
[ | |||
'user' => $user | |||
] | |||
); | |||
} | |||
if ($visitor) { | |||
$orderShopVisitor = $this->orderShopRepo->findCartCurrent( | |||
[ | |||
'visitor' => $visitor | |||
] | |||
); | |||
} | |||
if ($orderShopUser || $orderShopVisitor) { | |||
// merge | |||
if ($orderShopUser && $orderShopVisitor && $orderShopUser != $orderShopVisitor | |||
&& $orderShopVisitor->getOrderProducts() && count($orderShopVisitor->getOrderProducts()) | |||
&& $orderShopUser->getOrderStatus()->getAlias() == OrderStatus::ALIAS_CART) { | |||
$orderShop = $this->mergeOrderShops($orderShopUser, $orderShopVisitor); | |||
$this->utils->addFlash( | |||
'success', | |||
"Votre panier visiteur vient d'être fusionné avec votre panier client." | |||
); | |||
} else { | |||
$orderShop = ($orderShopUser) ? $orderShopUser : $orderShopVisitor; | |||
} | |||
// set user | |||
if ($orderShop && $user && !$orderShop->getUser()) { | |||
$orderShop->setUser($user); | |||
$orderShop->setVisitor(null); | |||
$this->em->persist($orderShop); | |||
$this->em->flush(); | |||
} | |||
} | |||
return $orderShop; | |||
}*/ | |||
} |
@@ -0,0 +1,53 @@ | |||
<?php | |||
namespace Lc\CaracoleBundle\Resolver\Reference; | |||
use Lc\CaracoleBundle\Definition\SectionSettingDefinition; | |||
use Lc\CaracoleBundle\Model\File\DocumentModel; | |||
use Lc\CaracoleBundle\Model\Merchant\MerchantInterface; | |||
use Lc\CaracoleBundle\Model\Order\OrderShopInterface; | |||
use Lc\CaracoleBundle\Model\Section\SectionModel; | |||
use Lc\CaracoleBundle\Repository\File\DocumentStore; | |||
class DocumentReferenceResolver | |||
{ | |||
protected DocumentStore $documentStore; | |||
public function __construct(DocumentStore $documentStore) | |||
{ | |||
$this->documentStore = $documentStore; | |||
} | |||
public function buildReference(MerchantInterface $merchant, string $documentType) | |||
{ | |||
$prefix = ''; | |||
if ($documentType == DocumentModel::TYPE_DELIVERY_NOTE) { | |||
$prefix = 'BL'; | |||
} elseif ($documentType == DocumentModel::TYPE_PURCHASE_ORDER) { | |||
$prefix = 'BC'; | |||
} elseif ($documentType == DocumentModel::TYPE_INVOICE) { | |||
$prefix = 'FA'; | |||
} elseif ($documentType == DocumentModel::TYPE_QUOTATION) { | |||
$prefix = 'DE'; | |||
} | |||
$oneDocumentExist = $this->documentStore | |||
->setMerchant($merchant) | |||
->getOneLatestByType($documentType) ; | |||
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'; | |||
} | |||
} | |||
} |
@@ -1,12 +1,12 @@ | |||
<?php | |||
namespace Lc\CaracoleBundle\ReferenceBuilder; | |||
namespace Lc\CaracoleBundle\Resolver\Reference; | |||
use Lc\CaracoleBundle\Definition\SectionSettingDefinition; | |||
use Lc\CaracoleBundle\Model\Order\OrderShopInterface; | |||
use Lc\CaracoleBundle\Model\Section\SectionModel; | |||
class OrderReferenceBuilder | |||
class OrderReferenceResolver | |||
{ | |||
public function buildReference(OrderShopInterface $orderShop, \DateTime $distributionDate = null): string |