Skip to content

Instantly share code, notes, and snippets.

@cezar62882
Created October 5, 2018 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cezar62882/9b8a049c1bb6c0f855756ae26efe1d80 to your computer and use it in GitHub Desktop.
Save cezar62882/9b8a049c1bb6c0f855756ae26efe1d80 to your computer and use it in GitHub Desktop.
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