src/Controller/DefaultController.php line 116

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\DailyAccount;
  4. use App\Entity\Sms;
  5. use App\Entity\Station;
  6. use App\Entity\Transaction;
  7. use App\Messages\SMSFestiveMessage;
  8. use App\Messages\SMSMessage;
  9. use App\Service\ActionMatrix;
  10. use App\Service\SMSSender;
  11. use Doctrine\DBAL\Driver\PDOException;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use Doctrine\Persistence\ObjectManager;
  14. use JMS\Serializer\SerializationContext;
  15. use JMS\Serializer\SerializerBuilder;
  16. use Psr\Log\LoggerInterface;
  17. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
  18. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\HttpFoundation\Response;
  21. use Symfony\Component\Messenger\MessageBusInterface;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. use Symfony\Contracts\HttpClient\HttpClientInterface;
  24. class DefaultController extends AbstractController {
  25. private MessageBusInterface $messageBus;
  26. private ObjectManager $em;
  27. private ManagerRegistry $managerRegistry;
  28. private LoggerInterface $logger;
  29. private HttpClientInterface $httpClient;
  30. public function __construct(LoggerInterface $logger,HttpClientInterface $httpClient,MessageBusInterface $messageBus, ManagerRegistry $managerRegistry)
  31. {
  32. $this->messageBus = $messageBus;
  33. $this->em = $managerRegistry->getManager();
  34. $this->managerRegistry = $managerRegistry;
  35. $this->logger = $logger;
  36. $this->httpClient = $httpClient;
  37. }
  38. /**
  39. * @Route("/", name="homepage")
  40. */
  41. public function indexAction(Request $request) {
  42. // $this->messageBus->dispatch(new SMSFestiveMessage());
  43. // replace this example code with whatever you need
  44. $em = $this->getDoctrine()->getManager();
  45. $station = $request->getSession()->get('STATION');
  46. // $date = new \DateTime("-11days");
  47. $date = new \DateTime();
  48. $date = $date->format('Y-m-d');
  49. /** @var DailyAccount $dailyAccount */
  50. $dailyAccount = $em->getRepository(DailyAccount::class)->getDashTotals($date);
  51. // dump($dailyAccount); die;
  52. $transactions = $dailyAccount['transactions'];
  53. $amount = $dailyAccount['amount'];
  54. $expenses = $dailyAccount['expenses'];
  55. return $this->render('dashboard/dash.html.twig',[
  56. 'transactions' => $transactions,
  57. 'amount' => $amount,
  58. 'expenses' => $expenses
  59. ]);
  60. }
  61. /**
  62. * @Route("/sign-up", name="new-sacco")
  63. */
  64. public function createSacco(Request $request) {
  65. }
  66. /**
  67. * @Route("/sms_test", name="new_sms_tester")
  68. */
  69. public function testSMS(Request $request) {
  70. // TODO: Implement __invoke() method.
  71. $sms = $this->em->getRepository(Sms::class)->findOneBy([
  72. 'id' => 1530641
  73. ]);
  74. $sms->setRecepient("+254716308459");
  75. $smsSender = new SMSSender($this->logger, $this->httpClient);
  76. // $smsSender->sendSms("254716308459", "hello");
  77. $k = $smsSender->sendSms($sms);
  78. // $k= json_encode($k);
  79. $this->em->flush();
  80. return new Response($sms->getMessageId());
  81. }
  82. /**
  83. * @Route("/barcode", name="create_barcode")
  84. */
  85. public function createBarcode() {
  86. // For demonstration purposes, get pararameters that are passed in through $_GET or set to the default value
  87. $filepath = (isset($_GET["filepath"])?$_GET["filepath"]:"");
  88. $text = (isset($_GET["text"])?$_GET["text"]:"0");
  89. $size = (isset($_GET["size"])?$_GET["size"]:"20");
  90. $orientation = (isset($_GET["orientation"])?$_GET["orientation"]:"horizontal");
  91. $code_type = (isset($_GET["codetype"])?$_GET["codetype"]:"code128");
  92. $print = (isset($_GET["print"])&&$_GET["print"]=='true'?true:false);
  93. $sizefactor = (isset($_GET["sizefactor"])?$_GET["sizefactor"]:"1");
  94. // This function call can be copied into your project and can be made from anywhere in your code
  95. return new Response($this->barcode( $filepath, $text, $size, $orientation, $code_type, $print, $sizefactor ));
  96. }
  97. function barcode($filepath="", $text="0", $size="20", $orientation="horizontal", $code_type="code128", $print=false, $SizeFactor=1 ) {
  98. $code_string = "";
  99. // Translate the $text into barcode the correct $code_type
  100. if ( in_array(strtolower($code_type), array("code128", "code128b")) ) {
  101. $chksum = 104;
  102. // Must not change order of array elements as the checksum depends on the array's key to validate final code
  103. $code_array = array(" "=>"212222","!"=>"222122","\""=>"222221","#"=>"121223","$"=>"121322","%"=>"131222","&"=>"122213","'"=>"122312","("=>"132212",")"=>"221213","*"=>"221312","+"=>"231212",","=>"112232","-"=>"122132","."=>"122231","/"=>"113222","0"=>"123122","1"=>"123221","2"=>"223211","3"=>"221132","4"=>"221231","5"=>"213212","6"=>"223112","7"=>"312131","8"=>"311222","9"=>"321122",":"=>"321221",";"=>"312212","<"=>"322112","="=>"322211",">"=>"212123","?"=>"212321","@"=>"232121","A"=>"111323","B"=>"131123","C"=>"131321","D"=>"112313","E"=>"132113","F"=>"132311","G"=>"211313","H"=>"231113","I"=>"231311","J"=>"112133","K"=>"112331","L"=>"132131","M"=>"113123","N"=>"113321","O"=>"133121","P"=>"313121","Q"=>"211331","R"=>"231131","S"=>"213113","T"=>"213311","U"=>"213131","V"=>"311123","W"=>"311321","X"=>"331121","Y"=>"312113","Z"=>"312311","["=>"332111","\\"=>"314111","]"=>"221411","^"=>"431111","_"=>"111224","\`"=>"111422","a"=>"121124","b"=>"121421","c"=>"141122","d"=>"141221","e"=>"112214","f"=>"112412","g"=>"122114","h"=>"122411","i"=>"142112","j"=>"142211","k"=>"241211","l"=>"221114","m"=>"413111","n"=>"241112","o"=>"134111","p"=>"111242","q"=>"121142","r"=>"121241","s"=>"114212","t"=>"124112","u"=>"124211","v"=>"411212","w"=>"421112","x"=>"421211","y"=>"212141","z"=>"214121","{"=>"412121","|"=>"111143","}"=>"111341","~"=>"131141","DEL"=>"114113","FNC 3"=>"114311","FNC 2"=>"411113","SHIFT"=>"411311","CODE C"=>"113141","FNC 4"=>"114131","CODE A"=>"311141","FNC 1"=>"411131","Start A"=>"211412","Start B"=>"211214","Start C"=>"211232","Stop"=>"2331112");
  104. $code_keys = array_keys($code_array);
  105. $code_values = array_flip($code_keys);
  106. for ( $X = 1; $X <= strlen($text); $X++ ) {
  107. $activeKey = substr( $text, ($X-1), 1);
  108. $code_string .= $code_array[$activeKey];
  109. $chksum=($chksum + ($code_values[$activeKey] * $X));
  110. }
  111. $code_string .= $code_array[$code_keys[($chksum - (intval($chksum / 103) * 103))]];
  112. $code_string = "211214" . $code_string . "2331112";
  113. } elseif ( strtolower($code_type) == "code128a" ) {
  114. $chksum = 103;
  115. $text = strtoupper($text); // Code 128A doesn't support lower case
  116. // Must not change order of array elements as the checksum depends on the array's key to validate final code
  117. $code_array = array(" "=>"212222","!"=>"222122","\""=>"222221","#"=>"121223","$"=>"121322","%"=>"131222","&"=>"122213","'"=>"122312","("=>"132212",")"=>"221213","*"=>"221312","+"=>"231212",","=>"112232","-"=>"122132","."=>"122231","/"=>"113222","0"=>"123122","1"=>"123221","2"=>"223211","3"=>"221132","4"=>"221231","5"=>"213212","6"=>"223112","7"=>"312131","8"=>"311222","9"=>"321122",":"=>"321221",";"=>"312212","<"=>"322112","="=>"322211",">"=>"212123","?"=>"212321","@"=>"232121","A"=>"111323","B"=>"131123","C"=>"131321","D"=>"112313","E"=>"132113","F"=>"132311","G"=>"211313","H"=>"231113","I"=>"231311","J"=>"112133","K"=>"112331","L"=>"132131","M"=>"113123","N"=>"113321","O"=>"133121","P"=>"313121","Q"=>"211331","R"=>"231131","S"=>"213113","T"=>"213311","U"=>"213131","V"=>"311123","W"=>"311321","X"=>"331121","Y"=>"312113","Z"=>"312311","["=>"332111","\\"=>"314111","]"=>"221411","^"=>"431111","_"=>"111224","NUL"=>"111422","SOH"=>"121124","STX"=>"121421","ETX"=>"141122","EOT"=>"141221","ENQ"=>"112214","ACK"=>"112412","BEL"=>"122114","BS"=>"122411","HT"=>"142112","LF"=>"142211","VT"=>"241211","FF"=>"221114","CR"=>"413111","SO"=>"241112","SI"=>"134111","DLE"=>"111242","DC1"=>"121142","DC2"=>"121241","DC3"=>"114212","DC4"=>"124112","NAK"=>"124211","SYN"=>"411212","ETB"=>"421112","CAN"=>"421211","EM"=>"212141","SUB"=>"214121","ESC"=>"412121","FS"=>"111143","GS"=>"111341","RS"=>"131141","US"=>"114113","FNC 3"=>"114311","FNC 2"=>"411113","SHIFT"=>"411311","CODE C"=>"113141","CODE B"=>"114131","FNC 4"=>"311141","FNC 1"=>"411131","Start A"=>"211412","Start B"=>"211214","Start C"=>"211232","Stop"=>"2331112");
  118. $code_keys = array_keys($code_array);
  119. $code_values = array_flip($code_keys);
  120. for ( $X = 1; $X <= strlen($text); $X++ ) {
  121. $activeKey = substr( $text, ($X-1), 1);
  122. $code_string .= $code_array[$activeKey];
  123. $chksum=($chksum + ($code_values[$activeKey] * $X));
  124. }
  125. $code_string .= $code_array[$code_keys[($chksum - (intval($chksum / 103) * 103))]];
  126. $code_string = "211412" . $code_string . "2331112";
  127. } elseif ( strtolower($code_type) == "code39" ) {
  128. $code_array = array("0"=>"111221211","1"=>"211211112","2"=>"112211112","3"=>"212211111","4"=>"111221112","5"=>"211221111","6"=>"112221111","7"=>"111211212","8"=>"211211211","9"=>"112211211","A"=>"211112112","B"=>"112112112","C"=>"212112111","D"=>"111122112","E"=>"211122111","F"=>"112122111","G"=>"111112212","H"=>"211112211","I"=>"112112211","J"=>"111122211","K"=>"211111122","L"=>"112111122","M"=>"212111121","N"=>"111121122","O"=>"211121121","P"=>"112121121","Q"=>"111111222","R"=>"211111221","S"=>"112111221","T"=>"111121221","U"=>"221111112","V"=>"122111112","W"=>"222111111","X"=>"121121112","Y"=>"221121111","Z"=>"122121111","-"=>"121111212","."=>"221111211"," "=>"122111211","$"=>"121212111","/"=>"121211121","+"=>"121112121","%"=>"111212121","*"=>"121121211");
  129. // Convert to uppercase
  130. $upper_text = strtoupper($text);
  131. for ( $X = 1; $X<=strlen($upper_text); $X++ ) {
  132. $code_string .= $code_array[substr( $upper_text, ($X-1), 1)] . "1";
  133. }
  134. $code_string = "1211212111" . $code_string . "121121211";
  135. } elseif ( strtolower($code_type) == "code25" ) {
  136. $code_array1 = array("1","2","3","4","5","6","7","8","9","0");
  137. $code_array2 = array("3-1-1-1-3","1-3-1-1-3","3-3-1-1-1","1-1-3-1-3","3-1-3-1-1","1-3-3-1-1","1-1-1-3-3","3-1-1-3-1","1-3-1-3-1","1-1-3-3-1");
  138. for ( $X = 1; $X <= strlen($text); $X++ ) {
  139. for ( $Y = 0; $Y < count($code_array1); $Y++ ) {
  140. if ( substr($text, ($X-1), 1) == $code_array1[$Y] )
  141. $temp[$X] = $code_array2[$Y];
  142. }
  143. }
  144. for ( $X=1; $X<=strlen($text); $X+=2 ) {
  145. if ( isset($temp[$X]) && isset($temp[($X + 1)]) ) {
  146. $temp1 = explode( "-", $temp[$X] );
  147. $temp2 = explode( "-", $temp[($X + 1)] );
  148. for ( $Y = 0; $Y < count($temp1); $Y++ )
  149. $code_string .= $temp1[$Y] . $temp2[$Y];
  150. }
  151. }
  152. $code_string = "1111" . $code_string . "311";
  153. } elseif ( strtolower($code_type) == "codabar" ) {
  154. $code_array1 = array("1","2","3","4","5","6","7","8","9","0","-","$",":","/",".","+","A","B","C","D");
  155. $code_array2 = array("1111221","1112112","2211111","1121121","2111121","1211112","1211211","1221111","2112111","1111122","1112211","1122111","2111212","2121112","2121211","1121212","1122121","1212112","1112122","1112221");
  156. // Convert to uppercase
  157. $upper_text = strtoupper($text);
  158. for ( $X = 1; $X<=strlen($upper_text); $X++ ) {
  159. for ( $Y = 0; $Y<count($code_array1); $Y++ ) {
  160. if ( substr($upper_text, ($X-1), 1) == $code_array1[$Y] )
  161. $code_string .= $code_array2[$Y] . "1";
  162. }
  163. }
  164. $code_string = "11221211" . $code_string . "1122121";
  165. }
  166. // Pad the edges of the barcode
  167. $code_length = 20;
  168. if ($print) {
  169. $text_height = 30;
  170. } else {
  171. $text_height = 0;
  172. }
  173. for ( $i=1; $i <= strlen($code_string); $i++ ){
  174. $code_length = $code_length + (integer)(substr($code_string,($i-1),1));
  175. }
  176. if ( strtolower($orientation) == "horizontal" ) {
  177. $img_width = $code_length*$SizeFactor;
  178. $img_height = $size;
  179. } else {
  180. $img_width = $size;
  181. $img_height = $code_length*$SizeFactor;
  182. }
  183. $image = imagecreate($img_width, $img_height + $text_height);
  184. $black = imagecolorallocate ($image, 0, 0, 0);
  185. $white = imagecolorallocate ($image, 255, 255, 255);
  186. imagefill( $image, 0, 0, $white );
  187. if ( $print ) {
  188. imagestring($image, 5, 31, $img_height, $text, $black );
  189. }
  190. $location = 10;
  191. for ( $position = 1 ; $position <= strlen($code_string); $position++ ) {
  192. $cur_size = $location + ( substr($code_string, ($position-1), 1) );
  193. if ( strtolower($orientation) == "horizontal" )
  194. imagefilledrectangle( $image, $location*$SizeFactor, 0, $cur_size*$SizeFactor, $img_height, ($position % 2 == 0 ? $white : $black) );
  195. else
  196. imagefilledrectangle( $image, 0, $location*$SizeFactor, $img_width, $cur_size*$SizeFactor, ($position % 2 == 0 ? $white : $black) );
  197. $location = $cur_size;
  198. }
  199. // Draw barcode to the screen or save in a file
  200. if ( $filepath=="" ) {
  201. header ('Content-type: image/png');
  202. imagepng($image);
  203. imagedestroy($image);
  204. } else {
  205. imagepng($image,$filepath);
  206. imagedestroy($image);
  207. }
  208. }
  209. /**
  210. * @Method("get")
  211. * @Route("/form/stations", name="form_stations")
  212. */
  213. public function getStations(Request $request){
  214. $queryTerm = $request->get('q');
  215. $em = $this->getDoctrine()->getManager();
  216. $towns = $em->getRepository(Station::class)->findFormTown($queryTerm);
  217. $serializer = SerializerBuilder::create()->build();
  218. $town = new Station();
  219. $town->setStationName('ALL STATIONS');
  220. $town->setId(-1);
  221. array_push($towns, $town);
  222. $data = $serializer->serialize($towns,'json');
  223. return new Response($data, Response::HTTP_OK);
  224. }
  225. /**
  226. * @Route("/sendsms/{id}", name="sms_sender")
  227. * @param Transaction $transaction
  228. * @return Response
  229. */
  230. public function sendSms(Transaction $transaction) {
  231. $em = $this->getDoctrine()->getManager();
  232. $waybill = $transaction->getWayBill();
  233. $userStation = $transaction->getWayBill()->getToStation();
  234. $smsMessage = new Sms();
  235. $smsMessage->setTransaction($transaction);
  236. $smsMessage->setRecepient('0716308459');
  237. $smsMessage->setCreatedAt(new \DateTime());
  238. $smsMessage->setCreatedBy($this->getUser());
  239. $smsMessage->setSms("HELLO ".strtoupper($waybill->getReceiverName())." 1 of {$waybill->getPercelCount()} parcels from {$waybill->getFromStation()->getStationName()} sent by ".strtoupper($waybill->getSenderName())." has arrived at {$waybill->getToStation()->getStationName()} NENO COURIER OFFICES kindly come collect, we are located {$waybill->getToStation()->getStationAddress()}");
  240. $smsMessage->setOrganization($userStation->getOrganization());
  241. $smsMessage->setAmount(0);
  242. try{
  243. $em->persist($smsMessage);
  244. $em->flush();
  245. $sender = new SendSms();
  246. $sender->sendMessage($smsMessage);
  247. }catch (PDOException $exception){
  248. dump($exception);
  249. }
  250. return new Response("OK", Response::HTTP_OK, [
  251. 'content-type' => 'text/html'
  252. ]);
  253. }
  254. /**
  255. * @Route("/sendsms/response/{id}", name="sms_sender_response")
  256. * @param Sms $sms
  257. * @param Request $request
  258. * @return Response
  259. */
  260. public function responseSmsData(Sms $sms, Request $request) {
  261. $em = $this->getDoctrine()->getManager();
  262. $context = new SerializationContext();
  263. $context->setSerializeNull(true);
  264. $serializer = SerializerBuilder::create()->build();
  265. $data = json_decode($request->getContent(),true);
  266. // dump($data);
  267. $sms->setAmount($data['amount']);
  268. $sms->setStatus($data['status']);
  269. $sms->setStatusCode($data['statusCode']);
  270. $em->flush();
  271. return new Response('ok', Response::HTTP_OK);
  272. }
  273. /**
  274. * @Route("/sendsms/response/testing/ground", name="sms_sender_response_tester")
  275. * @param Sms $sms
  276. * @param Request $request
  277. * @return Response
  278. */
  279. public function sendTestSms(Request $request) {
  280. $em = $this->getDoctrine()->getManager();
  281. $context = new SerializationContext();
  282. $context->setSerializeNull(true);
  283. $message = new SendSms();
  284. $sms = new Sms();
  285. $sms->setSms("Hello");
  286. $sms->setRecepient('0716308459');
  287. $message->sendSms(new Sms());
  288. return new Response('ok', Response::HTTP_OK);
  289. }
  290. }