src/Controller/Parcels/StationDailyAccountController.php line 49

Open in your IDE?
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: parcel
  5. * Date: 11/29/18
  6. * Time: 11:22 AM
  7. */
  8. namespace App\Controller\Parcels;
  9. use App\Entity\DailyAccount;
  10. use App\Entity\MpesaTransaction;
  11. use App\Entity\Person;
  12. use App\Entity\Station;
  13. use App\Entity\StationDailyAccount;
  14. use App\Entity\StationExpense;
  15. use App\Entity\Transaction;
  16. use App\Entity\User;
  17. use FOS\RestBundle\Controller\Annotations as Rest;
  18. use FOS\RestBundle\View\View;
  19. use JMS\Serializer\SerializationContext;
  20. use JMS\Serializer\SerializerBuilder;
  21. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. use Symfony\Component\VarDumper\Dumper\HtmlDumper;
  26. class StationDailyAccountController extends AbstractController {
  27. /**
  28. * @Route("/daily_accounts", name="daily_accounts")
  29. */
  30. public function transactionAction() {
  31. // replace this example code with whatever you need
  32. return $this->render('parcels/daily_accounts.html.twig',[]);
  33. }
  34. /**
  35. *
  36. * @Route("/daily_accounts/{id}", name="daily_station_account")
  37. */
  38. public function indexAction($id){
  39. $em = $this->getDoctrine()->getManager();
  40. $stationDailyAccount = $em->getRepository(StationDailyAccount::class)->findOneBy([
  41. 'id' => $id
  42. ]);
  43. if(!$stationDailyAccount) {
  44. return $this->render('', [
  45. ]);
  46. }
  47. //
  48. // /** @var DailyAccount[] $dailyAccounts */
  49. // $dailyAccounts = $em->getRepository(DailyAccount::class)->findBy([
  50. // 'stationDailyAccount' => $stationDailyAccount
  51. // ]);
  52. $dailyAccount = $em->getRepository(DailyAccount::class)->findOneAccount($stationDailyAccount->getId());
  53. $expenses = $em->getRepository(DailyAccount::class)->findDailyExpenses($stationDailyAccount->getId());
  54. dump($expenses);
  55. // dump($dailyAccount); die;
  56. return $this->render('bos/daily_accounts/account_detail.html.twig', [
  57. 'station_daily' => $dailyAccount,
  58. 'expenses' => $expenses
  59. ]);
  60. }
  61. /**
  62. *
  63. * @Route("/daily_accounts/view/{id}", name="daily_station_view_account")
  64. */
  65. public function dailyAccountIndex($id){
  66. $em = $this->getDoctrine()->getManager();
  67. $stationDailyAccount = $em->getRepository(StationDailyAccount::class)->findOneBy([
  68. 'id' => $id
  69. ]);
  70. if(!$stationDailyAccount) {
  71. return $this->render('', [
  72. ]);
  73. }
  74. /** @var DailyAccount[] $dailyAccounts */
  75. $dailyAccounts = $em->getRepository(DailyAccount::class)->findBy([
  76. 'stationDailyAccount' => $stationDailyAccount
  77. ]);
  78. $dailyAccount = $em->getRepository(DailyAccount::class)->findOneStationAccount($stationDailyAccount->getId());
  79. return $this->render('bos/daily_accounts/view_daily_account.html.twig', [
  80. 'station_daily' => $dailyAccount
  81. ]);
  82. }
  83. /**
  84. * @Route("/daily_accounts/view/user/{id}", name="daily_station_view_user_daily_account")
  85. */
  86. public function userDailyAccount($id){
  87. $em = $this->getDoctrine()->getManager();
  88. /** @var Transaction[] $transactions */
  89. $transactions = $em->getRepository(Transaction::class)->findUserDailyTransaction($id);
  90. $transactionCount = count($transactions);
  91. // dump($transactions); die;
  92. $total = 0;
  93. $mpesaTotal = 0;
  94. $cashTotal = 0;
  95. foreach ($transactions as $index => $transaction) {
  96. if(!$transaction['is_cancelled']){
  97. $total += $transaction['amount'];
  98. $mpesaTotal += $transaction['mpesaAmount'];
  99. $cashTotal += $transaction['cashAmount'];
  100. }
  101. }
  102. return $this->render('bos/daily_accounts/view_user_daily_account.html.twig', [
  103. 'transactions' => $transactions,
  104. 'total' => $total,
  105. 'cashTotal' => $cashTotal,
  106. 'mpesaTotal' => $mpesaTotal,
  107. 'transactionCount' => $transactionCount,
  108. 'transactions_json' => json_encode($transactions),
  109. 'user_id' => $id
  110. ]);
  111. }
  112. /**
  113. * @Route("/daily_accounts/view/user/{id}/mpesa/{transaction_id}", name="daily_station_view_user_daily_account_mpesa")
  114. */
  115. public function userDailyAccountMpesa($id,$transaction_id){
  116. $em = $em = $this->getDoctrine()->getManager();
  117. $transaction = $em->getRepository(Transaction::class)->findOneBy([
  118. 'id' => $transaction_id
  119. ]);
  120. $mpesa = null;
  121. if($transaction->getPaymentMethod() == 'MPESA' || $transaction->getPaymentMethod() == 'MPESA_CASH'){
  122. $mpesa = $em->getRepository(MpesaTransaction::class)->findOneBy([
  123. 'transaction' => $transaction_id
  124. ]);
  125. }
  126. return $this->render('bos/daily_accounts/mpesa/mpesa_transaction.html.twig', [
  127. 'mpesa' => $mpesa,
  128. 'transaction' => $transaction
  129. ]);
  130. }
  131. /**
  132. *
  133. * @Route("/daily_accounts/view/{id}/expenses", name="daily_station_view_account_expenses")
  134. */
  135. public function dailyAccountExpenses($id){
  136. $em = $this->getDoctrine()->getManager();
  137. /** @var DailyAccount $dailyAccount */
  138. $dailyAccount = $em->getRepository(DailyAccount::class)->findOneBy([
  139. 'id' => $id
  140. ]);
  141. /** @var StationExpense[] $expenses */
  142. $expenses = $em->getRepository(StationExpense::class)->findBy([
  143. 'dailyAccount' => $dailyAccount,
  144. 'isCancelled' => false
  145. ]);
  146. $total = 0;
  147. foreach ($expenses as $index => $expense) {
  148. $total = $total + $expense->getAmount();
  149. }
  150. return $this->render('bos/daily_accounts/expenses/account_expense.html.twig', [
  151. 'expenses' => $expenses,
  152. 'expensesCount' => count($expenses),
  153. 'total' => $total
  154. ]);
  155. }
  156. /**
  157. * @Route("/daily_accounts/view/json/{id}", methods={"POST"}, name="daily_station_view_account_json")
  158. */
  159. public function daily($id) {
  160. $em = $this->getDoctrine()->getManager();
  161. $context = new SerializationContext();
  162. $context->setSerializeNull(true);
  163. $serializer = SerializerBuilder::create()->build();
  164. $stationDailyAccount = $em->getRepository(StationDailyAccount::class)->findOneBy([
  165. 'id' => $id
  166. ]);
  167. if(!$stationDailyAccount) {
  168. return $this->render('', [
  169. ]);
  170. }
  171. $stationAccount = $em->getRepository(DailyAccount::class)->findOneStationAccount($stationDailyAccount->getId());
  172. /** @var DailyAccount[] $dailyAccounts */
  173. $dailyAccounts = $em->getRepository(DailyAccount::class)->findEveryDailyAccounts($stationDailyAccount->getId());
  174. // dump($dailyAccounts);die;
  175. // $dailyAccount = $em->getRepository(DailyAccount::class)->findOneAccount($stationDailyAccount->getId());
  176. $data = [
  177. 'total' => count($dailyAccounts),
  178. 'rows' => $dailyAccounts,
  179. 'footer' => $stationAccount
  180. ];
  181. $data = $serializer->serialize($data,'json', $context);
  182. return new Response($data, Response::HTTP_OK);
  183. }
  184. /**
  185. * @Route("/json/daily_accounts", methods={"POST"}, name="daily_accounts_json")
  186. */
  187. public function dailyTransactions(Request $request) {
  188. $em = $this->getDoctrine()->getManager();
  189. $context = new SerializationContext();
  190. $context->setSerializeNull(true);
  191. $serializer = SerializerBuilder::create()->build();
  192. $page = $request->request->get('page') > 1 ? $request->request->get('page'): 1;
  193. $rows = $request->request->get('rows') > 1 ? $request->request->get('rows'): 20;
  194. $offset = ($page - 1)*$rows;
  195. $filterRules = $request->request->get('filterRules');
  196. $fromDate = $request->request->get("from_date");
  197. $toDate = $request->request->get("to_date");
  198. $fDate = null;
  199. $date = new \DateTime('+1 day');
  200. $tDate = null;
  201. // dump($fDate);
  202. // dump($tDate); die;
  203. $fromDate = empty($fromDate) ? $fDate: $fromDate;
  204. $toDate = empty($toDate) ? $tDate: $toDate;
  205. if($fromDate && $toDate){
  206. $filterRules = json_decode($filterRules, true);
  207. $dates = [
  208. 'field'=> 'a.account_date',
  209. 'op'=> 'between',
  210. 'value' => [
  211. 'from' => str_replace(" ","", $fromDate),
  212. 'to' => str_replace(" ","", $toDate)
  213. ],
  214. ];
  215. array_push($filterRules, $dates);
  216. $filterRules = json_encode($filterRules);
  217. }
  218. $accounts = $em->getRepository(DailyAccount::class)->findAllDailyAccounts($filterRules, $offset, $rows);
  219. $totals = $em->getRepository(DailyAccount::class)->findAllTotals($filterRules);
  220. // $dailyAccount =
  221. // dump($totals);die;
  222. /*$dailyAccount = new DailyAccount();
  223. $stationAccount = new StationDailyAccount();
  224. $station = new Station();
  225. $user = new User();
  226. $person = new Person();
  227. $person->setFirstName('');
  228. $person->setSecondName('');
  229. $person->setSirName('');
  230. $user->setPerson($person);
  231. $station->setStationName('');
  232. $stationAccount->setStation($station);
  233. $dailyAccount->setStationDailyAccount($stationAccount);
  234. $dailyAccount->setUser($user);
  235. $dailyAccount->setTransactionsNo(number_format($totals[0]['transactions']));
  236. $dailyAccount->setAmount(number_format($totals[0]['amount']));
  237. $dailyAccount->setExpenses(number_format($totals[0]['expenses']));*/
  238. // $total = $totals[0]['total'];
  239. // $footerTotals = array();
  240. // array_push($footerTotals, $dailyAccount);
  241. $data = [
  242. 'total' => $totals['total_accounts'],
  243. 'rows' => $accounts,
  244. ];
  245. $data = $serializer->serialize($data,'json', $context);
  246. return new Response($data, Response::HTTP_OK);
  247. }
  248. /**
  249. * @Route("/daily_", name="daily_")
  250. * @param Request $request
  251. * @throws \Mpdf\MpdfException
  252. */
  253. public function dailyAccountReport(Request $request){
  254. $post = $request->request;
  255. $dates = $request->request->get('date_times');
  256. if(!$dates){
  257. return $this->redirectToRoute('daily_accounts');
  258. }
  259. $dates = explode('-', $dates);
  260. $date_1 = trim($dates[0]);
  261. $date_2 = trim($dates[1]);
  262. $days = date_diff(date_create($date_1), date_create($date_2))->format("%R%a")+1;
  263. $data = [
  264. 'message' => 'the date difference must be at least a week',
  265. 'date_1' => $date_1,
  266. 'date_2' => $date_2,
  267. 'dif' => $days
  268. ];
  269. $data = json_encode($data);
  270. if((int)$days < 7){
  271. return new Response(
  272. $data, Response::HTTP_EXPECTATION_FAILED);
  273. }
  274. $starting = date('Y-m-d', strtotime($date_1));
  275. $ending = date('Y-m-d',strtotime($date_2));
  276. $em = $this->getDoctrine()->getManager();
  277. $sqlSet = "SET SESSION group_concat_max_len = 1000000; ";
  278. $stmt = $em->getConnection()->prepare($sqlSet);
  279. $stmt->execute();
  280. $sql = " SELECT c.id, a.station_daily_account, c.station_name, ANY_VALUE(a.user_id) as parcel_no,
  281. ANY_VALUE(a.parcel_no) as parcel_no,
  282. ANY_VALUE(a.account_date) as account_date, sum(a.amount) as amount, sum(a.expenses) AS expenses,
  283. (sum(a.amount) - sum(a.expenses)) as net_income,
  284. (SELECT CONCAT('[',
  285. GROUP_CONCAT(
  286. JSON_OBJECT(
  287. 'user', concat(p.first_name,' ',p.second_name,' ',p.sir_name),
  288. 'account_id', da.id,
  289. 'gross_income', da.amount,
  290. 'total_expenses', da.expenses,
  291. 'net_income', (da.amount - da.expenses)
  292. )
  293. ),']') FROM daily_account da
  294. JOIN user u on da.user_id = u.id
  295. JOIN person p on u.person_id = p.id
  296. WHERE da.station_daily_account = a.station_daily_account) AS user_accounts,
  297. (SELECT CONCAT(
  298. '[', GROUP_CONCAT(
  299. JSON_OBJECT(
  300. 'user_id', daa.user_id,
  301. 'user', concat(paa.first_name,' ',paa.second_name,' ',paa.sir_name),
  302. 'details', (SELECT CONCAT('[',
  303. GROUP_CONCAT(
  304. JSON_OBJECT(
  305. 'expense', et.id,
  306. 'expense_type', et.expense_name,
  307. 'description', se0.description,
  308. 'amount', se0.amount,
  309. 'daily_account', daa.id
  310. )
  311. ),']'
  312. ) FROM station_expense se0
  313. JOIN daily_account da0 ON da0.id = se0.daily_account_id
  314. JOIN expense_type et on se0.expense_type_id = et.id
  315. WHERE se0.daily_account_id = daa.id and se0.created_by = da0.user_id)
  316. )
  317. ), ']') FROM daily_account daa
  318. JOIN user uaa ON uaa.id = daa.user_id
  319. JOIN person paa on uaa.person_id = paa.id
  320. WHERE daa.station_daily_account = a.station_daily_account AND daa.expenses > 0) AS detail_expenses
  321. FROM station_daily_account b
  322. JOIN daily_account a ON b.id = a.station_daily_account
  323. JOIN station c ON c.id = b.station_id
  324. WHERE b.account_date BETWEEN CAST('{$starting}' AS DATE) AND CAST('{$ending}' AS DATE) GROUP BY a.station_daily_account
  325. ORDER BY c.id";
  326. $stmt = $em->getConnection()->prepare($sql);
  327. $stmt->execute();
  328. $stations = $stmt->fetchAll();
  329. /** @var User $user */
  330. $user = $this->getUser();
  331. $person = $user->getPerson();
  332. $user = $person->getFirstName().' '.$person->getSecondName().' '.$person->getSirName();
  333. $fromDate = strtotime($starting);
  334. $toDate = strtotime($ending);
  335. $dates = [
  336. 'from' =>[
  337. 's' => strtolower(date('S', $fromDate)),
  338. 'm' => date('M', $fromDate),
  339. 'day' => date('d', $fromDate),
  340. 'year' => date('Y', $fromDate)
  341. ],
  342. 'to' => [
  343. 's' => date('S', $toDate),
  344. 'm' => date('M', $toDate),
  345. 'day' => date('d', $toDate),
  346. 'year' => date('Y', $toDate)
  347. ]
  348. ];
  349. $footer = '<table width="100%">
  350. <tr>
  351. <td align="left" style="text-transform: uppercase; font-size: 8px; font-family: "Amaranth", sans-serif">
  352. Printed by: <b style="color: gray;"><b>'.strtoupper($user).'</b></b>
  353. '.date('l F d Y').'
  354. </td>
  355. <td align="right" style="color: #000; text-decoration:italic; font-size:8px;">
  356. <span style=" font-size: 8px; color: green; font-family: "Amaranth", sans-serif">
  357. powered by ohau technologies
  358. </span>
  359. <b style="color: #000;font-size:10px;">&nbsp; {PAGENO}/{nb}</b>
  360. </td>
  361. </tr>
  362. </table>';
  363. $header = '
  364. <table width="100%"
  365. style=" border-bottom: 2px solid red;
  366. vertical-align: top;
  367. height:200px;
  368. font-family: serif;
  369. font-size: 9pt;
  370. color: blue;">
  371. <tr>
  372. <td style="width: 20%;">
  373. <img src="assets/img/courier.jpg" width="120px" height="100px" />
  374. </td>
  375. <td align="center">
  376. <div style="font-size:16pt;">
  377. <h4>NENO COURIER SERVICES LTD</h4>
  378. </div>
  379. <div style="font-size:12pt; ">
  380. P.O BOX 660-60100
  381. </div>
  382. <div style="font-size:14pt; ">
  383. EMBU
  384. </div>
  385. <div style="font-size:12pt; text-transform: lowercase;">
  386. nenocourier@gmail.com
  387. </div>
  388. </td>
  389. <td style="width:20%;">
  390. </td>
  391. </tr>
  392. </table>
  393. ';
  394. // dump($stations); die;
  395. /*$context = new SerializationContext();
  396. $context->setSerializeNull(true);
  397. //
  398. ////
  399. ////
  400. $serializer = SerializerBuilder::create()->build();
  401. ////
  402. $data = $serializer->serialize($stations,'json', $context);*/
  403. // $JSONStations = json_encode($stations, JSON_UNESCAPED_SLASHES);
  404. // return new Response($data);
  405. // dump($stations);
  406. // die;
  407. // dump($stations); die;
  408. // $new_date = date('S j F Y', strtotime('2018-11-22'));
  409. // dump($new_date); die;
  410. // return $this->render('report.html.twig', [
  411. // 'stations' => $stations
  412. // ]);
  413. $html = $this->render('report.html.twig', array(
  414. 'stations' => $stations,
  415. 'dates' => $dates
  416. ))->getContent();
  417. $mpdf = new Mpdf([
  418. 'mode' => 'utf-8',
  419. 'orientation' => 'P',
  420. 'margin_top' => 50,
  421. 'margin_footer' => 20,
  422. 'tempDir' => $this->getParameter('kernel.project_dir').'/var/mpdf_temp'
  423. ]);
  424. // epos300
  425. $mpdf->SetHeader($header);
  426. $mpdf->SetFooter($footer);
  427. $mpdf->WriteHTML($html);
  428. $mpdf->Output('daily_account_report.pdf', 'D');
  429. // return $this->render('report.html.twig', [
  430. // 'stations' => $stations
  431. // ]);
  432. }
  433. }