destination_delivery.class.php 78 KB

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