destination_delivery.class.php 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647
  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. if(_isApexLogin()){
  550. //$sqlWhere .= " and kln_pic ilike '"._getLoginEamil()."'";
  551. $sqlWhere .= "and '".strtolower(_getLoginEamil())."' in (select regexp_split_to_table(lower(kln_pic), ','))";
  552. }
  553. //条件带入
  554. $text_search = $_REQUEST['text_search'];
  555. if (!empty($text_search)){
  556. $sqlWhere .= " and (booking_no ilike '%".strtolower($text_search)."%'
  557. or h_bol ilike '%".strtolower($text_search)."%'
  558. or m_bol ilike '%".strtolower($text_search)."%'
  559. or ctnr ilike '%".strtolower($text_search)."%'
  560. or consignee ilike '%".strtolower($text_search)."%')";
  561. }
  562. if (isset($_REQUEST['delivery_date_start']) && !empty($_REQUEST['delivery_date_start']))
  563. $sqlWhere .= " and delivery_date >= '" . common::usDate2sqlDate($_REQUEST['delivery_date_start']) . " 00:00:00'";
  564. if (isset($_REQUEST['delivery_date_end']) && !empty($_REQUEST['delivery_date_end']))
  565. $sqlWhere .= " and delivery_date <= '" . common::usDate2sqlDate($_REQUEST['delivery_date_end']) . " 23:59:59'";
  566. if(!empty($_POST['delivery_mode'])){
  567. $sqlWhere .= " and delivery_mode ='". common::check_input($_REQUEST['delivery_mode'])."'";
  568. }
  569. if (isset($_REQUEST['created_time_start']) && !empty($_REQUEST['created_time_start']))
  570. $sqlWhere .= " and created_time >= '" . common::usDate2sqlDate($_REQUEST['created_time_start']) . " 00:00:00'";
  571. if (isset($_REQUEST['created_time_end']) && !empty($_REQUEST['created_time_end']))
  572. $sqlWhere .= " and created_time <= '" . common::usDate2sqlDate($_REQUEST['created_time_end']) . " 23:59:59'";
  573. //移除filterTag
  574. $sqlWhere_befrom_filterTag = $sqlWhere;
  575. $filterTag_param = "";
  576. if (!empty($_POST["filterTag"])) {
  577. if (utils::count($_POST['filterTag']) == 1){
  578. $filterTag = $_POST['filterTag'][0];
  579. }else{
  580. $filterTag = utils::implode(",", $_POST['filterTag']);
  581. }
  582. $filterTag_param = "1<>1";
  583. if(strtolower($filterTag) == "all"){
  584. $filterTag_param = "1=1";
  585. }
  586. if (stripos($filterTag, "Pending Approval") !== FALSE) {
  587. $filterTag_param .= " or (status = 'Pending Approval')";
  588. }
  589. if (stripos($filterTag, "Approve") !== FALSE) {
  590. $filterTag_param .= " or (status = 'Approve')";
  591. }
  592. if (stripos($filterTag, "Reject") !== FALSE) {
  593. $filterTag_param .= " or (status = 'Reject')";
  594. }
  595. if (stripos($filterTag, "Cancel") !== FALSE) {
  596. $filterTag_param .= " or (status = 'Cancel')";
  597. }
  598. if(strtolower($filterTag) <> "all" && !empty($filterTag)){
  599. $filterTag_param = " ($filterTag_param)";
  600. }
  601. }
  602. if(empty($filterTag_param)){
  603. $filterTag_param = "1=1";
  604. }
  605. $tag_and_mode_param = $filterTag_param;
  606. $rc = $_POST ['rc'];
  607. if ($rc == - 1 || true) {
  608. $sql = "select count(1) as rc,
  609. sum(case when $tag_and_mode_param then 1 else 0 end) as seach_rc,
  610. sum(case when status ='Pending Approval' then 1 else 0 end) as pending_approval_rc,
  611. sum(case when status ='Approve' then 1 else 0 end) as approved_rc,
  612. sum(case when status ='Reject' then 1 else 0 end) as rejected_rc,
  613. sum(case when status ='Cancel' then 1 else 0 end) as cancelled_rc
  614. from public.kln_destination_delivery
  615. where exists(select 1 from public.kln_ocean oo ". $sqlWhere_befrom_filterTag." and oo.serial_no = any(h_serial_no))";
  616. error_log("kln_destination_delivery_count:".$sql);
  617. $statusRc = common::excuteObjectSql($sql);
  618. $rc = $statusRc['rc'];
  619. $search_rc = $statusRc['seach_rc'];
  620. $pending_approval_rc = $statusRc['pending_approval_rc'];
  621. $approved_rc = $statusRc['approved_rc'];
  622. $rejected_rc = $statusRc['rejected_rc'];
  623. $cancelled_rc = $statusRc['cancelled_rc'];
  624. }
  625. $tp = ceil($rc / $ps);
  626. if ($rc > 0) {
  627. $sql = "select *,to_char((recommended_delivery_window_date_from)::date, 'YYYY-MM-DD')
  628. || ';' ||
  629. to_char((recommended_delivery_window_date_to)::date, 'YYYY-MM-DD') AS date_range
  630. from public.kln_destination_delivery
  631. where ".$tag_and_mode_param."
  632. and exists(select 1 from public.kln_ocean oo ". $sqlWhere." and oo.serial_no = any(h_serial_no))";
  633. $sql .= " order by id desc limit " . $ps . " offset " . ($cp - 1) * $ps;
  634. error_log("kln_destination_delivery_search:".$sql);
  635. $rs = common::excuteListSql($sql);
  636. foreach($rs as $key => $val){
  637. $rs[$key]["_serial_no"] = common::deCode($val['serial_no'], 'E');
  638. $rs[$key]["status"] = common::deliveryStatusConvert($val['status']);
  639. $rs[$key]["h_bol"] = json_decode($val['h_bol_multiple_link']);
  640. }
  641. $arrTmp = array('searchData' => $rs,
  642. 'is_employee' => _isApexLogin(),
  643. 'All' =>$rc,
  644. 'pending_approval_rc' =>$pending_approval_rc,
  645. 'approved_rc' =>$approved_rc,
  646. 'rejected_rc' =>$rejected_rc,
  647. 'cancelled_rc' =>$cancelled_rc,
  648. 'rc' => $search_rc,
  649. 'ps' => $ps,
  650. 'cp' => $cp,
  651. 'tp' => $tp);
  652. common::echo_json_encode(200,$arrTmp);
  653. exit();
  654. }else{
  655. $arrTmp = array('searchData' => array(),
  656. 'is_employee' => _isApexLogin(),
  657. 'All' =>$rc,
  658. 'pending_approval_rc' =>$pending_approval_rc,
  659. 'approved_rc' =>$approved_rc,
  660. 'rejected_rc' =>$rejected_rc,
  661. 'cancelled_rc' =>$cancelled_rc,
  662. 'rc' => $search_rc,
  663. 'ps' => $ps,
  664. 'cp' => $cp,
  665. 'tp' => $tp);
  666. common::echo_json_encode(200,$arrTmp);
  667. exit();
  668. }
  669. }
  670. if ($operate == "search_shipment"){
  671. $data = $this->search_shipment();
  672. if(!empty($data['msg'])){
  673. $arrTmp = array("msg" =>$data['msg']);
  674. }else{
  675. $arrTmp = array("msg" =>"success","data"=>$data['data']);
  676. }
  677. common::echo_json_encode(200, $arrTmp);
  678. exit();
  679. }
  680. if ($operate == "add"){
  681. $serial_no = common::deCode($_POST['serial_no'], 'D');
  682. //前端按钮 后台权限拦截
  683. if(!empty($serial_no)){
  684. common::checkedActionLegal($serial_no,$operate);
  685. }else{
  686. if(!_isApexLogin()){
  687. //$data = array("msg" =>"Employees cannot create booking");
  688. //common::echo_json_encode(200,$data);
  689. //exit();
  690. }
  691. }
  692. $booking_data = array();
  693. if(!empty($serial_no)){
  694. //to_char(delivery_date, 'MM/DD/YYYY') AS _delivery_date,
  695. $sql = "select *,
  696. to_char(delivery_date, 'HH24:MI') AS _delivery_time
  697. from public.kln_destination_delivery where serial_no = '$serial_no'";
  698. $booking_data = common::excuteObjectSql($sql);
  699. $booking_data["delivery_time"] = $booking_data["_delivery_time"];
  700. $booking_data["delivery_address_detail"] = json_decode($booking_data["delivery_address_detail"],true);
  701. $shipments = $this->search_shipment_with_booking($booking_data['serial_no'],$booking_data['h_serial_no'],$booking_data['ctnr']);
  702. } else {
  703. $shipments = $this->search_shipment();
  704. }
  705. if(!empty($shipments['msg'])){
  706. $data = array("msg" =>$shipments['msg']);
  707. common::echo_json_encode(200,$data);
  708. exit();
  709. }
  710. $booking_data['tableData'] = $shipments['data'];
  711. $data = array("msg" =>"success","data"=>$booking_data);
  712. common::echo_json_encode(200,$data);
  713. exit();
  714. }
  715. if ($operate == "save"){
  716. $serial_no = common::deCode($_POST['serial_no'], 'D');
  717. $booking_no = $_POST['booking_no'];
  718. $manifest_type = $_POST['manifest_type'];
  719. $h_bol = $_POST['h_bol'];
  720. $h_serial_no = $_POST['serial_no'];
  721. $order_from = $_POST['order_from'];
  722. $m_bol = $_POST['m_bol'];
  723. $ctnr = $_POST['ctnr'];
  724. $kln_pic = $_POST['dc_kln_pic'];
  725. $consignee = $_POST['consignee'];
  726. $consignee_id = $_POST['consignee_id'];
  727. $dc_country = $_POST['dc_country'];
  728. $recommended_delivery_window_date_from = $_POST['recommended_delivery_from_date'];
  729. $recommended_delivery_window_date_to = $_POST['recommended_delivery_to_date'];
  730. $recommended_delivery_from = $_POST['recommended_delivery_from'];
  731. $recommended_delivery_to = $_POST['recommended_delivery_to'];
  732. $modify_reason = common::check_input($_POST['modify_reason']);
  733. $delivery_date = $_POST['delivery_date'];
  734. $delivery_time = $_POST['delivery_time'];
  735. $delivery_mode = common::check_input($_POST['delivery_mode']);
  736. //这status 编辑只有两种情况,新加,和在拒绝后 用户重新编辑 状态都是Pending Approval
  737. $status = "Pending Approval";
  738. $is_new = false;
  739. if(empty($serial_no)){
  740. $is_new = true;
  741. }
  742. $delivery_address = common::check_input($_POST['delivery_address']);
  743. $special_requirements = common::check_input($_POST['special_requirements']);
  744. //检查不能重复添加booking 这里空运没问题,但是会遇到相同hbol 不同的柜子情况,本质上来说就是一条
  745. //$sqlWhere = ' where ' . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  746. //兜底规则
  747. //$sqlWhere .= " and eta >= now() - INTERVAL '6 months' and eta <= now() + INTERVAL '6 months'";
  748. // $ckeckedSql = "with save_table as (select regexp_split_to_table('".utils::implode(',',$h_serial_no)."', ',') as serial_no)
  749. // select string_agg(h_bol, ';') as h_bol from public.kln_destination_delivery kde
  750. // where exists(select 1 from save_table where save_table.serial_no = any(kde.h_serial_no))
  751. // and exists(select 1 from public.kln_ocean oo ". $sqlWhere." and oo.serial_no = any(kde.h_serial_no))";
  752. // //代表编辑
  753. // if(!empty($serial_no)){
  754. // $ckeckedSql .=" and kde.serial_no <> '$serial_no'";
  755. // }
  756. // $checkeData = common::excuteObjectSql($ckeckedSql);
  757. // if(!empty($checkeData['h_bol'])){
  758. // $data = array("msg" =>"The ".$checkeData['h_bol'] ." already exists");
  759. // common::echo_json_encode(200,$data);
  760. // exit();
  761. // }
  762. $sql = "";
  763. //代表修改
  764. if(!empty($serial_no)){
  765. $updateSqlSet = " modify_by = '"._getLoginName()."',update_time = now()";
  766. if (!empty($status)) {
  767. $updateSqlSet.= ", status = '$status' ";
  768. }
  769. if (!empty($delivery_address)) {
  770. $delivery_address_detail = common::check_input(json_encode(common::returnDAddress(),JSON_UNESCAPED_UNICODE));
  771. $updateSqlSet.= ", delivery_address = '$delivery_address' ";
  772. $updateSqlSet.= ", delivery_address_detail = '$delivery_address_detail' ";
  773. }
  774. if (!empty($special_requirements)) {
  775. $updateSqlSet.= ", special_requirements = '$special_requirements' ";
  776. }
  777. if (!empty($modify_reason)) {
  778. $updateSqlSet.= ", modify_reason = '$modify_reason' ";
  779. }
  780. if (!empty($delivery_date)) {
  781. $delivery_date = empty($_POST['delivery_date']) ? "null": "'".common::usDate2sqlDate($_POST['delivery_date'])." ".$delivery_time."'";
  782. $updateSqlSet.= ", delivery_date = $delivery_date";
  783. }
  784. //修改记录临时表
  785. common::saveDAddressTempTable($serial_no,$consignee_id);
  786. //代表update
  787. $sql .= "update public.kln_destination_delivery set ".$updateSqlSet."
  788. where serial_no = '$serial_no';";
  789. //记录log
  790. $sql .="INSERT INTO public.kln_destination_delivery_operation_log(
  791. serial_no, action,notes, create_by, created_time, created_zone)
  792. VALUES ('$serial_no', 'Submit','".$modify_reason."', '"._getLoginName()."', now(), ''); ";
  793. } else {
  794. $saveData = $this->groupShipments($booking_no,$manifest_type,$h_bol,$h_serial_no,$order_from,$m_bol,$ctnr,$kln_pic,$consignee,$consignee_id,
  795. $recommended_delivery_window_date_from, $recommended_delivery_window_date_to,
  796. $recommended_delivery_from,$recommended_delivery_to,$dc_country);
  797. foreach($saveData as $sData){
  798. $serial_no = common::uuid();
  799. // 使用 PostgreSQL 的 ARRAY 语法
  800. $tags_sql = "ARRAY['" . implode("','", array_map('pg_escape_string', $sData['h_serial_no'])) . "']";
  801. // 使用 PostgreSQL 的 ARRAY 语法
  802. $country_sql = "ARRAY['" . implode("','", array_map('pg_escape_string', $sData['dc_country'])) . "']";
  803. $recommended_delivery_window_date_from = "";
  804. if(empty($sData['recommended_delivery_window_date_from'])){
  805. $recommended_delivery_window_date_from = "null";
  806. } else {
  807. $recommended_delivery_window_date_from = "'".$sData['recommended_delivery_window_date_from']."'";
  808. }
  809. $recommended_delivery_window_date_to = "";
  810. if(empty($sData['recommended_delivery_window_date_to'])){
  811. $recommended_delivery_window_date_to = "null";
  812. } else {
  813. $recommended_delivery_window_date_to = "'".$sData['recommended_delivery_window_date_to']."'";
  814. }
  815. $delivery_date = empty($_POST['delivery_date']) ? "null": "'".common::usDate2sqlDate($_POST['delivery_date'])." ".$delivery_time."'";
  816. //修改记录临时表,有值代表选择的add
  817. $_sync_key_add = common::saveDAddressTempTable($serial_no,$sData['consignee_id']);
  818. $delivery_address_detail = common::check_input(json_encode(common::returnDAddress($_sync_key_add),JSON_UNESCAPED_UNICODE));
  819. $sql .= "INSERT INTO public.kln_destination_delivery(
  820. serial_no, h_serial_no,address_country,
  821. booking_no, h_bol,h_bol_multiple_link,
  822. m_bol, ctnr,
  823. consignee, delivery_date, delivery_mode, status, delivery_address,delivery_address_detail, special_requirements,
  824. recommended_delivery_window_date_from, recommended_delivery_window_date_to, d_address_change_log,
  825. recommended_delivery_from, recommended_delivery_to,kln_pic,
  826. create_by, created_time, modify_by, update_time)
  827. VALUES ('$serial_no', ".$tags_sql.",".$country_sql.",
  828. '".$sData['booking_no']."', '".utils::implode(',',$sData['h_bol'])."','".common::check_input(json_encode($sData['h_bol_multiple_link']))."',
  829. '".utils::implode(',',$sData['m_bol'])."', '".utils::implode(',',$sData['ctnr'])."',
  830. '".$sData['consignee']."', $delivery_date,'$delivery_mode','$status', '$delivery_address','$delivery_address_detail', '$special_requirements',
  831. $recommended_delivery_window_date_from, $recommended_delivery_window_date_to,'',
  832. '".$sData['recommended_delivery_from']."', '".$sData['recommended_delivery_to']."','".$sData['kln_pic']."',
  833. '"._getLoginName()."', now(), '"._getLoginName()."', now());";
  834. //记录log
  835. $sql .="INSERT INTO public.kln_destination_delivery_operation_log(
  836. serial_no, action,notes, create_by, created_time, created_zone)
  837. VALUES ('$serial_no', 'Submit','', '"._getLoginName()."', now(), ''); ";
  838. }
  839. }
  840. if(!empty($sql)){
  841. $rs = common::excuteUpdateSql($sql);
  842. if ($rs === FALSE){
  843. $data = array("msg" =>"error");
  844. common::echo_json_encode(200,$data);
  845. exit();
  846. }
  847. }
  848. if(!$is_new){
  849. //状态变更时,发送邮件提醒
  850. $status = "Modify";
  851. $data = common::excuteObjectSql("select *, TO_CHAR(delivery_date, 'Mon-DD-YYYY') as _delivery_date,
  852. TO_CHAR(created_time, 'Mon-DD-YYYY') as _created_time,
  853. TO_CHAR(update_time, 'Mon-DD-YYYY') as _update_time
  854. from public.kln_destination_delivery where serial_no = '$serial_no'");
  855. $shipmentsData = $this->search_shipment_with_booking($data['serial_no'],$data['h_serial_no'],$data['ctnr']);
  856. $email_sql = common::sendDestinationDeliveryReminder($data,$shipmentsData,$status);
  857. if(!empty($email_sql)){
  858. common::excuteUpdateSql($email_sql);
  859. }
  860. } else {
  861. $status = "Submit";
  862. $data = common::excuteObjectSql("select *, TO_CHAR(delivery_date, 'Mon-DD-YYYY') as _delivery_date,
  863. TO_CHAR(created_time, 'Mon-DD-YYYY') as _created_time,
  864. TO_CHAR(update_time, 'Mon-DD-YYYY') as _update_time
  865. from public.kln_destination_delivery where serial_no = '$serial_no'");
  866. $shipmentsData = $this->search_shipment_with_booking($data['serial_no'],$data['h_serial_no'],$data['ctnr']);
  867. $email_sql = common::sendDestinationDeliveryReminder($data,$shipmentsData,$status);
  868. if(!empty($email_sql)){
  869. common::excuteUpdateSql($email_sql);
  870. }
  871. }
  872. $data = array("msg" =>"success");
  873. common::echo_json_encode(200,$data);
  874. exit();
  875. }
  876. /**
  877. * 审核 Approve or Reject Cancel Pending Approval
  878. */
  879. if ($operate == "review"){
  880. $serial_no = common::deCode($_POST['serial_no'], 'D');
  881. $status = common::check_input($_POST['status']);
  882. $notes = common::check_input($_POST['notes']);
  883. $action_user = _getLoginName();
  884. //前端按钮 后台权限拦截
  885. common::checkedActionLegal($serial_no,$operate);
  886. $sql = "";
  887. //审核通过时,才取下放当时应用修改的地址
  888. if ($status == "Approve") {
  889. $address_country = common::excuteOneSql("select array_to_json(address_country) as address_country from public.kln_destination_delivery where serial_no = '$serial_no'");
  890. $onine_address = common::excuteListSql("select * from public.contacts_address_online where delivery_serial_no = '$serial_no'");
  891. $sql .= "delete from public.contacts_address_online where delivery_serial_no = '$serial_no';";
  892. $sql .=common::returnDAddressRecord($address_country,$onine_address);
  893. }
  894. if ($status == "Reject" || $status == "Cancel") {
  895. //这种情况,清除当前用户对这一票的修改和添加的信息
  896. $sql .= "delete from public.contacts_address_online where delivery_serial_no = '$serial_no';";
  897. }
  898. $sql .= "update public.kln_destination_delivery set status = '$status', modify_by = '"._getLoginName()."',update_time = now() where serial_no = '$serial_no';";
  899. $sql .="INSERT INTO public.kln_destination_delivery_operation_log(
  900. serial_no, action,notes, create_by, created_time, created_zone)
  901. VALUES ('$serial_no', '$status','$notes', '$action_user', now(), ''); ";
  902. $rs = common::excuteUpdateSql($sql);
  903. if ($rs === FALSE){
  904. $data = array("msg" => "Update Error");
  905. common::echo_json_encode(200,$data);
  906. } else{
  907. //状态变更时,发送邮件提醒
  908. $data = common::excuteObjectSql("select *,
  909. TO_CHAR(delivery_date, 'Mon-DD-YYYY') as _delivery_date,
  910. TO_CHAR(created_time, 'Mon-DD-YYYY') as _created_time,
  911. TO_CHAR(update_time, 'Mon-DD-YYYY') as _update_time
  912. from public.kln_destination_delivery where serial_no = '$serial_no'");
  913. $shipmentsData = $this->search_shipment_with_booking($data['serial_no'],$data['h_serial_no'],$data['ctnr']);
  914. $email_sql = common::sendDestinationDeliveryReminder($data,$shipmentsData,$status);
  915. if(!empty($email_sql)){
  916. common::excuteUpdateSql($email_sql);
  917. }
  918. $data = array("msg" =>"success");
  919. common::echo_json_encode(200,$data);
  920. }
  921. }
  922. /**
  923. * 邮件留言初始
  924. */
  925. if ($operate == "email_message_init"){
  926. try {
  927. $email_uuid = common::deCode($_POST['serial_no'], 'D');
  928. $emailRecords = $this->getCommunicationNew($email_uuid);
  929. common::echo_json_encode(200,array("msg" => "Sent Successfully", "emailRecords" => $emailRecords));
  930. exit();
  931. } catch (Exception $e) {
  932. common::echo_json_encode(500,array("msg" => "Sent Error."));
  933. exit();
  934. }
  935. }
  936. /**
  937. * 邮件留言
  938. */
  939. if ($operate == "email_message_board"){
  940. //前端按钮 后台权限拦截
  941. $email_uuid = common::deCode($_POST['serial_no'], 'D');
  942. common::checkedActionLegal($email_uuid,$operate);
  943. try {
  944. $content = $_POST["content"];
  945. $content = common::check_input($content);
  946. $content = urldecode($content);
  947. $text = $_POST["text"];
  948. $web_content = urldecode($text);
  949. $serial_no = common::uuid();
  950. $email_uuid = common::deCode($_POST['serial_no'], 'D');
  951. $add_by = _getLoginName();
  952. $refer_id = 0;
  953. $from_email = "US.KApex.Online@kerryapex.com";
  954. $to_email = $_POST["kln_pic"];
  955. $cc_email = "";
  956. $communication_cc = $_POST["communication_cc"];
  957. $communication_cc = trim($communication_cc);
  958. if (!empty($communication_cc)) {
  959. $communication_cc = common::check_input($communication_cc);
  960. $cc_email = $communication_cc;
  961. }
  962. $user_from = _getLoginName();
  963. $h_bol_str = array();
  964. foreach($_POST["h_bol"] as $obj){
  965. $h_bol_str[] = $obj['key'];
  966. }
  967. //邮件发送
  968. $title = "Destination Delivery from " . _getLoginName() . ", Shipment No: " . utils::implode(',',$h_bol_str);
  969. $emailContent = $content;
  970. 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());");
  971. $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)
  972. VALUES ('$serial_no', '$email_uuid', '$emailContent','$web_content', '$user_from', '$to_email', '$cc_email', $refer_id, '$add_by', now(), '$communication_cc');");
  973. $emailRecords = $this->getCommunicationNew($email_uuid);
  974. common::echo_json_encode(200,array("msg" => "Sent Successfully", "emailRecords" => $emailRecords));
  975. exit();
  976. } catch (Exception $e) {
  977. common::echo_json_encode(500,array("msg" => "Sent Error."));
  978. exit();
  979. }
  980. }
  981. /**
  982. * view detail and log
  983. */
  984. if ($operate == "view_detail"){
  985. $serial_no = common::deCode($_POST['serial_no'], 'D');
  986. //前端按钮 后台权限拦截
  987. common::checkedActionLegal($serial_no,$operate);
  988. $data = common::excuteObjectSql("select * from public.kln_destination_delivery where serial_no = '$serial_no'");
  989. $_shipmentsData = array();
  990. $shipmentsData = $this->search_shipment_with_booking($data['serial_no'],$data['h_serial_no'],$data['ctnr']);
  991. foreach($shipmentsData["data"] as $shipment){
  992. $_shipmentsData[] = array("HBOL No." => $shipment['h_bol'],"Container No." => $shipment['ctnr'],
  993. "Service Type" => $shipment['service'],"ETA" => $shipment['eta'],
  994. "Recommended Delivery Date" => $shipment['date_range']);
  995. }
  996. $_operation_log = array();
  997. $operation_log = common::excuteListSql("select * from public.kln_destination_delivery_operation_log where serial_no = '$serial_no' order by id desc");
  998. foreach($operation_log as $log){
  999. $action = $log['action'] == "Pending Approval" ? "Submit" : $log['action'];
  1000. $_operation_log[] = array("time" => $log['created_time'],"timezone" => $log['created_zone'],
  1001. "label" => $action,"createdBy" => $log['create_by'],
  1002. "tips" => $log['notes']);
  1003. }
  1004. $retData = $data;
  1005. $retData["status"] = common::deliveryStatusConvert($retData["status"]);
  1006. $retData['shipmentsData'] = $_shipmentsData;
  1007. $retData['operation_log'] = $_operation_log;
  1008. $data = array("msg" =>"success","data"=>$retData);
  1009. common::echo_json_encode(200,$data);
  1010. exit();
  1011. }
  1012. /**
  1013. * Delivery date
  1014. */
  1015. if ($operate == "delivery_date_load"){
  1016. $sqlWhere = ' where ' . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  1017. //兜底规则
  1018. //$sqlWhere .= " and eta >= now() - INTERVAL '6 months' and eta <= now() + INTERVAL '6 months'";
  1019. //代表employee
  1020. if(_isApexLogin()){
  1021. //$sqlWhere .= " and kln_pic = '"._getLoginEamil()."'";
  1022. }
  1023. $sqlWhere_befrom_filterTag = $sqlWhere;
  1024. $sql = "select delivery_date::date as delivery_date,
  1025. sum(case when status ='Pending Approval' then 1 else 0 end) as pending_approval_rc,
  1026. sum(case when status ='Approve' then 1 else 0 end) as approved_rc
  1027. from public.kln_destination_delivery
  1028. where exists(select 1 from public.kln_ocean oo ". $sqlWhere_befrom_filterTag." and oo.serial_no = any(h_serial_no))
  1029. group by delivery_date::date order by delivery_date::date desc";
  1030. $rs = common::excuteListSql($sql);
  1031. $dateArr = new stdClass();
  1032. foreach($rs as $val){
  1033. //$dateArr[] = array($val['delivery_date'] => array("pending"=>$val['pending_approval_rc'],"approved"=>$val['approved_rc']));
  1034. $dateArr->{$val['delivery_date']} = new stdClass();
  1035. $dateArr->{$val['delivery_date']}->pending = $val['pending_approval_rc'];
  1036. $dateArr->{$val['delivery_date']}->approved = $val['approved_rc'];
  1037. }
  1038. $data = array("msg" =>"success","data"=>$dateArr);
  1039. common::echo_json_encode(200,$data);
  1040. }
  1041. }
  1042. private function getCommunicationNew($serial_no) {
  1043. $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");
  1044. $emialRecords =array();
  1045. foreach ($list as $k => $v) {
  1046. $msg =array();
  1047. $msg["name"] = $v["add_by"];
  1048. $msg["creatTime"] = $v["add_times"];
  1049. $msg["content"] = urldecode($v["web_content"]);
  1050. $emialRecords[] = $msg;
  1051. }
  1052. return $emialRecords;
  1053. }
  1054. function search_shipment(){
  1055. $sqlWhere_common = ' where ' . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  1056. //根據用戶的賬號權限所能看到的全部shipment數據的目的地站點,去匹配是否有開通destination delivery的服務,如果未開通,則展示此提示頁面
  1057. //兜底规则
  1058. $sqlWhere = $sqlWhere_common;
  1059. $sqlWhere .= " and eta >= now() - INTERVAL '6 months' and eta <= now() + INTERVAL '6 months'";
  1060. // $checkOpenSql = "with oo as ( select DISTINCT agent from public.kln_ocean oo ". $sqlWhere.")
  1061. // select agent from oo
  1062. // where exists(select 1 from public.kln_destination_delivery_config where oo.agent in (select regexp_split_to_table(station, E',')))
  1063. // limit 1";
  1064. $checkOpenSql = "select station from public.kln_destination_delivery_config dc
  1065. where exists(select 1 from
  1066. public.kln_ocean oo ". $sqlWhere."
  1067. and oo.agent in (select regexp_split_to_table(dc.station, E',')) limit 1
  1068. )";
  1069. $config = common::excuteListSql($checkOpenSql);
  1070. if(empty($config)){
  1071. return array("msg"=>"Destination Delivery Service Not Available","data"=>"");
  1072. }
  1073. //根據用戶的賬號權限所能看到的全部shipment數據的目的地站點,去匹配是否有開通destination delivery的服務,如果有開通,但沒有符合條件的shipment,則展示此提示頁面
  1074. $sqlWhere = $sqlWhere_common;
  1075. $sqlWhere .= " and eta >= now() - INTERVAL '6 months' and eta <= now() + INTERVAL '6 months'";
  1076. $text_search = $_REQUEST['text_search'];
  1077. if (!empty($text_search)){
  1078. $sqlWhere .= " and (oo.h_bol ilike '%".strtolower($text_search)."%'
  1079. or oo.po_no ilike '%".strtolower($text_search)."%'
  1080. or oo.ctnrs ilike '%".strtolower($text_search)."%')";
  1081. }
  1082. //vessel name
  1083. $vessel = $_REQUEST['vessel'];
  1084. if (!empty($vessel)){
  1085. $sqlWhere .= " and vessel ilike '%".strtolower($vessel)."'%";
  1086. }
  1087. $consignee = $_REQUEST['consignee'];
  1088. if (!empty($vessel)){
  1089. $sqlWhere .= " and consignee ilike '%".strtolower($consignee)."'%";
  1090. }
  1091. $shipper = $_REQUEST['shipper'];
  1092. if (!empty($vessel)){
  1093. $sqlWhere .= " and shipper ilike '%".strtolower($shipper)."'%";
  1094. }
  1095. if (isset($_REQUEST['eta_start']) && !empty($_REQUEST['eta_start']))
  1096. $sqlWhere .= " and eta >= '" . common::usDate2sqlDate($_REQUEST['eta_start']) . " 00:00:00'";
  1097. if (isset($_REQUEST['eta_end']) && !empty($_REQUEST['eta_end']))
  1098. $sqlWhere .= " and eta <= '" . common::usDate2sqlDate($_REQUEST['eta_end']) . " 23:59:59'";
  1099. if (isset($_REQUEST['ata_start']) && !empty($_REQUEST['ata_start']))
  1100. $sqlWhere .= " and ata >= '" . common::usDate2sqlDate($_REQUEST['ata_start']) . " 00:00:00'";
  1101. if (isset($_REQUEST['ata_end']) && !empty($_REQUEST['ata_end']))
  1102. $sqlWhere .= " and ata <= '" . common::usDate2sqlDate($_REQUEST['ata_end']) . " 23:59:59'";
  1103. $sql = "with oo as(
  1104. select * from public.kln_ocean oo ". $sqlWhere ."
  1105. ),
  1106. ooc as (
  1107. select oo.*,
  1108. dc.serial_no as dc_serial_no,
  1109. dc.country as dc_country,
  1110. dc.kln_pic as dc_kln_pic
  1111. from public.kln_destination_delivery_config dc
  1112. Inner Join oo
  1113. on oo.agent in (select regexp_split_to_table(dc.station, ','))
  1114. where 1=1 and
  1115. case when dc.booking_window = 'Restrictions_ETD_ATD' and COALESCE(dc.booking_window_date_start,'')<>'' and COALESCE(dc.booking_window_date_end,'')<>''
  1116. 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
  1117. when dc.booking_window = 'Restrictions_ETA_ATA' and COALESCE(dc.booking_window_date_start,'')<>'' and COALESCE(dc.booking_window_date_end,'')<>''
  1118. 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
  1119. else 1=1 end
  1120. ),
  1121. matched_data as (
  1122. select
  1123. ooc.serial_no,
  1124. ooc.h_bol,
  1125. ooc.m_bol,
  1126. oc.ctnr,
  1127. ooc.service,
  1128. ooc.po_no,
  1129. (select reference_no from public.ocean o where o.serial_no = ooc.serial_no limit 1) as reference_no,
  1130. ooc.transport_mode,
  1131. oc.qty::text as pakages,
  1132. oc.unit as package_type ,
  1133. oc.grs_kgs::text as kgw,
  1134. oc.cbm as volume,
  1135. ooc.vessel,
  1136. ooc.voyage,
  1137. ooc.carrier,
  1138. ooc.fport_of_loading_un,
  1139. ooc.mport_of_discharge_un,
  1140. ooc.eta,
  1141. ooc.ata,
  1142. ooc.shipper,
  1143. ooc.consignee_id,
  1144. ooc.consignee,
  1145. (select customer_name from public.ocean_extend ex where ex.serial_no = ooc.serial_no limit 1) as controlling_customer,
  1146. ooc.booking_no,
  1147. (select manifest_type from public.ocean o where o.serial_no = ooc.serial_no limit 1) as manifest_type,
  1148. dc_serial_no,
  1149. dc_country,
  1150. dc_kln_pic,
  1151. ooc.agent,
  1152. ooc.order_from
  1153. from ooc
  1154. LEFT JOIN public.oc_container oc ON oc.serial_no::text = ooc.serial_no::text
  1155. where ooc.order_from = 'public' and transport_mode = 'sea'
  1156. union all
  1157. select
  1158. ooc.serial_no,
  1159. ooc.h_bol,
  1160. ooc.m_bol,
  1161. oc.ctnr,
  1162. ooc.service,
  1163. ooc.po_no,
  1164. (select reference_no from sfs.ocean o where o.serial_no = ooc.serial_no limit 1) as reference_no,
  1165. ooc.transport_mode,
  1166. oc.qty::text as pakages,
  1167. oc.unit as package_type ,
  1168. oc.grs_kgs::text as kgw,
  1169. oc.cbm as volume,
  1170. ooc.vessel,
  1171. ooc.voyage,
  1172. ooc.carrier,
  1173. ooc.fport_of_loading_un,
  1174. ooc.mport_of_discharge_un,
  1175. ooc.eta,
  1176. ooc.ata,
  1177. ooc.shipper,
  1178. ooc.consignee_id,
  1179. ooc.consignee,
  1180. (select customer_name from ocean_extend ex where ex.serial_no = ooc.serial_no limit 1) as controlling_customer,
  1181. ooc.booking_no,
  1182. (select manifest_type from sfs.ocean o where o.serial_no = ooc.serial_no limit 1) as manifest_type,
  1183. dc_serial_no,
  1184. dc_country,
  1185. dc_kln_pic,
  1186. ooc.agent,
  1187. ooc.order_from
  1188. from ooc
  1189. LEFT JOIN sfs.oc_container oc ON oc.serial_no::text = ooc.serial_no::text
  1190. where ooc.order_from = 'sfs' and transport_mode = 'sea'
  1191. union all
  1192. select
  1193. ooc.serial_no,
  1194. ooc.h_bol,
  1195. ooc.m_bol,
  1196. '' as ctnr,
  1197. ooc.service,
  1198. ooc.po_no,
  1199. '' as reference_no,
  1200. ooc.transport_mode,
  1201. ooc.qty as pakages,
  1202. ooc.qty_uom as package_type ,
  1203. ooc.piece_count as kgw,
  1204. ooc.cbm as volume,
  1205. ooc.vessel,
  1206. ooc.voyage,
  1207. ooc.carrier,
  1208. ooc.fport_of_loading_un,
  1209. ooc.mport_of_discharge_un,
  1210. ooc.eta,
  1211. ooc.ata,
  1212. ooc.shipper,
  1213. ooc.consignee_id,
  1214. ooc.consignee,
  1215. '' as controlling_customer,
  1216. ooc.booking_no,
  1217. '' as manifest_type,
  1218. dc_serial_no,
  1219. dc_country,
  1220. dc_kln_pic,
  1221. ooc.agent,
  1222. ooc.order_from
  1223. from ooc
  1224. where ooc.transport_mode = 'air'
  1225. )
  1226. select md.*,
  1227. CASE
  1228. WHEN r.recommended_delivery_from IS NOT NULL AND r.recommended_delivery_to IS NOT NULL THEN
  1229. to_char((COALESCE(ata, eta) - (r.recommended_delivery_from ||' days')::INTERVAL)::date, 'YYYY.MM.DD')
  1230. || '-' ||
  1231. to_char((COALESCE(ata, eta) + (r.recommended_delivery_to ||' days')::INTERVAL)::date, 'YYYY.MM.DD')
  1232. WHEN r.recommended_delivery_from IS NULL AND r.recommended_delivery_to IS NOT NULL THEN
  1233. '-' ||
  1234. to_char((COALESCE(ata, eta) + (r.recommended_delivery_to || ' days')::INTERVAL)::date, 'YYYY.MM.DD')
  1235. WHEN r.recommended_delivery_from IS NOT NULL AND r.recommended_delivery_to IS NULL THEN
  1236. to_char((COALESCE(ata, eta) - (r.recommended_delivery_from || ' days')::INTERVAL)::date, 'YYYY.MM.DD')
  1237. || '-'
  1238. ELSE '' END
  1239. AS date_range,
  1240. r.recommended_delivery_from,
  1241. r.recommended_delivery_to,
  1242. CASE
  1243. WHEN r.recommended_delivery_from IS NOT NULL THEN
  1244. (COALESCE(ata, eta) - (r.recommended_delivery_from ||' days')::INTERVAL)::date
  1245. ELSE null END
  1246. AS recommended_delivery_from_date,
  1247. CASE
  1248. WHEN r.recommended_delivery_to IS NOT NULL THEN
  1249. (COALESCE(ata, eta) + (r.recommended_delivery_to ||' days')::INTERVAL)::date
  1250. ELSE null END
  1251. AS recommended_delivery_to_date
  1252. from matched_data md
  1253. LEFT JOIN LATERAL (
  1254. SELECT
  1255. r.*
  1256. FROM public.kln_destination_delivery_rule r
  1257. WHERE md.transport_mode = r.mode_type
  1258. and r.recommended_delivery_serial_no = md.dc_serial_no
  1259. AND ((md.transport_mode = 'sea' AND (
  1260. (r.ports ilike '%'|| md.mport_of_discharge_un ||'%' AND r.carrier ilike '%'|| md.carrier ||'%')
  1261. OR (r.ports ilike '%'|| md.mport_of_discharge_un ||'%' AND r.carrier = 'ALL')
  1262. OR (r.ports = 'ALL' AND r.carrier ilike '%'|| md.carrier ||'%')
  1263. OR (r.ports = 'ALL' AND r.carrier = 'ALL'))
  1264. )
  1265. OR (md.transport_mode = 'air' AND (
  1266. r.ports ilike '%'|| md.mport_of_discharge_un ||'%'
  1267. OR r.ports = 'ALL')
  1268. )
  1269. )
  1270. ORDER BY priority asc
  1271. LIMIT 1
  1272. ) r ON true
  1273. order by md.eta desc";
  1274. $rs = common::excuteListSql($sql);
  1275. error_log($sql);
  1276. if(empty($rs)){
  1277. return array("msg"=>"No Eligible Shipments for Booking","data"=>"");
  1278. }
  1279. return array("msg"=>"","data"=>$rs);
  1280. }
  1281. /**
  1282. * 不依赖配置的规则查询,如果规则已经移除或者修改不在查询范围内,開通期間創建的booking數據正常保留和展示
  1283. */
  1284. function search_shipment_with_booking($serial_no,$h_serial_no,$ctnr){
  1285. $sqlCtnr = "";
  1286. if(!empty($ctnr)){
  1287. $sqlCtnr = "and oc.ctnr in (select regexp_split_to_table('".$ctnr."', ','))";
  1288. }
  1289. $sqlWhere = "where oo.serial_no = ANY('".$h_serial_no."'::TEXT[])";
  1290. $sql = "with ooc as(
  1291. select * from public.kln_ocean oo ". $sqlWhere ."
  1292. ),
  1293. matched_data as (
  1294. select
  1295. ooc.serial_no,
  1296. ooc.h_bol,
  1297. ooc.m_bol,
  1298. oc.ctnr,
  1299. ooc.service,
  1300. ooc.po_no,
  1301. (select reference_no from public.ocean o where o.serial_no = ooc.serial_no limit 1) as reference_no,
  1302. ooc.transport_mode,
  1303. oc.qty::text as pakages,
  1304. oc.unit as package_type ,
  1305. oc.grs_kgs::text as kgw,
  1306. oc.cbm as volume,
  1307. ooc.vessel,
  1308. ooc.voyage,
  1309. ooc.carrier,
  1310. ooc.fport_of_loading_un,
  1311. ooc.mport_of_discharge_un,
  1312. ooc.eta,
  1313. ooc.ata,
  1314. ooc.shipper,
  1315. ooc.consignee_id,
  1316. ooc.consignee,
  1317. (select customer_name from public.ocean_extend ex where ex.serial_no = ooc.serial_no limit 1) as controlling_customer,
  1318. ooc.booking_no,
  1319. (select manifest_type from public.ocean o where o.serial_no = ooc.serial_no limit 1) as manifest_type,
  1320. ooc.agent,
  1321. ooc.order_from
  1322. from ooc
  1323. LEFT JOIN public.oc_container oc ON oc.serial_no::text = ooc.serial_no::text
  1324. where ooc.order_from = 'public' and transport_mode = 'sea' ".$sqlCtnr."
  1325. union all
  1326. select
  1327. ooc.serial_no,
  1328. ooc.h_bol,
  1329. ooc.m_bol,
  1330. oc.ctnr,
  1331. ooc.service,
  1332. ooc.po_no,
  1333. (select reference_no from sfs.ocean o where o.serial_no = ooc.serial_no limit 1) as reference_no,
  1334. ooc.transport_mode,
  1335. oc.qty::text as pakages,
  1336. oc.unit as package_type ,
  1337. oc.grs_kgs::text as kgw,
  1338. oc.cbm as volume,
  1339. ooc.vessel,
  1340. ooc.voyage,
  1341. ooc.carrier,
  1342. ooc.fport_of_loading_un,
  1343. ooc.mport_of_discharge_un,
  1344. ooc.eta,
  1345. ooc.ata,
  1346. ooc.shipper,
  1347. ooc.consignee_id,
  1348. ooc.consignee,
  1349. (select customer_name from ocean_extend ex where ex.serial_no = ooc.serial_no limit 1) as controlling_customer,
  1350. ooc.booking_no,
  1351. (select manifest_type from sfs.ocean o where o.serial_no = ooc.serial_no limit 1) as manifest_type,
  1352. ooc.agent,
  1353. ooc.order_from
  1354. from ooc
  1355. LEFT JOIN sfs.oc_container oc ON oc.serial_no::text = ooc.serial_no::text
  1356. where ooc.order_from = 'sfs' and transport_mode = 'sea' ".$sqlCtnr."
  1357. union all
  1358. select
  1359. ooc.serial_no,
  1360. ooc.h_bol,
  1361. ooc.m_bol,
  1362. '' as ctnr,
  1363. ooc.service,
  1364. ooc.po_no,
  1365. '' as reference_no,
  1366. ooc.transport_mode,
  1367. ooc.qty as pakages,
  1368. ooc.qty_uom as package_type ,
  1369. ooc.piece_count as kgw,
  1370. ooc.cbm as volume,
  1371. ooc.vessel,
  1372. ooc.voyage,
  1373. ooc.carrier,
  1374. ooc.fport_of_loading_un,
  1375. ooc.mport_of_discharge_un,
  1376. ooc.eta,
  1377. ooc.ata,
  1378. ooc.shipper,
  1379. ooc.consignee_id,
  1380. ooc.consignee,
  1381. '' as controlling_customer,
  1382. ooc.booking_no,
  1383. '' as manifest_type,
  1384. ooc.agent,
  1385. ooc.order_from
  1386. from ooc
  1387. where ooc.transport_mode = 'air'
  1388. )
  1389. select md.*,
  1390. CASE
  1391. WHEN kd.recommended_delivery_window_date_from IS NOT NULL AND kd.recommended_delivery_window_date_to IS NOT NULL THEN
  1392. to_char((recommended_delivery_window_date_from)::date, 'YYYY-MM-DD')
  1393. || ';' ||
  1394. to_char((recommended_delivery_window_date_to)::date, 'YYYY-MM-DD')
  1395. WHEN kd.recommended_delivery_window_date_from IS NULL AND kd.recommended_delivery_window_date_to IS NOT NULL THEN
  1396. ';' ||
  1397. to_char((recommended_delivery_window_date_to)::date, 'YYYY-MM-DD')
  1398. WHEN kd.recommended_delivery_window_date_from IS NOT NULL AND kd.recommended_delivery_window_date_to IS NULL THEN
  1399. to_char((recommended_delivery_window_date_from)::date, 'YYYY-MM-DD')
  1400. || ';'
  1401. ELSE ''
  1402. END AS date_range,
  1403. kd.recommended_delivery_from,
  1404. kd.recommended_delivery_to,
  1405. kd.recommended_delivery_window_date_from as recommended_delivery_from_date,
  1406. kd.recommended_delivery_window_date_to as recommended_delivery_to_date,
  1407. (select dc.country from public.kln_destination_delivery_config dc where md.agent in (select regexp_split_to_table(dc.station, ','))) as dc_country
  1408. from matched_data md
  1409. LEFT join public.kln_destination_delivery kd on kd.serial_no = '$serial_no'
  1410. order by md.eta desc";
  1411. //error_log($sql);
  1412. $rs = common::excuteListSql($sql);
  1413. return array("msg"=>"","data"=>$rs);
  1414. }
  1415. function groupShipments($booking_no, $manifest_type, $h_bol,$h_serial_no, $order_from,$m_bol, $ctnr,$kln_pic,$consignee,$consignee_id,
  1416. $recommended_delivery_window_date_from, $recommended_delivery_window_date_to,
  1417. $recommended_delivery_from,$recommended_delivery_to,$dc_country){
  1418. // Combine the data into an array of shipments
  1419. $shipments = [];
  1420. foreach ($h_bol as $index => $_h_bol) {
  1421. $shipments[] = [
  1422. 'booking_no' =>$booking_no[$index],
  1423. 'manifest_type' => $manifest_type[$index],
  1424. 'h_bol' => $h_bol[$index],
  1425. 'h_serial_no' => $h_serial_no[$index],
  1426. 'order_from' => $order_from[$index],
  1427. 'm_bol' => $m_bol[$index],
  1428. 'ctnr' => $ctnr[$index],
  1429. 'kln_pic' => $kln_pic[$index],
  1430. 'consignee' => $consignee[$index],
  1431. 'consignee_id' => $consignee_id[$index],
  1432. 'dc_country' => $dc_country[$index],
  1433. 'recommended_delivery_window_date_from' => $recommended_delivery_window_date_from[$index],
  1434. 'recommended_delivery_window_date_to' => $recommended_delivery_window_date_to[$index],
  1435. 'recommended_delivery_from' => $recommended_delivery_from[$index],
  1436. 'recommended_delivery_to' => $recommended_delivery_to[$index]
  1437. ];
  1438. }
  1439. // 按 MBOL 分组
  1440. $groupedByMbol = [];
  1441. // 按 ShipmentID 分组
  1442. $groupedByShipmentId = [];
  1443. foreach ($shipments as $item) {
  1444. $m_bol = $item['m_bol'];
  1445. $h_serial_no = $item['h_serial_no'];
  1446. $h_bol = $item['h_bol'];
  1447. $order_from = $item['order_from'];
  1448. $ctnr = $item['ctnr'];
  1449. $dc_country = $item['dc_country'];
  1450. $consignee_id = $item['consignee_id'];
  1451. if($item['manifest_type'] == 'BCN'){
  1452. if (!isset($groupedByMbol[$m_bol])) {
  1453. $item['h_bol'] = array();
  1454. $item['h_bol_multiple_link'] = array();
  1455. $item['h_serial_no'] = array();
  1456. $item['ctnr'] = array();
  1457. $item['dc_country'] = array();
  1458. $item['consignee_id'] = array();
  1459. $item['m_bol'] = array($item['m_bol']);
  1460. $groupedByMbol[$m_bol] = $item;
  1461. }
  1462. if (!in_array($h_serial_no, $groupedByMbol[$m_bol]['h_serial_no'])) {
  1463. $groupedByMbol[$m_bol]['h_bol'][] = $h_bol;
  1464. $groupedByMbol[$m_bol]['h_bol_multiple_link'][] = array("key"=>$h_bol,"value" =>$h_serial_no,"order_from"=>$order_from);
  1465. $groupedByMbol[$m_bol]['h_serial_no'][] = $h_serial_no;
  1466. $groupedByMbol[$m_bol]['dc_country'][] = $dc_country;
  1467. $groupedByMbol[$m_bol]['consignee_id'][] = $consignee_id;
  1468. }
  1469. if (!in_array($ctnr, $groupedByMbol[$m_bol]['ctnr'])) {
  1470. $groupedByMbol[$m_bol]['ctnr'][] = $ctnr;
  1471. }
  1472. } else {
  1473. if (!isset($groupedByShipmentId[$h_serial_no])) {
  1474. $item['m_bol'] = array();
  1475. $item['ctnr'] = array();
  1476. $item['dc_country'] = array($item['dc_country']);
  1477. $item['consignee_id'] = array($item['consignee_id']);
  1478. $item['h_serial_no'] = array($item['h_serial_no']);
  1479. $item['h_bol'] = array($item['h_bol']);
  1480. $item['h_bol_multiple_link'] = array(array("key"=>$h_bol,"value" =>$h_serial_no,"order_from"=>$order_from));
  1481. $groupedByShipmentId[$h_serial_no] = $item;
  1482. }
  1483. if (!in_array($m_bol, $groupedByShipmentId[$h_serial_no]['m_bol'])) {
  1484. $groupedByShipmentId[$h_serial_no]['m_bol'][] = $m_bol;
  1485. }
  1486. if (!in_array($ctnr, $groupedByShipmentId[$h_serial_no]['ctnr'])) {
  1487. $groupedByShipmentId[$h_serial_no]['ctnr'][] = $ctnr;
  1488. }
  1489. }
  1490. }
  1491. $result = array();
  1492. foreach($groupedByMbol as $mb){
  1493. $result[] = $mb;
  1494. }
  1495. foreach($groupedByShipmentId as $hb){
  1496. $result[] = $hb;
  1497. }
  1498. return $result;
  1499. }
  1500. }
  1501. ?>