ColumnDealsStatus.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. <?php
  2. class ColumnDealsStatus {
  3. public static function get_table() {
  4. return 'temp_DEALS_STATUS';
  5. }
  6. public static function get_drop_query() {
  7. return "DROP TABLE IF EXISTS `" . self::get_table() . "`;";
  8. }
  9. public static function get_create_query() {
  10. return "CREATE TABLE IF NOT EXISTS `" . self::get_table() . "` (
  11. `ID_DEALS` int(11) NOT NULL,
  12. `ID_BILLING_USERS` int(11) NOT NULL,
  13. `A_CREATE_DATE` date NOT NULL DEFAULT '0000-00-00',
  14. `DEALS_ACTIVE` int(1) NOT NULL DEFAULT '0',
  15. `P_DEALDATE_TERM` date NOT NULL DEFAULT '0000-00-00',
  16. `SERVICES_STATUS` varchar(255) NOT NULL DEFAULT '',
  17. `SERVICES_S_ADDRESS_STREET` varchar(255) NOT NULL DEFAULT '',
  18. KEY `ID_DEALS` (`ID_DEALS`),
  19. KEY `ID_BILLING_USERS` (`ID_BILLING_USERS`),
  20. KEY `DEALS_ACTIVE` (`DEALS_ACTIVE`)
  21. ) ENGINE=MyISAM DEFAULT CHARSET=latin2;
  22. ";
  23. }
  24. public static function get_update_query($userID = null) {
  25. $sqlUserID = '';
  26. if ($userID > 0) {
  27. $sqlUserID = " and `ID_BILLING_USERS`='{$userID}' ";
  28. }
  29. return "UPDATE `" . self::get_table() . "` set
  30. `DEALS_ACTIVE`=1
  31. where `SERVICES_STATUS` like '%NORMAL%'
  32. {$sqlUserID}
  33. ";
  34. }
  35. public static function get_insert_query($userID = null) {
  36. global $A_STATUS_L2_SQL;// @from .config.php
  37. if (empty($A_STATUS_L2_SQL)) {
  38. die('Config Error: variable "$A_STATUS_L2_SQL" not exists!');
  39. }
  40. /**
  41. select d.`ID`
  42. , d.`ID_BILLING_USERS`
  43. , '2012-10-01' as A_CREATE_DATE
  44. , GROUP_CONCAT(if(SV.HANGUP_STATUS=SV.A_STATUS,coalesce(SV.A_STATUS),
  45. if( ( unix_timestamp(NOW()) > unix_timestamp(SV.HANGUP_FROM) and ( SV.HANGUP_TILL='0000-00-00' or SV.HANGUP_TILL is NULL )
  46. and ( SV.HANGUP_FROM<>'0000-00-00' and SV.HANGUP_FROM is not null )) , coalesce(SV.HANGUP_STATUS),
  47. if( ( unix_timestamp(NOW()) < unix_timestamp(SV.HANGUP_FROM) and ( SV.HANGUP_TILL='0000-00-00' or SV.HANGUP_TILL is NULL )
  48. and ( SV.HANGUP_FROM<>'0000-00-00' and SV.HANGUP_FROM is not null )) , coalesce(SV.A_STATUS),
  49. if( ( unix_timestamp(NOW()) > unix_timestamp(SV.HANGUP_FROM) and unix_timestamp(NOW()) < unix_timestamp(SV.HANGUP_TILL)
  50. and ( SV.HANGUP_TILL<>'0000-00-00' and SV.HANGUP_TILL is not NULL )
  51. and ( SV.HANGUP_FROM<>'0000-00-00' and SV.HANGUP_FROM is not null )) , coalesce(SV.HANGUP_STATUS),
  52. if( ( unix_timestamp(NOW()) < unix_timestamp(SV.HANGUP_FROM) and unix_timestamp(NOW())< unix_timestamp(SV.HANGUP_TILL)
  53. and ( SV.HANGUP_TILL<>'0000-00-00' and SV.HANGUP_TILL is not NULL )
  54. and ( SV.HANGUP_FROM<>'0000-00-00' and SV.HANGUP_FROM is not null )) , coalesce(SV.A_STATUS),
  55. if( (
  56. ( SV.HANGUP_TILL='0000-00-00' or SV.HANGUP_TILL is NULL )
  57. and ( SV.HANGUP_FROM='0000-00-00' or SV.HANGUP_FROM is null )) , coalesce(SV.A_STATUS),
  58. if( unix_timestamp(NOW()) > unix_timestamp(SV.HANGUP_FROM) and unix_timestamp(NOW()) > unix_timestamp(SV.HANGUP_TILL) and (
  59. ( SV.HANGUP_TILL<>'0000-00-00' and SV.HANGUP_TILL is not NULL )
  60. and ( SV.HANGUP_FROM<>'0000-00-00' and SV.HANGUP_FROM is not null )) , coalesce(SV.A_STATUS),
  61. if( unix_timestamp(NOW()) > unix_timestamp(SV.HANGUP_FROM) and unix_timestamp(NOW()) > unix_timestamp(SV.HANGUP_TILL) and
  62. SV.HANGUP_FROM<SV.HANGUP_TILL and (
  63. ( SV.HANGUP_TILL<>'0000-00-00' and SV.HANGUP_TILL is not NULL )
  64. and ( SV.HANGUP_FROM<>'0000-00-00' and SV.HANGUP_FROM is not null )) , 'ERROR1',
  65. 'ERROR2'
  66. )
  67. )
  68. )
  69. )
  70. )
  71. )
  72. )
  73. )
  74. ) as SERVICES_STATUS
  75. , d.`P_DEALDATE_TERM`
  76. from `DEALS_TABLE` as d
  77. left join `SERVICES` as SV on (SV.`ID_DEALS_TABLE`=d.`ID`)
  78. where
  79. SV.`NAME_LIST_SERVICES` not in('EMAIL')
  80. group by d.`ID`
  81. */
  82. // TODO: dodac adresy z pozostalych uslug VOIP, TVCP, etc...
  83. $sqlUserID = '';
  84. if ($userID > 0) {
  85. $sqlUserID = " and SV.`ID_BILLING_USERS`='{$userID}' ";
  86. }
  87. return "INSERT INTO `" . self::get_table() . "` (`ID_DEALS`, `ID_BILLING_USERS`, `A_CREATE_DATE`, `SERVICES_STATUS`, `P_DEALDATE_TERM`, `SERVICES_S_ADDRESS_STREET`)
  88. select d.`ID`
  89. , d.`ID_BILLING_USERS`
  90. , '" . date("Y-m-d") . "' as A_CREATE_DATE
  91. , GROUP_CONCAT({$A_STATUS_L2_SQL}) as SERVICES_STATUS
  92. , d.`P_DEALDATE_TERM`
  93. , GROUP_CONCAT( DISTINCT
  94. CASE SV.`NAME_LIST_SERVICES`
  95. WHEN 'USERS2' THEN (select `S_ADDRESS_STREET` from `SES_USERS2_A` as ses_a where ses_a.`ID_SERVICES`=SV.`ID`)
  96. WHEN 'TV' THEN (select `S_ADDRESS_STREET` from `SES_TV_A` as ses_a where ses_a.`ID_SERVICES`=SV.`ID`)
  97. END
  98. ) as SERVICES_S_ADDRESS_STREET
  99. from `DEALS_TABLE` as d
  100. left join `SERVICES` as SV on (SV.`ID_DEALS_TABLE`=d.`ID`)
  101. where
  102. SV.`NAME_LIST_SERVICES` not in('EMAIL','WWW_MASS')
  103. {$sqlUserID}
  104. group by d.`ID`
  105. ";
  106. }
  107. public static function get_last_update() {
  108. $db = DB::getDB();
  109. $_table = self::get_table();
  110. if (empty($_SESSION["{$_table}_COLUMN"]['_last_update'])) {
  111. $_SESSION["{$_table}_COLUMN"]['_last_update'] = '';
  112. $sql = "SELECT td.`A_CREATE_DATE` from `temp_DEALS_STATUS` as td limit 1";
  113. $res = $db->query($sql, false);
  114. if ($res) {
  115. if ($r = $db->fetch($res)) {
  116. $_SESSION["{$_table}_COLUMN"]['_last_update'] = $r->A_CREATE_DATE;
  117. }
  118. }
  119. }
  120. return $_SESSION["{$_table}_COLUMN"]['_last_update'];
  121. }
  122. public static function set_last_update( $date ) {
  123. $_SESSION[self::get_table().'_COLUMN']['_last_update'] = $date;
  124. }
  125. public static function is_updated() {
  126. if (self::get_last_update() >= date("Y-m-d")) {
  127. return true;
  128. }
  129. return false;
  130. }
  131. public static function run_update( $force = false, &$dbg_msgs = array() ) {
  132. $run = false;
  133. if ($force) {
  134. $run = true;
  135. } else {
  136. $run = ! self::is_updated();
  137. }
  138. if ($run) {
  139. $db = DB::getDB();
  140. $sql = self::get_drop_query();
  141. $dbg_msgs []= $sql;
  142. $db->query($sql, false);
  143. $sql = self::get_create_query();
  144. $dbg_msgs []= $sql;
  145. $db->query($sql, false);
  146. $sql = self::get_insert_query();
  147. $dbg_msgs []= $sql;
  148. $db->query($sql, false);
  149. $sql = self::get_update_query();
  150. $dbg_msgs []= $sql;
  151. $db->query($sql, false);
  152. $date = date("Y-m-d");
  153. self::set_last_update($date);
  154. }
  155. return true;
  156. }
  157. public static function run_update_for_user($userID) {
  158. $db = DB::getDB();
  159. $_table = self::get_table();
  160. $sql = "delete from `{$_table}` where `ID_BILLING_USERS`='{$userID}'";
  161. $db->query($sql, false);
  162. $sql = self::get_insert_query($userID);
  163. $dbg_msgs []= $sql;
  164. $db->query($sql, false);
  165. $sql = self::get_update_query($userID);
  166. $dbg_msgs []= $sql;
  167. $db->query($sql, false);
  168. return true;
  169. }
  170. public static function get_deals_stats_by_months( $force = false ) {
  171. $deals_stats = array();
  172. // read from cache if not force
  173. if (!$force) {
  174. if (!empty($_SESSION[self::get_table().'_COLUMN']['_stats_by_month_cache'])) {
  175. foreach ($_SESSION[self::get_table().'_COLUMN']['_stats_by_month_cache'] as $k => $v) {
  176. $deals_stats [ $k ] = $v;
  177. }
  178. return $deals_stats;
  179. }
  180. }
  181. $loop_limit = 12;
  182. // fetch months, count
  183. $sql = "select
  184. IF(substr(td.`P_DEALDATE_TERM`, 1, 7) < '" . date('Y-m') . "-00'
  185. , '0000-00'
  186. , substr(td.`P_DEALDATE_TERM`, 1, 7)
  187. ) as deal_term_month
  188. , count(1) as cnt
  189. from `" . self::get_table() . "` as td
  190. where td.`DEALS_ACTIVE`=1
  191. group by deal_term_month
  192. order by deal_term_month asc
  193. ";
  194. if (V::get('DBG_DEALS', '', $_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sql (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>';}
  195. $res = DB::query( $sql );
  196. while ($r = DB::fetch_assoc( $res )) {
  197. $r['suma'] = 0;
  198. $deals_stats [ $r['deal_term_month'] ]= $r;
  199. }
  200. // fetch suma, for first 12 months
  201. foreach ($deals_stats as $k_month => $v_month) {
  202. if ($loop_limit-- <= 0) {
  203. break;
  204. }
  205. $db = DB::getDB();
  206. $sqlWhereTerm = "td.`P_DEALDATE_TERM` like '" . $k_month . "-%'";
  207. if ($k_month == '0000-00') {
  208. $sqlWhereTerm = "substr(td.`P_DEALDATE_TERM`, 1, 7)<'" . date('Y-m') . "-00'";
  209. }
  210. $sql = "select
  211. sum( (u.`ABONAMENT_VALUE` * (100 + u.`VAT`))/100 ) as suma
  212. from `" . self::get_table() . "` as td
  213. left join `SERVICES` as u on (u.`ID_DEALS_TABLE`=td.`ID_DEALS`)
  214. where
  215. {$sqlWhereTerm}
  216. and td.`DEALS_ACTIVE`=1
  217. and u.`ID` is not null
  218. ";
  219. if (V::get('DBG_DEALS', '', $_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sql (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>';}
  220. $res = $db->query($sql);
  221. if ($r = $db->fetch($res)) {
  222. if (isset($deals_stats[$k_month])) {
  223. $deals_stats[$k_month]['suma'] = $r->suma;
  224. }
  225. }
  226. }
  227. if (V::get('DBG_DEALS', '', $_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">deals_stats (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($deals_stats);echo'</pre>';}
  228. // save in cache
  229. $_SESSION[self::get_table().'_COLUMN']['_stats_by_month_cache'] = array();
  230. foreach ($deals_stats as $k => $v) {
  231. $_SESSION[self::get_table().'_COLUMN']['_stats_by_month_cache'][ $k ] = $v;
  232. }
  233. return $deals_stats;
  234. }
  235. public static function get_deals_stats_by_month($_month, $_limitstart) {
  236. $sql_where = "";
  237. $sql_limit = "limit 100";
  238. if ($_limitstart > 0) {
  239. $sql_limit .= " offset " . $_limitstart;
  240. }
  241. $sql_where_and_arr = array();
  242. if ($_month == '0000-00') {// filter - P_DEALDATE_TERM
  243. $sql_where_and_arr []= "td.`P_DEALDATE_TERM`<'" . date("Y-m") . "-00'";
  244. } else {
  245. $sql_where_and_arr []= "td.`P_DEALDATE_TERM` like '" . $_month . "-%'";
  246. }
  247. $sql_where_and_arr []= "td.`DEALS_ACTIVE`=1";// filter - ACTIVE: tylko aktywne umowy
  248. $sql_where = (!empty($sql_where_and_arr))? "where" . "\n" . implode(" and ", $sql_where_and_arr) : "";
  249. $db = DB::getDB();
  250. $sql = "select
  251. d.`ID`
  252. , d.`ID_BILLING_USERS`
  253. , d.`P_DEALPREFIX`
  254. , d.`P_DEALNUMBER`
  255. , d.`P_DEALNUMBER_OLD`
  256. , d.`P_DEALDATE`
  257. , d.`P_DEALDATE_TERM`
  258. , d.`ANEX_NEEDED`
  259. , d.`ANEX_CONFIRM_DATE`
  260. , d.`DEALDESC`
  261. , d.`A_STATUS`
  262. , BUA.`P_PHONE` as BUA_P_PHONE
  263. , concat_ws(' ', BUA.`P_NAME`, BUA.`P_NAME_SECOND`) as BUA_P_NAME
  264. from `temp_DEALS_STATUS` as td
  265. left join `DEALS_TABLE` as d on (d.`ID`=td.`ID_DEALS`)
  266. left join `BILLING_USERS_ADD` as BUA on (BUA.`id_users`=d.`ID_BILLING_USERS`)
  267. " . $sql_where . "
  268. order by d.`ID` ASC
  269. " . $sql_limit . "
  270. ";
  271. if (V::get('DBG_DEALS', '', $_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sql (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>';}
  272. $res = $db->query($sql);
  273. while ($r = $db->fetch($res)) {
  274. $deals [$r->ID] = $r;
  275. }
  276. if (empty($deals)) {
  277. return $deals;
  278. }
  279. // function fetch_services(&$deals)
  280. $sql_deals_ids = array();
  281. foreach ($deals as $k_id => $v_deal) {
  282. $sql_deals_ids []= "'" . $k_id . "'";
  283. $deals[$k_id]->uslugi = array();
  284. }
  285. if (empty($sql_deals_ids)) {
  286. return $deals;
  287. }
  288. global $A_STATUS_L2_SQL;// @from .config.php, @require SERVICES as SV
  289. $db = DB::getDB();
  290. $sql = "select
  291. SV.`ID`
  292. , SV.`ID_DEALS_TABLE`
  293. , $A_STATUS_L2_SQL as A_STATUS
  294. , SV.`ID_OFFERS`
  295. , SV.`ABONAMENT_VALUE`
  296. , SV.`NAME_LIST_SERVICES`
  297. , SV.`VAT`
  298. , IF(SV.`NAME_LIST_SERVICES`='USERS2'
  299. , (select `S_ADDRESS_STREET` from `SES_USERS2_A` as ses_a where ses_a.`ID_SERVICES`=SV.`ID`)
  300. , IF(SV.`NAME_LIST_SERVICES`='TV'
  301. , (select `S_ADDRESS_STREET` from `SES_TV_A` as ses_a where ses_a.`ID_SERVICES`=SV.`ID`)
  302. , null
  303. )
  304. ) as S_ADDRESS_STREET
  305. from `SERVICES` as SV
  306. where SV.`ID_DEALS_TABLE` in(" . implode(", ", $sql_deals_ids) . ")
  307. ";
  308. if (V::get('DBG_DEALS', '', $_GET)){echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">sql (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($sql);echo'</pre>';}
  309. $res = $db->query($sql);
  310. while ($r = $db->fetch($res)) {
  311. $deals [$r->ID_DEALS_TABLE]->uslugi[$r->ID] = $r;
  312. }
  313. // } // fetch_services
  314. return $deals;
  315. }
  316. }