WindykacjaZestawienia.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. class Route_WindykacjaZestawienia extends RouteBase {
  4. public function handleAuth() {
  5. if (!User::logged()) {
  6. throw new HttpException('Unauthorized', 401);
  7. }
  8. }
  9. public function defaultAction() {
  10. SE_Layout::gora();
  11. SE_Layout::menu();
  12. echo'TODO: Main menu...';
  13. SE_Layout::dol();
  14. }
  15. public function sendMassAction() {
  16. $selectedMonth = V::get('zest_month', date("Y-m"), $_REQUEST);
  17. $f_type = V::get('f_type', '', $_REQUEST);
  18. $f_day = V::get('f_day', '', $_REQUEST);
  19. $f_status = V::get('f_status', '', $_REQUEST);
  20. SE_Layout::gora();
  21. SE_Layout::menu();
  22. $this->_sendMassMenu($selectedMonth);
  23. $this->_showSendMass($selectedMonth);
  24. if (!empty($f_day) && !empty($f_status)) {
  25. if ('MAIL' == $f_type) {
  26. $this->_showSendMassMailDetails($f_day, $f_status);
  27. } else if ('SMS' == $f_type) {
  28. $this->_showSendMassSmsDetails($f_day, $f_status);
  29. }
  30. }
  31. SE_Layout::dol();
  32. }
  33. private function _showSendMass($selectedMonth) {
  34. $zest = array();
  35. if (strlen($selectedMonth) != 7) return;
  36. $db = DB::getDB();
  37. $sql = <<<SQL
  38. (
  39. select statsMail.`TYPE`
  40. , statsMail.`A_RECORD_UPDATE_AUTHOR`
  41. , statsMail.`_wind_save`
  42. , statsMail.`STATUS`
  43. , count(*) as cnt
  44. from (
  45. select 'MAIL' as `TYPE`
  46. , h.`A_RECORD_UPDATE_AUTHOR`
  47. , substr(h.`A_RECORD_UPDATE_DATE`, 1, 10) as `_wind_save`
  48. -- , hc.`STATUS_MAIL` as `STATUS`
  49. , IF(hc.`REQUEST_STATUS_MAIL`='CONFIRM_SENT_MAIL' and hc.`STATUS_MAIL`='NONE'
  50. , 'WAITING'
  51. , IF(hc.`REQUEST_STATUS_MAIL`='CONFIRM_SENT_MAIL' and hc.`STATUS_MAIL`='SENT_MAIL'
  52. , 'SENT'
  53. , IF(hc.`REQUEST_STATUS_MAIL`='NONE' and hc.`STATUS_MAIL`='NONE'
  54. , 'UNDELIVERED'
  55. , 'UNKNOWN'
  56. )
  57. )
  58. ) as `STATUS`
  59. from `SES_USERS2`.`USERS2_WINDYKACJA_STATUS_HIST` h
  60. join `SES_USERS2`.`USERS2_WINDYKACJA_STATUS` w on(w.`ID`=h.`ID_USERS2`)
  61. join `billing2013`.`HIST_CONTACTS` hc on(hc.`ID`=h.`LAST_MAIL_MSG_ID`)
  62. where h.`LAST_MAIL_MSG_ID`>0
  63. and h.`A_RECORD_UPDATE_DATE` like '{$selectedMonth}-%'
  64. ) as statsMail
  65. group by statsMail.`A_RECORD_UPDATE_AUTHOR`, statsMail.`_wind_save`, statsMail.`STATUS`
  66. )
  67. union
  68. (
  69. select statsSms.`TYPE`
  70. , statsSms.`A_RECORD_UPDATE_AUTHOR`
  71. , statsSms.`_wind_save`
  72. , statsSms.`STATUS`
  73. , count(*) as cnt
  74. from (
  75. select 'SMS' as `TYPE`
  76. , h.`A_RECORD_UPDATE_AUTHOR`
  77. , substr(h.`A_RECORD_UPDATE_DATE`, 1, 10) as `_wind_save`
  78. , IF(hc.`REQUEST_STATUS_SMS`='SENT_SMS' and hc.`STATUS_SMS`='NONE'
  79. , 'WAITING'
  80. , IF(hc.`REQUEST_STATUS_SMS`='CONFIRM_SENT_SMS' and hc.`STATUS_SMS`='SENT_SMS'
  81. , 'MONITOR'
  82. , IF(hc.`REQUEST_STATUS_SMS`='NONE' and hc.`STATUS_SMS`='CONFIRM_SENT_SMS'
  83. , 'SENT'
  84. , IF(hc.`REQUEST_STATUS_SMS`='NONE' and hc.`STATUS_SMS` in('UNDELIVERED','NONE')
  85. , 'UNDELIVERED'
  86. , IF(hc.`REQUEST_STATUS_SMS`='NONE' and hc.`STATUS_SMS`='UNKNOWN'
  87. , 'OFF_SOFT'
  88. , 'UNKNOWN'
  89. )
  90. )
  91. )
  92. )
  93. ) as `STATUS`
  94. from `SES_USERS2`.`USERS2_WINDYKACJA_STATUS_HIST` h
  95. join `SES_USERS2`.`USERS2_WINDYKACJA_STATUS` w on(w.`ID`=h.`ID_USERS2`)
  96. join `billing2013`.`HIST_CONTACTS` hc on(hc.`ID`=h.`LAST_SMS_MSG_ID`)
  97. where h.`LAST_SMS_MSG_ID` > 0
  98. and h.`A_RECORD_UPDATE_DATE` like '{$selectedMonth}-%'
  99. ) as statsSms
  100. group by statsSms.`A_RECORD_UPDATE_AUTHOR`, statsSms.`_wind_save`, statsSms.`STATUS`
  101. )
  102. order by `_wind_save` ASC, `TYPE`
  103. ;
  104. SQL;
  105. $res = $db->query($sql);
  106. while ($r = $db->fetch($res)) {
  107. /*
  108. WAITING: REQUEST_STATUS_MAIL='CONFIRM_SENT_MAIL’ && STATUS_MAIL='NONE’ - mail czeka na wysłanie
  109. SENT: REQUEST_STATUS_MAIL='CONFIRM_SENT_MAIL’ && STATUS_MAIL=’SENT_MAIL’ - mail został wysłany
  110. UNDELIVERED: REQUEST_STATUS_MAIL=’NONE’ && STATUS_MAIL='NONE’ - mail nie został i nie zostanie wysłany (np. brak/błędny adres mailowy)
  111. WAITING: REQUEST_STATUS_SMS=’SENT_SMS’ && STATUS_SMS=’NONE' - sms do wysłania
  112. MONITOR: REQUEST_STATUS_SMS='CONFIRM_SENT_SMS’ && STATUS_SMS=’SENT_SMS’ - sms wysłany, oczekuje na potwierdzenie dostarczenia
  113. SENT: REQUEST_STATUS_SMS=’NONE’ && STATUS_SMS='CONFIRM_SENT_SMS' - sms dostarczony
  114. UNDELIVERED: REQUEST_STATUS_SMS=’NONE’ && STATUS_SMS='UNDELIVERED' - sms niedostarczony (musiał wystąpić jakiś błąd)
  115. UNDELIVERED: REQUEST_STATUS_SMS=’NONE’ && STATUS_SMS=‚NONE’ (obecnie nieużywana już kombinacja) - sms niedostarczony (musiał wystąpić jakiś błąd)
  116. OFF_SOFT: REQUEST_STATUS_SMS=’NONE’ && STATUS_SMS='UNKNOWN' - sms wysłany, ale nie wiadomo czy dostarczony (nie uzyskano odpowiedzi ze strony platformy na temat dostarczenia sms)
  117. */
  118. $r->STATUS_INFO = '';
  119. if ('MAIL' == $r->TYPE) {
  120. if ('WAITING' == $r->STATUS) {
  121. $r->STATUS_INFO = 'do wysłania';
  122. } else if ('SENT' == $r->STATUS) {
  123. $r->STATUS_INFO = 'wysłano';
  124. } else if ('UNDELIVERED' == $r->STATUS) {
  125. $r->STATUS_INFO = 'niewysłano (np. brak/błędny adres mailowy)';
  126. }
  127. } else if ('SMS' == $r->TYPE) {
  128. if ('WAITING' == $r->STATUS) {
  129. $r->STATUS_INFO = 'do wysłania';
  130. } else if ('MONITOR' == $r->STATUS) {
  131. $r->STATUS_INFO = 'oczekiwanie na potwierdzenie odbioru';
  132. } else if ('SENT' == $r->STATUS) {
  133. $r->STATUS_INFO = 'wysłano';
  134. } else if ('UNDELIVERED' == $r->STATUS) {
  135. $r->STATUS_INFO = 'niedostarczono';
  136. }
  137. }
  138. $zest[] = $r;
  139. }
  140. $this->_showCss();
  141. ?>
  142. <div class="container">
  143. <table class="table table-bordered">
  144. <thead>
  145. <th>Typ</th>
  146. <th>User</th>
  147. <th>Data</th>
  148. <th>Status</th>
  149. <th>Ilość</th>
  150. <th>Szczegóły</th>
  151. </thead>
  152. <tbody>
  153. <?php foreach ($zest as $r) : ?>
  154. <?php
  155. $link = "index.php?_route=WindykacjaZestawienia&_task=sendMass&zest_month={$selectedMonth}&f_type={$r->TYPE}&f_day={$r->_wind_save}&f_status={$r->STATUS}";
  156. ?>
  157. <tr>
  158. <td><?php echo $r->TYPE; ?></td>
  159. <td><?php echo $r->A_RECORD_UPDATE_AUTHOR; ?></td>
  160. <td><?php echo $r->_wind_save; ?></td>
  161. <td class="status_cell-<?php echo $r->STATUS; ?>" title="<?php echo $r->STATUS_INFO; ?>"><?php echo $r->STATUS; ?> <i class="glyphicon glyphicon-info-sign"></i></td>
  162. <td><?php echo $r->cnt; ?></td>
  163. <td><a href="<?php echo $link; ?>">szczegóły</a></td>
  164. </tr>
  165. <?php endforeach; ?>
  166. </tbody>
  167. </table>
  168. </div>
  169. <?php
  170. }
  171. private function _showSendMassMailDetails($f_day, $f_status) {
  172. if (empty($f_day) || strlen($f_day) != 10
  173. || empty($f_status) || strlen($f_status) > 20) {
  174. echo 'Wrong params';
  175. return;
  176. }
  177. $zest = array();
  178. $db = DB::getDB();
  179. $sql = <<<SQL
  180. select statsMail.`TYPE`
  181. , statsMail.`A_RECORD_UPDATE_AUTHOR`
  182. , statsMail.`_wind_save`
  183. , statsMail.`STATUS`
  184. , statsMail.`ID_BILLING_USERS`
  185. , '' as `STATUS_INFO`
  186. from (
  187. select 'MAIL' as `TYPE`
  188. , h.`A_RECORD_UPDATE_AUTHOR`
  189. , substr(h.`A_RECORD_UPDATE_DATE`, 1, 10) as `_wind_save`
  190. , IF(hc.`REQUEST_STATUS_MAIL`='CONFIRM_SENT_MAIL' and hc.`STATUS_MAIL`='NONE'
  191. , 'WAITING'
  192. , IF(hc.`REQUEST_STATUS_MAIL`='CONFIRM_SENT_MAIL' and hc.`STATUS_MAIL`='SENT_MAIL'
  193. , 'SENT'
  194. , IF(hc.`REQUEST_STATUS_MAIL`='NONE' and hc.`STATUS_MAIL`='NONE'
  195. , 'UNDELIVERED'
  196. , 'UNKNOWN'
  197. )
  198. )
  199. ) as `STATUS`
  200. , hc.`ID_BILLING_USERS`
  201. from `SES_USERS2`.`USERS2_WINDYKACJA_STATUS_HIST` h
  202. join `SES_USERS2`.`USERS2_WINDYKACJA_STATUS` w on(w.`ID`=h.`ID_USERS2`)
  203. join `billing2013`.`HIST_CONTACTS` hc on(hc.`ID`=h.`LAST_MAIL_MSG_ID`)
  204. where h.`LAST_MAIL_MSG_ID`>0
  205. and h.`A_RECORD_UPDATE_DATE` like '{$f_day}%'
  206. ) as statsMail
  207. where statsMail.`STATUS`='{$f_status}'
  208. SQL;
  209. $res = $db->query($sql);
  210. while ($r = $db->fetch($res)) {
  211. if ('UNDELIVERED' == $r->STATUS) {
  212. $r->STATUS_INFO = 'mail nie został i nie zostanie wysłany (np. brak/błędny adres mailowy)';
  213. } else if ('SENT' == $r->STATUS) {
  214. $r->STATUS_INFO = 'mail został wysłany';
  215. }
  216. $zest[] = $r;
  217. }
  218. $this->_showCss();
  219. ?>
  220. <div class="container">
  221. <h3>Szczegóły (rodzaj: MAIL, dzień: <?php echo $f_day; ?>, status: <?php echo $f_status; ?>)</h3>
  222. <table class="table table-bordered">
  223. <thead>
  224. <th>Typ</th>
  225. <th>User</th>
  226. <th>Data</th>
  227. <th>Nr klienta</th>
  228. <th>Status</th>
  229. <th>Info</th>
  230. </thead>
  231. <tbody>
  232. <?php foreach ($zest as $r) : ?>
  233. <tr>
  234. <td><?php echo $r->TYPE; ?></td>
  235. <td><?php echo $r->A_RECORD_UPDATE_AUTHOR; ?></td>
  236. <td><?php echo $r->_wind_save; ?></td>
  237. <td><?php echo $r->ID_BILLING_USERS; ?></td>
  238. <td class="status_cell-<?php echo $r->STATUS; ?>"><?php echo $r->STATUS; ?></td>
  239. <td><?php echo $r->STATUS_INFO; ?></td>
  240. </tr>
  241. <?php endforeach; ?>
  242. </tbody>
  243. </table>
  244. </div>
  245. <?php
  246. }
  247. private function _showSendMassSmsDetails($f_day, $f_status) {
  248. if (empty($f_day) || strlen($f_day) != 10
  249. || empty($f_status) || strlen($f_status) > 20) {
  250. echo 'Wrong params';
  251. return;
  252. }
  253. $zest = array();
  254. $db = DB::getDB();
  255. $sql = <<<SQL
  256. select statsSms.`TYPE`
  257. , statsSms.`A_RECORD_UPDATE_AUTHOR`
  258. , statsSms.`_wind_save`
  259. , statsSms.`STATUS`
  260. , statsSms.`ID_BILLING_USERS`
  261. , statsSms.`STATUS_INFO`
  262. from (
  263. select 'SMS' as `TYPE`
  264. , h.`A_RECORD_UPDATE_AUTHOR`
  265. , substr(h.`A_RECORD_UPDATE_DATE`, 1, 10) as `_wind_save`
  266. , IF(hc.`REQUEST_STATUS_SMS`='SENT_SMS' and hc.`STATUS_SMS`='NONE'
  267. , 'WAITING'
  268. , IF(hc.`REQUEST_STATUS_SMS`='CONFIRM_SENT_SMS' and hc.`STATUS_SMS`='SENT_SMS'
  269. , 'MONITOR'
  270. , IF(hc.`REQUEST_STATUS_SMS`='NONE' and hc.`STATUS_SMS`='CONFIRM_SENT_SMS'
  271. , 'SENT'
  272. , IF(hc.`REQUEST_STATUS_SMS`='NONE' and hc.`STATUS_SMS` in('UNDELIVERED','NONE')
  273. , 'UNDELIVERED'
  274. , IF(hc.`REQUEST_STATUS_SMS`='NONE' and hc.`STATUS_SMS`='UNKNOWN'
  275. , 'OFF_SOFT'
  276. , 'UNKNOWN'
  277. )
  278. )
  279. )
  280. )
  281. ) as `STATUS`
  282. , hc.`ID_BILLING_USERS`
  283. , hc.`STATUS_SMS_INFO` as `STATUS_INFO`
  284. from `SES_USERS2`.`USERS2_WINDYKACJA_STATUS_HIST` h
  285. join `SES_USERS2`.`USERS2_WINDYKACJA_STATUS` w on(w.`ID`=h.`ID_USERS2`)
  286. join `billing2013`.`HIST_CONTACTS` hc on(hc.`ID`=h.`LAST_SMS_MSG_ID`)
  287. where h.`LAST_SMS_MSG_ID` > 0
  288. and h.`A_RECORD_UPDATE_DATE` like '{$f_day}%'
  289. ) as statsSms
  290. where statsSms.`STATUS`='{$f_status}'
  291. SQL;
  292. $res = $db->query($sql);
  293. while ($r = $db->fetch($res)) {
  294. if ('WAITING' == $r->STATUS) {
  295. $r->STATUS_INFO = "do wysłania";
  296. } else if ('MONITOR' == $r->STATUS) {
  297. $r->STATUS_INFO = "oczekiwanie na potwierdzenie odbioru";
  298. }
  299. $zest[] = $r;
  300. }
  301. $this->_showCss();
  302. ?>
  303. <div class="container">
  304. <h3>Szczegóły (rodzaj: SMS, dzień: <?php echo $f_day; ?>, status: <?php echo $f_status; ?>)</h3>
  305. <table class="table table-bordered">
  306. <thead>
  307. <th>Typ</th>
  308. <th>User</th>
  309. <th>Data</th>
  310. <th>Nr klienta</th>
  311. <th>Status</th>
  312. <th>Info</th>
  313. </thead>
  314. <tbody>
  315. <?php foreach ($zest as $r) : ?>
  316. <tr>
  317. <td><?php echo $r->TYPE; ?></td>
  318. <td><?php echo $r->A_RECORD_UPDATE_AUTHOR; ?></td>
  319. <td><?php echo $r->_wind_save; ?></td>
  320. <td><?php echo $r->ID_BILLING_USERS; ?></td>
  321. <td class="status_cell-<?php echo $r->STATUS; ?>"><?php echo $r->STATUS; ?></td>
  322. <td><?php echo $r->STATUS_INFO; ?></td>
  323. </tr>
  324. <?php endforeach; ?>
  325. </tbody>
  326. </table>
  327. </div>
  328. <?php
  329. }
  330. private function _sendMassMenu($selectedMonth) {
  331. $year = ($selectedMonth)? $selectedMonth : date("Y-m");
  332. ?>
  333. <div class="jumbotron">
  334. <div class="container">
  335. <form class="form-inline" method="POST">
  336. <input type="hidden" name="task" value="sendMass" />
  337. <label for="zest_month">Zestawienie masowej wysyłki sms/mail:</label>
  338. <div class="input-group date" id="fldZestMonth">
  339. <input type="text" name="zest_month" class="form-control" value="<?php echo $selectedMonth; ?>" />
  340. <span class="input-group-addon"><span class="glyphicon glyphicon-time"></span></span>
  341. </div>
  342. <button type="submit" id="fldZestMonthBtn" class="btn btn-primary" autocomplete="off">
  343. Generuj zestawienie
  344. </button>
  345. </form>
  346. </div>
  347. </div>
  348. <script type="text/javascript">
  349. jQuery(document).ready(function () {
  350. jQuery('#fldZestMonthBtn').on('click', function () {
  351. jQuery(this).text(jQuery(this).text() + '...').attr('disabled', 'disabled');
  352. jQuery(this).parent().submit();
  353. })
  354. jQuery("#fldZestMonth").datetimepicker({
  355. format: "YYYY-MM",
  356. defaultDate: new Date(<?php echo date("Y"); ?>, <?php echo intval(date("m")) - 1; ?>, 1),
  357. minDate: new Date(2014, 11, 1),
  358. // maxDate: "<?php echo date("Y"); ?>"
  359. });
  360. });
  361. </script>
  362. <?php
  363. }
  364. private function _showCss() {
  365. static $_printed = false;
  366. if ($_printed) return;
  367. if (false == $_printed) $_printed = true;
  368. ?>
  369. <style type="text/css">
  370. .status_cell-SENT { background:#aeffae; color:#000; text-align:center; }
  371. .status_cell-WAITING { background:#ffd2ff; color:#000; text-align:center; }
  372. .status_cell-MONITOR { background:#cccaff; color:#000; text-align:center; }
  373. .status_cell-WARNING { background:#ffbaba; color:#000; text-align:center; }
  374. .status_cell-UNDELIVERED { background:#e0e0e0; color:#808080; text-align:center; }
  375. .status_cell-OFF_SOFT { background:#fce3b7; color:#808080; text-align:center; }
  376. .status_cell-OFF_HARD { background:#eee; color:#808080; text-align:center; }
  377. </style>
  378. <?php
  379. }
  380. }