<?php
/**
* Created by PhpStorm.
* User: parcel
* Date: 11/29/18
* Time: 11:22 AM
*/
namespace App\Controller\Parcels;
use App\Entity\DailyAccount;
use App\Entity\MpesaTransaction;
use App\Entity\Person;
use App\Entity\Station;
use App\Entity\StationDailyAccount;
use App\Entity\StationExpense;
use App\Entity\Transaction;
use App\Entity\User;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\View\View;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\SerializerBuilder;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
class StationDailyAccountController extends AbstractController {
/**
* @Route("/daily_accounts", name="daily_accounts")
*/
public function transactionAction() {
// replace this example code with whatever you need
return $this->render('parcels/daily_accounts.html.twig',[]);
}
/**
*
* @Route("/daily_accounts/{id}", name="daily_station_account")
*/
public function indexAction($id){
$em = $this->getDoctrine()->getManager();
$stationDailyAccount = $em->getRepository(StationDailyAccount::class)->findOneBy([
'id' => $id
]);
if(!$stationDailyAccount) {
return $this->render('', [
]);
}
//
// /** @var DailyAccount[] $dailyAccounts */
// $dailyAccounts = $em->getRepository(DailyAccount::class)->findBy([
// 'stationDailyAccount' => $stationDailyAccount
// ]);
$dailyAccount = $em->getRepository(DailyAccount::class)->findOneAccount($stationDailyAccount->getId());
// dump($dailyAccount); die;
return $this->render('bos/daily_accounts/account_detail.html.twig', [
'station_daily' => $dailyAccount
]);
}
/**
*
* @Route("/daily_accounts/view/{id}", name="daily_station_view_account")
*/
public function dailyAccountIndex($id){
$em = $this->getDoctrine()->getManager();
$stationDailyAccount = $em->getRepository(StationDailyAccount::class)->findOneBy([
'id' => $id
]);
if(!$stationDailyAccount) {
return $this->render('', [
]);
}
/** @var DailyAccount[] $dailyAccounts */
$dailyAccounts = $em->getRepository(DailyAccount::class)->findBy([
'stationDailyAccount' => $stationDailyAccount
]);
$dailyAccount = $em->getRepository(DailyAccount::class)->findOneStationAccount($stationDailyAccount->getId());
return $this->render('bos/daily_accounts/view_daily_account.html.twig', [
'station_daily' => $dailyAccount
]);
}
/**
* @Route("/daily_accounts/view/user/{id}", name="daily_station_view_user_daily_account")
*/
public function userDailyAccount($id){
$em = $this->getDoctrine()->getManager();
/** @var Transaction[] $transactions */
$transactions = $em->getRepository(Transaction::class)->findUserDailyTransaction($id);
$transactionCount = count($transactions);
// dump($transactions); die;
$total = 0;
$mpesaTotal = 0;
$cashTotal = 0;
foreach ($transactions as $index => $transaction) {
if(!$transaction['is_cancelled']){
$total += $transaction['amount'];
$mpesaTotal += $transaction['mpesaAmount'];
$cashTotal += $transaction['cashAmount'];
}
}
return $this->render('bos/daily_accounts/view_user_daily_account.html.twig', [
'transactions' => $transactions,
'total' => $total,
'cashTotal' => $cashTotal,
'mpesaTotal' => $mpesaTotal,
'transactionCount' => $transactionCount,
'transactions_json' => json_encode($transactions),
'user_id' => $id
]);
}
/**
* @Route("/daily_accounts/view/user/{id}/mpesa/{transaction_id}", name="daily_station_view_user_daily_account_mpesa")
*/
public function userDailyAccountMpesa($id,$transaction_id){
$em = $em = $this->getDoctrine()->getManager();
$transaction = $em->getRepository(Transaction::class)->findOneBy([
'id' => $transaction_id
]);
$mpesa = null;
if($transaction->getPaymentMethod() == 'MPESA' || $transaction->getPaymentMethod() == 'MPESA_CASH'){
$mpesa = $em->getRepository(MpesaTransaction::class)->findOneBy([
'transaction' => $transaction_id
]);
}
return $this->render('bos/daily_accounts/mpesa/mpesa_transaction.html.twig', [
'mpesa' => $mpesa,
'transaction' => $transaction
]);
}
/**
*
* @Route("/daily_accounts/view/{id}/expenses", name="daily_station_view_account_expenses")
*/
public function dailyAccountExpenses($id){
$em = $this->getDoctrine()->getManager();
/** @var DailyAccount $dailyAccount */
$dailyAccount = $em->getRepository(DailyAccount::class)->findOneBy([
'id' => $id
]);
/** @var StationExpense[] $expenses */
$expenses = $em->getRepository(StationExpense::class)->findBy([
'dailyAccount' => $dailyAccount,
'isCancelled' => false
]);
$total = 0;
foreach ($expenses as $index => $expense) {
$total = $total + $expense->getAmount();
}
return $this->render('bos/daily_accounts/expenses/account_expense.html.twig', [
'expenses' => $expenses,
'expensesCount' => count($expenses),
'total' => $total
]);
}
/**
* @Route("/daily_accounts/view/json/{id}", methods={"POST"}, name="daily_station_view_account_json")
*/
public function daily($id) {
$em = $this->getDoctrine()->getManager();
$context = new SerializationContext();
$context->setSerializeNull(true);
$serializer = SerializerBuilder::create()->build();
$stationDailyAccount = $em->getRepository(StationDailyAccount::class)->findOneBy([
'id' => $id
]);
if(!$stationDailyAccount) {
return $this->render('', [
]);
}
$stationAccount = $em->getRepository(DailyAccount::class)->findOneStationAccount($stationDailyAccount->getId());
/** @var DailyAccount[] $dailyAccounts */
$dailyAccounts = $em->getRepository(DailyAccount::class)->findEveryDailyAccounts($stationDailyAccount->getId());
// dump($dailyAccounts);die;
// $dailyAccount = $em->getRepository(DailyAccount::class)->findOneAccount($stationDailyAccount->getId());
$data = [
'total' => count($dailyAccounts),
'rows' => $dailyAccounts,
'footer' => $stationAccount
];
$data = $serializer->serialize($data,'json', $context);
return new Response($data, Response::HTTP_OK);
}
/**
* @Route("/json/daily_accounts", methods={"POST"}, name="daily_accounts_json")
*/
public function dailyTransactions(Request $request) {
$em = $this->getDoctrine()->getManager();
$context = new SerializationContext();
$context->setSerializeNull(true);
$serializer = SerializerBuilder::create()->build();
$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');
$fromDate = $request->request->get("from_date");
$toDate = $request->request->get("to_date");
$fDate = null;
$date = new \DateTime('+1 day');
$tDate = null;
// dump($fDate);
// dump($tDate); die;
$fromDate = empty($fromDate) ? $fDate: $fromDate;
$toDate = empty($toDate) ? $tDate: $toDate;
if($fromDate && $toDate){
$filterRules = json_decode($filterRules, true);
$dates = [
'field'=> 'a.account_date',
'op'=> 'between',
'value' => [
'from' => str_replace(" ","", $fromDate),
'to' => str_replace(" ","", $toDate)
],
];
array_push($filterRules, $dates);
$filterRules = json_encode($filterRules);
}
$accounts = $em->getRepository(DailyAccount::class)->findAllDailyAccounts($filterRules, $offset, $rows);
$totals = $em->getRepository(DailyAccount::class)->findAllTotals($filterRules);
// $dailyAccount =
// dump($totals);die;
/*$dailyAccount = new DailyAccount();
$stationAccount = new StationDailyAccount();
$station = new Station();
$user = new User();
$person = new Person();
$person->setFirstName('');
$person->setSecondName('');
$person->setSirName('');
$user->setPerson($person);
$station->setStationName('');
$stationAccount->setStation($station);
$dailyAccount->setStationDailyAccount($stationAccount);
$dailyAccount->setUser($user);
$dailyAccount->setTransactionsNo(number_format($totals[0]['transactions']));
$dailyAccount->setAmount(number_format($totals[0]['amount']));
$dailyAccount->setExpenses(number_format($totals[0]['expenses']));*/
// $total = $totals[0]['total'];
// $footerTotals = array();
// array_push($footerTotals, $dailyAccount);
$data = [
'total' => $totals['total_accounts'],
'rows' => $accounts,
];
$data = $serializer->serialize($data,'json', $context);
return new Response($data, Response::HTTP_OK);
}
/**
* @Route("/daily_", name="daily_")
* @param Request $request
* @throws \Mpdf\MpdfException
*/
public function dailyAccountReport(Request $request){
$post = $request->request;
$dates = $request->request->get('date_times');
if(!$dates){
return $this->redirectToRoute('daily_accounts');
}
$dates = explode('-', $dates);
$date_1 = trim($dates[0]);
$date_2 = trim($dates[1]);
$days = date_diff(date_create($date_1), date_create($date_2))->format("%R%a")+1;
$data = [
'message' => 'the date difference must be at least a week',
'date_1' => $date_1,
'date_2' => $date_2,
'dif' => $days
];
$data = json_encode($data);
if((int)$days < 7){
return new Response(
$data, Response::HTTP_EXPECTATION_FAILED);
}
$starting = date('Y-m-d', strtotime($date_1));
$ending = date('Y-m-d',strtotime($date_2));
$em = $this->getDoctrine()->getManager();
$sqlSet = "SET SESSION group_concat_max_len = 1000000; ";
$stmt = $em->getConnection()->prepare($sqlSet);
$stmt->execute();
$sql = " SELECT c.id, a.station_daily_account, c.station_name, ANY_VALUE(a.user_id) as parcel_no,
ANY_VALUE(a.parcel_no) as parcel_no,
ANY_VALUE(a.account_date) as account_date, sum(a.amount) as amount, sum(a.expenses) AS expenses,
(sum(a.amount) - sum(a.expenses)) as net_income,
(SELECT CONCAT('[',
GROUP_CONCAT(
JSON_OBJECT(
'user', concat(p.first_name,' ',p.second_name,' ',p.sir_name),
'account_id', da.id,
'gross_income', da.amount,
'total_expenses', da.expenses,
'net_income', (da.amount - da.expenses)
)
),']') FROM daily_account da
JOIN user u on da.user_id = u.id
JOIN person p on u.person_id = p.id
WHERE da.station_daily_account = a.station_daily_account) AS user_accounts,
(SELECT CONCAT(
'[', GROUP_CONCAT(
JSON_OBJECT(
'user_id', daa.user_id,
'user', concat(paa.first_name,' ',paa.second_name,' ',paa.sir_name),
'details', (SELECT CONCAT('[',
GROUP_CONCAT(
JSON_OBJECT(
'expense', et.id,
'expense_type', et.expense_name,
'description', se0.description,
'amount', se0.amount,
'daily_account', daa.id
)
),']'
) FROM station_expense se0
JOIN daily_account da0 ON da0.id = se0.daily_account_id
JOIN expense_type et on se0.expense_type_id = et.id
WHERE se0.daily_account_id = daa.id and se0.created_by = da0.user_id)
)
), ']') FROM daily_account daa
JOIN user uaa ON uaa.id = daa.user_id
JOIN person paa on uaa.person_id = paa.id
WHERE daa.station_daily_account = a.station_daily_account AND daa.expenses > 0) AS detail_expenses
FROM station_daily_account b
JOIN daily_account a ON b.id = a.station_daily_account
JOIN station c ON c.id = b.station_id
WHERE b.account_date BETWEEN CAST('{$starting}' AS DATE) AND CAST('{$ending}' AS DATE) GROUP BY a.station_daily_account
ORDER BY c.id";
$stmt = $em->getConnection()->prepare($sql);
$stmt->execute();
$stations = $stmt->fetchAll();
/** @var User $user */
$user = $this->getUser();
$person = $user->getPerson();
$user = $person->getFirstName().' '.$person->getSecondName().' '.$person->getSirName();
$fromDate = strtotime($starting);
$toDate = strtotime($ending);
$dates = [
'from' =>[
's' => strtolower(date('S', $fromDate)),
'm' => date('M', $fromDate),
'day' => date('d', $fromDate),
'year' => date('Y', $fromDate)
],
'to' => [
's' => date('S', $toDate),
'm' => date('M', $toDate),
'day' => date('d', $toDate),
'year' => date('Y', $toDate)
]
];
$footer = '<table width="100%">
<tr>
<td align="left" style="text-transform: uppercase; font-size: 8px; font-family: "Amaranth", sans-serif">
Printed by: <b style="color: gray;"><b>'.strtoupper($user).'</b></b>
'.date('l F d Y').'
</td>
<td align="right" style="color: #000; text-decoration:italic; font-size:8px;">
<span style=" font-size: 8px; color: green; font-family: "Amaranth", sans-serif">
powered by ohau technologies
</span>
<b style="color: #000;font-size:10px;"> {PAGENO}/{nb}</b>
</td>
</tr>
</table>';
$header = '
<table width="100%"
style=" border-bottom: 2px solid red;
vertical-align: top;
height:200px;
font-family: serif;
font-size: 9pt;
color: blue;">
<tr>
<td style="width: 20%;">
<img src="assets/img/courier.jpg" width="120px" height="100px" />
</td>
<td align="center">
<div style="font-size:16pt;">
<h4>NENO COURIER SERVICES LTD</h4>
</div>
<div style="font-size:12pt; ">
P.O BOX 660-60100
</div>
<div style="font-size:14pt; ">
EMBU
</div>
<div style="font-size:12pt; text-transform: lowercase;">
nenocourier@gmail.com
</div>
</td>
<td style="width:20%;">
</td>
</tr>
</table>
';
// dump($stations); die;
/*$context = new SerializationContext();
$context->setSerializeNull(true);
//
////
////
$serializer = SerializerBuilder::create()->build();
////
$data = $serializer->serialize($stations,'json', $context);*/
// $JSONStations = json_encode($stations, JSON_UNESCAPED_SLASHES);
// return new Response($data);
// dump($stations);
// die;
// dump($stations); die;
// $new_date = date('S j F Y', strtotime('2018-11-22'));
// dump($new_date); die;
// return $this->render('report.html.twig', [
// 'stations' => $stations
// ]);
$html = $this->render('report.html.twig', array(
'stations' => $stations,
'dates' => $dates
))->getContent();
$mpdf = new Mpdf([
'mode' => 'utf-8',
'orientation' => 'P',
'margin_top' => 50,
'margin_footer' => 20,
'tempDir' => $this->getParameter('kernel.project_dir').'/var/mpdf_temp'
]);
// epos300
$mpdf->SetHeader($header);
$mpdf->SetFooter($footer);
$mpdf->WriteHTML($html);
$mpdf->Output('daily_account_report.pdf', 'D');
// return $this->render('report.html.twig', [
// 'stations' => $stations
// ]);
}
}