src/Controller/Admin/Invoice/InvoiceController.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin\Invoice;
  3. use Symfony\Component\HttpFoundation\Request;
  4. //use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Knp\Component\Pager\PaginatorInterface;
  7. use App\Entity\Invoice\Invoice;
  8. use App\Entity\Customer\Customer;
  9. use App\Entity\Inventory\In;
  10. use App\Entity\Inventory\Out;
  11. class InvoiceController extends AbstractController
  12. {
  13.     public function list(Request $requestPaginatorInterface $paginator)
  14.     {
  15.         $em $this->getDoctrine()->getManager();
  16.         $dql   "SELECT i FROM App\Entity\Invoice\Invoice i WHERE i.deleted IS NULL ORDER BY i.number DESC";
  17.         $query $em->createQuery($dql);
  18.         $pagination $paginator->paginate(
  19.             $query/* query NOT result */
  20.             $request->query->getInt('page'1), /*page number*/
  21.             20 /*limit per page*/
  22.         );
  23.         return $this->render('admin/invoice/list.html.twig', [
  24.             'pagination' => $pagination
  25.         ]);
  26.     }
  27.     public function new(Request $requestPaginatorInterface $paginator)
  28.     {
  29.         $em $this->getDoctrine()->getManager();
  30.         $customers $em->getRepository(Customer::class)->findAll();
  31.         return $this->render('admin/invoice/form.html.twig', [
  32.             'invoice_number' => 6000,
  33.             'customers' => $customers
  34.         ]);
  35.     }
  36.     public function edit($invoice_id)
  37.     {
  38.         $em $this->getDoctrine()->getManager();
  39.         $customers $em->getRepository(Customer::class)->findAll();
  40.         $entity $em->getRepository(Invoice::class)->find($invoice_id);
  41.         if (!$entity) {
  42.             throw $this->createNotFoundException('Unable to find Contact entity.');
  43.         }
  44.         $ins $em->getRepository(In::class)->findByCustomer($entity->getCustomer());
  45.         $outs $em->getRepository(Out::class)->findByInvoice($entity);
  46.         return $this->render('admin/invoice/form.html.twig', [
  47.             'invoice_number' => $entity->getNumber(),
  48.             'customers' => $customers,
  49.             'ins' => $ins,
  50.             'outs' => $outs,
  51.             'entity' => $entity
  52.         ]);
  53.     }
  54.     public function update(Request $request)
  55.     {
  56.         $session $request->getSession();
  57.         if ($request->isMethod('POST')) {
  58.             $iData $request->request->get('invoice');
  59.             if (array_key_exists('customer_id'$iData)) {
  60.                 $em $this->getDoctrine()->getManager();
  61.                 $new false;
  62.                 if (array_key_exists('id'$iData)) {
  63.                     $invoice $em->getRepository(Invoice::class)->find($iData['id']);
  64.                 } else {
  65.                     $invoice = new Invoice();
  66.                     $invoice->setNumber($this->getInvoiceNumber());
  67.                     $new true;
  68.                 }
  69.                 $invoice->setCustomer($em->getRepository(Customer::class)->find($iData['customer_id']));
  70.                 $invoice->setSub('');
  71.                 if (array_key_exists('sub'$iData)) {
  72.                     $invoice->setSub($iData['sub']);
  73.                 }
  74.                 $invoice->setInvoiceDate(new \DateTime($iData['invoice_date']));
  75.                 $invoice->setTotal((float)$iData['total']);
  76.                 $invoice->setHandling((float)$iData['handling']);
  77.                 $invoice->setCopies((float)$iData['copies']);
  78.                 $invoice->setAdditionalForm((float)$iData['additional_form']);
  79.                 $invoice->setUnloadLoad((float)$iData['unload_load']);
  80.                 $invoice->setRamp((float)$iData['ramp']);
  81.                 $invoice->setForklift((float)$iData['forklift']);
  82.                 $invoice->setOvertime((float)$iData['overtime']);
  83.                 $invoice->setWarehouse((float)$iData['warehouse']);
  84.                 $invoice->setWarehouseText($iData['warehouse_text']);
  85.                 $invoice->setQuarantine((float)$iData['quarantine']);
  86.                 $invoice->setQuarantineBankCharge((float)$iData['quarantine_bank_charge']);
  87.                 $invoice->setApa((float)$iData['apa']);
  88.                 $invoice->setGuideNumber($iData['guide_number']);
  89.                 $invoice->setDmcIn($iData['dmc_in']);
  90.                 $invoice->setDmcInTotal((float)$iData['dmc_in_total']);
  91.                 $invoice->setDmcOut($iData['dmc_out']);
  92.                 $invoice->setDmcOutTotal((float)$iData['dmc_out_total']);
  93.                 $invoice->setTiTotal((float)$iData['ti_total']);
  94.                 $invoice->setTiCash((float)$iData['ti_cash']);
  95.                 $invoice->setOnlinePayment((float)$iData['online_payment']);
  96.                 $invoice->setStampText($iData['stamp_text']);
  97.                 $invoice->setStampTotal((float)$iData['stamp_total']);
  98.                 $invoice->setStampCost((float)$iData['stamp_cost']);
  99.                 //$invoice->setExpense($iData['expense']);
  100.                 //$invoice->setProfit($iData['profit']);
  101.                 $em->persist($invoice);
  102.                 $em->flush();
  103.                 // Upload Files
  104.                 /*$files = $request->files->get('invoice');
  105.                 foreach ($files as $id => $file) {
  106.                     if (isset($file)) {
  107.                         $uploaddir = $this->getParameter('files_directory')."/invoice/{$invoice->getNumber()}/";
  108.                         if(!is_dir($uploaddir))
  109.                             mkdir($uploaddir, 0644);
  110.                         $filename = $file->getClientOriginalName();
  111.                         move_uploaded_file($file, $uploaddir.$filename);
  112.                         if ($id == 'guide_attachment') {
  113.                             $invoice->setGuideAttachment($filename);
  114.                         } else if ($id == 'pre_attachment') {
  115.                             $invoice->setPreAttachment($filename);
  116.                         } else if ($id == 'ti_attachment') {
  117.                             $invoice->setTiAttachment($filename);
  118.                         }
  119.                     }
  120.                 }
  121.                 $em->persist($invoice);
  122.                 $em->flush();*/
  123.                 $this->addFlash('notice''invoice_update');
  124.             }
  125.         }
  126.         
  127.         return $this->redirectToRoute('invoice_edit', ['invoice_id' => $invoice->getId()]);
  128.     }
  129.     public function delete($invoice_id)
  130.     {
  131.         $em $this->getDoctrine()->getManager();
  132.         $customers $em->getRepository(Customer::class)->findAll();
  133.         $entity $em->getRepository(Invoice::class)->find($invoice_id);
  134.         if (!$entity) {
  135.             throw $this->createNotFoundException('Unable to find Invoice entity.');
  136.         }
  137.         $entity->setDeleted(new \DateTime('now'));
  138.         $em->persist($entity);
  139.         $em->flush();
  140.         $this->addFlash('notice''invoice_delete');
  141.         return $this->redirectToRoute('invoice_list');
  142.     }
  143.     public function paid($invoice_id)
  144.     {
  145.         $em $this->getDoctrine()->getManager();
  146.         $customers $em->getRepository(Customer::class)->findAll();
  147.         $entity $em->getRepository(Invoice::class)->find($invoice_id);
  148.         if (!$entity) {
  149.             throw $this->createNotFoundException('Unable to find Invoice entity.');
  150.         }
  151.         $entity->setPaid(1);
  152.         $entity->setPaidDate(new \DateTime('now'));
  153.         $entity->setPaidBy($this->getUser());
  154.         $em->persist($entity);
  155.         $em->flush();
  156.         $this->addFlash('notice''invoice_paid');
  157.         return $this->redirectToRoute('invoice_list');
  158.     }
  159.     public function unpaid($invoice_id)
  160.     {
  161.         $em $this->getDoctrine()->getManager();
  162.         $customers $em->getRepository(Customer::class)->findAll();
  163.         $entity $em->getRepository(Invoice::class)->find($invoice_id);
  164.         if (!$entity) {
  165.             throw $this->createNotFoundException('Unable to find Invoice entity.');
  166.         }
  167.         $entity->setPaid(0);
  168.         $entity->setPaidDate(NULL);
  169.         $entity->setPaidBy(NULL);
  170.         $em->persist($entity);
  171.         $em->flush();
  172.         $this->addFlash('notice''invoice_unpaid');
  173.         return $this->redirectToRoute('invoice_list');
  174.     }
  175.     public function getInvoiceNumber()
  176.     {
  177.         $em $this->getDoctrine()->getManager();
  178.         $sql "SELECT i.number FROM invoice i WHERE i.number REGEXP '^[0-9]+$' ORDER BY CAST(i.number AS UNSIGNED) DESC LIMIT 1";
  179.         $query $em
  180.             ->getConnection()
  181.             ->prepare($sql);
  182.         $res $query->execute();
  183.         $sequence $res->fetchOne();
  184.         $num 1;
  185.         if ($sequence) {
  186.             $num $sequence 1;
  187.         }
  188.         return $num;
  189.     }
  190. }