src/Parcels/ExpenseController.php line 146

Open in your IDE?
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: parcel
  5. * Date: 11/1/18
  6. * Time: 8:09 AM
  7. */
  8. namespace App\Parcels;
  9. use App\Entity\DailyAccount;
  10. use App\Entity\Station;
  11. use App\Entity\StationAccount;
  12. use App\Entity\StationDailyAccount;
  13. use App\Entity\StationExpense;
  14. use App\Entity\TransactionStationExpense;
  15. use App\Form\ExpenseForm;
  16. use Doctrine\DBAL\Exception;
  17. use JMS\Serializer\SerializerBuilder;
  18. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  19. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  20. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  21. use Symfony\Component\HttpFoundation\Request;
  22. use Symfony\Component\HttpFoundation\Response;
  23. class ExpenseController extends AbstractController {
  24. /**
  25. * @Route("/expenses", name="expensesHomepage")
  26. */
  27. public function indexExpenseAction(Request $request) {
  28. $em = $this->getDoctrine()->getManager();
  29. $station = $em->getRepository(Station::class)->findOneBy([
  30. 'id' => $request->getSession()->get('STATION')
  31. ]);
  32. $expense = new StationExpense();
  33. $expense->setCreatedBy($this->getUser());
  34. $form = $this->createForm(ExpenseForm::class, $expense);
  35. $form->handleRequest($request);
  36. if($form->isSubmitted() && $form->isValid()) {
  37. $expense->setStation($station);
  38. $expense->setCreatedAt(new \DateTime());
  39. $expense->setCreatedBy($this->getUser());
  40. $conn = $em->getConnection();
  41. $conn->beginTransaction();
  42. try {
  43. $dailyAccount = $em->getRepository(DailyAccount::class)->findOneBy([
  44. 'user' => $this->getUser(),
  45. 'isClosed' => false
  46. ],['id' => 'DESC']);
  47. if(!$dailyAccount) {
  48. $this->addFlash('error', 'Please open a daily account');
  49. return $this->render('fos/parcels/expenses.html.twig',[
  50. 'form' => $form->createView(),
  51. ]);
  52. }
  53. $stationExpense = $em->getRepository(StationExpense::class)->findOneBy([
  54. 'station' => $station,
  55. 'dailyAccount' => $dailyAccount
  56. ],['id' => 'DESC']);
  57. // $dailyAccount->setExpenses($dailyAccount->getExpenses() + $expense->getAmount());
  58. $expenseBalance = $expense->getAmount();
  59. if($stationExpense) {
  60. $expenseBalance = $stationExpense->getExpenseAccrued();
  61. $expenseBalance = $expenseBalance + $expense->getAmount();
  62. }
  63. $expense->setExpenseAccrued($expenseBalance);
  64. $balance = $expense->getAmount();
  65. $stationAccount = $em->getRepository(StationAccount::class)->findOneBy([
  66. 'station' => $station
  67. ],['id' => 'DESC']);
  68. if($stationAccount) {
  69. $balance = $stationAccount->getBalance();
  70. $balance = $balance - $expense->getAmount();
  71. }
  72. $newStationAccount = new StationAccount();
  73. $newStationAccount->setCreatedAt(new \DateTime());
  74. $newStationAccount->setStation($station);
  75. $newStationAccount->setAmount($expense->getAmount());
  76. $newStationAccount->setBalance($balance);
  77. $newStationAccount->setExpenses($expense->getAmount());
  78. $newStationAccount->setDailyAccount($dailyAccount);
  79. $newStationAccount->setCreatedBy($this->getUser());
  80. $expense->setDailyAccount($dailyAccount);
  81. $em->persist($newStationAccount);
  82. $em->persist($expense);
  83. $em->flush();
  84. $em->getConnection()->commit();
  85. $this->addFlash('success', 'Expense Saved Successfully');
  86. return $this->redirectToRoute('expensesHomepage');
  87. }catch (\PDOException $e){
  88. $em->getConnection()->rollBack();
  89. $this->addFlash('error', 'An Error Occurred Please check whether every thing is filled');
  90. return $this->render('fos/parcels/expenses.html.twig',[
  91. 'form' => $form->createView(),
  92. ]);
  93. }
  94. }
  95. return $this->render('fos/parcels/expenses.html.twig',[
  96. 'form' => $form->createView()
  97. ]);
  98. }
  99. /**
  100. * @Method("POST")
  101. * @Route("/all/expenses-list", name="get_all_station_expenses_Parcels")
  102. */
  103. public function getAllParcels(Request $request) {
  104. $em = $this->getDoctrine()->getManager();
  105. $station = $em->getRepository(Station::class)->findOneBy([
  106. 'id' => $request->getSession()->get('STATION')
  107. ]);
  108. $serializer = SerializerBuilder::create()->build();
  109. $page = empty($request->request->get('page')) ? intval('page') : 1;
  110. $rows = empty($request->request->get('rows')) ? intval('rows') : 10;
  111. $offset = ($page-1)*$rows;
  112. $filterRules = $request->request->get('filterRules');
  113. $em = $this->getDoctrine()->getManager();
  114. $dailyAccount = $em->getRepository(DailyAccount::class)->findOneBy([
  115. 'user' => $this->getUser(),
  116. 'isClosed' => false
  117. ],['id' => 'DESC']);
  118. $expenses = null;
  119. $data = null;
  120. if($dailyAccount){
  121. $expenses = $em->getRepository(StationExpense::class)->findBy([
  122. 'station' => $station,
  123. 'dailyAccount' => $dailyAccount
  124. ]);
  125. $data = [
  126. 'rows' => $expenses,
  127. 'total' => count($expenses)
  128. ];
  129. }
  130. /*$balance = $em->getRepository(StationExpense::class)->findOneBy([
  131. 'station' => $station
  132. ],['id' => 'DESC']);*/
  133. $data = $serializer->serialize($data,'json');
  134. return new Response($data, Response::HTTP_OK);
  135. }
  136. /**
  137. * @Route("/station_expenses", name="expensesStationExpensesHomepage")
  138. */
  139. public function indexStationExpensesAction(Request $request) {
  140. $em = $this->getDoctrine()->getManager();
  141. $station = $em->getRepository(Station::class)->findOneBy([
  142. 'id' => $request->getSession()->get('STATION')
  143. ]);
  144. return $this->render('fos/parcels/station_expenses.html.twig',[]);
  145. }
  146. /**
  147. * @Method("POST")
  148. * @Route("/all/station_expenses_json", name="get_station_expenses_Parcels")
  149. */
  150. public function getAllStationExpenses(Request $request) {
  151. $em = $this->getDoctrine()->getManager();
  152. $station = $em->getRepository(Station::class)->findOneBy([
  153. 'id' => $request->getSession()->get('STATION')
  154. ]);
  155. $serializer = SerializerBuilder::create()->build();
  156. $page = !empty($request->request->get('page')) ? intval($request->request->get('page')) : 1;
  157. $rows = !empty($request->request->get('rows')) ? intval($request->request->get('rows')) : 10;
  158. $offset = ($page-1)*$rows;
  159. // $filterRules = $request->request->get('filterRules');
  160. // print_r($rows); die;
  161. $conn = $em->getConnection();
  162. $sql = "SELECT sa.account_date, count(a.id) as expenses, sum(a.amount) AS total, sa.id as station_daily_account_id, sa.station_id FROM station_expense a
  163. JOIN daily_account da on a.daily_account_id = da.id
  164. JOIN station_daily_account sa on da.station_daily_account = sa.id
  165. WHERE a.is_cancelled = false
  166. AND a.station_id = :station
  167. GROUP BY sa.id ORDER BY sa.id DESC LIMIT {$offset}, {$rows}" ;
  168. $sqlCount = "SELECT count(sa.id) as days FROM station_expense a
  169. JOIN daily_account da on a.daily_account_id = da.id
  170. JOIN station_daily_account sa on da.station_daily_account = sa.id
  171. WHERE a.is_cancelled = false
  172. AND a.station_id = :station GROUP BY sa.id" ;
  173. $expenses = null;
  174. $days = null;
  175. try {
  176. $stmt = $conn->prepare($sql);
  177. $stmt->execute([
  178. 'station'=> $station->getId()
  179. ]);
  180. $stmtCount = $conn->prepare($sqlCount);
  181. $stmtCount->execute([
  182. 'station'=> $station->getId()
  183. ]);
  184. $days = $stmtCount->fetchAll(\PDO::FETCH_ASSOC);
  185. // print_r($counts);die;
  186. $expenses = $stmt->fetchAll(\PDO::FETCH_ASSOC);
  187. //print_r($amounts);die;
  188. // return $stmt->fetchAll(\PDO::FETCH_ASSOC);
  189. } catch (Exception $e) {
  190. return null;
  191. }
  192. $data = [
  193. 'rows' => $expenses,
  194. 'total' => count($days)
  195. ];
  196. $data = $serializer->serialize($data,'json');
  197. return new Response($data, Response::HTTP_OK);
  198. }
  199. /**
  200. * @Route("/all/daily_expense_list/{dailyAccount}", name="get_daily_expenses_list")
  201. * @param $dailyAccount
  202. * @return Response
  203. */
  204. public function dailyExpenseList(StationDailyAccount $dailyAccount){
  205. $sql = "SELECT a.id, a.account_date, concat(p.first_name,' ',p.second_name,' ',p.sir_name)as nname,
  206. (SELECT sum(amount) FROM station_expense ase
  207. where daily_account_id = a.id AND is_cancelled = false) AS total_amount,
  208. (SELECT CONCAT(
  209. '[',GROUP_CONCAT(
  210. JSON_OBJECT(
  211. 'amount',se.amount,
  212. 'description', se.description,
  213. 'expense_name', et.expense_name
  214. )
  215. ), ']' )
  216. FROM station_expense se
  217. JOIN expense_type et on se.expense_type_id = et.id
  218. where daily_account_id = a.id AND se.is_cancelled = false
  219. ) AS expenses
  220. FROM daily_account a
  221. JOIN station_daily_account sda ON sda.id = a.station_daily_account
  222. JOIN user u ON u.id = a.user_id
  223. JOIN person p ON p.id = u.person_id
  224. WHERE sda.id = :station_daily_account_id";
  225. try {
  226. $em = $this->getDoctrine()->getManager();
  227. $conn = $em->getConnection();
  228. $stmt = $conn->prepare($sql);
  229. $stmt->execute([
  230. 'station_daily_account_id'=> $dailyAccount->getId()
  231. ]);
  232. $expenses = $stmt->fetchAll(\PDO::FETCH_ASSOC);
  233. return $this->render('fos/parcels/station_expense.html.twig',[
  234. 'daily_accounts' => $expenses,
  235. 'station_account' => $dailyAccount
  236. ]);
  237. } catch (DBALException $e) {
  238. dump($e); die;
  239. }
  240. return $this->render('fos/parcels/station_expense.html.twig',[]);
  241. }
  242. }