destination_delivery.class.php 80 KB

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