ActiveServices.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. , c.`A_ADM_COMPANY`
  90. , '{$sqlYear}' as YEAR
  91. , '{$sqlMonth}' as MONTH
  92. , IF(DATE_SUB('{$sqlMonthFirstDay}', INTERVAL 2 DAY) > CURDATE()
  93. , '???'
  94. , A_STATUS_L2_SQL_DATE(srv.`ID`, DATE_SUB('{$sqlMonthFirstDay}', INTERVAL 2 DAY))
  95. ) as OPEN_DAY_MINUS_2
  96. , IF(DATE_SUB('{$sqlMonthFirstDay}', INTERVAL 1 DAY) > CURDATE()
  97. , '???'
  98. , A_STATUS_L2_SQL_DATE(srv.`ID`, DATE_SUB('{$sqlMonthFirstDay}', INTERVAL 1 DAY))
  99. ) as OPEN_DAY_MINUS_1
  100. , IF('{$sqlMonthFirstDay}' > CURDATE()
  101. , '???'
  102. , A_STATUS_L2_SQL_DATE(srv.`ID`, '{$sqlMonthFirstDay}')
  103. ) as OPEN_DAY
  104. , IF(DATE_ADD('{$sqlMonthFirstDay}', INTERVAL 1 DAY) > CURDATE()
  105. , '???'
  106. , A_STATUS_L2_SQL_DATE(srv.`ID`, DATE_ADD('{$sqlMonthFirstDay}', INTERVAL 1 DAY))
  107. ) as OPEN_DAY_PLUS_1
  108. , IF(DATE_SUB(LAST_DAY('{$sqlMonthFirstDay}'), INTERVAL 1 DAY) > CURDATE()
  109. , '???'
  110. , A_STATUS_L2_SQL_DATE(srv.`ID`, DATE_SUB(LAST_DAY('{$sqlMonthFirstDay}'), INTERVAL 1 DAY))
  111. ) as CLOSE_DAY_MINUS_1
  112. , IF(LAST_DAY('{$sqlMonthFirstDay}') > CURDATE()
  113. , '???'
  114. , A_STATUS_L2_SQL_DATE(srv.`ID`, LAST_DAY('{$sqlMonthFirstDay}'))
  115. ) as CLOSE_DAY
  116. , IF(DATE_ADD(LAST_DAY('{$sqlMonthFirstDay}'), INTERVAL 1 DAY) > CURDATE()
  117. , '???'
  118. , A_STATUS_L2_SQL_DATE(srv.`ID`, DATE_ADD(LAST_DAY('{$sqlMonthFirstDay}'), INTERVAL 1 DAY))
  119. ) as CLOSE_DAY_PLUS_1
  120. , IF(DATE_ADD(LAST_DAY('{$sqlMonthFirstDay}'), INTERVAL 2 DAY) > CURDATE()
  121. , '???'
  122. , A_STATUS_L2_SQL_DATE(srv.`ID`, DATE_ADD(LAST_DAY('{$sqlMonthFirstDay}'), INTERVAL 2 DAY))
  123. ) as CLOSE_DAY_PLUS_2
  124. , 0 as `_TO_REMOVE`
  125. from `COMPANIES` c
  126. join `SERVICES` srv on(srv.`ID_BILLING_USERS`=c.`ID`)
  127. -- where c.`A_ADM_COMPANY`='19994_PODMIOT_Vectra_wlasciciel'
  128. DB_SQL;
  129. //echo '<pre>';echo "{$sqlUpdateMonth}";echo'</pre>';
  130. $sqlUpdateMonth = <<<SQL
  131. insert into `STATS_ACTIVE_SERVICES_STATUS` (
  132. `ID_KLIENT`
  133. , `ID_SERVICES`
  134. , `A_ADM_COMPANY`
  135. , `YEAR`
  136. , `MONTH`
  137. , `OPEN_DAY_MINUS_2`
  138. , `OPEN_DAY_MINUS_1`
  139. , `OPEN_DAY`
  140. , `OPEN_DAY_PLUS_1`
  141. , `CLOSE_DAY_MINUS_1`
  142. , `CLOSE_DAY`
  143. , `CLOSE_DAY_PLUS_1`
  144. , `CLOSE_DAY_PLUS_2`
  145. , `_TO_REMOVE`
  146. )
  147. {$sqlUpdateMonth}
  148. SQL;
  149. //echo '<pre>';echo "{$sqlUpdateMonth}";echo'</pre>';
  150. $sqlList = array();
  151. $sqlList['drop month data'] = "delete from `STATS_ACTIVE_SERVICES_STATUS` where `YEAR`='{$sqlYear}' and `MONTH`='{$sqlMonth}' ";
  152. //$sqlList['set _TO_REMOVE=1'] = "update `STATS_ACTIVE_SERVICES_STATUS` set `_TO_REMOVE`=1 where `YEAR`='{$sqlYear}' and `MONTH`='{$sqlMonth}' ";
  153. $sqlList['update month data'] = $sqlUpdateMonth;
  154. //$sqlList['delete _TO_REMOVE=1'] = "delete from `STATS_ACTIVE_SERVICES_STATUS` where `_TO_REMOVE`=1 ";
  155. $db = DB::getDB();
  156. if ($db->has_errors()) {
  157. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  158. }
  159. foreach ($sqlList as $sqlName => $sql) {
  160. $res = $db->query($sql);
  161. if ($db->has_errors()) {
  162. throw new Exception("DB Errors at sql '{$sqlName}': " . implode("\n<br>", $db->get_errors()));
  163. }
  164. }
  165. }
  166. public function reinstallAction() {
  167. SE_Layout::gora();
  168. $this->reinstall();
  169. echo 'OK';
  170. SE_Layout::dol();
  171. }
  172. public function reinstall() {
  173. $sqlList = array();
  174. $sqlList['RemoveTable_STATS_ACTIVE_SERVICES_STATUS'] = "DROP TABLE IF EXISTS `STATS_ACTIVE_SERVICES_STATUS`";
  175. $sqlList['InstallTable_STATS_ACTIVE_SERVICES_STATUS'] = <<<SQL
  176. CREATE TABLE IF NOT EXISTS `STATS_ACTIVE_SERVICES_STATUS` (
  177. `ID` int(11) NOT NULL AUTO_INCREMENT
  178. , `ID_KLIENT` int(11) NOT NULL
  179. , `ID_SERVICES` int(11) NOT NULL
  180. , `A_ADM_COMPANY` varchar(100) NOT NULL DEFAULT ''
  181. , `YEAR` int(11) NOT NULL
  182. , `MONTH` int(11) NOT NULL
  183. , `OPEN_DAY_MINUS_2` varchar(16) NOT NULL DEFAULT ''
  184. , `OPEN_DAY_MINUS_1` varchar(16) NOT NULL DEFAULT ''
  185. , `OPEN_DAY` varchar(16) NOT NULL DEFAULT ''
  186. , `OPEN_DAY_PLUS_1` varchar(16) NOT NULL DEFAULT ''
  187. , `CLOSE_DAY_MINUS_1` varchar(16) NOT NULL DEFAULT ''
  188. , `CLOSE_DAY` varchar(16) NOT NULL DEFAULT ''
  189. , `CLOSE_DAY_PLUS_1` varchar(16) NOT NULL DEFAULT ''
  190. , `CLOSE_DAY_PLUS_2` varchar(16) NOT NULL DEFAULT ''
  191. , `_TO_REMOVE` tinyint(1) NOT NULL DEFAULT '0'
  192. , PRIMARY KEY (`ID`)
  193. , KEY `ID_KLIENT` (`ID_KLIENT`)
  194. , UNIQUE KEY `uniq__srv_month` (`ID_KLIENT`,`ID_SERVICES`,`YEAR`,`MONTH`)
  195. ) ENGINE=MyISAM DEFAULT CHARSET=latin2;
  196. SQL;
  197. $db = DB::getDB();
  198. if ($db->has_errors()) {
  199. throw new Exception("DB Errors: " . implode("\n<br>", $db->get_errors()));
  200. }
  201. foreach ($sqlList as $sqlName => $sql) {
  202. $res = $db->query($sql);
  203. if ($db->has_errors()) {
  204. throw new Exception("DB Errors at sql '{$sqlName}': " . implode("\n<br>", $db->get_errors()));
  205. }
  206. }
  207. }
  208. }