<?php
namespace App\Controller\Admin\Invoice;
use Symfony\Component\HttpFoundation\Request;
//use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Knp\Component\Pager\PaginatorInterface;
use App\Entity\Invoice\Invoice;
use App\Entity\Customer\Customer;
use App\Entity\Inventory\In;
use App\Entity\Inventory\Out;
class InvoiceController extends AbstractController
{
public function list(Request $request, PaginatorInterface $paginator)
{
$em = $this->getDoctrine()->getManager();
$dql = "SELECT i FROM App\Entity\Invoice\Invoice i WHERE i.deleted IS NULL ORDER BY i.number DESC";
$query = $em->createQuery($dql);
$pagination = $paginator->paginate(
$query, /* query NOT result */
$request->query->getInt('page', 1), /*page number*/
20 /*limit per page*/
);
return $this->render('admin/invoice/list.html.twig', [
'pagination' => $pagination
]);
}
public function new(Request $request, PaginatorInterface $paginator)
{
$em = $this->getDoctrine()->getManager();
$customers = $em->getRepository(Customer::class)->findAll();
return $this->render('admin/invoice/form.html.twig', [
'invoice_number' => 6000,
'customers' => $customers
]);
}
public function edit($invoice_id)
{
$em = $this->getDoctrine()->getManager();
$customers = $em->getRepository(Customer::class)->findAll();
$entity = $em->getRepository(Invoice::class)->find($invoice_id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Contact entity.');
}
$ins = $em->getRepository(In::class)->findByCustomer($entity->getCustomer());
$outs = $em->getRepository(Out::class)->findByInvoice($entity);
return $this->render('admin/invoice/form.html.twig', [
'invoice_number' => $entity->getNumber(),
'customers' => $customers,
'ins' => $ins,
'outs' => $outs,
'entity' => $entity
]);
}
public function update(Request $request)
{
$session = $request->getSession();
if ($request->isMethod('POST')) {
$iData = $request->request->get('invoice');
if (array_key_exists('customer_id', $iData)) {
$em = $this->getDoctrine()->getManager();
$new = false;
if (array_key_exists('id', $iData)) {
$invoice = $em->getRepository(Invoice::class)->find($iData['id']);
} else {
$invoice = new Invoice();
$invoice->setNumber($this->getInvoiceNumber());
$new = true;
}
$invoice->setCustomer($em->getRepository(Customer::class)->find($iData['customer_id']));
$invoice->setSub('');
if (array_key_exists('sub', $iData)) {
$invoice->setSub($iData['sub']);
}
$invoice->setInvoiceDate(new \DateTime($iData['invoice_date']));
$invoice->setTotal((float)$iData['total']);
$invoice->setHandling((float)$iData['handling']);
$invoice->setCopies((float)$iData['copies']);
$invoice->setAdditionalForm((float)$iData['additional_form']);
$invoice->setUnloadLoad((float)$iData['unload_load']);
$invoice->setRamp((float)$iData['ramp']);
$invoice->setForklift((float)$iData['forklift']);
$invoice->setOvertime((float)$iData['overtime']);
$invoice->setWarehouse((float)$iData['warehouse']);
$invoice->setWarehouseText($iData['warehouse_text']);
$invoice->setQuarantine((float)$iData['quarantine']);
$invoice->setQuarantineBankCharge((float)$iData['quarantine_bank_charge']);
$invoice->setApa((float)$iData['apa']);
$invoice->setGuideNumber($iData['guide_number']);
$invoice->setDmcIn($iData['dmc_in']);
$invoice->setDmcInTotal((float)$iData['dmc_in_total']);
$invoice->setDmcOut($iData['dmc_out']);
$invoice->setDmcOutTotal((float)$iData['dmc_out_total']);
$invoice->setTiTotal((float)$iData['ti_total']);
$invoice->setTiCash((float)$iData['ti_cash']);
$invoice->setOnlinePayment((float)$iData['online_payment']);
$invoice->setStampText($iData['stamp_text']);
$invoice->setStampTotal((float)$iData['stamp_total']);
$invoice->setStampCost((float)$iData['stamp_cost']);
//$invoice->setExpense($iData['expense']);
//$invoice->setProfit($iData['profit']);
$em->persist($invoice);
$em->flush();
// Upload Files
/*$files = $request->files->get('invoice');
foreach ($files as $id => $file) {
if (isset($file)) {
$uploaddir = $this->getParameter('files_directory')."/invoice/{$invoice->getNumber()}/";
if(!is_dir($uploaddir))
mkdir($uploaddir, 0644);
$filename = $file->getClientOriginalName();
move_uploaded_file($file, $uploaddir.$filename);
if ($id == 'guide_attachment') {
$invoice->setGuideAttachment($filename);
} else if ($id == 'pre_attachment') {
$invoice->setPreAttachment($filename);
} else if ($id == 'ti_attachment') {
$invoice->setTiAttachment($filename);
}
}
}
$em->persist($invoice);
$em->flush();*/
$this->addFlash('notice', 'invoice_update');
}
}
return $this->redirectToRoute('invoice_edit', ['invoice_id' => $invoice->getId()]);
}
public function delete($invoice_id)
{
$em = $this->getDoctrine()->getManager();
$customers = $em->getRepository(Customer::class)->findAll();
$entity = $em->getRepository(Invoice::class)->find($invoice_id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Invoice entity.');
}
$entity->setDeleted(new \DateTime('now'));
$em->persist($entity);
$em->flush();
$this->addFlash('notice', 'invoice_delete');
return $this->redirectToRoute('invoice_list');
}
public function paid($invoice_id)
{
$em = $this->getDoctrine()->getManager();
$customers = $em->getRepository(Customer::class)->findAll();
$entity = $em->getRepository(Invoice::class)->find($invoice_id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Invoice entity.');
}
$entity->setPaid(1);
$entity->setPaidDate(new \DateTime('now'));
$entity->setPaidBy($this->getUser());
$em->persist($entity);
$em->flush();
$this->addFlash('notice', 'invoice_paid');
return $this->redirectToRoute('invoice_list');
}
public function unpaid($invoice_id)
{
$em = $this->getDoctrine()->getManager();
$customers = $em->getRepository(Customer::class)->findAll();
$entity = $em->getRepository(Invoice::class)->find($invoice_id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Invoice entity.');
}
$entity->setPaid(0);
$entity->setPaidDate(NULL);
$entity->setPaidBy(NULL);
$em->persist($entity);
$em->flush();
$this->addFlash('notice', 'invoice_unpaid');
return $this->redirectToRoute('invoice_list');
}
public function getInvoiceNumber()
{
$em = $this->getDoctrine()->getManager();
$sql = "SELECT i.number FROM invoice i WHERE i.number REGEXP '^[0-9]+$' ORDER BY CAST(i.number AS UNSIGNED) DESC LIMIT 1";
$query = $em
->getConnection()
->prepare($sql);
$res = $query->execute();
$sequence = $res->fetchOne();
$num = 1;
if ($sequence) {
$num = $sequence + 1;
}
return $num;
}
}