ActiveServices.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. Lib::loadClass('RouteBase');
  3. /*
  4. * @uses MySQL Function `A_STATUS_L2_SQL_DATE`
  5. SET @p0 = '26199';
  6. SET @p1 = '2015-09-02';
  7. SELECT A_STATUS_L2_SQL_DATE ( @p0 , @p1 ) AS A_STATUS_L2_SQL_DATE;
  8. */
  9. class Route_ActiveServices extends RouteBase {
  10. public function handleAuth() {
  11. if (!User::logged()) {
  12. throw new HttpException('Unauthorized', 401);
  13. }
  14. }
  15. public function defaultAction() {
  16. $selectedMonth = V::get('zest_month', date("Y-m"), $_REQUEST);
  17. SE_Layout::gora();
  18. SE_Layout::menu();
  19. $this->_menu($selectedMonth);
  20. SE_Layout::dol();
  21. }
  22. private function _menu($selectedMonth) {
  23. $selectedMonth = ($selectedMonth)? $selectedMonth : date("Y-m");
  24. ?>
  25. <div class="jumbotron">
  26. <div class="container">
  27. <form class="form-inline" method="POST">
  28. <input type="hidden" name="_task" value="updateMonth" />
  29. <label for="zest_month">Aktualizacja zestawienia aktywnych usług na otwarcie/zamknięcie miesiąca:</label>
  30. <div class="input-group date" id="fldZestMonth">
  31. <input type="text" name="zest_month" class="form-control" value="<?php echo $selectedMonth; ?>" />
  32. <span class="input-group-addon"><span class="glyphicon glyphicon-time"></span></span>
  33. </div>
  34. <button type="submit" id="fldZestMonthBtn" class="btn btn-primary" autocomplete="off">
  35. Aktualizuj dane
  36. </button>
  37. </form>
  38. </div>
  39. </div>
  40. <script type="text/javascript">
  41. jQuery(document).ready(function () {
  42. jQuery('#fldZestMonthBtn').on('click', function () {
  43. jQuery(this).text(jQuery(this).text() + '...').attr('disabled', 'disabled');
  44. jQuery(this).parent().submit();
  45. })
  46. jQuery("#fldZestMonth").datetimepicker({
  47. format: "YYYY-MM",
  48. defaultDate: new Date(<?php echo date("Y"); ?>, <?php echo intval(date("m")) - 1; ?>, 1),
  49. minDate: new Date(2014, 11, 1),
  50. // maxDate: "<?php echo date("Y"); ?>"
  51. });
  52. });
  53. </script>
  54. <?php
  55. }
  56. public function updateMonthAction() {
  57. $selectedMonth = V::get('zest_month', date("Y-m"), $_REQUEST);
  58. SE_Layout::gora();
  59. SE_Layout::menu();
  60. $this->_menu($selectedMonth);
  61. if (strlen($selectedMonth) != 7) {
  62. echo '<div class="alert alert-gander">Wrong month</div>';
  63. } else {
  64. try {
  65. $this->_updateMonth($selectedMonth);
  66. } catch (Exception $e) {
  67. echo '<div class="alert alert-danger">' . $e->getMessage(). '</div>';
  68. SE_Layout::dol();
  69. exit;
  70. }
  71. echo '<div class="alert alert-success">' . "Gotowe" . '</div>';
  72. }
  73. // TODO: TableAjax for `STATS_ACTIVE_SERVICES_STATUS`
  74. SE_Layout::dol();
  75. }
  76. public function _updateMonth($selectedMonth) {
  77. //echo '<p>TODO: update month '.$selectedMonth.'</p>';
  78. $parts = explode('-', $selectedMonth);
  79. if (count($parts) != 2 || strlen($parts[0]) != 4 || strlen($parts[1]) != 2) {
  80. throw new Exception("Wrong month format");
  81. }
  82. $sqlYear = $parts[0];
  83. $sqlMonth = $parts[1];
  84. $sqlMonthFirstDay = "{$sqlYear}-{$sqlMonth}-01";
  85. $sqlUpdateMonth = <<<DB_SQL
  86. select
  87. c.`ID` as `ID_KLIENT`
  88. , srv.`ID` as `ID_SERVICES`
  89. , srv.`NAME_LIST_SERVICES` as `SERVICES_TYPE`
  90. , c.`A_ADM_COMPANY`
  91. , '{$sqlYear}' as YEAR
  92. , '{$sqlMonth}' as MONTH
  93. , IF(DATE_SUB('{$sqlMonthFirstDay}', INTERVAL 2 DAY) > CURDATE()
  94. , '???'
  95. , A_STATUS_L2_SQL_DATE(srv.`ID`, DATE_SUB('{$sqlMonthFirstDay}', INTERVAL 2 DAY))
  96. ) as OPEN_DAY_MINUS_2
  97. , IF(DATE_SUB('{$sqlMonthFirstDay}', INTERVAL 1 DAY) > CURDATE()
  98. , '???'
  99. , A_STATUS_L2_SQL_DATE(srv.`ID`, DATE_SUB('{$sqlMonthFirstDay}', INTERVAL 1 DAY))
  100. ) as OPEN_DAY_MINUS_1
  101. , IF('{$sqlMonthFirstDay}' > CURDATE()
  102. , '???'
  103. , A_STATUS_L2_SQL_DATE(srv.`ID`, '{$sqlMonthFirstDay}')
  104. ) as OPEN_DAY
  105. , IF(DATE_ADD('{$sqlMonthFirstDay}', INTERVAL 1 DAY) > CURDATE()
  106. , '???'
  107. , A_STATUS_L2_SQL_DATE(srv.`ID`, DATE_ADD('{$sqlMonthFirstDay}', INTERVAL 1 DAY))
  108. ) as OPEN_DAY_PLUS_1
  109. , IF(DATE_SUB(LAST_DAY('{$sqlMonthFirstDay}'), INTERVAL 1 DAY) > CURDATE()
  110. , '???'
  111. , A_STATUS_L2_SQL_DATE(srv.`ID`, DATE_SUB(LAST_DAY('{$sqlMonthFirstDay}'), INTERVAL 1 DAY))
  112. ) as CLOSE_DAY_MINUS_1
  113. , IF(LAST_DAY('{$sqlMonthFirstDay}') > CURDATE()
  114. , '???'
  115. , A_STATUS_L2_SQL_DATE(srv.`ID`, LAST_DAY('{$sqlMonthFirstDay}'))
  116. ) as CLOSE_DAY
  117. , IF(DATE_ADD(LAST_DAY('{$sqlMonthFirstDay}'), INTERVAL 1 DAY) > CURDATE()
  118. , '???'
  119. , A_STATUS_L2_SQL_DATE(srv.`ID`, DATE_ADD(LAST_DAY('{$sqlMonthFirstDay}'), INTERVAL 1 DAY))
  120. ) as CLOSE_DAY_PLUS_1
  121. , IF(DATE_ADD(LAST_DAY('{$sqlMonthFirstDay}'), INTERVAL 2 DAY) > CURDATE()
  122. , '???'
  123. , A_STATUS_L2_SQL_DATE(srv.`ID`, DATE_ADD(LAST_DAY('{$sqlMonthFirstDay}'), INTERVAL 2 DAY))
  124. ) as CLOSE_DAY_PLUS_2
  125. , 0 as `_TO_REMOVE`
  126. from `COMPANIES` c
  127. join `SERVICES` srv on(srv.`ID_BILLING_USERS`=c.`ID`)
  128. -- where c.`A_ADM_COMPANY`='19994_PODMIOT_Vectra_wlasciciel'
  129. DB_SQL;
  130. //echo '<pre>';echo "{$sqlUpdateMonth}";echo'</pre>';
  131. $sqlUpdateMonth = <<<SQL
  132. insert into `STATS_ACTIVE_SERVICES_STATUS` (
  133. `ID_KLIENT`
  134. , `ID_SERVICES`
  135. , `NAME_LIST_SERVICES`
  136. , `A_ADM_COMPANY`
  137. , `YEAR`
  138. , `MONTH`
  139. , `OPEN_DAY_MINUS_2`
  140. , `OPEN_DAY_MINUS_1`
  141. , `OPEN_DAY`
  142. , `OPEN_DAY_PLUS_1`
  143. , `CLOSE_DAY_MINUS_1`
  144. , `CLOSE_DAY`
  145. , `CLOSE_DAY_PLUS_1`
  146. , `CLOSE_DAY_PLUS_2`
  147. , `_TO_REMOVE`
  148. )
  149. {$sqlUpdateMonth}
  150. SQL;
  151. //echo '<pre>';echo "{$sqlUpdateMonth}";echo'</pre>';
  152. $sqlList = array();
  153. $sqlList['drop month data'] = "delete from `STATS_ACTIVE_SERVICES_STATUS` where `YEAR`='{$sqlYear}' and `MONTH`='{$sqlMonth}' ";
  154. //$sqlList['set _TO_REMOVE=1'] = "update `STATS_ACTIVE_SERVICES_STATUS` set `_TO_REMOVE`=1 where `YEAR`='{$sqlYear}' and `MONTH`='{$sqlMonth}' ";
  155. $sqlList['update month data'] = $sqlUpdateMonth;
  156. //$sqlList['delete _TO_REMOVE=1'] = "delete from `STATS_ACTIVE_SERVICES_STATUS` where `_TO_REMOVE`=1 ";
  157. $db = DB::getDB();
  158. if ($db->has_errors()) {
  159. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  160. }
  161. foreach ($sqlList as $sqlName => $sql) {
  162. $res = $db->query($sql);
  163. if ($db->has_errors()) {
  164. throw new Exception("DB Errors at sql '{$sqlName}': " . implode("\n<br>", $db->get_errors()));
  165. }
  166. }
  167. }
  168. public function reinstallAction() {
  169. SE_Layout::gora();
  170. $this->reinstall();
  171. echo 'OK';
  172. SE_Layout::dol();
  173. }
  174. public function reinstall() {
  175. $sqlList = array();
  176. $sqlList['RemoveTable_STATS_ACTIVE_SERVICES_STATUS'] = "DROP TABLE IF EXISTS `STATS_ACTIVE_SERVICES_STATUS`";
  177. $sqlList['InstallTable_STATS_ACTIVE_SERVICES_STATUS'] = <<<SQL
  178. CREATE TABLE IF NOT EXISTS `STATS_ACTIVE_SERVICES_STATUS` (
  179. `ID` int(11) NOT NULL AUTO_INCREMENT
  180. , `ID_KLIENT` int(11) NOT NULL
  181. , `ID_SERVICES` int(11) NOT NULL
  182. , `NAME_LIST_SERVICES` varchar(16) NOT NULL DEFAULT ''
  183. , `A_ADM_COMPANY` varchar(100) NOT NULL DEFAULT ''
  184. , `YEAR` int(11) NOT NULL
  185. , `MONTH` int(11) NOT NULL
  186. , `OPEN_DAY_MINUS_2` varchar(16) NOT NULL DEFAULT ''
  187. , `OPEN_DAY_MINUS_1` varchar(16) NOT NULL DEFAULT ''
  188. , `OPEN_DAY` varchar(16) NOT NULL DEFAULT ''
  189. , `OPEN_DAY_PLUS_1` varchar(16) NOT NULL DEFAULT ''
  190. , `CLOSE_DAY_MINUS_1` varchar(16) NOT NULL DEFAULT ''
  191. , `CLOSE_DAY` varchar(16) NOT NULL DEFAULT ''
  192. , `CLOSE_DAY_PLUS_1` varchar(16) NOT NULL DEFAULT ''
  193. , `CLOSE_DAY_PLUS_2` varchar(16) NOT NULL DEFAULT ''
  194. , `_TO_REMOVE` tinyint(1) NOT NULL DEFAULT '0'
  195. , PRIMARY KEY (`ID`)
  196. , KEY `ID_KLIENT` (`ID_KLIENT`)
  197. , UNIQUE KEY `uniq__srv_month` (`ID_KLIENT`,`ID_SERVICES`,`YEAR`,`MONTH`)
  198. ) ENGINE=MyISAM DEFAULT CHARSET=latin2;
  199. SQL;
  200. $db = DB::getDB();
  201. if ($db->has_errors()) {
  202. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  203. }
  204. foreach ($sqlList as $sqlName => $sql) {
  205. $res = $db->query($sql);
  206. if ($db->has_errors()) {
  207. throw new Exception("DB Errors at sql '{$sqlName}': " . implode("\n<br>", $db->get_errors()));
  208. }
  209. }
  210. }
  211. }