/
var
/
www
/
html
/
boaz2
/
src
/
Controller
/
Upload File
HOME
<?php namespace App\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; use App\Entity\User; use App\Entity\Cargo; use App\Entity\Depot; use App\Entity\Subcargo; use App\Entity\ReleaseOrder; use App\Entity\Loading; class CargoController extends AbstractController { /** * @Route("/cargo/add", name="addCargo") */ public function add() { if(isset($_POST['submit'])) { $name = $_POST['seller'] . "-" . $_POST['product'] . "-" . $_POST['date']; $cargo = new Cargo(); $cargo->setSeller($_POST['seller']); $cargo->setProduct($_POST['product']); $cargo->setQuantity(intval(str_replace(",", "", $_POST['quantity']))); $cargo->setInitial(intval(str_replace(",", "", $_POST['quantity']))); // $date = date($_POST['date']); $cargo->setDate(\DateTime::createFromFormat("m/d/Y", $_POST['date'])); $cargo->setCargoname($name); $em = $this->getDoctrine()->getManager(); $em->persist($cargo); $em->flush(); return $this->redirectToRoute('viewCargo', array('id' => $cargo->getId(), 'product' => $_POST['product'])); // return $this->redirectToRoute('subCargo', array('id' => $cargo->getId(), 'quantity' => $_POST['quantity'], 'date' => $_POST['date'])); } return $this->render('release/cargo.html.twig', ['title' => 'Enter Cargo Details']); } /** * @Route("/cargo/sub", name="subCargo") */ public function sub($id = null, $quantity = null, $date = null) { if(isset($_POST['submit'])) { $cargo = new Subcargo(); $oldCargo = $this->getDoctrine()->getRepository(Cargo::class)->find($_POST['cargo']); $cargo->setQuantity($_POST['quantity']); $cargo->setDate($_POST['date']); $cargo->setCargo($oldCargo); $oldCargo->setQuantity($oldCargo->getQuantity() + $_POST['quantity']); $cargo->setTotal($oldCargo->getQuantity()); $em = $this->getDoctrine()->getManager(); $em->persist($cargo); $em->persist($oldCargo); $em->flush(); return $this->redirectToRoute('viewCargo', array('id' => $_POST['cargo'])); }else if(isset($_GET['quantity'])){ $cargo = new Subcargo(); $oldCargo = $this->getDoctrine()->getRepository(Cargo::class)->find($_GET['id']); $cargo->setQuantity($_GET['quantity']); $cargo->setDate($_GET['date']); $cargo->setCargo($oldCargo); $oldCargo->setQuantity($oldCargo->getQuantity() + $quantity); $em = $this->getDoctrine()->getManager(); $em->persist($cargo); $em->persist($oldCargo); $em->flush(); return $this->redirectToRoute('viewCargo'); } return $this->render('cargo/subcargo.html.twig', ['title' => 'Enter Cargo Details', 'cargoid' => $_GET['id']]); } /** * @Route("/cargo/edit/{id}", name="editCargo") */ public function edit($id = null) { if(isset($_POST['submit'])) { $cargo = $this->getDoctrine()->getRepository(Cargo::class)->find($_POST['cargoid']); $cargo->setSeller($_POST['seller']); $cargo->setProduct($_POST['product']); $cargo->setDate(new \DateTime(date('d-m-Y H:i:s', strtotime($_POST['date'])))); $name = $_POST['seller'] . "-" . $_POST['product'] . "-" . $_POST['date']; if($this->isGranted('ROLE_MANAGEMENT')) { $cargo->setQuantity($_POST['quantity']); $cargo->setInitial($_POST['initial']); } $cargo->setCargoname($name); $em = $this->getDoctrine()->getManager(); $em->persist($cargo); $em->flush(); return $this->redirectToRoute('viewCargo'); } $cargo = $this->getDoctrine()->getRepository(Cargo::class)->find($id); return $this->render('cargo/edit.html.twig', ['title' => 'Enter Cargo Details', 'cargo' => $cargo]); } /** * @Route("/cargo/{id}/{product}", name="viewCargo", requirements={"id": "\s*|\d+"}) */ public function view($id = null, $product = null) { if($id) { // $balance = $this->getDoctrine()->getRepository(Cargo::class)->calculateBalance($id); $cargo = $this->getDoctrine()->getRepository(Cargo::class)->find($id); $subcargo = $this->getDoctrine()->getRepository(Subcargo::class)->findByCargoId($id); $release = $this->getDoctrine()->getRepository(ReleaseOrder::Class)->findByCargo($id); $releasedTotal = array_reduce($release, function($accum, $cur){ return $accum + $cur->getQuantity(); }); // calculate the total of all loadings from release orders attached to current cargo // in order to find the unloaded balance $loadedTotal = array_reduce($release, function($accum, $cur){ return $accum + array_reduce($cur->getLoading()->toArray(), function($accums, $curs){ return $accums + $curs->getQuantity(); }); }); $unreleased = $cargo->getQuantity() - $releasedTotal; $unloaded = $cargo->getQuantity() - $loadedTotal; return $this->render('cargo/view.html.twig', ['title' => 'Cargo Details', 'unreleased' => $unreleased, 'cargo' => $cargo, 'release' => $release, 'subcargo' => $subcargo, 'unloaded' => $unloaded]); } $cargo = $this->getDoctrine()->getRepository(Cargo::class)->findByProduct($product); return $this->render('cargo/list.html.twig', ['title' => 'Cargo List', 'cargo' => $cargo]); } /** * @Route("/cargo/full/{id}", name="viewCargoFull", requirements={"id": "\d+"}) */ public function viewFull($id = null) { if($id) { // $balance = $this->getDoctrine()->getRepository(Cargo::class)->calculateBalance($id); $cargo = $this->getDoctrine()->getRepository(Cargo::class)->find($id); $release = $this->getDoctrine()->getRepository(ReleaseOrder::Class)->findByCargo($id); $total = 0; foreach ($release as $key => $value) { $total = $total + $value->getQuantity(); } $balance = $cargo->getQuantity() - $total; return $this->render('cargo/view.html.twig', ['title' => 'Cargo Details', 'balance' => $balance, 'cargo' => $cargo, 'release' => $release]); } $cargo = $this->getDoctrine()->getRepository(Cargo::class)->findAll(); return $this->render('cargo/list.html.twig', ['title' => 'Cargo List', 'cargo' => $cargo]); } /** * @Route("/cargo/search", name="searchCargo") */ public function search() { $cargo = -1; if(isset($_GET['search'])) { if($_GET['search'] === 'seller') $cargo = $this->getDoctrine()->getRepository(Cargo::class)->findBySeller($_GET['query']); else if($_GET['search'] === 'product') $cargo = $this->getDoctrine()->getRepository(Cargo::class)->findByProduct($_GET['query']); else $cargo = $this->getDoctrine()->getRepository(Cargo::class)->findByDate($_GET['from'], $_GET['to']); } return $this->render('cargo/search.html.twig', ['title' => 'Search Cargo', 'cargo' => $cargo]); } /** * @Route("/cargo/{id}/release/generate", name="generate_release_from_cargo") */ public function genRelease(Request $request, $id) { $cargo = $this->getDoctrine()->getRepository(Cargo::class)->find($id); $depots = $this->getDoctrine()->getRepository(Depot::class)->findBy([], ['name' => 'ASC']); if (!$cargo) { return $this->redirectToRoute('viewCargoFull'); } return $this->render('release/generate.html.twig', ['title' => 'Generate Release Order', 'cargoId' => $cargo, 'depots' => $depots]); } /** * Delete Cargo * * @Route("/cargo/delete/{id}", name="deleteCargo") * @param Cargo $cargo * @return Response **/ public function delete(Cargo $cargo) { $em = $this->getDoctrine()->getManager(); if ($cargo) { $em->remove($cargo); $em->flush(); } $cargos = $em->getRepository(Cargo::class)->findAll(); return $this->render('cargo/list.html.twig', ['title' => 'Cargo List', 'cargo' => $cargos]); } /** * Delete Cargo Add-on * * @Route("/cargo/delete/{cargo}/{subcargo}", name="deleteSubCargo") * @param Cargo $cargo * @return Response **/ public function deleteSub(Cargo $cargo, Subcargo $subcargo) { $em = $this->getDoctrine()->getManager(); if ($cargo && $subcargo) { $cargo->setQuantity($cargo->getQuantity() - $subcargo->getQuantity()); $em->remove($subcargo); $em->persist($cargo); $em->flush(); } return $this->redirectToRoute('viewCargo', ['id' => $cargo->getId()]); } /** * @Route("/depots", name="depots") */ public function depots() { $depots = $this->getDoctrine()->getRepository(Depot::class)->findBy([], ['name' => 'ASC']); return $this->render('cargo/depots.html.twig', ['title' => 'Depot', 'depots' => $depots]); } /** * @Route("/depot/{id}", name="depot") */ public function depot(Request $request, $id = null) { $em = $this->getDoctrine()->getManager(); $depot = $id ? $em->getRepository(Depot::class)->find($id) : null; if($request->isMethod('POST')){ $depot = $depot ?: new Depot(); $depot->setName($request->get('name')); $depot->setLocation($request->get('location')); $em->persist($depot); $em->flush(); return $this->redirectToRoute('depots'); } return $this->render('cargo/depot.html.twig', ['title' => 'Depot', 'depot' => $depot]); } /** * @Route("/depot/delete/{id}", name="delete_depot") */ public function deleteDepot($id) { $em = $this->getDoctrine()->getManager(); $depot = $em->getRepository(Depot::class)->find($id); $em->getRepository(ReleaseOrder::class)->softDeleteByDepot($id); $em->remove($depot); $em->flush(); return $this->redirectToRoute('depots'); } /** * @Route("/sort/depots") */ public function sortDepots() { $em = $this->getDoctrine()->getManager(); $releases = $em->getRepository(ReleaseOrder::class)->findAll(); foreach ($releases as $release) { $depot = $em->getRepository(Depot::class)->findOneBy(['name' => $release->getDepots()]); $release->setDepot($depot); $em->persist($release); } $em->flush(); return $this->redirectToRoute('depots'); } }