|
- <?php
-
- namespace Lc\ShopBundle\Controller\Backend;
-
- use Doctrine\ORM\EntityManagerInterface;
- use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
- use FOS\UserBundle\Model\UserManagerInterface;
- use Lc\ShopBundle\Context\MerchantUtilsInterface;
- use Lc\ShopBundle\Context\OrderPaymentInterface;
- use Lc\ShopBundle\Context\OrderProductInterface;
- use Lc\ShopBundle\Context\OrderReductionCartInterface;
- use Lc\ShopBundle\Context\OrderReductionCreditInterface;
- use Lc\ShopBundle\Context\OrderShopInterface;
- use Lc\ShopBundle\Context\OrderUtilsInterface;
- use Lc\ShopBundle\Context\UserInterface;
- use Lc\ShopBundle\Form\Backend\Order\AddPoductToOrderType;
- use Lc\ShopBundle\Form\Backend\Order\AddRedeliveryOrderProductType;
- use Lc\ShopBundle\Form\Backend\Order\DeleteOrderPaymentType;
- use Lc\ShopBundle\Form\Backend\Order\DeleteOrderReductionCartType;
- use Lc\ShopBundle\Form\Backend\Order\DeleteOrderReductionCreditType;
- use Lc\ShopBundle\Form\Backend\Order\OrderDeliveryAddressType;
- use Lc\ShopBundle\Form\Backend\Order\OrderInvoiceAddressType;
- use Lc\ShopBundle\Form\Backend\Order\OrderPaymentType;
- use Lc\ShopBundle\Form\Backend\Order\OrderProductsActionType;
- use Lc\ShopBundle\Form\Backend\Order\OrderProductsType;
- use Lc\ShopBundle\Form\Backend\Order\AddOrderReductionCartType;
- use Lc\ShopBundle\Form\Backend\Order\AddOrderReductionCreditType;
- use Lc\ShopBundle\Form\Backend\Order\OrderStatusType;
- use Lc\ShopBundle\Model\CreditHistory;
- use Lc\ShopBundle\Model\OrderStatus;
- use Lc\ShopBundle\Services\CreditUtils;
- use Lc\ShopBundle\Services\Utils;
- use Mailjet\MailjetSwiftMailer\SwiftMailer\MailjetTransport;
- use Proxies\__CG__\App\Entity\OrderProduct;
- use Symfony\Bridge\Doctrine\Form\Type\EntityType;
- use Symfony\Component\Form\Extension\Core\Type\SubmitType;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\Security\Core\Security;
- use Symfony\Contracts\Translation\TranslatorInterface;
-
-
- class OrderController extends AdminController
- {
- protected $creditUtils;
-
- public function __construct(Security $security, UserManagerInterface $userManager, EntityManagerInterface $em, Utils $utils, MerchantUtilsInterface $merchantUtils, MailjetTransport $mailjetTransport, OrderUtilsInterface $orderUtils, TranslatorInterface $translator, CreditUtils $creditUtils)
- {
- parent::__construct($security, $userManager, $em, $utils, $merchantUtils, $mailjetTransport, $orderUtils, $translator);
- $this->creditUtils = $creditUtils;
- }
-
- protected function createListQueryBuilder($entityClass, $sortDirection, $sortField = null, $dqlFilter = null)
- {
- $filterIsOrderStatus = false;
- $filterNotOrderStatus = false;
-
- if (isset($dqlFilter['isOrderStatus'])) $filterIsOrderStatus = $dqlFilter['isOrderStatus'];
- if (isset($dqlFilter['notOrderStatus'])) $filterNotOrderStatus = $dqlFilter['notOrderStatus'];
- $dqlFilter = $dqlFilter['filter'];
-
- $queryBuilder = parent::createListQueryBuilder($entityClass, $sortDirection, $sortField, $dqlFilter);
-
- $queryBuilder->leftJoin('entity.orderStatus', 'orderStatus');
-
- if ($filterIsOrderStatus) {
- $queryBuilder->andWhere('orderStatus.alias IN (:status)');
- $queryBuilder->setParameter('status', $filterIsOrderStatus);
- }
-
- if ($filterNotOrderStatus) {
- $queryBuilder->andWhere('orderStatus.alias NOT IN (:status)');
- $queryBuilder->setParameter('status', $filterNotOrderStatus);
- }
-
- return $queryBuilder;
- }
-
-
- public function updateEntity($entity)
- {
-
- foreach ($entity->getOrderProducts() as $orderProduct) {
-
- $orderProduct->setCreatedBy($this->getUser());
- $orderProduct->setUpdatedBy($this->getUser());
- $orderProduct->setTaxRate($orderProduct->getProduct()->getProductFamily()->getTaxRate());
- $orderProduct->setOrderShop($entity);
-
- $this->em->persist($orderProduct);
-
- $entity->addOrderProduct($orderProduct);
- }
-
- $this->setUpdated($entity);
-
- parent::updateEntity($entity);
- }
-
- public function persistEntity($entity)
- {
- foreach ($entity->getOrderProducts() as $orderProduct) {
-
- $orderProduct->setUnit($orderProduct->getProduct()->getUnitInherited());
- $orderProduct->setTitle($orderProduct->getProduct()->getTitleInherited());
- $orderProduct->setPrice($orderProduct->getProduct()->getPriceInherited());
- if ($orderProduct->getProduct()->getProductFamily()->getTaxRate()) {
- $orderProduct->setTaxRate($orderProduct->getProduct()->getProductFamily()->getTaxRate());
- } else {
- $orderProduct->setTaxRate($this->security->getUser()->getMerchant()->getTaxRate());
- }
-
- $orderProduct->setOrderShop($entity);
- $orderProduct->setCreatedBy($this->getUser());
- $orderProduct->setUpdatedBy($this->getUser());
- $this->em->persist($orderProduct);
- $entity->addOrderProduct($orderProduct);
-
- }
-
-
- parent::persistEntity($entity);
- }
-
-
- public function getUserViaFirstStepForm($entity)
- {
- $userClass = $this->em->getClassMetadata(UserInterface::class);
-
- $userChoiceForm = $this->createFormBuilder($entity)
- ->add('user', EntityType::class, array(
- 'class' => $userClass->name
- ))
- ->add('nextStep', SubmitType::class)
- ->getForm();
- $userChoiceForm->handleRequest($this->request);
- if ($userChoiceForm->isSubmitted() && $userChoiceForm->isValid()) {
- return $userChoiceForm->get('user')->getData();
- }
-
- $parameters = [
- 'form' => $userChoiceForm->createView(),
- 'formView' => 'default',
- 'entity' => $entity,
- ];
-
- return $this->executeDynamicMethod('render<EntityName>Template', ['new', $this->entity['templates']['new'], $parameters]);
- }
-
-
- public function addProductToOrderAction()
- {
- $orderShop = $this->getOrderShopEntity();
- $orderProductClass = $this->em->getClassMetadata(OrderProductInterface::class);
- $formAddProductToOrder = $this->createForm(AddPoductToOrderType::class);
- $formAddProductToOrder->handleRequest($this->request);
-
-
- if ($formAddProductToOrder->isSubmitted() && $formAddProductToOrder->isValid()) {
- $orderProduct = new $orderProductClass->name;
-
- if ($this->orderUtils->isProductAvailable($formAddProductToOrder->get('product')->getData(), $formAddProductToOrder->get('quantity')->getData())) {
- $orderProduct->setQuantityOrder($formAddProductToOrder->get('quantity')->getData());
- $orderProduct->setProduct($formAddProductToOrder->get('product')->getData());
-
- $this->orderUtils->addOrderProduct($orderShop, $orderProduct);
-
- $this->utils->addFlash('success', 'success.order.addProduct');
- } else {
- $this->utils->addFlash('error', 'error.order.productUnavailable');
- }
- } else {
- $this->utils->addFlash('error', 'error.form.submitted');
- }
-
- return $this->createOrderAjaxReponse($orderShop);
- }
-
- public function orderProductsAction()
- {
-
- $orderShop = $this->getOrderShopEntity();
- $formOrderProducts = $this->createForm(OrderProductsType::class, $orderShop);
- $formOrderProducts->handleRequest($this->request);
-
-
- if ($formOrderProducts->isSubmitted() && $formOrderProducts->isValid()) {
- $error = false;
-
- foreach ($orderShop->getOrderProducts() as $orderProduct) {
-
- if($orderProduct->isRedelivery() === false) {
-
- if ($this->orderUtils->isProductAvailable($orderProduct->getProduct(), $orderProduct->getQuantityOrder())) {
- if ($orderProduct->getQuantityOrder() <= 0) {
- $this->em->remove($orderProduct);
- } else {
- $this->em->persist($orderProduct);
- }
- } else {
- $error = true;
- $this->utils->addFlash('error', 'error.order.productUnavailable');
- }
- }
- }
- if (!$error) {
- $this->em->flush();
- $this->utils->addFlash('success', 'success.order.editQuantity');
- }
- } else {
- $this->utils->addFlash('error', 'error.form.submitted');
- }
- return $this->createOrderAjaxReponse($orderShop);
- }
-
-
- public function orderInvoiceAddressAction()
- {
-
- $orderShop = $this->getOrderShopEntity();
- $formOrderInvoiceAddress = $this->createForm(OrderInvoiceAddressType::class, $orderShop);
- $formOrderInvoiceAddress->handleRequest($this->request);
-
- if ($formOrderInvoiceAddress->isSubmitted() && $formOrderInvoiceAddress->isValid()) {
-
- $this->em->persist($orderShop);
- $this->em->flush();
-
- $this->utils->addFlash('success', 'success.order.changeInvoiceAddress');
- } else {
- $this->utils->addFlash('error', 'error.form.submitted');
- }
- return $this->createOrderAjaxReponse($orderShop);
- }
-
- public function orderStatusAction()
- {
- $orderShop = $this->getOrderShopEntity();
- $formOrderStatus = $this->createForm(OrderStatusType::class, $orderShop);
- $formOrderStatus->handleRequest($this->request);
-
- if ($formOrderStatus->isSubmitted() && $formOrderStatus->isValid()) {
- $oldStatus = $orderShop->getOrderStatus();
-
- $orderShop = $this->orderUtils->changeOrderStatus($formOrderStatus->get('orderStatus')->getData(), $orderShop);
- if ($oldStatus !== $orderShop->getOrderStatus()) {
- $this->utils->addFlash('success', 'success.order.changeStatus');
- }
- } else {
- $this->utils->addFlash('success', 'error.form.submitted');
- }
-
- return $this->redirectToRoute('easyadmin', [
- 'action' => 'show',
- 'entity' => $this->request->query->get('entity'),
- 'id' => $orderShop->getId()
- ]);
-
- }
-
-
- public function orderPaymentAction()
- {
- $orderShop = $this->getOrderShopEntity();
-
-
-
- $orderPaymentPost = $this->request->request->get('order_payment');
- if ($orderPaymentPost['id']) {
- $orderPayment = $this->em->getRepository(OrderPaymentInterface::class)->find($orderPaymentPost['id']);
- } else {
- $orderPaymentClass = $this->em->getClassMetadata(OrderPaymentInterface::class);
- $orderPayment = new $orderPaymentClass->name;
- }
-
-
- $formOrderPayment = $this->createForm(OrderPaymentType::class, $orderPayment);
-
- $formOrderPayment->handleRequest($this->request);
-
-
- if ($formOrderPayment->isSubmitted() && $formOrderPayment->isValid()) {
-
- $orderPayment->setOrderShop($orderShop);
- if ($orderPayment->getMeanPayment() === Utils::MEAN_PAYMENT_CREDIT) {
- $orderPayment->setEditable(false);
- $params['orderPayment'] = $orderPayment;
- $creditHistory = $this->creditUtils->initCreditHistory(CreditHistory::TYPE_DEBIT, $orderShop->getUser(), $params);
- if ($this->creditUtils->isCreditSufficientToPay($creditHistory->getUserMerchant(), $creditHistory->getAmountInherited())) {
- if ($this->creditUtils->saveCreditHistory($creditHistory)) {
- $this->em->persist($orderPayment);
- $this->em->flush();
- $this->utils->addFlash('success', 'success.credit.debited');
- $this->utils->addFlash('success', 'success.order.addPayment');
- }
- } else {
- $this->utils->addFlash('error', 'error.credit.notEnoughCredit');
- }
-
- } else {
- $orderPayment->setEditable(true);
- $this->em->persist($orderPayment);
- $this->em->flush();
- $this->utils->addFlash('success', 'success.order.addPayment');
- }
-
-
- } else {
- $this->utils->addFlash('error', $formOrderPayment->getErrors());
- }
- return $this->createOrderAjaxReponse($orderShop);
- }
-
- public function deleteOrderPaymentAction()
- {
- $orderShop = $this->getOrderShopEntity();
- $formDeleteOrderPayment = $this->createForm(DeleteOrderPaymentType::class);
- $formDeleteOrderPayment->handleRequest($this->request);
-
- if ($formDeleteOrderPayment->isSubmitted() && $formDeleteOrderPayment->isValid()) {
- $orderPayment = $this->em->getRepository(OrderPaymentInterface::class)->find($formDeleteOrderPayment->get('id')->getData());
- if ($orderPayment && $orderShop->getOrderPayments()->contains($orderPayment) && $orderPayment->getEditable()) {
- $orderShop->removeOrderPayment($orderPayment);
- $this->em->remove($orderPayment);
- $this->em->flush();
-
- $this->utils->addFlash('success', 'success.order.removeOrderPayement');
- }
-
- } else {
- $this->utils->addFlash('error', $formDeleteOrderPayment->getErrors());
- }
- return $this->createOrderAjaxReponse($orderShop);
- }
-
- public function addOrderReductionCartAction()
- {
- $orderShop = $this->getOrderShopEntity();
- $formAddOrderReductionCart = $this->createForm(AddOrderReductionCartType::class, $orderShop);
- $formAddOrderReductionCart->handleRequest($this->request);
-
- if ($formAddOrderReductionCart->isSubmitted() && $formAddOrderReductionCart->isValid()) {
-
- $reductionCart = $formAddOrderReductionCart->get('reductionCart')->getData();
- $orderShop->reductionError = array();
- if ($this->orderUtils->isReductionCartAllowAddToOrder($orderShop, $reductionCart)) {
- $orderReductionCart = $this->orderUtils->createOrderReductionCart($orderShop, $reductionCart);
- $this->em->persist($orderReductionCart);
- $this->em->flush();
-
- $this->utils->addFlash('success', 'success.order.addReductionCart');
- }
- } else {
- $this->utils->addFlash('error', $formAddOrderReductionCart->getErrors());
- }
- return $this->createOrderAjaxReponse($orderShop);
- }
-
- public function addOrderReductionCreditAction()
- {
- $orderShop = $this->getOrderShopEntity();
- $formAddOrderReductionCredit = $this->createForm(AddOrderReductionCreditType::class, $orderShop);
- $formAddOrderReductionCredit->handleRequest($this->request);
-
- if ($formAddOrderReductionCredit->isSubmitted() && $formAddOrderReductionCredit->isValid()) {
- $reductionCredit = $formAddOrderReductionCredit->get('reductionCredit')->getData();
- if ($this->orderUtils->isReductionCreditAllowAddToOrder($orderShop, $reductionCredit)) {
-
- $orderReductionCredit = $this->orderUtils->createOrderReductionCredit($orderShop, $reductionCredit);
- $this->em->persist($orderReductionCredit);
- $this->em->flush();
-
- $this->utils->addFlash('success', 'success.order.addReductionCredit');
- }
-
- } else {
- $this->utils->addFlash('error', $formAddOrderReductionCredit->getErrors());
- }
- return $this->createOrderAjaxReponse($orderShop);
- }
-
-
- public function addRedeliveryOrderProductAction()
- {
- $orderShop = $this->getOrderShopEntity();
- $formAddOrderReductionCredit = $this->createForm(AddRedeliveryOrderProductType::class, $orderShop);
-
- }
-
- public function deleteOrderReductionCartAction()
- {
- $orderShop = $this->getOrderShopEntity();
- $formDeleteOrderReductionCart = $this->createForm(DeleteOrderReductionCartType::class);
- $formDeleteOrderReductionCart->handleRequest($this->request);
-
- if ($formDeleteOrderReductionCart->isSubmitted() && $formDeleteOrderReductionCart->isValid()) {
- $orderReductionCart = $this->em->getRepository(OrderReductionCartInterface::class)->find($formDeleteOrderReductionCart->get('id')->getData());
-
- if ($orderReductionCart && $orderShop->getOrderReductionCarts()->contains($orderReductionCart)) {
- $orderShop->removeOrderReductionCart($orderReductionCart);
- $this->em->remove($orderReductionCart);
- $this->em->flush();
-
- $this->utils->addFlash('warning', 'success.order.removeReductionCart');
- }
-
- } else {
- $this->utils->addFlash('error', $formDeleteOrderReductionCart->getErrors());
- }
- return $this->createOrderAjaxReponse($orderShop);
- }
-
- public function deleteOrderReductionCreditAction()
- {
- $orderShop = $this->getOrderShopEntity();
- $formDeleteOrderReductionCredit = $this->createForm(DeleteOrderReductionCreditType::class);
- $formDeleteOrderReductionCredit->handleRequest($this->request);
-
- if ($formDeleteOrderReductionCredit->isSubmitted() && $formDeleteOrderReductionCredit->isValid()) {
- $orderReductionCredit = $this->em->getRepository(OrderReductionCreditInterface::class)->find($formDeleteOrderReductionCredit->get('id')->getData());
- if ($formDeleteOrderReductionCredit && $orderShop->getOrderReductionCredits()->contains($orderReductionCredit)) {
- $orderShop->removeOrderReductionCredit($orderReductionCredit);
- $this->em->remove($orderReductionCredit);
- $this->em->flush();
-
- $this->utils->addFlash('warning', 'success.order.removeReductionCredit');
- }
-
- } else {
- $this->utils->addFlash('error', $formDeleteOrderReductionCredit->getErrors());
- }
- return $this->createOrderAjaxReponse($orderShop);
- }
-
- protected function createOrderAjaxReponse(OrderShopInterface $order)
- {
- $response['flashMessages'] = $this->utils->getFlashMessages();
- $response['data'] = $this->orderUtils->getOrderAsJsonObject($order);
- return new Response(json_encode($response));
- }
-
-
- public function renderOrderShopTemplate($actionName, $templatePath, array $parameters = [])
- {
-
- if ($actionName == 'show') {
-
- $orderShop = $this->getOrderShopEntity();
-
- switch ($orderShop->getOrderStatus()->getAlias()) {
- case OrderStatus::ALIAS_CART :
-
- if (!isset($parameters['form_order_delivery_address'])) {
- $parameters['form_order_delivery_address'] = $this->createCustomForm(OrderDeliveryAddressType::class, 'orderDeliveryAddress', $parameters)->createView();
- }
- $parameters['form_add_order_reduction_credit'] = $this->createCustomForm(AddOrderReductionCreditType::class, 'addOrderReductionCredit', $parameters)->createView();
- $parameters['form_add_order_reduction_cart'] = $this->createCustomForm(AddOrderReductionCartType::class, 'addOrderReductionCart', $parameters)->createView();
- $parameters['form_delete_order_reduction_cart'] = $this->createCustomForm(DeleteOrderReductionCartType::class, 'deleteOrderReductionCart', $parameters)->createView();
- $parameters['form_delete_order_reduction_credit'] = $this->createCustomForm(DeleteOrderReductionCreditType::class, 'deleteOrderReductionCredit', $parameters)->createView();
- $parameters['form_add_product_to_order'] = $this->createCustomForm(AddPoductToOrderType::class, 'addProductToOrder', $parameters)->createView();
- $parameters['form_order_products'] = $this->createCustomForm(OrderProductsType::class, 'orderProducts', $parameters)->createView();
- $parameters['form_order_status'] = $this->createCustomForm(OrderStatusType::class, 'orderStatus', $parameters)->createView();
- $parameters['form_order_invoice_address'] = $this->createCustomForm(OrderInvoiceAddressType::class, 'orderInvoiceAddress', $parameters)->createView();
- break;
- case OrderStatus::ALIAS_WAITING_PAYMENT_CREDIT :
- case OrderStatus::ALIAS_WAITING_PAYMENT_ONLINE :
- $parameters['form_order_payment'] = $this->createCustomForm(OrderPaymentType::class, 'orderPayment', $parameters, false)->createView();
- $parameters['form_delete_order_payment'] = $this->createCustomForm(DeleteOrderPaymentType::class, 'deleteOrderPayment', $parameters)->createView();
- $parameters['form_order_status'] = $this->createCustomForm(OrderStatusType::class, 'orderStatus', $parameters)->createView();
- $parameters['form_order_invoice_address'] = $this->createCustomForm(OrderInvoiceAddressType::class, 'orderInvoiceAddress', $parameters)->createView();
- break;
- case OrderStatus::ALIAS_PAID_BY_CREDIT :
- case OrderStatus::ALIAS_PAID_ONLINE :
- $parameters['form_order_status'] = $this->createCustomForm(OrderStatusType::class, 'orderStatus', $parameters)->createView();
- break;
- }
-
-
- }
-
- return parent::renderTemplate($actionName, $templatePath, $parameters);
- }
-
-
- protected function newAction()
- {
- $this->dispatch(EasyAdminEvents::PRE_NEW);
-
- $entity = $this->executeDynamicMethod('createNew<EntityName>Entity');
-
- $easyadmin = $this->request->attributes->get('easyadmin');
- $easyadmin['item'] = $entity;
- $this->request->attributes->set('easyadmin', $easyadmin);
-
- $fields = $this->entity['new']['fields'];
-
- $newForm = $this->executeDynamicMethod('create<EntityName>NewForm', [$entity, $fields]);
-
- $newForm->handleRequest($this->request);
-
-
- $user = $newForm->get('user')->getData();
-
- if ($user == null) {
- $user = $this->getUserViaFirstStepForm($entity);
- }
-
- if (!$user instanceof UserInterface) return $user;
- else {
- $orderShopUser = $this->em->getRepository(OrderShopInterface::class)->findCartCurrent(['user'=>$user]);
- if($orderShopUser){
- $this->utils->addFlash('info', 'error.order.otherOrderAlreadyExist');
- return $this->redirectToRoute('easyadmin', [
- 'action' => 'edit',
- 'entity' => $this->entity['name'],
- 'id' => $orderShopUser->getId()
- ]);
- }else {
-
-
- $orderShop = $this->orderUtils->createOrderShop(array(
- 'user' => $user,
- 'merchant' => $this->merchantUtils->getMerchantUser()
- ));
-
- return $this->redirectToRoute('easyadmin', [
- 'action' => 'edit',
- 'entity' => $this->entity['name'],
- 'id' => $orderShop->getId()
- ]);
- }
- }
- }
-
-
-
- public function showAction()
- {
- $this->dispatch(EasyAdminEvents::PRE_SHOW);
-
- $id = $this->request->query->get('id');
- $easyadmin = $this->request->attributes->get('easyadmin');
- $entity = $easyadmin['item'];
-
- $fields = $this->entity['show']['fields'];
- $deleteForm = $this->createDeleteForm($this->entity['name'], $id);
-
- $this->dispatch(EasyAdminEvents::POST_SHOW, [
- 'deleteForm' => $deleteForm,
- 'fields' => $fields,
- 'entity' => $entity,
- ]);
-
- $parameters = [
- 'entity' => $entity,
- 'fields' => $fields,
- 'delete_form' => $deleteForm->createView(),
- 'order' => $this->orderUtils->getOrderAsJsonObject($entity)
-
- ];
-
- return $this->executeDynamicMethod('renderOrderShopTemplate', ['show', $this->entity['templates']['show'], $parameters]);
- }
-
-
-
- public function editAction()
- {
- $id = $this->request->query->get('id');
- $entity = $this->request->query->get('entity');
-
- return $this->redirectToRoute('easyadmin', [
- 'action' => 'show',
- 'entity' => $entity,
- 'id' => $id
- ]);
- }
-
- protected function getOrderShopEntity()
- {
- $easyadmin = $this->request->attributes->get('easyadmin');
- return $easyadmin['item'];
- }
-
- }
|