src/ApplicationBundle/Modules/Accounts/Controller/RecurringBillingController.php line 19

Open in your IDE?
  1. <?php
  2. namespace ApplicationBundle\Modules\Accounts\Controller;
  3. use ApplicationBundle\Controller\GenericController;
  4. use ApplicationBundle\Interfaces\SessionCheckInterface;
  5. use ApplicationBundle\Modules\Authentication\Constants\UserConstants;
  6. use ApplicationBundle\Modules\Accounts\Service\RecurringBillingManager;
  7. use ApplicationBundle\Modules\Inventory\Inventory;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. /**
  11.  * Recurring billing management — list + add/edit recurring expense / sales-invoice
  12.  * templates, toggle, delete, run-now. Thin controller over RecurringBillingManager.
  13.  */
  14. class RecurringBillingController extends GenericController implements SessionCheckInterface
  15. {
  16.     public function listAction(Request $request)
  17.     {
  18.         $em  $this->getDoctrine()->getManager();
  19.         $mgr = new RecurringBillingManager($em);
  20.         $conn $em->getConnection();
  21.         $heads $conn->fetchAllAssociative(
  22.             "SELECT accounts_head_id AS id, name FROM acc_accounts_head
  23.               WHERE (lock_flag = 0 OR lock_flag IS NULL) ORDER BY name"
  24.         );
  25.         $salesOrders $conn->fetchAllAssociative(
  26.             "SELECT so.sales_order_id AS id, so.document_hash AS hash, c.client_name AS client
  27.                FROM sales_order so
  28.                LEFT JOIN acc_clients c ON c.client_id = so.client_id
  29.               WHERE so.approved = 1
  30.               ORDER BY so.sales_order_id DESC LIMIT 500"
  31.         );
  32.         return $this->render('@Accounts/pages/views/recurring_billing.html.twig', [
  33.             'schedules'    => $mgr->listSchedules(),
  34.             'heads'        => $heads,
  35.             'salesOrders'  => $salesOrders,
  36.             'currencyList' => Inventory::CurrencyList($em),
  37.             'sidebar_partial' => '@Accounts/inc/_finance_sidebar.html.twig',
  38.             'cp_active'    => 'recurring',
  39.         ]);
  40.     }
  41.     public function saveAction(Request $request): JsonResponse
  42.     {
  43.         $loginId $request->getSession()->get(UserConstants::USER_LOGIN_ID);
  44.         $em  $this->getDoctrine()->getManager();
  45.         $mgr = new RecurringBillingManager($em);
  46.         $s $mgr->saveSchedule($request->request->all(), $loginId);
  47.         return new JsonResponse(['success' => true'id' => $s->getId()]);
  48.     }
  49.     public function toggleAction(Request $request$id): JsonResponse
  50.     {
  51.         $em $this->getDoctrine()->getManager();
  52.         $s $em->getRepository('ApplicationBundle\\Entity\\RecurringSchedule')->findOneBy(['id' => (int) $id]);
  53.         if (!$s) { return new JsonResponse(['success' => false'message' => 'Not found']); }
  54.         $s->setEnabled($s->getEnabled() ? 1);
  55.         $s->setUpdatedAt(new \DateTime('now'));
  56.         $em->flush();
  57.         return new JsonResponse(['success' => true'enabled' => $s->getEnabled()]);
  58.     }
  59.     public function deleteAction(Request $request$id): JsonResponse
  60.     {
  61.         $em $this->getDoctrine()->getManager();
  62.         $em->getConnection()->executeStatement('DELETE FROM recurring_schedule WHERE id = ?', [(int) $id]);
  63.         return new JsonResponse(['success' => true]);
  64.     }
  65.     public function runNowAction(Request $request$id): JsonResponse
  66.     {
  67.         $loginId $request->getSession()->get(UserConstants::USER_LOGIN_ID);
  68.         $em  $this->getDoctrine()->getManager();
  69.         $mgr = new RecurringBillingManager($em);
  70.         $s $mgr->getSchedule($id);
  71.         if (!$s) { return new JsonResponse(['success' => false'message' => 'Not found']); }
  72.         $dry = (int) $request->request->get('dry'0) === 1;
  73.         $result $mgr->runSchedule($s, new \DateTime('now'), $loginId$dry);
  74.         return new JsonResponse(['success' => true'result' => $result]);
  75.     }
  76. }