ColumnDealsStatus.php 13 KB

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