common.class.php 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302
  1. <?php
  2. if (!defined('IN_ONLINE')) {
  3. exit('Access Denied');
  4. }
  5. /**
  6. * Description of common
  7. *
  8. * @author Administrator
  9. */
  10. class common {
  11. /*
  12. * mysql check input
  13. */
  14. public static function check_input($value, $boolean = FALSE, $toupper = FALSE) {
  15. if ($boolean !== FALSE) {
  16. if ($value == "t")
  17. return "'t'";
  18. else
  19. return "'f'";
  20. }
  21. $value = trim($value);
  22. if ($toupper !== FALSE)
  23. $value = mb_strtoupper($value);
  24. if (!is_numeric($value)) {
  25. $value = pg_escape_string($value);
  26. }
  27. return $value;
  28. }
  29. public static function excuteOneSql($sql, $exception = FALSE) {
  30. if ($exception) {
  31. global $db;
  32. $result = $db->GetOne($sql);
  33. return $result === FALSE ? null : $result;
  34. } else {
  35. if (empty($sql))
  36. exit(json_encode("Program encountered an error."));
  37. global $db;
  38. $result = $db->GetOne($sql) or ( (!$db->ErrorMsg()) or error_log(common::dbLog($db, $sql), 0));
  39. return $result === FALSE ? null : $result;
  40. }
  41. }
  42. public static function excuteObjectSql($sql) {
  43. if (empty($sql))
  44. exit(json_encode("Program encountered an error."));
  45. global $db;
  46. $result = $db->GetRow($sql) or ( (!$db->ErrorMsg()) or error_log(common::dbLog($db, $sql), 0));
  47. return $result === FALSE ? null : $result;
  48. }
  49. public static function excuteUpdateSql($sql, $req_id = FALSE) {
  50. if (empty($sql))
  51. exit(json_encode("Program encountered an error."));
  52. global $db;
  53. $rs = $db->Execute($sql) or ( (!$db->ErrorMsg()) or error_log(common::dbLog($db, $sql), 0));
  54. if ($rs && $req_id !== FALSE)
  55. return $db->PO_Insert_ID();
  56. else
  57. return $rs;
  58. }
  59. public static function excuteListSql($sql, $exception = FALSE) {
  60. if ($exception) {
  61. global $db;
  62. $result = $db->GetAll($sql);
  63. return $result === FALSE ? null : $result;
  64. } else {
  65. if (empty($sql))
  66. exit(json_encode("Program encountered an error."));
  67. global $db;
  68. $result = $db->GetAll($sql) or ( (!$db->ErrorMsg()) or error_log(common::dbLog($db, $sql), 0));
  69. return $result === FALSE ? null : $result;
  70. }
  71. }
  72. /*
  73. * Database log output
  74. */
  75. public static function dbLog($db, $sql) {
  76. $backMsg = $db->errorMsg() . ' sql=' . $sql;
  77. return $backMsg;
  78. }
  79. /*
  80. * get IP
  81. */
  82. public static function ip() {
  83. if (getenv("HTTP_X_FORWARDED_FOR"))
  84. return getenv("HTTP_X_FORWARDED_FOR");
  85. if (getenv("HTTP_CLIENT_IP"))
  86. return getenv("HTTP_CLIENT_IP");
  87. if (getenv("REMOTE_ADDR"))
  88. return getenv("REMOTE_ADDR");
  89. if ($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])
  90. return $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
  91. if ($HTTP_SERVER_VARS["HTTP_CLIENT_IP"])
  92. return $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
  93. if ($HTTP_SERVER_VARS["REMOTE_ADDR"])
  94. return $HTTP_SERVER_VARS["REMOTE_ADDR"];
  95. return "Unknown";
  96. }
  97. public static function getDBUuid() {
  98. global $db;
  99. $sql = "select uuid_generate_v1()";
  100. $uuid = $db->GetOne($sql);
  101. return $uuid;
  102. }
  103. public static function securityCheckHandNew($action) {
  104. if (strpos($action, "handset") === 0) {
  105. } elseif (strpos($action, "hand") === 0) {
  106. } else {
  107. common::sessionVerify();
  108. $httpAccept = $_SERVER['HTTP_ACCEPT']; // ajax request,is json or html
  109. $ajax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER
  110. ['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'; // is ajax request?
  111. //暂时这么写,放开action = main的查询是为了在没有登录时获取信息。
  112. //tools是为了避免查询的用户,没有配置tools权限,但是新版mark_save需要进入
  113. if($action == "main" || $action == "tools" || $action == "save_layout"){
  114. return;
  115. }
  116. if (!isset($_SESSION['ONLINE_USER'])) {
  117. self::timeoutPrintInfor($httpAccept, $ajax, 'no');
  118. } else {
  119. $time = time() - $_SESSION["LAST_OPERATE_TIME"];
  120. $set_session_timeout = $_SESSION['SESSION_TIMEOUT'];
  121. if (_getLoginName() == "ra.admin") {
  122. $set_session_timeout = 4 * 3600;
  123. }
  124. if ($time > $set_session_timeout) {
  125. self::timeoutPrintInfor($httpAccept, $ajax, 'yes');
  126. } else {
  127. $_SESSION["LAST_OPERATE_TIME"] = time();
  128. if (!_isAdmin() && $_GET["action"] != "linkcrm") {
  129. if (!utils::checkExist($_SESSION['ONLINE_USER']['permission'], $action)
  130. && !(stripos($action, "main") === 0)
  131. && !(stripos($action, "user_guide") === 0)
  132. && !(stripos($action, "feature_update") === 0)
  133. && !(stripos($action, "ajax") === 0)
  134. && !(stripos($action, "opreation_log") === 0)
  135. && !(stripos($action, "system_setting") === 0)
  136. && !(stripos($action, "monitoring_setting") === 0)
  137. && !(stripos($action, "notifications_rules") === 0)) {
  138. $data = array("msg"=>"Permission Denied");
  139. common::echo_json_encode(500, $data);
  140. exit();
  141. }
  142. }
  143. }
  144. }
  145. session_write_close();
  146. }
  147. }
  148. public static function getCompanySearch() {
  149. $sc_list = _get_schemas();
  150. if (utils::count($sc_list) == 1) {
  151. return "";
  152. }
  153. if (_isCustomerLogin()) {
  154. $msg = '<input type="hidden" name="_apex_or_sfs" value=""/>';
  155. } else {
  156. $msg = '<div class="modules">Apex/SFS/Kerry :&nbsp;<select name="_apex_or_sfs" class="sl"><option value="">All</option>';
  157. if ($_SESSION['ONLINE_USER']['main_schemas'] == "public") {
  158. $msg .= '<option value="public">Apex</option>';
  159. } else {
  160. $msg .= '<option value="public">Apex</option>';
  161. }
  162. if ($_SESSION['ONLINE_USER']['main_schemas'] == "sfs") {
  163. $msg .= '<option value="sfs">SFS</option>';
  164. } else {
  165. $msg .= '<option value="sfs">SFS</option>';
  166. }
  167. if ($_SESSION['ONLINE_USER']['main_schemas'] == "kyiff") {
  168. $msg .= '<option value="kyiff">Kerry</option>';
  169. } else {
  170. $msg .= '<option value="kyiff">Kerry</option>';
  171. }
  172. $msg .= '</select></div>';
  173. }
  174. return $msg;
  175. }
  176. public static function sessionVerify() {
  177. if (!isset($_SESSION['user_agent'])) {
  178. $_SESSION['user_agent'] = MD5($_SERVER['REMOTE_ADDR']
  179. . $_SERVER['HTTP_USER_AGENT']);
  180. } elseif ($_SESSION['user_agent'] != MD5($_SERVER['REMOTE_ADDR']
  181. . $_SERVER['HTTP_USER_AGENT'])) {
  182. session_regenerate_id();
  183. }
  184. }
  185. public static function searchExtendHand_KLN($type, $user, $company_name = "station_name") {
  186. $sqlWhere = " 1=1";
  187. if($type == "booking"){
  188. $ocean_booking_sqlWhere = self::searchExtendHandNew("booking",$user, $company_name);
  189. $air_booking_sqlWhere = self::searchExtendHandNew("air_booking",$user, $company_name);
  190. $sqlWhere = "((transport_mode ='sea' and ".$ocean_booking_sqlWhere.") or (transport_mode ='air' and ".$air_booking_sqlWhere."))";
  191. }
  192. if($type == "ocean"){
  193. $ocean_booking_sqlWhere = self::searchExtendHandNew("ocean",$user, $company_name);
  194. $air_booking_sqlWhere = self::searchExtendHandNew("air",$user, $company_name);
  195. $sqlWhere = "((transport_mode ='sea' and ".$ocean_booking_sqlWhere.") or (transport_mode ='air' and ".$air_booking_sqlWhere."))";
  196. }
  197. return $sqlWhere;
  198. }
  199. public static function searchExtendHandNew($type, $user, $company_name = "station_name") {
  200. if (_isAdminHandNew($user)) {
  201. if(strtolower($type) == "air_booking" )
  202. {
  203. return "1=1";
  204. }
  205. return " (COALESCE(schem_not_display,false) = false)";
  206. }
  207. if (_isDocAdmin($user["user_login"])) {
  208. if(strtolower($type) == "air_booking" )
  209. {
  210. return "1=1";
  211. }
  212. return ' (COALESCE(schem_not_display,false) = false)';
  213. }
  214. if (strtolower($type) != "ocean" && strtolower($type) != "booking" && strtolower($type) != "air_booking"&& strtolower($type) != "air") {
  215. return " 1<>1";
  216. }
  217. if (empty($user["schemas_list"])) {
  218. $user["schemas_list"] = $_SESSION["schemas_list"];
  219. }
  220. $schemas_list = $user["schemas_list"];
  221. if ($user["is_kerry_shipment"] == "t") {
  222. $sqlWhere = " is_kerry_shipment is not null and is_kerry_shipment=true";
  223. } else if ($user["is_kerry_shipment"] == "f") {
  224. $sqlWhere = " (COALESCE(is_kerry_shipment,false) = false)";
  225. } else {
  226. $sqlWhere = " 1=1 ";
  227. }
  228. if (!empty($_POST["_apex_or_sfs"])) {
  229. $sqlWhere .= " and order_from='" . $_POST["_apex_or_sfs"] . "'";
  230. }
  231. if (utils::count($schemas_list) == 1) {
  232. $schames = $schemas_list[0]["schemas_name"];
  233. if(strtolower($type) == "air_booking" ||strtolower($type) == "air")
  234. {
  235. }
  236. else
  237. $sqlWhere .= " and order_from='$schames'";
  238. if ($schames == "sfs" && empty($user["sfs_ONLINE_USER"])) {
  239. $user = $_SESSION["sfs_ONLINE_USER"];
  240. }
  241. if (strtolower($type) == "ocean") {
  242. $sqlWhere .= self::_oceanHandNew($user, $schames);
  243. if ($company_name == "doc") {
  244. if (empty($user["view_file_format"])) {
  245. if (strtolower($user["user_type"]) == "customer") {
  246. $sqlWhere .= " and serial_no in (SELECT serial_no from $schames.ra_online_file_format where active = true and client_display = true)";
  247. } else {
  248. $sqlWhere .= " and serial_no in (SELECT serial_no from $schames.ra_online_file_format where active = true)";
  249. }
  250. } else {
  251. $sqlWhere .= " and serial_no in (SELECT regexp_split_to_table('" . $user["view_file_format"] . "',';'))";
  252. }
  253. }
  254. }
  255. if (strtolower($type) == "booking") {
  256. $sqlWhere .= self::_bookingHandNew($user, $schames);
  257. }
  258. if (strtolower($type) == "air_booking") {
  259. $sqlWhere .= self::_air($schames);
  260. }
  261. if (strtolower($type) == "air") {
  262. $sqlWhere .= self::_air($schames);
  263. }
  264. } elseif (utils::count($schemas_list) == 2) {
  265. if(strtolower($type) == "air_booking" ){
  266. }else{
  267. $sqlWhere .= " and (COALESCE(schem_not_display,false) = false)";
  268. }
  269. if (strtolower($type) == "ocean") {
  270. $sqlWhere .= " and ((order_from='public' ";
  271. $sqlWhere .= self::_oceanHandNew($user, "public");
  272. if ($company_name == "doc") {
  273. if (empty($user["view_file_format"])) {
  274. if (strtolower($user["user_type"]) == "customer") {
  275. $sqlWhere .= " and serial_no in (SELECT serial_no from public.ra_online_file_format where active = true and client_display = true)";
  276. } else {
  277. $sqlWhere .= " and serial_no in (SELECT serial_no from public.ra_online_file_format where active = true)";
  278. }
  279. } else {
  280. $sqlWhere .= " and serial_no in (SELECT regexp_split_to_table('" . $user["view_file_format"] . "',';'))";
  281. }
  282. }
  283. $sqlWhere .= ") or (order_from='sfs' ";
  284. if (empty($user["sfs_ONLINE_USER"])) {
  285. $user = $_SESSION["sfs_ONLINE_USER"];
  286. }
  287. $sqlWhere .= self::_oceanHandNew($user, "sfs");
  288. if ($company_name == "doc") {
  289. if (empty($user["view_file_format"])) {
  290. if (strtolower($user["user_type"]) == "customer") {
  291. $sqlWhere .= " and serial_no in (SELECT serial_no from sfs.ra_online_file_format where active = true and client_display = true)";
  292. } else {
  293. $sqlWhere .= " and serial_no in (SELECT serial_no from sfs.ra_online_file_format where active = true)";
  294. }
  295. } else {
  296. $sqlWhere .= " and serial_no in (SELECT regexp_split_to_table('" . $user["view_file_format"] . "',';'))";
  297. }
  298. }
  299. $sqlWhere .= ") )";
  300. }
  301. if (strtolower($type) == "booking") {
  302. $sqlWhere .= " and ((order_from='public' ";
  303. $sqlWhere .= self::_bookingHandNew($user, "public");
  304. $sqlWhere .= ") or (order_from='sfs' ";
  305. if (empty($user["sfs_ONLINE_USER"])) {
  306. $user = $_SESSION["sfs_ONLINE_USER"];
  307. }
  308. $sqlWhere .= self::_bookingHandNew($user, "sfs");
  309. $sqlWhere .= "))";
  310. }
  311. if (strtolower($type) == "air_booking") {
  312. $sqlWhere .= " and ((order_from='public' ";
  313. $sqlWhere .= self::_air("public");
  314. $sqlWhere .= ") or (order_from='sfs' ";
  315. if (empty($user["sfs_ONLINE_USER"])) {
  316. $user = $_SESSION["sfs_ONLINE_USER"];
  317. }
  318. $sqlWhere .= self::_air("sfs");
  319. $sqlWhere .= "))";
  320. }
  321. if (strtolower($type) == "air") {
  322. $sqlWhere .= " and ((order_from='public' ";
  323. $sqlWhere .= self::_air($user, "public");
  324. $sqlWhere .= ") or (order_from='sfs' ";
  325. if (empty($user["sfs_ONLINE_USER"])) {
  326. $user = $_SESSION["sfs_ONLINE_USER"];
  327. }
  328. $sqlWhere .= self::_air($user, "sfs");
  329. $sqlWhere .= ") )";
  330. }
  331. } else {
  332. $sqlWhere = " 1<>1";
  333. }
  334. return $sqlWhere;
  335. }
  336. public static function getStatusStage($status) {
  337. if ($status == "EE" || $status == "I") {
  338. return 0;
  339. }
  340. if ($status == "AE" || $status == "VD" || $status == "VA_RELAY" || $status == "UV_RELAY" || $status == "AE_RELAY" || $status == "VD_RELAY") {
  341. return 1;
  342. }
  343. if ($status == "VA" || $status == "UV" || $status == "AL" || $status == "AR" || $status == "CU" || $status == "CT" || $status == "CR" || $status == "OA") {
  344. return 2;
  345. }
  346. if ($status == "AV" || $status == "D" || $status == "RD") {
  347. return 3;
  348. }
  349. return -1;
  350. }
  351. public static function getInNotInSql($contact_id, $type = 'in') {
  352. if (empty($contact_id))
  353. return " =''";
  354. $contact_id = trim($contact_id);
  355. $contact_id = trim($contact_id, ";");
  356. $contact_id = trim($contact_id);
  357. $contact_id = strtolower($contact_id);
  358. if ($type == 'in') {
  359. if (utils::checkExist($contact_id, ";")) {
  360. $ss = "";
  361. $aa = explode(";", $contact_id);
  362. foreach ($aa as $k => $v) {
  363. $v = trim($v);
  364. if (empty($ss))
  365. $ss = "'" . common::check_input($v) . "'";
  366. else
  367. $ss .= ",'" . common::check_input($v) . "'";
  368. }
  369. return " in (" . $ss . ")";
  370. } else {
  371. return " = '" . common::check_input($contact_id) . "'";
  372. }
  373. } else {
  374. if (utils::checkExist($contact_id, ";")) {
  375. $ss = "";
  376. $aa = explode(";", $contact_id);
  377. foreach ($aa as $k => $v) {
  378. $v = trim($v);
  379. if (empty($ss))
  380. $ss = "'" . common::check_input($v) . "'";
  381. else
  382. $ss .= ",'" . common::check_input($v) . "'";
  383. }
  384. return " in (" . $ss . ")";
  385. } else {
  386. return " != '" . common::check_input($contact_id) . "'";
  387. }
  388. }
  389. }
  390. public static function getInNotInSqlForSearch($contact_id, $type = 'in') {
  391. if (empty($contact_id))
  392. return " =''";
  393. $contact_id = trim($contact_id);
  394. $contact_id = trim($contact_id, ";");
  395. $contact_id = trim($contact_id);
  396. $contact_id = strtolower($contact_id);
  397. if ($type == 'in') {
  398. if (utils::checkExist($contact_id, ";")) {
  399. $ss = "";
  400. $aa = explode(";", $contact_id);
  401. foreach ($aa as $k => $v) {
  402. $v = trim($v);
  403. if (empty($ss))
  404. $ss = "'" . common::check_input($v) . "'";
  405. else
  406. $ss .= ",'" . common::check_input($v) . "'";
  407. }
  408. return $ss;
  409. } else {
  410. return "'" . common::check_input($contact_id) . "'";
  411. }
  412. }
  413. }
  414. /*
  415. * Encrypt a SQL query statement used to be passed as a parameter to get excel output
  416. encode :DeCode('str','E');
  417. decode :DeCode('enstr','D');
  418. */
  419. public static function deCode($string, $operation = "E") {
  420. $key = md5("uls_webwms");
  421. $key_length = strlen($key);
  422. if ($operation == "D")
  423. $string = rawurldecode($string);
  424. $string = $operation == 'D' ? base64_decode($string) : substr(md5($string . $key), 0, 8) . $string;
  425. $string_length = strlen($string);
  426. $rndkey = $box = array();
  427. $result = '';
  428. for ($i = 0; $i <= 255; $i++) {
  429. $rndkey [$i] = ord($key [$i % $key_length]);
  430. $box [$i] = $i;
  431. }
  432. for ($j = $i = 0; $i < 256; $i++) {
  433. $j = ($j + $box [$i] + $rndkey [$i]) % 256;
  434. $tmp = $box [$i];
  435. $box [$i] = $box [$j];
  436. $box [$j] = $tmp;
  437. }
  438. for ($a = $j = $i = 0; $i < $string_length; $i++) {
  439. $a = ($a + 1) % 256;
  440. $j = ($j + $box [$a]) % 256;
  441. $tmp = $box [$a];
  442. $box [$a] = $box [$j];
  443. $box [$j] = $tmp;
  444. $result .= chr(ord($string [$i]) ^ ($box [($box [$a] + $box [$j]) % 256]));
  445. }
  446. if ($operation == 'D') {
  447. if (substr($result, 0, 8) == substr(md5(substr($result, 8) . $key), 0, 8)) {
  448. return substr($result, 8);
  449. } else {
  450. return '';
  451. }
  452. } else {
  453. return rawurlencode(str_replace('=', '', base64_encode($result)));
  454. }
  455. }
  456. public static function getStatusDesc($code) {
  457. if (strtoupper($code) == "EE") {
  458. return "Empty Equipment Dispatched";
  459. }
  460. if (strtoupper($code) == "I") {
  461. return "Gate in full for a booking";
  462. }
  463. if (strtoupper($code) == "AE") {
  464. return "Container loaded on vessel";
  465. }
  466. if (strtoupper($code) == "VD") {
  467. return "Vessel Departure";
  468. }
  469. if (strtoupper($code) == "VA_RELAY") {
  470. return "Arrive Relay Port";
  471. }
  472. if (strtoupper($code) == "VD_RELAY") {
  473. return "Depart Relay Port";
  474. }
  475. if (strtoupper($code) == "AE_RELAY") {
  476. return "Loaded at Relay Port";
  477. }
  478. if (strtoupper($code) == "UV_RELAY") {
  479. return "Unloaded at Relay Port";
  480. }
  481. if (strtoupper($code) == "VA") {
  482. return "Vessel Arrival";
  483. }
  484. if (strtoupper($code) == "UV") {
  485. return "Unloaded From Vessel";
  486. }
  487. if (strtoupper($code) == "AL") {
  488. return "Container loaded on Rail";
  489. }
  490. if (strtoupper($code) == "AR") {
  491. return "Container unloaded from Rail";
  492. }
  493. if (strtoupper($code) == "CU") {
  494. return "Carrier and Customs Release";
  495. }
  496. if (strtoupper($code) == "CT") {
  497. return "Customs release";
  498. }
  499. if (strtoupper($code) == "CR") {
  500. return "Carrier release";
  501. }
  502. if (strtoupper($code) == "OA") {
  503. return "Gate out full from final discharge port";
  504. }
  505. if (strtoupper($code) == "AV") {
  506. return "Shipment available for pickup or delivery";
  507. }
  508. if (strtoupper($code) == "RD") {
  509. return "Container returned empty";
  510. }
  511. if (strtoupper($code) == "D") {
  512. return "Gate out for delivery to customer";
  513. }
  514. }
  515. public static function _toString($msg) {
  516. if ($msg == "" || $msg == NULL) {
  517. return "";
  518. }
  519. return $msg . "";
  520. }
  521. /*
  522. * timeout output
  523. */
  524. public static function timeoutPrintInfor($httpAccept, $ajax, $login) {
  525. //记录系统因session过期 退出登录
  526. $data = array("msg"=>"session_time_out");
  527. self::echo_json_encode(403,$data);
  528. exit();
  529. }
  530. /**
  531. * _bookingHandNew 和 _oceanHandNew逻辑完全一样,只是ocean 多一个dest_op字段,在booking里没有的
  532. */
  533. private static function _oceanHandNew($user, $schemas = "public") {
  534. $o = $user['ocean_station'];
  535. $o_or = $user['ocean_station_or'];
  536. $d = $user['ocean_agent'];
  537. $d_or = $user['ocean_agent_or'];
  538. $sales = $user['ocean_sales'];
  539. $sales_or = $user['ocean_sales_or'];
  540. $op = $user['ocean_dest_op'];
  541. $op_or = $user['ocean_dest_op_or'];
  542. $follow = $user['ocean_following_sales'];
  543. $follow_or = $user['ocean_following_sales_or'];
  544. if (strtolower($o_or) == "all" || strtolower($d_or) == "all" || strtolower($sales_or) == "all" || strtolower($op_or) == "all") {
  545. return " and 1=1";
  546. }
  547. $sqlWhere = "";
  548. if (_isCustomerLoginHandNew($user)) {
  549. $sqlWhere .= " and " . _customerFilerSearchHandNew($user, $schemas);
  550. } else {
  551. if (empty($o) && empty($d) && empty($sales) && empty($op) && empty($follow)) {
  552. return " and 1<>1";
  553. }
  554. if ((strtolower($o) == 'all' || empty($o)) && (strtolower($d) == "all" || empty($d))) {
  555. } else {
  556. $sql = "1=1";
  557. if (!empty($o) && strtolower($o) != 'all') {
  558. $sql .= " and lower(origin)";
  559. $sql .= utils::getInSql($o);
  560. }
  561. if (!empty($d) && strtolower($d) != 'all') {
  562. $sql .= " and lower(agent)";
  563. $sql .= utils::getInSql($d);
  564. }
  565. $sqlWhere .= " and (" . $sql . ")";
  566. }
  567. if (strtolower($sales) == 'all' || empty($sales)) {
  568. } else {
  569. if (utils::checkExist($sales, ";")) {
  570. $sql = "1!=1";
  571. $tt = explode(";", $sales);
  572. foreach ($tt as $t) {
  573. $t = trim($t);
  574. if (!empty($t))
  575. $sql .= " or lower(sales_rep)='" . strtolower($t) . "'";
  576. }
  577. $sqlWhere .= " and ($sql)";
  578. } else
  579. $sqlWhere .= " and lower(sales_rep)='" . strtolower($sales) . "'";
  580. }
  581. if (strtolower($op) == 'all' || empty($op)) {
  582. } else {
  583. $sqlWhere .= " and lower(dest_op) " . utils::getInSql($op);
  584. }
  585. if (strtolower($follow) == 'all' || empty($follow)) {
  586. } else {
  587. if (utils::checkExist($follow, ";")) {
  588. $sql = "1!=1";
  589. $tt = explode(";", $follow);
  590. foreach ($tt as $t) {
  591. $t = trim($t);
  592. if (!empty($t))
  593. $sql .= " or following_sales ilike '" . $t . "'";
  594. }
  595. $sqlWhere .= " and ($sql)";
  596. } else
  597. $sqlWhere .= " and following_sales ilike '" . $follow . "'";
  598. }
  599. $sqlWhere = " (1=1 $sqlWhere)";
  600. if (!empty($o_or)) {
  601. $sqlWhere .= " or lower(origin)";
  602. $sqlWhere .= utils::getInSql($o_or);
  603. }
  604. if (!empty($d_or)) {
  605. $sqlWhere .= " or lower(agent)";
  606. $sqlWhere .= utils::getInSql($d_or);
  607. }
  608. if (!empty($sales_or)) {
  609. if (utils::checkExist($sales_or, ";")) {
  610. $sql = "1!=1";
  611. $tt = explode(";", $sales_or);
  612. foreach ($tt as $t) {
  613. $t = trim($t);
  614. if (!empty($t))
  615. $sql .= " or lower(sales_rep)='" . strtolower($t) . "'";
  616. }
  617. $sqlWhere .= " or ($sql)";
  618. } else
  619. $sqlWhere .= " or lower(sales_rep)='" . strtolower($sales_or) . "'";
  620. }
  621. if (!empty($op_or)) {
  622. $sqlWhere .= " or lower(dest_op) " . utils::getInSql($op_or);
  623. }
  624. if (!empty($follow_or)) {
  625. if (utils::checkExist($follow_or, ";")) {
  626. $sql = "1!=1";
  627. $tt = explode(";", $follow_or);
  628. foreach ($tt as $t) {
  629. $t = trim($t);
  630. if (!empty($t))
  631. $sql .= " or following_sales ilike '" . $t . "'";
  632. }
  633. $sqlWhere .= " or ($sql)";
  634. }else {
  635. $sqlWhere .= " or following_sales ilike '" . $follow_or . "'";
  636. }
  637. }
  638. $sqlWhere = " and ($sqlWhere)";
  639. }
  640. return $sqlWhere;
  641. }
  642. /**
  643. * _bookingHandNew 和 _oceanHandNew逻辑完全一样,只是ocean 多一个dest_op字段,在booking里没有的
  644. */
  645. private static function _bookingHandNew($user, $schames) {
  646. $o = $user['ocean_station'];
  647. $o_or = $user['ocean_station_or'];
  648. $d = $user['ocean_agent'];
  649. $d_or = $user['ocean_agent_or'];
  650. $sales = $user['ocean_sales'];
  651. $sales_or = $user['ocean_sales_or'];
  652. $op = $user['ocean_dest_op'];
  653. $op_or = $user['ocean_dest_op_or'];
  654. $follow = $user['ocean_following_sales'];
  655. $follow_or = $user['ocean_following_sales_or'];
  656. if (strtolower($o_or) == "all" || strtolower($d_or) == "all" || strtolower($sales_or) == "all") {
  657. return " and 1=1";
  658. }
  659. $sqlWhere = "";
  660. if (_isCustomerLoginHandNew($user)) {
  661. $sqlWhere .= " and " . _customerFilerSearchHandNew($user, $schames);
  662. } else {
  663. if (empty($o) && empty($d) && empty($sales) && empty($op) && empty($follow)) {
  664. return " and 1<>1";
  665. }
  666. if ((strtolower($o) == 'all' || empty($o)) && (strtolower($d) == "all" || empty($d))) {
  667. } else {
  668. $sql = "1=1";
  669. if (!empty($o) && strtolower($o) != 'all') {
  670. $sql .= " and lower(origin)";
  671. $sql .= utils::getInSql($o);
  672. }
  673. if (!empty($d) && strtolower($d) != 'all') {
  674. $sql .= " and lower(agent)";
  675. $sql .= utils::getInSql($d);
  676. }
  677. $sqlWhere .= " and (" . $sql . ")";
  678. }
  679. if (strtolower($sales) == 'all' || empty($sales)) {
  680. } else {
  681. if (utils::checkExist($sales, ";")) {
  682. $sql = "1!=1";
  683. $tt = explode(";", $sales);
  684. foreach ($tt as $t) {
  685. $t = trim($t);
  686. if (!empty($t))
  687. $sql .= " or lower(sales_rep)='" . strtolower($t) . "'";
  688. }
  689. $sqlWhere .= " and ($sql)";
  690. } else
  691. $sqlWhere .= " and lower(sales_rep)='" . strtolower($sales) . "'";
  692. }
  693. if (strtolower($follow) == 'all' || empty($follow)) {
  694. } else {
  695. if (utils::checkExist($follow, ";")) {
  696. $sql = "1!=1";
  697. $tt = explode(";", $follow);
  698. foreach ($tt as $t) {
  699. $t = trim($t);
  700. if (!empty($t))
  701. $sql .= " or following_sales ilike '" . $t . "%'";
  702. }
  703. $sqlWhere .= " and ($sql)";
  704. } else
  705. $sqlWhere .= " and following_sales ilike '" . $follow . "%'";
  706. }
  707. $sqlWhere = " (1=1 $sqlWhere)";
  708. if (!empty($o_or)) {
  709. $sqlWhere .= " or lower(origin)";
  710. $sqlWhere .= utils::getInSql($o_or);
  711. }
  712. if (!empty($d_or)) {
  713. $sqlWhere .= " or lower(agent)";
  714. $sqlWhere .= utils::getInSql($d_or);
  715. }
  716. if (!empty($sales_or)) {
  717. if (utils::checkExist($sales_or, ";")) {
  718. $sql = "1!=1";
  719. $tt = explode(";", $sales_or);
  720. foreach ($tt as $t) {
  721. $t = trim($t);
  722. if (!empty($t))
  723. $sql .= " or lower(sales_rep)='" . strtolower($t) . "'";
  724. }
  725. $sqlWhere .= " or ($sql)";
  726. } else
  727. $sqlWhere .= " or lower(sales_rep)='" . strtolower($sales_or) . "%'";
  728. }
  729. if (!empty($follow_or)) {
  730. if (utils::checkExist($follow_or, ";")) {
  731. $sql = "1!=1";
  732. $tt = explode(";", $follow_or);
  733. foreach ($tt as $t) {
  734. $t = trim($t);
  735. if (!empty($t))
  736. $sql .= " or following_sales ilike '" . $t . "%'";
  737. }
  738. $sqlWhere .= " or ($sql)";
  739. } else
  740. $sqlWhere .= " or following_sales ilike '" . $follow_or . "%'";
  741. }
  742. $sqlWhere = " and ($sqlWhere)";
  743. }
  744. return $sqlWhere;
  745. }
  746. /**
  747. * online air
  748. */
  749. private static function _air($schemas = "public") {
  750. $station = _getAirStation($schemas);
  751. $station_or = _getAirStationOr($schemas);
  752. $sales = _getAirSales($schemas);
  753. $sales_or = _getAirSalesOr($schemas);
  754. $sqlWhere = " 1=1 ";
  755. if (_isCustomerLogin()) {
  756. $sqlWhere .= " and " . _customerAirFilerSearch($schemas);
  757. } else {
  758. if (empty($station) && empty($sales)) {
  759. return "and 1<>1";
  760. }
  761. if (strtolower($station_or) == "all" || strtolower($sales_or) == "all")
  762. return " and ($sqlWhere)";
  763. if (strtolower($station) == 'all' || empty($station)) {
  764. } else {
  765. $sqlWhere .= " and (lower(origin)";
  766. $sqlWhere .= utils::getInSql($station);
  767. $sqlWhere .= " or lower(agent)";
  768. $sqlWhere .= utils::getInSql($station);
  769. $sqlWhere .= ")";
  770. }
  771. if (strtolower($sales) == 'all' || empty($sales)) {
  772. } else {
  773. $sqlWhere .= " and (lower(sales_rep) " . utils::getInSql($sales) . " or lower(following_sales) " . utils::getInSql($sales) . ")";
  774. }
  775. $sqlWhere = " ($sqlWhere)";
  776. if (!empty($station_or))
  777. $sqlWhere = "$sqlWhere or lower(origin)" . utils::getInSql($station_or) . " or lower(agent)" . utils::getInSql($station_or);
  778. if (!empty($sales_or))
  779. $sqlWhere = "$sqlWhere or lower(sales_rep)" . utils::getInSql($sales_or) . " or lower(following_sales)" . utils::getInSql($sales_or);
  780. }
  781. return " and ($sqlWhere)";
  782. }
  783. public static function isNewVersion() {
  784. if (utils::checkExist($_SERVER['PHP_SELF'], "main_new_version.php")) {
  785. return "_new";
  786. }
  787. return "";
  788. }
  789. public static function removeTopOceanOldVersionSpecialField($rss){
  790. $rss_bk = $rss;
  791. $new_arr = array();
  792. foreach ($rss_bk as $k => $v) {
  793. if (utils::startWith($v['database_column_name'], "__") || utils::startWith($v['database_column_name'], "___")){
  794. //unset($rss[$k]);
  795. }else{
  796. $new_arr[] = $v;
  797. }
  798. }
  799. return $new_arr;
  800. }
  801. public static function echo_json_encode($code,$data){
  802. $resData = array();
  803. $resData["code"] = $code;
  804. $resData["data"] =$data;
  805. //echo utils::jsonFiltration("null", "\"\"", json_encode($resData));
  806. echo json_encode($resData);
  807. }
  808. /*
  809. * MM/DD/YYYY To YYYY-MM-DD
  810. */
  811. public static function usDate2sqlDate($timestr) {
  812. if (empty($timestr))
  813. return '';
  814. $datearray = explode("/", $timestr);
  815. $m = $datearray [0];
  816. $d = $datearray [1];
  817. $y = $datearray [2];
  818. return $y . "-" . $m . "-" . $d;
  819. }
  820. public static function uuid() {
  821. return md5(uniqid("", TRUE) . mt_rand());
  822. }
  823. /*
  824. * YYYYMMDD To MM/DD/YYYY
  825. */
  826. public static function date2usdate($datestr) {
  827. if (empty($datestr))
  828. return '';
  829. $y = substr($datestr, 0, 4);
  830. $m = substr($datestr, 4, 2);
  831. $d = substr($datestr, 6, 2);
  832. return $m . "/" . $d . "/" . $y;
  833. }
  834. /*
  835. * date add some days
  836. */
  837. public static function addDays($date, $days) {
  838. $time = strtotime($date) + $days * 24 * 3600;
  839. return date('m/d/Y', $time);
  840. }
  841. /*
  842. * download file from file system
  843. */
  844. public static function download_file($filename, $display_name = null, $delete = FALSE, $files = NULL) {
  845. $filename = str_replace("/", DIRECTORY_SEPARATOR, $filename);
  846. $filename = str_replace("\\", DIRECTORY_SEPARATOR, $filename);
  847. if (!file_exists($filename))
  848. exit('File Not Exist');
  849. if (empty($display_name))
  850. $display_name = basename($filename);
  851. //$file = fopen($filename, "r");
  852. header_remove("Content-type");
  853. header("Content-type:" . self::getContentType($filename));
  854. header("Expires: 0");
  855. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  856. header("Pragma: can-cache");
  857. header("Accept-ranges:bytes");
  858. header("Accept-length:" . filesize($filename));
  859. header("Content-Disposition:attachment;filename=\"" . $display_name . "\"");
  860. //echo fread($file, filesize($filename));
  861. //fclose($file);
  862. //针对大文件,规定每次读取文件的字节数为4096字节,直接输出数据
  863. $read_buffer = 4096;
  864. $handle = fopen($filename, 'rb');
  865. //总的缓冲的字节数
  866. $sum_buffer = 0;
  867. $filesize = filesize($filename);
  868. //只要没到文件尾,就一直读取
  869. while (!feof($handle) && $sum_buffer < $filesize) {
  870. echo fread($handle, $read_buffer);
  871. $sum_buffer += $read_buffer;
  872. }
  873. //关闭句柄
  874. fclose($handle);
  875. if ($delete !== FALSE) {
  876. unlink($filename);
  877. }
  878. if (!empty($files)) {
  879. foreach ($files as $f) {
  880. unlink($f);
  881. }
  882. }
  883. }
  884. public static function getContentType($filename) {
  885. $extend = self::getExtendFilename($filename);
  886. $filetype = array(
  887. 'xls' => 'application/vnd.ms-excel',
  888. 'doc' => 'application/msword',
  889. 'gif' => 'image/gif',
  890. 'jpg' => 'image/jpeg',
  891. 'jpeg' => 'image/jpeg',
  892. 'jpe' => 'image/jpeg',
  893. 'bmp' => 'image/bmp',
  894. 'png' => 'image/png',
  895. 'tif' => 'image/tiff',
  896. 'pdf' => 'application/pdf',
  897. 'zip' => 'application/zip'
  898. );
  899. return $filetype[$extend];
  900. }
  901. /*
  902. * Get the file extension
  903. */
  904. public static function getExtendFilename($file_name) {
  905. $extend = pathinfo($file_name);
  906. $extend = strtolower($extend["extension"]);
  907. return $extend;
  908. }
  909. public static function getManagement(){
  910. //Management 自己配置需要创建表保存
  911. $user_management_data = common::excuteObjectSql("select management,dashboard_filter from ra_online_user where user_login = '"._getLoginName()."'");
  912. //为空,配置使用默认值
  913. $user_management = $user_management_data['management'];
  914. if(empty($user_management)){
  915. $Management = common::getdefaultManagement();
  916. }else{
  917. $Management = json_decode($user_management,true);
  918. foreach($Management as $key =>$val){
  919. $Management[$key]['id'] = intval($val['id']);
  920. $Management[$key]['switchValue'] = $val['switchValue'] == "true" ? true : false;
  921. }
  922. }
  923. $user_dashboard_filter = $user_management_data['dashboard_filter'];
  924. $dashboard_filter =array();
  925. if(!empty($user_dashboard_filter)){
  926. $dashboard_filter = json_decode($user_dashboard_filter,true);
  927. }else{
  928. $threeMonthsAgoTimestamp = strtotime('-3 months');
  929. $date_start = date('Y-m-d', $threeMonthsAgoTimestamp);
  930. $date_end = date('Y-m-d');
  931. $date_start_two = date('m/d/Y', $threeMonthsAgoTimestamp);
  932. $date_end_two = date('m/d/Y');
  933. //ETD
  934. $_12MonthsAgoTimestamp = strtotime('-11 months');
  935. $etd_date_start = date('Y-m', $_12MonthsAgoTimestamp);
  936. $etd_date_end = date('Y-m');
  937. $etd_date_start_two = date('m/Y', $_12MonthsAgoTimestamp);
  938. $etd_date_end_two = date('m/Y');
  939. //柜子
  940. $ctnr_date_start = date('Y-m', $_12MonthsAgoTimestamp);
  941. $ctnr_date_end = date('Y-m');
  942. $ctnr_date_start_two = date('m/Y', $_12MonthsAgoTimestamp);
  943. $ctnr_date_end_two = date('m/Y');
  944. //Top
  945. $top_date_start = date('Y-m-d', $_12MonthsAgoTimestamp);
  946. $top_date_end = date('Y-m-d');
  947. $top_date_start_two = date('m/d/Y', $_12MonthsAgoTimestamp);
  948. $top_date_end_two = date('m/d/Y');
  949. //KPIDefaulteData
  950. $dashboard_filter['KPIDefaulteData'] = array("transportation" =>array('All'),"date_type"=>"ETD","date_start"=>$date_start_two,"date_start_two"=>$date_start,
  951. "date_end"=>$date_end_two,"date_end_two"=>$date_end);
  952. //PendingDefaultData
  953. $dashboard_filter['PendingDefaultData'] = array("transportation" =>array('All'),"date_type"=>"ETD","date_start"=>"","date_start_two"=>"",
  954. "date_end"=>"","date_end_two"=>"");
  955. //RecentDefaultData
  956. $dashboard_filter['RecentDefaultData'] = array("transportation" =>array('All'),"date_type"=>"ETD","date_start"=>$date_start_two,"date_start_two"=>$date_start,
  957. "date_end"=>$date_end_two,"date_end_two"=>$date_end);
  958. //ETDDefaultData
  959. $dashboard_filter['ETDDefaultData'] = array("transportation" =>array('All'),"date_type"=>"ETA","date_start"=>$etd_date_start_two,"date_start_two"=>$etd_date_start,
  960. "date_end"=>$etd_date_end_two,"date_end_two"=>$etd_date_end);
  961. //ContainerefaultData
  962. $dashboard_filter['ContainerefaultData'] = array("transportation" =>array('Sea'),"date_type"=>"ETA","date_start"=>$ctnr_date_start_two,"date_start_two"=>$ctnr_date_start,
  963. "date_end"=>$ctnr_date_end_two,"date_end_two"=>$ctnr_date_end);
  964. //RevenueDefaultData
  965. if(_isRevenueDisplay()){
  966. $dashboard_filter['RevenueDefaultData'] = array("transportation" =>array('All'),"date_type"=>"Invoice Issue Date","date_start"=>$ctnr_date_start_two,"date_start_two"=>$ctnr_date_start,
  967. "date_end"=>$ctnr_date_end_two,"date_end_two"=>$ctnr_date_end);
  968. }
  969. //Top10faultData
  970. $dashboard_filter['Top10faultData'] = array("transportation" =>array('All'),"date_type"=>"ETA","date_start"=>$top_date_start_two,"date_start_two"=>$top_date_start,
  971. "date_end"=>$top_date_end_two,"date_end_two"=>$top_date_end);
  972. //OriginCo2Top10faultData
  973. $dashboard_filter['OriginCo2Top10faultData'] = array("transportation" =>array('All'),"date_type"=>"ETA","date_start"=>$top_date_start_two,"date_start_two"=>$top_date_start,
  974. "date_end"=>$top_date_end_two,"date_end_two"=>$top_date_end);
  975. //DestinationCo2Top10faultData
  976. $dashboard_filter['DestinationCo2Top10faultData'] = array("transportation" =>array('All'),"date_type"=>"ETA","date_start"=>$top_date_start_two,"date_start_two"=>$top_date_start,
  977. "date_end"=>$top_date_end_two,"date_end_two"=>$top_date_end);
  978. }
  979. return array("Management" => $Management,"dashboard_filter" =>$dashboard_filter);
  980. }
  981. public static function getItemStyle($type,$code){
  982. if($type == 'r1'){
  983. $data = array("0-20 Days" =>"#ffc594",
  984. "20-40 Days" =>"#ff9e4c",
  985. "40-60 Days" =>"#ff7500",
  986. "60-80 Days" =>"#ff3d00",
  987. "Over 80 Days" =>"#d50000");
  988. return $data[$code];
  989. }
  990. if($type == 'r4' || $type == 'r3' || $type == 'atd_r4' || $type == 'ata_r3'){
  991. $data = array("0 Day" =>"#ffc594",
  992. "Today" =>"#ffc594",
  993. "1-2 Days" =>"#ff9e4c",
  994. "3-6 Days" =>"#ff7500",
  995. "7 Days" =>"#ff3d00");
  996. return $data[$code];
  997. }
  998. if($type == 'r2' || utils::startWith($type,"co2e")){
  999. $data = array("45" =>"#FFE3CC",
  1000. "40" =>"#FFAC66",
  1001. "20" =>"#FF7500",
  1002. "air" =>"#FFE3CC",
  1003. "sea" =>"#FFAC66",
  1004. "road" =>"#FF7500");
  1005. return $data[$code];
  1006. }
  1007. if($type == "top"){
  1008. $data = array("1" =>"#FF7500",
  1009. "2" =>"#ff9033",
  1010. "3" =>"#ff9e4d",
  1011. "4" =>"#ffac66",
  1012. "5" =>"#ffba80",
  1013. "6" =>"#ffc899",
  1014. "7" =>"#ffd6b3",
  1015. "8" =>"#ffe3cc",
  1016. "9" =>"#fff1e6",
  1017. "10" =>"#fff1e6");
  1018. return $data[$code];
  1019. }
  1020. }
  1021. //处理返回原表数据格式
  1022. public static function mian_repot_do($value,$type,$totalValue){
  1023. $data = array();
  1024. $value_arr = json_decode($value,true);
  1025. //r1 是 ETD to ETA (Days)圆形图表数据结构返回
  1026. if($type == 'r1'){
  1027. $ETDList = array();
  1028. foreach($value_arr as $arr){
  1029. $color = common::getItemStyle($type,$arr['name']);
  1030. $ETDList[] = array("value" =>intval($arr['value']),"name" =>$arr['name'],"itemStyle" =>array("color" =>$color));
  1031. }
  1032. // 排序依据的键数组(这里我们用一个数字数组来表示)
  1033. $order = ["0-20 Days", "20-40 Days", "40-60 Days","60-80 Days","Over 80 Days"];
  1034. $ETDList = utils::arrayOrderBykeys($order,$ETDList,"name");
  1035. //$ETDList = array_reverse($ETDList);
  1036. $ETD_Title = "Total: $totalValue";
  1037. $data = array("ETDList" =>$ETDList,"ETD_Radius"=>array('50%','80%'),"ETD_Title" =>$ETD_Title,"download_name" => "ETD to ETA (Days)");
  1038. }
  1039. if($type == 'r4' || $type == 'r3'){
  1040. //重新整理一下数据,给UI一致
  1041. $data_kd = array();
  1042. foreach($value_arr as $arr){
  1043. if(stripos("Today", $arr['name']) !== false){
  1044. $color = common::getItemStyle($type,"Today");
  1045. if(empty($data_kd["0"])){
  1046. $data_kd["0"] = array("value" =>intval($arr['value']),"name" =>"≤1 Day","itemStyle" =>array("color" =>$color));
  1047. }else{
  1048. $data_kd["0"]["value"] = $data_kd["0"]["value"] + intval($arr['value']);
  1049. }
  1050. }
  1051. if(stripos("+1 Days/+2 Days", $arr['name']) !== false){
  1052. $color = common::getItemStyle($type,"1-2 Days");
  1053. if(empty($data_kd["1"])){
  1054. $data_kd["1"] = array("value" =>intval($arr['value']),"name" =>"1-2 Days","itemStyle" =>array("color" =>$color));
  1055. }else{
  1056. $data_kd["1"]["value"] = $data_kd["1"]["value"] + intval($arr['value']);
  1057. }
  1058. }
  1059. if(stripos("+3 Days/+4 Days/+5 Days/+6 Days", $arr['name']) !== false){
  1060. $color = common::getItemStyle($type,"3-6 Days");
  1061. if(empty($data_kd["2"])){
  1062. $data_kd["2"] = array("value" =>intval($arr['value']),"name" =>"3-6 Days","itemStyle" =>array("color" =>$color));
  1063. }else{
  1064. $data_kd["2"]["value"] = $data_kd["2"]["value"] + intval($arr['value']);
  1065. }
  1066. }
  1067. if(stripos("+7 Days/Over 7 Days", $arr['name']) !== false){
  1068. $color = common::getItemStyle($type,"7 Days");
  1069. if(empty($data_kd["3"])){
  1070. $data_kd["3"] = array("value" =>intval($arr['value']),"name" =>"≥7 Days","itemStyle" =>array("color" =>$color));
  1071. }else{
  1072. $data_kd["3"]["value"] = $data_kd["3"]["value"] + intval($arr['value']);
  1073. }
  1074. }
  1075. }
  1076. // 排序依据的键数组(这里我们用一个数字数组来表示)
  1077. $order = ["≤1 Day", "1-2 Days", "3-6 Days","≥7 Days"];
  1078. $ETDList = utils::arrayOrderBykeys($order,$data_kd,"name");
  1079. if($type == 'r4'){
  1080. $data = array("ETDList" =>$data_kd,"ETD_Radius"=>array('30%','50%'),"title1" =>"Departure","title2" =>"","download_name" => "Pending Departure");
  1081. }
  1082. if($type == 'r3'){
  1083. $data = array("ETDList" =>$data_kd,"ETD_Radius"=>array('30%','50%'),"title1" =>"Arrival","title2" =>"","download_name" => "Pending Arrival");
  1084. }
  1085. }
  1086. return $data;
  1087. }
  1088. //单独处理co2e bar
  1089. public static function getCo2eBar(){
  1090. //分担查询
  1091. include ONLINE_ROOT . 'libs' . DS . 'map_config.ini.php';
  1092. //新UI air sea road 目前只有sea
  1093. $type = $_REQUEST["r_type"];
  1094. $sqlWhere = ' and ' . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  1095. $sqlWhere = " " . $sqlWhere;
  1096. $date_type = strtolower($_REQUEST["date_type"]);
  1097. if (isset($_REQUEST['date_start']) && !empty($_REQUEST['date_start']))
  1098. $sqlWhere .= " and $date_type >= '" . common::usDate2sqlDate($_REQUEST['date_start']) . " 00:00:00'";
  1099. if (isset($_REQUEST['date_end']) && !empty($_REQUEST['date_end']))
  1100. $sqlWhere .= " and $date_type <= '" . common::usDate2sqlDate($_REQUEST['date_end']) . " 23:59:59'";
  1101. $transportation = $_REQUEST["transportation"];
  1102. if(!is_array($transportation)){
  1103. $transportation = array($transportation);
  1104. }
  1105. $mode_param = "";
  1106. if(utils::count($transportation) == 1 && strtolower($transportation[0]) == 'all' ){
  1107. $mode_param = "'sea','air','road'";
  1108. }else{
  1109. $transport_mode = utils::implode(';', $transportation);
  1110. $mode_param = common::getInNotInSqlForSearch($transport_mode);
  1111. }
  1112. $sqlWhere .= " and transport_mode in ($mode_param)";
  1113. $ContainerCount_Title = array();
  1114. //先查询总的排放量sea air road,确定排名后,在分别查询对应的sea air road
  1115. //这里的问题跟 top 一样 。总的排放量查询有问题。目前是总的排放量 暂时等于 sea的排放量
  1116. if ($type == "co2e_orgin"){
  1117. $co2e_shippr_sql = "select SUM(COALESCE(carbon_emission,0)) as catnum ,
  1118. sum(case when (1<>1 or (transport_mode='air')) then COALESCE(carbon_emission,0) else 0 end) as air_rc,
  1119. sum(case when (1<>1 or (transport_mode='sea')) then COALESCE(carbon_emission,0) else 0 end) as sea_rc,
  1120. sum(case when (1<>1 or (transport_mode='road')) then COALESCE(carbon_emission,0) else 0 end) as road_rc,
  1121. shippr_uncode as station from public.kln_ocean where 1=1 and COALESCE(shippr_uncode,'')<>'' and carbon_emission is not null $sqlWhere group by shippr_uncode order by catnum desc limit 10";
  1122. //$co2e_aLL = common::excuteListSql($co2e_shippr_sql);
  1123. $co2e_aLL = $mapdb->GetAll($co2e_shippr_sql) or ( (!$mapdb->ErrorMsg()) or error_log(common::dbLog($mapdb, $co2e_shippr_sql), 0));
  1124. }
  1125. if ($type == "co2e_destination"){
  1126. $co2e_consignee_sql = "select SUM(COALESCE(carbon_emission,0)) as catnum ,
  1127. sum(case when (1<>1 or (transport_mode='air')) then COALESCE(carbon_emission,0) else 0 end) as air_rc,
  1128. sum(case when (1<>1 or (transport_mode='sea')) then COALESCE(carbon_emission,0) else 0 end) as sea_rc,
  1129. sum(case when (1<>1 or (transport_mode='road')) then COALESCE(carbon_emission,0) else 0 end) as road_rc,
  1130. consignee_uncode as station from public.kln_ocean where 1=1 and COALESCE(consignee_uncode,'')<>'' and carbon_emission is not null $sqlWhere group by consignee_uncode order by catnum desc limit 10";
  1131. //$co2e_aLL = common::excuteListSql($co2e_consignee_sql);
  1132. $co2e_aLL = $mapdb->GetAll($co2e_consignee_sql) or ( (!$mapdb->ErrorMsg()) or error_log(common::dbLog($mapdb, $co2e_consignee_sql), 0));
  1133. }
  1134. //最大Y值
  1135. $maxY = 0;
  1136. $stations = array();
  1137. foreach($co2e_aLL as $val){
  1138. $ContainerCount_Title[] = $val['station'];
  1139. $maxY = $maxY > $val['catnum'] ? $maxY : $val['catnum'];
  1140. if(!empty($val['station'])){
  1141. $stations[] = $val['station'];
  1142. }
  1143. }
  1144. // $station_str = utils::implode(',',$stations);
  1145. $ContainerCounSeries = array();
  1146. $shipment_mode_arr = array("air","sea","road");
  1147. foreach($shipment_mode_arr as $_shipment_mode){
  1148. //类型有 sea air road,目前之类只查询sea的,其他为空
  1149. $shipment_mode = $_shipment_mode;
  1150. // $co2e = array();
  1151. if ($type == "co2e_orgin"){
  1152. // $co2e_shippr_sql_union = utils::_getSql($station_str,$type,$shipment_mode,$sqlWhere);
  1153. // if(!empty($co2e_shippr_sql_union)){
  1154. // $co2e = common::excuteListSql($co2e_shippr_sql_union);
  1155. // }
  1156. $download_name = "CO2e Emission by Origin (Top 10)";
  1157. }
  1158. if ($type == "co2e_destination"){
  1159. // $co2e_consignee_union = utils::_getSql($station_str,$type,$shipment_mode,$sqlWhere);
  1160. // if(!empty($co2e_consignee_union)){
  1161. // $co2e = common::excuteListSql($co2e_consignee_union);
  1162. // }
  1163. $download_name = "CO2e Emission by Destination (Top 10)";
  1164. }
  1165. $total = 0;
  1166. $max = 0;
  1167. $data = array();
  1168. foreach($co2e_aLL as $val){
  1169. $catnum = $shipment_mode."_rc";
  1170. $data[] = empty($val[$catnum])? 0 : round($val[$catnum], 4);
  1171. $total = $total + $val[$catnum];
  1172. $max = $max < $val[$catnum] ? $val[$catnum] : $max;
  1173. }
  1174. $ContainerCounSeries[$shipment_mode] = array("data"=>$data,"total"=>$total,"max"=>$max);
  1175. }
  1176. //处理返回时数据格式 其实不太需要计算最大值,在最开始总量查询的时候以及查询出来
  1177. $ContainerCounSeries_return = array();
  1178. foreach($ContainerCounSeries as $k =>$v){
  1179. $color = common::getItemStyle($type,$k);
  1180. $ContainerCounSeries_return[] = array("name"=>$k,"type"=>"bar","emphasis" => array("focus" =>"none"),
  1181. "stack" =>"总计","data" =>$v['data'],"itemStyle" =>array("color" =>$color),'barWidth'=>'40');
  1182. }
  1183. //处理返回原表数据格式
  1184. //计算刻度值 最小值是0,最大值是3000,刻度是500 interval
  1185. $interval = utils::calculateTicks(0,$maxY,10);
  1186. if($interval == 0){
  1187. //处理返回默认值
  1188. $interval = 1;
  1189. }
  1190. $returnData = array("ContainerCount_Title"=>"","ContainerCountList" =>$ContainerCount_Title,"ContainerCounSeries" =>$ContainerCounSeries_return,
  1191. "min" => 0,"Max" =>$interval*10,"interval" =>$interval,"download_name" => $download_name);
  1192. return $returnData;
  1193. }
  1194. public static function getTopBar(){
  1195. $sqlWhere = ' and ' . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  1196. $sqlWhere = " " . $sqlWhere;
  1197. $date_type = strtolower($_REQUEST["date_type"]);
  1198. if (isset($_REQUEST['date_start']) && !empty($_REQUEST['date_start']))
  1199. $sqlWhere .= " and $date_type >= '" . common::usDate2sqlDate($_REQUEST['date_start']) . " 00:00:00'";
  1200. if (isset($_REQUEST['date_end']) && !empty($_REQUEST['date_end']))
  1201. $sqlWhere .= " and $date_type <= '" . common::usDate2sqlDate($_REQUEST['date_end']) . " 23:59:59'";
  1202. $transportation = $_REQUEST["transportation"];
  1203. if(!is_array($transportation)){
  1204. $transportation = array($transportation);
  1205. }
  1206. $mode_param = "";
  1207. if(utils::count($transportation) == 1 && strtolower($transportation[0]) == 'all' ){
  1208. $mode_param = "'sea','air','road'";
  1209. }else{
  1210. $transport_mode = utils::implode(';', $transportation);
  1211. $mode_param = common::getInNotInSqlForSearch($transport_mode);
  1212. }
  1213. $sqlWhere .= " and transport_mode in ($mode_param)";
  1214. $toporiginType = "shippr_uncode";
  1215. $shippr_uncode_10_sql ="with aa as (select count(shippr_uncode) as num,shippr_uncode,
  1216. (array_agg(shipper_city))[1] AS shipper_city from public.kln_ocean
  1217. where 1=1 and COALESCE(shippr_uncode,'')<>'' $sqlWhere group by shippr_uncode order by num desc limit 10)
  1218. select aa.*,dd.* from aa
  1219. left join LATERAL ( select lon as lng, lat as lat,
  1220. '' as label, '' as infor, 3 as sort,
  1221. null::timestamp without time zone as stime,''::text as ptype
  1222. from vessel.vt_unlocode where (lat<=90 and lat>=-90) and (lon<=180 and lon>=-180) and lon is not null and lat is not null and uncode=aa.shippr_uncode ) dd on true";
  1223. //todo 这里预写个大概
  1224. //common::getTopBarSQL($toporiginType,$_REQUEST["transportation"]);
  1225. $shippr_uncode_10 = common::excuteListSql($shippr_uncode_10_sql);
  1226. //如果值没有:客户地址-->站点地址-->Port地址(POL/POD)
  1227. if(utils::count($shippr_uncode_10) == 1 && empty($shippr_uncode_10[0]["shippr_uncode"])){
  1228. $toporiginType = "fport_of_loading_un";
  1229. $shippr_uncode_10_sql = "with aa as (select count(fport_of_loading_un) as num,fport_of_loading_un as shippr_uncode
  1230. from public.kln_ocean where 1=1 and COALESCE(fport_of_loading_un,'')<>'' $sqlWhere group by fport_of_loading_un order by num desc limit 10)
  1231. select aa.*,dd.* from aa
  1232. left join LATERAL ( select lon as lng, lat as lat,
  1233. '' as label, '' as infor, 3 as sort,
  1234. null::timestamp without time zone as stime,''::text as ptype
  1235. from vessel.vt_unlocode where (lat<=90 and lat>=-90) and (lon<=180 and lon>=-180) and lon is not null and lat is not null and uncode=aa.fport_of_loading_un ) dd on true";
  1236. $shippr_uncode_10 = common::excuteListSql($shippr_uncode_10_sql);
  1237. }
  1238. //$toporiginType = "fport_of_loading_un";
  1239. $topdestinationinType = "consignee_uncode";
  1240. $consignee_uncode_10_sql = "with aa as (select count(consignee_uncode) as num,consignee_uncode,
  1241. (array_agg(consignee_city))[1] AS consignee_city
  1242. from public.kln_ocean where 1=1 and COALESCE(consignee_uncode,'')<>'' $sqlWhere group by consignee_uncode order by num desc limit 10)
  1243. select aa.*,dd.* from aa
  1244. left join LATERAL ( select lon as lng, lat as lat,
  1245. '' as label, '' as infor, 3 as sort,
  1246. null::timestamp without time zone as stime,''::text as ptype
  1247. from vessel.vt_unlocode where (lat<=90 and lat>=-90) and (lon<=180 and lon>=-180) and lon is not null and lat is not null and uncode=aa.consignee_uncode ) dd on true";
  1248. $consignee_uncode_10 = common::excuteListSql($consignee_uncode_10_sql);
  1249. //如果值没有:客户地址-->站点地址-->Port地址(POL/POD)
  1250. if(utils::count($consignee_uncode_10) == 1 && empty($consignee_uncode_10[0]["consignee_uncode"])){
  1251. $topdestinationinType = "mport_of_discharge_un";
  1252. $consignee_uncode_10_sql = "with aa as (select count(mport_of_discharge_un) as num,mport_of_discharge_un as consignee_uncode from public.kln_ocean where 1=1 and COALESCE(mport_of_discharge_un,'')<>'' $sqlWhere group by mport_of_discharge_un order by num desc limit 10)
  1253. select aa.*,dd.* from aa
  1254. left join LATERAL ( select lon as lng, lat as lat,
  1255. '' as label, '' as infor, 3 as sort,
  1256. null::timestamp without time zone as stime,''::text as ptype
  1257. from vessel.vt_unlocode where (lat<=90 and lat>=-90) and (lon<=180 and lon>=-180) and lon is not null and lat is not null and uncode=aa.mport_of_discharge_un ) dd on true";
  1258. $consignee_uncode_10 = common::excuteListSql($consignee_uncode_10_sql);
  1259. }
  1260. $toporigin = array();
  1261. $toporiginMap = array();
  1262. $i = 0;
  1263. $origiNunMax = 0;
  1264. foreach($shippr_uncode_10 as $orgin){
  1265. $i = $i + 1;
  1266. $map = array();
  1267. if(!empty($orgin['lat'])&&!empty($orgin['lng'])){
  1268. $map = array("lat" =>$orgin['lat'],"lng" =>$orgin['lng']);
  1269. }
  1270. //$json = '{"lng":121.8525,"lat":29.926545,"label":"'.$orgin['origin'].'","infor":"LAT KRABANG, THAILAND","sort":"0","stime":null,"ptype":"por"}';
  1271. //$map = json_decode($json,true);
  1272. if(!empty($map)){
  1273. $toporiginMap[] = array("qandl"=>array(floatval($map['lat']),floatval($map['lng'])),
  1274. "divIcon" => array("iconSize"=>0),
  1275. "name" =>$orgin['shippr_uncode'],
  1276. "color" =>common::getItemStyle("top",$i),
  1277. "value" =>$orgin['num'],
  1278. "textcolor" =>"#FFF");
  1279. }
  1280. //查询uncode,对应的city
  1281. $city = "";
  1282. if($toporiginType == "shippr_uncode"){
  1283. $city = $orgin['shipper_city'];
  1284. }
  1285. //$city 做假数据
  1286. $toporigin[] = array("name"=>$orgin['shippr_uncode'],"city_name"=>$city,"value"=>$orgin['num'],"color"=>common::getItemStyle("top",$i));
  1287. $origiNunMax = $origiNunMax < $orgin['num'] ? $orgin['num'] : $origiNunMax;
  1288. }
  1289. $topdestination = array();
  1290. $topdestinationinMap = array();
  1291. $i = 0;
  1292. $agentiNunMax = 0;
  1293. foreach($consignee_uncode_10 as $agent){
  1294. $i = $i + 1;
  1295. $map = array();
  1296. if(!empty($agent['lat'])&&!empty($agent['lng'])){
  1297. $map = array("lat" =>$agent['lat'],"lng" =>$agent['lng']);
  1298. }
  1299. // $json = '{"lng":"100.78594000","lat":"13.68521000","label":"'.$agent['agent'].'","infor":"LAT KRABANG, THAILAND","sort":"0","stime":null,"ptype":"por"}';
  1300. // $map = json_decode($json,true);
  1301. if(!empty($map)){
  1302. $topdestinationinMap[] = array("qandl"=>array(floatval($map['lat']),floatval($map['lng'])),
  1303. "divIcon" => array("iconSize"=>0),
  1304. "name" =>$agent['consignee_uncode'],
  1305. "color" =>common::getItemStyle("top",$i),
  1306. "value" =>$agent['num'],
  1307. "textcolor" =>"#FFF");
  1308. }
  1309. //查询uncode,对应的city
  1310. $city = "";
  1311. if($topdestinationinType == "consignee_uncode"){
  1312. $city = $agent['consignee_city'];
  1313. }
  1314. //$city 做假数据
  1315. $topdestination[] = array("name"=>$agent['consignee_uncode'],"city_name"=>$city,"value"=>$agent['num'],"color"=>common::getItemStyle("top",$i));
  1316. $agentiNunMax = $agentiNunMax < $agent['num'] ? $agent['num'] : $agentiNunMax;
  1317. }
  1318. //处理返回原表数据格式
  1319. $interval = utils::calculateTicks(0,$origiNunMax,10);
  1320. if($interval == 0){
  1321. //处理返回默认值
  1322. $interval = 1;
  1323. }
  1324. $dest_interval = utils::calculateTicks(0,$agentiNunMax,10);
  1325. if($dest_interval == 0){
  1326. //处理返回默认值
  1327. $dest_interval = 1;
  1328. }
  1329. $returnData = array("seller_data_list_origin"=>$toporigin,"toporiginMap"=>array_reverse($toporiginMap),"toporiginType"=>$toporiginType,
  1330. "seller_data_list_destination"=>$topdestination,"topdestinationinMap"=>array_reverse($topdestinationinMap),"topdestinationinType"=>$topdestinationinType,
  1331. "min" => 0,"Max" =>$interval*10,"interval" =>$interval,
  1332. "dest_min" => 0,"dest_Max" =>$dest_interval*10,"dest_interval" =>$dest_interval);
  1333. return $returnData;
  1334. }
  1335. public static function getRevenue(){
  1336. //$data = '{"bar_title":"Total: 0","barList":["DEC,23","JAN,24","FEB,24","MAR,24","APR,24","MAY,24","JUN,24","JUL,24","AUG,24","SEP,24","OCT,24","NOV,24"],"barSeries":[{"name":"USD","type":"bar","data":[1,1,1,1,1,1,1,1,1,1,1,1],"emphasis":{"disabled":true,"focus":"none"},"itemStyle":{"color":"#FF7500","borderRadius":6},"label":{"show":false,"color":"#646A73","position":"top","fontFamily":"Lato-Light","hideWhenMouseLeave":false}},{"name":"THB","type":"bar","data":[2,2,2,2,2,2,2,2,2,2,2,2],"emphasis":{"disabled":true,"focus":"none"},"itemStyle":{"color":"#FFAC66","borderRadius":6},"label":{"show":false,"color":"#646A73","position":"top","fontFamily":"Lato-Light","hideWhenMouseLeave":false}},{"name":"SGD","type":"bar","data":[3,3,3,3,3,3,3,3,3,3,3,3],"emphasis":{"disabled":true,"focus":"none"},"itemStyle":{"color":"#FFE3CC","borderRadius":6},"label":{"show":false,"color":"#646A73","position":"top","fontFamily":"Lato-Light","hideWhenMouseLeave":false}}],"Max":5,"interval":1,"download_name":"Rvenue","isShowTooltips":true}';
  1337. //return json_decode($data,true);
  1338. $date_from = null;
  1339. $date_to = null;
  1340. if (isset($_REQUEST['date_start']) && !empty($_REQUEST['date_start'])){
  1341. $date_start = common::dateFormatToYM($_REQUEST['date_start']);
  1342. $date_from = $date_start."-01";
  1343. }
  1344. if (isset($_REQUEST['date_end']) && !empty($_REQUEST['date_end'])){
  1345. $date_end = common::dateFormatToYM($_REQUEST['date_end']);
  1346. $date_to = date('Y-m-t', strtotime($date_end."-01"));
  1347. }
  1348. $param = '{"user_login":"'._getLoginName().'","report_type":"ALL","date_from":"'.$date_from.'","date_to":"'.$date_to.'"}';
  1349. $sql = "SELECT * FROM get_customer_revenue_report('$param');FETCH ALL FROM r2;";
  1350. error_log("Revenue_sql:".$sql);
  1351. $r2_data = common::excuteListSql($sql);
  1352. //$r2_data = common::excuteListSql("select * from _test_data_table");
  1353. $barList = array();
  1354. $groupedItems = array();
  1355. foreach ($r2_data as $item) {
  1356. if(!utils::in_array($item['month'],$barList)){
  1357. $barList[] = $item['month'];
  1358. }
  1359. $groupedItems[$item['currency']][] = $item;
  1360. }
  1361. //补齐所有货币的月份
  1362. foreach($groupedItems as $ckey =>$cvalueArr){
  1363. $currency = array();
  1364. foreach($barList as $month){
  1365. $currency[] = common::findCurrencyByMonth($month,$ckey,$cvalueArr);
  1366. }
  1367. $groupedItems[$ckey] = $currency;
  1368. }
  1369. //处理barSeries
  1370. $barSeries = array();
  1371. //先准备4种颜色
  1372. $color = array("#FF7500","#FFAC66","#FFE3CC");
  1373. $index = 0;
  1374. $max = 0;
  1375. foreach($groupedItems as $currency => $series){
  1376. $series_max = 0;
  1377. $num = $index % 3;
  1378. $index = $index +1;
  1379. $tem_series = array();
  1380. $tem_series["name"] = $currency;
  1381. $tem_series["type"] = "bar";
  1382. $tem_series["emphasis"] = array("disabled"=>true,"focus"=>"none");
  1383. $tem_series["itemStyle"] = array("color"=>$color[$num],"borderRadius"=>6);
  1384. $tem_series["label"] = array("show"=>false,"color"=>"#646A73","position"=>"top","fontFamily"=>"Lato-Light","hideWhenMouseLeave"=>false);
  1385. //准备series data
  1386. $data = array();
  1387. $total = 0;
  1388. foreach($series as $_series){
  1389. $invoice_amount = empty($_series['invoice_amount'])? 0 : round($_series['invoice_amount'], 4);
  1390. $data[]=$invoice_amount;
  1391. $total = $total + $invoice_amount;
  1392. $max = $max<$_series['invoice_amount'] ? $_series['invoice_amount'] : $max;
  1393. $series_max = $series_max<$_series['invoice_amount'] ? $_series['invoice_amount'] : $series_max;
  1394. }
  1395. //计算Y坐标的间隔 每种货币的
  1396. $interval = utils::calculateTicks(0,$series_max,5);
  1397. if($interval == 0){
  1398. //处理返回默认值
  1399. $interval = 2;
  1400. }
  1401. $tem_series["Max"] = $interval*5;
  1402. $tem_series["interval"] = $interval;
  1403. $tem_series["data"] = $data;
  1404. $tem_series["total"] = floatval(number_format($total, 4, '.', ''));
  1405. $barSeries[] = $tem_series;
  1406. }
  1407. //计算Y坐标的间隔
  1408. $interval = utils::calculateTicks(0,$max,5);
  1409. if($interval == 0){
  1410. //处理返回默认值
  1411. $interval = 2;
  1412. }
  1413. $revenueBarData = array("bar_title" =>"Total:","barList" => $barList,
  1414. "barSeries" =>$barSeries,"download_name"=>"Rvenue","isShowTooltips"=>true,"Max" =>$interval*5,"interval" =>$interval);
  1415. return $revenueBarData;
  1416. }
  1417. public static function findCurrencyByMonth($month,$ckey,$cvalueArr){
  1418. $data = array("month" =>$month,"currency"=>$ckey,"invoice_amount"=>0,"sort_month"=>"");
  1419. foreach($cvalueArr as $realmonth){
  1420. if($realmonth['month'] == $month){
  1421. $data = $realmonth;
  1422. }
  1423. }
  1424. return $data;
  1425. }
  1426. public static function getdefaultManagement(){
  1427. $Management = array();
  1428. $Management[] = array("id"=>1 ,
  1429. "title"=>"KPI",
  1430. "switchValue"=>true,
  1431. "text"=>"Pie chart showing figures of shipments KPI of Departure and Arrival.");
  1432. $Management[] = array("id"=>2 ,
  1433. "title"=>"Pending",
  1434. "switchValue"=>true,
  1435. "text"=>"Pie chart showing figures of shipments which are soon to depart/arrive (Calculated from ETD/ETA).");
  1436. $Management[] = array("id"=>3 ,
  1437. "title"=>"ETD to ETA (Days)",
  1438. "switchValue"=>true,
  1439. "text"=>"Pie chart showing figures of shipments which are soon to depart/arrive (Calculated from ETD/ETA).");
  1440. $Management[] = array("id"=>4 ,
  1441. "title"=>"Container Count",
  1442. "switchValue"=>true,
  1443. "text"=>"Pie chart showing figures of shipments which are soon to depart/arrive (Calculated from ETD/ETA).");
  1444. if(_isRevenueDisplay()){
  1445. $Management[] = array("id"=>5 ,
  1446. "title"=>"Revenue Spent",
  1447. "switchValue"=>true,
  1448. "text"=>"Revenue data for the past 12 months.");
  1449. }
  1450. $Management[] = array("id"=>6 ,
  1451. "title"=>"Top 10 Origin/Destination",
  1452. "switchValue"=>true,
  1453. "text"=>"Figure of the top 10 origin/destination.",
  1454. "title1"=>"Top 10 Origin",
  1455. "title2"=>"Top 10 Destination");
  1456. $Management[] = array("id"=>7 ,
  1457. "title"=>"CO2e Emission by Origin (Top 10)",
  1458. "switchValue"=>true,
  1459. "text"=>"Figure of the CO2e Emission by origin.");
  1460. $Management[] = array("id"=>8 ,
  1461. "title"=>"CO2e Emission by Destination (Top 10)",
  1462. "switchValue"=>true,
  1463. "text"=>"Figure of the CO2e Emission by destination.");
  1464. $Management[] = array("id"=>9 ,
  1465. "title"=>"Recent Status",
  1466. "switchValue"=>true,
  1467. "text"=>"A shipment list with latest status update on top.");
  1468. return $Management;
  1469. }
  1470. /**
  1471. * Destroy Session
  1472. */
  1473. public static function sessionDestroy() {
  1474. session_destroy();
  1475. setcookie(session_name(), '', time() - 3600);
  1476. $_SESSION = array();
  1477. }
  1478. public static function dateFormatToYM($dateString){
  1479. $date = DateTime::createFromFormat('m/Y', $dateString);
  1480. $formattedDate = $date->format('Y-m'); // YYYY-MM
  1481. return $formattedDate;
  1482. }
  1483. public static function retStationInfo($address_1,$address_2,$address_3,$address_4,$city,$state,$country,$zipcode){
  1484. $stationInfo = "";
  1485. if(!empty($address_1)){
  1486. $stationInfo .= $address_1;
  1487. }
  1488. if(!empty($address_2)){
  1489. $stationInfo .= " ".$address_2;
  1490. }
  1491. if(!empty($address_3)){
  1492. $stationInfo .= " ".$address_3;
  1493. }
  1494. if(!empty($address_4)){
  1495. $stationInfo .= " ".$address_4;
  1496. }
  1497. $temp_str = "";
  1498. if(!empty($city)){
  1499. $temp_str .= $city." ";
  1500. }
  1501. if(!empty($state)){
  1502. $temp_str .= $state." ";
  1503. }
  1504. if(!empty($zipcode)){
  1505. $temp_str .= $zipcode." ";
  1506. }
  1507. if(!empty($country)){
  1508. $temp_str .= $country." ";
  1509. }
  1510. if(!empty($temp_str)){
  1511. return $stationInfo." ".trim($temp_str);
  1512. }
  1513. return $stationInfo;
  1514. }
  1515. public static function getInsertSqlNull($table_name, $values) {
  1516. $field = "";
  1517. $value = "";
  1518. foreach ($values as $k => $v) {
  1519. if ($k == 'tmp' || $k == 'action' || $k == 'operate' || $k == 'x' || $k == 'y') {
  1520. continue;
  1521. }
  1522. if (is_array($v)) {
  1523. $v = utils::implode(",", $v);
  1524. }
  1525. if (empty($field)) {
  1526. $field = $k;
  1527. if (utils::checkExist($v, 'now()')) {
  1528. $value = $v;
  1529. } elseif ($v == null) {
  1530. $value .= 'null';
  1531. } elseif ($v == 'TRUE' || $v == 'FALSE') {
  1532. $value .= $v;
  1533. } else {
  1534. $value = '\'' . common::check_input($v) . '\'';
  1535. }
  1536. } else {
  1537. $field .= ',' . $k;
  1538. if (utils::checkExist($v, 'now()'))
  1539. $value .= ', now()';
  1540. elseif ($v == null) {
  1541. $value .= ',null';
  1542. } else if ($v == 'TRUE' || $v == 'FALSE') {
  1543. $value .= ',' . $v;
  1544. } else {
  1545. $value .= ', \'' . common::check_input($v) . '\'';
  1546. }
  1547. }
  1548. }
  1549. return 'insert into ' . $table_name . '(' . $field . ') values (' . $value . ')';
  1550. }
  1551. //密码规则验证
  1552. public static function checkPasswordRule($login, $new_password){
  1553. $sql="select item_value from config where item='passwordCheckRules'";
  1554. $rs = common::excuteObjectSql($sql);
  1555. $str = "";
  1556. if (!empty($rs)) {
  1557. $passwordCheckRules = json_decode($rs["item_value"],true);
  1558. //校验使用次数
  1559. if (!empty($passwordCheckRules["pastPasswordCheckNum"])) {
  1560. $sql = "select password from public.ra_online_user_password_history where lower(user_login)='".common::check_input(strtolower($login))."' order by id desc limit ".$passwordCheckRules["pastPasswordCheckNum"];
  1561. $passwords = common::excuteListSql($sql);
  1562. foreach ($passwords as $pwd) {
  1563. if ($pwd['password'] == $new_password) {
  1564. $str = "This password has been recently used";
  1565. }
  1566. }
  1567. }
  1568. if(empty($str)){
  1569. $str = utils::checkPassword($new_password,$passwordCheckRules,$login);
  1570. }
  1571. }else{
  1572. $str = utils::checkPassword($new_password);
  1573. }
  1574. return $str;
  1575. }
  1576. public static function getMilestonesInfo($ocean,$transport_mode,$_schemas,$EDI315TimeAndLocation=array()){
  1577. //Milestones info列名固定
  1578. $Milestones_column = array();
  1579. $Milestones_column[] = array("title" =>"Milestones","field" =>"milestones","formatter" =>"normal","type" =>"normal");
  1580. $Milestones_column[] = array("title" =>"Date Time","field" =>"date_time","formatter" =>"dateTime","type" =>"normal");
  1581. $Milestones_column[] = array("title" =>"Locations","field" =>"locations","formatter" =>"normal","type" =>"normal");
  1582. $Milestones_column[] = array("title" =>"Remarks","field" =>"remarks","formatter" =>"normal","type" =>"normal");
  1583. //EDI315 有关联查询,以那个为准。但是如果是手工录入或者不是EDI 进去的Milestone的情况,这以这个为准
  1584. $Milestones_NO_Mapping = common::getMilestones_NO_Mapping_Data($ocean);
  1585. //Milestones 数据信息待定
  1586. $Milestones_data = array();
  1587. if($transport_mode == 'sea'){
  1588. $Milestones_data_arr = common::excuteListSql("select sn.description,act_date||' '||COALESCE(act_time,'') as date_time,
  1589. remark,timezone,a.code
  1590. from public.ocean_milestone a
  1591. inner join public.customer_service_milestone_sno sn on sn.code=a.code and sn.type = 'sea'
  1592. where a.serial_no='".$ocean["serial_no"]."' and act_date is not null order by sn.sno asc");
  1593. }else{
  1594. $Milestones_data_arr = common::excuteListSql("select sn.description,act_date||' '||COALESCE(act_time,'') as date_time,
  1595. remark,timezone,a.code
  1596. from $_schemas.air_milestone a
  1597. inner join public.customer_service_milestone_sno sn on sn.code=a.code and sn.type = 'air'
  1598. where a.serial_no='".$ocean["serial_no"]."' and act_date is not null order by sn.sno asc");
  1599. }
  1600. foreach($Milestones_data_arr as $mda){
  1601. $timezone = "";
  1602. $IFFICC_locations ="";
  1603. if(!empty($EDI315TimeAndLocation[$mda['code']])){
  1604. $edi315Info = $EDI315TimeAndLocation[$mda['code']];
  1605. if(!empty($edi315Info)){
  1606. $timezone = $edi315Info['timezone'];
  1607. }
  1608. //IFFICC 比较特殊用EDI的locations
  1609. if($mda['code'] == "IFFICC"){
  1610. $IFFICC_locations = $edi315Info['location'];
  1611. }
  1612. }else{
  1613. $edi315Info = $Milestones_NO_Mapping[$mda['code']];
  1614. if(!empty($edi315Info)){
  1615. $timezone = $edi315Info['timezone'];
  1616. }
  1617. }
  1618. //locations 以配置的信息为准
  1619. $locations = "";
  1620. $edi315Info = $Milestones_NO_Mapping[$mda['code']];
  1621. if(!empty($edi315Info)){
  1622. $locations = $edi315Info['location'];
  1623. if($mda['code'] == "IFFICC" && !empty($IFFICC_locations)){
  1624. $locations = $IFFICC_locations;
  1625. }
  1626. }
  1627. $Milestones_data[] = array("milestones"=>$mda['description'],"date_time"=>$mda['date_time'],"timezone" =>$timezone,
  1628. "locations" => $locations, "remarks" =>$mda['remark']);
  1629. }
  1630. $Milestones = array("Milestones_column"=>$Milestones_column,"Milestones_data" =>$Milestones_data);
  1631. return $Milestones;
  1632. }
  1633. /**
  1634. * 查询uncode 对应的时区
  1635. */
  1636. public static function getCityPortsInfo($uncodes){
  1637. $data =array();
  1638. $sql = "select time_zone as timezonecode,uncode as ctrycitycode from public.city_timezone where lower(uncode) ". common::getInNotInSql($uncodes);
  1639. $uncode_rs = common::excuteListSql($sql);
  1640. foreach($uncode_rs as $value){
  1641. $data[$value['ctrycitycode']] = $value['timezonecode'];
  1642. }
  1643. return $data;
  1644. }
  1645. /**
  1646. * EDI315 有关联查询,以那个为准。但是如果是手工录入或者没有对应的情况,这以这个为准
  1647. */
  1648. public static function getMilestones_NO_Mapping_Data($ocean){
  1649. $Milestones_NO_Mapping = array();
  1650. //取origin $ocean['aa_city'] 不是必需,对应文件里未提location来源
  1651. $Milestones_NO_Mapping['IFFBCF'] = array("code"=>"origin","timezone"=>$ocean['aa_timezone'],"location"=>"");
  1652. $Milestones_NO_Mapping['IFFBCR'] = array("code"=>"origin","timezone"=>$ocean['aa_timezone'],"location"=>"");
  1653. $Milestones_NO_Mapping['IFFCPU'] = array("code"=>"origin","timezone"=>$ocean['aa_timezone'],"location"=>"");
  1654. $Milestones_NO_Mapping['IFFSIL'] = array("code"=>"origin","timezone"=>$ocean['aa_timezone'],"location"=>"");
  1655. $Milestones_NO_Mapping['IFFEDO'] = array("code"=>"origin","timezone"=>$ocean['aa_timezone'],"location"=>"");
  1656. $Milestones_NO_Mapping['IFFECC'] = array("code"=>"origin","timezone"=>$ocean['aa_timezone'],"location"=>"");
  1657. //Place of Receipt
  1658. $Milestones_NO_Mapping['IFFREC'] = array("code"=>"por/pol","timezone"=>$ocean['por_timezone'],"location"=>$ocean['por_uncity']);
  1659. $Milestones_NO_Mapping['IFFECP'] = array("code"=>"por/pol","timezone"=>$ocean['por_timezone'],"location"=>$ocean['por_uncity']);
  1660. //Port of loading
  1661. $Milestones_NO_Mapping['IFFONB'] = array("code"=>"por/pol","timezone"=>$ocean['pol_timezone'],"location"=>$ocean['pol_uncity']);
  1662. $Milestones_NO_Mapping['IFFDEP'] = array("code"=>"por/pol","timezone"=>$ocean['pol_timezone'],"location"=>$ocean['pol_uncity']);
  1663. //取destination $ocean['dd_city']不是必需
  1664. $Milestones_NO_Mapping['IFFCSN'] = array("code"=>"destination","timezone"=>$ocean['dd_timezone'],"location"=>"");
  1665. $Milestones_NO_Mapping['IFFIDO'] = array("code"=>"destination","timezone"=>$ocean['dd_timezone'],"location"=>"");
  1666. //取Port of discharge
  1667. $Milestones_NO_Mapping['IFFARR'] = array("code"=>"mpod","timezone"=>$ocean['mpod_timezone'],"location"=>$ocean['mpod_uncity']);
  1668. $Milestones_NO_Mapping['IFFUND'] = array("code"=>"mpod/pod/fd","timezone"=>$ocean['mpod_timezone'],"location"=>$ocean['mpod_uncity']);
  1669. $Milestones_NO_Mapping['IFFICC'] = array("code"=>"mpod","timezone"=>$ocean['mpod_timezone'],"location"=>$ocean['mpod_uncity']);
  1670. //Place of Delivery
  1671. $Milestones_NO_Mapping['IFFAFD'] = array("code"=>"pod","timezone"=>$ocean['pod_timezone'],"location"=>$ocean['pod_uncity']);
  1672. $Milestones_NO_Mapping['IFFCTA'] = array("code"=>"pod/mpod","timezone"=>$ocean['pod_timezone'],"location"=>$ocean['pod_uncity']);
  1673. $Milestones_NO_Mapping['IFFADW'] = array("code"=>"pod","timezone"=>$ocean['pod_timezone'],"location"=>$ocean['pod_uncity']);
  1674. $Milestones_NO_Mapping['IFFDDW'] = array("code"=>"pod","timezone"=>$ocean['pod_timezone'],"location"=>$ocean['pod_uncity']);
  1675. $Milestones_NO_Mapping['IFFPPD'] = array("code"=>"mpod/pod/fd","timezone"=>$ocean['pod_timezone'],"location"=>$ocean['pod_uncity']);
  1676. $Milestones_NO_Mapping['IFFECR'] = array("code"=>"mpod/pod/fd","timezone"=>$ocean['pod_timezone'],"location"=>$ocean['pod_uncity']);
  1677. //final_destination $ocean['fd_city']不是必需
  1678. $Milestones_NO_Mapping['IFFDEL'] = array("code"=>"fd","timezone"=>$ocean['_fd_timezone'],"location"=>"");
  1679. $Milestones_NO_Mapping['IFFHBL'] = array("code"=>"fd","timezone"=>$ocean['_fd_timezone'],"location"=>"");
  1680. return $Milestones_NO_Mapping;
  1681. }
  1682. /**
  1683. * 这个详细查询已经带模式了
  1684. */
  1685. public static function getEDI315StatusForDetail($serial_no){
  1686. $data = array();
  1687. $ctnr_sql = "SELECT oc.ctnr, oc.serial_no,oc.size FROM oc_container oc LEFT JOIN ocean o ON oc.serial_no = o.serial_no
  1688. WHERE o.serial_no='$serial_no'";
  1689. $ctnr_data = common::excuteListSql($ctnr_sql);
  1690. $containerStatusData = array();
  1691. $VD = "";
  1692. $VA = "";
  1693. $VD_RELAY = "";
  1694. $VA_RELAY = "";
  1695. foreach ($ctnr_data as $cd){
  1696. //存在柜号为空的数据情况
  1697. if(empty($cd['ctnr'])){
  1698. continue;
  1699. }
  1700. //(select uncity from public.ports where uncode = s.event_code) as uncity, 按新版的逻辑这个先取消掉
  1701. $ctnr_status_sql = "select s.source_id, s.event_base as event,
  1702. to_char(to_timestamp(s.event_date, 'YYYYMMDD'), 'YYYY-MM-DD') as eventdate,
  1703. to_char(to_timestamp(s.event_time, 'HH24MI'), 'HH24:MI') as eventtime,
  1704. e.description,s.event_type as eventtype,
  1705. s.event_code as eventcode, s.event_city as eventcity,
  1706. (select time_zone from public.city_timezone where uncode = s.event_code) as timezone,
  1707. s.event_city as uncity,
  1708. case when s.event_base ='I' or s.event_base ='TR' then 'IFFREC'::text
  1709. when s.event_base ='AE' then 'IFFONB'::text
  1710. when s.event_base ='VD' then 'IFFDEP'::text
  1711. when s.event_base ='EB' or s.event_base ='VA' then 'IFFARR'::text
  1712. when s.event_base ='UV' then 'IFFUND'::text
  1713. when s.event_base ='VA' then 'IFFAFD'::text
  1714. when s.event_base ='AV' then 'IFFCTA'::text
  1715. when s.event_base ='CT' then 'IFFICC'::text
  1716. when s.event_base ='OA' or s.event_base ='D' then 'IFFPPD'::text
  1717. when s.event_base ='RD' then 'IFFECR'::text
  1718. when s.event_base ='EE' then 'IFFECP'::text
  1719. else '' ::text
  1720. end as milestone_code
  1721. from public.ra_online_container_status_v s
  1722. left join public.ra_online_edi_event e on s.event_base = e.ra_name
  1723. where s.serial_no = '" . pg_escape_string($cd['serial_no']) . "'
  1724. and s.container_no = '" . pg_escape_string($cd['ctnr']) . "' and is_display = true
  1725. order by to_timestamp(s.event_date, 'YYYYMMDD') desc,
  1726. to_timestamp(s.event_time, 'HH24MI') desc,e.ra_order desc";
  1727. $ctnr_status = common::excuteListSql($ctnr_status_sql);
  1728. //Container_Status 新版只显示几个状态, CTNR# EE I AE VD VA
  1729. $container_status_column = array("EE","I","AE","VD","VA");
  1730. $containerStatusDataTemp = array();
  1731. $content = array();
  1732. //按顺序处理 新版只显示几个状态
  1733. //foreach($container_status_column as $status){
  1734. foreach($ctnr_status as $event){
  1735. //if(strtolower($event['event']) == strtolower($status)){
  1736. $eventdate = $event["eventdate"];
  1737. if (empty($event["source_id"]) || $event["source_id"]==0) {
  1738. $eventdate.= " " . $event["eventtime"];
  1739. }
  1740. $location_code = $event["eventcode"];
  1741. $content[] = array("code" =>$event['event'],"title" =>$event['description'],"date"=>$eventdate,"country"=>$location_code,"timezone"=>$event['timezone']);
  1742. if(strtolower($event['event']) == "vd" && !empty($eventdate)){
  1743. $VD = $eventdate;
  1744. }
  1745. if(strtolower($event['event']) == "va" && !empty($eventdate)){
  1746. $VA = $eventdate;
  1747. }
  1748. if(strtolower($event['event']) == strtolower("VD_RELAY") && !empty($eventdate)){
  1749. $VD_RELAY = $eventdate;
  1750. }
  1751. if(strtolower($event['event']) == strtolower("VA_RELAY") && !empty($eventdate)){
  1752. $VA_RELAY = $eventdate;
  1753. }
  1754. //}
  1755. }
  1756. //}
  1757. //记录所有的信息
  1758. $EDI315TimeAndLocation = array();
  1759. foreach($ctnr_status as $event){
  1760. if(!empty($EDI315TimeAndLocation['IFFARR']) && $EDI315TimeAndLocation['IFFARR']['code'] == "EB"){
  1761. //如果存在EB 的EB 的优先级最高
  1762. continue;
  1763. }
  1764. if(!empty($EDI315TimeAndLocation['IFFPPD']) && $EDI315TimeAndLocation['IFFARR']['code'] == "OA"){
  1765. //如果存在OA 的OA 的优先级最高
  1766. continue;
  1767. }
  1768. $EDI315TimeAndLocation[$event['milestone_code']] = array("code"=>$event['event'],"timezone"=>$event['timezone'],"location"=>$event['uncity']);
  1769. }
  1770. $containerStatusDataTemp['label'] = $cd['ctnr']." / ".$cd['size'];
  1771. $containerStatusDataTemp['content'] = $content;
  1772. $containerStatusData[] = $containerStatusDataTemp;
  1773. }
  1774. $data['containerStatusData'] = $containerStatusData;
  1775. $data['EDI315TimeAndLocation'] = $EDI315TimeAndLocation;
  1776. $data['VD'] = $VD;
  1777. $data['VA'] = $VA;
  1778. $data['VD_RELAY'] = $VD_RELAY;
  1779. $data['VA_RELAY'] = $VA_RELAY;
  1780. return $data;
  1781. }
  1782. public static function getEDI315Time($serial_no,$_schemas){
  1783. //Timezone From 来自于EDI315
  1784. $ctnr_sql = "SELECT oc.ctnr, oc.serial_no,oc.size FROM $_schemas.oc_container oc LEFT JOIN $_schemas.ocean o ON oc.serial_no = o.serial_no
  1785. WHERE o.serial_no='$serial_no'";
  1786. $ctnr_data = common::excuteListSql($ctnr_sql);
  1787. $EDI315TimeAndLocation = array();
  1788. foreach ($ctnr_data as $cd){
  1789. //存在柜号为空的数据情况
  1790. if(empty($cd['ctnr'])){
  1791. continue;
  1792. }
  1793. $ctnr_status_sql = "select
  1794. (select time_zone from public.city_timezone where uncode = s.event_code) as timezone,
  1795. case when s.event_base ='I' or s.event_base ='TR' then 'IFFREC'::text
  1796. when s.event_base ='AE' then 'IFFONB'::text
  1797. when s.event_base ='VD' then 'IFFDEP'::text
  1798. when s.event_base ='EB' or s.event_base ='VA' then 'IFFARR'::text
  1799. when s.event_base ='UV' then 'IFFUND'::text
  1800. when s.event_base ='VA' then 'IFFAFD'::text
  1801. when s.event_base ='AV' then 'IFFCTA'::text
  1802. when s.event_base ='CT' then 'IFFICC'::text
  1803. when s.event_base ='OA' or s.event_base ='D' then 'IFFPPD'::text
  1804. when s.event_base ='RD' then 'IFFECR'::text
  1805. when s.event_base ='EE' then 'IFFECP'::text
  1806. else '' ::text
  1807. end as milestone_code
  1808. from public.ra_online_container_status_v s
  1809. left join public.ra_online_edi_event e on s.event_base = e.ra_name
  1810. where s.serial_no = '" . pg_escape_string($cd['serial_no']) . "'
  1811. and s.container_no = '" . pg_escape_string($cd['ctnr']) . "' and is_display = true
  1812. order by to_timestamp(s.event_date, 'YYYYMMDD') asc,
  1813. to_timestamp(s.event_time, 'HH24MI') asc,e.ra_order asc";
  1814. $ctnr_status = common::excuteListSql($ctnr_status_sql);
  1815. //记录所有的信息、替换最后一个柜子
  1816. $EDI315TimeAndLocation = array();
  1817. foreach($ctnr_status as $event){
  1818. if(!empty($EDI315TimeAndLocation['IFFARR']) && $EDI315TimeAndLocation['IFFARR']['code'] == "EB"){
  1819. //如果存在EB 的EB 的优先级最高
  1820. continue;
  1821. }
  1822. if(!empty($EDI315TimeAndLocation['IFFPPD']) && $EDI315TimeAndLocation['IFFARR']['code'] == "OA"){
  1823. //如果存在OA 的OA 的优先级最高
  1824. continue;
  1825. }
  1826. $EDI315TimeAndLocation[$event['milestone_code']] = array("code"=>$event['event'],"timezone"=>$event['timezone']);
  1827. }
  1828. }
  1829. return $EDI315TimeAndLocation;
  1830. }
  1831. public static function getMilestoneTimeAndlocations($milestone_code,$transport_mode,$serial_no,$order_from){
  1832. $EDI315TimeAndLocation = array();
  1833. if($transport_mode == "sea"){
  1834. $ctnr_sql = "SELECT oc.ctnr, oc.serial_no,oc.size FROM $order_from.oc_container oc LEFT JOIN ocean o ON oc.serial_no = o.serial_no
  1835. WHERE o.serial_no='$serial_no'";
  1836. $ctnr_data = common::excuteListSql($ctnr_sql);
  1837. foreach ($ctnr_data as $cd){
  1838. //存在柜号为空的数据情况
  1839. if(empty($cd['ctnr'])){
  1840. continue;
  1841. }
  1842. $ctnr_status_sql = "select s.source_id, s.event_base as event,
  1843. to_char(to_timestamp(s.event_date, 'YYYYMMDD'), 'YYYY-MM-DD') as eventdate,
  1844. to_char(to_timestamp(s.event_time, 'HH24MI'), 'HH24:MI') as eventtime,
  1845. e.description,s.event_type as eventtype,
  1846. s.event_code as eventcode, s.event_city as eventcity,
  1847. (select time_zone from public.city_timezone where uncode = s.event_code) as timezone,
  1848. (select uncity from public.ports where uncode = s.event_code) as uncity,
  1849. case when s.event_base ='I' then 'IFFREC'::text
  1850. when s.event_base ='AE' then 'IFFONB'::text
  1851. when s.event_base ='VD' then 'IFFDEP'::text
  1852. when s.event_base ='EB' or s.event_base ='VA' then 'IFFARR'::text
  1853. when s.event_base ='UV' then 'IFFUND'::text
  1854. when s.event_base ='VA' then 'IFFAFD'::text
  1855. when s.event_base ='AV' then 'IFFCTA'::text
  1856. when s.event_base ='CT' then 'IFFICC'::text
  1857. when s.event_base ='OA' or s.event_base ='D' then 'IFFPPD'::text
  1858. when s.event_base ='EE' then 'IFFECP'::text
  1859. else '' ::text
  1860. end as milestone_code
  1861. from public.ra_online_container_status_v s
  1862. left join ra_online_edi_event e on s.event_base = e.ra_name
  1863. where s.serial_no = '" . pg_escape_string($cd['serial_no']) . "'
  1864. and s.container_no = '" . pg_escape_string($cd['ctnr']) . "' and is_display = true
  1865. order by to_timestamp(s.event_date, 'YYYYMMDD') asc,
  1866. to_timestamp(s.event_time, 'HH24MI') asc,e.ra_order asc";
  1867. $ctnr_status = common::excuteListSql($ctnr_status_sql);
  1868. //记录所有的信息
  1869. $EDI315TimeAndLocation = array();
  1870. foreach($ctnr_status as $event){
  1871. if(!empty($EDI315TimeAndLocation['IFFARR']) && $EDI315TimeAndLocation['IFFARR']['code'] == "EB"){
  1872. //如果存在EB 的EB 的优先级最高
  1873. continue;
  1874. }
  1875. if(!empty($EDI315TimeAndLocation['IFFPPD']) && $EDI315TimeAndLocation['IFFARR']['code'] == "OA"){
  1876. //如果存在OA 的OA 的优先级最高
  1877. continue;
  1878. }
  1879. $EDI315TimeAndLocation[$event['milestone_code']] = array("code"=>$event['event'],"timezone"=>$event['timezone'],"location"=>$event['uncity']);
  1880. }
  1881. }
  1882. }
  1883. //当前数据的所有time city 查询
  1884. $sql = common::trackingSql($serial_no,$order_from);
  1885. $data_arr = common::excuteListSql($sql);
  1886. $data = $data_arr[0];
  1887. //EDI315 有关联查询,以那个为准。但是如果是手工录入或者不是EDI 进去的Milestone的情况,这以这个为准
  1888. $Milestones_NO_Mapping = common::getMilestones_NO_Mapping_Data($data);
  1889. $timezone = "";
  1890. $IFFICC_locations ="";
  1891. if(!empty($EDI315TimeAndLocation[$milestone_code])){
  1892. $edi315Info = $EDI315TimeAndLocation[$milestone_code];
  1893. if(!empty($edi315Info)){
  1894. $timezone = $edi315Info['timezone'];
  1895. }
  1896. //IFFICC 比较特殊用EDI的locations
  1897. if($milestone_code == "IFFICC"){
  1898. $IFFICC_locations = $edi315Info['location'];
  1899. }
  1900. }else{
  1901. $edi315Info = $Milestones_NO_Mapping[$milestone_code];
  1902. if(!empty($edi315Info)){
  1903. $timezone = $edi315Info['timezone'];
  1904. }
  1905. }
  1906. //locations 以配置的信息为准
  1907. $locations = "";
  1908. $edi315Info = $Milestones_NO_Mapping[$milestone_code];
  1909. if(!empty($edi315Info)){
  1910. $locations = $edi315Info['location'];
  1911. if($milestone_code == "IFFICC" && !empty($IFFICC_locations)){
  1912. $locations = $IFFICC_locations;
  1913. }
  1914. }
  1915. return array("timezone" =>$timezone,"locations" =>$locations);
  1916. }
  1917. public static function trackingSql($serial_no,$order_from){
  1918. $_schemas = $order_from;
  1919. if($_schemas == 'public'){
  1920. $_schemas = "ocean";
  1921. }
  1922. $sql = "with o as(
  1923. SELECT oo.*,m_bol as _m_bol, h_bol as _h_bol,
  1924. (select time_zone from public.city_timezone where uncode = oo.fport_of_loading_un limit 1) as pol_timezone,
  1925. case when oo.transport_mode ='sea'
  1926. then (select uncity from $order_from.ports where uncode = oo.fport_of_loading_un limit 1)
  1927. else (select city from sfs.airport where coalesce(airport.country_abb,'')||airport.airport_code = oo.fport_of_loading_un limit 1)
  1928. end as pol_uncity,
  1929. (select time_zone from public.city_timezone where uncode = oo.mport_of_discharge_un limit 1) as mpod_timezone,
  1930. case when oo.transport_mode ='sea'
  1931. then (select uncity from $order_from.ports where uncode = oo.mport_of_discharge_un limit 1)
  1932. else (select city from sfs.airport where coalesce(airport.country_abb,'')||airport.airport_code = oo.mport_of_discharge_un limit 1)
  1933. end as mpod_uncity,
  1934. (select time_zone from public.city_timezone where uncode = oo.place_of_receipt_un limit 1) as por_timezone,
  1935. case when oo.transport_mode ='sea'
  1936. then (select uncity from $order_from.ports where uncode = oo.place_of_receipt_un limit 1)
  1937. else (select city from sfs.airport where coalesce(airport.country_abb,'')||airport.airport_code = oo.place_of_receipt_un limit 1)
  1938. end as por_uncity,
  1939. (select time_zone from public.city_timezone where uncode = oo.place_of_delivery_un limit 1) as pod_timezone,
  1940. case when oo.transport_mode ='sea'
  1941. then (select uncity from $order_from.ports where uncode = oo.place_of_delivery_un limit 1)
  1942. else (select city from sfs.airport where coalesce(airport.country_abb,'')||airport.airport_code = oo.place_of_delivery_un limit 1)
  1943. end as pod_uncity,
  1944. (select time_zone from public.city_timezone where uncode = oo.final_desination_uncode limit 1) as _fd_timezone,
  1945. case when oo.transport_mode ='sea'
  1946. then (select uncity from $order_from.ports where uncode = oo.final_desination_uncode limit 1)
  1947. else (select city from sfs.airport where coalesce(airport.country_abb,'')||airport.airport_code = oo.final_desination_uncode limit 1)
  1948. end as _pd_uncity,
  1949. CASE
  1950. WHEN ((m_iffbcf is not null or m_iffbcf is null) and m_iffcpu is null and m_iffrec is null and m_iffdep is null and m_iffarr is null and m_iffdel is null) THEN 'Created'::text
  1951. WHEN ((m_iffcpu is not null or m_iffrec is not null) and m_iffdep is null and m_iffarr is null and m_iffdel is null) THEN 'Cargo Received'::text
  1952. WHEN (m_iffdep is not null and m_iffarr is null and m_iffdel is null) THEN 'Departure'::text
  1953. WHEN (m_iffarr is not null and m_iffdel is null) THEN 'Arrived'::text
  1954. WHEN (m_iffdel is not null) THEN 'Completed'::text
  1955. ELSE 'Created'::text
  1956. END AS new_status
  1957. from public.kln_ocean oo where oo.serial_no = '" . $serial_no . "' and oo.order_from = '$order_from'
  1958. )
  1959. SELECT o.* ,sh.*, cn.*,aa.*,dd.*,fd.*
  1960. from o
  1961. LEFT JOIN LATERAL ( SELECT company as cn_company,
  1962. address_1 as cn_address_1,
  1963. address_2 as cn_address_2,
  1964. address_3 as cn_address_3,
  1965. address_4 as cn_address_4,
  1966. city as cn_city, state as cn_state, zipcode as cn_zipcode, country as cn_country,
  1967. phone_1 as cn_phone
  1968. FROM $_schemas.contacts c WHERE o.consignee_id::text = c.contact_id::text) cn ON true
  1969. LEFT JOIN LATERAL ( SELECT company as sh_company,
  1970. address_1 as sh_address_1,
  1971. address_2 as sh_address_2,
  1972. address_3 as sh_address_3,
  1973. address_4 as sh_address_4,
  1974. city as sh_city, state as sh_state, zipcode as sh_zipcode, country as sh_country,
  1975. phone_1 as sh_phone
  1976. FROM $_schemas.contacts c WHERE o.shipper_id::text = c.contact_id::text) sh ON true
  1977. LEFT JOIN LATERAL ( SELECT company as aa_company,
  1978. address_1 as aa_address_1,
  1979. address_2 as aa_address_2,
  1980. address_3 as aa_address_3,
  1981. address_4 as aa_address_4,
  1982. city as aa_city, state as aa_state, zipcode as aa_zipcode, country as aa_country,
  1983. phone_1 as aa_phone,
  1984. (select time_zone from public.city_timezone where uncode = LEFT(c.country, 2) || COALESCE(c.city_code,'') limit 1) as aa_timezone
  1985. FROM $_schemas.contacts c WHERE o.origin::text = c.contact_id::text) aa ON true
  1986. LEFT JOIN LATERAL ( SELECT company as dd_company,
  1987. address_1 as dd_address_1,
  1988. address_2 as dd_address_2,
  1989. address_3 as dd_address_3,
  1990. address_4 as dd_address_4,
  1991. city as dd_city, state as dd_state, zipcode as dd_zipcode, country as dd_country,
  1992. phone_1 as dd_phone,
  1993. (select time_zone from public.city_timezone where uncode = LEFT(c.country, 2) || COALESCE(c.city_code,'') limit 1) as dd_timezone
  1994. FROM $_schemas.contacts c WHERE o.agent::text = c.contact_id::text) dd ON true
  1995. LEFT JOIN LATERAL ( SELECT
  1996. city as fd_city,
  1997. (select time_zone from public.city_timezone where uncode = LEFT(c.country, 2) || COALESCE(c.city_code,'') limit 1) as fd_timezone
  1998. FROM $_schemas.contacts c WHERE o.final_desination::text = c.contact_id::text) fd ON true";
  1999. return $sql;
  2000. }
  2001. public static function getDashboardTransportationSqlWhere(){
  2002. $transportation = $_REQUEST["transportation"];
  2003. if(!is_array($transportation)){
  2004. $transportation = array($transportation);
  2005. }
  2006. $mode_param = "";
  2007. if(utils::count($transportation) == 1 && strtolower($transportation[0]) == 'all' ){
  2008. $mode_param = "'sea','air','road'";
  2009. }else{
  2010. $transport_mode = utils::implode(';', $transportation);
  2011. $mode_param = common::getInNotInSqlForSearch($transport_mode);
  2012. }
  2013. return " and transport_mode in ($mode_param)";
  2014. }
  2015. /*
  2016. * Create Directory
  2017. */
  2018. public static function mkdirs($path, $mode = 0777) { //creates directory tree recursively
  2019. if (!file_exists($path)) {
  2020. if (!mkdir($path, $mode, TRUE))
  2021. return FALSE;
  2022. }
  2023. return TRUE;
  2024. }
  2025. /**
  2026. * checked is_subscribe
  2027. */
  2028. public static function checkedSubscribe($serial_no) { //creates directory tree recursively
  2029. $is_subscribe_exist = common::excuteOneSql("select user_login from public.kln_user_subscribed us
  2030. where lower(us.user_login) = '".strtolower(_getLoginName())."' and us.subscribed_serial_no = '".$serial_no."'");
  2031. if(empty($is_subscribe_exist)){
  2032. $is_subscribe = false;
  2033. }else{
  2034. $is_subscribe = true;
  2035. }
  2036. return $is_subscribe;
  2037. }
  2038. public static function getWeek($week){
  2039. if ($week == "0"){
  2040. $week = "Sunday";
  2041. } elseif ($week == "1"){
  2042. $week = "Monday";
  2043. } elseif ($week == "2"){
  2044. $week = "Tuesday";
  2045. } elseif ($week == "3"){
  2046. $week = "Wednesday";
  2047. } elseif ($week == "4"){
  2048. $week = "Thursday";
  2049. } elseif ($week == "5"){
  2050. $week = "Friday";
  2051. } elseif ($week == "6"){
  2052. $week = "Sunday";
  2053. }
  2054. return $week;
  2055. }
  2056. /**
  2057. * 这里基准event 写死, 根据online查询页面的通用的来, 这里需提问确定
  2058. */
  2059. public static function getEDICtnrEvent(){
  2060. $event =array(array("event_name"=>"EE","description"=>"Empty Equipment Dispatched"),
  2061. array("event_name"=>"I","description"=>"Gate in full for a booking"),
  2062. array("event_name"=>"AE","description"=>"Container loaded on vessel"),
  2063. array("event_name"=>"VD","description"=>"Vessel Departure"),
  2064. array("event_name"=>"VA_RELAY","description"=>"Arrive Relay Port"),
  2065. array("event_name"=>"UV_RELAY","description"=>"Unloaded at Relay Port"),
  2066. array("event_name"=>"AE_RELAY","description"=>"Loaded at Relay Port"),
  2067. array("event_name"=>"VD_RELAY","description"=>"Depart Relay Port"),
  2068. array("event_name"=>"CU","description"=>"Carrier and Customs Release"),
  2069. array("event_name"=>"CT","description"=>"Customs release"),
  2070. array("event_name"=>"CR","description"=>"Carrier release"),
  2071. array("event_name"=>"VA","description"=>"Vessel Arrival"),
  2072. array("event_name"=>"UV","description"=>"Unloaded From Vessel"),
  2073. array("event_name"=>"AG","description"=>"Estimated Delivery"),
  2074. array("event_name"=>"OA","description"=>"Gate out full from final discharge port"),
  2075. array("event_name"=>"FT","description"=>"Free Time Expired"),
  2076. array("event_name"=>"AL","description"=>"Container loaded on Rail"),
  2077. array("event_name"=>"AR","description"=>"Container unloaded from Rail"),
  2078. array("event_name"=>"AV","description"=>"Shipment available for pickup or delivery"),
  2079. array("event_name"=>"D","description"=>"Gate out for delivery to customer"),
  2080. array("event_name"=>"RD","description"=>"Container returned empty"),
  2081. array("event_name"=>"C","description"=>"Vessel Estimated Time of Departure"),
  2082. array("event_name"=>"C_RELAY","description"=>"Estimated Time of Departure from Tranship Port"),
  2083. array("event_name"=>"AG_DES","description"=>"Estimated Delivery Destination"),
  2084. array("event_name"=>"IFFADW","description"=>"Shipment in CFS warehouse"),
  2085. array("event_name"=>"IFFDDW","description"=>"Shipment Out from CFS House"));
  2086. return $event;
  2087. }
  2088. /**
  2089. * 处理daily 和 week 不同周或者天的数据分组
  2090. */
  2091. public static function handleDailyWeekedData($notificationsArr){
  2092. $uniqe = array();
  2093. $ret = array();
  2094. foreach($notificationsArr as $info){
  2095. $uniqe_group_str = $info['insert_date_format'];
  2096. if(utils::in_array($uniqe_group_str,$uniqe)){
  2097. $tempArr = $ret[$uniqe_group_str];
  2098. $tempArr[] = $info;
  2099. $ret[$uniqe_group_str] = $tempArr;
  2100. } else {
  2101. $ret[$uniqe_group_str] = array($info);
  2102. $uniqe[] = $uniqe_group_str;
  2103. }
  2104. }
  2105. return $ret;
  2106. }
  2107. /**
  2108. * /unit=="Day(s)" ? "days":"hours";
  2109. */
  2110. public static function convertoVue($unit){
  2111. if(empty($unit))
  2112. return $unit;
  2113. if ($unit == "days"){
  2114. return "Day(s)";
  2115. }else{
  2116. return "Hour(s)";
  2117. }
  2118. }
  2119. /**
  2120. * vue to int
  2121. */
  2122. public static function convertoint($num){
  2123. if($num == "0"){
  2124. return intval($num);
  2125. }
  2126. return !empty($num) ? intval($num): $num;
  2127. }
  2128. }
  2129. ?>