tools.class.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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. } else {
  45. $str = "Old password is incorrect!";
  46. }
  47. } else {
  48. $str = "Old password is incorrect!";
  49. }
  50. } else {
  51. $str = $msg;
  52. }
  53. }
  54. $returnData = array("msg" => $str);
  55. common::echo_json_encode(200, $returnData);
  56. exit();
  57. }
  58. public function markSystem(){
  59. $operate = utils::_get('operate');
  60. $operate = strtolower($operate);
  61. if ($operate == "mark_save") {
  62. $suggestion = utils::implode(",",$_POST['suggestion']);
  63. $proposal = common::check_input($_POST['proposal']);
  64. $expression = common::check_input($_POST['expression']);
  65. $complete_funtionality = common::check_input($_POST['Complete_funtionality']);
  66. $accurate_data = common::check_input($_POST['Accurate_data']);
  67. $clear_information = common::check_input($_POST['Clear_information']);
  68. $easy_to_use = common::check_input($_POST['Easy_to_use']);
  69. $system_Performance = common::check_input($_POST['System_Performance']);
  70. $username = common::check_input($_POST['username']);
  71. $user_type = _isApexLogin() ? "employee" : "customer";
  72. if(!isset($_SESSION['ONLINE_USER'])){
  73. $user_type = "other";
  74. }
  75. $loginName = _getLoginName();
  76. $loginEamil = _getLoginEamil();
  77. //如果在没有登录前,没有登录信息,指定用户-- 这里逻辑取消,没有登录相当于匿名用户的评价,无法获取用户名
  78. // if(!isset($_SESSION['ONLINE_USER'])){
  79. // $user_type = "Customer";
  80. // if(!empty($username)){
  81. // $loginName = $username;
  82. // $loginEamil = common::excuteOneSql("select email from public.ra_online_user u where lower(user_login) = '" . strtolower($username) . "'");
  83. // }
  84. // }
  85. $sql = "INSERT INTO public.customer_service_user_mark(user_type, user_name, suggestion, proposal, expression, complete_funtionality,
  86. accurate_data, clear_information, easy_to_use, system_performance,
  87. created_time,email)
  88. VALUES ('$user_type', '$loginName', '$suggestion', '$proposal', '$expression', '$complete_funtionality',
  89. '$accurate_data', '$clear_information', '$easy_to_use', '$system_Performance', now(),'$loginEamil')";
  90. common::excuteUpdateSql($sql);
  91. $data = array("msg" =>"success");
  92. common::echo_json_encode(200,$data);
  93. exit();
  94. }
  95. }
  96. public function user_system_setting(){
  97. $operate = utils::_get('operate');
  98. $operate = strtolower($operate);
  99. if ($operate == "personal_profile_init") {
  100. // get system config
  101. $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')";
  102. $rs1s = common::excuteListSql($sql);
  103. foreach ($rs1s as $rs1) {
  104. if ($rs1['ra_name'] == 'employee_password_change_cycle')
  105. $EMPLOYEE_PASSWORD_CHANGE_CYCLE = $rs1['ra_value'];
  106. if ($rs1['ra_name'] == 'customer_password_change_cycle')
  107. $CUSTOMER_PASSWORD_CHANGE_CYCLE = $rs1['ra_value'];
  108. }
  109. $sql="select item_value from config where item='passwordChangePeriod'";
  110. $pcp = common::excuteObjectSql($sql);
  111. $passwordChangePeriod = json_decode($pcp["item_value"],true);
  112. if (_isApexLogin()) {
  113. $PASSWORD_CHANGE_CYCLE = $EMPLOYEE_PASSWORD_CHANGE_CYCLE;
  114. //如果有新配置,则采用新配置
  115. if (!empty($pcp)) {
  116. $PASSWORD_CHANGE_CYCLE = $passwordChangePeriod["Employee"]["days"];
  117. }
  118. } else {
  119. $PASSWORD_CHANGE_CYCLE = $CUSTOMER_PASSWORD_CHANGE_CYCLE;
  120. //如果有新配置,则采用新配置
  121. if (!empty($pcp)) {
  122. $PASSWORD_CHANGE_CYCLE = $passwordChangePeriod["Customer"]["days"];
  123. }
  124. }
  125. $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,
  126. ue.date_format,ue.numbers_format
  127. from ra_online_user u
  128. left join kln_user_extend ue on u.user_login = ue.user_login
  129. where lower(u.user_login) = '".strtolower(_getLoginName())."' ";
  130. $data = common::excuteObjectSql($sql);
  131. $data["expire_day"] = $PASSWORD_CHANGE_CYCLE - $data['last_pwd_change_date'];
  132. common::echo_json_encode(200,$data);
  133. exit();
  134. }
  135. if ($operate == "personal_profile_save") {
  136. $save_model = common::check_input($_POST['save_model']);
  137. if ($save_model == "profile"){
  138. $user_name = common::check_input($_POST['user_name']);
  139. $email = common::check_input($_POST['email']);
  140. $sql = "update public.ra_online_user set user_login = '$user_name',email = '$email' where lower(user_login) = '".strtolower(_getLoginName())."'";
  141. }else{
  142. $date_fromat = common::check_input($_POST['date_fromat']);
  143. $numbers_format = common::check_input($_POST['numbers_format']);
  144. $exist_kln_user = common::excuteObjectSql("select user_login from public.kln_user_extend where lower(user_login) = '".strtolower(_getLoginName())."'");
  145. if (!empty($exist_kln_user['user_login'])){
  146. $sql = "update public.kln_user_extend set date_fromat = '$date_fromat',numbers_format = '$numbers_format' where lower(user_login) = '".strtolower(_getLoginName())."'";
  147. } else {
  148. $sql = "INSERT INTO public.kln_user_extend(user_login, date_format, numbers_format, subscribe_hbol)
  149. VALUES ('"._getLoginName()."', '$date_fromat', '$numbers_format', null);";
  150. }
  151. }
  152. common::excuteUpdateSql($sql);
  153. $data = array("msg" => "save Successful");
  154. common::echo_json_encode(200,$data);
  155. exit();
  156. }
  157. if ($operate == "subscribe_notification_init") {
  158. $subscribur_data =array();
  159. //查询用户对应的Rule
  160. $subscribe_rule_sql = "select *,TO_CHAR(daily_time, 'HH24:MI') as _daily_time,
  161. TO_CHAR(weekly_time, 'HH24:MI') as _weekly_time
  162. from public.notifications_rules where notifications_type = 'Subscribe' and lower(user_login) = '".strtolower(_getLoginName())."' order by id";
  163. $subscribe_rules = common::excuteListSql($subscribe_rule_sql);
  164. $all_rules = array("Milestone_Update","Container_Status_Update","Departure/Arrival_Delay","ETD/ETA_Change");
  165. foreach($all_rules as $rule_name){
  166. $rules = $this->getSubscribeRules($rule_name,$subscribe_rules);
  167. $subscribur_data[$rule_name] = $rules;
  168. }
  169. //整合拼接addedRules
  170. $addedRules = array();
  171. foreach($subscribe_rules as $addedRule){
  172. $addedRules[] = array(
  173. "visible" => false,
  174. "id" =>$addedRule['id'],
  175. "Event" =>$addedRule['rules_type'],
  176. "Event Details" =>$addedRule['event_details'],
  177. "Frequency" =>$addedRule['frequency_display'],
  178. "Methods" =>$addedRule['method_display']);
  179. }
  180. $subscribur_data['addedRules'] = array("tableData"=>$addedRules);
  181. //获取subscribe shipment 当前页数cp,每页ps
  182. $subscribeShipmentWithPage = $this->getSubscribeShipment(1,15);
  183. $subscribur_data['subscribeShipmentWithPage'] = $subscribeShipmentWithPage;
  184. common::echo_json_encode(200,$subscribur_data);
  185. exit();
  186. }
  187. if ($operate == "subscribe_notification_event_update"){
  188. $rules_type = common::check_input($_POST["rules_type"]);
  189. //判断该规则是否存在
  190. $exist = common::excuteObjectSql("select user_login,id from public.notifications_rules where notifications_type = 'Subscribe' and rules_type = '".$rules_type."'
  191. and lower(user_login) = '".strtolower(_getLoginName())."'");
  192. $updateOrInsert = empty($exist) ? "insert" : "update";
  193. $sql = $this->getNotificationsRulesUpdateSql($updateOrInsert,$rules_type,"Subscribe",$exist['id']);
  194. $rs = common::excuteUpdateSql($sql);
  195. if ($rs === FALSE){
  196. $data = array("msg" => "Update Error");
  197. } else{
  198. $data = array("msg" => "Update Successful");
  199. //返回addedRules 全部列表
  200. $subscribe_rule_sql = "select * from public.notifications_rules where notifications_type = 'Subscribe' and lower(user_login) = '".strtolower(_getLoginName())."' order by id";
  201. $subscribe_rules = common::excuteListSql($subscribe_rule_sql);
  202. //整合拼接addedRules
  203. $addedRules = array();
  204. foreach($subscribe_rules as $addedRule){
  205. $addedRules[] = array(
  206. "id" =>$addedRule['id'],
  207. "Event" =>$addedRule['rules_type'],
  208. "Event Details" =>$addedRule['event_details'],
  209. "Frequency" =>$addedRule['frequency_display'],
  210. "Methods" =>$addedRule['method_display']);
  211. }
  212. $data['addedRules'] = array("tableData"=>$addedRules);
  213. }
  214. common::echo_json_encode(200,$data);
  215. exit();
  216. }
  217. if ($operate == "subscribe_notification_rules_delete"){
  218. $rules_type = common::check_input($_POST['rules_type']);
  219. $sql = "delete from notifications_rules where notifications_type = 'Subscribe'
  220. and rules_type = '$rules_type' and lower(user_login) = '".strtolower(_getLoginName())."'";
  221. common::excuteUpdateSql($sql);
  222. $data = array("msg" => "Delete Successful");
  223. common::echo_json_encode(200,$data);
  224. exit();
  225. }
  226. if ($operate == "subscribe_shipment"){
  227. $serial_no = common::deCode($_POST['serial_no'], 'D');
  228. $is_subscribe = common::check_input($_POST['is_subscribe']);
  229. if($is_subscribe == "true"){
  230. $exist = common::excuteOneSql("select user_login from public.kln_user_subscribed where lower(user_login) = '".strtolower(_getLoginName())."' and subscribed_serial_no = '$serial_no'");
  231. if(!empty($exist)){
  232. $data = array("msg" => "Subscribe exist,Please check");
  233. common::echo_json_encode(200,$data);
  234. exit();
  235. }
  236. $sql = "INSERT INTO public.kln_user_subscribed(user_login, subscribed_serial_no, create_user, create_time)
  237. VALUES ('"._getLoginName()."', '$serial_no', '"._getLoginName()."', now());";
  238. common::excuteUpdateSql($sql);
  239. $data = array("msg" => "Subscribe Successful");
  240. common::echo_json_encode(200,$data);
  241. exit();
  242. }else{
  243. //取消订阅
  244. $sql = "delete from public.kln_user_subscribed where lower(user_login) = '".strtolower(_getLoginName())."' and subscribed_serial_no = '$serial_no';";
  245. common::excuteUpdateSql($sql);
  246. $data = array("msg" => "Cancel Subscribe successfully");
  247. common::echo_json_encode(200,$data);
  248. exit();
  249. }
  250. }
  251. if ($operate == "subscribe_shipment_search"){
  252. $cp = common::check_input($_POST ['cp']); //current_page
  253. $ps = common::check_input($_POST ['ps']); //ps
  254. $arrTmp = $this->getSubscribeShipment($cp,$ps);
  255. common::echo_json_encode(200,$arrTmp);
  256. exit();
  257. }
  258. }
  259. public function user_monitoring_setting(){
  260. $operate = utils::_get('operate');
  261. $operate = strtolower($operate);
  262. if ($operate == "monitoring_rules_search") {
  263. $cp = common::check_input($_POST ['cp']); //current_page
  264. $ps = common::check_input($_POST ['ps']); //ps
  265. if (empty($ps))
  266. $ps = 50;
  267. $sql = "select count(1) from public.notifications_rules where lower(user_login) = '".strtolower(_getLoginName())."' and notifications_type = 'Monitoring'";
  268. $rc = common::excuteOneSql($sql);
  269. $tp = ceil($rc / $ps);
  270. if ($rc > 0) {
  271. $sql = "select count(1) from public.notifications_rules
  272. where lower(user_login) = '".strtolower(_getLoginName())."'
  273. and notifications_type = 'Monitoring' order by id desc limit " . $ps . " offset " . ($cp - 1) * $ps;
  274. $monitoringRules = common::excuteObjectSql($sql);
  275. $arrTmp = array('monitoringRules' => $monitoringRules,
  276. 'rc' => intval($rc),
  277. 'ps' => $ps,
  278. 'cp' => $cp,
  279. 'tp' => $tp
  280. );
  281. } else {
  282. $arrTmp = array('searchData' => array(),
  283. 'rc' => $rc,
  284. 'ps' => $ps,
  285. 'cp' => $cp,
  286. 'tp' => $tp,
  287. );
  288. }
  289. common::echo_json_encode(200,$arrTmp);
  290. exit();
  291. }
  292. if ($operate == "monitoring_rules_do") {
  293. $rules_type = common::check_input($_POST["rules_type"]);
  294. //检查编辑提交的Monitoring规则,是否允许保存
  295. $msg = $this->checkedMonitoringRulesSave($rules_type);
  296. if(!empty($msg)){
  297. $data = array("msg" =>$msg);
  298. common::echo_json_encode(200,$data);
  299. exit();
  300. }
  301. $updateOrInsert = "insert";
  302. if(isset($_POST['id']) && !empty($_POST['id'])){
  303. $updateOrInsert = "update";
  304. }
  305. $sql = $this->getNotificationsRulesUpdateSql($updateOrInsert,$rules_type,"Monitoring",$_POST['id']);
  306. $rs = common::excuteUpdateSql($sql);
  307. if ($rs === FALSE){
  308. $data = array("msg" => "Update Error");
  309. } else{
  310. $data = array("msg" => "Update Successful");
  311. }
  312. common::echo_json_encode(200,$data);
  313. exit();
  314. }
  315. if ($operate == "monitoring_rules_delete"){
  316. $id = common::check_input($_POST['id']);
  317. $rules_type = common::check_input($_POST['rules_type']);
  318. $sql = "delete from notifications_rules where notifications_type = 'Monitoring'
  319. and rules_type = '$rules_type' and lower(user_login) = '".strtolower(_getLoginName())."' and id = '$id '";
  320. common::excuteUpdateSql($sql);
  321. $data = array("msg" => "Delete Successful");
  322. common::echo_json_encode(200,$data);
  323. exit();
  324. }
  325. }
  326. public function notifications_rules(){
  327. $operate = utils::_get('operate');
  328. $operate = strtolower($operate);
  329. if ($operate == "notifications"){
  330. $rules_type = common::check_input($_POST ['rules_type']);
  331. if ($rules_type == "all"){
  332. $sql = "select *,
  333. (select o.h_bol from public.kln_ocean o where o.serial_no = ni.serial_no limit 1)
  334. from public.kln_notifiation_info ni
  335. where lower(ni.user_login) = '"._getLoginName()."'
  336. and (ni.frequency_type = 'Instant'
  337. or (ni.frequency_type = 'Daily' and timezone(ni.daily_time_zone, NOW()::time) > ni.daily_time::time)
  338. or (ni.frequency_type = 'Weekly' and timezone(ni.weekly_time_zone, NOW()::time) > ni.weekly_time::time
  339. and ni.weekly_week ilike '%'|| EXTRACT(dow FROM timezone(ni.weekly_time_zone, NOW())) ||'%'))
  340. and ni.notifications_method = true and ni.is_send_message = false order by ni.insert_date desc";
  341. }else{
  342. $sql = "select case when COALESCE(ni.milestone_code,'') <> '' and ni.milestone_transport_mode = 'sea'
  343. then (select description from public.customer_service_milestone_sno where type = 'sea' and code = ni.milestone_code)
  344. else (select description from public.customer_service_milestone_sno where type = 'air' and code = ni.milestone_code) end as milestone_desc,
  345. ni.milestone_code,*
  346. (select o.h_bol from public.kln_ocean o where o.serial_no = ni.serial_no limit 1)
  347. from public.kln_notifiation_info ni
  348. where lower(ni.user_login) = '"._getLoginName()."'
  349. and ni.rules_type = '".$rules_type."'
  350. and (ni.frequency_type = 'Instant'
  351. or (ni.frequency_type = 'Daily' and timezone(ni.daily_time_zone, NOW()::time) > ni.daily_time::time)
  352. or (ni.frequency_type = 'Weekly' and timezone(ni.weekly_time_zone, NOW()::time) > ni.weekly_time::time
  353. and ni.weekly_week ilike '%'|| EXTRACT(dow FROM timezone(ni.weekly_time_zone, NOW())) ||'%'))
  354. and ni.notifications_method = true and ni.is_send_message = false order by ni.insert_date desc";
  355. }
  356. $notifications = common::excuteListSql($sql);
  357. $all_rules = array("Milestone_Update","Container_Status_Update","Departure/Arrival_Delay","ETD/ETA_Change");
  358. foreach($notifications as $notification){
  359. if($notification["notifiation_type"] == "Milestone_Update"){
  360. if ($notification["frequency_type"] == "Instant"){
  361. $milestoneInstantMessage[] = array("mode"=>$notification["milestone_transport_mode"],"HBOL"=>$notification["milestone_transport_mode"],
  362. "HBOL"=>$notification["milestone_transport_mode"]);
  363. }
  364. }
  365. }
  366. }
  367. }
  368. /**
  369. * 遍历查找对应的rule。
  370. */
  371. public function getSubscribeRules($rule_name,$subscribe_rules){
  372. //初始是不显示,没有值的情况
  373. $ret = array("is_display" => false);
  374. foreach($subscribe_rules as $rules){
  375. if($rules['rules_type'] == $rule_name){
  376. $rules["is_display"] = true;
  377. $rules["daily_time"] = $rules["_daily_time"];
  378. $rules["weekly_time"] = $rules["_weekly_time"];
  379. $rules["weekly_week"] = common::getWeek($rules["weekly_week"]);
  380. $ret = $rules;
  381. }
  382. }
  383. //Milestone Update的结构处理,处理init page load
  384. if($rule_name == "Milestone_Update"){
  385. //Milestone Update的页面配置数据
  386. $milestones = common::excuteListSql("select * from customer_service_milestone_sno order by type, sno");
  387. $oceanMilestone = array();
  388. $airMilestone = array();
  389. foreach($milestones as $milestone){
  390. if($milestone['type'] == "air"){
  391. $airMilestone[] = array("label"=>$milestone['description'],"value"=>$milestone['description'],
  392. "code"=>$milestone['code'],"description"=>$milestone['description']);
  393. }
  394. if($milestone['type'] == "ocean"){
  395. $oceanMilestone[] = array("label"=>$milestone['description'],"value"=>$milestone['description'],
  396. "code"=>$milestone['code'],"description"=>$milestone['description']);
  397. }
  398. }
  399. $ret["OceanCheckBoxList"] = $oceanMilestone;
  400. $ret["AirCheckBoxList"] = $airMilestone;
  401. $oceanMilestoneSetting = !empty($ret['ocean_milestone']) ? explode(";",$ret['ocean_milestone']) : array();
  402. $airMilestoneSetting = !empty($ret['air_milestone']) ? explode(";",$ret['air_milestone']): array();
  403. //转换描述,因为前端支持description,没有code的对应
  404. $oceanMilestoneDescription = $this->convertDescriptionForMilestoneAndCtnr("Milestone_Update",$oceanMilestoneSetting,$oceanMilestone);
  405. //转换描述,因为前端支持description,没有code的对应
  406. $airMilestoneDescription = $this->convertDescriptionForMilestoneAndCtnr("Milestone_Update",$airMilestoneSetting,$airMilestone);
  407. $ret["OceanCheckedList"] = $oceanMilestoneDescription;
  408. $ret["AirCheckedList"] = $airMilestoneDescription;
  409. }
  410. //Milestone Update的结构处理
  411. if($rule_name == "Container_Status_Update"){
  412. //这里基准event 写死, 根据online查询页面的通用的来, 这里需提问确定
  413. $event =$this->getEDICtnrEvent();
  414. $ctnrStatus = array();
  415. foreach($event as $e){
  416. $ctnrStatus[] = array("label"=>$e['description'],"value"=>$e['description']);
  417. }
  418. $ret["CtnrCheckBoxList"] = $ctnrStatus;
  419. $ctnrStatusSetting = !empty($ret['ocean_ctnr_status']) ? explode(";",$ret['ocean_ctnr_status']) : array();
  420. //转换描述,因为前端支持description,没有code的对应
  421. $ctnrStatusDescription = $this->convertDescriptionForMilestoneAndCtnr("Container_Status_Update",$ctnrStatusSetting,$event);
  422. $ret["CtnrCheckedList"] = $ctnrStatusDescription;
  423. }
  424. return $ret;
  425. }
  426. /**
  427. * 查询对应用户订阅的shipment信息.可能存在分页查询,如果有需要就改正
  428. * cp current_page
  429. */
  430. public function getSubscribeShipment($cp,$ps){
  431. if (empty($cp)){
  432. $cp = 1;
  433. }
  434. if (empty($ps)){
  435. $ps = 15;
  436. }
  437. $sql = "select count(1) from public.kln_user_subscribed u
  438. left join public.kln_ocean o on o.serial_no = u.subscribed_serial_no
  439. where lower(user_login) = '".strtolower(_getLoginName())."'";
  440. $rc = common::excuteOneSql($sql);
  441. $tp = ceil($rc / $ps);
  442. if ($rc > 0) {
  443. $sql = "select o.h_bol,
  444. o.shipper,o.consignee,o.etd,o.eta,
  445. case when transport_mode = 'sea'
  446. then (select sn.description
  447. from public.ocean_milestone a
  448. inner join public.customer_service_milestone_sno sn on sn.code=a.code and sn.type = 'sea'
  449. where a.serial_no=o.serial_no and act_date is not null order by sn.sno desc limit 1)
  450. when transport_mode = 'air' and order_from = 'public'
  451. then (select sn.description
  452. from public.air_milestone a
  453. inner join public.customer_service_milestone_sno sn on sn.code=a.code and sn.type = 'air'
  454. where a.serial_no=o.serial_no and act_date is not null order by sn.sno desc limit 1)
  455. when transport_mode = 'air' and order_from = 'sfs'
  456. then (select sn.description
  457. from sfs.air_milestone a
  458. inner join public.customer_service_milestone_sno sn on sn.code=a.code and sn.type = 'air'
  459. where a.serial_no=o.serial_no and act_date is not null order by sn.sno desc limit 1)
  460. else '' end as recent_milestone
  461. from public.kln_user_subscribed u
  462. left join public.kln_ocean o on o.serial_no = u.subscribed_serial_no
  463. where lower(user_login) = '".strtolower(_getLoginName())."' order by u.id desc limit " . $ps . " offset " . ($cp - 1) * $ps;
  464. $subscribeShipment = common::excuteListSql($sql);
  465. $arrTmp = array('tableData' => $subscribeShipment,
  466. 'rc' => intval($rc),
  467. 'ps' => $ps,
  468. 'cp' => $cp,
  469. 'tp' => $tp
  470. );
  471. } else {
  472. $arrTmp = array('tableData' => array(),
  473. 'rc' => $rc,
  474. 'ps' => $ps,
  475. 'cp' => $cp,
  476. 'tp' => $tp,
  477. );
  478. }
  479. return $arrTmp;
  480. }
  481. /**
  482. * 转换描述,因为前端支持description,没有code的对应
  483. */
  484. public function convertDescriptionForMilestoneAndCtnr($type,$codeArr,$mappingData){
  485. $descriptions = array();
  486. $key = $type == "Milestone_Update" ? "code" : "event_name";
  487. foreach($codeArr as $code){
  488. foreach($mappingData as $md){
  489. if($md[$key] == $code){
  490. $descriptions[] = common::check_input($md['description']);
  491. }
  492. }
  493. }
  494. return $descriptions;
  495. }
  496. /**
  497. * 转换code,因为前端提交的是description,没有code的
  498. */
  499. public function convertCodeForMilestoneAndCtnr($type,$descriptionArr,$mappingData){
  500. $codes = array();
  501. $key = $type == "Milestone_Update" ? "code" : "event_name";
  502. foreach($descriptionArr as $description){
  503. foreach($mappingData as $md){
  504. if($md['description'] == $description){
  505. $codes[] = common::check_input($md[$key]);
  506. }
  507. }
  508. }
  509. return $codes;
  510. }
  511. public function getNotificationsRulesUpdateSql($updateOrInsert,$rules_type,$notifications_type,$id){
  512. $sql = "";
  513. //先删后加
  514. if($updateOrInsert == "update"){
  515. $sql.="delete from public.notifications_rules where rules_type = '$rules_type'
  516. and notifications_type = '$notifications_type' and lower(user_login) = '".strtolower(_getLoginName())."'
  517. and id = '$id';";
  518. }
  519. //这个几个参数是所有规则都有的参数
  520. $frequency_type = common::check_input($_POST['frequency_type']);
  521. $daily_time = "null";
  522. $daily_time_zone = "";
  523. $weekly_week = "";
  524. $weekly_time = "null";
  525. $weekly_time_zone = "";
  526. if(strtolower($frequency_type) == "daily"){
  527. $daily_time = "'".common::check_input($_POST['daily_time'])."'";
  528. $daily_time_zone = common::check_input($_POST['daily_time_zone']);
  529. } elseif (strtolower($frequency_type) == "weekly"){
  530. $weekly_week = common::check_input($_POST['weekly_week']);
  531. $weekly_time = "'".common::check_input($_POST['weekly_time'])."'";
  532. $weekly_time_zone = common::check_input($_POST['weekly_time_zone']);
  533. }
  534. $method_by_email = common::check_input($_POST['method_by_email']);
  535. $method_by_message = common::check_input($_POST['method_by_message']);
  536. $event_details = common::check_input($_POST['event_details']);
  537. $frequency_display = common::check_input($_POST['frequency_display']);
  538. $method_display = common::check_input($_POST['method_display']);
  539. //当规则是 Monitoring类型是,需要配置的range
  540. $shipment_transport_mode = "";
  541. $shipment_etd_limit = "";
  542. $shipment_eta_limit = "";
  543. if($notifications_type == "Monitoring"){
  544. $shipment_transport_mode = utils::implode(";",$_POST['shipment_transport_mode']);
  545. $shipment_etd_limit = common::check_input($_POST['shipment_etd_limit']);
  546. $shipment_eta_limit = common::check_input($_POST['shipment_eta_limit']);
  547. }
  548. if ($rules_type == "Milestone_Update"){
  549. //提交的description 的转换code
  550. $milestones = common::excuteListSql("select * from customer_service_milestone_sno order by type, sno");
  551. $oceanMilestone = array();
  552. $airMilestone = array();
  553. foreach($milestones as $milestone){
  554. if($milestone['type'] == "air"){
  555. $airMilestone[] = $milestone;
  556. }
  557. if($milestone['type'] == "ocean"){
  558. $oceanMilestone[] = $milestone;
  559. }
  560. }
  561. $ocean_milestone = $this->convertCodeForMilestoneAndCtnr("Milestone_Update",$_POST['ocean_milestone'],$oceanMilestone);
  562. $air_milestone = $this->convertCodeForMilestoneAndCtnr("Milestone_Update",$_POST['air_milestone'],$airMilestone);
  563. $ocean_milestone = utils::implode(";",$ocean_milestone);
  564. $air_milestone = utils::implode(";",$air_milestone);
  565. $sql.="INSERT INTO public.notifications_rules(
  566. user_login, notifications_type, rules_type, ocean_milestone,
  567. air_milestone, frequency_type, daily_time, daily_time_zone,
  568. weekly_week, weekly_time, weekly_time_zone, method_by_email, method_by_message,
  569. event_details, frequency_display, method_display,
  570. shipment_transport_mode,shipment_etd_limit,shipment_eta_limit)
  571. VALUES ('".strtolower(_getLoginName())."', '$notifications_type', '$rules_type', '$ocean_milestone',
  572. '$air_milestone', '$frequency_type', $daily_time, '$daily_time_zone',
  573. '$weekly_week', $weekly_time, '$weekly_time_zone', '$method_by_email', '$method_by_message',
  574. '$event_details', '$frequency_display', '$method_display',
  575. '$shipment_transport_mode','$shipment_etd_limit','$shipment_eta_limit');";
  576. }
  577. if ($rules_type == "Container_Status_Update"){
  578. $event = $this->getEDICtnrEvent();
  579. $ocean_ctnr_status = $this->convertCodeForMilestoneAndCtnr("Container_Status_Update",$_POST['ocean_ctnr_status'],$event);
  580. $ocean_ctnr_status = utils::implode(";",$ocean_ctnr_status);
  581. $sql.="INSERT INTO public.notifications_rules(
  582. user_login, notifications_type, rules_type, ocean_ctnr_status,
  583. frequency_type, daily_time, daily_time_zone,
  584. weekly_week, weekly_time, weekly_time_zone, method_by_email, method_by_message,
  585. event_details, frequency_display, method_display,
  586. shipment_transport_mode,shipment_etd_limit,shipment_eta_limit)
  587. VALUES ('".strtolower(_getLoginName())."', '$notifications_type', '$rules_type', '$ocean_ctnr_status',
  588. '$frequency_type', $daily_time, '$daily_time_zone',
  589. '$weekly_week', $weekly_time, '$weekly_time_zone', '$method_by_email', '$method_by_message',
  590. '$event_details', '$frequency_display', '$method_display',
  591. '$shipment_transport_mode','$shipment_etd_limit','$shipment_eta_limit');";
  592. }
  593. if ($rules_type == "Departure/Arrival_Delay"){
  594. $ocean_atd_sub_etd = $_POST['ocean_atd_sub_etd'];
  595. $ocean_atd_sub_etd_unit = $_POST['ocean_atd_sub_etd_unit'];
  596. if(!empty($ocean_atd_sub_etd_unit)){
  597. $ocean_atd_sub_etd_unit = $ocean_atd_sub_etd_unit=="Day(s)" ? "days":"hours";
  598. }
  599. $ocean_ata_sub_eta = $_POST['ocean_ata_sub_eta'];
  600. $ocean_ata_sub_eta_unit = $_POST['ocean_ata_sub_eta_unit'];
  601. if(!empty($ocean_ata_sub_eta_unit)){
  602. $ocean_ata_sub_eta_unit = $ocean_ata_sub_eta_unit=="Day(s)" ? "days":"hours";
  603. }
  604. $air_atd_sub_etd = $_POST['air_atd_sub_etd'];
  605. $air_atd_sub_etd_unit = $_POST['air_atd_sub_etd_unit'];
  606. if(!empty($air_atd_sub_etd_unit)){
  607. $air_atd_sub_etd_unit = $air_atd_sub_etd_unit=="Day(s)" ? "days":"hours";
  608. }
  609. $air_ata_sub_eta = $_POST['air_ata_sub_eta'];
  610. $air_ata_sub_eta_unit = $_POST['air_ata_sub_eta_unit'];
  611. if(!empty($air_ata_sub_eta_unit)){
  612. $air_ata_sub_eta_unit = $air_ata_sub_eta_unit=="Day(s)" ? "days":"hours";
  613. }
  614. $sql.="INSERT INTO public.notifications_rules(
  615. user_login, notifications_type, rules_type,
  616. ocean_atd_sub_etd, ocean_atd_sub_etd_unit,ocean_ata_sub_eta,ocean_ata_sub_eta_unit,
  617. air_atd_sub_etd, air_atd_sub_etd_unit,air_ata_sub_eta,air_ata_sub_eta_unit,
  618. frequency_type, daily_time, daily_time_zone,
  619. weekly_week, weekly_time, weekly_time_zone, method_by_email, method_by_message,
  620. event_details, frequency_display, method_display,
  621. shipment_transport_mode,shipment_etd_limit,shipment_eta_limit)
  622. VALUES ('".strtolower(_getLoginName())."', '$notifications_type', '$rules_type',
  623. '$ocean_atd_sub_etd','$ocean_atd_sub_etd_unit','$ocean_ata_sub_eta','$ocean_ata_sub_eta_unit',
  624. '$air_atd_sub_etd','$air_atd_sub_etd_unit','$air_ata_sub_eta','$air_ata_sub_eta_unit',
  625. '$frequency_type', $daily_time, '$daily_time_zone',
  626. '$weekly_week', $weekly_time, '$weekly_time_zone', '$method_by_email', '$method_by_message',
  627. '$event_details', '$frequency_display', '$method_display',
  628. '$shipment_transport_mode','$shipment_etd_limit','$shipment_eta_limit');";
  629. }
  630. if ($rules_type == "ETD/ETA_Change"){
  631. $ocean_etd_change = $_POST['ocean_etd_change'];
  632. $ocean_etd_old_sub_new = $_POST['ocean_etd_old_sub_new'];
  633. $ocean_etd_old_sub_new_unit = $_POST['ocean_etd_old_sub_new_unit'];
  634. if(!empty($ocean_etd_old_sub_new_unit)){
  635. $ocean_etd_old_sub_new_unit = $ocean_etd_old_sub_new_unit=="Day(s)" ? "days":"hours";
  636. }
  637. $ocean_eta_change = $_POST['ocean_eta_change'];
  638. $ocean_eta_old_sub_new = $_POST['ocean_eta_old_sub_new'];
  639. $ocean_eta_old_sub_new_unit = $_POST['ocean_eta_old_sub_new_unit'];
  640. if(!empty($ocean_eta_old_sub_new_unit)){
  641. $ocean_eta_old_sub_new_unit = $ocean_eta_old_sub_new_unit=="Day(s)" ? "days":"hours";
  642. }
  643. $air_etd_change = $_POST['air_etd_change'];
  644. $air_etd_old_sub_new = $_POST['air_etd_old_sub_new'];
  645. $air_etd_old_sub_new_unit = $_POST['air_etd_old_sub_new_unit'];
  646. if(!empty($air_etd_old_sub_new_unit)){
  647. $air_etd_old_sub_new_unit = $air_etd_old_sub_new_unit=="Day(s)" ? "days":"hours";
  648. }
  649. $air_eta_change = $_POST['air_eta_change'];
  650. $air_eta_old_sub_new = $_POST['air_eta_old_sub_new'];
  651. $air_eta_old_sub_new_unit = $_POST['air_eta_old_sub_new_unit'];
  652. if(!empty($air_eta_old_sub_new_unit)){
  653. $air_eta_old_sub_new_unit = $air_eta_old_sub_new_unit=="Day(s)" ? "days":"hours";
  654. }
  655. $sql.="INSERT INTO public.notifications_rules(
  656. user_login, notifications_type, rules_type,
  657. 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,
  658. 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,
  659. frequency_type, daily_time, daily_time_zone,
  660. weekly_week, weekly_time, weekly_time_zone, method_by_email, method_by_message,
  661. event_details, frequency_display, method_display,
  662. shipment_transport_mode,shipment_etd_limit,shipment_eta_limit)
  663. VALUES ('".strtolower(_getLoginName())."', '$notifications_type', '$rules_type',
  664. '$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',
  665. '$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',
  666. '$frequency_type', $daily_time, '$daily_time_zone',
  667. '$weekly_week', $weekly_time, '$weekly_time_zone', '$method_by_email', '$method_by_message',
  668. '$event_details', '$frequency_display', '$method_display',
  669. '$shipment_transport_mode','$shipment_etd_limit','$shipment_eta_limit');";
  670. }
  671. return $sql;
  672. }
  673. /**
  674. * 检查编辑提交的Monitoring规则,是否允许保存
  675. */
  676. public function checkedMonitoringRulesSave($rules_type){
  677. $sql_where = "";
  678. if(isset($_POST['id']) && !empty($_POST['id'])){
  679. $sql_where = " and id <> '".common::check_input($_POST['id'])."'";
  680. }
  681. $rules = common::excuteOneSql("select * from public.notifications_rules where notifications_type = 'Monitoring' and rules_type = '".$rules_type."'
  682. and lower(user_login) = '".strtolower(_getLoginName())."' $sql_where");
  683. foreach($rules as $rule){
  684. //判断range 是否一样
  685. $checkRangeFiled = array("shipment_transport_mode","shipment_etd_limit","shipment_eta_limit");
  686. $range_flag = true;
  687. foreach($checkRangeFiled as $filed){
  688. if($filed == "shipment_transport_mode"){
  689. $postValue = utils::implode(";",$_POST[$filed]);
  690. }else{
  691. $postValue = !empty($_POST[$filed]) ? $_POST[$filed] : "";
  692. }
  693. if($postValue != $rule[$filed]){
  694. $range_flag = false;
  695. }
  696. }
  697. //判断details 是否一样
  698. $checkDetailsFiled = array("ocean_milestone","air_milestone","ocean_ctnr_status",
  699. "ocean_atd_sub_etd","ocean_atd_sub_etd_unit","ocean_ata_sub_eta","ocean_ata_sub_eta_unit",
  700. "air_atd_sub_etd","air_atd_sub_etd_unit","air_ata_sub_eta","air_ata_sub_eta_unit",
  701. "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",
  702. "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");
  703. $details_flag = true;
  704. foreach($checkDetailsFiled as $filed){
  705. if($filed == "ocean_milestone" || $filed == "air_milestone" || $filed == "ocean_ctnr_status"){
  706. $postValue = utils::implode(";",$_POST[$filed]);
  707. }else{
  708. $postValue = !empty($_POST[$filed]) ? $_POST[$filed] : "";
  709. }
  710. if($postValue != $rule[$filed]){
  711. $details_flag = false;
  712. }
  713. }
  714. //判断frequency 是否一样
  715. $checkFrequencyFiled = array("frequency_type","daily_time","daily_time_zone",
  716. "weekly_week","weekly_time","weekly_time_zone","daily_time_zone");
  717. $frequency_flag = true;
  718. foreach($checkFrequencyFiled as $filed){
  719. $postValue = !empty($_POST[$filed]) ? $_POST[$filed] : "";
  720. if($postValue != $rule[$filed]){
  721. $frequency_flag = false;
  722. }
  723. }
  724. //判断通知方式是否一样
  725. $checkMethodFiled = array("method_by_email","method_by_message");
  726. $method_flag = true;
  727. foreach($checkMethodFiled as $filed){
  728. $postValue = !empty($_POST[$filed]) ? $_POST[$filed] : "";
  729. if($postValue != $rule[$filed]){
  730. $method_flag = false;
  731. }
  732. }
  733. //五个条件一样,不允许保存
  734. if($range_flag && $details_flag && $frequency_flag && $method_flag){
  735. $msg = "Unable to Save";
  736. continue;
  737. }
  738. //前三个重回,后面不重合,提示但允许保存
  739. if($range_flag && $details_flag){
  740. $msg = "Similar Rule Detected";
  741. continue;
  742. }
  743. }
  744. return $msg;
  745. }
  746. /**
  747. * 这里基准event 写死, 根据online查询页面的通用的来, 这里需提问确定
  748. */
  749. public function getEDICtnrEvent(){
  750. $event =array(array("event_name"=>"EE","description"=>"Empty Equipment Dispatched"),array("event_name"=>"I","description"=>"Gate in full for a booking"),array("event_name"=>"AE","description"=>"Container loaded on vessel"),
  751. array("event_name"=>"VD","description"=>"Vessel Departure"),array("event_name"=>"VA_RELAY","description"=>"Arrive Relay Port"),array("event_name"=>"UV_RELAY","description"=>"Unloaded at Relay Port"),
  752. array("event_name"=>"AE_RELAY","description"=>"Loaded at Relay Port"),array("event_name"=>"VD_RELAY","description"=>"Depart Relay Port"),array("event_name"=>"CU","description"=>"Carrier and Customs Release"),
  753. array("event_name"=>"CT","description"=>"Customs release"),array("event_name"=>"CR","description"=>"Carrier release"),array("event_name"=>"VA","description"=>"Vessel Arrival"),
  754. array("event_name"=>"UV","description"=>"Unloaded From Vessel"),array("event_name"=>"AG","description"=>"Estimated Delivery"),array("event_name"=>"OA","description"=>"Gate out full from final discharge port"),
  755. array("event_name"=>"FT","description"=>"Free Time Expired"),array("event_name"=>"AL","description"=>"Container loaded on Rail"),array("event_name"=>"AR","description"=>"Container unloaded from Rail"),
  756. array("event_name"=>"AV","description"=>"Shipment available for pickup or delivery"),array("event_name"=>"D","description"=>"Gate out for delivery to customer"),array("event_name"=>"RD","description"=>"Container returned empty"),
  757. array("event_name"=>"C","description"=>"Vessel Estimated Time of Departure"),array("event_name"=>"C_RELAY","description"=>"Estimated Time of Departure from Tranship Port"),array("event_name"=>"AG_DES","description"=>"Estimated Delivery Destination"),
  758. array("event_name"=>"IFFADW","description"=>"Shipment in CFS warehouse"),array("event_name"=>"IFFDDW","description"=>"Shipment Out from CFS House"));
  759. return $event;
  760. }
  761. public function getNotifications($rules_type){
  762. //$all_rules = array("Milestone_Update","Container_Status_Update","Departure/Arrival_Delay","ETD/ETA_Change");
  763. if($rules_type == "Milestone_Update"){
  764. $sql = "select case when COALESCE(ni.milestone_code,'') <> '' and ni.milestone_transport_mode = 'sea'
  765. then (select description from public.customer_service_milestone_sno where type = 'sea' and code = ni.milestone_code)
  766. else (select description from public.customer_service_milestone_sno where type = 'air' and code = ni.milestone_code)
  767. end as milestone_desc,
  768. case when ni.milestone_code in ('IFFBCF','IFFBCR','IFFSIL','IFFEDO','IFFECC')
  769. then ''
  770. when ni.milestone_code in ('IFFREC','IFFECP')
  771. then ''
  772. ni.milestone_date,ni.milestone_time,
  773. (select o.h_bol from public.kln_ocean o where o.serial_no = ni.serial_no limit 1)
  774. from public.kln_notifiation_info ni
  775. where lower(ni.user_login) = '"._getLoginName()."'
  776. and ni.rules_type = '".$rules_type."'
  777. and (ni.frequency_type = 'Instant'
  778. or (ni.frequency_type = 'Daily' and timezone(ni.daily_time_zone, NOW()::time) > ni.daily_time::time)
  779. or (ni.frequency_type = 'Weekly' and timezone(ni.weekly_time_zone, NOW()::time) > ni.weekly_time::time
  780. and ni.weekly_week ilike '%'|| EXTRACT(dow FROM timezone(ni.weekly_time_zone, NOW())) ||'%'))
  781. and ni.notifications_method = true and ni.is_send_message = false order by ni.insert_date desc";
  782. }
  783. }
  784. }
  785. ?>