Guest User

test

a guest
Oct 30th, 2020
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.86 KB | None | 0 0
  1. <?php
  2.  
  3. // src/Eleven/CRMBundle/Controller/InvoiceController.php
  4. namespace App\Eleven\CRMBundle\Controller;
  5.  
  6. // Symfony
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. // PDF
  14. use Dompdf\Dompdf;
  15. use Dompdf\Options;
  16. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  17.  
  18. // Eleven
  19. use App\Eleven\FrameworkBundle\Service\SettingManager;
  20. use App\Eleven\CRMBundle\Entity\Invoice;
  21. use App\Eleven\CRMBundle\Repository\InvoiceRepository;
  22. use App\Eleven\CRMBundle\Form\Type\InvoiceFormType;
  23. use App\Eleven\FrameworkBundle\Repository\DocumentRepository;
  24. use App\Eleven\FrameworkBundle\Utils\FormUtils;
  25. use App\Eleven\FrameworkBundle\Service\FileStorageManager;
  26.  
  27. use Twig\Extra\Intl\IntlExtension;
  28.  
  29.  
  30. class InvoiceController extends AbstractController
  31. {
  32.  
  33. /**
  34. * Invoice index with paginator
  35. *
  36. * @Route("/invoice/test", name="invoice_test")
  37. */
  38. public function test() {
  39. $invoice = new Invoice('12345');
  40. dump($invoice->getId());
  41. $em = $this->getDoctrine()->getManager();
  42. $em->persist($invoice);
  43. dump($invoice->getId());
  44.  
  45. $em->flush();
  46. dump($invoice->getId());
  47.  
  48.  
  49. die;
  50. }
  51.  
  52. /**
  53. * Invoice index with paginator
  54. *
  55. * @Route("/invoice", defaults={"page": "1", "_format"="html"}, methods="GET", name="invoice_index")
  56. * @Route("/invoice/page/{page<[1-9]\d*>}", defaults={"_format"="html"}, methods="GET", name="invoice_index_paginated")
  57. */
  58. public function invoiceIndex(int $page, string $_format, InvoiceRepository $invoices) {
  59. $invoicePaginator = $invoices->findLast($page);
  60.  
  61. return $this->render('@ElevenCRM/invoice/index.'.$_format.'.twig', [
  62. 'paginator' => $invoicePaginator,
  63. 'defaultRoute' => '/invoice'
  64. ] );
  65.  
  66. }
  67.  
  68. /**
  69. * @route("/invoice/add", name="invoice_add")
  70. */
  71. public function invoiceAdd(Request $request, InvoiceRepository $invoices, FileStorageManager $fileStorageManager) {
  72. $lastUsedInvoiceNumber = $invoices->findNextInvoiceNumber();
  73. $invoice = new Invoice($lastUsedInvoiceNumber);
  74. if (!$request->isXmlHttpRequest()) {
  75. $em = $this->getDoctrine()->getManager();
  76. $em->persist($invoice);
  77. }
  78. return $this->invoiceResponse($invoice, $request, $fileStorageManager);
  79. }
  80.  
  81. /**
  82. * @Route("/invoice/edit/{id}", name="invoice_edit")
  83. */
  84. public function invoiceEdit(Invoice $invoice, Request $request, FileStorageManager $fileStorageManager) {
  85. return $this->invoiceResponse($invoice, $request, $fileStorageManager);
  86. }
  87.  
  88. /**
  89. * extra info : double form in 1 post.
  90. * https://stackoverflow.com/questions/11841166/how-to-get-the-name-of-the-form-symfony2
  91. */
  92. private function invoiceResponse($invoice, Request $request, FileStorageManager $fileStorageManager){
  93.  
  94.  
  95. $form = $this->createForm(InvoiceFormType::class, $invoice);
  96.  
  97. // fix empty dates with string '__-__-____'
  98. FormUtils::fixRequestDate($request, 'invoice_form');
  99.  
  100. $form->handleRequest($request);
  101.  
  102.  
  103. if ($form->isSubmitted() && $form->isValid()) {
  104. $invoice = $form->getData();
  105.  
  106. echo($invoice->getId());
  107. die;
  108.  
  109. //TODO: transactions https://github.com/doctrine/orm/issues/5759
  110. $em = $this->getDoctrine()->getManager();
  111. $em->persist($invoice);
  112. $em->flush();
  113.  
  114.  
  115.  
  116. // File
  117. $file = $form->get('file')->getData();
  118.  
  119. if ($file) {
  120. $document = $fileStorageManager->uploadDocumentFile($file,'invoice',$invoice->getId());
  121. //dump ($document);
  122. //die;
  123. }
  124.  
  125.  
  126.  
  127.  
  128. if ($request->isXmlHttpRequest()) {
  129. return new JsonResponse(array('message' => 'Success!',
  130. 'id' => $invoice->getId() ,
  131. 'output' => $this->renderView('@ElevenCRM/invoice/form.html.twig',
  132. array(
  133. 'entity' => $invoice,
  134. 'form' => $form->createView()
  135. ))
  136. ),200);
  137. }
  138.  
  139. return $this->redirectToRoute('invoice_index');
  140. }
  141.  
  142. if ($request->isXmlHttpRequest()) {
  143.  
  144. return new JsonResponse(array('message' => 'Error',
  145. 'id' => $invoice->getId() ,
  146. 'output' => $this->renderView('@ElevenCRM/invoice/form.html.twig',
  147. array(
  148. 'entity' => $invoice,
  149. 'form' => $form->createView()
  150. ))
  151. ), 200);
  152. }
  153.  
  154.  
  155.  
  156. return $this->render('@ElevenCRM/invoice/invoice.html.twig', array(
  157. 'id' => $invoice->getId(),
  158. 'table' => 'invoice',
  159. 'form' => $form->createView(),
  160. 'entity' => $invoice,
  161. 'defaultRoute' => '/invoice'
  162. ));
  163. }
  164.  
  165.  
  166.  
  167. /**
  168. * @Route("/invoice/delete/{id}", name="invoice_delete")
  169. */
  170. public function delete($id, Request $request)
  171. {
  172. $em = $this->getDoctrine()->getManager();
  173. $invoice = $em->getRepository(Invoice::class)->find($id);
  174.  
  175. $em->remove($invoice);
  176. $em->flush();
  177.  
  178. if ($request->isXmlHttpRequest()) {
  179. return new JsonResponse(array('message' => 'Success!',
  180. 'id' => $id
  181. ),200);
  182. }
  183.  
  184. return $this->redirectToRoute('invoice_index');
  185. }
  186.  
  187. /**
  188. * @Route("/invoice/pdf/{id}", name="invoice_pdf")
  189. */
  190. public function pdf($id) {
  191. $em = $this->getDoctrine()->getManager();
  192. $invoice = $em->getRepository(Invoice::class)->find($id);
  193.  
  194. // Configure Dompdf according to your needs
  195. $pdfOptions = new Options();
  196. $pdfOptions->set('defaultFont', 'Arial');
  197.  
  198. // Instantiate Dompdf with our options
  199. $dompdf = new Dompdf($pdfOptions);
  200.  
  201. // Retrieve the HTML generated in our twig file
  202. $html = $this->renderView('@ElevenCRM/invoice/invoice_pdf.html.twig', [
  203. 'invoice' => $invoice
  204. ]);
  205.  
  206. // Load HTML to Dompdf
  207. $dompdf->loadHtml($html);
  208.  
  209. // (Optional) Setup the paper size and orientation 'portrait' or 'portrait'
  210. $dompdf->setPaper('A4', 'portrait');
  211.  
  212. // Render the HTML as PDF
  213. $dompdf->render();
  214.  
  215.  
  216.  
  217. // Output the generated PDF to Browser (force download)
  218. $dompdf->stream("invoice_".$invoice->getNumber().".pdf", [
  219. "Attachment" => true
  220. // Attachment = true => download pdf file
  221. // Attachment = false => view pdf file
  222. ]);
  223.  
  224.  
  225. }
  226.  
  227. /**
  228. * @Route("/invoice/savepdf/{id}", name="invoice_save_pdf")
  229. */
  230. public function savePDF($id)
  231. {
  232. $em = $this->getDoctrine()->getManager();
  233. $invoice = $em->getRepository(Invoice::class)->find($id);
  234.  
  235. // Configure Dompdf according to your needs
  236. $pdfOptions = new Options();
  237. $pdfOptions->set('defaultFont', 'Arial');
  238.  
  239. // Instantiate Dompdf with our options
  240. $dompdf = new Dompdf($pdfOptions);
  241.  
  242. // Retrieve the HTML generated in our twig file
  243. $html = $this->renderView('@ElevenCRM/invoice/invoice_pdf.html.twig', [
  244. 'invoice' => $invoice
  245. ]);
  246.  
  247. // Load HTML to Dompdf
  248. $dompdf->loadHtml($html);
  249.  
  250. // (Optional) Setup the paper size and orientation 'portrait' or 'portrait'
  251. $dompdf->setPaper('A4', 'portrait');
  252.  
  253. // Render the HTML as PDF
  254. $dompdf->render();
  255.  
  256. // Store PDF Binary Data
  257. $output = $dompdf->output();
  258.  
  259. // In this case, we want to write the file in the public directory
  260. //$publicDirectory = $this->get('kernel')->getProjectDir() . '/public';
  261. $publicDirectory = '/var/www/vhosts/dev.host.nl/storage';
  262. // e.g /var/www/project/public/mypdf.pdf
  263. $pdfFilepath = $publicDirectory . '/mypdf.pdf';
  264.  
  265. // Write file to the desired path
  266. file_put_contents($pdfFilepath, $output);
  267.  
  268. // Send some text response
  269. return new Response("The PDF file has been succesfully generated !");
  270. }
  271.  
  272. /**
  273. * @Route("/invoice/openpdf/{id}", name="invoice_open_pdf")
  274. */
  275. public function openPDF($id) {
  276. $publicDirectory = '/var/www/vhosts/dev.host.nl/storage';
  277. $pdfFilepath = $publicDirectory . '/mypdf.pdf';
  278.  
  279. return new BinaryFileResponse($pdfFilepath);
  280. }
  281.  
  282.  
  283. public function getCustomerData() {
  284. return '';
  285. }
  286.  
  287. /**
  288. * @Route("/invoice/{id}/document", name="invoice_document")
  289. * @return JsonResponse
  290. */
  291. public function getDocumentView($id, Request $request, DocumentRepository $documents) {
  292.  
  293. $paginator = $documents->findLast('invoice', $id);
  294.  
  295. if ($request->isXmlHttpRequest()) {
  296. return new JsonResponse(array('message' => 'Success',
  297. 'output' => $this->renderView('@ElevenFramework/document/table_view.html.twig',
  298. array(
  299. 'id' => $id,
  300. 'table' => 'invoice',
  301. 'paginator' => $paginator,
  302. 'defaultRoute' => '/invoice/'.$id.'/document'
  303. ))
  304. ), 200);
  305. }
  306.  
  307. return new Response("XML Http Request Only.");
  308. }
  309.  
  310. }
  311.  
  312. //------------------------------------------------
  313.  
  314. <?php
  315.  
  316. namespace App\Eleven\CRMBundle\Entity;
  317.  
  318. //Symfony
  319. use Symfony\Component\Validator\Constraints as Assert;
  320. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  321. use Doctrine\ORM\Mapping as ORM;
  322. //use Symfony\Component\HttpFoundation\Session\Session;
  323. use Doctrine\Common\Collections\ArrayCollection;
  324. use Doctrine\Common\Collections\Collection;
  325.  
  326. // Eleven
  327. use App\Eleven\FrameworkBundle\Entity\CompanyAbstractEntity;
  328. use App\Eleven\CRMBundle\Entity\Customer;
  329. use App\Eleven\CRMBundle\Entity\InvoiceLine;
  330.  
  331. //use App\Eleven\FrameworkExampleBundle\Validator\Constraints as AcmeAssert;
  332. use App\Eleven\FrameworkBundle\Service\SettingManager;
  333.  
  334. /**
  335. * @ORM\Entity
  336. *
  337. * @UniqueEntity(
  338. * fields={"number"},
  339. * errorPath="number",
  340. * message="The invoice number has already been used."
  341. * )
  342. */
  343. class Invoice extends CompanyAbstractEntity {
  344.  
  345. protected $settingManager;
  346.  
  347. // https://stackoverflow.com/questions/9087585/generating-next-sequence-value-manually-in-doctrine-2
  348. // https://stackoverflow.com/questions/11079509/how-to-use-entitymanager-inside-entity?lq=1
  349.  
  350.  
  351. /**
  352. * Set default values.
  353. */
  354. public function __construct($number) {
  355. /*
  356. global $kernel;
  357. if ( 'AppCache' == get_class($kernel) ) {
  358. $kernel = $kernel->getKernel();
  359. }
  360. $em = $kernel->getContainer()->get( 'doctrine.orm.entity_manager' );
  361. $conn = $em->getConnection();
  362.  
  363. $sql = "SELECT nextval('invoice_id_seq')";
  364. $stmt = $conn->prepare($sql);
  365. $stmt->execute();
  366. $this->id = $stmt->fetchAll()[0]['nextval'];
  367. */
  368. //$this->id = 382;
  369.  
  370. $this->status = 'NEW';
  371. $this->date = new \DateTime();
  372. $this->invoiceLines = new ArrayCollection();
  373. }
  374.  
  375. /**
  376. * @ORM\Id()
  377. * @ORM\Column(type="integer")
  378. * @ORM\GeneratedValue()
  379. * _ORM\GeneratedValue(strategy="CUSTOM") //strategy="IDENTITY" // strategy="SEQUENCE"
  380. * @ORM\SequenceGenerator(sequenceName="invoice_id_seq", allocationSize=1, initialValue=800)
  381. * _ORM\CustomIdGenerator(class="App\Eleven\FrameworkBundle\Doctrine\Generator\NextvalIdGenerator")
  382. */
  383. protected $id;
  384.  
  385. /**
  386. * invoice number
  387. * @ORM\Column(type="integer" , nullable=true)
  388. */
  389. private $number;
  390.  
  391. /**
  392. * @ORM\Column(type="date")
  393. */
  394. private $date;
  395.  
  396. /**
  397. *
  398. * @ORM\ManyToOne(targetEntity="App\Eleven\CRMBundle\Entity\Customer")
  399. * @ORM\JoinColumn(nullable=true)
  400. */
  401. private $customer;
  402.  
  403. /**
  404. * @ORM\Column(type="string", length=100, nullable=true)
  405. */
  406. private $reference;
  407.  
  408. ///**
  409. // * @ORM\Column(type="boolean", nullable=true )
  410. // * @var boolean
  411. // */
  412. //private $sent;
  413.  
  414. /**
  415. * @ORM\Column(type="string", length=100, nullable=true )
  416. */
  417. private $status;
  418.  
  419.  
  420. /**
  421. * @ORM\Column(type="boolean", nullable=true )
  422. * @var boolean
  423. */
  424. private $proforma;
  425.  
  426. /**
  427. * @ORM\OneToMany(targetEntity="App\Eleven\CRMBundle\Entity\InvoiceLine", mappedBy="invoice", orphanRemoval=true, cascade={"persist"})
  428. * @ORM\JoinColumn(nullable=true)
  429. */
  430. private $invoiceLines;
  431.  
  432.  
  433.  
  434. public function getId() {
  435. return $this->id;
  436. }
  437. public function setId(?int $id) {
  438. $this->id = $id;
  439. return $this;
  440. }
  441.  
  442. public function getNumber(): ?string {
  443. return $this->number;
  444. }
  445.  
  446. public function setNumber(?string $number): self {
  447. $this->number = $number;
  448. return $this;
  449. }
  450.  
  451. public function getDate(): ?\DateTimeInterface {
  452. return $this->date;
  453. }
  454.  
  455. public function setDate(?\DateTimeInterface $date): self {
  456. $this->date = $date;
  457. return $this;
  458. }
  459.  
  460. public function getCustomer(): ?Customer {
  461. return $this->customer;
  462. }
  463.  
  464. public function setCustomer(?Customer $customer): self {
  465. $this->customer = $customer;
  466. return $this;
  467. }
  468.  
  469. public function getReference(): ?string {
  470. return $this->reference;
  471. }
  472.  
  473. public function setReference(?string $reference): self {
  474. $this->reference = $reference;
  475. return $this;
  476. }
  477.  
  478.  
  479. public function getStatus(): ?string {
  480. return $this->status;
  481. }
  482.  
  483. public function setStatus(?string $status): self {
  484. $this->status = $status;
  485. return $this;
  486. }
  487.  
  488. //TODO: fix to quotation
  489.  
  490. public function getProforma(): ?bool {
  491. return $this->proforma;
  492. }
  493.  
  494. public function setProforma(?bool $proforma): self {
  495. $this->proforma = $proforma;
  496. return $this;
  497. }
  498.  
  499.  
  500. /**
  501. * @return Collection|InvoiceLine[]
  502. */
  503. public function getInvoiceLines(): Collection {
  504. return $this->invoiceLines;
  505. }
  506.  
  507. public function addInvoiceLine(InvoiceLine $invoiceLine): self {
  508. if (!$this->invoiceLines->contains($invoiceLine)) {
  509. $this->invoiceLines[] = $invoiceLine;
  510. $invoiceLine->setInvoice($this);
  511. }
  512. return $this;
  513. }
  514.  
  515. public function removeInvoiceLine(InvoiceLine $invoiceLine): self {
  516. if ($this->invoiceLines->contains($invoiceLine)) {
  517. $this->invoiceLines->removeElement($invoiceLine);
  518.  
  519. // set the owning side to null (unless already changed)
  520. if ($invoiceLine->getInvoice() === $this) {
  521. $invoiceLine->setInvoice(null);
  522. }
  523. }
  524. return $this;
  525. }
  526.  
  527.  
  528. public function getTotalInvoiceLines() {
  529. return count($this->invoiceLines);
  530. }
  531.  
  532. public function getTotalAmount() {
  533. $totalAmount = 0;
  534. foreach ($this->invoiceLines as $invoiceLine) {
  535. $totalAmount += $invoiceLine->getAmount();
  536. }
  537. return $totalAmount;
  538. }
  539.  
  540. }
  541.  
Add Comment
Please, Sign In to add comment