src/Parcels/DefaultController.php line 73

Open in your IDE?
  1. <?php
  2. namespace App\Parcels;
  3. use App\Entity\UserStation;
  4. use App\Entity\TransactionExpense;
  5. use App\Entity\WayBill;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Doctrine\Persistence\ObjectManager;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\Form\Extension\Core\Type\TextType;
  10. use Symfony\Component\Form\FormError;
  11. use Symfony\Component\HttpFoundation\File\File;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Symfony\Component\Validator\Constraints\NotBlank;
  15. class DefaultController extends AbstractController
  16. {
  17.     private ObjectManager $em;
  18.     public function __construct(ManagerRegistry $managerRegistry)
  19.     {
  20.         $this->em $managerRegistry->getManager();
  21.     }
  22.     /**
  23.      * @Route("/", name="parcels_homepage")
  24.      */
  25.     public function indexAction(Request $request){
  26.         $em $this->em;
  27.         /** @var UserStation $userTown */
  28.         $userTown $em->getRepository(UserStation::class)->findOneBy([
  29.             'user' => $this->getUser(),
  30.             'isActive' => true
  31.         ], ['id'=>'DESC']);
  32.         $form $this->searchWaybill();
  33.         $form->handleRequest($request);
  34.         if($form->isSubmitted()){
  35.             $wayb $form->getData()['waybill'];
  36.             $waybill $em->getRepository(WayBill::class)->findOneBy([
  37.                 'id' => $wayb
  38.             ]);
  39.             if(!$waybill){
  40.                 $form->get('waybill')->addError(new FormError(
  41.                     "waybill {$wayb} does not exist"
  42.                 ));
  43.             }
  44.             if($form->isValid()){
  45.                 // continue here
  46.                 return $this->redirectToRoute('one_way_bill',['id'=>$wayb]);
  47.             }
  48.         }
  49.         // replace this example code with whatever you need
  50.         return $this->render('fos/home/home.html.twig', [
  51.             'user_town' => $userTown,
  52.             'form' => $form->createView()
  53.         ]);
  54.     }
  55.     public function nav() {
  56.         return $this->render('fos/nav.html.twig', []);
  57.     }
  58.     /**
  59.      * @Route("/add-expense", name="parcels_add_expense")
  60.      */
  61.     public function addExpense(){
  62.         $em $this->getDoctrine()->getManager();
  63.         $transaction $em->getRepository(Transaction::class)->findOneBy([
  64.             'wayBill' => 23
  65.         ]);
  66.         $expenseType $em->getRepository('App:ExpenseType')->findOneBy([
  67.             'id' => 3
  68.         ]);
  69.         $autoExpenses $em->getRepository('App:ExpenseType')->findBy([
  70.             'isActive' => true,
  71.             'isAutomatic' => true
  72.         ]);
  73.         $wayBillExpense = new TransactionExpense();
  74.         $wayBillExpense->setCreatedAt(new \DateTime());
  75.         $wayBillExpense->setCreatedBy($this->getUser());
  76.         $wayBillExpense->setTransaction($transaction);
  77.         $wayBillExpense->setAmount(50);
  78.         $wayBillExpense->setExpenseType($expenseType);
  79.         $em->persist($wayBillExpense);
  80.         /*foreach ($autoExpenses as $item) {
  81.             if($item->getAutoExpense()) {
  82.                 if($item->getAutoExpense()->getisSystem()) {
  83.                     dump($item->getAutoExpense());
  84.                     $wayBillExpense = new TransactionExpense();
  85.                     $wayBillExpense->setCreatedAt(new \DateTime());
  86.                     $wayBillExpense->setCreatedBy($this->getUser());
  87.                     $wayBillExpense->setTransaction($transaction);
  88.                     $wayBillExpense->setAmount($item->getAutoExpense()->getAmount());
  89.                     $wayBillExpense->setExpenseType($item);
  90.                     $em->persist($wayBillExpense);
  91.                 }
  92.             }
  93.         }*/
  94.         $em->flush();
  95.         /** @var UserStation $userTown */
  96.         $userTown $em->getRepository(UserStation::class)->findOneBy([
  97.             'user' => $this->getUser(),
  98.             'isActive' => true
  99.         ], ['id'=>'DESC']);
  100.         // replace this example code with whatever you need
  101.         return $this->render('fos/home/home.html.twig', [
  102.             'user_town' => $userTown
  103.         ]);
  104.     }
  105.     private function searchWaybill() {
  106.         return $this->createFormBuilder()
  107.             ->add('waybill',TextType::class,[
  108.                 'constraints' =>[
  109.                     new NotBlank(['message' => 'Please enter a waybill'])
  110.                 ]
  111.             ])
  112.             ->setMethod('POST')
  113.             ->getForm();
  114.     }
  115.     /**
  116.      * @Route("/download_app", name="download_app")
  117.      */
  118.     public function downloadApp(){
  119.         // load the file from the filesystem
  120.         $file = new File("../public/assets/android-app/app-nenocourier.apk");
  121.         return $this->file($file);
  122.     }
  123. }