destination_delivery.class.php 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  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 destination_delivery {
  11. private static $_destination_delivery;
  12. public static function getInstance() {
  13. if (!self::$_destination_delivery) {
  14. $c = __CLASS__;
  15. self::$_destination_delivery = new $c;
  16. }
  17. return self::$_destination_delivery;
  18. }
  19. /**
  20. * 关于destination_delivery 页面的所有的静态加载数据查询
  21. */
  22. public function destination_delivery_load(){
  23. $operate = utils::_get('operate');
  24. $operate = strtolower($operate);
  25. /**
  26. * select country
  27. */
  28. if ($operate == "country") {
  29. $term = $_POST['term'];
  30. $term = trim($term);
  31. $sql = "select DISTINCT category AS country_code from public.kerry_system_code
  32. where description='COUNTRY-STATION'
  33. and category ilike '" . common::check_input($term) . "%'";
  34. $sql .= " order by category";
  35. $rs = common::excuteListSql($sql);
  36. //前端要加上id 从1开始
  37. $retData = array();
  38. foreach($rs as $key => $val){
  39. if(!empty($val['country_code'])){
  40. $retData[] = array("value" =>$val['country_code'] ,"label"=>$val['country_code']);
  41. }
  42. }
  43. common::echo_json_encode(200,$retData);
  44. exit();
  45. }
  46. /**
  47. * select station 选择国家后再展示 station list数据
  48. */
  49. if ($operate == "station") {
  50. $country = common::check_input($_REQUEST['country']);
  51. $sql = "select code from public.kerry_system_code
  52. where description='COUNTRY-STATION'
  53. and category = '".$country."'
  54. and code NOT LIKE 'Online_%'";
  55. $rs = common::excuteListSql($sql);
  56. foreach($rs as $key => $val){
  57. if(!empty($val['code'])){
  58. $retData[] = array("value" =>$val['code'] ,"label"=>$val['code']);
  59. }
  60. }
  61. common::echo_json_encode(200,$retData);
  62. exit();
  63. }
  64. /**
  65. * select KLN PIC 下拉可供选择的employee帐号,且帐号权限站点包括选中的station ?
  66. */
  67. if ($operate == "employee_account") {
  68. $term = $_POST['term'];
  69. $term = trim($term);
  70. $station = $_REQUEST['station'];
  71. $more_param = common::getInNotInSqlForSearch(utils::implode(';',$station));
  72. $sql = "select e.email from public.ra_online_user r, employee e
  73. where r.employee_id=e.employee_id
  74. and r.online_active = true
  75. and r.user_type = 'Employee'
  76. and e.active = true
  77. and lower(e.contact_id) in ($more_param)
  78. and coalesce(e.email,'') <>''
  79. and e.email ilike '" . common::check_input($term) . "%' limit 20";
  80. // $sql = "select email from public.ra_online_user where user_type = 'Employee' and lower(contact_id) in ($more_param)
  81. // and email ilike '" . common::check_input($term) . "%' limit 20";
  82. $rs = common::excuteListSql($sql);
  83. $retData = array();
  84. foreach($rs as $key => $val){
  85. if(!empty($val['email'])){
  86. $retData[] = array("value" =>$val['email'] ,"label"=>$val['email']);
  87. }
  88. }
  89. common::echo_json_encode(200,$retData);
  90. exit();
  91. }
  92. /**
  93. * 下拉可供选择的ports
  94. */
  95. if ($operate == "ports") {
  96. $mode = $_POST['mode'];
  97. $term = $_POST['term'];
  98. $term = trim($term);
  99. if ($mode == 'sea'){
  100. $sql = "select DISTINCT uncode from public.ports
  101. where coalesce(uncode,'')<> '' and uncode ilike '" . common::check_input($term) . "%'";
  102. } else {
  103. $sql = "select DISTINCT coalesce(airport.country_abb,'')||airport.airport_code as uncode from sfs.airport
  104. where coalesce(country_abb,'')<>''
  105. and coalesce(airport.country_abb,'')||airport.airport_code ilike '" . common::check_input($term) . "%'";
  106. }
  107. $sql .= " order by uncode limit 20";
  108. $rs = common::excuteListSql($sql);
  109. //前端要加上id 从1开始
  110. $retData = array();
  111. foreach($rs as $key => $val){
  112. if(!empty($val['uncode'])){
  113. $retData[] = array("value" =>$val['uncode'] ,"label"=>$val['uncode'],"checked"=>false);
  114. }
  115. }
  116. common::echo_json_encode(200,$retData);
  117. exit();
  118. }
  119. /**
  120. * 下拉可供选择的carrier
  121. */
  122. if ($operate == "carrier") {
  123. $term = $_POST['term'];
  124. $term = trim($term);
  125. $sql = "select DISTINCT scac from public.vessel_company
  126. where coalesce(scac,'')<> '' and scac ilike '" . common::check_input($term) . "%'";
  127. $sql .= " order by scac limit 20";
  128. $rs = common::excuteListSql($sql);
  129. //前端要加上id 从1开始
  130. $retData = array();
  131. foreach($rs as $key => $val){
  132. if(!empty($val['scac'])){
  133. $retData[] = array("value" =>$val['scac'] ,"label"=>$val['scac'],"checked"=>false);
  134. }
  135. }
  136. common::echo_json_encode(200,$retData);
  137. exit();
  138. }
  139. /**
  140. * Manage Address load
  141. */
  142. if ($operate == "manage_address"){
  143. $consignee = $_REQUEST['consignee_id'];
  144. //$consignee =array('COMGEN38002','FGB050132','ATOZTI35005','WILSON80001');
  145. $more_param = common::getInNotInSqlForSearch(utils::implode(';',$consignee));
  146. $country = $_REQUEST['country'];
  147. //$country =array('GB','DE','US');
  148. $more_param_country = common::getInNotInSqlForSearch(utils::implode(';',$country));
  149. $delivery_serial_no = common::deCode($_REQUEST['delivery_serial_no'], 'D');
  150. //$delivery_serial_no = 'c4d2951f438a4c9fccde37f26614740a';
  151. if(!empty($delivery_serial_no)){
  152. $sql = "SELECT addr1,addr2,addr3,addr4,
  153. ctry_code,city_code,postal_code,
  154. contact_person,contact_number,
  155. create_user,contact_id,sync_key,
  156. from_station,
  157. 'from Ksmart' as op_action
  158. FROM public.contacts_address ca
  159. WHERE addr_type = 'D'
  160. and lower(ca.contact_id) in (".$more_param.")
  161. and ca.from_station in (select code from public.kerry_system_code where lower(category) in (".$more_param_country."))
  162. and ca.sync_key not in (select sync_key from public.contacts_address_online where delivery_serial_no = '".$delivery_serial_no."')
  163. union all
  164. SELECT addr1,addr2,addr3,addr4,
  165. ctry_code,city_code,postal_code,
  166. contact_person,contact_number,
  167. create_user,contact_id,sync_key,
  168. from_station,
  169. action as op_action
  170. FROM public.contacts_address_online
  171. where delivery_serial_no = '$delivery_serial_no'";
  172. }else{
  173. $sql = "SELECT addr1,addr2,addr3,addr4,
  174. ctry_code,city_code,postal_code,
  175. contact_person,contact_number,
  176. create_user,contact_id,sync_key,
  177. from_station,
  178. 'from Ksmart' as op_action
  179. FROM public.contacts_address ca
  180. WHERE addr_type = 'D'
  181. and lower(ca.contact_id) in (".$more_param.")
  182. and ca.from_station in (select code from public.kerry_system_code where lower(category) in (".$more_param_country."))";
  183. }
  184. $data = common::excuteListSql($sql);
  185. error_log($sql);
  186. $manageAddressList = array();
  187. //Online_D_Address
  188. foreach($data as $d){
  189. if ($d['op_action'] == "from Ksmart" && $d['create_user'] <> "Online_D_Address"){
  190. $address_remark = "read-only";
  191. } elseif ($d['op_action'] == "from Ksmart" && $d['create_user'] == "Online_D_Address"){
  192. $address_remark = "reviewed";
  193. } elseif ($d['op_action'] == "Add" && $d['create_user'] == "Online_D_Address"){
  194. $address_remark = "unreviewed";
  195. } elseif ($d['op_action'] == "Modify" && $d['create_user'] == "Online_D_Address"){
  196. $address_remark = "unreviewed";
  197. } else {
  198. $address_remark = "deletion";
  199. }
  200. $manageAddressList[] = array(
  201. "address_1"=>$d['addr1'],
  202. "address_2"=>$d['addr2'],
  203. "address_3"=>$d['addr3'],
  204. "address_4"=>$d['addr4'],
  205. "country"=>$d['ctry_code'],
  206. "city"=>$d['city_code'],
  207. "postal_code"=>$d['postal_code'],
  208. "contact_person"=>$d['contact_person'] === null ? '' : $d['contact_person'],
  209. "contact_number" =>$d['contact_number'] === null ? '' : $d['contact_number'],
  210. "create_user" => $d['create_user'], //create_user = Online_D_Address 时,代表對客戶賬號類型自己創建的D類地址
  211. "contact_id" => $d['contact_id'],
  212. "sync_key" => $d['sync_key'], //唯一key
  213. "from_station" => $d['from_station'], //只有是客户新键的才会特殊处理station Online_GE. 好判断是那个国家的
  214. "contact_type" => "Unedit", //这个前端控制的
  215. "op_action" => $d['op_action'],
  216. "address_remark" => $address_remark
  217. );
  218. }
  219. common::echo_json_encode(200,$manageAddressList);
  220. exit();
  221. }
  222. /**
  223. * country and city load
  224. */
  225. if ($operate == "country_city_load") {
  226. $term_type = $_POST['term_type'];
  227. $limit = $_POST['limit'];
  228. $term = $_POST['term'];
  229. $term = trim($term);
  230. $extend_sql_where = "";
  231. if ($term_type == "country" && !empty($limit)){
  232. $extend_sql_where = " and location_code = '$limit'";
  233. }
  234. if ($term_type == "city" && !empty($limit)){
  235. $extend_sql_where = " and country_code = '$limit'";
  236. }
  237. if ($term_type == "country"){
  238. $sql = "select DISTINCT country_code AS code from public.unlocode
  239. where country_code ilike '" . common::check_input($term) . "%'".$extend_sql_where;
  240. $sql .= " order by country_code limit 20";
  241. }
  242. if ($term_type == "city"){
  243. $sql = "select DISTINCT location_code AS code from public.unlocode
  244. where location_code ilike '" . common::check_input($term) . "%'".$extend_sql_where;
  245. $sql .= " order by location_code limit 20";
  246. }
  247. $rs = common::excuteListSql($sql);
  248. //前端要加上id 从1开始
  249. $retData = array();
  250. foreach($rs as $key => $val){
  251. if(!empty($val['code'])){
  252. $retData[] = array("value" =>$val['code'] ,"label"=>$val['code']);
  253. }
  254. }
  255. common::echo_json_encode(200,$retData);
  256. exit();
  257. }
  258. }
  259. /**
  260. * destination_delivery 配置
  261. */
  262. public function destination_delivery_config(){
  263. $operate = utils::_get('operate');
  264. $operate = strtolower($operate);
  265. /**
  266. * 这个是嵌套在destination_delivery的配置查询,
  267. */
  268. if ($operate == "search"){
  269. $cp = common::check_input($_POST ['cp']); //current_page
  270. $ps = common::check_input($_POST ['ps']); //ps
  271. if (empty($ps))
  272. $ps = 100;
  273. if (empty($cp))
  274. $cp = 1;
  275. $rc = $_POST ['rc'];
  276. if ($rc == - 1) {
  277. $sql = "select count(*) from public.kln_destination_delivery_config";
  278. $rc = common::excuteOneSql($sql);
  279. }
  280. $tp = ceil($rc / $ps);
  281. if ($rc > 0) {
  282. $sql = "select * from public.kln_destination_delivery_config";
  283. $sql .= " order by id desc limit " . $ps . " offset " . ($cp - 1) * $ps;
  284. $rs = common::excuteListSql($sql);
  285. foreach($rs as $key => $val){
  286. $rs[$key]["_serial_no"] = common::deCode($val['serial_no'], 'E');
  287. }
  288. $arrTmp = array('searchData' => $rs,
  289. 'rc' => intval($rc),
  290. 'ps' => intval($ps),
  291. 'cp' => intval($cp),
  292. 'tp' => intval($tp));
  293. common::echo_json_encode(200,$arrTmp);
  294. exit();
  295. }else{
  296. $arrTmp = array('searchData' => array());
  297. common::echo_json_encode(200, $arrTmp);
  298. exit();
  299. }
  300. }
  301. if ($operate == "add"){
  302. $serial_no = common::deCode($_REQUEST['a'], 'D');
  303. $data = array();
  304. $rule = array();
  305. if (!empty($serial_no)){
  306. $sql = "select * from public.kln_destination_delivery_config where serial_no = '$serial_no'";
  307. $data = common::excuteObjectSql($sql);
  308. if ($data['recommended_delivery'] == 'Delivery_ETA_ATA') {
  309. $sql = "select * from public.kln_destination_delivery_rule where recommended_delivery_serial_no = '$serial_no'";
  310. $rule = common::excuteListSql($sql);
  311. }
  312. } else{
  313. //代表新加,页面初始
  314. $data['booking_window'] = "No_Restrictions";
  315. $data['booking_window_date_start'] ="";
  316. $data['booking_window_date_end'] ="";
  317. $data['recommended_delivery'] ="No_Recommended";
  318. }
  319. //处理组装返回格式
  320. //RcommendDeliveryDate
  321. $RecommendCheckedAirList = array();
  322. $RecommendCheckedSeaList = array();
  323. $RecommendCheckedList = array();
  324. foreach($rule as $item){
  325. if ($item['mode_type'] == 'sea'){
  326. $RecommendCheckedSeaList[] = array("priority"=>$item['priority'],"rule_type"=>$item['rule_type'],
  327. "ports"=>explode(",", $item['ports']),
  328. "carrier"=>explode(",", $item['carrier']),
  329. "recommended_delivery_from"=>$item['recommended_delivery_from'],
  330. "recommended_delivery_to"=>$item['recommended_delivery_to'],
  331. "mode_type"=>$item['mode_type']);
  332. }
  333. if ($item['mode_type'] == 'air'){
  334. $RecommendCheckedAirList[] = array("priority"=>$item['priority'],"rule_type"=>$item['rule_type'],
  335. "ports"=>explode(",", $item['ports']),
  336. "carrier"=>explode(",", $item['carrier']),
  337. "recommended_delivery_from"=>$item['recommended_delivery_from'],
  338. "recommended_delivery_to"=>$item['recommended_delivery_to'],
  339. "mode_type"=>$item['mode_type']);
  340. }
  341. if (!utils::in_array(ucfirst($item['mode_type']), $RecommendCheckedList)) {
  342. $RecommendCheckedList[] = ucfirst($item['mode_type']);
  343. }
  344. }
  345. $returnData = $data;
  346. $returnData["SetBookingWindow"] = array("windowradio" =>common::destination_delivery_window_radio($data['booking_window']),
  347. "windowBeforeDays"=>$data['booking_window_date_start'],
  348. "windowAfterDays"=>$data['booking_window_date_end']);
  349. $returnData["RcommendDeliveryDate"] = array("Recommendradio" =>common::destination_delivery_recommended($data['recommended_delivery']),
  350. "RecommendCheckedList"=>$RecommendCheckedList,
  351. "RecommendCheckedAirList"=>$RecommendCheckedAirList,
  352. "RecommendCheckedSeaList"=>$RecommendCheckedSeaList);
  353. $returnData["KLNPLCvalue"] = empty($data['kln_pic']) ? array() : explode(";", $data['kln_pic']);
  354. $CountryCheckedList = array();
  355. $sql = "select crm.overeas from public.contract_region_mapping crm
  356. left join ocean.contacts c on c.contact_id = crm.overeas and crm.ksmart_station_new ilike 'ksmart%'
  357. where SUBSTRING(c.country FROM 1 FOR 2) = '".$data['country']."'";
  358. $rs = common::excuteListSql($sql);
  359. foreach($rs as $key => $val){
  360. if(!empty($val['overeas'])){
  361. $CountryCheckedList[] = array("value" =>$val['overeas'] ,"label"=>$val['overeas']);
  362. }
  363. }
  364. $returnData["CountryCheckedList"] = $CountryCheckedList;
  365. $returnData["station"] = explode(",", $returnData["station"]);
  366. $data = array('returnData' =>$returnData);
  367. common::echo_json_encode(200,$data);
  368. exit();
  369. }
  370. if ($operate == "save"){
  371. $serial_no = common::deCode($_POST['serial_no'], 'D');
  372. $country = common::check_input($_POST['country']);
  373. $station = empty($_POST['station']) ? "" : utils::implode(",",$_POST['station']);
  374. $booking_window = common::check_input($_POST['booking_window']);
  375. $booking_window_date_start = common::check_input($_POST['booking_window_date_start']);
  376. $booking_window_date_end = common::check_input($_POST['booking_window_date_end']);
  377. $recommended_delivery = common::check_input($_POST['recommended_delivery']);
  378. $kln_pic = common::check_input($_POST['kln_pic']);
  379. $booking_window_desc = common::check_input($_POST['booking_window_desc']);
  380. $recommended_delivery_date_desc = common::check_input($_POST['recommended_delivery_date_desc']);
  381. //rule
  382. $_mode_type = $_POST['mode_type'];
  383. $_priority = $_POST['priority'];
  384. $_rule_type = $_POST['rule_type'];
  385. $_ports = $_POST['ports'];
  386. $_carrier = $_POST['carrier'];
  387. $_recommended_delivery_from = $_POST['recommended_delivery_from'];
  388. $_recommended_delivery_to = $_POST['recommended_delivery_to'];
  389. $sql = "";
  390. if (!empty($serial_no)){
  391. $updateSqlSet = " modify_by = '"._getLoginName()."',update_time = now()";
  392. if (!empty($country)) {
  393. $updateSqlSet.= ", country = '$country' ";
  394. }
  395. if (!empty($station)) {
  396. $updateSqlSet.= ", station = '$station' ";
  397. }
  398. if (!empty($booking_window)) {
  399. $updateSqlSet.= ", booking_window = '$booking_window' ";
  400. }
  401. if (!empty($booking_window_date_start)) {
  402. $updateSqlSet.= ", booking_window_date_start = '$booking_window_date_start' ";
  403. }
  404. if (!empty($booking_window_date_end)) {
  405. $updateSqlSet.= ", booking_window_date_end = '$booking_window_date_end' ";
  406. }
  407. if (!empty($recommended_delivery)) {
  408. $updateSqlSet.= ", recommended_delivery = '$recommended_delivery' ";
  409. }
  410. if (!empty($kln_pic)) {
  411. $updateSqlSet.= ", kln_pic = '$kln_pic' ";
  412. }
  413. if (!empty($booking_window_desc)) {
  414. $updateSqlSet.= ", booking_window_desc = '$booking_window_desc' ";
  415. }
  416. if (!empty($recommended_delivery_date_desc)) {
  417. $updateSqlSet.= ", recommended_delivery_date_desc = '$recommended_delivery_date_desc' ";
  418. }
  419. //代表update
  420. $sql .= "update public.kln_destination_delivery_config set ".$updateSqlSet."
  421. where serial_no = '$serial_no';";
  422. } else {
  423. //检查配置的站点是否于以前的配置的,是否重合
  424. $exist = common::excuteListSql("select country,station from public.kln_destination_delivery_config WHERE string_to_array('$station', ',') && string_to_array(station, ',')");
  425. if(!empty($exist)){
  426. $data = array("msg" =>"The station has been reconfigured. Please check ".$exist[0]['country'] ." - " .$exist[0]['station']);
  427. common::echo_json_encode(200,$data);
  428. exit();
  429. }
  430. //代表更新
  431. $serial_no = common::uuid();
  432. $sql .="INSERT INTO public.kln_destination_delivery_config(
  433. serial_no, country, station, booking_window, booking_window_date_start,
  434. booking_window_date_end, recommended_delivery, kln_pic, booking_window_desc,recommended_delivery_date_desc,
  435. create_by, created_time, modify_by, update_time)
  436. VALUES ('$serial_no', '$country', '$station', '$booking_window', '$booking_window_date_start',
  437. '$booking_window_date_end', '$recommended_delivery', '$kln_pic','$booking_window_desc','$recommended_delivery_date_desc',
  438. '"._getLoginName()."', now(), '"._getLoginName()."', now());";
  439. }
  440. $errmsg = "";
  441. //柜子是先删除,后添加
  442. $sql .= "delete from public.kln_destination_delivery_rule where recommended_delivery_serial_no = '$serial_no';";
  443. if ($recommended_delivery == "Delivery_ETA_ATA"){
  444. // 用于统计每种类型是否存在,以及是否满足 rule_type = 'all'
  445. $typeExists = ['air' => false, 'sea' => false];
  446. $typeHasAllRule = ['air' => false, 'sea' => false];
  447. //检查判断规则不能重合
  448. $seaRule = [];
  449. $airRule = [];
  450. foreach($_mode_type as $key => $v){
  451. $mode_type = $v;
  452. $priority = $_priority[$key];
  453. $rule_type = $_rule_type[$key];
  454. $ports = $_ports[$key];
  455. $carrier = $_carrier[$key];
  456. if( $mode_type == 'sea'){
  457. $seaRule[] = explode(",",$_ports[$key]);
  458. ///$sea_carrier_index += 1;
  459. } else {
  460. $airRule[] = explode(",",$_ports[$key]);
  461. }
  462. $recommended_delivery_from = $_recommended_delivery_from[$key];
  463. $recommended_delivery_to = $_recommended_delivery_to[$key];
  464. $sql .= "INSERT INTO public.kln_destination_delivery_rule(
  465. recommended_delivery_serial_no, mode_type, priority,
  466. rule_type, ports, carrier, recommended_delivery_from, recommended_delivery_to,
  467. create_by, created_time, modify_by, update_time)
  468. VALUES ('$serial_no', '$mode_type', '$priority',
  469. '$rule_type', '$ports', '$carrier', '$recommended_delivery_from', '$recommended_delivery_to',
  470. '"._getLoginName()."', now(), '"._getLoginName()."', now());";
  471. $typeExists[$mode_type] = true;
  472. if ($rule_type == '*Default Rule') {
  473. $typeHasAllRule[$mode_type] = true;
  474. }
  475. }
  476. // 检查缺失的 rule_type = 'all'
  477. $missingTypes = [];
  478. foreach (['air', 'sea'] as $type) {
  479. if ($typeExists[$type] && !$typeHasAllRule[$type]) {
  480. $missingTypes[] = $type;
  481. }
  482. }
  483. if (!empty($missingTypes)) {
  484. $errmsg = "The Default Rule must exist.";
  485. }
  486. //检查规则不能重复
  487. $errmsg .= common::checkOverlap($seaRule,"Sea Rule");
  488. $errmsg .= common::checkOverlap($airRule,"Air Rule");
  489. }
  490. if (empty($errmsg) && !empty($sql)){
  491. common::excuteUpdateSql($sql);
  492. $data = array("msg" =>"success");
  493. } else {
  494. $data = array("msg" =>$errmsg);
  495. }
  496. common::echo_json_encode(200,$data);
  497. exit();
  498. }
  499. if ($operate == "delete"){
  500. $serial_no = common::deCode($_POST['a'], 'D');
  501. $sql = "delete from public.kln_destination_delivery_config where serial_no = '$serial_no';";
  502. $sql .= "delete from public.kln_destination_delivery_rule where recommended_delivery_serial_no = '$serial_no';";
  503. error_log($sql);
  504. common::excuteUpdateSql($sql);
  505. $data = array("msg" =>"success");
  506. common::echo_json_encode(200,$data);
  507. exit();
  508. }
  509. }
  510. /**
  511. * booking
  512. */
  513. public function destination_delivery_booking(){
  514. $operate = utils::_get('operate');
  515. $operate = strtolower($operate);
  516. if ($operate == "destination_delivery_search") {
  517. //search
  518. //栏位信息
  519. $column = column::getInstance()->getDisplayColumn('Destination_Delivery_Search');
  520. $BookingTableColumns = column::getInstance()->tableColumns('Destination_Delivery_Search',$column);
  521. $data['TrackingTableColumns'] = $BookingTableColumns;
  522. common::echo_json_encode(200,$data);
  523. exit();
  524. }
  525. if ($operate == "destination_delivery_shipment_search") {
  526. //search
  527. //栏位信息
  528. $column = column::getInstance()->getDisplayColumn('destination_delivery_shipment_search');
  529. $BookingTableColumns = column::getInstance()->tableColumns('destination_delivery_shipment_search',$column);
  530. $data['TrackingTableColumns'] = $BookingTableColumns;
  531. common::echo_json_encode(200,$data);
  532. exit();
  533. }
  534. /**
  535. * select shipment,
  536. * Pending Approval Approve Reject Cancel Modify Submit
  537. */
  538. if ($operate == "search"){
  539. $cp = common::check_input($_POST ['cp']); //current_page
  540. $ps = common::check_input($_POST ['ps']); //ps
  541. if (empty($ps))
  542. $ps = 100;
  543. if (empty($cp))
  544. $cp = 1;
  545. $sqlWhere = ' where ' . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  546. //兜底规则
  547. $sqlWhere .= " and eta >= now() - INTERVAL '6 months' and eta <= now() + INTERVAL '6 months'";
  548. //代表employee
  549. $sqlDeliveryWhere = " 1=1";
  550. if(_isApexLogin()){
  551. //$sqlWhere .= " and kln_pic ilike '"._getLoginEamil()."'";
  552. $sqlDeliveryWhere .= " and '".strtolower(_getLoginEamil())."' in (select regexp_split_to_table(lower(kd.kln_pic), ','))";
  553. }
  554. //条件带入
  555. $text_search = $_REQUEST['text_search'];
  556. if (!empty($text_search)){
  557. $sqlWhere .= " and (booking_no ilike '%".strtolower($text_search)."%'
  558. or h_bol ilike '%".strtolower($text_search)."%'
  559. or m_bol ilike '%".strtolower($text_search)."%'
  560. or ctnr ilike '%".strtolower($text_search)."%'
  561. or consignee ilike '%".strtolower($text_search)."%')";
  562. }
  563. if (isset($_REQUEST['delivery_date_start']) && !empty($_REQUEST['delivery_date_start']))
  564. $sqlDeliveryWhere .= " and kd.delivery_date >= '" . common::usDate2sqlDate($_REQUEST['delivery_date_start']) . " 00:00:00'";
  565. if (isset($_REQUEST['delivery_date_end']) && !empty($_REQUEST['delivery_date_end']))
  566. $sqlDeliveryWhere .= " and kd.delivery_date <= '" . common::usDate2sqlDate($_REQUEST['delivery_date_end']) . " 23:59:59'";
  567. if(!empty($_POST['delivery_mode'])){
  568. $sqlDeliveryWhere .= " and kd.delivery_mode ='". common::check_input($_REQUEST['delivery_mode'])."'";
  569. }
  570. if (isset($_REQUEST['created_time_start']) && !empty($_REQUEST['created_time_start']))
  571. $sqlDeliveryWhere .= " and kd.created_time >= '" . common::usDate2sqlDate($_REQUEST['created_time_start']) . " 00:00:00'";
  572. if (isset($_REQUEST['created_time_end']) && !empty($_REQUEST['created_time_end']))
  573. $sqlDeliveryWhere .= " and kd.created_time <= '" . common::usDate2sqlDate($_REQUEST['created_time_end']) . " 23:59:59'";
  574. //移除filterTag
  575. //$sqlWhere_befrom_filterTag = $sqlWhere;
  576. $filterTag_param = "";
  577. if (!empty($_POST["filterTag"])) {
  578. if (utils::count($_POST['filterTag']) == 1){
  579. $filterTag = $_POST['filterTag'][0];
  580. }else{
  581. $filterTag = utils::implode(",", $_POST['filterTag']);
  582. }
  583. $filterTag_param = "1<>1";
  584. if(strtolower($filterTag) == "all"){
  585. $filterTag_param = "1=1";
  586. }
  587. if (stripos($filterTag, "Pending Approval") !== FALSE) {
  588. $filterTag_param .= " or (kd.status = 'Pending Approval')";
  589. }
  590. if (stripos($filterTag, "Approve") !== FALSE) {
  591. $filterTag_param .= " or (kd.status = 'Approve')";
  592. }
  593. if (stripos($filterTag, "Reject") !== FALSE) {
  594. $filterTag_param .= " or (kd.status = 'Reject')";
  595. }
  596. if (stripos($filterTag, "Cancel") !== FALSE) {
  597. $filterTag_param .= " or (kd.status = 'Cancel')";
  598. }
  599. if(strtolower($filterTag) <> "all" && !empty($filterTag)){
  600. $filterTag_param = " ($filterTag_param)";
  601. }
  602. }
  603. if(empty($filterTag_param)){
  604. $filterTag_param = "1=1";
  605. }
  606. $tag_and_mode_param = $filterTag_param;
  607. $rc = $_POST ['rc'];
  608. if ($rc == - 1 || true) {
  609. $sql = "select count(1) as rc,
  610. sum(case when $tag_and_mode_param then 1 else 0 end) as seach_rc,
  611. sum(case when kd.status ='Pending Approval' then 1 else 0 end) as pending_approval_rc,
  612. sum(case when kd.status ='Approve' then 1 else 0 end) as approved_rc,
  613. sum(case when kd.status ='Reject' then 1 else 0 end) as rejected_rc,
  614. sum(case when kd.status ='Cancel' then 1 else 0 end) as cancelled_rc
  615. from public.kln_destination_delivery kd
  616. where ".$sqlDeliveryWhere."
  617. and exists(select 1 from public.kln_ocean oo ". $sqlWhere." and oo.serial_no = any(kd.h_serial_no))";
  618. error_log("kln_destination_delivery_count:".$sql);
  619. $statusRc = common::excuteObjectSql($sql);
  620. $rc = $statusRc['rc'];
  621. $search_rc = $statusRc['seach_rc'];
  622. $pending_approval_rc = $statusRc['pending_approval_rc'];
  623. $approved_rc = $statusRc['approved_rc'];
  624. $rejected_rc = $statusRc['rejected_rc'];
  625. $cancelled_rc = $statusRc['cancelled_rc'];
  626. }
  627. $tp = ceil($rc / $ps);
  628. if ($rc > 0) {
  629. $sql = "select *,to_char((recommended_delivery_window_date_from)::date, 'YYYY-MM-DD')
  630. || ';' ||
  631. to_char((recommended_delivery_window_date_to)::date, 'YYYY-MM-DD') AS date_range
  632. from public.kln_destination_delivery kd
  633. where ".$tag_and_mode_param."
  634. and ".$sqlDeliveryWhere."
  635. and exists(select 1 from public.kln_ocean oo ". $sqlWhere." and oo.serial_no = any(h_serial_no))";
  636. $sql .= " order by id desc limit " . $ps . " offset " . ($cp - 1) * $ps;
  637. error_log("kln_destination_delivery_search:".$sql);
  638. $rs = common::excuteListSql($sql);
  639. foreach($rs as $key => $val){
  640. $rs[$key]["_serial_no"] = common::deCode($val['serial_no'], 'E');
  641. $rs[$key]["status"] = common::deliveryStatusConvert($val['status']);
  642. $rs[$key]["h_bol"] = json_decode($val['h_bol_multiple_link']);
  643. }
  644. $arrTmp = array('searchData' => $rs,
  645. 'is_employee' => _isApexLogin(),
  646. 'All' =>$rc,
  647. 'pending_approval_rc' =>$pending_approval_rc,
  648. 'approved_rc' =>$approved_rc,
  649. 'rejected_rc' =>$rejected_rc,
  650. 'cancelled_rc' =>$cancelled_rc,
  651. 'rc' => $search_rc,
  652. 'ps' => $ps,
  653. 'cp' => $cp,
  654. 'tp' => $tp);
  655. common::echo_json_encode(200,$arrTmp);
  656. exit();
  657. }else{
  658. $arrTmp = array('searchData' => array(),
  659. 'is_employee' => _isApexLogin(),
  660. 'All' =>$rc,
  661. 'pending_approval_rc' =>$pending_approval_rc,
  662. 'approved_rc' =>$approved_rc,
  663. 'rejected_rc' =>$rejected_rc,
  664. 'cancelled_rc' =>$cancelled_rc,
  665. 'rc' => $search_rc,
  666. 'ps' => $ps,
  667. 'cp' => $cp,
  668. 'tp' => $tp);
  669. common::echo_json_encode(200,$arrTmp);
  670. exit();
  671. }
  672. }
  673. if ($operate == "search_shipment"){
  674. $data = $this->search_shipment();
  675. if(!empty($data['msg'])){
  676. $arrTmp = array("msg" =>$data['msg']);
  677. }else{
  678. $arrTmp = array("msg" =>"success","data"=>$data['data']);
  679. }
  680. common::echo_json_encode(200, $arrTmp);
  681. exit();
  682. }
  683. if ($operate == "add"){
  684. $serial_no = common::deCode($_POST['serial_no'], 'D');
  685. //前端按钮 后台权限拦截
  686. if(!empty($serial_no)){
  687. common::checkedActionLegal($serial_no,$operate);
  688. }else{
  689. if(!_isApexLogin()){
  690. //$data = array("msg" =>"Employees cannot create booking");
  691. //common::echo_json_encode(200,$data);
  692. //exit();
  693. }
  694. }
  695. $booking_data = array();
  696. if(!empty($serial_no)){
  697. //to_char(delivery_date, 'MM/DD/YYYY') AS _delivery_date,
  698. $sql = "select *,
  699. to_char(delivery_date, 'HH24:MI') AS _delivery_time
  700. from public.kln_destination_delivery where serial_no = '$serial_no'";
  701. $booking_data = common::excuteObjectSql($sql);
  702. $booking_data["delivery_time"] = $booking_data["_delivery_time"];
  703. $booking_data["delivery_address_detail"] = json_decode($booking_data["delivery_address_detail"],true);
  704. $shipments = $this->search_shipment_with_booking($booking_data['serial_no'],$booking_data['h_serial_no'],$booking_data['ctnr']);
  705. } else {
  706. $shipments = $this->search_shipment();
  707. }
  708. if(!empty($shipments['msg'])){
  709. $data = array("msg" =>$shipments['msg']);
  710. common::echo_json_encode(200,$data);
  711. exit();
  712. }
  713. $booking_data['tableData'] = $shipments['data'];
  714. $data = array("msg" =>"success","data"=>$booking_data);
  715. common::echo_json_encode(200,$data);
  716. exit();
  717. }
  718. if ($operate == "save"){
  719. $serial_no = common::deCode($_POST['serial_no'], 'D');
  720. $booking_no = $_POST['booking_no'];
  721. $manifest_type = $_POST['manifest_type'];
  722. $h_bol = $_POST['h_bol'];
  723. $h_serial_no = $_POST['serial_no'];
  724. $order_from = $_POST['order_from'];
  725. $m_bol = $_POST['m_bol'];
  726. $ctnr = $_POST['ctnr'];
  727. $kln_pic = $_POST['dc_kln_pic'];
  728. $consignee = $_POST['consignee'];
  729. $consignee_id = $_POST['consignee_id'];
  730. $dc_country = $_POST['dc_country'];
  731. $recommended_delivery_window_date_from = $_POST['recommended_delivery_from_date'];
  732. $recommended_delivery_window_date_to = $_POST['recommended_delivery_to_date'];
  733. $recommended_delivery_from = $_POST['recommended_delivery_from'];
  734. $recommended_delivery_to = $_POST['recommended_delivery_to'];
  735. $modify_reason = common::check_input($_POST['modify_reason']);
  736. $delivery_date = $_POST['delivery_date'];
  737. $delivery_time = $_POST['delivery_time'];
  738. $delivery_mode = common::check_input($_POST['delivery_mode']);
  739. //这status 编辑只有两种情况,新加,和在拒绝后 用户重新编辑 状态都是Pending Approval
  740. $status = "Pending Approval";
  741. $is_new = false;
  742. if(empty($serial_no)){
  743. $is_new = true;
  744. }
  745. $delivery_address = common::check_input($_POST['delivery_address']);
  746. $special_requirements = common::check_input($_POST['special_requirements']);
  747. //检查不能重复添加booking 这里空运没问题,但是会遇到相同hbol 不同的柜子情况,本质上来说就是一条
  748. //$sqlWhere = ' where ' . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  749. //兜底规则
  750. //$sqlWhere .= " and eta >= now() - INTERVAL '6 months' and eta <= now() + INTERVAL '6 months'";
  751. // $ckeckedSql = "with save_table as (select regexp_split_to_table('".utils::implode(',',$h_serial_no)."', ',') as serial_no)
  752. // select string_agg(h_bol, ';') as h_bol from public.kln_destination_delivery kde
  753. // where exists(select 1 from save_table where save_table.serial_no = any(kde.h_serial_no))
  754. // and exists(select 1 from public.kln_ocean oo ". $sqlWhere." and oo.serial_no = any(kde.h_serial_no))";
  755. // //代表编辑
  756. // if(!empty($serial_no)){
  757. // $ckeckedSql .=" and kde.serial_no <> '$serial_no'";
  758. // }
  759. // $checkeData = common::excuteObjectSql($ckeckedSql);
  760. // if(!empty($checkeData['h_bol'])){
  761. // $data = array("msg" =>"The ".$checkeData['h_bol'] ." already exists");
  762. // common::echo_json_encode(200,$data);
  763. // exit();
  764. // }
  765. $sql = "";
  766. //代表修改
  767. if(!empty($serial_no)){
  768. $updateSqlSet = " modify_by = '"._getLoginName()."',update_time = now()";
  769. if (!empty($status)) {
  770. $updateSqlSet.= ", status = '$status' ";
  771. }
  772. if (!empty($delivery_address)) {
  773. $delivery_address_detail = common::check_input(json_encode(common::returnDAddress(),JSON_UNESCAPED_UNICODE));
  774. $updateSqlSet.= ", delivery_address = '$delivery_address' ";
  775. $updateSqlSet.= ", delivery_address_detail = '$delivery_address_detail' ";
  776. }
  777. if (!empty($special_requirements)) {
  778. $updateSqlSet.= ", special_requirements = '$special_requirements' ";
  779. }
  780. if (!empty($modify_reason)) {
  781. $updateSqlSet.= ", modify_reason = '$modify_reason' ";
  782. }
  783. if (!empty($delivery_date)) {
  784. $delivery_date = empty($_POST['delivery_date']) ? "null": "'".common::usDate2sqlDate($_POST['delivery_date'])." ".$delivery_time."'";
  785. $updateSqlSet.= ", delivery_date = $delivery_date";
  786. }
  787. //修改记录临时表
  788. common::saveDAddressTempTable($serial_no,$consignee_id);
  789. //代表update
  790. $sql .= "update public.kln_destination_delivery set ".$updateSqlSet."
  791. where serial_no = '$serial_no';";
  792. //记录log
  793. $sql .="INSERT INTO public.kln_destination_delivery_operation_log(
  794. serial_no, action,notes, create_by, created_time, created_zone)
  795. VALUES ('$serial_no', 'Submit','".$modify_reason."', '"._getLoginName()."', now(), ''); ";
  796. } else {
  797. $saveData = $this->groupShipments($booking_no,$manifest_type,$h_bol,$h_serial_no,$order_from,$m_bol,$ctnr,$kln_pic,$consignee,$consignee_id,
  798. $recommended_delivery_window_date_from, $recommended_delivery_window_date_to,
  799. $recommended_delivery_from,$recommended_delivery_to,$dc_country);
  800. foreach($saveData as $sData){
  801. $serial_no = common::uuid();
  802. // 使用 PostgreSQL 的 ARRAY 语法
  803. $tags_sql = "ARRAY['" . implode("','", array_map('pg_escape_string', $sData['h_serial_no'])) . "']";
  804. // 使用 PostgreSQL 的 ARRAY 语法
  805. $country_sql = "ARRAY['" . implode("','", array_map('pg_escape_string', $sData['dc_country'])) . "']";
  806. $recommended_delivery_window_date_from = "";
  807. if(empty($sData['recommended_delivery_window_date_from'])){
  808. $recommended_delivery_window_date_from = "null";
  809. } else {
  810. $recommended_delivery_window_date_from = "'".$sData['recommended_delivery_window_date_from']."'";
  811. }
  812. $recommended_delivery_window_date_to = "";
  813. if(empty($sData['recommended_delivery_window_date_to'])){
  814. $recommended_delivery_window_date_to = "null";
  815. } else {
  816. $recommended_delivery_window_date_to = "'".$sData['recommended_delivery_window_date_to']."'";
  817. }
  818. $delivery_date = empty($_POST['delivery_date']) ? "null": "'".common::usDate2sqlDate($_POST['delivery_date'])." ".$delivery_time."'";
  819. //修改记录临时表,有值代表选择的add
  820. $_sync_key_add = common::saveDAddressTempTable($serial_no,$sData['consignee_id']);
  821. $delivery_address_detail = common::check_input(json_encode(common::returnDAddress($_sync_key_add),JSON_UNESCAPED_UNICODE));
  822. $sql .= "INSERT INTO public.kln_destination_delivery(
  823. serial_no, h_serial_no,address_country,
  824. booking_no, h_bol,h_bol_multiple_link,
  825. m_bol, ctnr,
  826. consignee, delivery_date, delivery_mode, status, delivery_address,delivery_address_detail, special_requirements,
  827. recommended_delivery_window_date_from, recommended_delivery_window_date_to, d_address_change_log,
  828. recommended_delivery_from, recommended_delivery_to,kln_pic,
  829. create_by, created_time, modify_by, update_time)
  830. VALUES ('$serial_no', ".$tags_sql.",".$country_sql.",
  831. '".$sData['booking_no']."', '".utils::implode(',',$sData['h_bol'])."','".common::check_input(json_encode($sData['h_bol_multiple_link']))."',
  832. '".utils::implode(',',$sData['m_bol'])."', '".utils::implode(',',$sData['ctnr'])."',
  833. '".$sData['consignee']."', $delivery_date,'$delivery_mode','$status', '$delivery_address','$delivery_address_detail', '$special_requirements',
  834. $recommended_delivery_window_date_from, $recommended_delivery_window_date_to,'',
  835. '".$sData['recommended_delivery_from']."', '".$sData['recommended_delivery_to']."','".$sData['kln_pic']."',
  836. '"._getLoginName()."', now(), '"._getLoginName()."', now());";
  837. //记录log
  838. $sql .="INSERT INTO public.kln_destination_delivery_operation_log(
  839. serial_no, action,notes, create_by, created_time, created_zone)
  840. VALUES ('$serial_no', 'Submit','', '"._getLoginName()."', now(), ''); ";
  841. }
  842. }
  843. if(!empty($sql)){
  844. $rs = common::excuteUpdateSql($sql);
  845. if ($rs === FALSE){
  846. $data = array("msg" =>"error");
  847. common::echo_json_encode(200,$data);
  848. exit();
  849. }
  850. }
  851. if(!$is_new){
  852. //状态变更时,发送邮件提醒
  853. $status = "Modify";
  854. $data = common::excuteObjectSql("select *, TO_CHAR(delivery_date, 'Mon-DD-YYYY') as _delivery_date,
  855. TO_CHAR(created_time, 'Mon-DD-YYYY') as _created_time,
  856. TO_CHAR(update_time, 'Mon-DD-YYYY') as _update_time
  857. from public.kln_destination_delivery where serial_no = '$serial_no'");
  858. $shipmentsData = $this->search_shipment_with_booking($data['serial_no'],$data['h_serial_no'],$data['ctnr']);
  859. $email_sql = common::sendDestinationDeliveryReminder($data,$shipmentsData,$status);
  860. if(!empty($email_sql)){
  861. common::excuteUpdateSql($email_sql);
  862. }
  863. } else {
  864. $status = "Submit";
  865. $data = common::excuteObjectSql("select *, TO_CHAR(delivery_date, 'Mon-DD-YYYY') as _delivery_date,
  866. TO_CHAR(created_time, 'Mon-DD-YYYY') as _created_time,
  867. TO_CHAR(update_time, 'Mon-DD-YYYY') as _update_time
  868. from public.kln_destination_delivery where serial_no = '$serial_no'");
  869. $shipmentsData = $this->search_shipment_with_booking($data['serial_no'],$data['h_serial_no'],$data['ctnr']);
  870. $email_sql = common::sendDestinationDeliveryReminder($data,$shipmentsData,$status);
  871. if(!empty($email_sql)){
  872. common::excuteUpdateSql($email_sql);
  873. }
  874. }
  875. $data = array("msg" =>"success");
  876. common::echo_json_encode(200,$data);
  877. exit();
  878. }
  879. /**
  880. * 审核 Approve or Reject Cancel Pending Approval
  881. */
  882. if ($operate == "review"){
  883. $serial_no = common::deCode($_POST['serial_no'], 'D');
  884. $status = common::check_input($_POST['status']);
  885. $notes = common::check_input($_POST['notes']);
  886. $action_user = _getLoginName();
  887. //前端按钮 后台权限拦截
  888. common::checkedActionLegal($serial_no,$operate);
  889. $sql = "";
  890. //审核通过时,才取下放当时应用修改的地址
  891. if ($status == "Approve") {
  892. $address_country = common::excuteOneSql("select array_to_json(address_country) as address_country from public.kln_destination_delivery where serial_no = '$serial_no'");
  893. $onine_address = common::excuteListSql("select * from public.contacts_address_online where delivery_serial_no = '$serial_no'");
  894. $sql .= "delete from public.contacts_address_online where delivery_serial_no = '$serial_no';";
  895. $sql .=common::returnDAddressRecord($address_country,$onine_address);
  896. }
  897. if ($status == "Reject" || $status == "Cancel") {
  898. //这种情况,清除当前用户对这一票的修改和添加的信息
  899. $sql .= "delete from public.contacts_address_online where delivery_serial_no = '$serial_no';";
  900. }
  901. $sql .= "update public.kln_destination_delivery set status = '$status', modify_by = '"._getLoginName()."',update_time = now() where serial_no = '$serial_no';";
  902. $sql .="INSERT INTO public.kln_destination_delivery_operation_log(
  903. serial_no, action,notes, create_by, created_time, created_zone)
  904. VALUES ('$serial_no', '$status','$notes', '$action_user', now(), ''); ";
  905. $rs = common::excuteUpdateSql($sql);
  906. if ($rs === FALSE){
  907. $data = array("msg" => "Update Error");
  908. common::echo_json_encode(200,$data);
  909. } else{
  910. //状态变更时,发送邮件提醒
  911. $data = common::excuteObjectSql("select *,
  912. TO_CHAR(delivery_date, 'Mon-DD-YYYY') as _delivery_date,
  913. TO_CHAR(created_time, 'Mon-DD-YYYY') as _created_time,
  914. TO_CHAR(update_time, 'Mon-DD-YYYY') as _update_time
  915. from public.kln_destination_delivery where serial_no = '$serial_no'");
  916. $shipmentsData = $this->search_shipment_with_booking($data['serial_no'],$data['h_serial_no'],$data['ctnr']);
  917. $email_sql = common::sendDestinationDeliveryReminder($data,$shipmentsData,$status);
  918. if(!empty($email_sql)){
  919. common::excuteUpdateSql($email_sql);
  920. }
  921. $data = array("msg" =>"success");
  922. common::echo_json_encode(200,$data);
  923. }
  924. }
  925. /**
  926. * 邮件留言初始
  927. */
  928. if ($operate == "email_message_init"){
  929. try {
  930. $email_uuid = common::deCode($_POST['serial_no'], 'D');
  931. $emailRecords = $this->getCommunicationNew($email_uuid);
  932. common::echo_json_encode(200,array("msg" => "Sent Successfully", "emailRecords" => $emailRecords));
  933. exit();
  934. } catch (Exception $e) {
  935. common::echo_json_encode(500,array("msg" => "Sent Error."));
  936. exit();
  937. }
  938. }
  939. /**
  940. * 邮件留言
  941. */
  942. if ($operate == "email_message_board"){
  943. //前端按钮 后台权限拦截
  944. $email_uuid = common::deCode($_POST['serial_no'], 'D');
  945. common::checkedActionLegal($email_uuid,$operate);
  946. try {
  947. $content = $_POST["content"];
  948. $content = common::check_input($content);
  949. $content = urldecode($content);
  950. $text = $_POST["text"];
  951. $web_content = urldecode($text);
  952. $serial_no = common::uuid();
  953. $email_uuid = common::deCode($_POST['serial_no'], 'D');
  954. $add_by = _getLoginName();
  955. $refer_id = 0;
  956. $from_email = "US.KApex.Online@kerryapex.com";
  957. $to_email = $_POST["kln_pic"];
  958. $cc_email = "";
  959. $communication_cc = $_POST["communication_cc"];
  960. $communication_cc = trim($communication_cc);
  961. if (!empty($communication_cc)) {
  962. $communication_cc = common::check_input($communication_cc);
  963. $cc_email = $communication_cc;
  964. }
  965. $user_from = _getLoginName();
  966. $h_bol_str = array();
  967. foreach($_POST["h_bol"] as $obj){
  968. $h_bol_str[] = $obj['key'];
  969. }
  970. //邮件发送
  971. $title = "Destination Delivery from " . _getLoginName() . ", Shipment No: " . utils::implode(',',$h_bol_str);
  972. $emailContent = $content;
  973. common::excuteUpdateSql("insert into public.email_record (type,title,from_email,to_email,cc_email,content,insert_date)values('Delivery_Comm','$title','$from_email','$to_email','$cc_email','$emailContent',now());");
  974. $rs = common::excuteUpdateSql("INSERT INTO public.online_ocean_communication(serial_no, email_uuid, content,web_content,user_from, user_to, user_cc, refer_id, add_by, add_time, cc_email)
  975. VALUES ('$serial_no', '$email_uuid', '$emailContent','$web_content', '$user_from', '$to_email', '$cc_email', $refer_id, '$add_by', now(), '$communication_cc');");
  976. $emailRecords = $this->getCommunicationNew($email_uuid);
  977. common::echo_json_encode(200,array("msg" => "Sent Successfully", "emailRecords" => $emailRecords));
  978. exit();
  979. } catch (Exception $e) {
  980. common::echo_json_encode(500,array("msg" => "Sent Error."));
  981. exit();
  982. }
  983. }
  984. /**
  985. * view detail and log
  986. */
  987. if ($operate == "view_detail"){
  988. $serial_no = common::deCode($_POST['serial_no'], 'D');
  989. //前端按钮 后台权限拦截
  990. common::checkedActionLegal($serial_no,$operate);
  991. $data = common::excuteObjectSql("select * from public.kln_destination_delivery where serial_no = '$serial_no'");
  992. $_shipmentsData = array();
  993. $shipmentsData = $this->search_shipment_with_booking($data['serial_no'],$data['h_serial_no'],$data['ctnr']);
  994. foreach($shipmentsData["data"] as $shipment){
  995. $_shipmentsData[] = array("HBOL No." => $shipment['h_bol'],"Container No." => $shipment['ctnr'],
  996. "Service Type" => $shipment['service'],"ETA" => $shipment['eta'],
  997. "Recommended Delivery Date" => $shipment['date_range']);
  998. }
  999. $_operation_log = array();
  1000. $operation_log = common::excuteListSql("select * from public.kln_destination_delivery_operation_log where serial_no = '$serial_no' order by id desc");
  1001. foreach($operation_log as $log){
  1002. $action = $log['action'] == "Pending Approval" ? "Submit" : $log['action'];
  1003. $_operation_log[] = array("time" => $log['created_time'],"timezone" => $log['created_zone'],
  1004. "label" => $action,"createdBy" => $log['create_by'],
  1005. "tips" => $log['notes']);
  1006. }
  1007. $retData = $data;
  1008. $retData["status"] = common::deliveryStatusConvert($retData["status"]);
  1009. $retData['shipmentsData'] = $_shipmentsData;
  1010. $retData['operation_log'] = $_operation_log;
  1011. $data = array("msg" =>"success","data"=>$retData);
  1012. common::echo_json_encode(200,$data);
  1013. exit();
  1014. }
  1015. /**
  1016. * Delivery date
  1017. */
  1018. if ($operate == "delivery_date_load"){
  1019. $sqlWhere = ' where ' . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  1020. //兜底规则
  1021. $sqlWhere .= " and eta >= now() - INTERVAL '6 months' and eta <= now() + INTERVAL '6 months'";
  1022. $sqlDeliveryWhere = " 1=1";
  1023. if(_isApexLogin()){
  1024. //$sqlWhere .= " and kln_pic ilike '"._getLoginEamil()."'";
  1025. $sqlDeliveryWhere .= " and '".strtolower(_getLoginEamil())."' in (select regexp_split_to_table(lower(kd.kln_pic), ','))";
  1026. }
  1027. //$sqlWhere_befrom_filterTag = $sqlWhere;
  1028. $sql = "select delivery_date::date as delivery_date,
  1029. sum(case when status ='Pending Approval' then 1 else 0 end) as pending_approval_rc,
  1030. sum(case when status ='Approve' then 1 else 0 end) as approved_rc
  1031. from public.kln_destination_delivery kd
  1032. where ".$sqlDeliveryWhere."
  1033. and exists(select 1 from public.kln_ocean oo ". $sqlWhere." and oo.serial_no = any(h_serial_no))
  1034. group by delivery_date::date order by delivery_date::date desc";
  1035. $rs = common::excuteListSql($sql);
  1036. error_log("delivery_date_load:".$sql);
  1037. $dateArr = new stdClass();
  1038. foreach($rs as $val){
  1039. //$dateArr[] = array($val['delivery_date'] => array("pending"=>$val['pending_approval_rc'],"approved"=>$val['approved_rc']));
  1040. $dateArr->{$val['delivery_date']} = new stdClass();
  1041. $dateArr->{$val['delivery_date']}->pending = $val['pending_approval_rc'];
  1042. $dateArr->{$val['delivery_date']}->approved = $val['approved_rc'];
  1043. }
  1044. $data = array("msg" =>"success","data"=>$dateArr);
  1045. common::echo_json_encode(200,$data);
  1046. }
  1047. }
  1048. private function getCommunicationNew($serial_no) {
  1049. $list = common::excuteListSql("select to_char(add_time, 'MM/dd/yyyy hh24:MI:ss') as add_times, * from public.online_ocean_communication where email_uuid='$serial_no' and refer_id = 0 order by id");
  1050. $emialRecords =array();
  1051. foreach ($list as $k => $v) {
  1052. $msg =array();
  1053. $msg["name"] = $v["add_by"];
  1054. $msg["creatTime"] = $v["add_times"];
  1055. $msg["content"] = urldecode($v["web_content"]);
  1056. $emialRecords[] = $msg;
  1057. }
  1058. return $emialRecords;
  1059. }
  1060. function search_shipment(){
  1061. $sqlWhere_common = ' where ' . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  1062. //根據用戶的賬號權限所能看到的全部shipment數據的目的地站點,去匹配是否有開通destination delivery的服務,如果未開通,則展示此提示頁面
  1063. //兜底规则
  1064. $sqlWhere = $sqlWhere_common;
  1065. $sqlWhere .= " and eta >= now() - INTERVAL '6 months' and eta <= now() + INTERVAL '6 months'";
  1066. // $checkOpenSql = "with oo as ( select DISTINCT agent from public.kln_ocean oo ". $sqlWhere.")
  1067. // select agent from oo
  1068. // where exists(select 1 from public.kln_destination_delivery_config where oo.agent in (select regexp_split_to_table(station, E',')))
  1069. // limit 1";
  1070. $checkOpenSql = "select station from public.kln_destination_delivery_config dc
  1071. where exists(select 1 from
  1072. public.kln_ocean oo ". $sqlWhere."
  1073. and oo.agent in (select regexp_split_to_table(dc.station, E',')) limit 1
  1074. )";
  1075. $config = common::excuteListSql($checkOpenSql);
  1076. if(empty($config)){
  1077. return array("msg"=>"Destination Delivery Service Not Available","data"=>"");
  1078. }
  1079. //根據用戶的賬號權限所能看到的全部shipment數據的目的地站點,去匹配是否有開通destination delivery的服務,如果有開通,但沒有符合條件的shipment,則展示此提示頁面
  1080. $sqlWhere = $sqlWhere_common;
  1081. $sqlWhere .= " and eta >= now() - INTERVAL '6 months' and eta <= now() + INTERVAL '6 months'";
  1082. $text_search = $_REQUEST['text_search'];
  1083. if (!empty($text_search)){
  1084. $sqlWhere .= " and (oo.h_bol ilike '%".strtolower($text_search)."%'
  1085. or oo.po_no ilike '%".strtolower($text_search)."%'
  1086. or oo.ctnrs ilike '%".strtolower($text_search)."%')";
  1087. }
  1088. //vessel name
  1089. $vessel = $_REQUEST['vessel'];
  1090. if (!empty($vessel)){
  1091. $sqlWhere .= " and vessel ilike '%".strtolower($vessel)."'%";
  1092. }
  1093. $consignee = $_REQUEST['consignee'];
  1094. if (!empty($vessel)){
  1095. $sqlWhere .= " and consignee ilike '%".strtolower($consignee)."'%";
  1096. }
  1097. $shipper = $_REQUEST['shipper'];
  1098. if (!empty($vessel)){
  1099. $sqlWhere .= " and shipper ilike '%".strtolower($shipper)."'%";
  1100. }
  1101. if (isset($_REQUEST['eta_start']) && !empty($_REQUEST['eta_start']))
  1102. $sqlWhere .= " and eta >= '" . common::usDate2sqlDate($_REQUEST['eta_start']) . " 00:00:00'";
  1103. if (isset($_REQUEST['eta_end']) && !empty($_REQUEST['eta_end']))
  1104. $sqlWhere .= " and eta <= '" . common::usDate2sqlDate($_REQUEST['eta_end']) . " 23:59:59'";
  1105. if (isset($_REQUEST['ata_start']) && !empty($_REQUEST['ata_start']))
  1106. $sqlWhere .= " and ata >= '" . common::usDate2sqlDate($_REQUEST['ata_start']) . " 00:00:00'";
  1107. if (isset($_REQUEST['ata_end']) && !empty($_REQUEST['ata_end']))
  1108. $sqlWhere .= " and ata <= '" . common::usDate2sqlDate($_REQUEST['ata_end']) . " 23:59:59'";
  1109. $sql = "with oo as(
  1110. select * from public.kln_ocean oo ". $sqlWhere ."
  1111. ),
  1112. ooc as (
  1113. select oo.*,
  1114. dc.serial_no as dc_serial_no,
  1115. dc.country as dc_country,
  1116. dc.kln_pic as dc_kln_pic
  1117. from public.kln_destination_delivery_config dc
  1118. Inner Join oo
  1119. on oo.agent in (select regexp_split_to_table(dc.station, ','))
  1120. where 1=1 and
  1121. case when dc.booking_window = 'Restrictions_ETD_ATD' and COALESCE(dc.booking_window_date_start,'')<>'' and COALESCE(dc.booking_window_date_end,'')<>''
  1122. then COALESCE(atd, etd) >= (NOW() - (dc.booking_window_date_start ||' days')::INTERVAL)::date and COALESCE(atd, etd) <= (NOW() + (dc.booking_window_date_end ||' days')::INTERVAL)::date
  1123. when dc.booking_window = 'Restrictions_ETA_ATA' and COALESCE(dc.booking_window_date_start,'')<>'' and COALESCE(dc.booking_window_date_end,'')<>''
  1124. then COALESCE(ata, eta) >= (NOW() - (dc.booking_window_date_start ||' days')::INTERVAL)::date and COALESCE(ata, eta) <= (NOW() + (dc.booking_window_date_end ||' days')::INTERVAL)::date
  1125. else 1=1 end
  1126. ),
  1127. matched_data as (
  1128. select
  1129. ooc.serial_no,
  1130. ooc.h_bol,
  1131. ooc.m_bol,
  1132. oc.ctnr,
  1133. ooc.service,
  1134. ooc.po_no,
  1135. (select reference_no from public.ocean o where o.serial_no = ooc.serial_no limit 1) as reference_no,
  1136. ooc.transport_mode,
  1137. oc.qty::text as pakages,
  1138. oc.unit as package_type ,
  1139. oc.grs_kgs::text as kgw,
  1140. oc.cbm as volume,
  1141. ooc.vessel,
  1142. ooc.voyage,
  1143. ooc.carrier,
  1144. ooc.fport_of_loading_un,
  1145. ooc.mport_of_discharge_un,
  1146. ooc.eta,
  1147. ooc.ata,
  1148. ooc.shipper,
  1149. ooc.consignee_id,
  1150. ooc.consignee,
  1151. (select customer_name from public.ocean_extend ex where ex.serial_no = ooc.serial_no limit 1) as controlling_customer,
  1152. ooc.booking_no,
  1153. (select manifest_type from public.ocean o where o.serial_no = ooc.serial_no limit 1) as manifest_type,
  1154. dc_serial_no,
  1155. dc_country,
  1156. dc_kln_pic,
  1157. ooc.agent,
  1158. ooc.order_from
  1159. from ooc
  1160. LEFT JOIN public.oc_container oc ON oc.serial_no::text = ooc.serial_no::text
  1161. where ooc.order_from = 'public' and transport_mode = 'sea'
  1162. union all
  1163. select
  1164. ooc.serial_no,
  1165. ooc.h_bol,
  1166. ooc.m_bol,
  1167. oc.ctnr,
  1168. ooc.service,
  1169. ooc.po_no,
  1170. (select reference_no from sfs.ocean o where o.serial_no = ooc.serial_no limit 1) as reference_no,
  1171. ooc.transport_mode,
  1172. oc.qty::text as pakages,
  1173. oc.unit as package_type ,
  1174. oc.grs_kgs::text as kgw,
  1175. oc.cbm as volume,
  1176. ooc.vessel,
  1177. ooc.voyage,
  1178. ooc.carrier,
  1179. ooc.fport_of_loading_un,
  1180. ooc.mport_of_discharge_un,
  1181. ooc.eta,
  1182. ooc.ata,
  1183. ooc.shipper,
  1184. ooc.consignee_id,
  1185. ooc.consignee,
  1186. (select customer_name from ocean_extend ex where ex.serial_no = ooc.serial_no limit 1) as controlling_customer,
  1187. ooc.booking_no,
  1188. (select manifest_type from sfs.ocean o where o.serial_no = ooc.serial_no limit 1) as manifest_type,
  1189. dc_serial_no,
  1190. dc_country,
  1191. dc_kln_pic,
  1192. ooc.agent,
  1193. ooc.order_from
  1194. from ooc
  1195. LEFT JOIN sfs.oc_container oc ON oc.serial_no::text = ooc.serial_no::text
  1196. where ooc.order_from = 'sfs' and transport_mode = 'sea'
  1197. union all
  1198. select
  1199. ooc.serial_no,
  1200. ooc.h_bol,
  1201. ooc.m_bol,
  1202. '' as ctnr,
  1203. ooc.service,
  1204. ooc.po_no,
  1205. '' as reference_no,
  1206. ooc.transport_mode,
  1207. ooc.qty as pakages,
  1208. ooc.qty_uom as package_type ,
  1209. ooc.piece_count as kgw,
  1210. ooc.cbm as volume,
  1211. ooc.vessel,
  1212. ooc.voyage,
  1213. ooc.carrier,
  1214. ooc.fport_of_loading_un,
  1215. ooc.mport_of_discharge_un,
  1216. ooc.eta,
  1217. ooc.ata,
  1218. ooc.shipper,
  1219. ooc.consignee_id,
  1220. ooc.consignee,
  1221. '' as controlling_customer,
  1222. ooc.booking_no,
  1223. '' as manifest_type,
  1224. dc_serial_no,
  1225. dc_country,
  1226. dc_kln_pic,
  1227. ooc.agent,
  1228. ooc.order_from
  1229. from ooc
  1230. where ooc.transport_mode = 'air'
  1231. )
  1232. select md.*,
  1233. CASE
  1234. WHEN r.recommended_delivery_from IS NOT NULL AND r.recommended_delivery_to IS NOT NULL THEN
  1235. to_char((COALESCE(ata, eta) - (r.recommended_delivery_from ||' days')::INTERVAL)::date, 'YYYY.MM.DD')
  1236. || '-' ||
  1237. to_char((COALESCE(ata, eta) + (r.recommended_delivery_to ||' days')::INTERVAL)::date, 'YYYY.MM.DD')
  1238. WHEN r.recommended_delivery_from IS NULL AND r.recommended_delivery_to IS NOT NULL THEN
  1239. '-' ||
  1240. to_char((COALESCE(ata, eta) + (r.recommended_delivery_to || ' days')::INTERVAL)::date, 'YYYY.MM.DD')
  1241. WHEN r.recommended_delivery_from IS NOT NULL AND r.recommended_delivery_to IS NULL THEN
  1242. to_char((COALESCE(ata, eta) - (r.recommended_delivery_from || ' days')::INTERVAL)::date, 'YYYY.MM.DD')
  1243. || '-'
  1244. ELSE '' END
  1245. AS date_range,
  1246. r.recommended_delivery_from,
  1247. r.recommended_delivery_to,
  1248. CASE
  1249. WHEN r.recommended_delivery_from IS NOT NULL THEN
  1250. (COALESCE(ata, eta) - (r.recommended_delivery_from ||' days')::INTERVAL)::date
  1251. ELSE null END
  1252. AS recommended_delivery_from_date,
  1253. CASE
  1254. WHEN r.recommended_delivery_to IS NOT NULL THEN
  1255. (COALESCE(ata, eta) + (r.recommended_delivery_to ||' days')::INTERVAL)::date
  1256. ELSE null END
  1257. AS recommended_delivery_to_date
  1258. from matched_data md
  1259. LEFT JOIN LATERAL (
  1260. SELECT
  1261. r.*
  1262. FROM public.kln_destination_delivery_rule r
  1263. WHERE md.transport_mode = r.mode_type
  1264. and r.recommended_delivery_serial_no = md.dc_serial_no
  1265. AND ((md.transport_mode = 'sea' AND (
  1266. (r.ports ilike '%'|| md.mport_of_discharge_un ||'%' AND r.carrier ilike '%'|| md.carrier ||'%')
  1267. OR (r.ports ilike '%'|| md.mport_of_discharge_un ||'%' AND r.carrier = 'ALL')
  1268. OR (r.ports = 'ALL' AND r.carrier ilike '%'|| md.carrier ||'%')
  1269. OR (r.ports = 'ALL' AND r.carrier = 'ALL'))
  1270. )
  1271. OR (md.transport_mode = 'air' AND (
  1272. r.ports ilike '%'|| md.mport_of_discharge_un ||'%'
  1273. OR r.ports = 'ALL')
  1274. )
  1275. )
  1276. ORDER BY priority asc
  1277. LIMIT 1
  1278. ) r ON true
  1279. order by md.eta desc";
  1280. $rs = common::excuteListSql($sql);
  1281. error_log($sql);
  1282. if(empty($rs)){
  1283. return array("msg"=>"No Eligible Shipments for Booking","data"=>"");
  1284. }
  1285. return array("msg"=>"","data"=>$rs);
  1286. }
  1287. /**
  1288. * 不依赖配置的规则查询,如果规则已经移除或者修改不在查询范围内,開通期間創建的booking數據正常保留和展示
  1289. */
  1290. function search_shipment_with_booking($serial_no,$h_serial_no,$ctnr){
  1291. $sqlCtnr = "";
  1292. if(!empty($ctnr)){
  1293. $sqlCtnr = "and oc.ctnr in (select regexp_split_to_table('".$ctnr."', ','))";
  1294. }
  1295. $sqlWhere = "where oo.serial_no = ANY('".$h_serial_no."'::TEXT[])";
  1296. $sql = "with ooc as(
  1297. select * from public.kln_ocean oo ". $sqlWhere ."
  1298. ),
  1299. matched_data as (
  1300. select
  1301. ooc.serial_no,
  1302. ooc.h_bol,
  1303. ooc.m_bol,
  1304. oc.ctnr,
  1305. ooc.service,
  1306. ooc.po_no,
  1307. (select reference_no from public.ocean o where o.serial_no = ooc.serial_no limit 1) as reference_no,
  1308. ooc.transport_mode,
  1309. oc.qty::text as pakages,
  1310. oc.unit as package_type ,
  1311. oc.grs_kgs::text as kgw,
  1312. oc.cbm as volume,
  1313. ooc.vessel,
  1314. ooc.voyage,
  1315. ooc.carrier,
  1316. ooc.fport_of_loading_un,
  1317. ooc.mport_of_discharge_un,
  1318. ooc.eta,
  1319. ooc.ata,
  1320. ooc.shipper,
  1321. ooc.consignee_id,
  1322. ooc.consignee,
  1323. (select customer_name from public.ocean_extend ex where ex.serial_no = ooc.serial_no limit 1) as controlling_customer,
  1324. ooc.booking_no,
  1325. (select manifest_type from public.ocean o where o.serial_no = ooc.serial_no limit 1) as manifest_type,
  1326. ooc.agent,
  1327. ooc.order_from
  1328. from ooc
  1329. LEFT JOIN public.oc_container oc ON oc.serial_no::text = ooc.serial_no::text
  1330. where ooc.order_from = 'public' and transport_mode = 'sea' ".$sqlCtnr."
  1331. union all
  1332. select
  1333. ooc.serial_no,
  1334. ooc.h_bol,
  1335. ooc.m_bol,
  1336. oc.ctnr,
  1337. ooc.service,
  1338. ooc.po_no,
  1339. (select reference_no from sfs.ocean o where o.serial_no = ooc.serial_no limit 1) as reference_no,
  1340. ooc.transport_mode,
  1341. oc.qty::text as pakages,
  1342. oc.unit as package_type ,
  1343. oc.grs_kgs::text as kgw,
  1344. oc.cbm as volume,
  1345. ooc.vessel,
  1346. ooc.voyage,
  1347. ooc.carrier,
  1348. ooc.fport_of_loading_un,
  1349. ooc.mport_of_discharge_un,
  1350. ooc.eta,
  1351. ooc.ata,
  1352. ooc.shipper,
  1353. ooc.consignee_id,
  1354. ooc.consignee,
  1355. (select customer_name from ocean_extend ex where ex.serial_no = ooc.serial_no limit 1) as controlling_customer,
  1356. ooc.booking_no,
  1357. (select manifest_type from sfs.ocean o where o.serial_no = ooc.serial_no limit 1) as manifest_type,
  1358. ooc.agent,
  1359. ooc.order_from
  1360. from ooc
  1361. LEFT JOIN sfs.oc_container oc ON oc.serial_no::text = ooc.serial_no::text
  1362. where ooc.order_from = 'sfs' and transport_mode = 'sea' ".$sqlCtnr."
  1363. union all
  1364. select
  1365. ooc.serial_no,
  1366. ooc.h_bol,
  1367. ooc.m_bol,
  1368. '' as ctnr,
  1369. ooc.service,
  1370. ooc.po_no,
  1371. '' as reference_no,
  1372. ooc.transport_mode,
  1373. ooc.qty as pakages,
  1374. ooc.qty_uom as package_type ,
  1375. ooc.piece_count as kgw,
  1376. ooc.cbm as volume,
  1377. ooc.vessel,
  1378. ooc.voyage,
  1379. ooc.carrier,
  1380. ooc.fport_of_loading_un,
  1381. ooc.mport_of_discharge_un,
  1382. ooc.eta,
  1383. ooc.ata,
  1384. ooc.shipper,
  1385. ooc.consignee_id,
  1386. ooc.consignee,
  1387. '' as controlling_customer,
  1388. ooc.booking_no,
  1389. '' as manifest_type,
  1390. ooc.agent,
  1391. ooc.order_from
  1392. from ooc
  1393. where ooc.transport_mode = 'air'
  1394. )
  1395. select md.*,
  1396. CASE
  1397. WHEN kd.recommended_delivery_window_date_from IS NOT NULL AND kd.recommended_delivery_window_date_to IS NOT NULL THEN
  1398. to_char((recommended_delivery_window_date_from)::date, 'YYYY-MM-DD')
  1399. || ';' ||
  1400. to_char((recommended_delivery_window_date_to)::date, 'YYYY-MM-DD')
  1401. WHEN kd.recommended_delivery_window_date_from IS NULL AND kd.recommended_delivery_window_date_to IS NOT NULL THEN
  1402. ';' ||
  1403. to_char((recommended_delivery_window_date_to)::date, 'YYYY-MM-DD')
  1404. WHEN kd.recommended_delivery_window_date_from IS NOT NULL AND kd.recommended_delivery_window_date_to IS NULL THEN
  1405. to_char((recommended_delivery_window_date_from)::date, 'YYYY-MM-DD')
  1406. || ';'
  1407. ELSE ''
  1408. END AS date_range,
  1409. kd.recommended_delivery_from,
  1410. kd.recommended_delivery_to,
  1411. kd.recommended_delivery_window_date_from as recommended_delivery_from_date,
  1412. kd.recommended_delivery_window_date_to as recommended_delivery_to_date,
  1413. (select dc.country from public.kln_destination_delivery_config dc where md.agent in (select regexp_split_to_table(dc.station, ','))) as dc_country
  1414. from matched_data md
  1415. LEFT join public.kln_destination_delivery kd on kd.serial_no = '$serial_no'
  1416. order by md.eta desc";
  1417. //error_log($sql);
  1418. $rs = common::excuteListSql($sql);
  1419. return array("msg"=>"","data"=>$rs);
  1420. }
  1421. function groupShipments($booking_no, $manifest_type, $h_bol,$h_serial_no, $order_from,$m_bol, $ctnr,$kln_pic,$consignee,$consignee_id,
  1422. $recommended_delivery_window_date_from, $recommended_delivery_window_date_to,
  1423. $recommended_delivery_from,$recommended_delivery_to,$dc_country){
  1424. // Combine the data into an array of shipments
  1425. $shipments = [];
  1426. foreach ($h_bol as $index => $_h_bol) {
  1427. $shipments[] = [
  1428. 'booking_no' =>$booking_no[$index],
  1429. 'manifest_type' => $manifest_type[$index],
  1430. 'h_bol' => $h_bol[$index],
  1431. 'h_serial_no' => $h_serial_no[$index],
  1432. 'order_from' => $order_from[$index],
  1433. 'm_bol' => $m_bol[$index],
  1434. 'ctnr' => $ctnr[$index],
  1435. 'kln_pic' => $kln_pic[$index],
  1436. 'consignee' => $consignee[$index],
  1437. 'consignee_id' => $consignee_id[$index],
  1438. 'dc_country' => $dc_country[$index],
  1439. 'recommended_delivery_window_date_from' => $recommended_delivery_window_date_from[$index],
  1440. 'recommended_delivery_window_date_to' => $recommended_delivery_window_date_to[$index],
  1441. 'recommended_delivery_from' => $recommended_delivery_from[$index],
  1442. 'recommended_delivery_to' => $recommended_delivery_to[$index]
  1443. ];
  1444. }
  1445. // 按 MBOL 分组
  1446. $groupedByMbol = [];
  1447. // 按 ShipmentID 分组
  1448. $groupedByShipmentId = [];
  1449. foreach ($shipments as $item) {
  1450. $m_bol = $item['m_bol'];
  1451. $h_serial_no = $item['h_serial_no'];
  1452. $h_bol = $item['h_bol'];
  1453. $order_from = $item['order_from'];
  1454. $ctnr = $item['ctnr'];
  1455. $dc_country = $item['dc_country'];
  1456. $consignee_id = $item['consignee_id'];
  1457. if($item['manifest_type'] == 'BCN'){
  1458. if (!isset($groupedByMbol[$m_bol])) {
  1459. $item['h_bol'] = array();
  1460. $item['h_bol_multiple_link'] = array();
  1461. $item['h_serial_no'] = array();
  1462. $item['ctnr'] = array();
  1463. $item['dc_country'] = array();
  1464. $item['consignee_id'] = array();
  1465. $item['m_bol'] = array($item['m_bol']);
  1466. $groupedByMbol[$m_bol] = $item;
  1467. }
  1468. if (!in_array($h_serial_no, $groupedByMbol[$m_bol]['h_serial_no'])) {
  1469. $groupedByMbol[$m_bol]['h_bol'][] = $h_bol;
  1470. $groupedByMbol[$m_bol]['h_bol_multiple_link'][] = array("key"=>$h_bol,"value" =>$h_serial_no,"order_from"=>$order_from);
  1471. $groupedByMbol[$m_bol]['h_serial_no'][] = $h_serial_no;
  1472. $groupedByMbol[$m_bol]['dc_country'][] = $dc_country;
  1473. $groupedByMbol[$m_bol]['consignee_id'][] = $consignee_id;
  1474. }
  1475. if (!in_array($ctnr, $groupedByMbol[$m_bol]['ctnr'])) {
  1476. $groupedByMbol[$m_bol]['ctnr'][] = $ctnr;
  1477. }
  1478. } else {
  1479. if (!isset($groupedByShipmentId[$h_serial_no])) {
  1480. $item['m_bol'] = array();
  1481. $item['ctnr'] = array();
  1482. $item['dc_country'] = array($item['dc_country']);
  1483. $item['consignee_id'] = array($item['consignee_id']);
  1484. $item['h_serial_no'] = array($item['h_serial_no']);
  1485. $item['h_bol'] = array($item['h_bol']);
  1486. $item['h_bol_multiple_link'] = array(array("key"=>$h_bol,"value" =>$h_serial_no,"order_from"=>$order_from));
  1487. $groupedByShipmentId[$h_serial_no] = $item;
  1488. }
  1489. if (!in_array($m_bol, $groupedByShipmentId[$h_serial_no]['m_bol'])) {
  1490. $groupedByShipmentId[$h_serial_no]['m_bol'][] = $m_bol;
  1491. }
  1492. if (!in_array($ctnr, $groupedByShipmentId[$h_serial_no]['ctnr'])) {
  1493. $groupedByShipmentId[$h_serial_no]['ctnr'][] = $ctnr;
  1494. }
  1495. }
  1496. }
  1497. $result = array();
  1498. foreach($groupedByMbol as $mb){
  1499. $result[] = $mb;
  1500. }
  1501. foreach($groupedByShipmentId as $hb){
  1502. $result[] = $hb;
  1503. }
  1504. return $result;
  1505. }
  1506. }
  1507. ?>