Created
October 5, 2018 08:37
-
-
Save cezar62882/9b8a049c1bb6c0f855756ae26efe1d80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function create(Order $order, EntityManagerInterface $em, Request $request): Response | |
{ | |
$shipment = new Shipment(); | |
/** @var OrderProduct[] $products */ | |
$products = $em->getRepository('App:OrderProduct')->findAllNotShippedByOrder($order); | |
foreach ($products as $product) { | |
$shipmentProduct = new ShipmentProduct(); | |
$shipmentProduct->setQuantity($product->getQuantity()); | |
$shipmentProduct->setOrderProduct($product); | |
$shipment->addProduct($shipmentProduct); | |
} | |
$form = $this->createForm(ShipmentType::class, $shipment); | |
$form->handleRequest($request); | |
if ($form->isSubmitted() && $form->isValid()) { | |
if (is_array($request->get('shipment')['products'])) { | |
$checkedKeys = array_keys($request->get('shipment')['products']); | |
foreach ($shipment->getProducts() as $key => $product) { | |
if (!in_array($key, $checkedKeys)) { | |
$shipment->removeProduct($product); | |
} | |
} | |
} | |
$shipment->setOrder($order); | |
$shipment->setSupplierId(Supplier::MANUAL); | |
$shipment->setCurrency($em->getRepository('App:Currency')->find(Currency::USD)); | |
$em->persist($shipment); | |
$em->flush(); | |
$products = $em->getRepository('App:OrderProduct')->findAllNotShippedByOrder($order); | |
if (empty($products)) { | |
$shipments = $em->getRepository('App:Shipment')->findAllWithoutTrackerByOrder($order); | |
$order->setStatus($shipments ? Order::STATUS_MANUALLY_PROCESSED : Order::STATUS_FULFILLED); | |
$em->flush(); | |
} | |
return $this->redirectToRoute('panel_order_show', ['id' => $order->getId()]); | |
} | |
return $this->render('panel/shipment/create.html.twig', [ | |
'order' => $order, | |
'products' => $products, | |
'form' => $form->createView() | |
]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment