<?php
/**
* Created by PhpStorm.
* User: parcel
* Date: 10/19/18
* Time: 11:09 AM
*/
namespace App\Parcels;
use App\Entity\DailyAccount;
use App\Entity\Station;
use App\Entity\Transaction;
use App\Entity\WayBill;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\SerializerBuilder;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class ParcelTransactions extends AbstractController {
/**
* @Route("/transactions", name="daily_parcel_transactions")
*/
public function dailyParcelTransactions() {
return $this->render('fos/parcels/daily_transaction.html.twig', []);
}
/**
* @Route("/transactions/transaction-list",methods={"POST", "GET"}, name="get_all_transactions")
*/
public function dailyTransactions(Request $request) {
$em = $this->getDoctrine()->getManager();
$context = new SerializationContext();
$context->setSerializeNull(true);
$serializer = SerializerBuilder::create()->build();
if(!$request->getSession()->get('STATION')) {
$data = [
'error' => 'User is not well registered'
];
$data = $serializer->serialize($data,'json', $context);
return new Response($data, Response::HTTP_OK);
}
$user = $this->getUser();
$station_id = $request->getSession()->get('STATION');
$page = $request->request->get('page') > 1 ? $request->request->get('page'): 1;
$rows = $request->request->get('rows') > 1 ? $request->request->get('rows'): 20;
$offset = ($page - 1)*$rows;
$filterRules = $request->request->get('filterRules');
$parcels = $em->getRepository(Transaction::class)->getTransactions($user->getId(),$station_id, $filterRules, $offset,$rows);
// dump($parcels);die;
/** @var integer $total */
$total = $em->getRepository(Transaction::class)->getTotalTransactions($user->getId(),$station_id, $filterRules);
$transactionDetails = $em->getRepository(Transaction::class)->fosTotals($user->getId(),$station_id, $filterRules);
// dump($total['rows']); die;
$dailyAccount = new DailyAccount();
$transaction = new Transaction();
$transaction->setAmount(number_format($transactionDetails['amount']));
$transaction->setTaxAmount(number_format($transactionDetails['tax_amount']));
$transaction->setDailyAccount($dailyAccount);
$transaction->setStationExpenses(number_format($transactionDetails['station_expenses']));
$transaction->setBalance(number_format($transactionDetails['balance']));
$wayBill = new WayBill();
$wayBill->setPercelCount(number_format($transactionDetails['parcel_count']));
$wayBill->setReceiverPhoneNumber('Total');
$wayBill->setFromStation(new Station());
$wayBill->setToStation(new Station());
$transaction->setWayBill($wayBill);
$footerTotals = array();
array_push($footerTotals, $transaction);
$data = [
'total' => $total['rows'],
'rows' => $parcels,
'footer' => $footerTotals
];
dump($footerTotals);
// $data = $serializer->serialize($data,'json', $context);
return new JsonResponse($data, Response::HTTP_OK);
}
/**
* @Method("get")
* @Route("/transactions/details/{id}", name="one_transaction_detail_parcel")
*/
public function getDetailParcel($id){
$em = $this->getDoctrine()->getManager();
$transaction = $em->getRepository(Transaction::class)->findOneBy([
'wayBill' => $id
]);
return $this->render('fos/transactions/transaction_detail.html.twig',[
'transaction' => $transaction
]);
}
}