tools.class.php 161 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643
  1. <?php
  2. if (!defined('IN_ONLINE')) {
  3. exit('Access Denied');
  4. }
  5. /**
  6. * Description of operation_log
  7. *
  8. * @author Administrator
  9. */
  10. class tools {
  11. private static $_tools;
  12. public static function getInstance() {
  13. if (!self::$_tools) {
  14. $c = __CLASS__;
  15. self::$_tools = new $c;
  16. }
  17. return self::$_tools;
  18. }
  19. /*
  20. * update password when login success
  21. */
  22. public function updatePassword() {
  23. if ($_SESSION['ONLINE_USER']['is_demo'] == "t") {
  24. $str = "DEMO cannot update password.";
  25. } else {
  26. $opsw = common::check_input($_POST ['opsw']);
  27. $npsw = common::check_input($_POST ['npsw']);
  28. $username = _getLoginName();
  29. $msg = common::checkPasswordRule($username, $npsw);
  30. //为空代表验证通过
  31. if (empty($msg)) {
  32. $sql = "select ra_password as password from ra_online_user where lower(user_login) = '" . strtolower($username) . "'";
  33. $rs = common::excuteObjectSql($sql);
  34. $str = '';
  35. if (!empty($rs)) {
  36. if ($rs['password'] == $opsw) {
  37. $sql = "UPDATE ra_online_user SET ra_password = '" . $npsw . "', last_pwd_change = now() WHERE lower(user_login) = '" . strtolower($username) . "'";
  38. $rls = common::excuteUpdateSql($sql);
  39. if (!$rls) {
  40. $str = "Password modification failed!";
  41. } else {
  42. $str = " Your password has been modified!";
  43. //发送更新成功的邮件
  44. $user_type = $_SESSION['ONLINE_USER']['user_type'];
  45. $user_login = _getLoginName();
  46. utils::sendEmailWithPasswordUpdate($user_login,$user_type);
  47. }
  48. } else {
  49. $str = "Old password is incorrect!";
  50. }
  51. } else {
  52. $str = "Old password is incorrect!";
  53. }
  54. } else {
  55. $str = $msg;
  56. }
  57. }
  58. $returnData = array("msg" => $str);
  59. common::echo_json_encode(200, $returnData);
  60. exit();
  61. }
  62. public function markSystem(){
  63. $operate = utils::_get('operate');
  64. $operate = strtolower($operate);
  65. if ($operate == "mark_save") {
  66. $suggestion = utils::implode(",",$_POST['suggestion']);
  67. $proposal = common::check_input($_POST['proposal']);
  68. $expression = common::check_input($_POST['expression']);
  69. $complete_funtionality = common::check_input($_POST['Complete_funtionality']);
  70. $accurate_data = common::check_input($_POST['Accurate_data']);
  71. $clear_information = common::check_input($_POST['Clear_information']);
  72. $easy_to_use = common::check_input($_POST['Easy_to_use']);
  73. $system_Performance = common::check_input($_POST['System_Performance']);
  74. $username = common::check_input($_POST['username']);
  75. $user_type = _isApexLogin() ? "employee" : "customer";
  76. if(!isset($_SESSION['ONLINE_USER'])){
  77. $user_type = "other";
  78. }
  79. $loginName = _getLoginName();
  80. $loginEamil = _getLoginEamil();
  81. //如果在没有登录前,没有登录信息,指定用户-- 这里逻辑取消,没有登录相当于匿名用户的评价,无法获取用户名
  82. // if(!isset($_SESSION['ONLINE_USER'])){
  83. // $user_type = "Customer";
  84. // if(!empty($username)){
  85. // $loginName = $username;
  86. // $loginEamil = common::excuteOneSql("select email from public.ra_online_user u where lower(user_login) = '" . strtolower($username) . "'");
  87. // }
  88. // }
  89. $sql = "INSERT INTO public.customer_service_user_mark(user_type, user_name, suggestion, proposal, expression, complete_funtionality,
  90. accurate_data, clear_information, easy_to_use, system_performance,
  91. created_time,email)
  92. VALUES ('$user_type', '$loginName', '$suggestion', '$proposal', '$expression', '$complete_funtionality',
  93. '$accurate_data', '$clear_information', '$easy_to_use', '$system_Performance', now(),'$loginEamil')";
  94. common::excuteUpdateSql($sql);
  95. $data = array("msg" =>"success");
  96. common::echo_json_encode(200,$data);
  97. exit();
  98. }
  99. }
  100. public function user_system_setting(){
  101. $operate = utils::_get('operate');
  102. $operate = strtolower($operate);
  103. if ($operate == "personal_profile_init") {
  104. // get system config
  105. $sql = "SELECT lower(ra_name) as ra_name, ra_value from ra_online_config where lower(ra_name) in ('employee_password_change_cycle', 'customer_password_change_cycle')";
  106. $rs1s = common::excuteListSql($sql);
  107. foreach ($rs1s as $rs1) {
  108. if ($rs1['ra_name'] == 'employee_password_change_cycle')
  109. $EMPLOYEE_PASSWORD_CHANGE_CYCLE = $rs1['ra_value'];
  110. if ($rs1['ra_name'] == 'customer_password_change_cycle')
  111. $CUSTOMER_PASSWORD_CHANGE_CYCLE = $rs1['ra_value'];
  112. }
  113. $sql="select item_value from config where item='passwordChangePeriod'";
  114. $pcp = common::excuteObjectSql($sql);
  115. $passwordChangePeriod = json_decode($pcp["item_value"],true);
  116. if (_isApexLogin()) {
  117. $PASSWORD_CHANGE_CYCLE = $EMPLOYEE_PASSWORD_CHANGE_CYCLE;
  118. //如果有新配置,则采用新配置
  119. if (!empty($pcp)) {
  120. $PASSWORD_CHANGE_CYCLE = $passwordChangePeriod["Employee"]["days"];
  121. }
  122. } else {
  123. $PASSWORD_CHANGE_CYCLE = $CUSTOMER_PASSWORD_CHANGE_CYCLE;
  124. //如果有新配置,则采用新配置
  125. if (!empty($pcp)) {
  126. $PASSWORD_CHANGE_CYCLE = $passwordChangePeriod["Customer"]["days"];
  127. }
  128. }
  129. $sql = "select u.first_name,u.last_name,u.user_login,u.email,EXTRACT(DAY from (now() - u.last_pwd_change)) as last_pwd_change_date,
  130. ue.date_format,ue.numbers_format
  131. from public.ra_online_user u
  132. left join public.kln_user_extend ue on u.user_login = ue.user_login
  133. where lower(u.user_login) = '".strtolower(_getLoginName())."' ";
  134. $data = common::excuteObjectSql($sql);
  135. $data["expire_day"] = $PASSWORD_CHANGE_CYCLE - $data['last_pwd_change_date'];
  136. common::echo_json_encode(200,$data);
  137. exit();
  138. }
  139. if ($operate == "personal_profile_save") {
  140. $save_model = common::check_input($_POST['save_model']);
  141. if ($save_model == "profile"){
  142. $first_name = common::check_input($_POST['first_name']);
  143. $last_name = common::check_input($_POST['last_name']);
  144. $sql = "update public.ra_online_user set first_name = '$first_name',last_name = '$last_name' where lower(user_login) = '".strtolower(_getLoginName())."'";
  145. }else{
  146. $date_format = common::check_input($_REQUEST['date_format']);
  147. $numbers_format = common::check_input($_REQUEST['numbers_format']);
  148. $exist_kln_user = common::excuteObjectSql("select user_login from public.kln_user_extend where lower(user_login) = '".strtolower(_getLoginName())."'");
  149. if (!empty($exist_kln_user['user_login'])){
  150. $sql = "update public.kln_user_extend set date_format = '$date_format',numbers_format = '$numbers_format' where lower(user_login) = '".strtolower(_getLoginName())."'";
  151. } else {
  152. $sql = "INSERT INTO public.kln_user_extend(user_login, date_format, numbers_format, subscribe_hbol)
  153. VALUES ('"._getLoginName()."', '$date_format', '$numbers_format', null);";
  154. }
  155. }
  156. common::excuteUpdateSql($sql);
  157. $data = array("msg" => "save Successful");
  158. common::echo_json_encode(200,$data);
  159. exit();
  160. }
  161. if ($operate == "subscribe_notification_default_init"){
  162. $default_time_zone = common::check_input($_POST['default_time_zone']);
  163. $default_time_zone_db = utils::comvertutcinfo($default_time_zone);
  164. //检查用户是否是设置过subscribe_notification
  165. $count = common::excuteOneSql("select count(*) from public.notifications_rules where
  166. notifications_type = 'Subscribe'
  167. and lower(user_login) = '".strtolower(_getLoginName())."'");
  168. if( $count > 0){
  169. } else {
  170. //手动的保存两条,通用默认规则
  171. $sql="INSERT INTO public.notifications_rules(
  172. user_login, notifications_type, rules_type, ocean_milestone,
  173. air_milestone, frequency_type, daily_time, daily_time_zone,
  174. weekly_week, weekly_time, weekly_time_zone, method_by_email, method_by_message,
  175. event_details, frequency_display, method_display,shipment_details,
  176. shipment_transport_mode,shipment_etd_limit,shipment_eta_limit,shipment_etd_limit_from,shipment_eta_limit_from)
  177. VALUES ('"._getLoginName()."', 'Subscribe', 'Milestone_Update', 'IFFBCF;IFFCPU;IFFREC;IFFONB;IFFDEP;IFFCSN;IFFARR;IFFAFD;IFFECR;IFFDEL',
  178. 'IFFBCF;IFFCPU;IFFREC;IFFONB;IFFARR;IFFADW;IFFDDW;IFFDEL', 'Daily', '09:00', '$default_time_zone_db',
  179. '', null, '', 'false', 'true',
  180. 'Ocean Milestones: Booking Confirmation,Cargo Pickup,Cargo Arrived at Origin,On Board,Departure,Arrival Notification,Arrived at Final Discharge Port,Arrived at Final Destination,Empty Container Return,Document Turnover / Delivered;
  181. Air Milestones: Booking Confirmation,Cargo Pickup,Cargo Arrived at CFS,Departure,Landed at Destination Port,Arrived Destination Warehouse,Departed Destination Warehouse,Document Turnover / Delivered;', 'Daily, 09:00, ".$default_time_zone."', 'System Message','',
  182. '','','','','');";
  183. $sql.="INSERT INTO public.notifications_rules(
  184. user_login, notifications_type, rules_type,
  185. ocean_etd_change, ocean_etd_old_sub_new,ocean_etd_old_sub_new_unit,ocean_eta_change,ocean_eta_old_sub_new,ocean_eta_old_sub_new_unit,
  186. air_etd_change, air_etd_old_sub_new,air_etd_old_sub_new_unit,air_eta_change,air_eta_old_sub_new,air_eta_old_sub_new_unit,
  187. frequency_type, daily_time, daily_time_zone,
  188. weekly_week, weekly_time, weekly_time_zone, method_by_email, method_by_message,
  189. event_details, frequency_display, method_display,shipment_details,
  190. shipment_transport_mode,shipment_etd_limit,shipment_eta_limit,shipment_etd_limit_from,shipment_eta_limit_from)
  191. VALUES ('"._getLoginName()."', 'Subscribe', 'ETD/ETA_Change',
  192. 'false','','','false','1','days',
  193. 'false','','','false','1','days',
  194. 'Daily', '09:00', '$default_time_zone_db',
  195. '', null, '', 'false', 'true',
  196. '[Ocean]ETA: Notify only when time difference ≥ 1 Day(s);
  197. [Air]ETA: Notify only when time difference ≥ 1 Day(s);', 'Daily, 09:00, ".$default_time_zone."', 'System Message','',
  198. '','','','','');";
  199. $rs = common::excuteUpdateSql($sql);
  200. if (!$rs) {
  201. $data = array("msg" => "Subscribe Notification Initialized Error");
  202. common::echo_json_encode(500,$data);
  203. exit();
  204. }
  205. }
  206. $data = array("msg"=>"Has been initialized");
  207. common::echo_json_encode(200,$data);
  208. exit();
  209. }
  210. if ($operate == "subscribe_notification_init") {
  211. $subscribur_data =array();
  212. //查询用户对应的Rule
  213. $subscribe_rule_sql = "select *,TO_CHAR(daily_time, 'HH24:MI') as _daily_time,
  214. TO_CHAR(weekly_time, 'HH24:MI') as _weekly_time
  215. from public.notifications_rules where notifications_type = 'Subscribe' and lower(user_login) = '".strtolower(_getLoginName())."' order by id desc";
  216. $subscribe_rules = common::excuteListSql($subscribe_rule_sql);
  217. $all_rules = array("Milestone_Update","Container_Status_Update","Departure/Arrival_Delay","ETD/ETA_Change");
  218. foreach($all_rules as $rule_name){
  219. $rules = $this->getSubscribeRules($rule_name,$subscribe_rules);
  220. $subscribur_data[$rule_name] = $rules;
  221. }
  222. //整合拼接addedRules
  223. $addedRules = array();
  224. foreach($subscribe_rules as $addedRule){
  225. $addedRules[] = array(
  226. "visible" => false,
  227. "id" =>$addedRule['id'],
  228. "Event" =>$addedRule['rules_type'],
  229. "Event Details" =>$addedRule['event_details'],
  230. "Frequency" =>$addedRule['frequency_display'],
  231. "Methods" =>$addedRule['method_display']);
  232. }
  233. $subscribur_data['addedRules'] = array("tableData"=>$addedRules);
  234. //获取subscribe shipment 当前页数cp,每页ps
  235. $subscribeShipmentWithPage = $this->getSubscribeShipment(1,15);
  236. $subscribur_data['subscribeShipmentWithPage'] = $subscribeShipmentWithPage;
  237. common::echo_json_encode(200,$subscribur_data);
  238. exit();
  239. }
  240. if ($operate == "subscribe_notification_event_update"){
  241. $rules_type = common::check_input($_POST["rules_type"]);
  242. //判断该规则是否存在
  243. $exist = common::excuteObjectSql("select user_login,id from public.notifications_rules where notifications_type = 'Subscribe' and rules_type = '".$rules_type."'
  244. and lower(user_login) = '".strtolower(_getLoginName())."'");
  245. //检查参数是否正常
  246. $err = $this->checkedNotificationParam();
  247. if(!empty($err)){
  248. $data = array("msg" =>"Parameter error: ".$err);
  249. common::echo_json_encode(500,$data);
  250. exit();
  251. }
  252. $updateOrInsert = empty($exist) ? "insert" : "update";
  253. $sql = $this->getNotificationsRulesUpdateSql($updateOrInsert,$rules_type,"Subscribe",$exist['id']);
  254. $rs = common::excuteUpdateSql($sql);
  255. if ($rs === FALSE){
  256. $data = array("msg" => "Update Error");
  257. } else{
  258. $data = array("msg" => "Update Successful");
  259. //返回addedRules 全部列表
  260. $subscribe_rule_sql = "select * from public.notifications_rules where notifications_type = 'Subscribe' and lower(user_login) = '".strtolower(_getLoginName())."' order by id desc";
  261. $subscribe_rules = common::excuteListSql($subscribe_rule_sql);
  262. //整合拼接addedRules
  263. $addedRules = array();
  264. foreach($subscribe_rules as $addedRule){
  265. $addedRules[] = array(
  266. "id" =>$addedRule['id'],
  267. "Event" =>$addedRule['rules_type'],
  268. "Event Details" =>$addedRule['event_details'],
  269. "Frequency" =>$addedRule['frequency_display'],
  270. "Methods" =>$addedRule['method_display']);
  271. }
  272. $data['addedRules'] = array("tableData"=>$addedRules);
  273. }
  274. common::echo_json_encode(200,$data);
  275. exit();
  276. }
  277. if ($operate == "subscribe_notification_rules_delete"){
  278. $rules_type = common::check_input($_POST['rules_type']);
  279. $sql = "delete from notifications_rules where notifications_type = 'Subscribe'
  280. and rules_type = '$rules_type' and lower(user_login) = '".strtolower(_getLoginName())."'";
  281. common::excuteUpdateSql($sql);
  282. $data = array("msg" => "Delete Successful");
  283. common::echo_json_encode(200,$data);
  284. exit();
  285. }
  286. if ($operate == "subscribe_shipment"){
  287. $serial_no = common::deCode($_POST['serial_no'], 'D');
  288. $is_subscribe = common::check_input($_POST['is_subscribe']);
  289. if($is_subscribe == "true"){
  290. $exist = common::excuteOneSql("select user_login from public.kln_user_subscribed where lower(user_login) = '".strtolower(_getLoginName())."' and subscribed_serial_no = '$serial_no'");
  291. if(!empty($exist)){
  292. $data = array("msg" => "Subscribe exist,Please check");
  293. common::echo_json_encode(200,$data);
  294. exit();
  295. }
  296. $sql = "INSERT INTO public.kln_user_subscribed(user_login, subscribed_serial_no, create_user, create_time)
  297. VALUES ('"._getLoginName()."', '$serial_no', '"._getLoginName()."', now());";
  298. common::excuteUpdateSql($sql);
  299. $data = array("msg" => "Subscribe Successful");
  300. common::echo_json_encode(200,$data);
  301. exit();
  302. }else{
  303. //取消订阅
  304. $sql = "delete from public.kln_user_subscribed where lower(user_login) = '".strtolower(_getLoginName())."' and subscribed_serial_no = '$serial_no';";
  305. common::excuteUpdateSql($sql);
  306. $data = array("msg" => "Cancel Subscribe successfully");
  307. common::echo_json_encode(200,$data);
  308. exit();
  309. }
  310. }
  311. if ($operate == "subscribe_shipment_search"){
  312. $cp = common::check_input($_POST ['cp']); //current_page
  313. $ps = common::check_input($_POST ['ps']); //ps
  314. $arrTmp = $this->getSubscribeShipment($cp,$ps);
  315. common::echo_json_encode(200,$arrTmp);
  316. exit();
  317. }
  318. }
  319. public function user_monitoring_setting(){
  320. $operate = utils::_get('operate');
  321. $operate = strtolower($operate);
  322. if ($operate == "monitoring_rules_init"){
  323. $ret = array();
  324. //Milestone Update的页面配置数据
  325. $milestones = common::excuteListSql("select * from public.customer_service_milestone_sno order by type, sno");
  326. $oceanMilestone = array();
  327. $airMilestone = array();
  328. foreach($milestones as $milestone){
  329. if($milestone['type'] == "air"){
  330. $airMilestone[] = array("label"=>$milestone['description'],"value"=>$milestone['code']);
  331. }
  332. if($milestone['type'] == "sea"){
  333. $oceanMilestone[] = array("label"=>$milestone['description'],"value"=>$milestone['code']);
  334. }
  335. }
  336. $ret["OceanCheckBoxList"] = $oceanMilestone;
  337. $ret["AirCheckBoxList"] = $airMilestone;
  338. //Milestone Update的结构处理
  339. //这里基准event 写死, 根据online查询页面的通用的来, 这里需提问确定
  340. //$event =common::getEDICtnrEvent();
  341. $event = common::excuteListSql("select ra_name as event_name,ra_order,description
  342. from public.ra_online_edi_event e
  343. where e.ra_name in('I','VD','VA','UV','AL','AR','OA','RD') order by e.ra_order desc");
  344. $ctnrStatus = array();
  345. foreach($event as $e){
  346. $ctnrStatus[] = array("label"=>$e['description'],"value"=>$e['event_name']);
  347. }
  348. $ret["CtnrCheckBoxList"] = $ctnrStatus;
  349. common::echo_json_encode(200,$ret);
  350. exit();
  351. }
  352. if ($operate == "monitoring_rules_search") {
  353. $cp = common::check_input($_POST ['cp']); //current_page
  354. $ps = common::check_input($_POST ['ps']); //ps
  355. if (empty($ps))
  356. $ps = 15;
  357. $sql = "select count(1) from public.notifications_rules where lower(user_login) = '".strtolower(_getLoginName())."' and notifications_type = 'Monitoring'";
  358. $rc = common::excuteOneSql($sql);
  359. $tp = ceil($rc / $ps);
  360. if ($rc > 0) {
  361. $sql = "select *,replace(rules_type, '_', ' ') AS _rules_type_display,
  362. case when rules_type = 'Milestone_Update' then 'Milestone'
  363. when rules_type = 'Container_Status_Update' then 'Container'
  364. when rules_type = 'Departure/Arrival_Delay' then 'Departure'
  365. when rules_type = 'ETD/ETA_Change' then 'ETDChange'
  366. else '' end as notifications_option
  367. from public.notifications_rules
  368. where lower(user_login) = '".strtolower(_getLoginName())."'
  369. and notifications_type = 'Monitoring' order by id desc limit " . $ps . " offset " . ($cp - 1) * $ps;
  370. $monitoringRules = common::excuteListSql($sql);
  371. $arrTmp = array('monitoringRules' => $monitoringRules,
  372. 'rc' => intval($rc),
  373. 'ps' => intval($ps),
  374. 'cp' => intval($cp),
  375. 'tp' => intval($tp)
  376. );
  377. } else {
  378. $arrTmp = array('monitoringRules' => array(),
  379. 'rc' => intval($rc),
  380. 'ps' => intval($ps),
  381. 'cp' => intval($cp),
  382. 'tp' => intval($tp)
  383. );
  384. }
  385. common::echo_json_encode(200,$arrTmp);
  386. exit();
  387. }
  388. if ($operate == "monitoring_rules_edit"){
  389. $id = $_POST['id'];
  390. $rules_type = common::check_input($_POST['rules_type']);
  391. $subscribe_rule_sql = "select *,
  392. TO_CHAR(daily_time, 'HH24:MI') as _daily_time,
  393. TO_CHAR(weekly_time, 'HH24:MI') as _weekly_time,
  394. case when rules_type = 'Milestone_Update' then 'Milestone'
  395. when rules_type = 'Container_Status_Update' then 'Container'
  396. when rules_type = 'Departure/Arrival_Delay' then 'Departure'
  397. when rules_type = 'ETD/ETA_Change' then 'ETDChange'
  398. else '' end as notifications_option
  399. from public.notifications_rules where notifications_type = 'Monitoring' and lower(user_login) = '".strtolower(_getLoginName())."'
  400. and id = '$id' order by id";
  401. $subscribe_rules = common::excuteListSql($subscribe_rule_sql);
  402. $rules = $this->getSubscribeRules($rules_type,$subscribe_rules);
  403. //数据转换前端需要的显示的格式
  404. $rules["shipment_transport_mode"] = utils::converModeToDisplay($rules["shipment_transport_mode"]);
  405. $monitoring_data[$rules_type] = $rules;
  406. common::echo_json_encode(200,$monitoring_data);
  407. exit();
  408. }
  409. if ($operate == "monitoring_rules_do") {
  410. $rules_type = common::check_input($_POST["rules_type"]);
  411. //检查参数是否正常
  412. $err = $this->checkedNotificationParam();
  413. if(!empty($err)){
  414. $data = array("msg" =>"Parameter error: ".$err);
  415. common::echo_json_encode(500,$data);
  416. exit();
  417. }
  418. //检查编辑提交的Monitoring规则,是否允许保存
  419. $msg = $this->checkedMonitoringRulesSave($rules_type);
  420. if(!empty($msg)){
  421. $data = array("msg" =>$msg);
  422. common::echo_json_encode(200,$data);
  423. exit();
  424. }
  425. $updateOrInsert = "insert";
  426. if(isset($_POST['id']) && !empty($_POST['id'])){
  427. $updateOrInsert = "update";
  428. }
  429. $sql = $this->getNotificationsRulesUpdateSql($updateOrInsert,$rules_type,"Monitoring",$_POST['id']);
  430. $rs = common::excuteUpdateSql($sql);
  431. if ($rs === FALSE){
  432. $data = array("msg" => "Update Error");
  433. } else{
  434. $data = array("msg" => "Update Successful");
  435. }
  436. common::echo_json_encode(200,$data);
  437. exit();
  438. }
  439. if ($operate == "monitoring_rules_delete"){
  440. $id = common::check_input($_POST['id']);
  441. $sql = "delete from notifications_rules where notifications_type = 'Monitoring'
  442. and lower(user_login) = '".strtolower(_getLoginName())."' and id = '$id'";
  443. common::excuteUpdateSql($sql);
  444. $data = array("msg" => "Delete Successful");
  445. common::echo_json_encode(200,$data);
  446. exit();
  447. }
  448. }
  449. public function notifications_rules(){
  450. $operate = utils::_get('operate');
  451. $operate = strtolower($operate);
  452. if ($operate == "notifications_init_old"){
  453. $rules_type = common::check_input($_REQUEST['rules_type']);
  454. $milestoneData = array();
  455. $containerData = array();
  456. $delayData = array();
  457. $changeData = array();
  458. if ($rules_type == "all"){
  459. $rules_type = "Milestone_Update;Container_Status_Update;Departure/Arrival_Delay;ETD/ETA_Change;Feature_Update;Passwond_Notifcations";
  460. $allData = $this->getNotifications($rules_type,"all");
  461. $milestoneData = $allData['Milestone_Update'];
  462. $containerData = $allData['Container_Status_Update'];
  463. $delayData = $allData['Departure/Arrival_Delay'];
  464. $changeData = $allData['ETD/ETA_Change'];
  465. $featureUpdate = $allData['Feature_Update'];
  466. $passwond_Notifcations = $allData['Passwond_Notifcations'];
  467. } else {
  468. $data = $this->getNotifications($rules_type,"all");
  469. if($rules_type == "Milestone_Update"){
  470. $milestoneData = $data['Milestone_Update'];
  471. }elseif($rules_type == "Container_Status_Update"){
  472. $containerData = $data['Container_Status_Update'];
  473. }elseif($rules_type == "Departure/Arrival_Delay"){
  474. $delayData = $data['Departure/Arrival_Delay'];
  475. }elseif($rules_type == "ETD/ETA_Change"){
  476. $changeData = $data['ETD/ETA_Change'];
  477. }elseif($rules_type == "Feature_Update"){
  478. $featureUpdate = $data['Feature_Update'];
  479. }elseif($rules_type == "Passwond_Notifcations"){
  480. $passwond_Notifcations = $data['Passwond_Notifcations'];
  481. }
  482. }
  483. $data = array("milestoneData"=>$milestoneData,"containerData"=>$containerData,"delayData"=>$delayData,
  484. "changeData"=>$changeData,"featureUpdate"=>$featureUpdate,"passwond_Notifcations"=>$passwond_Notifcations);
  485. $instant_sum = array();
  486. foreach($data as $v){
  487. if(!empty($v['instant'])){
  488. foreach($v['instant'] as $instant){
  489. $instant_sum[] = $instant;
  490. }
  491. }
  492. if(!empty($v['daily'])){
  493. $dailys = common::handleDailyWeekedData($v['daily']);
  494. foreach($dailys as $dailyArr){
  495. //取第一组的第一个显示
  496. $dailyFristAndFrist = utils::getDailyAndweeklyFrist($dailyArr);
  497. $instant_sum[]= $dailyFristAndFrist;
  498. }
  499. }
  500. if(!empty($v['weekly'])){
  501. $weeklys = common::handleDailyWeekedData($v['weekly']);
  502. foreach($weeklys as $weeklyArr){
  503. $weeklyFristAndFrist = utils::getDailyAndweeklyFrist($weeklyArr);
  504. $instant_sum[]= $weeklyFristAndFrist;
  505. }
  506. }
  507. }
  508. //根据时间顺序排序
  509. $insert_dates = array_column($instant_sum, 'insert_date');
  510. array_multisort($insert_dates, SORT_DESC, $instant_sum);
  511. $info = array();
  512. foreach($instant_sum as $mInfo){
  513. $eventCard = $this->getEventCard($mInfo);
  514. if(!empty($mInfo['other_type']) && $mInfo['other_type'] == "password"){
  515. $info[] = array("notificationType"=>"password","info" =>$eventCard);
  516. }elseif(!empty($mInfo['other_type']) && $mInfo['other_type'] == "feature"){
  517. $info[] = array("notificationType"=>"feature","info" =>$eventCard);
  518. }else{
  519. $info[] = array("notificationType"=>"event","info" =>$eventCard);
  520. }
  521. }
  522. $returnData = $info;
  523. common::echo_json_encode(200,$returnData);
  524. exit();
  525. }
  526. if ($operate == "notifications_init"){
  527. $rules_type = common::check_input($_REQUEST['rules_type']);
  528. if ($rules_type == "all"){
  529. $rules_type = "Milestone_Update;Container_Status_Update;Departure/Arrival_Delay;ETD/ETA_Change;Feature_Update;Passwond_Notifcations";
  530. $data = $this->getNotificationsNew($rules_type,"all");
  531. } else {
  532. $data = $this->getNotificationsNew($rules_type,"all");
  533. }
  534. foreach($data as $k => $v){
  535. if($v['frequency_type'] == 'Daily' || $v['frequency_type'] == 'Weekly'){
  536. $numericRecords = $v['total_count'];
  537. $numericRecords_one = 0;
  538. $numericRecords_two = 0;
  539. if($v['notifiation_type'] == 'Departure/Arrival_Delay'){
  540. $numericRecords_one = $v['departure_count'];
  541. $numericRecords_two = $v['arrival_count'];
  542. }
  543. if ($v['notifiation_type'] == 'ETD/ETA_Change'){
  544. $numericRecords_one = $v['etd_count'];
  545. $numericRecords_two = $v['eta_count'];
  546. }
  547. $data[$k]["numericRecords"]= intval($numericRecords);
  548. //对Delay and change 特殊处理
  549. $data[$k]["numericRecords_one"]= intval($numericRecords_one);
  550. $data[$k]["numericRecords_two"]= intval($numericRecords_two);
  551. }
  552. }
  553. $info = array();
  554. foreach($data as $mInfo){
  555. $eventCard = $this->getEventCard($mInfo);
  556. if(!empty($mInfo['other_type']) && $mInfo['other_type'] == "password"){
  557. $info[] = array("notificationType"=>"password","info" =>$eventCard);
  558. }elseif(!empty($mInfo['other_type']) && $mInfo['other_type'] == "feature"){
  559. $info[] = array("notificationType"=>"feature","info" =>$eventCard);
  560. }else{
  561. $info[] = array("notificationType"=>"event","info" =>$eventCard);
  562. }
  563. }
  564. $returnData = $info;
  565. common::echo_json_encode(200,$returnData);
  566. exit();
  567. }
  568. if($operate == "notifications_see_all_old"){
  569. $rules_type = common::check_input($_REQUEST['rules_type']);
  570. $frequency_type = common::check_input($_REQUEST['frequency_type']); //这个只会传daily 和weekly
  571. $insert_date_format = common::check_input($_REQUEST['insert_date_format']);
  572. $notificationsData = $this->getNotifications($rules_type,$frequency_type,$insert_date_format);
  573. $moreData = $notificationsData[$rules_type][strtolower($frequency_type)];
  574. //这个函数里面带有分开计数的信息
  575. $dataInfo =utils::getDailyAndweeklyFrist($moreData);
  576. $returnData = array();
  577. $notificationList = array();
  578. $ids = array();
  579. foreach($moreData as $key => $data){
  580. $eventCard = $this->getEventCard($data);
  581. //sea all的数据格式和查询全部的格式有区别
  582. if($key == 0){
  583. $returnData["title"] = $eventCard["title"];
  584. if($eventCard["type"] == "change" || $eventCard["type"] == "delay"){
  585. $returnData["etdOrdeparturNum"] = $dataInfo["numericRecords_one"];
  586. $returnData["etaOrarrivalNum"] =$dataInfo["numericRecords_two"];
  587. $returnData["type"] =$eventCard["type"];
  588. }else{
  589. $returnData["numericRecords"] = $dataInfo["numericRecords"];
  590. }
  591. }
  592. //移除不需要的字段
  593. unset($eventCard["title"]);
  594. $notificationList[] = $eventCard;
  595. $ids[] = $data['id'];
  596. }
  597. if(!empty($notificationList)){
  598. $returnData["notificationList"] = $notificationList;
  599. }
  600. //点击seall会默认全部标记为已读
  601. if(!empty($ids)){
  602. $more_param = common::getInNotInSqlForSearch(strtolower(utils::implode(';',$ids)));
  603. $markReadSql = "update public.kln_notifiation_info set is_send_message = now(),readed_date = now() where id in ($more_param)";
  604. common::excuteUpdateSql($markReadSql);
  605. }
  606. common::echo_json_encode(200,$returnData);
  607. exit();
  608. }
  609. if($operate == "notifications_see_all"){
  610. $rules_type = common::check_input($_REQUEST['rules_type']);
  611. $frequency_type = common::check_input($_REQUEST['frequency_type']); //这个只会传daily 和weekly
  612. $insert_date_format = common::check_input($_REQUEST['insert_date_format']);
  613. $moreData = $this->getNotificationsSeeAll($rules_type,$frequency_type,$insert_date_format);
  614. //这个函数里面带有分开计数的信息
  615. $dataInfo =utils::getDailyAndweeklyFrist($moreData);
  616. $returnData = array();
  617. $notificationList = array();
  618. $ids = array();
  619. foreach($moreData as $key => $data){
  620. $eventCard = $this->getEventCard($data);
  621. //sea all的数据格式和查询全部的格式有区别
  622. if($key == 0){
  623. $returnData["title"] = $eventCard["title"];
  624. if($eventCard["type"] == "change" || $eventCard["type"] == "delay"){
  625. $returnData["etdOrdeparturNum"] = $dataInfo["numericRecords_one"];
  626. $returnData["etaOrarrivalNum"] =$dataInfo["numericRecords_two"];
  627. $returnData["type"] =$eventCard["type"];
  628. }else{
  629. $returnData["numericRecords"] = $dataInfo["numericRecords"];
  630. }
  631. }
  632. //移除不需要的字段
  633. unset($eventCard["title"]);
  634. $notificationList[] = $eventCard;
  635. $ids[] = $data['id'];
  636. }
  637. if(!empty($notificationList)){
  638. $returnData["notificationList"] = $notificationList;
  639. }
  640. //点击seall会默认全部标记为已读
  641. if(!empty($ids)){
  642. $more_param = common::getInNotInSqlForSearch(strtolower(utils::implode(';',$ids)));
  643. $markReadSql = "update public.kln_notifiation_info set is_send_message = now(),readed_date = now() where id in ($more_param)";
  644. common::excuteUpdateSql($markReadSql);
  645. }
  646. common::echo_json_encode(200,$returnData);
  647. exit();
  648. }
  649. if($operate == "notifications_read"){
  650. $read_type = common::check_input($_POST["read_type"]);
  651. $id = $_POST["id"];
  652. //代表改用户下的所有信息全部标记为已读
  653. if ($read_type == "true"){
  654. $rs = common::excuteUpdateSql("update public.kln_notifiation_info set is_send_message = now(),readed_date = now() where lower(user_login) in ('".strtolower(_getLoginName())."','all_user')");
  655. }else{
  656. //处理一下前端提交的不正确参数
  657. $param_id = array();
  658. foreach($id as $_ids){
  659. if(!empty($_ids)){
  660. $param_id[] = $_ids;
  661. }
  662. }
  663. if(empty($param_id)){
  664. //为空直接返回成功
  665. $returnData = array("msg" =>"Success");
  666. common::echo_json_encode(200,$returnData);
  667. }
  668. $more_param = common::getInNotInSqlForSearch(strtolower(utils::implode(';',$param_id)));
  669. $markReadSql = "update public.kln_notifiation_info set is_send_message = now(),readed_date = now() where id in ($more_param)";
  670. $rs = common::excuteUpdateSql($markReadSql);
  671. }
  672. if ($rs === FALSE){
  673. $returnData = array("msg" =>"Error");
  674. common::echo_json_encode(500,$returnData);
  675. }else{
  676. $returnData = array("msg" =>"Success");
  677. common::echo_json_encode(200,$returnData);
  678. }
  679. exit();
  680. }
  681. if ($operate == "notifications_message_init_old"){
  682. //查询所有情况得未读情况 查询最近一年的情况
  683. $unreadSql = "with countTbale as (
  684. select ni.notifiation_type,
  685. case when COALESCE(ni.frequency_type,'') = 'Daily'
  686. then to_char(timezone(ni.daily_time_zone, ni.insert_date),'Mon DD, YYYY')
  687. when COALESCE(ni.frequency_type,'') = 'Weekly'
  688. then to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL,'Mon DD, YYYY')
  689. || ' - ' ||
  690. to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date + (((ni.weekly_week::integer - EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer + 7) % 7)-1 + CASE WHEN EXTRACT(DOW FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer=ni.weekly_week::integer THEN 7 ELSE 0 END ||' days')::INTERVAL,'Mon DD, YYYY')
  691. else ''
  692. end as insert_date_format
  693. from public.kln_notifiation_info ni
  694. where ni.insert_date > NOW() - INTERVAL '3 months'
  695. and ni.notifications_method = 'true' and ni.is_send_message is null
  696. and lower(ni.user_login) = '".strtolower(_getLoginName())."'
  697. and ((ni.frequency_type = 'Daily'
  698. and (case when (timezone(ni.daily_time_zone, NOW())::time > ni.daily_time::time)
  699. then timezone(ni.daily_time_zone,ni.insert_date)::date < timezone(ni.daily_time_zone, NOW())::date
  700. else
  701. timezone(ni.daily_time_zone,ni.insert_date)::date < timezone(ni.daily_time_zone, NOW())::date - '1 days'::INTERVAL
  702. end))
  703. or (ni.frequency_type = 'Weekly'
  704. and (case when (timezone(ni.weekly_time_zone, NOW())::time < ni.weekly_time::time
  705. and timezone(ni.weekly_time_zone,NOW())::date = (timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date)
  706. then timezone(ni.weekly_time_zone,ni.insert_date)::date < ((timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date - '7 days'::INTERVAL)::date
  707. else
  708. timezone(ni.weekly_time_zone,ni.insert_date)::date < (timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date
  709. end)))
  710. group by ni.notifiation_type,insert_date_format
  711. union all
  712. select ni.notifiation_type, '' as insert_date_format
  713. from public.kln_notifiation_info ni
  714. where ni.insert_date > NOW() - INTERVAL '3 months'
  715. and lower(ni.user_login) in ('".strtolower(_getLoginName())."','all_user')
  716. and ni.notifications_method = 'true' and ni.is_send_message is null
  717. and frequency_type = 'Instant'
  718. )
  719. select
  720. sum(case when (1<>1 or (notifiation_type='Milestone_Update')) then 1 else 0 end) as m_rc,
  721. sum(case when (1<>1 or (notifiation_type='Container_Status_Update')) then 1 else 0 end) as cs_rc,
  722. sum(case when (1<>1 or (notifiation_type='Departure/Arrival_Delay')) then 1 else 0 end) as da_rc,
  723. sum(case when (1<>1 or (notifiation_type='ETD/ETA_Change')) then 1 else 0 end) as ec_rc,
  724. sum(case when (1<>1 or (notifiation_type='Feature_Update')) then 1 else 0 end) as f_rc
  725. from countTbale ";
  726. $count = common::excuteObjectSql($unreadSql);
  727. //单独的选中的数据
  728. $rules_type = common::check_input($_REQUEST['rules_type']);
  729. $data = $this->getNotifications($rules_type,"all");
  730. $unreadCount = 0;
  731. $readCount = 0;
  732. $instant_sum = array();
  733. if(!empty($data[$rules_type]['instant'])){
  734. foreach($data[$rules_type]['instant'] as $instant){
  735. $instant_sum[] = $instant;
  736. if(!empty($instant["is_send_message"])){
  737. $readCount +=1;
  738. }else{
  739. $unreadCount +=1;
  740. }
  741. }
  742. }
  743. if(!empty($data[$rules_type]['daily'])){
  744. $dailys = common::handleDailyWeekedData($data[$rules_type]['daily']);
  745. foreach($dailys as $dailyArr){
  746. //取第一组的第一个显示
  747. $dailyFristAndFrist = utils::getDailyAndweeklyFrist($dailyArr);
  748. $instant_sum[]= $dailyFristAndFrist;
  749. if(!empty($dailyFristAndFrist["is_send_message"])){
  750. $readCount +=1;
  751. }else{
  752. $unreadCount +=1;
  753. }
  754. }
  755. }
  756. if(!empty($data[$rules_type]['weekly'])){
  757. $weeklys = common::handleDailyWeekedData($data[$rules_type]['weekly']);
  758. foreach($weeklys as $weeklyArr){
  759. $weeklyFristAndFrist = utils::getDailyAndweeklyFrist($weeklyArr);
  760. $instant_sum[]= $weeklyFristAndFrist;
  761. if(!empty($weeklyFristAndFrist["is_send_message"])){
  762. $readCount +=1;
  763. }else{
  764. $unreadCount +=1;
  765. }
  766. }
  767. }
  768. //根据时间顺序排序
  769. $insert_dates = array_column($instant_sum, 'insert_date');
  770. array_multisort($insert_dates, SORT_DESC, $instant_sum);
  771. $info = array();
  772. foreach($instant_sum as $mInfo){
  773. $eventCard = $this->getEventCard($mInfo);
  774. if(!empty($mInfo['other_type']) && $mInfo['other_type'] == "password"){
  775. $info[] = array("notificationType"=>"password","info" =>$eventCard);
  776. }elseif(!empty($mInfo['other_type']) && $mInfo['other_type'] == "feature"){
  777. $info[] = array("notificationType"=>"feature","info" =>$eventCard);
  778. }else{
  779. $info[] = array("notificationType"=>"event","info" =>$eventCard);
  780. }
  781. }
  782. //返回数据结构
  783. $returnData = array();
  784. $m_rc = empty($count['m_rc']) ? 0 : intval($count['m_rc']);
  785. $cs_rc = empty($count['cs_rc']) ? 0 : intval($count['cs_rc']);
  786. $da_rc = empty($count['da_rc']) ? 0 : intval($count['da_rc']);
  787. $ec_rc = empty($count['ec_rc']) ? 0 : intval($count['ec_rc']);
  788. $f_rc = empty($count['f_rc']) ? 0 : intval($count['f_rc']);
  789. $returnData['countList'] = array($m_rc,$cs_rc,$da_rc,$ec_rc,$f_rc);
  790. $returnData['allCount'] =count($instant_sum);
  791. $returnData['unreadCount'] =$unreadCount;
  792. $returnData['readCount'] =$readCount;
  793. $returnData['cardList'] =$info;
  794. common::echo_json_encode(200,$returnData);
  795. exit();
  796. }
  797. if ($operate == "notifications_message_init"){
  798. //查询所有情况得未读情况 查询最近一年的情况
  799. $unreadSql = "with countTbale as (
  800. select ni.notifiation_type,
  801. case when COALESCE(ni.frequency_type,'') = 'Daily'
  802. then to_char(timezone(ni.daily_time_zone, ni.insert_date),'Mon DD, YYYY')
  803. when COALESCE(ni.frequency_type,'') = 'Weekly'
  804. then to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL,'Mon DD, YYYY')
  805. || ' - ' ||
  806. to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date + (((ni.weekly_week::integer - EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer + 7) % 7)-1 + CASE WHEN EXTRACT(DOW FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer=ni.weekly_week::integer THEN 7 ELSE 0 END ||' days')::INTERVAL,'Mon DD, YYYY')
  807. else ''
  808. end as insert_date_format
  809. from public.kln_notifiation_info ni
  810. where ni.insert_date > NOW() - INTERVAL '3 months'
  811. and ni.notifications_method = 'true' and ni.is_send_message is null
  812. and lower(ni.user_login) = '".strtolower(_getLoginName())."'
  813. and ((ni.frequency_type = 'Daily'
  814. and (case when (timezone(ni.daily_time_zone, NOW())::time > ni.daily_time::time)
  815. then timezone(ni.daily_time_zone,ni.insert_date)::date < timezone(ni.daily_time_zone, NOW())::date
  816. else
  817. timezone(ni.daily_time_zone,ni.insert_date)::date < timezone(ni.daily_time_zone, NOW())::date - '1 days'::INTERVAL
  818. end))
  819. or (ni.frequency_type = 'Weekly'
  820. and (case when (timezone(ni.weekly_time_zone, NOW())::time < ni.weekly_time::time
  821. and timezone(ni.weekly_time_zone,NOW())::date = (timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date)
  822. then timezone(ni.weekly_time_zone,ni.insert_date)::date < ((timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date - '7 days'::INTERVAL)::date
  823. else
  824. timezone(ni.weekly_time_zone,ni.insert_date)::date < (timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date
  825. end)))
  826. group by ni.notifiation_type,insert_date_format
  827. union all
  828. select ni.notifiation_type, '' as insert_date_format
  829. from public.kln_notifiation_info ni
  830. where ni.insert_date > NOW() - INTERVAL '3 months'
  831. and lower(ni.user_login) in ('".strtolower(_getLoginName())."','all_user')
  832. and ni.notifications_method = 'true' and ni.is_send_message is null
  833. and frequency_type = 'Instant'
  834. )
  835. select
  836. sum(case when (1<>1 or (notifiation_type='Milestone_Update')) then 1 else 0 end) as m_rc,
  837. sum(case when (1<>1 or (notifiation_type='Container_Status_Update')) then 1 else 0 end) as cs_rc,
  838. sum(case when (1<>1 or (notifiation_type='Departure/Arrival_Delay')) then 1 else 0 end) as da_rc,
  839. sum(case when (1<>1 or (notifiation_type='ETD/ETA_Change')) then 1 else 0 end) as ec_rc,
  840. sum(case when (1<>1 or (notifiation_type='Feature_Update')) then 1 else 0 end) as f_rc
  841. from countTbale ";
  842. $count = common::excuteObjectSql($unreadSql);
  843. //单独的选中的数据
  844. $rules_type = common::check_input($_REQUEST['rules_type']);
  845. $data = $this->getNotificationsNew($rules_type,"all");
  846. $unreadCount = 0;
  847. $readCount = 0;
  848. foreach($data as $k => $v){
  849. if(!empty($v["is_send_message"])){
  850. $readCount +=1;
  851. }else{
  852. $unreadCount +=1;
  853. }
  854. if($v['frequency_type'] == 'Daily' || $v['frequency_type'] == 'Weekly'){
  855. $numericRecords = $v['total_count'];
  856. $numericRecords_one = 0;
  857. $numericRecords_two = 0;
  858. if($v['notifiation_type'] == 'Departure/Arrival_Delay'){
  859. $numericRecords_one = $v['departure_count'];
  860. $numericRecords_two = $v['arrival_count'];
  861. }
  862. if ($v['notifiation_type'] == 'ETD/ETA_Change'){
  863. $numericRecords_one = $v['etd_count'];
  864. $numericRecords_two = $v['eta_count'];
  865. }
  866. $data[$k]["numericRecords"]= intval($numericRecords);
  867. //对Delay and change 特殊处理
  868. $data[$k]["numericRecords_one"]= intval($numericRecords_one);
  869. $data[$k]["numericRecords_two"]= intval($numericRecords_two);
  870. }
  871. }
  872. $info = array();
  873. foreach($data as $mInfo){
  874. $eventCard = $this->getEventCard($mInfo);
  875. if(!empty($mInfo['other_type']) && $mInfo['other_type'] == "password"){
  876. $info[] = array("notificationType"=>"password","info" =>$eventCard);
  877. }elseif(!empty($mInfo['other_type']) && $mInfo['other_type'] == "feature"){
  878. $info[] = array("notificationType"=>"feature","info" =>$eventCard);
  879. }else{
  880. $info[] = array("notificationType"=>"event","info" =>$eventCard);
  881. }
  882. }
  883. //返回数据结构
  884. $returnData = array();
  885. $m_rc = empty($count['m_rc']) ? 0 : intval($count['m_rc']);
  886. $cs_rc = empty($count['cs_rc']) ? 0 : intval($count['cs_rc']);
  887. $da_rc = empty($count['da_rc']) ? 0 : intval($count['da_rc']);
  888. $ec_rc = empty($count['ec_rc']) ? 0 : intval($count['ec_rc']);
  889. $f_rc = empty($count['f_rc']) ? 0 : intval($count['f_rc']);
  890. $returnData['countList'] = array($m_rc,$cs_rc,$da_rc,$ec_rc,$f_rc);
  891. $returnData['allCount'] =count($data);
  892. $returnData['unreadCount'] =$unreadCount;
  893. $returnData['readCount'] =$readCount;
  894. $returnData['cardList'] =$info;
  895. common::echo_json_encode(200,$returnData);
  896. exit();
  897. }
  898. if ($operate == "check_notifications_message"){
  899. $checkUnread = "select id
  900. from public.kln_notifiation_info ni
  901. inner join LATERAL (select oo.h_bol,oo.transport_mode,oo.order_from,oo.m_bol
  902. from public.kln_ocean oo
  903. where oo.serial_no = ni.serial_no limit 1) ccc on true
  904. where lower(ni.user_login) in ('".strtolower(_getLoginName())."','all_user')
  905. AND ni.insert_date > NOW() - INTERVAL '3 months'
  906. and (ni.frequency_type = 'Instant'
  907. or (ni.frequency_type = 'Daily'
  908. and (case when (timezone(ni.daily_time_zone, NOW())::time > ni.daily_time::time)
  909. then timezone(ni.daily_time_zone,ni.insert_date)::date < timezone(ni.daily_time_zone, NOW())::date
  910. else
  911. timezone(ni.daily_time_zone,ni.insert_date)::date < timezone(ni.daily_time_zone, NOW())::date - '1 days'::INTERVAL
  912. end))
  913. or (ni.frequency_type = 'Weekly'
  914. and (case when (timezone(ni.weekly_time_zone, NOW())::time < ni.weekly_time::time
  915. and timezone(ni.weekly_time_zone,NOW())::date = (timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date)
  916. then timezone(ni.weekly_time_zone,ni.insert_date)::date < ((timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date - '7 days'::INTERVAL)::date
  917. else
  918. timezone(ni.weekly_time_zone,ni.insert_date)::date < (timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date
  919. end)))
  920. and ni.notifications_method = true and is_send_message is null limit 1";
  921. $unread = common::excuteObjectSql($checkUnread);
  922. $returnData = array("has_message" =>!empty($unread));
  923. common::echo_json_encode(200,$returnData);
  924. }
  925. }
  926. /**
  927. * 遍历查找对应的rule。
  928. */
  929. public function getSubscribeRules($rule_name,$subscribe_rules){
  930. //初始是不显示,没有值的情况
  931. $ret = array("is_display" => false);
  932. foreach($subscribe_rules as $rules){
  933. if($rules['rules_type'] == $rule_name){
  934. $rules["is_display"] = true;
  935. $rules["daily_time"] = $rules["_daily_time"];
  936. $rules["weekly_time"] = $rules["_weekly_time"];
  937. $rules["weekly_week"] = common::getWeek($rules["weekly_week"]);
  938. $rules["daily_time_zone"] = utils::comvertutcinfo($rules["daily_time_zone"]);
  939. $rules["weekly_time_zone"] = utils::comvertutcinfo($rules["weekly_time_zone"]);
  940. //字符串转数字
  941. $rules["ocean_atd_sub_etd"] = common::convertoint($rules["ocean_atd_sub_etd"]);
  942. $rules["ocean_ata_sub_eta"] = common::convertoint($rules["ocean_ata_sub_eta"]);
  943. $rules["air_atd_sub_etd"] = common::convertoint($rules["air_atd_sub_etd"]);
  944. $rules["air_ata_sub_eta"] = common::convertoint($rules["air_ata_sub_eta"]);
  945. $rules["ocean_etd_old_sub_new"] = common::convertoint($rules["ocean_etd_old_sub_new"]);
  946. $rules["ocean_eta_old_sub_new"] = common::convertoint($rules["ocean_eta_old_sub_new"]);
  947. $rules["air_etd_old_sub_new"] = common::convertoint($rules["air_etd_old_sub_new"]);
  948. $rules["air_eta_old_sub_new"] = common::convertoint($rules["air_eta_old_sub_new"]);
  949. $rules["shipment_etd_limit"] = common::convertoint($rules["shipment_etd_limit"]);
  950. $rules["shipment_eta_limit"] = common::convertoint($rules["shipment_eta_limit"]);
  951. $rules["shipment_etd_limit_from"] = common::convertoint($rules["shipment_etd_limit_from"]);
  952. $rules["shipment_eta_limit_from"] = common::convertoint($rules["shipment_eta_limit_from"]);
  953. // $air_etd_old_sub_new_unit=="Day(s)" ? "days":"hours";
  954. $rules["ocean_atd_sub_etd_unit"] = common::convertoVue($rules["ocean_atd_sub_etd_unit"]);
  955. $rules["ocean_ata_sub_eta_unit"] = common::convertoVue($rules["ocean_ata_sub_eta_unit"]);
  956. $rules["air_atd_sub_etd_unit"] = common::convertoVue($rules["air_atd_sub_etd_unit"]);
  957. $rules["air_ata_sub_eta_unit"] = common::convertoVue($rules["air_ata_sub_eta_unit"]);
  958. $rules["ocean_etd_old_sub_new_unit"] = common::convertoVue($rules["ocean_etd_old_sub_new_unit"]);
  959. $rules["ocean_eta_old_sub_new_unit"] = common::convertoVue($rules["ocean_eta_old_sub_new_unit"]);
  960. $rules["air_etd_old_sub_new_unit"] = common::convertoVue($rules["air_etd_old_sub_new_unit"]);
  961. $rules["air_eta_old_sub_new_unit"] = common::convertoVue($rules["air_eta_old_sub_new_unit"]);
  962. $ret = $rules;
  963. }
  964. }
  965. //Milestone Update的结构处理,处理init page load
  966. if($rule_name == "Milestone_Update"){
  967. //Milestone Update的页面配置数据
  968. $milestones = common::excuteListSql("select * from public.customer_service_milestone_sno order by type, sno");
  969. $oceanMilestone = array();
  970. $airMilestone = array();
  971. foreach($milestones as $milestone){
  972. if($milestone['type'] == "air"){
  973. $airMilestone[] = array("label"=>$milestone['description'],"value"=>$milestone['code']);
  974. }
  975. if($milestone['type'] == "sea"){
  976. $oceanMilestone[] = array("label"=>$milestone['description'],"value"=>$milestone['code']);
  977. }
  978. }
  979. $ret["OceanCheckBoxList"] = $oceanMilestone;
  980. $ret["AirCheckBoxList"] = $airMilestone;
  981. $oceanMilestoneSetting = !empty($ret['ocean_milestone']) ? explode(";",$ret['ocean_milestone']) : array();
  982. $airMilestoneSetting = !empty($ret['air_milestone']) ? explode(";",$ret['air_milestone']): array();
  983. $ret["OceanCheckedList"] = $oceanMilestoneSetting;
  984. $ret["AirCheckedList"] = $airMilestoneSetting;
  985. }
  986. //Milestone Update的结构处理
  987. if($rule_name == "Container_Status_Update"){
  988. //这里基准event 写死, 根据online查询页面的通用的来, 这里需提问确定
  989. //$event =common::getEDICtnrEvent();
  990. $event = common::excuteListSql("select ra_name as event_name,ra_order,description
  991. from public.ra_online_edi_event e
  992. where e.ra_name in('I','VD','VA','UV','AL','AR','OA','RD') order by e.ra_order desc");
  993. $ctnrStatus = array();
  994. foreach($event as $e){
  995. $ctnrStatus[] = array("label"=>$e['description'],"value"=>$e['event_name']);
  996. }
  997. $ret["CtnrCheckBoxList"] = $ctnrStatus;
  998. $ctnrStatusSetting = !empty($ret['ocean_ctnr_status']) ? explode(";",$ret['ocean_ctnr_status']) : array();
  999. $ret["CtnrCheckedList"] = $ctnrStatusSetting;
  1000. }
  1001. return $ret;
  1002. }
  1003. /**
  1004. * 查询对应用户订阅的shipment信息.可能存在分页查询,如果有需要就改正
  1005. * cp current_page
  1006. */
  1007. public function getSubscribeShipment($cp,$ps){
  1008. if (empty($cp)){
  1009. $cp = 1;
  1010. }
  1011. if (empty($ps)){
  1012. $ps = 15;
  1013. }
  1014. $sql = "select count(1) from public.kln_user_subscribed u
  1015. inner join public.kln_ocean o on o.serial_no = u.subscribed_serial_no
  1016. where lower(user_login) = '".strtolower(_getLoginName())."'";
  1017. $rc = common::excuteOneSql($sql);
  1018. $tp = ceil($rc / $ps);
  1019. if ($rc > 0) {
  1020. $sql = "select o.serial_no,order_from as _schemas,o.h_bol,
  1021. o.shipper,o.consignee,o.etd,o.eta,
  1022. case when transport_mode = 'sea'
  1023. then (select sn.description
  1024. from public.ocean_milestone a
  1025. inner join public.customer_service_milestone_sno sn on sn.code=a.code and sn.type = 'sea'
  1026. where a.serial_no=o.serial_no and act_date is not null order by sn.sno desc limit 1)
  1027. when transport_mode = 'air' and order_from = 'public'
  1028. then (select sn.description
  1029. from public.air_milestone a
  1030. inner join public.customer_service_milestone_sno sn on sn.code=a.code and sn.type = 'air'
  1031. where a.serial_no=o.serial_no and act_date is not null order by sn.sno desc limit 1)
  1032. when transport_mode = 'air' and order_from = 'sfs'
  1033. then (select sn.description
  1034. from sfs.air_milestone a
  1035. inner join public.customer_service_milestone_sno sn on sn.code=a.code and sn.type = 'air'
  1036. where a.serial_no=o.serial_no and act_date is not null order by sn.sno desc limit 1)
  1037. else '' end as recent_milestone
  1038. from public.kln_user_subscribed u
  1039. inner join public.kln_ocean o on o.serial_no = u.subscribed_serial_no
  1040. where lower(user_login) = '".strtolower(_getLoginName())."' order by u.id desc limit " . $ps . " offset " . ($cp - 1) * $ps;
  1041. $subscribeShipment = common::excuteListSql($sql);
  1042. foreach($subscribeShipment as $key => $val){
  1043. $subscribeShipment[$key]["__serial_no"] = common::deCode($val['serial_no'], 'E');
  1044. }
  1045. $arrTmp = array('tableData' => $subscribeShipment,
  1046. 'rc' => intval($rc),
  1047. 'ps' => intval($ps),
  1048. 'cp' => intval($cp),
  1049. 'tp' => intval($tp)
  1050. );
  1051. } else {
  1052. $arrTmp = array('tableData' => array(),
  1053. 'rc' => intval($rc),
  1054. 'ps' => intval($ps),
  1055. 'cp' => intval($cp),
  1056. 'tp' => intval($tp),
  1057. );
  1058. }
  1059. return $arrTmp;
  1060. }
  1061. public function checkedNotificationParam(){
  1062. $rules_type = common::check_input($_POST["rules_type"]);
  1063. //先判断异常数据
  1064. $msg = "";
  1065. if ($rules_type == "Departure/Arrival_Delay"){
  1066. $ocean_atd_sub_etd = common::check_input($_POST['ocean_atd_sub_etd']);
  1067. if (!empty($ocean_atd_sub_etd) && !ctype_digit($ocean_atd_sub_etd)) {
  1068. $msg = "Ocean Delayed(ATD-ETD) is not Number";
  1069. }
  1070. $ocean_ata_sub_eta = common::check_input($_POST['ocean_ata_sub_eta']);
  1071. if (!empty($ocean_ata_sub_eta) && !ctype_digit($ocean_ata_sub_eta)) {
  1072. $msg = "Ocean Delayed(ATA-ETA) is not Number";
  1073. }
  1074. $air_atd_sub_etd = common::check_input($_POST['air_atd_sub_etd']);
  1075. if (!empty($air_atd_sub_etd) && !ctype_digit($air_atd_sub_etd)) {
  1076. $msg = "Air Delayed(ATD-ETD) is not Number";
  1077. }
  1078. $air_ata_sub_eta = common::check_input($_POST['air_ata_sub_eta']);
  1079. if (!empty($air_ata_sub_eta) && !ctype_digit($air_ata_sub_eta)) {
  1080. $msg = "Air Delayed(ATA-ETA) is not Number";
  1081. }
  1082. }
  1083. if ($rules_type == "ETD/ETA_Change"){
  1084. $ocean_etd_old_sub_new = common::check_input($_POST['ocean_etd_old_sub_new']);
  1085. if (!empty($ocean_etd_old_sub_new) && !ctype_digit($ocean_etd_old_sub_new)) {
  1086. $msg = "Ocean Notify(ETD) is not Number";
  1087. }
  1088. $ocean_eta_old_sub_new = common::check_input($_POST['ocean_eta_old_sub_new']);
  1089. if (!empty($ocean_eta_old_sub_new) && !ctype_digit($ocean_eta_old_sub_new)) {
  1090. $msg = "Ocean Notify(ETA) is not Number";
  1091. }
  1092. $air_etd_old_sub_new = common::check_input($_POST['air_etd_old_sub_new']);
  1093. if (!empty($air_etd_old_sub_new) && !ctype_digit($air_etd_old_sub_new)) {
  1094. $msg = "Air Notify(ETD) is not Number";
  1095. }
  1096. $air_eta_old_sub_new = common::check_input($_POST['air_eta_old_sub_new']);
  1097. if (!empty($air_eta_old_sub_new) && !ctype_digit($air_eta_old_sub_new)) {
  1098. $msg = "Air Notify(ETA) is not Number";
  1099. }
  1100. }
  1101. return $msg;
  1102. }
  1103. public function getNotificationsRulesUpdateSql($updateOrInsert,$rules_type,$notifications_type,$id){
  1104. $sql = "";
  1105. //先删后加 这个逻辑有移除
  1106. // if($updateOrInsert == "update"){
  1107. // $sql.="delete from public.notifications_rules where rules_type = '$rules_type'
  1108. // and notifications_type = '$notifications_type' and lower(user_login) = '".strtolower(_getLoginName())."'
  1109. // and id = '$id';";
  1110. // }
  1111. //这个几个参数是所有规则都有的参数
  1112. $frequency_type = common::check_input($_POST['frequency_type']);
  1113. $daily_time = "null";
  1114. $daily_time_zone = "";
  1115. $weekly_week = "";
  1116. $weekly_time = "null";
  1117. $weekly_time_zone = "";
  1118. if(strtolower($frequency_type) == "daily"){
  1119. $daily_time = "'".common::check_input($_POST['daily_time'])."'";
  1120. $daily_time_zone = common::check_input($_POST['daily_time_zone']);
  1121. $daily_time_zone = utils::comvertutcinfo($daily_time_zone);
  1122. } elseif (strtolower($frequency_type) == "weekly"){
  1123. $weekly_week = common::check_input($_POST['weekly_week']);
  1124. $weekly_time = "'".common::check_input($_POST['weekly_time'])."'";
  1125. $weekly_time_zone = common::check_input($_POST['weekly_time_zone']);
  1126. $weekly_time_zone = utils::comvertutcinfo($weekly_time_zone);
  1127. }
  1128. $method_by_email = !empty($_POST['method_by_email']) ? common::check_input($_POST['method_by_email']) : 'false';
  1129. $method_by_message = !empty($_POST['method_by_message']) ? common::check_input($_POST['method_by_message']) : 'false';
  1130. $event_details = common::check_input($_POST['event_details']);
  1131. $frequency_display = common::check_input($_POST['frequency_display']);
  1132. $method_display = common::check_input($_POST['method_display']);
  1133. $shipment_detail = common::check_input($_POST['shipment_details']);
  1134. //当规则是 Monitoring类型是,需要配置的range
  1135. $shipment_transport_mode = "";
  1136. $shipment_etd_limit = "";
  1137. $shipment_eta_limit = "";
  1138. $shipment_etd_limit_from = "";
  1139. $shipment_eta_limit_from = "";
  1140. if($notifications_type == "Monitoring"){
  1141. $shipment_transport_mode = utils::converModeToDB($_POST['shipment_transport_mode']);
  1142. $shipment_etd_limit = common::check_input($_POST['shipment_etd_limit']);
  1143. $shipment_eta_limit = common::check_input($_POST['shipment_eta_limit']);
  1144. $shipment_etd_limit_from = common::check_input($_POST['shipment_etd_limit_from']);
  1145. $shipment_eta_limit_from = common::check_input($_POST['shipment_eta_limit_from']);
  1146. }
  1147. if ($rules_type == "Milestone_Update"){
  1148. $ocean_milestone = utils::implode(";",$_POST['ocean_milestone']);
  1149. $air_milestone = utils::implode(";",$_POST['air_milestone']);
  1150. //改为update 不在是删除后添加,为了保留原始顺序
  1151. if ($updateOrInsert == "update"){
  1152. $sql.="update public.notifications_rules set ocean_milestone = '$ocean_milestone',air_milestone = '$air_milestone',
  1153. frequency_type = '$frequency_type',daily_time = $daily_time, daily_time_zone = '$daily_time_zone',
  1154. weekly_week = '$weekly_week', weekly_time = $weekly_time, weekly_time_zone = '$weekly_time_zone', method_by_email = '$method_by_email',method_by_message = '$method_by_message',
  1155. event_details = '$event_details', frequency_display = '$frequency_display', method_display = '$method_display', shipment_details = '$shipment_detail',
  1156. shipment_transport_mode = '$shipment_transport_mode', shipment_etd_limit = '$shipment_etd_limit', shipment_eta_limit = '$shipment_eta_limit',
  1157. shipment_etd_limit_from = '$shipment_etd_limit_from', shipment_eta_limit_from = '$shipment_eta_limit_from'
  1158. where rules_type = '$rules_type'
  1159. and notifications_type = '$notifications_type' and lower(user_login) = '".strtolower(_getLoginName())."'
  1160. and id = '$id';";
  1161. } else {
  1162. $sql.="INSERT INTO public.notifications_rules(
  1163. user_login, notifications_type, rules_type, ocean_milestone,
  1164. air_milestone, frequency_type, daily_time, daily_time_zone,
  1165. weekly_week, weekly_time, weekly_time_zone, method_by_email, method_by_message,
  1166. event_details, frequency_display, method_display,shipment_details,
  1167. shipment_transport_mode,shipment_etd_limit,shipment_eta_limit,shipment_etd_limit_from,shipment_eta_limit_from)
  1168. VALUES ('".strtolower(_getLoginName())."', '$notifications_type', '$rules_type', '$ocean_milestone',
  1169. '$air_milestone', '$frequency_type', $daily_time, '$daily_time_zone',
  1170. '$weekly_week', $weekly_time, '$weekly_time_zone', '$method_by_email', '$method_by_message',
  1171. '$event_details', '$frequency_display', '$method_display','$shipment_detail',
  1172. '$shipment_transport_mode','$shipment_etd_limit','$shipment_eta_limit','$shipment_etd_limit_from','$shipment_eta_limit_from');";
  1173. }
  1174. }
  1175. if ($rules_type == "Container_Status_Update"){
  1176. $ocean_ctnr_status = utils::implode(";",$_POST['ocean_ctnr_status']);
  1177. //改为update 不在是删除后添加,为了保留原始顺序
  1178. if ($updateOrInsert == "update"){
  1179. $sql.="update public.notifications_rules set ocean_ctnr_status = '$ocean_ctnr_status',
  1180. frequency_type = '$frequency_type',daily_time = $daily_time, daily_time_zone = '$daily_time_zone',
  1181. weekly_week = '$weekly_week', weekly_time = $weekly_time, weekly_time_zone = '$weekly_time_zone', method_by_email = '$method_by_email',method_by_message = '$method_by_message',
  1182. event_details = '$event_details', frequency_display = '$frequency_display', method_display = '$method_display', shipment_details = '$shipment_detail',
  1183. shipment_transport_mode = '$shipment_transport_mode', shipment_etd_limit = '$shipment_etd_limit', shipment_eta_limit = '$shipment_eta_limit',
  1184. shipment_etd_limit_from = '$shipment_etd_limit_from', shipment_eta_limit_from = '$shipment_eta_limit_from'
  1185. where rules_type = '$rules_type'
  1186. and notifications_type = '$notifications_type' and lower(user_login) = '".strtolower(_getLoginName())."'
  1187. and id = '$id';";
  1188. }else{
  1189. $sql.="INSERT INTO public.notifications_rules(
  1190. user_login, notifications_type, rules_type, ocean_ctnr_status,
  1191. frequency_type, daily_time, daily_time_zone,
  1192. weekly_week, weekly_time, weekly_time_zone, method_by_email, method_by_message,
  1193. event_details, frequency_display, method_display,shipment_details,
  1194. shipment_transport_mode,shipment_etd_limit,shipment_eta_limit,shipment_etd_limit_from,shipment_eta_limit_from)
  1195. VALUES ('".strtolower(_getLoginName())."', '$notifications_type', '$rules_type', '$ocean_ctnr_status',
  1196. '$frequency_type', $daily_time, '$daily_time_zone',
  1197. '$weekly_week', $weekly_time, '$weekly_time_zone', '$method_by_email', '$method_by_message',
  1198. '$event_details', '$frequency_display', '$method_display','$shipment_detail',
  1199. '$shipment_transport_mode','$shipment_etd_limit','$shipment_eta_limit','$shipment_etd_limit_from','$shipment_eta_limit_from');";
  1200. }
  1201. }
  1202. if ($rules_type == "Departure/Arrival_Delay"){
  1203. $ocean_atd_sub_etd = common::check_input($_POST['ocean_atd_sub_etd']);
  1204. $ocean_atd_sub_etd_unit = common::check_input($_POST['ocean_atd_sub_etd_unit']);
  1205. if(!empty($ocean_atd_sub_etd_unit)){
  1206. $ocean_atd_sub_etd_unit = $ocean_atd_sub_etd_unit=="Day(s)" ? "days":"hours";
  1207. }
  1208. $ocean_ata_sub_eta = common::check_input($_POST['ocean_ata_sub_eta']);
  1209. $ocean_ata_sub_eta_unit = common::check_input($_POST['ocean_ata_sub_eta_unit']);
  1210. if(!empty($ocean_ata_sub_eta_unit)){
  1211. $ocean_ata_sub_eta_unit = $ocean_ata_sub_eta_unit=="Day(s)" ? "days":"hours";
  1212. }
  1213. $air_atd_sub_etd = common::check_input($_POST['air_atd_sub_etd']);
  1214. $air_atd_sub_etd_unit = common::check_input($_POST['air_atd_sub_etd_unit']);
  1215. if(!empty($air_atd_sub_etd_unit)){
  1216. $air_atd_sub_etd_unit = $air_atd_sub_etd_unit=="Day(s)" ? "days":"hours";
  1217. }
  1218. $air_ata_sub_eta = common::check_input($_POST['air_ata_sub_eta']);
  1219. $air_ata_sub_eta_unit = common::check_input($_POST['air_ata_sub_eta_unit']);
  1220. if(!empty($air_ata_sub_eta_unit)){
  1221. $air_ata_sub_eta_unit = $air_ata_sub_eta_unit=="Day(s)" ? "days":"hours";
  1222. }
  1223. //改为update 不在是删除后添加,为了保留原始顺序
  1224. if ($updateOrInsert == "update"){
  1225. $sql.="update public.notifications_rules set
  1226. ocean_atd_sub_etd = '$ocean_atd_sub_etd',ocean_atd_sub_etd_unit = '$ocean_atd_sub_etd_unit',
  1227. ocean_ata_sub_eta = '$ocean_ata_sub_eta',ocean_ata_sub_eta_unit = '$ocean_ata_sub_eta_unit',
  1228. air_atd_sub_etd = '$air_atd_sub_etd',air_atd_sub_etd_unit = '$air_atd_sub_etd_unit',
  1229. air_ata_sub_eta = '$air_ata_sub_eta',air_ata_sub_eta_unit = '$air_ata_sub_eta_unit',
  1230. frequency_type = '$frequency_type',daily_time = $daily_time, daily_time_zone = '$daily_time_zone',
  1231. weekly_week = '$weekly_week', weekly_time = $weekly_time, weekly_time_zone = '$weekly_time_zone', method_by_email = '$method_by_email',method_by_message = '$method_by_message',
  1232. event_details = '$event_details', frequency_display = '$frequency_display', method_display = '$method_display', shipment_details = '$shipment_detail',
  1233. shipment_transport_mode = '$shipment_transport_mode', shipment_etd_limit = '$shipment_etd_limit', shipment_eta_limit = '$shipment_eta_limit',
  1234. shipment_etd_limit_from = '$shipment_etd_limit_from', shipment_eta_limit_from = '$shipment_eta_limit_from'
  1235. where rules_type = '$rules_type'
  1236. and notifications_type = '$notifications_type' and lower(user_login) = '".strtolower(_getLoginName())."'
  1237. and id = '$id';";
  1238. }else{
  1239. $sql.="INSERT INTO public.notifications_rules(
  1240. user_login, notifications_type, rules_type,
  1241. ocean_atd_sub_etd, ocean_atd_sub_etd_unit,ocean_ata_sub_eta,ocean_ata_sub_eta_unit,
  1242. air_atd_sub_etd, air_atd_sub_etd_unit,air_ata_sub_eta,air_ata_sub_eta_unit,
  1243. frequency_type, daily_time, daily_time_zone,
  1244. weekly_week, weekly_time, weekly_time_zone, method_by_email, method_by_message,
  1245. event_details, frequency_display, method_display,shipment_details,
  1246. shipment_transport_mode,shipment_etd_limit,shipment_eta_limit,shipment_etd_limit_from,shipment_eta_limit_from)
  1247. VALUES ('".strtolower(_getLoginName())."', '$notifications_type', '$rules_type',
  1248. '$ocean_atd_sub_etd','$ocean_atd_sub_etd_unit','$ocean_ata_sub_eta','$ocean_ata_sub_eta_unit',
  1249. '$air_atd_sub_etd','$air_atd_sub_etd_unit','$air_ata_sub_eta','$air_ata_sub_eta_unit',
  1250. '$frequency_type', $daily_time, '$daily_time_zone',
  1251. '$weekly_week', $weekly_time, '$weekly_time_zone', '$method_by_email', '$method_by_message',
  1252. '$event_details', '$frequency_display', '$method_display','$shipment_detail',
  1253. '$shipment_transport_mode','$shipment_etd_limit','$shipment_eta_limit','$shipment_etd_limit_from','$shipment_eta_limit_from');";
  1254. }
  1255. }
  1256. if ($rules_type == "ETD/ETA_Change"){
  1257. $ocean_etd_change = !empty($_POST['ocean_etd_change']) ? common::check_input($_POST['ocean_etd_change']) : 'false';
  1258. $ocean_etd_old_sub_new = common::check_input($_POST['ocean_etd_old_sub_new']);
  1259. $ocean_etd_old_sub_new_unit = common::check_input($_POST['ocean_etd_old_sub_new_unit']);
  1260. if(!empty($ocean_etd_old_sub_new_unit)){
  1261. $ocean_etd_old_sub_new_unit = $ocean_etd_old_sub_new_unit=="Day(s)" ? "days":"hours";
  1262. }
  1263. $ocean_eta_change = !empty($_POST['ocean_eta_change']) ? common::check_input($_POST['ocean_eta_change']) : 'false';
  1264. $ocean_eta_old_sub_new = common::check_input($_POST['ocean_eta_old_sub_new']);
  1265. $ocean_eta_old_sub_new_unit = common::check_input($_POST['ocean_eta_old_sub_new_unit']);
  1266. if(!empty($ocean_eta_old_sub_new_unit)){
  1267. $ocean_eta_old_sub_new_unit = $ocean_eta_old_sub_new_unit=="Day(s)" ? "days":"hours";
  1268. }
  1269. $air_etd_change = !empty($_POST['air_etd_change']) ? common::check_input($_POST['air_etd_change']) : 'false';
  1270. $air_etd_old_sub_new = common::check_input($_POST['air_etd_old_sub_new']);
  1271. $air_etd_old_sub_new_unit = common::check_input($_POST['air_etd_old_sub_new_unit']);
  1272. if(!empty($air_etd_old_sub_new_unit)){
  1273. $air_etd_old_sub_new_unit = $air_etd_old_sub_new_unit=="Day(s)" ? "days":"hours";
  1274. }
  1275. $air_eta_change = !empty($_POST['air_eta_change']) ? common::check_input($_POST['air_eta_change']): 'false';
  1276. $air_eta_old_sub_new = common::check_input($_POST['air_eta_old_sub_new']);
  1277. $air_eta_old_sub_new_unit = common::check_input($_POST['air_eta_old_sub_new_unit']);
  1278. if(!empty($air_eta_old_sub_new_unit)){
  1279. $air_eta_old_sub_new_unit = $air_eta_old_sub_new_unit=="Day(s)" ? "days":"hours";
  1280. }
  1281. //改为update 不在是删除后添加,为了保留原始顺序
  1282. if ($updateOrInsert == "update"){
  1283. $sql.="update public.notifications_rules set
  1284. ocean_etd_change = '$ocean_etd_change',ocean_etd_old_sub_new = '$ocean_etd_old_sub_new',ocean_etd_old_sub_new_unit = '$ocean_etd_old_sub_new_unit',
  1285. ocean_eta_change = '$ocean_eta_change',ocean_eta_old_sub_new = '$ocean_eta_old_sub_new',ocean_eta_old_sub_new_unit = '$ocean_eta_old_sub_new_unit',
  1286. air_etd_change = '$air_etd_change',air_etd_old_sub_new = '$air_etd_old_sub_new',air_etd_old_sub_new_unit = '$air_etd_old_sub_new_unit',
  1287. air_eta_change = '$air_eta_change',air_eta_old_sub_new = '$air_eta_old_sub_new',air_eta_old_sub_new_unit = '$air_eta_old_sub_new_unit',
  1288. frequency_type = '$frequency_type',daily_time = $daily_time, daily_time_zone = '$daily_time_zone',
  1289. weekly_week = '$weekly_week', weekly_time = $weekly_time, weekly_time_zone = '$weekly_time_zone', method_by_email = '$method_by_email',method_by_message = '$method_by_message',
  1290. event_details = '$event_details', frequency_display = '$frequency_display', method_display = '$method_display', shipment_details = '$shipment_detail',
  1291. shipment_transport_mode = '$shipment_transport_mode', shipment_etd_limit = '$shipment_etd_limit', shipment_eta_limit = '$shipment_eta_limit',
  1292. shipment_etd_limit_from = '$shipment_etd_limit_from', shipment_eta_limit_from = '$shipment_eta_limit_from'
  1293. where rules_type = '$rules_type'
  1294. and notifications_type = '$notifications_type' and lower(user_login) = '".strtolower(_getLoginName())."'
  1295. and id = '$id';";
  1296. }else{
  1297. $sql.="INSERT INTO public.notifications_rules(
  1298. user_login, notifications_type, rules_type,
  1299. ocean_etd_change, ocean_etd_old_sub_new,ocean_etd_old_sub_new_unit,ocean_eta_change,ocean_eta_old_sub_new,ocean_eta_old_sub_new_unit,
  1300. air_etd_change, air_etd_old_sub_new,air_etd_old_sub_new_unit,air_eta_change,air_eta_old_sub_new,air_eta_old_sub_new_unit,
  1301. frequency_type, daily_time, daily_time_zone,
  1302. weekly_week, weekly_time, weekly_time_zone, method_by_email, method_by_message,
  1303. event_details, frequency_display, method_display,shipment_details,
  1304. shipment_transport_mode,shipment_etd_limit,shipment_eta_limit,shipment_etd_limit_from,shipment_eta_limit_from)
  1305. VALUES ('".strtolower(_getLoginName())."', '$notifications_type', '$rules_type',
  1306. '$ocean_etd_change','$ocean_etd_old_sub_new','$ocean_etd_old_sub_new_unit','$ocean_eta_change','$ocean_eta_old_sub_new','$ocean_eta_old_sub_new_unit',
  1307. '$air_etd_change','$air_etd_old_sub_new','$air_etd_old_sub_new_unit','$air_eta_change','$air_eta_old_sub_new','$air_eta_old_sub_new_unit',
  1308. '$frequency_type', $daily_time, '$daily_time_zone',
  1309. '$weekly_week', $weekly_time, '$weekly_time_zone', '$method_by_email', '$method_by_message',
  1310. '$event_details', '$frequency_display', '$method_display','$shipment_detail',
  1311. '$shipment_transport_mode','$shipment_etd_limit','$shipment_eta_limit','$shipment_etd_limit_from','$shipment_eta_limit_from');";
  1312. }
  1313. }
  1314. //保存用户默认的时区
  1315. $default_time_zone = common::check_input($_POST['default_time_zone']);
  1316. $default_time_zone_db = utils::comvertutcinfo($default_time_zone);
  1317. $sql .= "update public.kln_user_extend set default_time_zone = '$default_time_zone_db' where lower(user_login) = '".strtolower(_getLoginName())."';";
  1318. return $sql;
  1319. }
  1320. /**
  1321. * 检查编辑提交的Monitoring规则,是否允许保存
  1322. */
  1323. public function checkedMonitoringRulesSave($rules_type){
  1324. $sql_where = "";
  1325. if(isset($_POST['id']) && !empty($_POST['id'])){
  1326. $sql_where = " and id <> '".common::check_input($_POST['id'])."'";
  1327. }
  1328. $rules = common::excuteListSql("select *,
  1329. TO_CHAR(daily_time, 'HH24:MI') as _daily_time,
  1330. TO_CHAR(weekly_time, 'HH24:MI') as _weekly_time
  1331. from public.notifications_rules where notifications_type = 'Monitoring' and rules_type = '".$rules_type."'
  1332. and lower(user_login) = '".strtolower(_getLoginName())."' $sql_where");
  1333. foreach($rules as $rule){
  1334. //判断range 是否一样
  1335. $checkRangeFiled = array("shipment_transport_mode","shipment_etd_limit","shipment_eta_limit","shipment_etd_limit_from","shipment_eta_limit_from");
  1336. $range_flag = true;
  1337. foreach($checkRangeFiled as $filed){
  1338. if($filed == "shipment_transport_mode"){
  1339. $postValue = utils::converModeToDB($_POST[$filed]);
  1340. $rule_mode_arr = explode(";", $rule[$filed]);
  1341. $post_mode_arr = explode(";", $postValue);
  1342. if(!utils::compareArrayEq($post_mode_arr,$rule_mode_arr)){
  1343. $range_flag = false;
  1344. }
  1345. }else{
  1346. //正常字段直接比较就行
  1347. $postValue = !empty($_POST[$filed]) ? $_POST[$filed] : "";
  1348. $dbValue = !empty($rule[$filed]) ? $rule[$filed] : "";
  1349. if($postValue != $dbValue){
  1350. $range_flag = false;
  1351. }
  1352. }
  1353. }
  1354. //判断details 是否一样
  1355. $checkDetailsFiled = array("ocean_milestone","air_milestone","ocean_ctnr_status",
  1356. "ocean_atd_sub_etd","ocean_atd_sub_etd_unit","ocean_ata_sub_eta","ocean_ata_sub_eta_unit",
  1357. "air_atd_sub_etd","air_atd_sub_etd_unit","air_ata_sub_eta","air_ata_sub_eta_unit",
  1358. "ocean_etd_change","ocean_etd_old_sub_new","ocean_etd_old_sub_new_unit","ocean_eta_change","ocean_eta_old_sub_new","ocean_eta_old_sub_new_unit",
  1359. "air_etd_change","air_etd_old_sub_new","air_etd_old_sub_new_unit","air_eta_change","air_eta_old_sub_new","air_eta_old_sub_new_unit");
  1360. $details_flag = true;
  1361. foreach($checkDetailsFiled as $filed){
  1362. if($filed == "ocean_milestone" || $filed == "air_milestone" || $filed == "ocean_ctnr_status"){
  1363. $rule_mode_arr = empty($rule[$filed])? array() :explode(";", $rule[$filed]);
  1364. $post_mode_arr = $_POST[$filed];
  1365. if(!utils::compareArrayEq($post_mode_arr,$rule_mode_arr)){
  1366. $details_flag = false;
  1367. }
  1368. } elseif ($filed == "ocean_etd_change" || $filed == "ocean_eta_change" || $filed == "air_etd_change" || $filed == "air_eta_change"){
  1369. $post_boolean = (empty($_POST[$filed]) || $_POST[$filed] == "false") ? "f":"t";
  1370. if($post_boolean != $rule[$filed]){
  1371. $details_flag = false;
  1372. }
  1373. } elseif ($filed == "ocean_atd_sub_etd_unit" || $filed == "ocean_ata_sub_eta_unit"
  1374. || $filed == "air_atd_sub_etd_unit" || $filed == "air_ata_sub_eta_unit"
  1375. || $filed == "ocean_etd_old_sub_new_unit" || $filed == "ocean_eta_old_sub_new_unit"
  1376. || $filed == "air_etd_old_sub_new_unit" || $filed == "air_eta_old_sub_new_unit"){
  1377. $postValue = !empty($_POST[$filed]) ? $_POST[$filed] : "";
  1378. if(!empty($postValue)){
  1379. $postValue = $postValue=="Day(s)" ? "days":"hours";
  1380. }
  1381. $dbValue = !empty($rule[$filed]) ? $rule[$filed] : "";
  1382. if($postValue != $dbValue){
  1383. $details_flag = false;
  1384. }
  1385. } else {
  1386. $postValue = !empty($_POST[$filed]) ? $_POST[$filed] : "";
  1387. $dbValue = !empty($rule[$filed]) ? $rule[$filed] : "";
  1388. if($postValue != $dbValue){
  1389. $details_flag = false;
  1390. }
  1391. }
  1392. }
  1393. //判断frequency 是否一样
  1394. $checkFrequencyFiled = array("frequency_type","daily_time","daily_time_zone",
  1395. "weekly_week","weekly_time","weekly_time_zone","daily_time_zone");
  1396. $frequency_flag = true;
  1397. foreach($checkFrequencyFiled as $filed){
  1398. if($filed == "daily_time" || $filed == "weekly_time"){
  1399. $postValue = !empty($_POST[$filed]) ? $_POST[$filed] : "";
  1400. //_daily_time _weekly_time
  1401. $dbValue = !empty($rule["_".$filed]) ? $rule["_".$filed] : "";
  1402. if($postValue != $dbValue){
  1403. $frequency_flag = false;
  1404. }
  1405. } else {
  1406. $postValue = !empty($_POST[$filed]) ? $_POST[$filed] : "";
  1407. $dbValue = !empty($rule[$filed]) ? $rule[$filed] : "";
  1408. if($postValue != $dbValue){
  1409. $frequency_flag = false;
  1410. }
  1411. }
  1412. }
  1413. //判断通知方式是否一样
  1414. $checkMethodFiled = array("method_by_email","method_by_message");
  1415. $method_flag = true;
  1416. foreach($checkMethodFiled as $filed){
  1417. $postValue = (empty($_POST[$filed]) || $_POST[$filed] == "false") ? "f" : "t";
  1418. if($postValue != $rule[$filed]){
  1419. $method_flag = false;
  1420. }
  1421. }
  1422. //五个条件一样,不允许保存
  1423. if($range_flag && $details_flag && $frequency_flag && $method_flag){
  1424. $msg = "Unable to Save";
  1425. break;
  1426. }
  1427. //前三个重回,后面不重合,提示但允许保存
  1428. if($range_flag && $details_flag && $_POST['is_similar_rule'] <> 'true'){
  1429. $msg = "Similar Rule Detected";
  1430. break;
  1431. }
  1432. }
  1433. return $msg;
  1434. }
  1435. public function getNotifications($notifiation_type,$frequency_type,$insert_date_format = null){
  1436. if ($frequency_type == "all"){
  1437. //Daily 频率:超过昨天的数据,不在限制提醒的daily_time,全部应该查出来
  1438. //比如今天周四 17号, 上第一周截止是10号, 10号以后的数据,不在做weekly_time和weekly_week 的限制,直接查出来
  1439. $sql_where = " and (ni.frequency_type = 'Instant'
  1440. or (ni.frequency_type = 'Daily'
  1441. and (case when (timezone(ni.daily_time_zone, NOW())::time > ni.daily_time::time)
  1442. then timezone(ni.daily_time_zone,ni.insert_date)::date < timezone(ni.daily_time_zone, NOW())::date
  1443. else
  1444. timezone(ni.daily_time_zone,ni.insert_date)::date < timezone(ni.daily_time_zone, NOW())::date - '1 days'::INTERVAL
  1445. end))
  1446. or (ni.frequency_type = 'Weekly'
  1447. and (case when (timezone(ni.weekly_time_zone, NOW())::time < ni.weekly_time::time
  1448. and timezone(ni.weekly_time_zone,NOW())::date = (timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date)
  1449. then timezone(ni.weekly_time_zone,ni.insert_date)::date < ((timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date - '7 days'::INTERVAL)::date
  1450. else
  1451. timezone(ni.weekly_time_zone,ni.insert_date)::date < (timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date
  1452. end)))";
  1453. } elseif($frequency_type == "Daily"){
  1454. $sql_where = " and (ni.frequency_type = 'Daily'
  1455. and (case when (timezone(ni.daily_time_zone, NOW())::time > ni.daily_time::time)
  1456. then timezone(ni.daily_time_zone,ni.insert_date)::date < timezone(ni.daily_time_zone, NOW())::date
  1457. else
  1458. timezone(ni.daily_time_zone,ni.insert_date)::date < timezone(ni.daily_time_zone, NOW())::date - '1 days'::INTERVAL
  1459. end))";
  1460. } elseif($frequency_type == "Weekly"){
  1461. //当前now()的配置是周2。 如今天是4-17,最小边界值是4.15- 4.21
  1462. // 如今天是4-15(这个是周二),最小边界值是4.15- 4.21
  1463. // 如今天是4-14,最小边界值是4.8- 4.14
  1464. // 当 前时间是 4.15时,但没到规定的time,取4.8,但此时的边界值4.15- 4.21(这个是一个例外,除此之外,可以直接用当前now的最小边界值);
  1465. // 当 前时间是 4.15时,但到规定的time,取4.15
  1466. $sql_where = " and (ni.frequency_type = 'Weekly'
  1467. and (case when (timezone(ni.weekly_time_zone, NOW())::time < ni.weekly_time::time
  1468. and timezone(ni.weekly_time_zone,NOW())::date = (timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date)
  1469. then timezone(ni.weekly_time_zone,ni.insert_date)::date < ((timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date - '7 days'::INTERVAL)::date
  1470. else
  1471. timezone(ni.weekly_time_zone,ni.insert_date)::date < (timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date
  1472. end))";
  1473. }
  1474. if (!empty($_REQUEST['current_time'])){
  1475. // $sql_where .= " and ni.insert_date >= TO_TIMESTAMP('".$_REQUEST['current_time']."', 'MM/DD/YYYY HH24:MI:SS') - interval '5 minutes'
  1476. // and ni.is_send_message is null";
  1477. //这里都以服务器时间,检查标准
  1478. $sql_where .= " and ni.insert_date >= now() - interval '5 minutes' and ni.is_send_message is null";
  1479. }
  1480. if (!empty($_REQUEST['info_type']) && $_REQUEST['info_type'] == 'true'){
  1481. $sql_where .= " and ni.is_send_message is null";
  1482. }
  1483. //这里的查询会把不同日期的但hbol相同的信息,过滤掉只剩下最新的那一条。所以移除
  1484. $aa_where = "";
  1485. if (!empty($insert_date_format)){
  1486. $aa_where = " where insert_date_format = '$insert_date_format'";
  1487. }
  1488. $more_param = common::getInNotInSqlForSearch($notifiation_type);
  1489. $sql = "select *
  1490. from (select ni.*,
  1491. case when ni.notifiation_type = 'Departure/Arrival_Delay' and ni.delay_unit = 'days'
  1492. then (EXTRACT(DAY FROM ((delay_act_date||' '||delay_act_time)::timestamp - (delay_est_date||' '||delay_est_time)::timestamp)))
  1493. when ni.notifiation_type = 'Departure/Arrival_Delay' and ni.delay_unit = 'hours'
  1494. then (FLOOR(EXTRACT(epoch FROM ((delay_act_date||' '||delay_act_time)::timestamp - (delay_est_date||' '||delay_est_time)::timestamp))/3600))
  1495. else 0
  1496. end as _delay_diff,
  1497. case when COALESCE(ni.frequency_type,'') = 'Daily'
  1498. then to_char(timezone(ni.daily_time_zone, ni.insert_date),'Mon DD, YYYY')
  1499. when COALESCE(ni.frequency_type,'') = 'Weekly'
  1500. then to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL,'Mon DD, YYYY')
  1501. || ' - ' ||
  1502. to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date + (((ni.weekly_week::integer - EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer + 7) % 7)-1 + CASE WHEN EXTRACT(DOW FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer=ni.weekly_week::integer THEN 7 ELSE 0 END ||' days')::INTERVAL,'Mon DD, YYYY')
  1503. else ''
  1504. end as insert_date_format,
  1505. case when COALESCE(ni.frequency_type,'') = 'Instant'
  1506. then to_char(timezone(ddd.default_time_zone, ni.insert_date),'YYYY-mm-dd HH24:MI:SS')
  1507. when COALESCE(ni.frequency_type,'') = 'Daily'
  1508. then to_char(timezone(ni.daily_time_zone, ni.insert_date),'YYYY-mm-dd')||' '||ni.daily_time
  1509. when COALESCE(ni.frequency_type,'') = 'Weekly'
  1510. then to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date + (((ni.weekly_week::integer - EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer + 7) % 7) + CASE WHEN EXTRACT(DOW FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer=ni.weekly_week::integer THEN 7 ELSE 0 END ||' days')::INTERVAL,'YYYY-mm-dd')||' '||ni.weekly_time
  1511. else ''
  1512. end as first_notifiation_date,
  1513. case when ni.notifiation_type ='Milestone_Update'
  1514. then public.getPreviousMilestone(ni.serial_no,ni.milestone_code,ccc.transport_mode,ccc.order_from)
  1515. else ''
  1516. end as milestone_previous_json,
  1517. case when ni.notifiation_type ='Container_Status_Update'
  1518. then public.getPreviousCtnrStatus(ni.serial_no,ni.ctnr,ni.ctnr_status_code)
  1519. else ''
  1520. end as ctnr_previous_json,
  1521. case when ni.notifiation_type ='Container_Status_Update'
  1522. then (select description
  1523. from public.ra_online_edi_event e where e.ra_name = ni.ctnr_status_code limit 1)
  1524. else ''
  1525. end as ctnr_desc,
  1526. ccc.order_from,ccc.h_bol,ccc.transport_mode,ccc.m_bol
  1527. from public.kln_notifiation_info ni
  1528. inner join LATERAL (select oo.h_bol,oo.transport_mode,oo.order_from,oo.m_bol
  1529. from public.kln_ocean oo
  1530. where oo.serial_no = ni.serial_no limit 1) ccc on true
  1531. left join LATERAL (select COALESCE(ke.default_time_zone,'UTC-08') as default_time_zone from public.kln_user_extend ke where lower(ke.user_login) = lower(ni.user_login) limit 1) ddd on true
  1532. where lower(ni.user_login) in ('".strtolower(_getLoginName())."','all_user')
  1533. and ni.insert_date > NOW() - INTERVAL '3 months'
  1534. and lower(ni.notifiation_type) in ($more_param)
  1535. ".$sql_where." and ni.notifications_method = true order by ni.insert_date desc) aa $aa_where";
  1536. error_log($sql);
  1537. $data_all_type = common::excuteListSql($sql);
  1538. $data_group = array();
  1539. $data_group_uniqe = array();
  1540. foreach($data_all_type as $dat){
  1541. $uniqe_group_str = $dat['notifiation_type'];
  1542. if(utils::in_array($uniqe_group_str,$data_group_uniqe)){
  1543. $tempArr = $data_group[$uniqe_group_str];
  1544. $tempArr[] = $dat;
  1545. $data_group[$uniqe_group_str] = $tempArr;
  1546. } else {
  1547. $data_group[$uniqe_group_str] = array($dat);
  1548. $data_group_uniqe[] = $uniqe_group_str;
  1549. }
  1550. }
  1551. $retData = array();
  1552. foreach($data_group as $key => $data){
  1553. $notifiation_type_db = $key;
  1554. //统一处理数据Instant Daily weekly_week 先分开在处理
  1555. $instant = array();
  1556. $daily = array();
  1557. $daily_uniqe = array();
  1558. $weekly = array();
  1559. $weekly_uniqe = array();
  1560. foreach($data as $d){
  1561. if ($d['frequency_type'] == "Instant"){
  1562. $instant[] = $d;
  1563. }
  1564. //Daily 或者 Weekly类型为这个时才用这个去重,否则要加上描述(转船的情况,会让相同的HBOL显示)
  1565. $uniqe_str = $d['serial_no'];
  1566. if ($notifiation_type_db == "Milestone_Update"){
  1567. $uniqe_str = $d['serial_no']."_".$d['insert_date_format']."_".$d['milestone_code'];
  1568. }else if ($notifiation_type_db == "Container_Status_Update"){
  1569. $uniqe_str = $d['ctnr']."_".$d['insert_date_format']."_".$d['ctnr_status_code'];
  1570. }else if($notifiation_type_db == "Departure/Arrival_Delay"){
  1571. $uniqe_str = $d['serial_no']."_".$d['insert_date_format']."_".$d['delay_name'];
  1572. }else if($notifiation_type_db == "ETD/ETA_Change"){
  1573. $uniqe_str = $d['serial_no']."_".$d['insert_date_format']."_".$d['date_change_name'];
  1574. }
  1575. if ($d['frequency_type'] == "Daily"){
  1576. if(!utils::in_array($uniqe_str,$daily_uniqe)){
  1577. $daily[$uniqe_str] = $d;
  1578. $daily_uniqe[] = $uniqe_str;
  1579. }
  1580. }
  1581. if ($d['frequency_type'] == "Weekly"){
  1582. if(!utils::in_array($uniqe_str,$weekly_uniqe)){
  1583. $weekly[$uniqe_str] = $d;
  1584. $weekly_uniqe[] = $uniqe_str;
  1585. }
  1586. }
  1587. }
  1588. $retData[$key]= array("instant" =>$instant,"daily" =>utils::arrayKeyToInt($daily),"weekly"=>utils::arrayKeyToInt($weekly));
  1589. }
  1590. return $retData;
  1591. }
  1592. public function getNotificationsNew($notifiation_type,$frequency_type,$insert_date_format = null){
  1593. if ($frequency_type == "all"){
  1594. //Daily 频率:超过昨天的数据,不在限制提醒的daily_time,全部应该查出来
  1595. //比如今天周四 17号, 上第一周截止是10号, 10号以后的数据,不在做weekly_time和weekly_week 的限制,直接查出来
  1596. $sql_where = " and (ni.frequency_type = 'Instant'
  1597. or (ni.frequency_type = 'Daily'
  1598. and (case when (timezone(ni.daily_time_zone, NOW())::time > ni.daily_time::time)
  1599. then timezone(ni.daily_time_zone,ni.insert_date)::date < timezone(ni.daily_time_zone, NOW())::date
  1600. else
  1601. timezone(ni.daily_time_zone,ni.insert_date)::date < timezone(ni.daily_time_zone, NOW())::date - '1 days'::INTERVAL
  1602. end))
  1603. or (ni.frequency_type = 'Weekly'
  1604. and (case when (timezone(ni.weekly_time_zone, NOW())::time < ni.weekly_time::time
  1605. and timezone(ni.weekly_time_zone,NOW())::date = (timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date)
  1606. then timezone(ni.weekly_time_zone,ni.insert_date)::date < ((timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date - '7 days'::INTERVAL)::date
  1607. else
  1608. timezone(ni.weekly_time_zone,ni.insert_date)::date < (timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date
  1609. end)))";
  1610. } elseif($frequency_type == "Daily"){
  1611. $sql_where = " and (ni.frequency_type = 'Daily'
  1612. and (case when (timezone(ni.daily_time_zone, NOW())::time > ni.daily_time::time)
  1613. then timezone(ni.daily_time_zone,ni.insert_date)::date < timezone(ni.daily_time_zone, NOW())::date
  1614. else
  1615. timezone(ni.daily_time_zone,ni.insert_date)::date < timezone(ni.daily_time_zone, NOW())::date - '1 days'::INTERVAL
  1616. end))";
  1617. } elseif($frequency_type == "Weekly"){
  1618. //当前now()的配置是周2。 如今天是4-17,最小边界值是4.15- 4.21
  1619. // 如今天是4-15(这个是周二),最小边界值是4.15- 4.21
  1620. // 如今天是4-14,最小边界值是4.8- 4.14
  1621. // 当 前时间是 4.15时,但没到规定的time,取4.8,但此时的边界值4.15- 4.21(这个是一个例外,除此之外,可以直接用当前now的最小边界值);
  1622. // 当 前时间是 4.15时,但到规定的time,取4.15
  1623. $sql_where = " and (ni.frequency_type = 'Weekly'
  1624. and (case when (timezone(ni.weekly_time_zone, NOW())::time < ni.weekly_time::time
  1625. and timezone(ni.weekly_time_zone,NOW())::date = (timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date)
  1626. then timezone(ni.weekly_time_zone,ni.insert_date)::date < ((timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date - '7 days'::INTERVAL)::date
  1627. else
  1628. timezone(ni.weekly_time_zone,ni.insert_date)::date < (timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date
  1629. end))";
  1630. }
  1631. if (!empty($_REQUEST['current_time'])){
  1632. // $sql_where .= " and ni.insert_date >= TO_TIMESTAMP('".$_REQUEST['current_time']."', 'MM/DD/YYYY HH24:MI:SS') - interval '5 minutes'
  1633. // and ni.is_send_message is null";
  1634. //这里都以服务器时间,检查标准
  1635. $sql_where .= " and ni.insert_date >= now() - interval '5 minutes' and ni.is_send_message is null";
  1636. }
  1637. if (!empty($_REQUEST['info_type']) && $_REQUEST['info_type'] == 'true'){
  1638. $sql_where .= " and ni.is_send_message is null";
  1639. }
  1640. if (!empty($_REQUEST['info_type']) && $_REQUEST['info_type'] == 'false'){
  1641. $sql_where .= " and ni.is_send_message is not null";
  1642. }
  1643. $more_param = common::getInNotInSqlForSearch($notifiation_type);
  1644. $cp = common::check_input($_REQUEST['cp']); //current_page
  1645. $ps = common::check_input($_REQUEST['ps']); //ps
  1646. if (empty($ps) || empty($cp)){
  1647. $limit_param = " ";
  1648. } else {
  1649. $limit_param = " limit " . $ps . " offset " . ($cp - 1) * $ps;
  1650. }
  1651. $sql= "WITH base_data AS (
  1652. SELECT
  1653. id,
  1654. notifiation_type,
  1655. frequency_type,
  1656. insert_date,
  1657. case when COALESCE(ni.frequency_type,'') = 'Daily'
  1658. then to_char(timezone(ni.daily_time_zone, ni.insert_date),'Mon DD, YYYY')
  1659. when COALESCE(ni.frequency_type,'') = 'Weekly'
  1660. then to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL,'Mon DD, YYYY')
  1661. || ' - ' ||
  1662. to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date + (((ni.weekly_week::integer - EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer + 7) % 7)-1 + CASE WHEN EXTRACT(DOW FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer=ni.weekly_week::integer THEN 7 ELSE 0 END ||' days')::INTERVAL,'Mon DD, YYYY')
  1663. else ''
  1664. end as insert_date_format,
  1665. -- 提取用于去重的字段
  1666. serial_no,
  1667. milestone_code,
  1668. ctnr,
  1669. ctnr_status_code,
  1670. delay_name,
  1671. date_change_name
  1672. FROM public.kln_notifiation_info ni
  1673. WHERE lower(ni.user_login) in ('".strtolower(_getLoginName())."','all_user')
  1674. AND ni.insert_date > NOW() - INTERVAL '3 months'
  1675. and lower(ni.notifiation_type) in ($more_param)
  1676. ".$sql_where." and ni.notifications_method = true
  1677. ),
  1678. -- Step 1: 按 insert_date_format + notifiation_type 分组,统计去重数量
  1679. grouped_stats AS (
  1680. SELECT
  1681. insert_date_format,
  1682. notifiation_type,
  1683. COUNT(DISTINCT
  1684. CASE WHEN notifiation_type = 'Milestone_Update' THEN (serial_no || '_' || milestone_code)
  1685. WHEN notifiation_type = 'Container_Status_Update' THEN (ctnr || '_' || ctnr_status_code)
  1686. WHEN notifiation_type = 'Departure/Arrival_Delay' THEN (serial_no || '_' || delay_name)
  1687. WHEN notifiation_type = 'ETD/ETA_Change' THEN (serial_no || '_' || date_change_name)
  1688. END
  1689. ) AS total_count,
  1690. -- Departure / Arrival 单独统计
  1691. COUNT(DISTINCT
  1692. CASE WHEN notifiation_type = 'Departure/Arrival_Delay' AND delay_name LIKE '%Departure%' THEN (serial_no || '_' || delay_name) END
  1693. ) AS departure_count,
  1694. COUNT(DISTINCT
  1695. CASE WHEN notifiation_type = 'Departure/Arrival_Delay' AND delay_name LIKE '%Arrival%' THEN (serial_no || '_' || delay_name) END
  1696. ) AS arrival_count,
  1697. -- ETD / ETA 单独统计
  1698. COUNT(DISTINCT
  1699. CASE WHEN notifiation_type = 'ETD/ETA_Change' AND date_change_name LIKE '%ETD%' THEN (serial_no || '_' || date_change_name) END
  1700. ) AS etd_count,
  1701. COUNT(DISTINCT
  1702. CASE WHEN notifiation_type = 'ETD/ETA_Change' AND date_change_name LIKE '%ETA%' THEN (serial_no || '_' || date_change_name) END
  1703. ) AS eta_count
  1704. FROM base_data
  1705. where frequency_type <> 'Instant'
  1706. GROUP BY insert_date_format, notifiation_type
  1707. ),
  1708. -- Step 2: 找出每个 insert_date_format + notifiation_type 组内的最新一条记录
  1709. latest_records AS (
  1710. SELECT DISTINCT ON (insert_date_format, notifiation_type)
  1711. bd.id,
  1712. bd.notifiation_type,
  1713. bd.insert_date,
  1714. bd.insert_date_format
  1715. FROM base_data bd
  1716. where frequency_type <> 'Instant'
  1717. ORDER BY insert_date_format, notifiation_type, insert_date DESC
  1718. ),
  1719. daily_weekly_summary AS (
  1720. SELECT
  1721. lr.id,
  1722. lr.insert_date,
  1723. gs.total_count,
  1724. gs.departure_count,
  1725. gs.arrival_count,
  1726. gs.etd_count,
  1727. gs.eta_count
  1728. FROM latest_records lr
  1729. LEFT JOIN grouped_stats gs
  1730. ON lr.insert_date_format = gs.insert_date_format
  1731. AND lr.notifiation_type = gs.notifiation_type
  1732. UNION ALL
  1733. SELECT
  1734. id,
  1735. insert_date,
  1736. NULL::INT AS total_count,
  1737. NULL::INT AS departure_count,
  1738. NULL::INT AS arrival_count,
  1739. NULL::INT AS etd_count,
  1740. NULL::INT AS eta_count
  1741. FROM base_data where frequency_type = 'Instant'
  1742. ),
  1743. summary AS (
  1744. SELECT id ,
  1745. insert_date,
  1746. total_count,
  1747. departure_count,
  1748. arrival_count,
  1749. etd_count,
  1750. eta_count
  1751. FROM daily_weekly_summary order by insert_date desc ".$limit_param."
  1752. )
  1753. select *
  1754. from (select ni.*,
  1755. case when ni.notifiation_type = 'Departure/Arrival_Delay' and ni.delay_unit = 'days'
  1756. then (EXTRACT(DAY FROM ((delay_act_date||' '||delay_act_time)::timestamp - (delay_est_date||' '||delay_est_time)::timestamp)))
  1757. when ni.notifiation_type = 'Departure/Arrival_Delay' and ni.delay_unit = 'hours'
  1758. then (FLOOR(EXTRACT(epoch FROM ((delay_act_date||' '||delay_act_time)::timestamp - (delay_est_date||' '||delay_est_time)::timestamp))/3600))
  1759. else 0
  1760. end as _delay_diff,
  1761. case when COALESCE(ni.frequency_type,'') = 'Daily'
  1762. then to_char(timezone(ni.daily_time_zone, ni.insert_date),'Mon DD, YYYY')
  1763. when COALESCE(ni.frequency_type,'') = 'Weekly'
  1764. then to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL,'Mon DD, YYYY')
  1765. || ' - ' ||
  1766. to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date + (((ni.weekly_week::integer - EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer + 7) % 7)-1 + CASE WHEN EXTRACT(DOW FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer=ni.weekly_week::integer THEN 7 ELSE 0 END ||' days')::INTERVAL,'Mon DD, YYYY')
  1767. else ''
  1768. end as insert_date_format,
  1769. case when COALESCE(ni.frequency_type,'') = 'Instant'
  1770. then to_char(timezone(ddd.default_time_zone, ni.insert_date),'YYYY-mm-dd HH24:MI:SS')
  1771. when COALESCE(ni.frequency_type,'') = 'Daily'
  1772. then to_char(timezone(ni.daily_time_zone, ni.insert_date),'YYYY-mm-dd')||' '||ni.daily_time
  1773. when COALESCE(ni.frequency_type,'') = 'Weekly'
  1774. then to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date + (((ni.weekly_week::integer - EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer + 7) % 7) + CASE WHEN EXTRACT(DOW FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer=ni.weekly_week::integer THEN 7 ELSE 0 END ||' days')::INTERVAL,'YYYY-mm-dd')||' '||ni.weekly_time
  1775. else ''
  1776. end as first_notifiation_date,
  1777. case when ni.notifiation_type ='Milestone_Update'
  1778. then public.getPreviousMilestone(ni.serial_no,ni.milestone_code,ccc.transport_mode,ccc.order_from)
  1779. else ''
  1780. end as milestone_previous_json,
  1781. case when ni.notifiation_type ='Container_Status_Update'
  1782. then public.getPreviousCtnrStatus(ni.serial_no,ni.ctnr,ni.ctnr_status_code)
  1783. else ''
  1784. end as ctnr_previous_json,
  1785. case when ni.notifiation_type ='Container_Status_Update'
  1786. then (select description
  1787. from public.ra_online_edi_event e where e.ra_name = ni.ctnr_status_code limit 1)
  1788. else ''
  1789. end as ctnr_desc,
  1790. ccc.order_from,ccc.h_bol,ccc.transport_mode,ccc.m_bol,eee.*
  1791. from public.kln_notifiation_info ni
  1792. inner join LATERAL (select id,total_count,departure_count,arrival_count,etd_count,eta_count
  1793. from summary
  1794. where id = ni.id ) eee on true
  1795. left join LATERAL (select oo.h_bol,oo.transport_mode,oo.order_from,oo.m_bol
  1796. from public.kln_ocean oo
  1797. where oo.serial_no = ni.serial_no limit 1) ccc on true
  1798. left join LATERAL (select COALESCE(ke.default_time_zone,'UTC-08') as default_time_zone from public.kln_user_extend ke where lower(ke.user_login) = lower(ni.user_login) limit 1) ddd on true
  1799. order by ni.insert_date desc) aa";
  1800. // $sql = "WITH base_data AS (
  1801. // SELECT
  1802. // id,
  1803. // notifiation_type,
  1804. // frequency_type,
  1805. // insert_date,
  1806. // case when COALESCE(ni.frequency_type,'') = 'Daily'
  1807. // then to_char(timezone(ni.daily_time_zone, ni.insert_date),'Mon DD, YYYY')
  1808. // when COALESCE(ni.frequency_type,'') = 'Weekly'
  1809. // then to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL,'Mon DD, YYYY')
  1810. // || ' - ' ||
  1811. // to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date + (((ni.weekly_week::integer - EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer + 7) % 7)-1 + CASE WHEN EXTRACT(DOW FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer=ni.weekly_week::integer THEN 7 ELSE 0 END ||' days')::INTERVAL,'Mon DD, YYYY')
  1812. // else ''
  1813. // end as insert_date_format,
  1814. // -- 提取用于去重的字段
  1815. // serial_no,
  1816. // milestone_code,
  1817. // ctnr,
  1818. // ctnr_status_code,
  1819. // delay_name,
  1820. // date_change_name
  1821. // FROM public.kln_notifiation_info ni
  1822. // WHERE lower(ni.user_login) in ('".strtolower(_getLoginName())."','all_user')
  1823. // AND ni.insert_date > NOW() - INTERVAL '3 months'
  1824. // and lower(ni.notifiation_type) in ($more_param)
  1825. // AND frequency_type IN ('Daily', 'Weekly')
  1826. // ".$sql_where." and ni.notifications_method = true
  1827. // ),
  1828. // base_instant_data AS (
  1829. // SELECT id,insert_date FROM public.kln_notifiation_info ni
  1830. // WHERE lower(ni.user_login) in ('".strtolower(_getLoginName())."','all_user')
  1831. // AND ni.insert_date > NOW() - INTERVAL '3 months'
  1832. // and lower(ni.notifiation_type) in ($more_param)
  1833. // AND frequency_type = 'Instant'
  1834. // ".$sql_where." and ni.notifications_method = true
  1835. // ),
  1836. // -- Step 1: 按 insert_date_format + notifiation_type 分组,统计去重数量
  1837. // grouped_stats AS (
  1838. // SELECT
  1839. // insert_date_format,
  1840. // notifiation_type,
  1841. // COUNT(DISTINCT
  1842. // CASE WHEN notifiation_type = 'Milestone_Update' THEN (serial_no || '_' || milestone_code)
  1843. // WHEN notifiation_type = 'Container_Status_Update' THEN (ctnr || '_' || ctnr_status_code)
  1844. // WHEN notifiation_type = 'Departure/Arrival_Delay' THEN (serial_no || '_' || delay_name)
  1845. // WHEN notifiation_type = 'ETD/ETA_Change' THEN (serial_no || '_' || date_change_name)
  1846. // END
  1847. // ) AS total_count,
  1848. // -- Departure / Arrival 单独统计
  1849. // COUNT(DISTINCT
  1850. // CASE WHEN notifiation_type = 'Departure/Arrival_Delay' AND delay_name LIKE '%Departure%' THEN (serial_no || '_' || delay_name) END
  1851. // ) AS departure_count,
  1852. // COUNT(DISTINCT
  1853. // CASE WHEN notifiation_type = 'Departure/Arrival_Delay' AND delay_name LIKE '%Arrival%' THEN (serial_no || '_' || delay_name) END
  1854. // ) AS arrival_count,
  1855. // -- ETD / ETA 单独统计
  1856. // COUNT(DISTINCT
  1857. // CASE WHEN notifiation_type = 'ETD/ETA_Change' AND date_change_name LIKE '%ETD%' THEN (serial_no || '_' || date_change_name) END
  1858. // ) AS etd_count,
  1859. // COUNT(DISTINCT
  1860. // CASE WHEN notifiation_type = 'ETD/ETA_Change' AND date_change_name LIKE '%ETA%' THEN (serial_no || '_' || date_change_name) END
  1861. // ) AS eta_count
  1862. // FROM base_data
  1863. // where notifiation_type in ('Milestone_Update','Container_Status_Update','Departure/Arrival_Delay','ETD/ETA_Change')
  1864. // GROUP BY insert_date_format, notifiation_type
  1865. // ),
  1866. // -- Step 2: 找出每个 insert_date_format + notifiation_type 组内的最新一条记录
  1867. // latest_records AS (
  1868. // SELECT DISTINCT ON (insert_date_format, notifiation_type)
  1869. // ni.id,
  1870. // ni.notifiation_type,
  1871. // ni.insert_date,
  1872. // bd.insert_date_format
  1873. // FROM public.kln_notifiation_info ni
  1874. // INNER JOIN base_data bd
  1875. // ON ni.id = bd.id
  1876. // ORDER BY insert_date_format, notifiation_type, ni.insert_date DESC
  1877. // ),
  1878. // daily_weekly_summary AS (
  1879. // SELECT
  1880. // lr.id,
  1881. // lr.insert_date,
  1882. // gs.total_count,
  1883. // gs.departure_count,
  1884. // gs.arrival_count,
  1885. // gs.etd_count,
  1886. // gs.eta_count
  1887. // FROM latest_records lr
  1888. // LEFT JOIN grouped_stats gs
  1889. // ON lr.insert_date_format = gs.insert_date_format
  1890. // AND lr.notifiation_type = gs.notifiation_type
  1891. // UNION ALL
  1892. // SELECT
  1893. // id,
  1894. // insert_date,
  1895. // NULL::INT AS total_count,
  1896. // NULL::INT AS departure_count,
  1897. // NULL::INT AS arrival_count,
  1898. // NULL::INT AS etd_count,
  1899. // NULL::INT AS eta_count
  1900. // FROM base_instant_data
  1901. // ),
  1902. // summary AS (
  1903. // SELECT id ,
  1904. // insert_date,
  1905. // total_count,
  1906. // departure_count,
  1907. // arrival_count,
  1908. // etd_count,
  1909. // eta_count
  1910. // FROM daily_weekly_summary order by insert_date desc ".$limit_param."
  1911. // )
  1912. // select *
  1913. // from (select ni.*,
  1914. // case when ni.notifiation_type = 'Departure/Arrival_Delay' and ni.delay_unit = 'days'
  1915. // then (EXTRACT(DAY FROM ((delay_act_date||' '||delay_act_time)::timestamp - (delay_est_date||' '||delay_est_time)::timestamp)))
  1916. // when ni.notifiation_type = 'Departure/Arrival_Delay' and ni.delay_unit = 'hours'
  1917. // then (FLOOR(EXTRACT(epoch FROM ((delay_act_date||' '||delay_act_time)::timestamp - (delay_est_date||' '||delay_est_time)::timestamp))/3600))
  1918. // else 0
  1919. // end as _delay_diff,
  1920. // case when COALESCE(ni.frequency_type,'') = 'Daily'
  1921. // then to_char(timezone(ni.daily_time_zone, ni.insert_date),'Mon DD, YYYY')
  1922. // when COALESCE(ni.frequency_type,'') = 'Weekly'
  1923. // then to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL,'Mon DD, YYYY')
  1924. // || ' - ' ||
  1925. // to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date + (((ni.weekly_week::integer - EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer + 7) % 7)-1 + CASE WHEN EXTRACT(DOW FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer=ni.weekly_week::integer THEN 7 ELSE 0 END ||' days')::INTERVAL,'Mon DD, YYYY')
  1926. // else ''
  1927. // end as insert_date_format,
  1928. // case when COALESCE(ni.frequency_type,'') = 'Instant'
  1929. // then to_char(timezone(ddd.default_time_zone, ni.insert_date),'YYYY-mm-dd HH24:MI:SS')
  1930. // when COALESCE(ni.frequency_type,'') = 'Daily'
  1931. // then to_char(timezone(ni.daily_time_zone, ni.insert_date),'YYYY-mm-dd')||' '||ni.daily_time
  1932. // when COALESCE(ni.frequency_type,'') = 'Weekly'
  1933. // then to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date + (((ni.weekly_week::integer - EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer + 7) % 7) + CASE WHEN EXTRACT(DOW FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer=ni.weekly_week::integer THEN 7 ELSE 0 END ||' days')::INTERVAL,'YYYY-mm-dd')||' '||ni.weekly_time
  1934. // else ''
  1935. // end as first_notifiation_date,
  1936. // case when ni.notifiation_type ='Milestone_Update'
  1937. // then public.getPreviousMilestone(ni.serial_no,ni.milestone_code,ccc.transport_mode,ccc.order_from)
  1938. // else ''
  1939. // end as milestone_previous_json,
  1940. // case when ni.notifiation_type ='Container_Status_Update'
  1941. // then public.getPreviousCtnrStatus(ni.serial_no,ni.ctnr,ni.ctnr_status_code)
  1942. // else ''
  1943. // end as ctnr_previous_json,
  1944. // case when ni.notifiation_type ='Container_Status_Update'
  1945. // then (select description
  1946. // from public.ra_online_edi_event e where e.ra_name = ni.ctnr_status_code limit 1)
  1947. // else ''
  1948. // end as ctnr_desc,
  1949. // ccc.order_from,ccc.h_bol,ccc.transport_mode,ccc.m_bol,eee.*
  1950. // from public.kln_notifiation_info ni
  1951. // left join LATERAL (select oo.h_bol,oo.transport_mode,oo.order_from,oo.m_bol
  1952. // from public.kln_ocean oo
  1953. // where oo.serial_no = ni.serial_no limit 1) ccc on true
  1954. // inner join LATERAL (select id,total_count,departure_count,arrival_count,etd_count,eta_count
  1955. // from summary
  1956. // where id = ni.id ) eee on true
  1957. // left join LATERAL (select COALESCE(ke.default_time_zone,'UTC-08') as default_time_zone from public.kln_user_extend ke where lower(ke.user_login) = lower(ni.user_login) limit 1) ddd on true
  1958. // order by ni.insert_date desc) aa";
  1959. error_log($sql);
  1960. $retData = common::excuteListSql($sql);
  1961. return $retData;
  1962. }
  1963. public function getNotificationsSeeAll($notifiation_type,$frequency_type,$insert_date_format = null){
  1964. if ($frequency_type == "all"){
  1965. //Daily 频率:超过昨天的数据,不在限制提醒的daily_time,全部应该查出来
  1966. //比如今天周四 17号, 上第一周截止是10号, 10号以后的数据,不在做weekly_time和weekly_week 的限制,直接查出来
  1967. $sql_where = " and (ni.frequency_type = 'Instant'
  1968. or (ni.frequency_type = 'Daily'
  1969. and (case when (timezone(ni.daily_time_zone, NOW())::time > ni.daily_time::time)
  1970. then timezone(ni.daily_time_zone,ni.insert_date)::date < timezone(ni.daily_time_zone, NOW())::date
  1971. else
  1972. timezone(ni.daily_time_zone,ni.insert_date)::date < timezone(ni.daily_time_zone, NOW())::date - '1 days'::INTERVAL
  1973. end))
  1974. or (ni.frequency_type = 'Weekly'
  1975. and (case when (timezone(ni.weekly_time_zone, NOW())::time < ni.weekly_time::time
  1976. and timezone(ni.weekly_time_zone,NOW())::date = (timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date)
  1977. then timezone(ni.weekly_time_zone,ni.insert_date)::date < ((timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date - '7 days'::INTERVAL)::date
  1978. else
  1979. timezone(ni.weekly_time_zone,ni.insert_date)::date < (timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date
  1980. end)))";
  1981. } elseif($frequency_type == "Daily"){
  1982. $sql_where = " and (ni.frequency_type = 'Daily'
  1983. and (case when (timezone(ni.daily_time_zone, NOW())::time > ni.daily_time::time)
  1984. then timezone(ni.daily_time_zone,ni.insert_date)::date < timezone(ni.daily_time_zone, NOW())::date
  1985. else
  1986. timezone(ni.daily_time_zone,ni.insert_date)::date < timezone(ni.daily_time_zone, NOW())::date - '1 days'::INTERVAL
  1987. end))";
  1988. } elseif($frequency_type == "Weekly"){
  1989. //当前now()的配置是周2。 如今天是4-17,最小边界值是4.15- 4.21
  1990. // 如今天是4-15(这个是周二),最小边界值是4.15- 4.21
  1991. // 如今天是4-14,最小边界值是4.8- 4.14
  1992. // 当 前时间是 4.15时,但没到规定的time,取4.8,但此时的边界值4.15- 4.21(这个是一个例外,除此之外,可以直接用当前now的最小边界值);
  1993. // 当 前时间是 4.15时,但到规定的time,取4.15
  1994. $sql_where = " and (ni.frequency_type = 'Weekly'
  1995. and (case when (timezone(ni.weekly_time_zone, NOW())::time < ni.weekly_time::time
  1996. and timezone(ni.weekly_time_zone,NOW())::date = (timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date)
  1997. then timezone(ni.weekly_time_zone,ni.insert_date)::date < ((timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date - '7 days'::INTERVAL)::date
  1998. else
  1999. timezone(ni.weekly_time_zone,ni.insert_date)::date < (timezone(ni.weekly_time_zone,NOW())::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,NOW())::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL)::date
  2000. end))";
  2001. }
  2002. //这里的查询会把不同日期的但hbol相同的信息,过滤掉只剩下最新的那一条。所以移除
  2003. if (!empty($insert_date_format)){
  2004. $sql_where = " and eee.insert_date_format = '$insert_date_format'";
  2005. }
  2006. $more_param = common::getInNotInSqlForSearch($notifiation_type);
  2007. $cp = common::check_input($_REQUEST['cp']); //current_page
  2008. $ps = common::check_input($_REQUEST['ps']); //ps
  2009. if (empty($ps) || empty($cp)){
  2010. $limit_param = " ";
  2011. } else {
  2012. $limit_param = " limit " . $ps . " offset " . ($cp - 1) * $ps;
  2013. }
  2014. $sql = "with aa as (select ni.*,
  2015. ROW_NUMBER() OVER (
  2016. PARTITION BY
  2017. CASE
  2018. WHEN notifiation_type = 'Milestone_Update' THEN (serial_no || '_' || milestone_code)
  2019. WHEN notifiation_type = 'Container_Status_Update' THEN (ctnr || '_' || ctnr_status_code)
  2020. WHEN notifiation_type = 'Departure/Arrival_Delay' THEN (serial_no || '_' || delay_name)
  2021. WHEN notifiation_type = 'ETD/ETA_Change' THEN (serial_no || '_' || date_change_name)
  2022. END
  2023. ORDER BY insert_date DESC
  2024. ) AS rn,
  2025. case when ni.notifiation_type = 'Departure/Arrival_Delay' and ni.delay_unit = 'days'
  2026. then (EXTRACT(DAY FROM ((delay_act_date||' '||delay_act_time)::timestamp - (delay_est_date||' '||delay_est_time)::timestamp)))
  2027. when ni.notifiation_type = 'Departure/Arrival_Delay' and ni.delay_unit = 'hours'
  2028. then (FLOOR(EXTRACT(epoch FROM ((delay_act_date||' '||delay_act_time)::timestamp - (delay_est_date||' '||delay_est_time)::timestamp))/3600))
  2029. else 0
  2030. end as _delay_diff,
  2031. eee.insert_date_format,
  2032. case when COALESCE(ni.frequency_type,'') = 'Instant'
  2033. then to_char(timezone(ddd.default_time_zone, ni.insert_date),'YYYY-mm-dd HH24:MI:SS')
  2034. when COALESCE(ni.frequency_type,'') = 'Daily'
  2035. then to_char(timezone(ni.daily_time_zone, ni.insert_date),'YYYY-mm-dd')||' '||ni.daily_time
  2036. when COALESCE(ni.frequency_type,'') = 'Weekly'
  2037. then to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date + (((ni.weekly_week::integer - EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer + 7) % 7) + CASE WHEN EXTRACT(DOW FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer=ni.weekly_week::integer THEN 7 ELSE 0 END ||' days')::INTERVAL,'YYYY-mm-dd')||' '||ni.weekly_time
  2038. else ''
  2039. end as first_notifiation_date,
  2040. case when ni.notifiation_type ='Container_Status_Update'
  2041. then public.getPreviousCtnrStatus(ni.serial_no,ni.ctnr,ni.ctnr_status_code)
  2042. else ''
  2043. end as ctnr_previous_json,
  2044. case when ni.notifiation_type ='Container_Status_Update'
  2045. then (select description
  2046. from public.ra_online_edi_event e where e.ra_name = ni.ctnr_status_code limit 1)
  2047. else ''
  2048. end as ctnr_desc
  2049. from public.kln_notifiation_info ni
  2050. left join LATERAL (select COALESCE(ke.default_time_zone,'UTC-08') as default_time_zone from public.kln_user_extend ke where lower(ke.user_login) = lower(ni.user_login) limit 1) ddd on true
  2051. left join LATERAL (select
  2052. case when COALESCE(ni.frequency_type,'') = 'Daily'
  2053. then to_char(timezone(ni.daily_time_zone, ni.insert_date),'Mon DD, YYYY')
  2054. when COALESCE(ni.frequency_type,'') = 'Weekly'
  2055. then to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL,'Mon DD, YYYY')
  2056. || ' - ' ||
  2057. to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date + (((ni.weekly_week::integer - EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer + 7) % 7)-1 + CASE WHEN EXTRACT(DOW FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer=ni.weekly_week::integer THEN 7 ELSE 0 END ||' days')::INTERVAL,'Mon DD, YYYY')
  2058. else ''
  2059. end as insert_date_format
  2060. ) eee on true
  2061. where lower(ni.user_login) in ('".strtolower(_getLoginName())."','all_user')
  2062. and ni.insert_date > NOW() - INTERVAL '3 months'
  2063. and lower(ni.notifiation_type) in ($more_param)
  2064. ".$sql_where." and ni.notifications_method = true
  2065. order by ni.insert_date desc),
  2066. bb as (select * from aa where rn = 1)
  2067. select *,
  2068. case when bb.notifiation_type ='Milestone_Update'
  2069. then public.getPreviousMilestone(bb.serial_no,bb.milestone_code,ccc.transport_mode,ccc.order_from)
  2070. else ''
  2071. end as milestone_previous_json,
  2072. ccc.order_from,ccc.h_bol,ccc.transport_mode,ccc.m_bol
  2073. from bb
  2074. inner join LATERAL (select oo.h_bol,oo.transport_mode,oo.order_from,oo.m_bol
  2075. from public.kln_ocean oo
  2076. where oo.serial_no = bb.serial_no limit 1) ccc on true " .$limit_param;
  2077. // $sql = "select *
  2078. // from (select ni.*,
  2079. // ROW_NUMBER() OVER (
  2080. // PARTITION BY
  2081. // CASE
  2082. // WHEN notifiation_type = 'Milestone_Update' THEN (serial_no || '_' || milestone_code)
  2083. // WHEN notifiation_type = 'Container_Status_Update' THEN (ctnr || '_' || ctnr_status_code)
  2084. // WHEN notifiation_type = 'Departure/Arrival_Delay' THEN (serial_no || '_' || delay_name)
  2085. // WHEN notifiation_type = 'ETD/ETA_Change' THEN (serial_no || '_' || date_change_name)
  2086. // END
  2087. // ORDER BY insert_date DESC
  2088. // ) AS rn,
  2089. // case when ni.notifiation_type = 'Departure/Arrival_Delay' and ni.delay_unit = 'days'
  2090. // then (EXTRACT(DAY FROM ((delay_act_date||' '||delay_act_time)::timestamp - (delay_est_date||' '||delay_est_time)::timestamp)))
  2091. // when ni.notifiation_type = 'Departure/Arrival_Delay' and ni.delay_unit = 'hours'
  2092. // then (FLOOR(EXTRACT(epoch FROM ((delay_act_date||' '||delay_act_time)::timestamp - (delay_est_date||' '||delay_est_time)::timestamp))/3600))
  2093. // else 0
  2094. // end as _delay_diff,
  2095. // eee.insert_date_format,
  2096. // case when COALESCE(ni.frequency_type,'') = 'Instant'
  2097. // then to_char(timezone(ddd.default_time_zone, ni.insert_date),'YYYY-mm-dd HH24:MI:SS')
  2098. // when COALESCE(ni.frequency_type,'') = 'Daily'
  2099. // then to_char(timezone(ni.daily_time_zone, ni.insert_date),'YYYY-mm-dd')||' '||ni.daily_time
  2100. // when COALESCE(ni.frequency_type,'') = 'Weekly'
  2101. // then to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date + (((ni.weekly_week::integer - EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer + 7) % 7) + CASE WHEN EXTRACT(DOW FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer=ni.weekly_week::integer THEN 7 ELSE 0 END ||' days')::INTERVAL,'YYYY-mm-dd')||' '||ni.weekly_time
  2102. // else ''
  2103. // end as first_notifiation_date,
  2104. // case when ni.notifiation_type ='Milestone_Update'
  2105. // then public.getPreviousMilestone(ni.serial_no,ni.milestone_code,ccc.transport_mode,ccc.order_from)
  2106. // else ''
  2107. // end as milestone_previous_json,
  2108. // case when ni.notifiation_type ='Container_Status_Update'
  2109. // then public.getPreviousCtnrStatus(ni.serial_no,ni.ctnr,ni.ctnr_status_code)
  2110. // else ''
  2111. // end as ctnr_previous_json,
  2112. // case when ni.notifiation_type ='Container_Status_Update'
  2113. // then (select description
  2114. // from public.ra_online_edi_event e where e.ra_name = ni.ctnr_status_code limit 1)
  2115. // else ''
  2116. // end as ctnr_desc,
  2117. // ccc.order_from,ccc.h_bol,ccc.transport_mode,ccc.m_bol
  2118. // from public.kln_notifiation_info ni
  2119. // inner join LATERAL (select oo.h_bol,oo.transport_mode,oo.order_from,oo.m_bol
  2120. // from public.kln_ocean oo
  2121. // where oo.serial_no = ni.serial_no limit 1) ccc on true
  2122. // left join LATERAL (select COALESCE(ke.default_time_zone,'UTC-08') as default_time_zone from public.kln_user_extend ke where lower(ke.user_login) = lower(ni.user_login) limit 1) ddd on true
  2123. // left join LATERAL (select case when COALESCE(ni.frequency_type,'') = 'Daily'
  2124. // then to_char(timezone(ni.daily_time_zone, ni.insert_date),'Mon DD, YYYY')
  2125. // when COALESCE(ni.frequency_type,'') = 'Weekly'
  2126. // then to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date - (((EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer - ni.weekly_week::integer + 7) % 7) ||' days')::INTERVAL,'Mon DD, YYYY')
  2127. // || ' - ' ||
  2128. // to_char(timezone(ni.weekly_time_zone,ni.insert_date)::date + (((ni.weekly_week::integer - EXTRACT(dow FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer + 7) % 7)-1 + CASE WHEN EXTRACT(DOW FROM timezone(ni.weekly_time_zone,ni.insert_date)::date)::integer=ni.weekly_week::integer THEN 7 ELSE 0 END ||' days')::INTERVAL,'Mon DD, YYYY')
  2129. // else ''
  2130. // end as insert_date_format) eee on true
  2131. // where lower(ni.user_login) in ('".strtolower(_getLoginName())."','all_user')
  2132. // and ni.insert_date > NOW() - INTERVAL '3 months'
  2133. // and lower(ni.notifiation_type) in ($more_param)
  2134. // ".$sql_where." and ni.notifications_method = true order by ni.insert_date desc) aa
  2135. // WHERE rn = 1 ORDER BY insert_date DESC " .$limit_param;
  2136. error_log($sql);
  2137. $retData = common::excuteListSql($sql);
  2138. return $retData;
  2139. }
  2140. public function getEventCard($mInfo){
  2141. $eventCard = array();
  2142. $notifiation_type = $mInfo['notifiation_type'];
  2143. if($notifiation_type == "Milestone_Update"){
  2144. $eventCard = array("type" =>'milestone',
  2145. "numericRecords"=>0,
  2146. "isRead"=>!empty($mInfo["is_send_message"]) ? true : false,
  2147. "title"=>"Milestone Update",
  2148. "mode"=>$mInfo["transport_mode"] == 'sea' ? "Ocean Freight": "Air Freight",
  2149. "no"=>!empty($mInfo["h_bol"]) ? $mInfo["h_bol"] : $mInfo["m_bol"],
  2150. "tag"=>$mInfo["milestone_description"],
  2151. "location"=>$mInfo["milestone_locations"],
  2152. "timezone"=>$mInfo["milestone_timezone"],
  2153. "time"=>$mInfo["milestone_date"]." ".$mInfo["milestone_time"],
  2154. "timeLabel"=>"",
  2155. "previous"=>"",
  2156. "frequency_type"=>$mInfo["frequency_type"],
  2157. "serial_no"=>common::deCode($mInfo["serial_no"], 'E'),
  2158. "order_from"=>$mInfo["order_from"],
  2159. "id"=>$mInfo["id"],
  2160. "insert_date_format"=>'',
  2161. "rules_type"=>$mInfo["notifiation_type"],
  2162. "is_display_hbol"=>!empty($mInfo["h_bol"]),
  2163. "first_notifiation_date"=>$mInfo["first_notifiation_date"],
  2164. "info"=>new stdClass());
  2165. if ($mInfo["frequency_type"] == "Daily"){
  2166. $eventCard["numericRecords"] = !empty($mInfo["numericRecords"]) ? $mInfo["numericRecords"] : 0;
  2167. $eventCard["title"] = "Milestone Update Daily Summary(".$mInfo["insert_date_format"].")";
  2168. $eventCard["insert_date_format"] = $mInfo["insert_date_format"];
  2169. //seeall 的时候,前端不需要id
  2170. $eventCard["id"] = '';
  2171. $milestone_previous = json_decode($mInfo["milestone_previous_json"],true);
  2172. if(!empty($milestone_previous['milestone']['milestone_date'])){
  2173. $eventCard["previous"] = array("tag" =>"Previous:".$milestone_previous['milestone']['milestone_description']." from ".$milestone_previous['milestone']['locations'],
  2174. "date" => $milestone_previous['milestone']['milestone_date'],
  2175. "time" => $milestone_previous['milestone']['milestone_time'],
  2176. "timezone" =>$milestone_previous['milestone']['timezone']);
  2177. }
  2178. } else if($mInfo["frequency_type"] == "Weekly"){
  2179. $eventCard["numericRecords"] = !empty($mInfo["numericRecords"]) ? $mInfo["numericRecords"] : 0;
  2180. $eventCard["title"] = "Milestone Update Weekly Summary(".$mInfo["insert_date_format"].")";
  2181. $eventCard["insert_date_format"] = $mInfo["insert_date_format"];
  2182. //seeall 的时候,前端不需要id
  2183. $eventCard["id"] = '';
  2184. $milestone_previous = json_decode($mInfo["milestone_previous_json"],true);
  2185. if(!empty($milestone_previous['milestone']['milestone_date'])){
  2186. $eventCard["previous"] = array("tag" =>"Previous:".$milestone_previous['milestone']['milestone_description']." from ".$milestone_previous['milestone']['locations'],
  2187. "date" => $milestone_previous['milestone']['milestone_date'],
  2188. "time" => $milestone_previous['milestone']['milestone_time'],
  2189. "timezone" =>$milestone_previous['milestone']['timezone']);
  2190. }
  2191. }
  2192. }
  2193. if($notifiation_type == "Container_Status_Update"){
  2194. //当前状态的描述ctnr_desc
  2195. //$ctnrStatusdesc = $this->getContainerStatusDesc($mInfo["ctnr_status_code"]);
  2196. $eventCard = array("type" =>'container',
  2197. "numericRecords"=>0,
  2198. "isRead"=>!empty($mInfo["is_send_message"]) ? true : false,
  2199. "title"=>"Container_Status_Update",
  2200. "mode"=>"Ocean Freight",
  2201. "no"=>$mInfo["ctnr"],
  2202. "tag"=>$mInfo["ctnr_desc"],
  2203. "location"=>$mInfo["ctnr_status_locations"],
  2204. "timezone"=>$mInfo["ctnr_status_timezone"],
  2205. "time"=>$mInfo["ctnr_status_date"]." ".$mInfo["ctnr_status_time"],
  2206. "timeLabel"=>"",
  2207. "previous"=>"",
  2208. "frequency_type"=>$mInfo["frequency_type"],
  2209. "serial_no"=>common::deCode($mInfo["serial_no"], 'E'),
  2210. "order_from"=>$mInfo["order_from"],
  2211. "id"=>$mInfo["id"],
  2212. "insert_date_format"=>'',
  2213. "rules_type"=>$mInfo["notifiation_type"],
  2214. "first_notifiation_date"=>$mInfo["first_notifiation_date"],
  2215. "info"=>new stdClass());
  2216. if ($mInfo["frequency_type"] == "Daily"){
  2217. $eventCard["numericRecords"] = !empty($mInfo["numericRecords"]) ? $mInfo["numericRecords"] : 0;
  2218. $eventCard["title"] = "Container Status Update Daily Summary(".$mInfo["insert_date_format"].")";
  2219. $eventCard["insert_date_format"] = $mInfo["insert_date_format"];
  2220. //seeall 的时候,前端不需要id
  2221. $eventCard["id"] = '';
  2222. $ctnr_previous = json_decode($mInfo["ctnr_previous_json"],true);
  2223. if(!empty($ctnr_previous["ctnrStatus"]["date"])){
  2224. //当前状态 前一个的描述
  2225. $eventCard["previous"] = array("tag" =>"Previous:" .$ctnr_previous["ctnrStatus"]["description"]. " from " .$ctnr_previous["ctnrStatus"]["locations"],
  2226. "date" => $ctnr_previous["ctnrStatus"]["date"],
  2227. "time" => $ctnr_previous["ctnrStatus"]["time"],
  2228. "timezone" =>$ctnr_previous["ctnrStatus"]["timezone"]);
  2229. }
  2230. } else if($mInfo["frequency_type"] == "Weekly"){
  2231. $eventCard["numericRecords"] = !empty($mInfo["numericRecords"]) ? $mInfo["numericRecords"] : 0;
  2232. $eventCard["title"] = "Container Status Update Weekly Summary(".$mInfo["insert_date_format"].")";
  2233. $eventCard["insert_date_format"] = $mInfo["insert_date_format"];
  2234. //seeall 的时候,前端不需要id
  2235. $eventCard["id"] = '';
  2236. $ctnr_previous = json_decode($mInfo["ctnr_previous_json"],true);
  2237. if(!empty($ctnr_previous["ctnrStatus"]["date"])){
  2238. //当前状态 前一个的描述
  2239. $eventCard["previous"] = array("tag" =>"Previous:" .$ctnr_previous["ctnrStatus"]["description"]. " from " .$ctnr_previous["ctnrStatus"]["locations"],
  2240. "date" => $ctnr_previous["ctnrStatus"]["date"],
  2241. "time" => $ctnr_previous["ctnrStatus"]["time"],
  2242. "timezone" =>$ctnr_previous["ctnrStatus"]["timezone"]);
  2243. }
  2244. }
  2245. }
  2246. if($notifiation_type == "Departure/Arrival_Delay"){
  2247. //代表信息为转船信息,title要处理一下: leg 2/3 Departure_Delay => Departure_Delay
  2248. $title = $mInfo["delay_name"];
  2249. $outsideLocation = "";
  2250. $outsideTimezone = "";
  2251. $outsideTimeLabel= "";
  2252. $insideTimeLabel= "";
  2253. if(utils::checkExist($mInfo["delay_name"],"Departure_Delay")){
  2254. $outsideTimeLabel = "ETD";
  2255. $insideTimeLabel = "ATD";
  2256. }
  2257. if(utils::checkExist($mInfo["delay_name"],"Arrival_Delay")){
  2258. $outsideTimeLabel = "ETA";
  2259. $insideTimeLabel = "ATA";
  2260. }
  2261. //直航的的
  2262. if(utils::checkExist($mInfo["delay_name"],"Departure_Delay") and $mInfo["delay_is_direct"] == 't'){
  2263. $outsideLocation = $mInfo["delay_locations_from"];
  2264. }
  2265. if(utils::checkExist($mInfo["delay_name"],"Arrival_Delay") and $mInfo["delay_is_direct"] == 't'){
  2266. $outsideLocation = $mInfo["delay_locations_to"];
  2267. }
  2268. $route = array();
  2269. $leg = array();
  2270. if($mInfo["delay_is_direct"] <>'t'){
  2271. $title = substr($mInfo["delay_name"],8);
  2272. $route = array($mInfo["delay_locations_from"],$mInfo["delay_locations_transshipment"],$mInfo["delay_locations_to"]);
  2273. //当前current Leg
  2274. $leg = array($mInfo["delay_locations_from"],$mInfo["delay_locations_transshipment"]);
  2275. if($mInfo["delay_current"] == "2"){
  2276. $leg = array($mInfo["delay_locations_transshipment"],$mInfo["delay_locations_to"]);
  2277. }
  2278. }
  2279. $act_date = $mInfo["delay_act_date"]." ".$mInfo["delay_act_time"];
  2280. $est_date = $mInfo["delay_est_date"]." ".$mInfo["delay_est_time"];
  2281. $delay_diff = $mInfo["_delay_diff"];
  2282. $delay_unit = $mInfo["delay_unit"];
  2283. $eventCard = array("type" =>'delay',
  2284. "numericRecords"=>0,
  2285. "isRead"=>!empty($mInfo["is_send_message"]) ? true : false,
  2286. "title"=>$title,
  2287. "mode"=>$mInfo["transport_mode"] == 'sea' ? "Ocean Freight": "Air Freight",
  2288. "no"=>!empty($mInfo["h_bol"]) ? $mInfo["h_bol"] : $mInfo["m_bol"],
  2289. //第一个字母大写
  2290. "tag"=>ucfirst($mInfo["delay_name"]),
  2291. "location"=>$outsideLocation,
  2292. "timezone"=>$mInfo["delay_timezone"],
  2293. "time"=>$est_date,
  2294. "timeLabel"=>$outsideTimeLabel,
  2295. "previous"=>"",
  2296. "frequency_type"=>$mInfo["frequency_type"],
  2297. "serial_no"=>common::deCode($mInfo["serial_no"], 'E'),
  2298. "order_from"=>$mInfo["order_from"],
  2299. "id"=>$mInfo["id"],
  2300. "insert_date_format"=>'',
  2301. "rules_type"=>$mInfo["notifiation_type"],
  2302. "is_display_hbol"=>!empty($mInfo["h_bol"]),
  2303. "first_notifiation_date"=>$mInfo["first_notifiation_date"],
  2304. "info"=>array("route"=>$route,
  2305. "leg"=>$leg,
  2306. "etdOrdeparturNum"=>0,
  2307. "etaOrarrivalNum"=>0,
  2308. "time"=>$act_date,
  2309. "timeLabel"=>$insideTimeLabel,
  2310. "delayTimeTip"=>"+".$delay_diff." ".$delay_unit." delay",
  2311. "timezone"=>$mInfo["delay_timezone"]
  2312. ));
  2313. if ($mInfo["frequency_type"] == "Daily"){
  2314. $eventCard["numericRecords"] = !empty($mInfo["numericRecords"]) ? $mInfo["numericRecords"] : 0;
  2315. $eventCard["info"]["etdOrdeparturNum"] = !empty($mInfo["numericRecords_one"]) ? $mInfo["numericRecords_one"] : 0;
  2316. $eventCard["info"]["etaOrarrivalNum"] = !empty($mInfo["numericRecords_two"]) ? $mInfo["numericRecords_two"] : 0;
  2317. $eventCard["title"] = "Delay Daily Summary(".$mInfo["insert_date_format"].")";
  2318. $eventCard["insert_date_format"] = $mInfo["insert_date_format"];
  2319. //seeall 的时候,前端不需要id
  2320. $eventCard["id"] = '';
  2321. } else if($mInfo["frequency_type"] == "Weekly"){
  2322. $eventCard["numericRecords"] = !empty($mInfo["numericRecords"]) ? $mInfo["numericRecords"] : 0;
  2323. $eventCard["info"]["etdOrdeparturNum"] = !empty($mInfo["numericRecords_one"]) ? $mInfo["numericRecords_one"] : 0;
  2324. $eventCard["info"]["etaOrarrivalNum"] = !empty($mInfo["numericRecords_two"]) ? $mInfo["numericRecords_two"] : 0;
  2325. $eventCard["title"] = "Delay Weekly Summary(".$mInfo["insert_date_format"].")";
  2326. $eventCard["insert_date_format"] = $mInfo["insert_date_format"];
  2327. //seeall 的时候,前端不需要id
  2328. $eventCard["id"] = '';
  2329. }
  2330. }
  2331. if($notifiation_type == "ETD/ETA_Change"){
  2332. $title = $mInfo["date_change_name"];
  2333. if(utils::checkExist($mInfo["date_change_name"],"ETD Change")){
  2334. $outsideTimeLabel = "Original ETD";
  2335. $insideTimeLabel = "Updated ETD";
  2336. }
  2337. if(utils::checkExist($mInfo["date_change_name"],"ETA Change")){
  2338. $outsideTimeLabel = "Original ETA";
  2339. $insideTimeLabel = "Updated ETA";
  2340. }
  2341. if($mInfo["date_change_is_direct"] <>'t'){
  2342. //代表信息为转船信息,title要处理一下: leg 1/3 ETD Change
  2343. $title = substr($mInfo["date_change_name"],8);
  2344. $route = array($mInfo["date_change_locations_from"],$mInfo["date_change_locations_transshipment"],$mInfo["date_change_locations_to"]);
  2345. $leg = array($mInfo["date_change_locations_from"],$mInfo["date_change_locations_transshipment"]);
  2346. if($mInfo["date_change_current"] == "2"){
  2347. $leg = array($mInfo["date_change_locations_transshipment"],$mInfo["date_change_locations_to"]);
  2348. }
  2349. }
  2350. $updated_date = $mInfo["date_change_updated_date"]." ".$mInfo["date_change_updated_time"];
  2351. $original_date = $mInfo["date_change_original_date"]." ".$mInfo["date_change_original_time"];
  2352. $eventCard = array("type" =>'change',
  2353. "numericRecords"=>0,
  2354. "isRead"=>!empty($mInfo["is_send_message"]) ? true : false,
  2355. "title"=>$title,
  2356. "mode"=>$mInfo["transport_mode"] == 'sea' ? "Ocean Freight": "Air Freight",
  2357. "no"=>!empty($mInfo["h_bol"]) ? $mInfo["h_bol"] : $mInfo["m_bol"],
  2358. "tag"=>ucfirst($mInfo["date_change_name"]),
  2359. "location"=>"",
  2360. "timezone"=>$mInfo["date_change_timezone"],
  2361. "time"=>$original_date,
  2362. "timeLabel"=>$outsideTimeLabel,
  2363. "previous"=>"",
  2364. "frequency_type"=>$mInfo["frequency_type"],
  2365. "serial_no"=>common::deCode($mInfo["serial_no"], 'E'),
  2366. "order_from"=>$mInfo["order_from"],
  2367. "id"=>$mInfo["id"],
  2368. "insert_date_format"=>'',
  2369. "rules_type"=>$mInfo["notifiation_type"],
  2370. "is_display_hbol"=>!empty($mInfo["h_bol"]),
  2371. "first_notifiation_date"=>$mInfo["first_notifiation_date"],
  2372. "info"=>array("route"=>$route,
  2373. "leg"=>$leg,
  2374. "etdOrdeparturNum"=>0,
  2375. "etaOrarrivalNum"=>0,
  2376. "time"=>$updated_date,
  2377. "timeLabel"=>$insideTimeLabel,
  2378. "delayTimeTip"=>"",
  2379. "timezone"=>$mInfo["date_change_timezone"]
  2380. ));
  2381. if ($mInfo["frequency_type"] == "Daily"){
  2382. $eventCard["numericRecords"] = !empty($mInfo["numericRecords"]) ? $mInfo["numericRecords"] : 0;
  2383. $eventCard["info"]["etdOrdeparturNum"] = !empty($mInfo["numericRecords_one"]) ? $mInfo["numericRecords_one"] : 0;
  2384. $eventCard["info"]["etaOrarrivalNum"] = !empty($mInfo["numericRecords_two"]) ? $mInfo["numericRecords_two"] : 0;
  2385. $eventCard["title"] = "ETD/ETA Change Daily Summary(".$mInfo["insert_date_format"].")";
  2386. $eventCard["insert_date_format"] = $mInfo["insert_date_format"];
  2387. $eventCard["id"] = "";
  2388. } else if($mInfo["frequency_type"] == "Weekly"){
  2389. $eventCard["numericRecords"] = !empty($mInfo["numericRecords"]) ? $mInfo["numericRecords"] : 0;
  2390. $eventCard["info"]["etdOrdeparturNum"] = !empty($mInfo["numericRecords_one"]) ? $mInfo["numericRecords_one"] : 0;
  2391. $eventCard["info"]["etaOrarrivalNum"] = !empty($mInfo["numericRecords_two"]) ? $mInfo["numericRecords_two"] : 0;
  2392. $eventCard["title"] = "ETD/ETA Change Weekly Summary(".$mInfo["insert_date_format"].")";
  2393. $eventCard["insert_date_format"] = $mInfo["insert_date_format"];
  2394. $eventCard["id"] = "";
  2395. }
  2396. }
  2397. if($notifiation_type == "Feature_Update" || $notifiation_type == "Passwond_Notifcations"){
  2398. $title = $mInfo["other_type"] == "password" ? "Password Notifications" : "Feature Update";
  2399. $isExpiration = false;
  2400. if($title == "Password Notifications" and $mInfo["other_pnum"] <= 3){
  2401. $isExpiration = true;
  2402. }
  2403. $path = $mInfo["id"];
  2404. if(!empty($mInfo["serial_no"])){
  2405. $path =$mInfo["serial_no"];
  2406. }
  2407. $eventCard = array(
  2408. "title" => $title,
  2409. "id"=> $mInfo["id"],
  2410. "header"=> $mInfo["other_name"],
  2411. "content"=>$mInfo["other_desc"],
  2412. "content_two"=>$mInfo["other_desc_two"],
  2413. "isRead"=>!empty($mInfo["is_send_message"]) ? true : false,
  2414. "isExpiration"=> $isExpiration,
  2415. "rules_type"=>$mInfo["notifiation_type"],
  2416. "imgSrc"=>SERVER_PAHT.FILE_UPLOAD_PAHT."feature_update_".$path.".png",
  2417. "view_more_link" =>"main_new_version.php?action=feature_update&id=".$path
  2418. );
  2419. }
  2420. return $eventCard;
  2421. }
  2422. /**
  2423. * 返回当前柜子的status信息描述,目前作废
  2424. */
  2425. public static function getContainerStatusDesc($ctnr_status_code){
  2426. $event =common::getEDICtnrEvent();
  2427. $ctnrStatusdesc = "";
  2428. foreach($event as $e){
  2429. if($e['event_name'] == $ctnr_status_code){
  2430. $ctnrStatusdesc = $e['description'];
  2431. }
  2432. }
  2433. return $ctnrStatusdesc;
  2434. }
  2435. }
  2436. ?>