src/Controller/Admin/QuotesCrudController.php line 93

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use App\Entity\Quotes;
  6. use App\Entity\QuoteItems;
  7. use App\Entity\QuoteLines;
  8. use App\Entity\Attachment;
  9. use App\Service\Settings;
  10. use App\Entity\PdfFiles;
  11. use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
  12. use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
  13. use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
  14. use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
  15. use EasyCorp\Bundle\EasyAdminBundle\Field\MoneyField;
  16. use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
  17. use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
  18. use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
  19. use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
  20. use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
  21. use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;
  22. use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
  23. use EasyCorp\Bundle\EasyAdminBundle\Field\TelephoneField;
  24. use EasyCorp\Bundle\EasyAdminBundle\Field\NumberField;
  25. use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField;
  26. use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
  27. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  28. use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
  29. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  30. use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
  31. use App\Form\Type\QuoteItemType;
  32. use App\Form\Type\QuoteWorkItemType;
  33. use App\Form\Type\AttachmentType;
  34. use App\Field\FilesField;
  35. use App\Field\ListField;
  36. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  37. use Doctrine\ORM\EntityManagerInterface;
  38. use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore;
  39. use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
  40. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  41. use Symfony\Component\Form\FormInterface;
  42. use EasyCorp\Bundle\EasyAdminBundle\Config\Filters;
  43. use Knp\Bundle\SnappyBundle\Snappy\Response\PdfResponse;
  44. use Knp\Snappy\Pdf;
  45. use Twig\Environment;
  46. class QuotesCrudController extends AbstractCrudController
  47. {
  48. public $files_download_path;
  49. private $pdf;
  50. private $params;
  51. private $settings;
  52. private $twig;
  53. public static function getEntityFqcn(): string
  54. {
  55. return Quotes::class;
  56. }
  57. public function __construct(ContainerBagInterface $params,Pdf $knpSnappyPdf, Settings $settings, Environment $twig)
  58. {
  59. $this->files_download_path = $params->get('app.files.download.path');
  60. $this->pdf= $knpSnappyPdf;
  61. $this->settings = $settings;
  62. $this->params = $params;
  63. $this->twig = $twig;
  64. }
  65. /**
  66. * Called before rendering create entity form
  67. * modify entity and call parent method
  68. */
  69. public function createNewForm(EntityDto $entityDto, KeyValueStore $formOptions, AdminContext $context): FormInterface
  70. {
  71. //$tax = $this->settings->get('tax')*100;
  72. $tax = 27;
  73. $entityDto->getInstance()->setTax($tax);
  74. if(isset($_GET['plateNumber'])){
  75. $car = $this->getDoctrine()->getRepository('App:Cars')->findOneBy(['plateNumber'=>$_GET['plateNumber']]);
  76. $entityDto->getInstance()->setCar($car);
  77. }
  78. return parent::createNewForm($entityDto,$formOptions,$context);
  79. }
  80. public function configureFields(string $pageName): iterable
  81. {
  82. $req = $this->get('request_stack');
  83. $em = $this->getDoctrine()->getManager();
  84. $user = $this->getUser();
  85. $quote = $em->getRepository(Quotes::class)->findOneBy(['id'=>$req->getCurrentRequest()->query->get('entityId')]);
  86. $this->quote = $quote;
  87. if(!$quote){
  88. $quote = new Quotes();
  89. }
  90. yield FormField::addPanel('Alapadatok')->addCssClass('col-sm-6');
  91. //yield AssociationField::new('customer');
  92. yield TextField::new('customerName','Ajánlatkérő neve');
  93. yield DateField::new('created','Létrehozva')->hideOnForm();
  94. yield TextField::new('customerAddress','Ajánlatkérő címe');
  95. yield TelephoneField::new('customerPhone','Ajánlatkérő telefonszám');
  96. yield EmailField::new('customerMail','Ajánlatkérő email');
  97. yield ChoiceField::new('discovery', 'Kapcsolatfelvétel')->setChoices(array_flip(Quotes::$discoveries));
  98. yield ChoiceField::new('status')->setChoices(array_flip(Quotes::$statuses));
  99. yield FormField::addPanel('Összesen')->addCssClass('col-sm-6');
  100. yield MoneyField::new('price_sum','Nettó összeg')->setCssClass('font-bold')->setCurrency('HUF')->setNumDecimals(0)->setStoredAsCents(false);
  101. yield TextField::new('tax')->hideOnIndex()->setCssClass('font-bold');
  102. yield MoneyField::new('price_sum_br','Bruttó összeg')->setCssClass('font-bold')->setCurrency('HUF')->setNumDecimals(0)->setStoredAsCents(false);
  103. yield FormField::addPanel('Adatok')->addCssClass('col-sm-12');
  104. yield TextField::new('title','Ajánlat tárgya');
  105. if($pageName == Crud::PAGE_DETAIL){
  106. yield TextField::new('originalText','Eredeti szöveg')->hideOnIndex()->renderAsHtml();
  107. }else{
  108. yield TextEditorField::new('originalText','Eredeti szöveg')->hideOnIndex();
  109. }
  110. yield TextEditorField::new('shortDesc','Munka összefoglaló');
  111. if($pageName == Crud::PAGE_DETAIL){
  112. yield TextField::new('sentMail','Küldött levél')->hideOnIndex()->renderAsHtml();
  113. }else{
  114. yield TextEditorField::new('sentMail','Küldött levél')->hideOnIndex();
  115. }
  116. yield TextEditorField::new('note');
  117. yield FormField::addPanel('Munkák/javítások')->addCssClass('col-sm-6');
  118. if($pageName == Crud::PAGE_DETAIL){
  119. yield ListField::new('quoteBodyguardItems','Lakatos munka')->hideOnIndex();
  120. }else{
  121. yield MoneyField::new('price_bodyguard','Lakatos óradíj')->setCurrency('HUF')->setNumDecimals(0)->setStoredAsCents(false)->hideOnIndex();
  122. yield CollectionField::new('quoteBodyguardItems','Lakatos munka')->onlyOnForms()->setEntryType(QuoteWorkItemType::class)->addCssClass('hide-legend');
  123. }
  124. yield FormField::addPanel('Egyéb költségek/anyagköltség')->addCssClass('col-sm-6');;
  125. if($pageName == Crud::PAGE_DETAIL){
  126. yield ListField::new('quoteOtherItems','Alkatrész/egyéb','material')->hideOnIndex();
  127. }else{
  128. yield CollectionField::new('quoteOtherItems','Alkatrész/egyéb')->onlyOnForms()->setEntryType(QuoteItemType::class)->addJsFiles('js/collection.js')->addCssClass('hide-legend');
  129. }
  130. }
  131. public function configureActions(Actions $actions): Actions
  132. {
  133. $actions->add(Crud::PAGE_INDEX, Action::DETAIL);
  134. $actions->add(Crud::PAGE_EDIT, Action::DETAIL);
  135. //$actions->remove(Crud::PAGE_INDEX, Action::DELETE);
  136. $actionReportPrint = Action::new('ReportPrint', 'Nyomtatás', 'fa fa-print')->linkToCrudAction('actionReportPrint')->setHtmlAttributes(['target'=>'_blank'])->addCssClass('btn btn-primary');
  137. $actionReportPrintSave = Action::new('ReportPrintSave', 'PDF Mentés', 'fa fa-file-pdf')->linkToCrudAction('actionReportPrintSave')->setHtmlAttributes(['target'=>'_blank'])->addCssClass('btn btn-primary');
  138. $actions->add(Crud::PAGE_DETAIL, $actionReportPrint);
  139. $actions->add(Crud::PAGE_DETAIL, $actionReportPrintSave);
  140. $actionMakeQuote = Action::new('MakeQuote', 'Ajánlat készítése', 'fa fa-edit')->linkToCrudAction('actionMakeQuote')->setHtmlAttributes(['target'=>'_blank'])->addCssClass('');
  141. $actions->add(Crud::PAGE_DETAIL, $actionMakeQuote);
  142. $actions->add(Crud::PAGE_INDEX, $actionMakeQuote);
  143. return $actions;
  144. }
  145. /**
  146. * Init crud
  147. */
  148. public function configureCrud(Crud $crud): Crud
  149. {
  150. return $crud
  151. ->setEntityLabelInSingular('Quote')
  152. ->setDefaultSort(['id' => 'DESC', 'created'=>'DESC'])
  153. // ->setSearchFields(['car.plateNumber', 'car.manufacturer', 'car.type'])
  154. //->setPaginatorPageSize($this->settings->get('pageSize')?$this->settings->get('pageSize'):20)
  155. ;
  156. }
  157. public function updateEntity(EntityManagerInterface $entityManager, $entityInstance): void
  158. {
  159. $now = new \DateTime();
  160. $sum = 0;
  161. foreach ($entityInstance->getQuoteItems() as $key => $value) {
  162. $sum += $value->getPriceSum();
  163. }
  164. $entityInstance->setPriceSum($sum);
  165. if($entityInstance->getTax() == null){
  166. $tax = 27;
  167. }else{
  168. $tax = $entityInstance->getTax();
  169. }
  170. $tx = (intval($tax) / 100 )+1;
  171. foreach ($entityInstance->getQuoteItems()->getValues() as $key => $value) {
  172. switch ($value->getType()) {
  173. case QuoteItems::PAINT:
  174. $price = $entityInstance->getPricePaint();
  175. $value->setPriceQty($price);
  176. break;
  177. case QuoteItems::BODYGUARD:
  178. $price = $entityInstance->getPriceBodyguard();
  179. $value->setPriceQty($price);
  180. break;
  181. case QuoteItems::ASSEMBLY:
  182. $price = $entityInstance->getPriceAssembly();
  183. $value->setPriceQty($price);
  184. break;
  185. }
  186. }
  187. $entityInstance->setPriceSumBr(intval($sum*$tx));
  188. $entityManager->persist($entityInstance);
  189. $entityManager->flush();
  190. }
  191. /**
  192. * configure filter fields
  193. */
  194. public function configureFilters(Filters $filters): Filters
  195. {
  196. return $filters
  197. // ->add('car')
  198. // ->add('note')
  199. ;
  200. }
  201. public function actionMakeQuote(Request $request){
  202. $quote = $this->getDoctrine()->getRepository(Quotes::class)->findOneBy(['id'=>$request->query->get('entityId')]);
  203. $quoteLines = $this->getDoctrine()->getRepository(QuoteLines::class)->findAll();
  204. if(isset($_POST['quote'])){
  205. $quote->setSentMail($_POST['quote']);
  206. $em = $this->getDoctrine()->getManager();
  207. $em->persist($quote);
  208. $em->flush();
  209. }
  210. if(isset($_POST['sendMail']) && $_POST['sendMail'] == 'true'){
  211. $this->sendQuoteMail($quote);
  212. }
  213. return $this->render('quote/make.html.twig',[
  214. 'quote'=>$quote,
  215. 'quoteLines' => $quoteLines,
  216. ]);
  217. }
  218. public function sendQuoteMail($quote){
  219. if($this->params->get('app.mailer')){
  220. $transport = new \Swift_SmtpTransport('mail.gortech.hu', 465)->setStreamOptions([
  221. 'ssl' => [
  222. 'verify_peer' => false,
  223. 'verify_peer_name' => false,
  224. 'allow_self_signed' => true,
  225. ],
  226. ]);
  227. //$transport = new \Swift_SmtpTransport($this->params->get('app.mailer.host'), $this->params->get('app.mailer.port'));
  228. $transport->setUsername($this->params->get('app.mailer.user'))->setPassword($this->params->get('app.mailer.pass'))->setEncryption($this->params->get('app.mailer.sec'));
  229. //dd($transport);
  230. $mailer = new \Swift_Mailer($transport);
  231. $message = (new \Swift_Message('Ajánlat '));
  232. $message->setFrom('info@gortech.hu');
  233. $message->setTo($quote->getCustomerMail());
  234. //$message->setTo('info@microservice.hu');
  235. $message->setBody(
  236. $this->twig->render('emails/quote_mail.html.twig',[
  237. 'quote' => $quote,
  238. ])
  239. ,'text/html'
  240. );
  241. if($mailer->send($message)){
  242. $quote->setUpdated(new \DateTime());
  243. $quote->setSentDate(new \DateTime());
  244. $quote->setSentMail($_POST['quote']);
  245. $quote->setStatus(2);
  246. $em = $this->getDoctrine()->getManager();
  247. $em->persist($quote);
  248. $em->flush();
  249. }
  250. }
  251. }
  252. public function actionReportPrintSave(Request $request){
  253. $em = $this->getDoctrine()->getManager();
  254. $repair = $this->getDoctrine()->getRepository(Quotes::class)->findOneBy(['id'=>$request->query->get('entityId')]);
  255. $html = $this->renderView('quote/quote_pdf.html.twig',[
  256. 'type'=>'pdf',
  257. 'entity' => $repair,
  258. 'settings' => $this->settings,
  259. 'base_dir' => $this->params->get('kernel.project_dir').'/public',
  260. 'base_image_dir' => $this->params->get('kernel.project_dir').$this->params->get('app.files.download.path')
  261. ]);
  262. $name = $repair->getCar()->getPlateNumber()."-".$repair->getId()."-".time().".pdf";
  263. $this->pdf->generateFromHtml($html,'pdf/'.$name);
  264. $pdfFile = new PdfFiles();
  265. $pdfFile->setCar($repair->getCar());
  266. $pdfFile->setFilepath($name);
  267. $pdfFile->setType('Munkalap');
  268. $em->persist($pdfFile);
  269. $em->flush();
  270. return new PdfResponse(
  271. $this->pdf->getOutputFromHtml($html),
  272. $name
  273. );
  274. }
  275. public function actionReportPrint(Request $request){
  276. $em = $this->getDoctrine()->getManager();
  277. $repair = $this->getDoctrine()->getRepository(Quotes::class)->findOneBy(['id'=>$request->query->get('entityId')]);
  278. // $t = $this->getDoctrine()->getRepository(Settings::class)->findOneBy(['name'=>'company']);
  279. $html = $this->renderView('quote/quote_pdf.html.twig',[
  280. 'type'=>'pdf',
  281. 'entity' => $repair,
  282. 'settings' => $this->settings,
  283. 'base_dir' => $this->params->get('kernel.project_dir').'/public',
  284. 'base_image_dir' => $this->params->get('kernel.project_dir').$this->params->get('app.files.download.path')
  285. ]);
  286. $name = $repair->getCar()->getPlateNumber()."-".$repair->getId()."-".time().".pdf";
  287. //return $this->render('repair/report_pdf.html.twig',['type'=>'pdf','entity' => $repair, 'settings' => $this->settings, 'base_dir' => __DIR__.'/../../../public'] );
  288. $this->pdf->generateFromHtml($html,'pdf/'.$name);
  289. return $this->render('page/show_pdf.html.twig',[
  290. 'path' => 'pdf/'.$name,
  291. 'base_dir' => $this->params->get('kernel.project_dir').'/public',
  292. 'type'=>'pdf'
  293. ]);
  294. }
  295. }