<?php
namespace App\Parcels;
use App\Entity\UserStation;
use App\Entity\TransactionExpense;
use App\Entity\WayBill;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Validator\Constraints\NotBlank;
class DefaultController extends AbstractController
{
private ObjectManager $em;
public function __construct(ManagerRegistry $managerRegistry)
{
$this->em = $managerRegistry->getManager();
}
/**
* @Route("/", name="parcels_homepage")
*/
public function indexAction(Request $request){
$em = $this->em;
/** @var UserStation $userTown */
$userTown = $em->getRepository(UserStation::class)->findOneBy([
'user' => $this->getUser(),
'isActive' => true
], ['id'=>'DESC']);
$form = $this->searchWaybill();
$form->handleRequest($request);
if($form->isSubmitted()){
$wayb = $form->getData()['waybill'];
$waybill = $em->getRepository(WayBill::class)->findOneBy([
'id' => $wayb
]);
if(!$waybill){
$form->get('waybill')->addError(new FormError(
"waybill {$wayb} does not exist"
));
}
if($form->isValid()){
// continue here
return $this->redirectToRoute('one_way_bill',['id'=>$wayb]);
}
}
// replace this example code with whatever you need
return $this->render('fos/home/home.html.twig', [
'user_town' => $userTown,
'form' => $form->createView()
]);
}
public function nav() {
return $this->render('fos/nav.html.twig', []);
}
/**
* @Route("/add-expense", name="parcels_add_expense")
*/
public function addExpense(){
$em = $this->getDoctrine()->getManager();
$transaction = $em->getRepository(Transaction::class)->findOneBy([
'wayBill' => 23
]);
$expenseType = $em->getRepository('App:ExpenseType')->findOneBy([
'id' => 3
]);
$autoExpenses = $em->getRepository('App:ExpenseType')->findBy([
'isActive' => true,
'isAutomatic' => true
]);
$wayBillExpense = new TransactionExpense();
$wayBillExpense->setCreatedAt(new \DateTime());
$wayBillExpense->setCreatedBy($this->getUser());
$wayBillExpense->setTransaction($transaction);
$wayBillExpense->setAmount(50);
$wayBillExpense->setExpenseType($expenseType);
$em->persist($wayBillExpense);
/*foreach ($autoExpenses as $item) {
if($item->getAutoExpense()) {
if($item->getAutoExpense()->getisSystem()) {
dump($item->getAutoExpense());
$wayBillExpense = new TransactionExpense();
$wayBillExpense->setCreatedAt(new \DateTime());
$wayBillExpense->setCreatedBy($this->getUser());
$wayBillExpense->setTransaction($transaction);
$wayBillExpense->setAmount($item->getAutoExpense()->getAmount());
$wayBillExpense->setExpenseType($item);
$em->persist($wayBillExpense);
}
}
}*/
$em->flush();
/** @var UserStation $userTown */
$userTown = $em->getRepository(UserStation::class)->findOneBy([
'user' => $this->getUser(),
'isActive' => true
], ['id'=>'DESC']);
// replace this example code with whatever you need
return $this->render('fos/home/home.html.twig', [
'user_town' => $userTown
]);
}
private function searchWaybill() {
return $this->createFormBuilder()
->add('waybill',TextType::class,[
'constraints' =>[
new NotBlank(['message' => 'Please enter a waybill'])
]
])
->setMethod('POST')
->getForm();
}
/**
* @Route("/download_app", name="download_app")
*/
public function downloadApp(){
// load the file from the filesystem
$file = new File("../public/assets/android-app/app-nenocourier.apk");
return $this->file($file);
}
}