common.class.php 104 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249
  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. $ETDList = array_reverse($ETDList);
  1033. $ETD_Title = "Total: $totalValue";
  1034. $data = array("ETDList" =>$ETDList,"ETD_Radius"=>array('50%','80%'),"ETD_Title" =>$ETD_Title,"download_name" => "ETD to ETA (Days)");
  1035. }
  1036. if($type == 'r4' || $type == 'r3'){
  1037. //重新整理一下数据,给UI一致
  1038. $data_kd = array();
  1039. foreach($value_arr as $arr){
  1040. if(stripos("Today", $arr['name']) !== false){
  1041. $color = common::getItemStyle($type,"Today");
  1042. if(empty($data_kd["0"])){
  1043. $data_kd["0"] = array("value" =>intval($arr['value']),"name" =>"≤1 Day","itemStyle" =>array("color" =>$color));
  1044. }else{
  1045. $data_kd["0"]["value"] = $data_kd["0"]["value"] + intval($arr['value']);
  1046. }
  1047. }
  1048. if(stripos("+1 Days/+2 Days", $arr['name']) !== false){
  1049. $color = common::getItemStyle($type,"1-2 Days");
  1050. if(empty($data_kd["1"])){
  1051. $data_kd["1"] = array("value" =>intval($arr['value']),"name" =>"1-2 Days","itemStyle" =>array("color" =>$color));
  1052. }else{
  1053. $data_kd["1"]["value"] = $data_kd["1"]["value"] + intval($arr['value']);
  1054. }
  1055. }
  1056. if(stripos("+3 Days/+4 Days/+5 Days/+6 Days", $arr['name']) !== false){
  1057. $color = common::getItemStyle($type,"3-6 Days");
  1058. if(empty($data_kd["2"])){
  1059. $data_kd["2"] = array("value" =>intval($arr['value']),"name" =>"3-6 Days","itemStyle" =>array("color" =>$color));
  1060. }else{
  1061. $data_kd["2"]["value"] = $data_kd["2"]["value"] + intval($arr['value']);
  1062. }
  1063. }
  1064. if(stripos("+7 Days/Over 7 Days", $arr['name']) !== false){
  1065. $color = common::getItemStyle($type,"7 Days");
  1066. if(empty($data_kd["3"])){
  1067. $data_kd["3"] = array("value" =>intval($arr['value']),"name" =>"≥7 Days","itemStyle" =>array("color" =>$color));
  1068. }else{
  1069. $data_kd["3"]["value"] = $data_kd["3"]["value"] + intval($arr['value']);
  1070. }
  1071. }
  1072. }
  1073. if($type == 'r4'){
  1074. $data = array("ETDList" =>$data_kd,"ETD_Radius"=>array('30%','50%'),"title1" =>"Departure","title2" =>"","download_name" => "Pending Departure");
  1075. }
  1076. if($type == 'r3'){
  1077. $data = array("ETDList" =>$data_kd,"ETD_Radius"=>array('30%','50%'),"title1" =>"Arrival","title2" =>"","download_name" => "Pending Arrival");
  1078. }
  1079. }
  1080. return $data;
  1081. }
  1082. //单独处理co2e bar
  1083. public static function getCo2eBar(){
  1084. //新UI air sea road 目前只有sea
  1085. $type = $_REQUEST["r_type"];
  1086. $sqlWhere = ' and ' . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  1087. $sqlWhere = " " . $sqlWhere;
  1088. $date_type = strtolower($_REQUEST["date_type"]);
  1089. if (isset($_REQUEST['date_start']) && !empty($_REQUEST['date_start']))
  1090. $sqlWhere .= " and $date_type >= '" . common::usDate2sqlDate($_REQUEST['date_start']) . " 00:00:00'";
  1091. if (isset($_REQUEST['date_end']) && !empty($_REQUEST['date_end']))
  1092. $sqlWhere .= " and $date_type <= '" . common::usDate2sqlDate($_REQUEST['date_end']) . " 23:59:59'";
  1093. $transportation = $_REQUEST["transportation"];
  1094. if(!is_array($transportation)){
  1095. $transportation = array($transportation);
  1096. }
  1097. $mode_param = "";
  1098. if(utils::count($transportation) == 1 && strtolower($transportation[0]) == 'all' ){
  1099. $mode_param = "'sea','air','road'";
  1100. }else{
  1101. $transport_mode = utils::implode(';', $transportation);
  1102. $mode_param = common::getInNotInSqlForSearch($transport_mode);
  1103. }
  1104. $sqlWhere .= " and transport_mode in ($mode_param)";
  1105. $ContainerCount_Title = array();
  1106. //先查询总的排放量sea air road,确定排名后,在分别查询对应的sea air road
  1107. //这里的问题跟 top 一样 。总的排放量查询有问题。目前是总的排放量 暂时等于 sea的排放量
  1108. if ($type == "co2e_orgin"){
  1109. $co2e_shippr_sql = "select SUM(COALESCE(carbon_emission,0)) as catnum ,
  1110. sum(case when (1<>1 or (transport_mode='air')) then 1 else 0 end) as air_rc,
  1111. sum(case when (1<>1 or (transport_mode='sea')) then 1 else 0 end) as sea_rc,
  1112. sum(case when (1<>1 or (transport_mode='road')) then 1 else 0 end) as road_rc,
  1113. 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";
  1114. $co2e_aLL = common::excuteListSql($co2e_shippr_sql);
  1115. }
  1116. if ($type == "co2e_destination"){
  1117. $co2e_consignee_sql = "select SUM(COALESCE(carbon_emission,0)) as catnum ,
  1118. sum(case when (1<>1 or (transport_mode='air')) then 1 else 0 end) as air_rc,
  1119. sum(case when (1<>1 or (transport_mode='sea')) then 1 else 0 end) as sea_rc,
  1120. sum(case when (1<>1 or (transport_mode='road')) then 1 else 0 end) as road_rc,
  1121. 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";
  1122. $co2e_aLL = common::excuteListSql($co2e_consignee_sql);
  1123. }
  1124. //最大Y值
  1125. $maxY = 0;
  1126. $stations = array();
  1127. foreach($co2e_aLL as $val){
  1128. $ContainerCount_Title[] = $val['station'];
  1129. $maxY = $maxY > $val['catnum'] ? $maxY : $val['catnum'];
  1130. if(!empty($val['station'])){
  1131. $stations[] = $val['station'];
  1132. }
  1133. }
  1134. $ContainerCounSeries = array();
  1135. $shipment_mode_arr = array("air","sea","road");
  1136. foreach($shipment_mode_arr as $_shipment_mode){
  1137. //类型有 sea air road,目前之类只查询sea的,其他为空
  1138. $shipment_mode = $_shipment_mode;
  1139. $total = 0;
  1140. $max = 0;
  1141. $data = array();
  1142. foreach($co2e_aLL as $val){
  1143. $catnum = $shipment_mode."_rc";
  1144. $data[] = empty($val[$catnum])? 0 : round($val[$catnum], 4);
  1145. $total = $total + $val[$catnum];
  1146. $max = $max < $val[$catnum] ? $val[$catnum] : $max;
  1147. }
  1148. $ContainerCounSeries[$shipment_mode] = array("data"=>$data,"total"=>$total,"max"=>$max);
  1149. }
  1150. //处理返回时数据格式 其实不太需要计算最大值,在最开始总量查询的时候以及查询出来
  1151. $ContainerCounSeries_return = array();
  1152. foreach($ContainerCounSeries as $k =>$v){
  1153. $color = common::getItemStyle($type,$k);
  1154. $ContainerCounSeries_return[] = array("name"=>$k,"type"=>"bar","emphasis" => array("focus" =>"none"),
  1155. "stack" =>"总计","data" =>$v['data'],"itemStyle" =>array("color" =>$color),'barWidth'=>'40');
  1156. }
  1157. //处理返回原表数据格式
  1158. //计算刻度值 最小值是0,最大值是3000,刻度是500 interval
  1159. $interval = utils::calculateTicks(0,$maxY,10);
  1160. if($interval == 0){
  1161. //处理返回默认值
  1162. $interval = 1;
  1163. }
  1164. $returnData = array("ContainerCount_Title"=>"","ContainerCountList" =>$ContainerCount_Title,"ContainerCounSeries" =>$ContainerCounSeries_return,
  1165. "min" => 0,"Max" =>$interval*10,"interval" =>$interval,"download_name" => $download_name);
  1166. return $returnData;
  1167. }
  1168. public static function getTopBar(){
  1169. $sqlWhere = ' and ' . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  1170. $sqlWhere = " " . $sqlWhere;
  1171. $date_type = strtolower($_REQUEST["date_type"]);
  1172. if (isset($_REQUEST['date_start']) && !empty($_REQUEST['date_start']))
  1173. $sqlWhere .= " and $date_type >= '" . common::usDate2sqlDate($_REQUEST['date_start']) . " 00:00:00'";
  1174. if (isset($_REQUEST['date_end']) && !empty($_REQUEST['date_end']))
  1175. $sqlWhere .= " and $date_type <= '" . common::usDate2sqlDate($_REQUEST['date_end']) . " 23:59:59'";
  1176. $transportation = $_REQUEST["transportation"];
  1177. if(!is_array($transportation)){
  1178. $transportation = array($transportation);
  1179. }
  1180. $mode_param = "";
  1181. if(utils::count($transportation) == 1 && strtolower($transportation[0]) == 'all' ){
  1182. $mode_param = "'sea','air','road'";
  1183. }else{
  1184. $transport_mode = utils::implode(';', $transportation);
  1185. $mode_param = common::getInNotInSqlForSearch($transport_mode);
  1186. }
  1187. $sqlWhere .= " and transport_mode in ($mode_param)";
  1188. $toporiginType = "shippr_uncode";
  1189. $shippr_uncode_10_sql ="with aa as (select count(shippr_uncode) as num,shippr_uncode,
  1190. (array_agg(shipper_city))[1] AS shipper_city from public.kln_ocean
  1191. where 1=1 and COALESCE(shippr_uncode,'')<>'' $sqlWhere group by shippr_uncode order by num desc limit 10)
  1192. select aa.*,dd.* from aa
  1193. left join LATERAL ( select lon as lng, lat as lat,
  1194. '' as label, '' as infor, 3 as sort,
  1195. null::timestamp without time zone as stime,''::text as ptype
  1196. 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";
  1197. //todo 这里预写个大概
  1198. //common::getTopBarSQL($toporiginType,$_REQUEST["transportation"]);
  1199. $shippr_uncode_10 = common::excuteListSql($shippr_uncode_10_sql);
  1200. //如果值没有:客户地址-->站点地址-->Port地址(POL/POD)
  1201. if(utils::count($shippr_uncode_10) == 1 && empty($shippr_uncode_10[0]["shippr_uncode"])){
  1202. $toporiginType = "fport_of_loading_un";
  1203. $shippr_uncode_10_sql = "with aa as (select count(fport_of_loading_un) as num,fport_of_loading_un as shippr_uncode
  1204. 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)
  1205. select aa.*,dd.* from aa
  1206. left join LATERAL ( select lon as lng, lat as lat,
  1207. '' as label, '' as infor, 3 as sort,
  1208. null::timestamp without time zone as stime,''::text as ptype
  1209. 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";
  1210. $shippr_uncode_10 = common::excuteListSql($shippr_uncode_10_sql);
  1211. }
  1212. //$toporiginType = "fport_of_loading_un";
  1213. $topdestinationinType = "consignee_uncode";
  1214. $consignee_uncode_10_sql = "with aa as (select count(consignee_uncode) as num,consignee_uncode,
  1215. (array_agg(consignee_city))[1] AS consignee_city
  1216. from public.kln_ocean where 1=1 and COALESCE(consignee_uncode,'')<>'' $sqlWhere group by consignee_uncode order by num desc limit 10)
  1217. select aa.*,dd.* from aa
  1218. left join LATERAL ( select lon as lng, lat as lat,
  1219. '' as label, '' as infor, 3 as sort,
  1220. null::timestamp without time zone as stime,''::text as ptype
  1221. 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";
  1222. $consignee_uncode_10 = common::excuteListSql($consignee_uncode_10_sql);
  1223. //如果值没有:客户地址-->站点地址-->Port地址(POL/POD)
  1224. if(utils::count($consignee_uncode_10) == 1 && empty($consignee_uncode_10[0]["consignee_uncode"])){
  1225. $topdestinationinType = "mport_of_discharge_un";
  1226. $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)
  1227. select aa.*,dd.* from aa
  1228. left join LATERAL ( select lon as lng, lat as lat,
  1229. '' as label, '' as infor, 3 as sort,
  1230. null::timestamp without time zone as stime,''::text as ptype
  1231. 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";
  1232. $consignee_uncode_10 = common::excuteListSql($consignee_uncode_10_sql);
  1233. }
  1234. $toporigin = array();
  1235. $toporiginMap = array();
  1236. $i = 0;
  1237. $origiNunMax = 0;
  1238. foreach($shippr_uncode_10 as $orgin){
  1239. $i = $i + 1;
  1240. $map = array();
  1241. if(!empty($orgin['lat'])&&!empty($orgin['lng'])){
  1242. $map = array("lat" =>$orgin['lat'],"lng" =>$orgin['lng']);
  1243. }
  1244. //$json = '{"lng":121.8525,"lat":29.926545,"label":"'.$orgin['origin'].'","infor":"LAT KRABANG, THAILAND","sort":"0","stime":null,"ptype":"por"}';
  1245. //$map = json_decode($json,true);
  1246. if(!empty($map)){
  1247. $toporiginMap[] = array("qandl"=>array(floatval($map['lat']),floatval($map['lng'])),
  1248. "divIcon" => array("iconSize"=>0),
  1249. "name" =>$orgin['shippr_uncode'],
  1250. "color" =>common::getItemStyle("top",$i),
  1251. "value" =>$orgin['num'],
  1252. "textcolor" =>"#FFF");
  1253. }
  1254. //查询uncode,对应的city
  1255. $city = "";
  1256. if($toporiginType == "shippr_uncode"){
  1257. $city = $orgin['shipper_city'];
  1258. }
  1259. //$city 做假数据
  1260. $toporigin[] = array("name"=>$orgin['shippr_uncode'],"city_name"=>$city,"value"=>$orgin['num'],"color"=>common::getItemStyle("top",$i));
  1261. $origiNunMax = $origiNunMax < $orgin['num'] ? $orgin['num'] : $origiNunMax;
  1262. }
  1263. $topdestination = array();
  1264. $topdestinationinMap = array();
  1265. $i = 0;
  1266. $agentiNunMax = 0;
  1267. foreach($consignee_uncode_10 as $agent){
  1268. $i = $i + 1;
  1269. $map = array();
  1270. if(!empty($agent['lat'])&&!empty($agent['lng'])){
  1271. $map = array("lat" =>$agent['lat'],"lng" =>$agent['lng']);
  1272. }
  1273. // $json = '{"lng":"100.78594000","lat":"13.68521000","label":"'.$agent['agent'].'","infor":"LAT KRABANG, THAILAND","sort":"0","stime":null,"ptype":"por"}';
  1274. // $map = json_decode($json,true);
  1275. if(!empty($map)){
  1276. $topdestinationinMap[] = array("qandl"=>array(floatval($map['lat']),floatval($map['lng'])),
  1277. "divIcon" => array("iconSize"=>0),
  1278. "name" =>$agent['consignee_uncode'],
  1279. "color" =>common::getItemStyle("top",$i),
  1280. "value" =>$agent['num'],
  1281. "textcolor" =>"#FFF");
  1282. }
  1283. //查询uncode,对应的city
  1284. $city = "";
  1285. if($topdestinationinType == "consignee_uncode"){
  1286. $city = $agent['consignee_city'];
  1287. }
  1288. //$city 做假数据
  1289. $topdestination[] = array("name"=>$agent['consignee_uncode'],"city_name"=>$city,"value"=>$agent['num'],"color"=>common::getItemStyle("top",$i));
  1290. $agentiNunMax = $agentiNunMax < $agent['num'] ? $agent['num'] : $agentiNunMax;
  1291. }
  1292. //处理返回原表数据格式
  1293. $interval = utils::calculateTicks(0,$origiNunMax,10);
  1294. if($interval == 0){
  1295. //处理返回默认值
  1296. $interval = 1;
  1297. }
  1298. $dest_interval = utils::calculateTicks(0,$agentiNunMax,10);
  1299. if($dest_interval == 0){
  1300. //处理返回默认值
  1301. $dest_interval = 1;
  1302. }
  1303. $returnData = array("seller_data_list_origin"=>$toporigin,"toporiginMap"=>array_reverse($toporiginMap),"toporiginType"=>$toporiginType,
  1304. "seller_data_list_destination"=>$topdestination,"topdestinationinMap"=>array_reverse($topdestinationinMap),"topdestinationinType"=>$topdestinationinType,
  1305. "min" => 0,"Max" =>$interval*10,"interval" =>$interval,
  1306. "dest_min" => 0,"dest_Max" =>$dest_interval*10,"dest_interval" =>$dest_interval);
  1307. return $returnData;
  1308. }
  1309. public static function getRevenue(){
  1310. //$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}';
  1311. //return json_decode($data,true);
  1312. $date_from = null;
  1313. $date_to = null;
  1314. if (isset($_REQUEST['date_start']) && !empty($_REQUEST['date_start'])){
  1315. $date_start = common::dateFormatToYM($_REQUEST['date_start']);
  1316. $date_from = $date_start."-01";
  1317. }
  1318. if (isset($_REQUEST['date_end']) && !empty($_REQUEST['date_end'])){
  1319. $date_end = common::dateFormatToYM($_REQUEST['date_end']);
  1320. $date_to = date('Y-m-t', strtotime($date_end."-01"));
  1321. }
  1322. $param = '{"user_login":"'._getLoginName().'","report_type":"ALL","date_from":"'.$date_from.'","date_to":"'.$date_to.'"}';
  1323. $sql = "SELECT * FROM get_customer_revenue_report('$param');FETCH ALL FROM r2;";
  1324. error_log("Revenue_sql:".$sql);
  1325. $r2_data = common::excuteListSql($sql);
  1326. //$r2_data = common::excuteListSql("select * from _test_data_table");
  1327. $barList = array();
  1328. $groupedItems = array();
  1329. foreach ($r2_data as $item) {
  1330. if(!utils::in_array($item['month'],$barList)){
  1331. $barList[] = $item['month'];
  1332. }
  1333. $groupedItems[$item['currency']][] = $item;
  1334. }
  1335. //补齐所有货币的月份
  1336. foreach($groupedItems as $ckey =>$cvalueArr){
  1337. $currency = array();
  1338. foreach($barList as $month){
  1339. $currency[] = common::findCurrencyByMonth($month,$ckey,$cvalueArr);
  1340. }
  1341. $groupedItems[$ckey] = $currency;
  1342. }
  1343. //处理barSeries
  1344. $barSeries = array();
  1345. //先准备4种颜色
  1346. $color = array("#FF7500","#FFAC66","#FFE3CC");
  1347. $index = 0;
  1348. $max = 0;
  1349. foreach($groupedItems as $currency => $series){
  1350. $series_max = 0;
  1351. $num = $index % 3;
  1352. $index = $index +1;
  1353. $tem_series = array();
  1354. $tem_series["name"] = $currency;
  1355. $tem_series["type"] = "bar";
  1356. $tem_series["emphasis"] = array("disabled"=>true,"focus"=>"none");
  1357. $tem_series["itemStyle"] = array("color"=>$color[$num],"borderRadius"=>6);
  1358. $tem_series["label"] = array("show"=>false,"color"=>"#646A73","position"=>"top","fontFamily"=>"Lato-Light","hideWhenMouseLeave"=>false);
  1359. //准备series data
  1360. $data = array();
  1361. $total = 0;
  1362. foreach($series as $_series){
  1363. $invoice_amount = empty($_series['invoice_amount'])? 0 : round($_series['invoice_amount'], 4);
  1364. $data[]=$invoice_amount;
  1365. $total = $total + $invoice_amount;
  1366. $max = $max<$_series['invoice_amount'] ? $_series['invoice_amount'] : $max;
  1367. $series_max = $series_max<$_series['invoice_amount'] ? $_series['invoice_amount'] : $series_max;
  1368. }
  1369. //计算Y坐标的间隔 每种货币的
  1370. $interval = utils::calculateTicks(0,$series_max,5);
  1371. if($interval == 0){
  1372. //处理返回默认值
  1373. $interval = 2;
  1374. }
  1375. $tem_series["Max"] = $interval*5;
  1376. $tem_series["interval"] = $interval;
  1377. $tem_series["data"] = $data;
  1378. $tem_series["total"] = floatval(number_format($total, 4, '.', ''));
  1379. $barSeries[] = $tem_series;
  1380. }
  1381. //计算Y坐标的间隔
  1382. $interval = utils::calculateTicks(0,$max,5);
  1383. if($interval == 0){
  1384. //处理返回默认值
  1385. $interval = 2;
  1386. }
  1387. $revenueBarData = array("bar_title" =>"Total:","barList" => $barList,
  1388. "barSeries" =>$barSeries,"download_name"=>"Rvenue","isShowTooltips"=>true,"Max" =>$interval*5,"interval" =>$interval);
  1389. return $revenueBarData;
  1390. }
  1391. public static function findCurrencyByMonth($month,$ckey,$cvalueArr){
  1392. $data = array("month" =>$month,"currency"=>$ckey,"invoice_amount"=>0,"sort_month"=>"");
  1393. foreach($cvalueArr as $realmonth){
  1394. if($realmonth['month'] == $month){
  1395. $data = $realmonth;
  1396. }
  1397. }
  1398. return $data;
  1399. }
  1400. public static function getdefaultManagement(){
  1401. $Management = array();
  1402. $Management[] = array("id"=>1 ,
  1403. "title"=>"KPI",
  1404. "switchValue"=>true,
  1405. "text"=>"Pie chart showing figures of shipments KPI of Departure and Arrival.");
  1406. $Management[] = array("id"=>2 ,
  1407. "title"=>"Pending",
  1408. "switchValue"=>true,
  1409. "text"=>"Pie chart showing figures of shipments which are soon to depart/arrive (Calculated from ETD/ETA).");
  1410. $Management[] = array("id"=>3 ,
  1411. "title"=>"ETD to ETA (Days)",
  1412. "switchValue"=>true,
  1413. "text"=>"Pie chart showing figures of shipments which are soon to depart/arrive (Calculated from ETD/ETA).");
  1414. $Management[] = array("id"=>4 ,
  1415. "title"=>"Container Count",
  1416. "switchValue"=>true,
  1417. "text"=>"Pie chart showing figures of shipments which are soon to depart/arrive (Calculated from ETD/ETA).");
  1418. if(_isRevenueDisplay()){
  1419. $Management[] = array("id"=>5 ,
  1420. "title"=>"Revenue Spent",
  1421. "switchValue"=>true,
  1422. "text"=>"Revenue data for the past 12 months.");
  1423. }
  1424. $Management[] = array("id"=>6 ,
  1425. "title"=>"Top 10 Origin/Destination",
  1426. "switchValue"=>true,
  1427. "text"=>"Figure of the top 10 origin/destination.",
  1428. "title1"=>"Top 10 Origin",
  1429. "title2"=>"Top 10 Destination");
  1430. $Management[] = array("id"=>7 ,
  1431. "title"=>"CO2e Emission by Origin (Top 10)",
  1432. "switchValue"=>true,
  1433. "text"=>"Figure of the CO2e Emission by origin.");
  1434. $Management[] = array("id"=>8 ,
  1435. "title"=>"CO2e Emission by Destination (Top 10)",
  1436. "switchValue"=>true,
  1437. "text"=>"Figure of the CO2e Emission by destination.");
  1438. $Management[] = array("id"=>9 ,
  1439. "title"=>"Recent Status",
  1440. "switchValue"=>true,
  1441. "text"=>"A shipment list with latest status update on top.");
  1442. return $Management;
  1443. }
  1444. /**
  1445. * Destroy Session
  1446. */
  1447. public static function sessionDestroy() {
  1448. session_destroy();
  1449. setcookie(session_name(), '', time() - 3600);
  1450. $_SESSION = array();
  1451. }
  1452. public static function dateFormatToYM($dateString){
  1453. $date = DateTime::createFromFormat('m/Y', $dateString);
  1454. $formattedDate = $date->format('Y-m'); // YYYY-MM
  1455. return $formattedDate;
  1456. }
  1457. public static function retStationInfo($address_1,$address_2,$address_3,$address_4,$city,$state,$country,$zipcode){
  1458. $stationInfo = "";
  1459. if(!empty($address_1)){
  1460. $stationInfo .= $address_1;
  1461. }
  1462. if(!empty($address_2)){
  1463. $stationInfo .= " ".$address_2;
  1464. }
  1465. if(!empty($address_3)){
  1466. $stationInfo .= " ".$address_3;
  1467. }
  1468. if(!empty($address_4)){
  1469. $stationInfo .= " ".$address_4;
  1470. }
  1471. $temp_str = "";
  1472. if(!empty($city)){
  1473. $temp_str .= $city." ";
  1474. }
  1475. if(!empty($state)){
  1476. $temp_str .= $state." ";
  1477. }
  1478. if(!empty($zipcode)){
  1479. $temp_str .= $zipcode." ";
  1480. }
  1481. if(!empty($country)){
  1482. $temp_str .= $country." ";
  1483. }
  1484. if(!empty($temp_str)){
  1485. return $stationInfo." ".trim($temp_str);
  1486. }
  1487. return $stationInfo;
  1488. }
  1489. public static function getInsertSqlNull($table_name, $values) {
  1490. $field = "";
  1491. $value = "";
  1492. foreach ($values as $k => $v) {
  1493. if ($k == 'tmp' || $k == 'action' || $k == 'operate' || $k == 'x' || $k == 'y') {
  1494. continue;
  1495. }
  1496. if (is_array($v)) {
  1497. $v = utils::implode(",", $v);
  1498. }
  1499. if (empty($field)) {
  1500. $field = $k;
  1501. if (utils::checkExist($v, 'now()')) {
  1502. $value = $v;
  1503. } elseif ($v == null) {
  1504. $value .= 'null';
  1505. } elseif ($v == 'TRUE' || $v == 'FALSE') {
  1506. $value .= $v;
  1507. } else {
  1508. $value = '\'' . common::check_input($v) . '\'';
  1509. }
  1510. } else {
  1511. $field .= ',' . $k;
  1512. if (utils::checkExist($v, 'now()'))
  1513. $value .= ', now()';
  1514. elseif ($v == null) {
  1515. $value .= ',null';
  1516. } else if ($v == 'TRUE' || $v == 'FALSE') {
  1517. $value .= ',' . $v;
  1518. } else {
  1519. $value .= ', \'' . common::check_input($v) . '\'';
  1520. }
  1521. }
  1522. }
  1523. return 'insert into ' . $table_name . '(' . $field . ') values (' . $value . ')';
  1524. }
  1525. //密码规则验证
  1526. public static function checkPasswordRule($login, $new_password){
  1527. $sql="select item_value from config where item='passwordCheckRules'";
  1528. $rs = common::excuteObjectSql($sql);
  1529. $str = "";
  1530. if (!empty($rs)) {
  1531. $passwordCheckRules = json_decode($rs["item_value"],true);
  1532. //校验使用次数
  1533. if (!empty($passwordCheckRules["pastPasswordCheckNum"])) {
  1534. $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"];
  1535. $passwords = common::excuteListSql($sql);
  1536. foreach ($passwords as $pwd) {
  1537. if ($pwd['password'] == $new_password) {
  1538. $str = "This password has been recently used";
  1539. }
  1540. }
  1541. }
  1542. if(empty($str)){
  1543. $str = utils::checkPassword($new_password,$passwordCheckRules,$login);
  1544. }
  1545. }else{
  1546. $str = utils::checkPassword($new_password);
  1547. }
  1548. return $str;
  1549. }
  1550. public static function getMilestonesInfo($ocean,$transport_mode,$_schemas,$EDI315TimeAndLocation=array()){
  1551. //Milestones info列名固定
  1552. $Milestones_column = array();
  1553. $Milestones_column[] = array("title" =>"Milestones","field" =>"milestones","formatter" =>"normal","type" =>"normal");
  1554. $Milestones_column[] = array("title" =>"Date Time","field" =>"date_time","formatter" =>"dateTime","type" =>"normal");
  1555. $Milestones_column[] = array("title" =>"Locations","field" =>"locations","formatter" =>"normal","type" =>"normal");
  1556. $Milestones_column[] = array("title" =>"Remarks","field" =>"remarks","formatter" =>"normal","type" =>"normal");
  1557. //EDI315 有关联查询,以那个为准。但是如果是手工录入或者不是EDI 进去的Milestone的情况,这以这个为准
  1558. $Milestones_NO_Mapping = common::getMilestones_NO_Mapping_Data($ocean);
  1559. //Milestones 数据信息待定
  1560. $Milestones_data = array();
  1561. if($transport_mode == 'sea'){
  1562. $Milestones_data_arr = common::excuteListSql("select sn.description,act_date||' '||COALESCE(act_time,'') as date_time,
  1563. remark,timezone,a.code
  1564. from public.ocean_milestone a
  1565. inner join public.customer_service_milestone_sno sn on sn.code=a.code and sn.type = 'sea'
  1566. where a.serial_no='".$ocean["serial_no"]."' and act_date is not null order by sn.sno asc");
  1567. }else{
  1568. $Milestones_data_arr = common::excuteListSql("select sn.description,act_date||' '||COALESCE(act_time,'') as date_time,
  1569. remark,timezone,a.code
  1570. from $_schemas.air_milestone a
  1571. inner join public.customer_service_milestone_sno sn on sn.code=a.code and sn.type = 'air'
  1572. where a.serial_no='".$ocean["serial_no"]."' and act_date is not null order by sn.sno asc");
  1573. }
  1574. foreach($Milestones_data_arr as $mda){
  1575. $timezone = "";
  1576. $IFFICC_locations ="";
  1577. if(!empty($EDI315TimeAndLocation[$mda['code']])){
  1578. $edi315Info = $EDI315TimeAndLocation[$mda['code']];
  1579. if(!empty($edi315Info)){
  1580. $timezone = $edi315Info['timezone'];
  1581. }
  1582. //IFFICC 比较特殊用EDI的locations
  1583. if($mda['code'] == "IFFICC"){
  1584. $IFFICC_locations = $edi315Info['location'];
  1585. }
  1586. }else{
  1587. $edi315Info = $Milestones_NO_Mapping[$mda['code']];
  1588. if(!empty($edi315Info)){
  1589. $timezone = $edi315Info['timezone'];
  1590. }
  1591. }
  1592. //locations 以配置的信息为准
  1593. $locations = "";
  1594. $edi315Info = $Milestones_NO_Mapping[$mda['code']];
  1595. if(!empty($edi315Info)){
  1596. $locations = $edi315Info['location'];
  1597. if($mda['code'] == "IFFICC" && !empty($IFFICC_locations)){
  1598. $locations = $IFFICC_locations;
  1599. }
  1600. }
  1601. $Milestones_data[] = array("milestones"=>$mda['description'],"date_time"=>$mda['date_time'],"timezone" =>$timezone,
  1602. "locations" => $locations, "remarks" =>$mda['remark']);
  1603. }
  1604. $Milestones = array("Milestones_column"=>$Milestones_column,"Milestones_data" =>$Milestones_data);
  1605. return $Milestones;
  1606. }
  1607. /**
  1608. * 查询uncode 对应的时区
  1609. */
  1610. public static function getCityPortsInfo($uncodes){
  1611. $data =array();
  1612. $sql = "select time_zone as timezonecode,uncode as ctrycitycode from public.city_timezone where lower(uncode) ". common::getInNotInSql($uncodes);
  1613. $uncode_rs = common::excuteListSql($sql);
  1614. foreach($uncode_rs as $value){
  1615. $data[$value['ctrycitycode']] = $value['timezonecode'];
  1616. }
  1617. return $data;
  1618. }
  1619. /**
  1620. * EDI315 有关联查询,以那个为准。但是如果是手工录入或者没有对应的情况,这以这个为准
  1621. */
  1622. public static function getMilestones_NO_Mapping_Data($ocean){
  1623. $Milestones_NO_Mapping = array();
  1624. //取origin $ocean['aa_city'] 不是必需,对应文件里未提location来源
  1625. $Milestones_NO_Mapping['IFFBCF'] = array("code"=>"origin","timezone"=>$ocean['aa_timezone'],"location"=>"");
  1626. $Milestones_NO_Mapping['IFFBCR'] = array("code"=>"origin","timezone"=>$ocean['aa_timezone'],"location"=>"");
  1627. $Milestones_NO_Mapping['IFFCPU'] = array("code"=>"origin","timezone"=>$ocean['aa_timezone'],"location"=>"");
  1628. $Milestones_NO_Mapping['IFFSIL'] = array("code"=>"origin","timezone"=>$ocean['aa_timezone'],"location"=>"");
  1629. $Milestones_NO_Mapping['IFFEDO'] = array("code"=>"origin","timezone"=>$ocean['aa_timezone'],"location"=>"");
  1630. $Milestones_NO_Mapping['IFFECC'] = array("code"=>"origin","timezone"=>$ocean['aa_timezone'],"location"=>"");
  1631. //Place of Receipt
  1632. $Milestones_NO_Mapping['IFFREC'] = array("code"=>"por/pol","timezone"=>$ocean['por_timezone'],"location"=>$ocean['por_uncity']);
  1633. $Milestones_NO_Mapping['IFFECP'] = array("code"=>"por/pol","timezone"=>$ocean['por_timezone'],"location"=>$ocean['por_uncity']);
  1634. //Port of loading
  1635. $Milestones_NO_Mapping['IFFONB'] = array("code"=>"por/pol","timezone"=>$ocean['pol_timezone'],"location"=>$ocean['pol_uncity']);
  1636. $Milestones_NO_Mapping['IFFDEP'] = array("code"=>"por/pol","timezone"=>$ocean['pol_timezone'],"location"=>$ocean['pol_uncity']);
  1637. //取destination $ocean['dd_city']不是必需
  1638. $Milestones_NO_Mapping['IFFCSN'] = array("code"=>"destination","timezone"=>$ocean['dd_timezone'],"location"=>"");
  1639. $Milestones_NO_Mapping['IFFIDO'] = array("code"=>"destination","timezone"=>$ocean['dd_timezone'],"location"=>"");
  1640. //取Port of discharge
  1641. $Milestones_NO_Mapping['IFFARR'] = array("code"=>"mpod","timezone"=>$ocean['mpod_timezone'],"location"=>$ocean['mpod_uncity']);
  1642. $Milestones_NO_Mapping['IFFUND'] = array("code"=>"mpod/pod/fd","timezone"=>$ocean['mpod_timezone'],"location"=>$ocean['mpod_uncity']);
  1643. $Milestones_NO_Mapping['IFFICC'] = array("code"=>"mpod","timezone"=>$ocean['mpod_timezone'],"location"=>$ocean['mpod_uncity']);
  1644. //Place of Delivery
  1645. $Milestones_NO_Mapping['IFFAFD'] = array("code"=>"pod","timezone"=>$ocean['pod_timezone'],"location"=>$ocean['pod_uncity']);
  1646. $Milestones_NO_Mapping['IFFCTA'] = array("code"=>"pod/mpod","timezone"=>$ocean['pod_timezone'],"location"=>$ocean['pod_uncity']);
  1647. $Milestones_NO_Mapping['IFFADW'] = array("code"=>"pod","timezone"=>$ocean['pod_timezone'],"location"=>$ocean['pod_uncity']);
  1648. $Milestones_NO_Mapping['IFFDDW'] = array("code"=>"pod","timezone"=>$ocean['pod_timezone'],"location"=>$ocean['pod_uncity']);
  1649. $Milestones_NO_Mapping['IFFPPD'] = array("code"=>"mpod/pod/fd","timezone"=>$ocean['pod_timezone'],"location"=>$ocean['pod_uncity']);
  1650. $Milestones_NO_Mapping['IFFECR'] = array("code"=>"mpod/pod/fd","timezone"=>$ocean['pod_timezone'],"location"=>$ocean['pod_uncity']);
  1651. //final_destination $ocean['fd_city']不是必需
  1652. $Milestones_NO_Mapping['IFFDEL'] = array("code"=>"fd","timezone"=>$ocean['_fd_timezone'],"location"=>"");
  1653. $Milestones_NO_Mapping['IFFHBL'] = array("code"=>"fd","timezone"=>$ocean['_fd_timezone'],"location"=>"");
  1654. return $Milestones_NO_Mapping;
  1655. }
  1656. /**
  1657. * 这个详细查询已经带模式了
  1658. */
  1659. public static function getEDI315StatusForDetail($serial_no){
  1660. $data = array();
  1661. $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
  1662. WHERE o.serial_no='$serial_no'";
  1663. $ctnr_data = common::excuteListSql($ctnr_sql);
  1664. $containerStatusData = array();
  1665. $VD = "";
  1666. $VA = "";
  1667. $VD_RELAY = "";
  1668. $VA_RELAY = "";
  1669. foreach ($ctnr_data as $cd){
  1670. //存在柜号为空的数据情况
  1671. if(empty($cd['ctnr'])){
  1672. continue;
  1673. }
  1674. //(select uncity from public.ports where uncode = s.event_code) as uncity, 按新版的逻辑这个先取消掉
  1675. $ctnr_status_sql = "select s.source_id, s.event_base as event,
  1676. to_char(to_timestamp(s.event_date, 'YYYYMMDD'), 'YYYY-MM-DD') as eventdate,
  1677. to_char(to_timestamp(s.event_time, 'HH24MI'), 'HH24:MI') as eventtime,
  1678. e.description,s.event_type as eventtype,
  1679. s.event_code as eventcode, s.event_city as eventcity,
  1680. (select time_zone from public.city_timezone where uncode = s.event_code) as timezone,
  1681. s.event_city as uncity,
  1682. case when s.event_base ='I' or s.event_base ='TR' then 'IFFREC'::text
  1683. when s.event_base ='AE' then 'IFFONB'::text
  1684. when s.event_base ='VD' then 'IFFDEP'::text
  1685. when s.event_base ='EB' or s.event_base ='VA' then 'IFFARR'::text
  1686. when s.event_base ='UV' then 'IFFUND'::text
  1687. when s.event_base ='VA' then 'IFFAFD'::text
  1688. when s.event_base ='AV' then 'IFFCTA'::text
  1689. when s.event_base ='CT' then 'IFFICC'::text
  1690. when s.event_base ='OA' or s.event_base ='D' then 'IFFPPD'::text
  1691. when s.event_base ='RD' then 'IFFECR'::text
  1692. when s.event_base ='EE' then 'IFFECP'::text
  1693. else '' ::text
  1694. end as milestone_code
  1695. from public.ra_online_container_status_v s
  1696. left join public.ra_online_edi_event e on s.event_base = e.ra_name
  1697. where s.serial_no = '" . pg_escape_string($cd['serial_no']) . "'
  1698. and s.container_no = '" . pg_escape_string($cd['ctnr']) . "' and is_display = true
  1699. order by to_timestamp(s.event_date, 'YYYYMMDD') desc,
  1700. to_timestamp(s.event_time, 'HH24MI') desc,e.ra_order desc";
  1701. $ctnr_status = common::excuteListSql($ctnr_status_sql);
  1702. //Container_Status 新版只显示几个状态, CTNR# EE I AE VD VA
  1703. $container_status_column = array("EE","I","AE","VD","VA");
  1704. $containerStatusDataTemp = array();
  1705. $content = array();
  1706. //按顺序处理 新版只显示几个状态
  1707. //foreach($container_status_column as $status){
  1708. foreach($ctnr_status as $event){
  1709. //if(strtolower($event['event']) == strtolower($status)){
  1710. $eventdate = $event["eventdate"];
  1711. if (empty($event["source_id"]) || $event["source_id"]==0) {
  1712. $eventdate.= " " . $event["eventtime"];
  1713. }
  1714. $location_code = $event["eventcode"];
  1715. $content[] = array("code" =>$event['event'],"title" =>$event['description'],"date"=>$eventdate,"country"=>$location_code,"timezone"=>$event['timezone']);
  1716. if(strtolower($event['event']) == "vd" && !empty($eventdate)){
  1717. $VD = $eventdate;
  1718. }
  1719. if(strtolower($event['event']) == "va" && !empty($eventdate)){
  1720. $VA = $eventdate;
  1721. }
  1722. if(strtolower($event['event']) == strtolower("VD_RELAY") && !empty($eventdate)){
  1723. $VD_RELAY = $eventdate;
  1724. }
  1725. if(strtolower($event['event']) == strtolower("VA_RELAY") && !empty($eventdate)){
  1726. $VA_RELAY = $eventdate;
  1727. }
  1728. //}
  1729. }
  1730. //}
  1731. //记录所有的信息
  1732. $EDI315TimeAndLocation = array();
  1733. foreach($ctnr_status as $event){
  1734. if(!empty($EDI315TimeAndLocation['IFFARR']) && $EDI315TimeAndLocation['IFFARR']['code'] == "EB"){
  1735. //如果存在EB 的EB 的优先级最高
  1736. continue;
  1737. }
  1738. if(!empty($EDI315TimeAndLocation['IFFPPD']) && $EDI315TimeAndLocation['IFFARR']['code'] == "OA"){
  1739. //如果存在OA 的OA 的优先级最高
  1740. continue;
  1741. }
  1742. $EDI315TimeAndLocation[$event['milestone_code']] = array("code"=>$event['event'],"timezone"=>$event['timezone'],"location"=>$event['uncity']);
  1743. }
  1744. $containerStatusDataTemp['label'] = $cd['ctnr']." / ".$cd['size'];
  1745. $containerStatusDataTemp['content'] = $content;
  1746. $containerStatusData[] = $containerStatusDataTemp;
  1747. }
  1748. $data['containerStatusData'] = $containerStatusData;
  1749. $data['EDI315TimeAndLocation'] = $EDI315TimeAndLocation;
  1750. $data['VD'] = $VD;
  1751. $data['VA'] = $VA;
  1752. $data['VD_RELAY'] = $VD_RELAY;
  1753. $data['VA_RELAY'] = $VA_RELAY;
  1754. return $data;
  1755. }
  1756. public static function getEDI315Time($serial_no,$_schemas){
  1757. //Timezone From 来自于EDI315
  1758. $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
  1759. WHERE o.serial_no='$serial_no'";
  1760. $ctnr_data = common::excuteListSql($ctnr_sql);
  1761. $EDI315TimeAndLocation = array();
  1762. foreach ($ctnr_data as $cd){
  1763. //存在柜号为空的数据情况
  1764. if(empty($cd['ctnr'])){
  1765. continue;
  1766. }
  1767. $ctnr_status_sql = "select
  1768. (select time_zone from public.city_timezone where uncode = s.event_code) as timezone,
  1769. case when s.event_base ='I' or s.event_base ='TR' then 'IFFREC'::text
  1770. when s.event_base ='AE' then 'IFFONB'::text
  1771. when s.event_base ='VD' then 'IFFDEP'::text
  1772. when s.event_base ='EB' or s.event_base ='VA' then 'IFFARR'::text
  1773. when s.event_base ='UV' then 'IFFUND'::text
  1774. when s.event_base ='VA' then 'IFFAFD'::text
  1775. when s.event_base ='AV' then 'IFFCTA'::text
  1776. when s.event_base ='CT' then 'IFFICC'::text
  1777. when s.event_base ='OA' or s.event_base ='D' then 'IFFPPD'::text
  1778. when s.event_base ='RD' then 'IFFECR'::text
  1779. when s.event_base ='EE' then 'IFFECP'::text
  1780. else '' ::text
  1781. end as milestone_code
  1782. from public.ra_online_container_status_v s
  1783. left join public.ra_online_edi_event e on s.event_base = e.ra_name
  1784. where s.serial_no = '" . pg_escape_string($cd['serial_no']) . "'
  1785. and s.container_no = '" . pg_escape_string($cd['ctnr']) . "' and is_display = true
  1786. order by to_timestamp(s.event_date, 'YYYYMMDD') asc,
  1787. to_timestamp(s.event_time, 'HH24MI') asc,e.ra_order asc";
  1788. $ctnr_status = common::excuteListSql($ctnr_status_sql);
  1789. //记录所有的信息、替换最后一个柜子
  1790. $EDI315TimeAndLocation = array();
  1791. foreach($ctnr_status as $event){
  1792. if(!empty($EDI315TimeAndLocation['IFFARR']) && $EDI315TimeAndLocation['IFFARR']['code'] == "EB"){
  1793. //如果存在EB 的EB 的优先级最高
  1794. continue;
  1795. }
  1796. if(!empty($EDI315TimeAndLocation['IFFPPD']) && $EDI315TimeAndLocation['IFFARR']['code'] == "OA"){
  1797. //如果存在OA 的OA 的优先级最高
  1798. continue;
  1799. }
  1800. $EDI315TimeAndLocation[$event['milestone_code']] = array("code"=>$event['event'],"timezone"=>$event['timezone']);
  1801. }
  1802. }
  1803. return $EDI315TimeAndLocation;
  1804. }
  1805. public static function getMilestoneTimeAndlocations($milestone_code,$transport_mode,$serial_no,$order_from){
  1806. $EDI315TimeAndLocation = array();
  1807. if($transport_mode == "sea"){
  1808. $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
  1809. WHERE o.serial_no='$serial_no'";
  1810. $ctnr_data = common::excuteListSql($ctnr_sql);
  1811. foreach ($ctnr_data as $cd){
  1812. //存在柜号为空的数据情况
  1813. if(empty($cd['ctnr'])){
  1814. continue;
  1815. }
  1816. $ctnr_status_sql = "select s.source_id, s.event_base as event,
  1817. to_char(to_timestamp(s.event_date, 'YYYYMMDD'), 'YYYY-MM-DD') as eventdate,
  1818. to_char(to_timestamp(s.event_time, 'HH24MI'), 'HH24:MI') as eventtime,
  1819. e.description,s.event_type as eventtype,
  1820. s.event_code as eventcode, s.event_city as eventcity,
  1821. (select time_zone from public.city_timezone where uncode = s.event_code) as timezone,
  1822. (select uncity from public.ports where uncode = s.event_code) as uncity,
  1823. case when s.event_base ='I' then 'IFFREC'::text
  1824. when s.event_base ='AE' then 'IFFONB'::text
  1825. when s.event_base ='VD' then 'IFFDEP'::text
  1826. when s.event_base ='EB' or s.event_base ='VA' then 'IFFARR'::text
  1827. when s.event_base ='UV' then 'IFFUND'::text
  1828. when s.event_base ='VA' then 'IFFAFD'::text
  1829. when s.event_base ='AV' then 'IFFCTA'::text
  1830. when s.event_base ='CT' then 'IFFICC'::text
  1831. when s.event_base ='OA' or s.event_base ='D' then 'IFFPPD'::text
  1832. when s.event_base ='EE' then 'IFFECP'::text
  1833. else '' ::text
  1834. end as milestone_code
  1835. from public.ra_online_container_status_v s
  1836. left join ra_online_edi_event e on s.event_base = e.ra_name
  1837. where s.serial_no = '" . pg_escape_string($cd['serial_no']) . "'
  1838. and s.container_no = '" . pg_escape_string($cd['ctnr']) . "' and is_display = true
  1839. order by to_timestamp(s.event_date, 'YYYYMMDD') asc,
  1840. to_timestamp(s.event_time, 'HH24MI') asc,e.ra_order asc";
  1841. $ctnr_status = common::excuteListSql($ctnr_status_sql);
  1842. //记录所有的信息
  1843. $EDI315TimeAndLocation = array();
  1844. foreach($ctnr_status as $event){
  1845. if(!empty($EDI315TimeAndLocation['IFFARR']) && $EDI315TimeAndLocation['IFFARR']['code'] == "EB"){
  1846. //如果存在EB 的EB 的优先级最高
  1847. continue;
  1848. }
  1849. if(!empty($EDI315TimeAndLocation['IFFPPD']) && $EDI315TimeAndLocation['IFFARR']['code'] == "OA"){
  1850. //如果存在OA 的OA 的优先级最高
  1851. continue;
  1852. }
  1853. $EDI315TimeAndLocation[$event['milestone_code']] = array("code"=>$event['event'],"timezone"=>$event['timezone'],"location"=>$event['uncity']);
  1854. }
  1855. }
  1856. }
  1857. //当前数据的所有time city 查询
  1858. $sql = common::trackingSql($serial_no,$order_from);
  1859. $data_arr = common::excuteListSql($sql);
  1860. $data = $data_arr[0];
  1861. //EDI315 有关联查询,以那个为准。但是如果是手工录入或者不是EDI 进去的Milestone的情况,这以这个为准
  1862. $Milestones_NO_Mapping = common::getMilestones_NO_Mapping_Data($data);
  1863. $timezone = "";
  1864. $IFFICC_locations ="";
  1865. if(!empty($EDI315TimeAndLocation[$milestone_code])){
  1866. $edi315Info = $EDI315TimeAndLocation[$milestone_code];
  1867. if(!empty($edi315Info)){
  1868. $timezone = $edi315Info['timezone'];
  1869. }
  1870. //IFFICC 比较特殊用EDI的locations
  1871. if($milestone_code == "IFFICC"){
  1872. $IFFICC_locations = $edi315Info['location'];
  1873. }
  1874. }else{
  1875. $edi315Info = $Milestones_NO_Mapping[$milestone_code];
  1876. if(!empty($edi315Info)){
  1877. $timezone = $edi315Info['timezone'];
  1878. }
  1879. }
  1880. //locations 以配置的信息为准
  1881. $locations = "";
  1882. $edi315Info = $Milestones_NO_Mapping[$milestone_code];
  1883. if(!empty($edi315Info)){
  1884. $locations = $edi315Info['location'];
  1885. if($milestone_code == "IFFICC" && !empty($IFFICC_locations)){
  1886. $locations = $IFFICC_locations;
  1887. }
  1888. }
  1889. return array("timezone" =>$timezone,"locations" =>$locations);
  1890. }
  1891. public static function trackingSql($serial_no,$order_from){
  1892. $_schemas = $order_from;
  1893. if($_schemas == 'public'){
  1894. $_schemas = "ocean";
  1895. }
  1896. $sql = "with o as(
  1897. SELECT oo.*,m_bol as _m_bol, h_bol as _h_bol,
  1898. (select time_zone from public.city_timezone where uncode = oo.fport_of_loading_un limit 1) as pol_timezone,
  1899. case when oo.transport_mode ='sea'
  1900. then (select uncity from $order_from.ports where uncode = oo.fport_of_loading_un limit 1)
  1901. else (select city from sfs.airport where coalesce(airport.country_abb,'')||airport.airport_code = oo.fport_of_loading_un limit 1)
  1902. end as pol_uncity,
  1903. (select time_zone from public.city_timezone where uncode = oo.mport_of_discharge_un limit 1) as mpod_timezone,
  1904. case when oo.transport_mode ='sea'
  1905. then (select uncity from $order_from.ports where uncode = oo.mport_of_discharge_un limit 1)
  1906. else (select city from sfs.airport where coalesce(airport.country_abb,'')||airport.airport_code = oo.mport_of_discharge_un limit 1)
  1907. end as mpod_uncity,
  1908. (select time_zone from public.city_timezone where uncode = oo.place_of_receipt_un limit 1) as por_timezone,
  1909. case when oo.transport_mode ='sea'
  1910. then (select uncity from $order_from.ports where uncode = oo.place_of_receipt_un limit 1)
  1911. else (select city from sfs.airport where coalesce(airport.country_abb,'')||airport.airport_code = oo.place_of_receipt_un limit 1)
  1912. end as por_uncity,
  1913. (select time_zone from public.city_timezone where uncode = oo.place_of_delivery_un limit 1) as pod_timezone,
  1914. case when oo.transport_mode ='sea'
  1915. then (select uncity from $order_from.ports where uncode = oo.place_of_delivery_un limit 1)
  1916. else (select city from sfs.airport where coalesce(airport.country_abb,'')||airport.airport_code = oo.place_of_delivery_un limit 1)
  1917. end as pod_uncity,
  1918. (select time_zone from public.city_timezone where uncode = oo.final_desination_uncode limit 1) as _fd_timezone,
  1919. case when oo.transport_mode ='sea'
  1920. then (select uncity from $order_from.ports where uncode = oo.final_desination_uncode limit 1)
  1921. else (select city from sfs.airport where coalesce(airport.country_abb,'')||airport.airport_code = oo.final_desination_uncode limit 1)
  1922. end as _pd_uncity,
  1923. CASE
  1924. 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
  1925. 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
  1926. WHEN (m_iffdep is not null and m_iffarr is null and m_iffdel is null) THEN 'Departure'::text
  1927. WHEN (m_iffarr is not null and m_iffdel is null) THEN 'Arrived'::text
  1928. WHEN (m_iffdel is not null) THEN 'Completed'::text
  1929. ELSE 'Created'::text
  1930. END AS new_status
  1931. from public.kln_ocean oo where oo.serial_no = '" . $serial_no . "' and oo.order_from = '$order_from'
  1932. )
  1933. SELECT o.* ,sh.*, cn.*,aa.*,dd.*,fd.*
  1934. from o
  1935. LEFT JOIN LATERAL ( SELECT company as cn_company,
  1936. address_1 as cn_address_1,
  1937. address_2 as cn_address_2,
  1938. address_3 as cn_address_3,
  1939. address_4 as cn_address_4,
  1940. city as cn_city, state as cn_state, zipcode as cn_zipcode, country as cn_country,
  1941. phone_1 as cn_phone
  1942. FROM $_schemas.contacts c WHERE o.consignee_id::text = c.contact_id::text) cn ON true
  1943. LEFT JOIN LATERAL ( SELECT company as sh_company,
  1944. address_1 as sh_address_1,
  1945. address_2 as sh_address_2,
  1946. address_3 as sh_address_3,
  1947. address_4 as sh_address_4,
  1948. city as sh_city, state as sh_state, zipcode as sh_zipcode, country as sh_country,
  1949. phone_1 as sh_phone
  1950. FROM $_schemas.contacts c WHERE o.shipper_id::text = c.contact_id::text) sh ON true
  1951. LEFT JOIN LATERAL ( SELECT company as aa_company,
  1952. address_1 as aa_address_1,
  1953. address_2 as aa_address_2,
  1954. address_3 as aa_address_3,
  1955. address_4 as aa_address_4,
  1956. city as aa_city, state as aa_state, zipcode as aa_zipcode, country as aa_country,
  1957. phone_1 as aa_phone,
  1958. (select time_zone from public.city_timezone where uncode = LEFT(c.country, 2) || COALESCE(c.city_code,'') limit 1) as aa_timezone
  1959. FROM $_schemas.contacts c WHERE o.origin::text = c.contact_id::text) aa ON true
  1960. LEFT JOIN LATERAL ( SELECT company as dd_company,
  1961. address_1 as dd_address_1,
  1962. address_2 as dd_address_2,
  1963. address_3 as dd_address_3,
  1964. address_4 as dd_address_4,
  1965. city as dd_city, state as dd_state, zipcode as dd_zipcode, country as dd_country,
  1966. phone_1 as dd_phone,
  1967. (select time_zone from public.city_timezone where uncode = LEFT(c.country, 2) || COALESCE(c.city_code,'') limit 1) as dd_timezone
  1968. FROM $_schemas.contacts c WHERE o.agent::text = c.contact_id::text) dd ON true
  1969. LEFT JOIN LATERAL ( SELECT
  1970. city as fd_city,
  1971. (select time_zone from public.city_timezone where uncode = LEFT(c.country, 2) || COALESCE(c.city_code,'') limit 1) as fd_timezone
  1972. FROM $_schemas.contacts c WHERE o.final_desination::text = c.contact_id::text) fd ON true";
  1973. return $sql;
  1974. }
  1975. public static function getDashboardTransportationSqlWhere(){
  1976. $transportation = $_REQUEST["transportation"];
  1977. if(!is_array($transportation)){
  1978. $transportation = array($transportation);
  1979. }
  1980. $mode_param = "";
  1981. if(utils::count($transportation) == 1 && strtolower($transportation[0]) == 'all' ){
  1982. $mode_param = "'sea','air','road'";
  1983. }else{
  1984. $transport_mode = utils::implode(';', $transportation);
  1985. $mode_param = common::getInNotInSqlForSearch($transport_mode);
  1986. }
  1987. return " and transport_mode in ($mode_param)";
  1988. }
  1989. /*
  1990. * Create Directory
  1991. */
  1992. public static function mkdirs($path, $mode = 0777) { //creates directory tree recursively
  1993. if (!file_exists($path)) {
  1994. if (!mkdir($path, $mode, TRUE))
  1995. return FALSE;
  1996. }
  1997. return TRUE;
  1998. }
  1999. /**
  2000. * checked is_subscribe
  2001. */
  2002. public static function checkedSubscribe($serial_no) { //creates directory tree recursively
  2003. $is_subscribe_exist = common::excuteOneSql("select user_login from public.kln_user_subscribed us
  2004. where lower(us.user_login) = '".strtolower(_getLoginName())."' and us.subscribed_serial_no = '".$serial_no."'");
  2005. if(empty($is_subscribe_exist)){
  2006. $is_subscribe = false;
  2007. }else{
  2008. $is_subscribe = true;
  2009. }
  2010. return $is_subscribe;
  2011. }
  2012. public static function getWeek($week){
  2013. if ($week == "0"){
  2014. $week = "Sunday";
  2015. } elseif ($week == "1"){
  2016. $week = "Monday";
  2017. } elseif ($week == "2"){
  2018. $week = "Tuesday";
  2019. } elseif ($week == "3"){
  2020. $week = "Wednesday";
  2021. } elseif ($week == "4"){
  2022. $week = "Thursday";
  2023. } elseif ($week == "5"){
  2024. $week = "Friday";
  2025. } elseif ($week == "6"){
  2026. $week = "Sunday";
  2027. }
  2028. return $week;
  2029. }
  2030. /**
  2031. * 这里基准event 写死, 根据online查询页面的通用的来, 这里需提问确定
  2032. */
  2033. public static function getEDICtnrEvent(){
  2034. $event =array(array("event_name"=>"EE","description"=>"Empty Equipment Dispatched"),
  2035. array("event_name"=>"I","description"=>"Gate in full for a booking"),
  2036. array("event_name"=>"AE","description"=>"Container loaded on vessel"),
  2037. array("event_name"=>"VD","description"=>"Vessel Departure"),
  2038. array("event_name"=>"VA_RELAY","description"=>"Arrive Relay Port"),
  2039. array("event_name"=>"UV_RELAY","description"=>"Unloaded at Relay Port"),
  2040. array("event_name"=>"AE_RELAY","description"=>"Loaded at Relay Port"),
  2041. array("event_name"=>"VD_RELAY","description"=>"Depart Relay Port"),
  2042. array("event_name"=>"CU","description"=>"Carrier and Customs Release"),
  2043. array("event_name"=>"CT","description"=>"Customs release"),
  2044. array("event_name"=>"CR","description"=>"Carrier release"),
  2045. array("event_name"=>"VA","description"=>"Vessel Arrival"),
  2046. array("event_name"=>"UV","description"=>"Unloaded From Vessel"),
  2047. array("event_name"=>"AG","description"=>"Estimated Delivery"),
  2048. array("event_name"=>"OA","description"=>"Gate out full from final discharge port"),
  2049. array("event_name"=>"FT","description"=>"Free Time Expired"),
  2050. array("event_name"=>"AL","description"=>"Container loaded on Rail"),
  2051. array("event_name"=>"AR","description"=>"Container unloaded from Rail"),
  2052. array("event_name"=>"AV","description"=>"Shipment available for pickup or delivery"),
  2053. array("event_name"=>"D","description"=>"Gate out for delivery to customer"),
  2054. array("event_name"=>"RD","description"=>"Container returned empty"),
  2055. array("event_name"=>"C","description"=>"Vessel Estimated Time of Departure"),
  2056. array("event_name"=>"C_RELAY","description"=>"Estimated Time of Departure from Tranship Port"),
  2057. array("event_name"=>"AG_DES","description"=>"Estimated Delivery Destination"),
  2058. array("event_name"=>"IFFADW","description"=>"Shipment in CFS warehouse"),
  2059. array("event_name"=>"IFFDDW","description"=>"Shipment Out from CFS House"));
  2060. return $event;
  2061. }
  2062. /**
  2063. * 处理daily 和 week 不同周或者天的数据分组
  2064. */
  2065. public static function handleDailyWeekedData($notificationsArr){
  2066. $uniqe = array();
  2067. $ret = array();
  2068. foreach($notificationsArr as $info){
  2069. $uniqe_group_str = $info['insert_date_format'];
  2070. if(utils::in_array($uniqe_group_str,$uniqe)){
  2071. $tempArr = $ret[$uniqe_group_str];
  2072. $tempArr[] = $info;
  2073. $ret[$uniqe_group_str] = $tempArr;
  2074. } else {
  2075. $ret[$uniqe_group_str] = array($info);
  2076. $uniqe[] = $uniqe_group_str;
  2077. }
  2078. }
  2079. return $ret;
  2080. }
  2081. }
  2082. ?>