src/Controller/Parcels/StationDailyAccountController.php line 207

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