common.class.php 104 KB

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