destination_delivery.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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 SUBSTRING(c.country FROM 1 FOR 2) AS country_code
  32. from public.contract_region_mapping crm
  33. left join ocean.contacts c on c.contact_id = crm.overeas and crm.ksmart_station_new ilike 'ksmart%'
  34. where coalesce(c.country,'')<> ''
  35. and SUBSTRING(c.country FROM 1 FOR 2) ilike '" . common::check_input($term) . "%'";
  36. $sql .= " order by country_code";
  37. $rs = common::excuteListSql($sql);
  38. //前端要加上id 从1开始
  39. $retData = array();
  40. foreach($rs as $key => $val){
  41. if(!empty($val['country_code'])){
  42. $retData[] = array("value" =>$val['country_code'] ,"label"=>$val['country_code']);
  43. }
  44. }
  45. common::echo_json_encode(200,$retData);
  46. exit();
  47. }
  48. /**
  49. * select station 选择国家后再展示 station list数据
  50. */
  51. if ($operate == "station") {
  52. $country = common::check_input($_REQUEST['country']);
  53. $sql = "select crm.overeas from public.contract_region_mapping crm
  54. left join ocean.contacts c on c.contact_id = crm.overeas and crm.ksmart_station_new ilike 'ksmart%'
  55. where SUBSTRING(c.country FROM 1 FOR 2) = '".$country."'";
  56. $rs = common::excuteListSql($sql);
  57. foreach($rs as $key => $val){
  58. if(!empty($val['overeas'])){
  59. $retData[] = array("value" =>$val['overeas'] ,"label"=>$val['overeas']);
  60. }
  61. }
  62. common::echo_json_encode(200,$retData);
  63. exit();
  64. }
  65. /**
  66. * select KLN PIC 下拉可供选择的employee帐号,且帐号权限站点包含选中的station
  67. */
  68. if ($operate == "employee_account") {
  69. $term = $_POST['term'];
  70. $term = trim($term);
  71. $station = $_REQUEST['station'];
  72. $more_param = common::getInNotInSqlForSearch(utils::implode(';',$station));
  73. $sql = "select email from employee where contact_id in ($more_param) and email ilike '" . common::check_input($term) . "%' limit 20";
  74. $rs = common::excuteListSql($sql);
  75. foreach($rs as $key => $val){
  76. if(!empty($val['email'])){
  77. $retData[] = array("value" =>$val['email'] ,"label"=>$val['email']);
  78. }
  79. }
  80. common::echo_json_encode(200,$retData);
  81. exit();
  82. }
  83. /**
  84. * 下拉可供选择的ports
  85. */
  86. if ($operate == "ports") {
  87. $mode = $_POST['mode'];
  88. $term = $_POST['term'];
  89. $term = trim($term);
  90. if ($mode == 'sea'){
  91. $sql = "select DISTINCT uncode from public.ports
  92. where coalesce(uncode,'')<> '' and uncode ilike '" . common::check_input($term) . "%'";
  93. } else {
  94. $sql = "select DISTINCT coalesce(airport.country_abb,'')||airport.airport_code as uncode from sfs.airport
  95. where coalesce(country_abb,'')<>''
  96. and coalesce(airport.country_abb,'')||airport.airport_code ilike '" . common::check_input($term) . "%'";
  97. }
  98. $sql .= " order by uncode limit 20";
  99. $rs = common::excuteListSql($sql);
  100. //前端要加上id 从1开始
  101. $retData = array();
  102. foreach($rs as $key => $val){
  103. if(!empty($val['uncode'])){
  104. $retData[] = array("value" =>$val['uncode'] ,"label"=>$val['uncode']);
  105. }
  106. }
  107. common::echo_json_encode(200,$retData);
  108. exit();
  109. }
  110. /**
  111. * 下拉可供选择的carrier
  112. */
  113. if ($operate == "carrier") {
  114. $term = $_POST['term'];
  115. $term = trim($term);
  116. $sql = "select DISTINCT scac from public.vessel_company
  117. where coalesce(scac,'')<> '' and scac ilike '" . common::check_input($term) . "%'";
  118. $sql .= " order by scac limit 20";
  119. $rs = common::excuteListSql($sql);
  120. //前端要加上id 从1开始
  121. $retData = array();
  122. foreach($rs as $key => $val){
  123. if(!empty($val['scac'])){
  124. $retData[] = array("value" =>$val['scac'] ,"label"=>$val['scac']);
  125. }
  126. }
  127. common::echo_json_encode(200,$retData);
  128. exit();
  129. }
  130. }
  131. /**
  132. * destination_delivery 配置
  133. */
  134. public function destination_delivery_config(){
  135. $operate = utils::_get('operate');
  136. $operate = strtolower($operate);
  137. /**
  138. * 这个是嵌套在destination_delivery的配置查询,
  139. */
  140. if ($operate == "search"){
  141. $cp = common::check_input($_POST ['cp']); //current_page
  142. $ps = common::check_input($_POST ['ps']); //ps
  143. if (empty($ps))
  144. $ps = 100;
  145. if (empty($cp))
  146. $cp = 1;
  147. $rc = $_POST ['rc'];
  148. if ($rc == - 1) {
  149. $sql = "select count(*) from public.kln_destination_delivery_config";
  150. $rc = common::excuteOneSql($sql);
  151. }
  152. $tp = ceil($rc / $ps);
  153. if ($rc > 0) {
  154. $sql = "select * from public.kln_destination_delivery_config";
  155. $sql .= " order by id desc limit " . $ps . " offset " . ($cp - 1) * $ps;
  156. $rs = common::excuteListSql($sql);
  157. foreach($rs as $key => $val){
  158. $rs[$key]["_serial_no"] = common::deCode($val['serial_no'], 'E');
  159. }
  160. $arrTmp = array('searchData' => $rs,
  161. 'rc' => $rc,
  162. 'ps' => $ps,
  163. 'cp' => $cp,
  164. 'tp' => $tp);
  165. common::echo_json_encode(200,$arrTmp);
  166. exit();
  167. }else{
  168. $arrTmp = array('searchData' => array());
  169. common::echo_json_encode(200, $arrTmp);
  170. exit();
  171. }
  172. }
  173. if ($operate == "add"){
  174. $serial_no = common::deCode($_GET['a'], 'D');
  175. $data = array();
  176. $rule = array();
  177. if (!empty($serial_no)){
  178. $sql = "select * from public.kln_destination_delivery_config where serial_no = '$serial_no'";
  179. $data = common::excuteObjectSql($sql);
  180. if ($data['recommended_delivery'] == 'delivery_eta_ata') {
  181. $sql = "select * from public.kln_destination_delivery_rule where recommended_delivery_serial_no = '$serial_no'";
  182. $rule = common::excuteObjectSql($sql);
  183. }
  184. }
  185. //处理组装返回格式
  186. //RcommendDeliveryDate
  187. $RecommendCheckedAirList = array();
  188. $RecommendCheckedSeaList = array();
  189. $RecommendCheckedList = array();
  190. foreach($rule as $item){
  191. if ($item['mode_type'] == 'sea'){
  192. $RecommendCheckedSeaList[] = array("Priority"=>$item['priority'],"RuleType"=>$item['rule_type'],
  193. "Port"=>explode(";", $item['ports']),
  194. "Carrier"=>explode(";", $item['carrier']),
  195. "FromDate"=>$item['recommended_delivery_from'],
  196. "ToDate"=>$item['recommended_delivery_to']);
  197. }
  198. if ($item['mode_type'] == 'air'){
  199. $RecommendCheckedAirList[] = array("Priority"=>$item['priority'],"RuleType"=>$item['rule_type'],
  200. "Port"=>explode(";", $item['ports']),
  201. "Carrier"=>explode(";", $item['carrier']),
  202. "FromDate"=>$item['recommended_delivery_from'],
  203. "ToDate"=>$item['recommended_delivery_to']);
  204. }
  205. if (!utils::in_array($item['mode_type'], $RecommendCheckedList)) {
  206. $RecommendCheckedList[] = $item['mode_type'];
  207. }
  208. }
  209. $returnData = $data;
  210. if (!empty($data['booking_window'])) {
  211. $returnData["SetBookingWindow"] = array("windowradio" =>$data['booking_window'],
  212. "windowBeforeDays"=>$data['booking_window'],
  213. "windowAfterDays"=>$data['booking_window']);
  214. } else {
  215. $returnData["SetBookingWindow"] = array();
  216. }
  217. if ($data['recommended_delivery'] == 'Delivery_ETA_ATA') {
  218. $returnData["RcommendDeliveryDate"] = array("Recommendradio" =>$data['recommended_delivery'],
  219. "RecommendCheckedList"=>$RecommendCheckedList,
  220. "RecommendCheckedAirList"=>$RecommendCheckedAirList,
  221. "RecommendCheckedSeaList"=>$RecommendCheckedSeaList);
  222. } else {
  223. $returnData["RcommendDeliveryDate"] = array();
  224. }
  225. $returnData["KLNPLCvalue"] = explode(";", $data['kln_pic']);
  226. global $_COPYRIGHT;
  227. $data = array('copyright' =>$_COPYRIGHT,
  228. 'returnData' =>$returnData);
  229. common::echo_json_encode(200,$data);
  230. exit();
  231. }
  232. if ($operate == "save"){
  233. $serial_no = common::deCode($_POST['a'], 'D');
  234. $country = common::check_input($_POST['country']);
  235. $station = empty($_POST['station']) ? "" :explode(";", $_POST['station']);
  236. $booking_window = common::check_input($_POST['booking_window']);
  237. $booking_window_date_start = common::check_input($_POST['booking_window_date_start']);
  238. $booking_window_date_end = common::check_input($_POST['booking_window_date_end']);
  239. $recommended_delivery = common::check_input($_POST['recommended_delivery']);
  240. $kln_pic = empty($_POST['kln_pic']) ? "" :explode(";", $_POST['kln_pic']);
  241. //rule
  242. $_mode_type = $_POST['mode_type'];
  243. $_priority = $_POST['priority'];
  244. $_rule_type = $_POST['rule_type'];
  245. $_ports = $_POST['ports'];
  246. $_carrier = $_POST['carrier'];
  247. $_recommended_delivery_from = $_POST['recommended_delivery_from'];
  248. $_recommended_delivery_to = $_POST['recommended_delivery_to'];
  249. $errmsg = "";
  250. $sql = "";
  251. if (!empty($serial_no)){
  252. $updateSqlSet = " modify_by = '"._getLoginName()."',update_time = now()";
  253. if (!empty($country)) {
  254. $updateSqlSet.= ", country = '$country' ";
  255. }
  256. if (!empty($station)) {
  257. $updateSqlSet.= ", station = '$station' ";
  258. }
  259. if (!empty($booking_window)) {
  260. $updateSqlSet.= ", booking_window = '$booking_window' ";
  261. }
  262. if (!empty($booking_window_date_start)) {
  263. $updateSqlSet.= ", booking_window_date_start = '$booking_window_date_start' ";
  264. }
  265. if (!empty($booking_window_date_end)) {
  266. $updateSqlSet.= ", booking_window_date_end = '$booking_window_date_end' ";
  267. }
  268. if (!empty($recommended_delivery)) {
  269. $updateSqlSet.= ", recommended_delivery = '$recommended_delivery' ";
  270. }
  271. if (!empty($kln_pic)) {
  272. $updateSqlSet.= ", kln_pic = '$kln_pic' ";
  273. }
  274. //代表update
  275. $sql .= "update public.kln_destination_delivery_config set ".$updateSqlSet."
  276. where serial_no = '$serial_no';";
  277. } else {
  278. //代表更新
  279. $serial_no = common::uuid();
  280. $sql .="INSERT INTO public.kln_destination_delivery_config(
  281. serial_no, country, station, booking_window, booking_window_date_start,
  282. booking_window_date_end, recommended_delivery, kln_pic, create_by,
  283. created_time, modify_by, update_time)
  284. VALUES ('$serial_no', '$country', '$station', '$booking_window', '$booking_window_date_start',
  285. '$booking_window_date_end', '$recommended_delivery', '$kln_pic',
  286. '"._getLoginName()."', now(), '"._getLoginName()."', now());";
  287. }
  288. if ($recommended_delivery == "delivery_eta_ata"){
  289. // 用于统计每种类型是否存在,以及是否满足 rule_type = 'all'
  290. $typeExists = ['air' => false, 'sea' => false];
  291. $typeHasAllRule = ['air' => false, 'sea' => false];
  292. foreach($_mode_type as $key => $v){
  293. $mode_type = $v;
  294. $priority = $_priority[$key];
  295. $rule_type = $_rule_type[$key];
  296. $ports = empty($_ports[$key]) ? "" :explode(";", $_ports[$key]);
  297. $carrier = empty($_carrier[$key]) ? "" :explode(";", $_carrier[$key]);
  298. $recommended_delivery_from = $_recommended_delivery_from[$key];
  299. $recommended_delivery_to = $_recommended_delivery_to[$key];
  300. // 只处理 air 和 sea
  301. if ($mode_type == 'air' || $mode_type == 'sea') {
  302. $typeExists[$mode_type] = true;
  303. if ($rule_type == 'all') {
  304. $typeHasAllRule[$mode_type] = true;
  305. }
  306. }
  307. //先删除,后添加
  308. $sql .= "delete from public.kln_destination_delivery_rule where recommended_delivery_serial_no = '$serial_no';";
  309. $sql .= "INSERT INTO public.kln_destination_delivery_rule(
  310. serial_no, recommended_delivery_serial_no, mode_type, priority,
  311. rule_type, ports, carrier, recommended_delivery_from, recommended_delivery_to,
  312. create_by, created_time, modify_by, update_time)
  313. VALUES ('$serial_no', '$mode_type', '$priority',
  314. '$rule_type', '$ports', '$carrier', '$recommended_delivery_from', '$recommended_delivery_to',
  315. '"._getLoginName()."', now(), '"._getLoginName()."', now());";
  316. }
  317. // 检查缺失的 rule_type = 'all'
  318. $missingTypes = [];
  319. foreach (['air', 'sea'] as $type) {
  320. if ($typeExists[$type] && !$typeHasAllRule[$type]) {
  321. $missingTypes[] = $type;
  322. }
  323. }
  324. if (!empty($missingTypes)) {
  325. $errmsg = "The Default Rule must exist.";
  326. }
  327. }
  328. if (empty($errmsg) && !empty($sql)){
  329. common::excuteUpdateSql($sql);
  330. $data = array("msg" =>"success");
  331. } else {
  332. $data = array("msg" =>$errmsg);
  333. }
  334. common::echo_json_encode(200,$data);
  335. exit();
  336. }
  337. }
  338. /**
  339. * booking
  340. */
  341. public function destination_delivery(){
  342. }
  343. }
  344. ?>