common.class.php 106 KB

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