report.class.php 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  1. <?php
  2. if (!defined('IN_ONLINE')) {
  3. exit('Access Denied');
  4. }
  5. /**
  6. * Description of operation_log
  7. *
  8. * @author Administrator
  9. */
  10. class report {
  11. private static $_report;
  12. public static function getInstance() {
  13. if (!self::$_report) {
  14. $c = __CLASS__;
  15. self::$_report = new $c;
  16. }
  17. return self::$_report;
  18. }
  19. /**
  20. * report 配置
  21. */
  22. public function report_config(){
  23. $operate = utils::_get('operate');
  24. $operate = strtolower($operate);
  25. if ($operate == "parity_id"){
  26. //search parity id
  27. if(_isCustomerLogin()){
  28. //$ocean_contact_id = _getContactIDHandNew($_SESSION["ONLINE_USER"], 'public');
  29. $ocean_contact_id = _getCompanyContactHandNew($_SESSION["ONLINE_USER"]);
  30. $air_contact_id = _getAirContactID('public');
  31. $all_id = "ALL;".$ocean_contact_id;
  32. if (utils::endWith($ocean_contact_id,";")){
  33. $all_id .=$air_contact_id;
  34. } else {
  35. $all_id .=";".$air_contact_id;
  36. }
  37. $arr = explode(';', $all_id);
  38. $unique_arr = array_unique($arr);
  39. }else{
  40. $unique_arr= ['ALL'];
  41. }
  42. $option = array();
  43. foreach($unique_arr as $temp){
  44. $option[] =array("label"=>$temp,"value"=>$temp);
  45. }
  46. common::echo_json_encode(200, $option);
  47. exit();
  48. }
  49. /**
  50. * report的配置查询,
  51. */
  52. if ($operate == "search"){
  53. $cp = common::check_input($_POST ['cp']); //current_page
  54. $ps = common::check_input($_POST ['ps']); //ps
  55. if (empty($ps))
  56. $ps = 100;
  57. if (empty($cp))
  58. $cp = 1;
  59. $sqlWhere = "1=1";
  60. $text_search = $_POST['text_search'];
  61. if (!empty($text_search)){
  62. $sqlWhere .= " and (lower(name) like '%".strtolower($text_search)."%')";
  63. }
  64. if(!empty($_POST['application_scope'])){
  65. $pplication_scope = $_POST['application_scope'] =='all' ? "All Users" : "Specific Users";
  66. $sqlWhere .= " and access_type ='". common::check_input($pplication_scope)."'";
  67. }
  68. if(!empty($_POST['is_active'])){
  69. if($_POST['is_active'] == 'true'){
  70. $sqlWhere .= " and is_active = true";
  71. }else{
  72. $sqlWhere .= " and is_active = false";
  73. }
  74. }
  75. if(!empty($_POST['party_id'])){
  76. if($_POST['party_id'] != 'ALL'){
  77. $sqlWhere .= " and '".common::check_input($_POST['party_id'])."' = any(party_ids)";
  78. }
  79. }
  80. $sqlWhere_party_id = '';
  81. $sqlWhere_group_name = '';
  82. $systemAccountsqlWhere = " OR system_account IS NULL OR array_length(system_account, 1) = 0 OR '".common::check_input(_getLoginName())."' = any(system_account)";
  83. if(_isCustomerLogin()){
  84. $ocean_contact_id = _getCompanyContactHandNew($_SESSION["ONLINE_USER"]);
  85. $air_contact_id = _getAirContactID('public');
  86. $all_id = $ocean_contact_id;
  87. if (utils::endWith($ocean_contact_id,";")){
  88. $all_id .=$air_contact_id;
  89. } else {
  90. $all_id .=";".$air_contact_id;
  91. }
  92. $all_id = strtolower($all_id);
  93. $unique_arr = explode(';', $all_id);
  94. //构建安全的数组字面量用于 PostgreSQL 的 && 交集判断
  95. if (!empty($unique_arr)) {
  96. // 转义每个 ID 防注入
  97. $escaped_ids = array();
  98. foreach($unique_arr as $id){
  99. $id = trim($id);
  100. if (empty($id))
  101. continue;
  102. $escaped_ids[] = "'" .common::check_input($id) . "'";
  103. }
  104. if(empty($escaped_ids)){
  105. $escaped_ids[] = "'" .common::check_input('default_value_that_does_not_exist') . "'";
  106. }
  107. $array_str = 'ARRAY[' . implode(',', $escaped_ids) . ']';
  108. $sqlWhere_party_id = " OR party_ids && $array_str ";
  109. }
  110. //Group Name 判断
  111. $company = _getCompanyHandNew($_SESSION["ONLINE_USER"],'public');
  112. $company = strtolower($company);
  113. $aa = explode(";", $company);
  114. if(!empty($aa)){
  115. $escaped_company = array();
  116. foreach ($aa as $value) {
  117. $value = trim($value);
  118. if (empty($value))
  119. continue;
  120. $escaped_company[] = "'" . common::check_input($value) . "'";
  121. }
  122. if(empty($escaped_company)){
  123. $escaped_company[] = "'" . common::check_input('default_value_that_does_not_exist') . "'";
  124. }
  125. $array_str = 'ARRAY[' . implode(',', $escaped_company) . ']';
  126. $sqlWhere_group_name = " OR group_names && $array_str";
  127. }
  128. $sqlWhere .= " and ( access_type = 'All Users' ".$sqlWhere_party_id . " ".$sqlWhere_group_name ." ".$systemAccountsqlWhere .")";
  129. } else {
  130. $sqlWhere .= " and ( 1<>1 ".$systemAccountsqlWhere .")";
  131. }
  132. $rc = $_POST ['rc'];
  133. if ($rc == -1) {
  134. $sql = "select count(*) from public.kln_report_template where " .$sqlWhere;
  135. $rc = common::excuteOneSql($sql);
  136. }
  137. $tp = ceil($rc / $ps);
  138. if ($rc > 0) {
  139. $sql = "select * from public.kln_report_template where " .$sqlWhere;
  140. $sql .= " order by id desc limit " . $ps . " offset " . ($cp - 1) * $ps;
  141. $rs = common::excuteListSql($sql);
  142. $arrTmp = array('searchData' => $rs,
  143. 'isDelete' =>_isVipReportAdmin(_getLoginName()),
  144. 'rc' => intval($rc),
  145. 'ps' => intval($ps),
  146. 'cp' => intval($cp),
  147. 'tp' => intval($tp));
  148. common::echo_json_encode(200,$arrTmp);
  149. exit();
  150. }else{
  151. $arrTmp = array('searchData' =>array(),
  152. 'isDelete' =>_isVipReportAdmin(_getLoginName()),
  153. 'rc' => intval($rc),
  154. 'ps' => intval($ps),
  155. 'cp' => intval($cp),
  156. 'tp' => intval($tp));
  157. common::echo_json_encode(200, $arrTmp);
  158. exit();
  159. }
  160. }
  161. if ($operate == "add"){
  162. $serial_no = $_REQUEST['serial_no'];
  163. if(!empty($serial_no)){
  164. //代表编辑
  165. $reportMain = common::excuteObjectSql("select *,
  166. array_to_json(party_ids) as party_ids_json,
  167. array_to_json(group_names) as group_names_json,
  168. array_to_json(system_account) as system_account_json
  169. from kln_report_template where serial_no = '$serial_no'");
  170. $reportFields = array();
  171. $reportField = common::excuteListSql("select
  172. field_id as ids,
  173. field_level as \"fieldLevel\",
  174. field_type as \"fieldType\",
  175. field_group_name as \"groupName\",
  176. field_db as field,
  177. field_display_name as title,
  178. field_display_name_user as \"displayName\",
  179. data_type as \"dataType\",
  180. custom_fixed_value as value,
  181. is_filter_enabled as \"isFilter\",
  182. is_sort_enabled as \"isSort\"
  183. from kln_report_field_config where template_serial_no = '$serial_no' order by id");
  184. foreach($reportField as $_reportField){
  185. $_reportField['isFilter'] = $_reportField['isFilter'] == 't' ? true : false;
  186. $_reportField['isSort'] = $_reportField['isSort'] == 't' ? true : false;
  187. $reportFields[] = $_reportField;
  188. }
  189. $reportAccess = array("type"=>$reportMain['access_type'],
  190. "partyId"=>json_decode($reportMain['party_ids_json'],true),
  191. "groupName"=>json_decode($reportMain['group_names_json'],true),
  192. "systemAccount"=>json_decode($reportMain['system_account_json'],true));
  193. $data = array("reportName"=>$reportMain['name'],"reportLevel"=>$reportMain['level'],"reportDescription"=>$reportMain['description'],
  194. "reportFields"=>$reportFields,"reportAccess"=>$reportAccess);
  195. }else{
  196. $data = array();
  197. }
  198. common::echo_json_encode(200,$data);
  199. exit();
  200. }
  201. if ($operate == "report_field_load"){
  202. column::getInstance()->settingDisplayForVIPReport();
  203. }
  204. if ($operate == "save"){
  205. $serial_no = $_POST['serial_no'];
  206. $name = common::check_input($_POST['report_name']);
  207. $description = common::check_input($_POST['report_description']);
  208. $level = common::check_input($_POST['report_level']);
  209. $access_type = common::check_input($_POST['access_type']);
  210. $party_ids = $_POST['party_ids'];
  211. $group_names = $_POST['group_names'];
  212. $system_account = $_POST['system_account'];
  213. //单独处理字段id
  214. $fieldsList = $_POST['fieldsList'];
  215. $field_ids = array();
  216. foreach($fieldsList as $_fieldsList){
  217. if (!empty($_fieldsList['ids'])){
  218. $field_ids[] = $_fieldsList['ids'];
  219. }
  220. }
  221. $report_sql_data = common::excuteObjectSql("select sql,count_sql from public.kln_report_sql_manage where level = '$level'");
  222. $sql = "";
  223. if (!empty($serial_no)){
  224. $updateSqlSet = " report_sql = '".common::check_input($report_sql_data['sql'])."',
  225. count_sql = '".common::check_input($report_sql_data['count_sql'])."',
  226. modify_by = '"._getLoginName()."',update_time = now()";
  227. if (!empty($name)) {
  228. $updateSqlSet.= ", name = '$name' ";
  229. }
  230. if (!empty($description)) {
  231. $updateSqlSet.= ", description = '$description' ";
  232. }
  233. if (!empty($level)) {
  234. $updateSqlSet.= ", level = '$level' ";
  235. }
  236. if (!empty($access_type)) {
  237. $updateSqlSet.= ", access_type = '$access_type' ";
  238. }
  239. if (!empty($party_ids)) {
  240. $party_ids_filed= common::toPgTextArrayLiteral($party_ids);
  241. $updateSqlSet.= ", party_ids = $party_ids_filed";
  242. }
  243. if (!empty($group_names)) {
  244. $group_names_filed = common::toPgTextArrayLiteral($group_names);
  245. $updateSqlSet.= ", group_names = $group_names_filed";
  246. }
  247. if (!empty($field_ids)) {
  248. $ids_filed = common::toPgTextArrayLiteral($field_ids);
  249. $updateSqlSet.= ", field_ids = $ids_filed";
  250. }
  251. if (!empty($system_account)) {
  252. $system_account_filed = common::toPgTextArrayLiteral($system_account);
  253. $updateSqlSet.= ", system_account = $system_account_filed";
  254. }
  255. //代表update
  256. $sql .= "update public.kln_report_template set ".$updateSqlSet."
  257. where serial_no = '$serial_no';";
  258. } else {
  259. $party_ids_filed = "NULL";
  260. if (!empty($party_ids)) {
  261. $party_ids_filed= common::toPgTextArrayLiteral($party_ids);
  262. }
  263. $group_names_filed = "NULL";
  264. if (!empty($group_names)) {
  265. $group_names_filed = common::toPgTextArrayLiteral($group_names);
  266. }
  267. $ids_filed = "NULL";
  268. if (!empty($field_ids)) {
  269. $ids_filed = common::toPgTextArrayLiteral($field_ids);
  270. }
  271. $system_account_filed = "NULL";
  272. if (!empty($system_account)) {
  273. $system_account_filed = common::toPgTextArrayLiteral($system_account);
  274. }
  275. $serial_no = common::uuid();
  276. $sql .= "INSERT INTO public.kln_report_template(
  277. serial_no,name, description, level, field_ids, access_type, report_sql,count_sql,
  278. party_ids, group_names, system_account, create_by, created_time, modify_by, update_time )
  279. VALUES ('$serial_no','$name', '$description', '$level', $ids_filed, '$access_type',
  280. '".common::check_input($report_sql_data['sql'])."','".common::check_input($report_sql_data['count_sql'])."',
  281. $party_ids_filed, $group_names_filed ,$system_account_filed,'"._getLoginName()."', now(), '"._getLoginName()."', now());";
  282. }
  283. //先删除,后添加
  284. $sql .= "delete from public.kln_report_field_config where template_serial_no = '$serial_no';";
  285. foreach($fieldsList as $key =>$_tempFieldsList){
  286. $_field_id = empty($_tempFieldsList['ids'])? "NULL": $_tempFieldsList['ids'];
  287. $_field_level = common::check_input($_tempFieldsList['fieldLevel']);
  288. $_field_type = common::check_input($_tempFieldsList['fieldType']);
  289. $_field_group_name = common::check_input($_tempFieldsList['groupName']);
  290. $_field_db = common::check_input($_tempFieldsList['field']);
  291. $_field_code = common::check_input($_tempFieldsList['title']);
  292. $_display_name = common::check_input($_tempFieldsList['displayName']);
  293. $_data_type = common::check_input($_tempFieldsList['dataType']);
  294. $_value_type = $_field_type == "System" ? "" : (empty($_tempFieldsList['value']) ? "Blank" :"Fixed Value");
  295. $_fixed_value = common::check_input($_tempFieldsList['value']);
  296. //这两个不能配置查询和过滤
  297. if($_field_group_name == 'Container Status' || $_field_group_name == 'Milestone'
  298. ||$_field_code == 'Other reference No.' || $_field_code == 'Shipper Zip Code' || $_field_code == 'Consignee Zip Code' ){
  299. $_is_filter_enabled = "false";
  300. } else {
  301. $_is_filter_enabled = $_tempFieldsList['isFilter'];
  302. }
  303. $_is_sort_enabled = $_tempFieldsList['isSort'];
  304. $sql .= "INSERT INTO public.kln_report_field_config(
  305. template_serial_no, field_id, field_level, field_type, field_db, field_group_name,
  306. field_display_name, field_display_name_user, data_type, custom_value_type,
  307. custom_fixed_value, is_filter_enabled, is_sort_enabled, created_time)
  308. VALUES ('$serial_no', $_field_id, '$_field_level', '$_field_type', '$_field_db', '$_field_group_name',
  309. '$_field_code', '$_display_name', '$_data_type', '$_value_type',
  310. '$_fixed_value', '$_is_filter_enabled', '$_is_sort_enabled',now());";
  311. }
  312. if (!empty($sql)){
  313. common::excuteUpdateSql($sql);
  314. $data = array("msg" =>"success");
  315. }
  316. common::echo_json_encode(200,$data);
  317. exit();
  318. }
  319. if ($operate == "active"){
  320. $serial_no =$_POST['serial_no'];
  321. $is_active = $_POST['is_active'];
  322. $sql = "update public.kln_report_template set is_active = '$is_active' where serial_no = '$serial_no';";
  323. common::excuteUpdateSql($sql);
  324. $data = array("msg" =>"success");
  325. common::echo_json_encode(200,$data);
  326. exit();
  327. }
  328. if ($operate == "delete"){
  329. $serial_no =$_POST['serial_no'];
  330. $sql = "delete from public.kln_report_template where serial_no = '$serial_no';";
  331. $sql .= "delete from public.kln_report_field_config where template_serial_no = '$serial_no';";
  332. common::excuteUpdateSql($sql);
  333. $data = array("msg" =>"success");
  334. common::echo_json_encode(200,$data);
  335. exit();
  336. }
  337. }
  338. /**
  339. * shipment_status_report
  340. */
  341. public function shipment_status_report(){
  342. $operate = utils::_get('operate');
  343. $operate = strtolower($operate);
  344. if ($operate == "report_search") {
  345. $cp = common::check_input($_POST ['cp']); //current_page
  346. $ps = common::check_input($_POST ['ps']); //ps
  347. if (empty($ps))
  348. $ps = 100;
  349. if (empty($cp))
  350. $cp = 1;
  351. $sqlWhere = "1=1 and is_active = true";
  352. $text_search = $_POST['text_search'];
  353. if (!empty($text_search)){
  354. $sqlWhere .= " and (lower(name) like '%".strtolower($text_search)."%')";
  355. }
  356. $sqlWhere_party_id = '';
  357. $sqlWhere_group_name = '';
  358. $systemAccountsqlWhere = " OR system_account IS NULL OR array_length(system_account, 1) = 0 OR '".common::check_input(_getLoginName())."' = any(system_account)";
  359. if(_isCustomerLogin()){
  360. $ocean_contact_id = _getCompanyContactHandNew($_SESSION["ONLINE_USER"]);
  361. $air_contact_id = _getAirContactID('public');
  362. $all_id = $ocean_contact_id;
  363. if (utils::endWith($ocean_contact_id,";")){
  364. $all_id .=$air_contact_id;
  365. } else {
  366. $all_id .=";".$air_contact_id;
  367. }
  368. $all_id = strtolower($all_id);
  369. $unique_arr = explode(';', $all_id);
  370. //构建安全的数组字面量用于 PostgreSQL 的 && 交集判断
  371. if (!empty($unique_arr)) {
  372. // 转义每个 ID 防注入
  373. $escaped_ids = array();
  374. foreach($unique_arr as $id){
  375. $id = trim($id);
  376. if (empty($id))
  377. continue;
  378. $escaped_ids[] = "'" .common::check_input($id) . "'";
  379. }
  380. if(empty($escaped_ids)){
  381. $escaped_ids[] = "'" .common::check_input('default_value_that_does_not_exist') . "'";
  382. }
  383. $array_str = 'ARRAY[' . implode(',', $escaped_ids) . ']';
  384. $sqlWhere_party_id = " OR party_ids && $array_str ";
  385. }
  386. //Group Name 判断
  387. $company = _getCompanyHandNew($_SESSION["ONLINE_USER"],'public');
  388. $company = strtolower($company);
  389. $aa = explode(";", $company);
  390. if(!empty($aa)){
  391. $escaped_company = array();
  392. foreach ($aa as $value) {
  393. $value = trim($value);
  394. if (empty($value))
  395. continue;
  396. $escaped_company[] = "'" . common::check_input($value) . "'";
  397. }
  398. if(empty($escaped_company)){
  399. $escaped_company[] = "'" . common::check_input('default_value_that_does_not_exist') . "'";
  400. }
  401. $array_str = 'ARRAY[' . implode(',', $escaped_company) . ']';
  402. $sqlWhere_group_name = " OR group_names && $array_str";
  403. }
  404. $sqlWhere .= " and ( access_type = 'All Users' ".$sqlWhere_party_id . " ".$sqlWhere_group_name ." ".$systemAccountsqlWhere .")";
  405. } else {
  406. $sqlWhere .= " and ( 1<>1 ".$systemAccountsqlWhere .")";
  407. }
  408. $rc = $_POST ['rc'];
  409. if ($rc == - 1) {
  410. $sql = "select count(*) from public.kln_report_template where " .$sqlWhere;
  411. $rc = common::excuteOneSql($sql);
  412. }
  413. $tp = ceil($rc / $ps);
  414. if ($rc > 0) {
  415. $sql = "select serial_no,name,description from public.kln_report_template where " .$sqlWhere;
  416. $sql .= " order by id desc limit " . $ps . " offset " . ($cp - 1) * $ps;
  417. $rs = common::excuteListSql($sql);
  418. $arrTmp = array('searchData' => $rs,
  419. 'rc' => intval($rc),
  420. 'ps' => intval($ps),
  421. 'cp' => intval($cp),
  422. 'tp' => intval($tp));
  423. common::echo_json_encode(200,$arrTmp);
  424. exit();
  425. }else{
  426. $arrTmp = array('searchData' => array(),
  427. 'rc' => intval($rc),
  428. 'ps' => intval($ps),
  429. 'cp' => intval($cp),
  430. 'tp' => intval($tp));
  431. common::echo_json_encode(200, $arrTmp);
  432. exit();
  433. }
  434. }
  435. if ($operate == "report_detail") {
  436. $serial_no = common::check_input($_POST ['serial_no']);
  437. $dataReturn = array();
  438. $tableColumns = array();
  439. $filtersList = array();
  440. $sortByOptions = array();
  441. $CustomFiled = "";
  442. $reportFiled = common::excuteListSql("select * from public.kln_report_field_config where template_serial_no = '".$serial_no."'
  443. and is_enabled = true order by id ");
  444. foreach($reportFiled as $filed){
  445. if($filed['is_filter_enabled'] == 't'){
  446. $type = $filed['data_type'] == "string" ? "input" : ($filed['data_type'] == "date" ? "date" : "input");
  447. $field_display_name = $filed['field_display_name'];
  448. $field_display_name = strtolower($field_display_name);
  449. $field_display_name = preg_replace('/[^a-z0-9]+/', '_', $field_display_name); // 非字母数字 → _
  450. $field_display_name = trim($field_display_name, '_');
  451. $filtersList[] = array(
  452. "label"=>$filed['field_display_name_user'],
  453. "field"=>$field_display_name,
  454. "field_real"=>$filed['field_display_name'],
  455. "type"=>$type,
  456. "data_type"=>$filed['data_type'],
  457. "value"=>[],"options"=>[]);
  458. }
  459. if($filed['is_sort_enabled'] == 't'){
  460. $sortByOptions[] = $filed['field_db'];
  461. }
  462. //用户自定义字段
  463. if($filed['field_type'] == 'Custom'){
  464. $CustomFiled .= " , '".$filed['custom_fixed_value']."' AS \"".$filed['field_display_name_user']."\"";
  465. }
  466. $temp = array();
  467. $temp['field'] = $filed['field_display_name'];
  468. $temp['title'] = $filed['field_display_name_user'];
  469. $temp['type'] = $filed['field_db'] == "Status" ? "status" : "normal";
  470. $temp['formatter'] = "";
  471. $tableColumns[] = $temp;
  472. }
  473. $dataReturn['tableColumns'] = $tableColumns;
  474. $dataReturn['filtersList'] = $filtersList;
  475. if(!empty($sortByOptions)){
  476. $dataReturn['sortBy'] = array("field"=>$sortByOptions[0],"options"=>$sortByOptions,"order"=>"asc");
  477. }else{
  478. $dataReturn['sortBy'] = array("field"=>"","options"=>[],"order"=>"asc");
  479. }
  480. $reportFiled = common::excuteObjectSql("select name,report_sql,count_sql,level from public.kln_report_template where serial_no = '".$serial_no."'");
  481. $sqlManage = common::excuteObjectSql("select sql,count_sql,level from public.kln_report_sql_manage where level = '".$reportFiled['level']."'");
  482. $report_sql = $sqlManage["sql"];
  483. //$count_sql = $sqlManage["count_sql"];
  484. $filterSQLArr = $this->returnFilterSql_v1($filtersList);
  485. $count_sql = common::buildCountSql($filterSQLArr['filterFields'],$reportFiled['level']);
  486. //return array("vvSearchKLN"=>$vvSearchKLN,"klnOceanSearchKLN"=>$klnOceanSearchKLN,"ocItemSearchKLN"=>$ocItemSearchKLN);
  487. $count_sql = str_replace('<{klnOceanSearchKLN}>', $filterSQLArr['klnOceanSearchKLN'], $count_sql);
  488. $count_sql = str_replace('<{ocItemSearchKLN}>', $filterSQLArr['ocItemSearchKLN'], $count_sql);
  489. $count_sql = str_replace('<{vvSearchKLN}>', $filterSQLArr['vvSearchKLN'], $count_sql);
  490. $count_sql = str_replace('<{orderby}>', "", $count_sql);
  491. error_log($reportFiled['name'].": report_detail_count, ".$count_sql);
  492. $report_sql = str_replace('<{klnOceanSearchKLN}>', $filterSQLArr['klnOceanSearchKLN'], $report_sql);
  493. $report_sql = str_replace('<{ocItemSearchKLN}>', $filterSQLArr['ocItemSearchKLN'], $report_sql);
  494. $report_sql = str_replace('<{CustomFiled}>', $CustomFiled, $report_sql);
  495. $report_sql = str_replace('<{vvSearchKLN}>', $filterSQLArr['vvSearchKLN'], $report_sql);
  496. $report_sql = str_replace('<{orderby}>', $this->returnSortBySql(), $report_sql);
  497. //查询data
  498. $cp = common::check_input($_POST ['cp']); //current_page
  499. $ps = common::check_input($_POST ['ps']); //ps
  500. if (empty($ps))
  501. $ps = 10;
  502. if (empty($cp))
  503. $cp = 1;
  504. $rc = $_POST ['rc'];
  505. if ($rc == -1) {
  506. $rc = common::excuteOneSql($count_sql);
  507. }
  508. $tp = ceil($rc / $ps);
  509. if ($rc > 0) {
  510. $tmp_search_without_limit = $report_sql;
  511. $report_sql .= " limit " . $ps . " offset " . ($cp - 1) * $ps;
  512. error_log($reportFiled['name'].": report_detail_search, ".$report_sql);
  513. $rs = common::excuteListSql($report_sql);
  514. $arrTmp = array('searchData' => $rs,
  515. 'rc' => intval($rc),
  516. 'ps' => intval($ps),
  517. 'cp' => intval($cp),
  518. 'tp' => intval($tp));
  519. }else{
  520. $arrTmp = array('searchData' => array());
  521. }
  522. $dataReturn['tableData'] = $arrTmp;
  523. $dataReturn['tmp_search'] = common::deCode($tmp_search_without_limit, 'E');
  524. $dataReturn['reportName'] = $reportFiled['name'];
  525. common::echo_json_encode(200, $dataReturn);
  526. exit();
  527. }
  528. /*
  529. * export excel
  530. */
  531. if ($operate == "excel") {
  532. $sql = common::deCode($_POST['tmp_search'], 'D');
  533. if(!empty($sql)){
  534. $rs = common::excuteListSql($sql);
  535. }
  536. //去除null
  537. foreach($rs as $index => $val) {
  538. foreach($val as $index_2 => $_val) {
  539. if(empty($rs[$index][$index_2]) || $rs[$index][$index_2] == null){
  540. $rs[$index][$index_2] = "";
  541. }
  542. }
  543. }
  544. common::echo_json_encode(200,array("msg"=>"success","Data" => $rs));
  545. exit;
  546. }
  547. if ($operate == "manage_fileds") {
  548. $serial_no = common::check_input($_POST ['serial_no']);
  549. $reportFiled = common::excuteListSql("select * from public.kln_report_field_config
  550. where template_serial_no = '".$serial_no."' order by id ");
  551. $data = array();
  552. foreach($reportFiled as $_reportFiled){
  553. $_reportFiled['is_filter_enabled'] = $_reportFiled['is_filter_enabled'] == 't' ? true : false;
  554. $_reportFiled['is_sort_enabled'] = $_reportFiled['is_sort_enabled'] == 't' ? true : false;
  555. $_reportFiled['is_enabled'] = $_reportFiled['is_enabled'] == 't' ? true : false;
  556. $data[] = $_reportFiled;
  557. }
  558. common::echo_json_encode(200,array("msg"=>"success","data" => $data));
  559. exit;
  560. }
  561. if ($operate == "manage_fileds_save") {
  562. $serial_no = common::check_input($_POST['serial_no']);
  563. $fieldsList = $_POST['fieldsList'];
  564. //先删除,后添加 因为不涉及系统配置字段,全部用kln_report_field_config 字段name
  565. $sql = "delete from public.kln_report_field_config where template_serial_no = '$serial_no';";
  566. foreach($fieldsList as $key =>$_tempFieldsList){
  567. $_field_id = empty($_tempFieldsList['field_id'])? "NULL": $_tempFieldsList['field_id'];
  568. $_field_level = common::check_input($_tempFieldsList['field_level']);
  569. $_field_type = common::check_input($_tempFieldsList['field_type']);
  570. $_field_group_name = common::check_input($_tempFieldsList['field_group_name']);
  571. $_field_db = common::check_input($_tempFieldsList['field_db']);
  572. $_field_code = common::check_input($_tempFieldsList['field_display_name']);
  573. $_display_name = common::check_input($_tempFieldsList['field_display_name_user']);
  574. $_data_type = common::check_input($_tempFieldsList['data_type']);
  575. $_value_type = common::check_input($_tempFieldsList['custom_value_type']);
  576. $_fixed_value = common::check_input($_tempFieldsList['custom_fixed_value']);
  577. $_is_filter_enabled = $_tempFieldsList['is_filter_enabled'];
  578. $_is_sort_enabled = $_tempFieldsList['is_sort_enabled'];
  579. $_is_enabled = $_tempFieldsList['is_enabled'];
  580. $sql .= "INSERT INTO public.kln_report_field_config(
  581. template_serial_no, field_id, field_level, field_type, field_db, field_group_name,
  582. field_display_name, field_display_name_user, data_type, custom_value_type,
  583. custom_fixed_value, is_filter_enabled, is_sort_enabled,is_enabled, created_time)
  584. VALUES ('$serial_no', $_field_id, '$_field_level', '$_field_type', '$_field_db', '$_field_group_name',
  585. '$_field_code', '$_display_name', '$_data_type', '$_value_type',
  586. '$_fixed_value', '$_is_filter_enabled', '$_is_sort_enabled','$_is_enabled',now());";
  587. }
  588. if (!empty($sql)){
  589. common::excuteUpdateSql($sql);
  590. $data = array("msg" =>"success");
  591. }
  592. common::echo_json_encode(200,array("msg"=>"success","Data" => ''));
  593. exit;
  594. }
  595. if ($operate == "report_schedule"){
  596. $serial_no = common::check_input($_POST ['serial_no']);
  597. $schedule = common::excuteObjectSql("select schedule_order_field,validity_type,valid_from,valid_to,
  598. data_reference_field,data_range_type,dynamic_start_offset,dynamic_end_offset,fixed_start_date,fixed_end_date,
  599. delivery_frequency,TO_CHAR(daily_time, 'HH24:MI') as daily_time,
  600. array_to_json(weekly_day) as weekly_days_json,TO_CHAR(weekly_time, 'HH24:MI') as weekly_time,
  601. array_to_json(monthly_day) as monthly_day_json,TO_CHAR(monthly_time, 'HH24:MI') as monthly_time,
  602. quarterly_month,quarterly_day,TO_CHAR(quarterly_time, 'HH24:MI') as quarterly_time,
  603. array_to_json(yearly_month) as yearly_month_json,yearly_day, TO_CHAR(yearly_time, 'HH24:MI') as yearly_time,
  604. timezone,email_recipients
  605. from public.kln_report_template where serial_no = '$serial_no'");
  606. $schedule["weekly_days"] = json_decode($schedule["weekly_days"],true);
  607. $schedule["monthly_day"] = json_decode($schedule["monthly_day"],true);
  608. $schedule["yearly_month"] = json_decode($schedule["yearly_month"],true);
  609. //处理成VUE 识别的格式
  610. $data = array();
  611. $data["validityPeriod"] = array("type"=>$schedule["validity_type"],"startDate"=>$schedule["valid_from"],"endDate"=>$schedule["valid_to"]);
  612. if($schedule["data_range_type"] == "dynamic_rolling"){
  613. $data["timeRange"] = array("fieldType"=>$schedule["data_reference_field"],"type"=>$schedule["data_range_type"],
  614. "startDate"=>$schedule["dynamic_start_offset"],"endDate"=>$schedule["dynamic_end_offset"]);
  615. }else{
  616. $data["timeRange"] = array("fieldType"=>$schedule["data_reference_field"],"type"=>$schedule["data_range_type"],
  617. "startDate"=>$schedule["fixed_start_date"],"endDate"=>$schedule["fixed_end_date"]);
  618. }
  619. $time = "";
  620. $week = array();
  621. $month = array();
  622. $quarterMonth = "";
  623. $day = array();
  624. $delivery_frequency = $schedule["delivery_frequency"];
  625. if($delivery_frequency == 'daily'){
  626. $time = $schedule["daily_time"];
  627. }elseif($delivery_frequency == 'weekly'){
  628. $week= json_decode($schedule["weekly_days_json"],true);
  629. $time = $schedule["weekly_time"];
  630. }elseif($delivery_frequency == 'monthly'){
  631. $monthlyDay= json_decode($schedule["monthly_day_json"],true);
  632. $time = $schedule["monthly_time"];
  633. }elseif($delivery_frequency == 'quarterly'){
  634. $quarterMonth = $schedule["quarterly_month"];
  635. $day = $schedule["quarterly_day"];
  636. $time = $schedule["quarterly_time"];
  637. }elseif($delivery_frequency == 'yearly'){
  638. $month= json_decode($schedule["yearly_month_json"],true);
  639. $day = $schedule["yearly_day"];
  640. $time = $schedule["yearly_time"];
  641. }
  642. $data["deliveryFrequency"] = array("emailRecipients"=>$schedule["email_recipients"],"orderBy"=>$schedule["schedule_order_field"],
  643. "timezone"=>$schedule["timezone"],"deliveryFrequency"=>$schedule["delivery_frequency"],
  644. "scheduleDetails" =>array("time"=>$time,"week"=>$week,"month"=>$month,"monthlyDay" =>$monthlyDay,"quarterMonth"=>$quarterMonth,"day"=>$day));
  645. common::echo_json_encode(200,array("msg"=>"success","showData" => $data));
  646. exit;
  647. }
  648. if ($operate == "report_schedule_search"){
  649. $dataReturn = array();
  650. $serial_no = common::check_input($_POST ['serial_no']);
  651. $data_reference_field = strtolower(common::check_input($_POST ['fieldType']));
  652. $data_range_type = common::check_input($_POST ['type']);
  653. if($data_range_type == "dynamic_rolling"){
  654. $dynamic_start_offset = common::check_input($_POST ['startDate']);
  655. $dynamic_end_offset = common::check_input($_POST ['endDate']);
  656. $fixed_start_date = "null";
  657. $fixed_end_date = "null";
  658. }else{
  659. $dynamic_start_offset = "null";
  660. $dynamic_end_offset = "null";
  661. $fixed_start_date = common::check_input($_POST ['startDate']);
  662. $fixed_end_date = common::check_input($_POST ['endDate']);
  663. }
  664. $schedule_order_field = common::check_input($_POST ['orderBy']);
  665. //查询列名
  666. $tableColumns = array();
  667. $CustomFiled = "";
  668. $reportFiled = common::excuteListSql("select * from public.kln_report_field_config where template_serial_no = '".$serial_no."'
  669. and is_enabled = true order by id ");
  670. foreach($reportFiled as $filed){
  671. $temp = array();
  672. $temp['field'] = $filed['field_display_name'];
  673. $temp['title'] = $filed['field_display_name_user'];
  674. $temp['type'] = $filed['field_db'] == "Status" ? "status" : "normal";
  675. $temp['formatter'] = "";
  676. $tableColumns[] = $temp;
  677. //用户自定义字段
  678. if($filed['field_type'] == 'Custom'){
  679. $CustomFiled .= " , '".$filed['custom_fixed_value']."' AS \"".$filed['field_display_name_user']."\"";
  680. }
  681. }
  682. $dataReturn['tableColumns'] = $tableColumns;
  683. //形成sql
  684. $schedule_search = common::excuteObjectSql("select name,level,schedule_order_field,data_reference_field,data_range_type,
  685. dynamic_start_offset,dynamic_end_offset,
  686. fixed_start_date,fixed_end_date,
  687. report_sql,count_sql
  688. from public.kln_report_template where serial_no = '$serial_no'");
  689. $report_sql = $schedule_search["report_sql"];
  690. //$count_sql = $schedule_search["count_sql"];
  691. $filterFields = [];
  692. $count_sql = common::buildCountSql($filterFields,$schedule_search['level']);
  693. //$klnOceanSearchKLN = " where " . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  694. $klnOceanSearchKLN = " where created_time > CURRENT_DATE - INTERVAL '1 years' and " . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  695. $orderby = "";
  696. if(!empty($data_range_type)){
  697. //代表有用户设置的查询参数
  698. if($data_range_type == "dynamic_rolling"){
  699. $klnOceanSearchKLN .= " and ".$data_reference_field." >= CURRENT_DATE - ".$dynamic_start_offset." and ".$data_reference_field." <= CURRENT_DATE + ".$dynamic_end_offset."";
  700. } else {
  701. if(!empty($fixed_start_date)){
  702. $klnOceanSearchKLN .= " and ".$data_reference_field." >= '".common::usDate2sqlDate($fixed_start_date)."'";
  703. }
  704. if(!empty($fixed_end_date)){
  705. $klnOceanSearchKLN .= " and ".$data_reference_field." <= '".common::usDate2sqlDate($fixed_end_date)."'";
  706. }
  707. }
  708. if(!empty($schedule_order_field)){
  709. $orderby = " order by \"".strtoupper($schedule_order_field)."\"";
  710. }
  711. } else {
  712. if($schedule_search["data_range_type"] == "dynamic_rolling"){
  713. $field_db = strtolower($schedule_search["data_reference_field"]);
  714. $dynamic_start_offset_db = empty($schedule_search["dynamic_start_offset"]) ? 0 : $schedule_search["dynamic_start_offset"];
  715. $dynamic_end_offset_db = empty($schedule_search["dynamic_end_offset"]) ? 0 : $schedule_search["dynamic_end_offset"];
  716. $klnOceanSearchKLN .= " and ".$field_db." >= CURRENT_DATE - ".$dynamic_start_offset_db." and ".$field_db." <= CURRENT_DATE + ".$dynamic_end_offset_db."";
  717. } else {
  718. $field_db = strtolower($schedule_search["data_reference_field"]);
  719. $fixed_start_date_db = $schedule_search["fixed_start_date"];
  720. $fixed_end_date_db = $schedule_search["fixed_end_date"];
  721. if(!empty($fixed_start_date_db)){
  722. $klnOceanSearchKLN .= " and ".$field_db." >= '".$fixed_start_date_db."'";
  723. }
  724. if(!empty($fixed_end_date_db)){
  725. $klnOceanSearchKLN .= " and ".$field_db." <= '".$fixed_end_date_db."'";
  726. }
  727. }
  728. if(!empty($schedule_search["schedule_order_field"])){
  729. $orderby = " order by \"".strtoupper($schedule_search["schedule_order_field"])."\"";
  730. }
  731. }
  732. $count_sql = str_replace('<{klnOceanSearchKLN}>', $klnOceanSearchKLN, $count_sql);
  733. $count_sql = str_replace('<{ocItemSearchKLN}>', " ", $count_sql);
  734. $count_sql = str_replace('<{vvSearchKLN}>', " ", $count_sql);
  735. $count_sql = str_replace('<{orderby}>', "", $count_sql);
  736. error_log($schedule_search['name'].": report_schedule_count, ".$count_sql);
  737. $report_sql = str_replace('<{klnOceanSearchKLN}>', $klnOceanSearchKLN, $report_sql);
  738. $report_sql = str_replace('<{ocItemSearchKLN}>', " ", $report_sql);
  739. $report_sql = str_replace('<{CustomFiled}>', $CustomFiled, $report_sql);
  740. $report_sql = str_replace('<{vvSearchKLN}>', " ", $report_sql);
  741. $report_sql = str_replace('<{orderby}>', $orderby, $report_sql);
  742. //查询sql
  743. $cp = common::check_input($_POST ['cp']); //current_page
  744. $ps = common::check_input($_POST ['ps']); //ps
  745. if (empty($ps))
  746. $ps = 10;
  747. if (empty($cp))
  748. $cp = 1;
  749. $rc = $_POST ['rc'];
  750. if ($rc == -1) {
  751. $rc = common::excuteOneSql($count_sql);
  752. }
  753. $tp = ceil($rc / $ps);
  754. if ($rc > 0) {
  755. $tmp_search_without_limit = $report_sql;
  756. $report_sql .= " limit " . $ps . " offset " . ($cp - 1) * $ps;
  757. error_log($schedule_search['name'].": report_schedule_search, ".$report_sql);
  758. $rs = common::excuteListSql($report_sql);
  759. $arrTmp = array('searchData' => $rs,
  760. 'rc' => intval($rc),
  761. 'ps' => intval($ps),
  762. 'cp' => intval($cp),
  763. 'tp' => intval($tp));
  764. }else{
  765. $arrTmp = array('searchData' => array(),
  766. 'rc' => intval($rc),
  767. 'ps' => intval($ps),
  768. 'cp' => intval($cp),
  769. 'tp' => intval($tp));
  770. }
  771. $dataReturn['tableData'] = $arrTmp;
  772. common::echo_json_encode(200, $dataReturn);
  773. exit();
  774. exit;
  775. }
  776. if ($operate == "report_schedule_save"){
  777. $serial_no = common::check_input($_POST ['serial_no']);
  778. $validity_type = common::check_input($_POST ['validityPeriodType']);
  779. $valid_from = "null";
  780. $valid_to = "null";
  781. if($validity_type == "custom"){
  782. $valid_from = "'".common::check_input($_POST ['validityPeriodStartDate'])."'";
  783. $valid_to = "'".common::check_input($_POST ['validityPeriodEndDate'])."'";
  784. }
  785. $data_reference_field = strtolower(common::check_input($_POST ['fieldType']));
  786. $data_range_type = common::check_input($_POST ['type']);
  787. if($data_range_type == "dynamic_rolling"){
  788. $dynamic_start_offset = "'".common::check_input($_POST ['startDate'])."'";
  789. $dynamic_end_offset = "'".common::check_input($_POST ['endDate'])."'";
  790. $fixed_start_date = "null";
  791. $fixed_end_date = "null";
  792. }else{
  793. $dynamic_start_offset = "null";
  794. $dynamic_end_offset = "null";
  795. $fixed_start_date = "'".common::check_input($_POST ['startDate'])."'";
  796. $fixed_end_date = "'".common::check_input($_POST ['endDate'])."'";
  797. }
  798. $schedule_order_field = common::check_input($_POST ['orderBy']);
  799. if(empty($schedule_order_field)){
  800. $schedule_order_field = "ETD";
  801. }
  802. $email_recipients = common::check_input($_POST ['emailRecipients']);
  803. $timezone = common::check_input($_POST ['timezone']);
  804. $daily_time = "null";
  805. $weekly_day = "null";
  806. $weekly_time = "null";
  807. $monthly_day = "null";
  808. $monthly_time = "null";
  809. $quarterly_month= "null";
  810. $quarterly_day= "null";
  811. $quarterly_time = "null";
  812. $yearly_month = "null";
  813. $yearly_day = "null";
  814. $yearly_time = "null";
  815. $delivery_frequency = common::check_input($_POST ['deliveryFrequency']);
  816. if($delivery_frequency == 'daily'){
  817. $daily_time = "'".common::check_input($_POST ['time'])."'";
  818. }elseif($delivery_frequency == 'weekly'){
  819. $weekly_day= common::toPgTextArrayLiteral($_POST ['week']);
  820. $weekly_time = "'".common::check_input($_POST ['time'])."'";
  821. }elseif($delivery_frequency == 'monthly'){
  822. $monthly_day= common::toPgTextArrayLiteral($_POST ['monthlyDay']);
  823. $monthly_time = "'".common::check_input($_POST ['time'])."'";
  824. }elseif($delivery_frequency == 'quarterly'){
  825. //检查数组是否有值
  826. $quarterly_month = "'".common::check_input($_POST ['quarterMonth'])."'";
  827. $quarterly_day = "'".common::check_input($_POST ['day'])."'";
  828. $quarterly_time = "'".common::check_input($_POST ['time'])."'";
  829. }elseif($delivery_frequency == 'yearly'){
  830. $yearly_month= common::toPgTextArrayLiteral($_POST ['yearlyMonth']);
  831. $yearly_day = "'".common::check_input($_POST ['day'])."'";
  832. $yearly_time = "'".common::check_input($_POST ['time'])."'";
  833. }
  834. $sql = "";
  835. $klnOceanSearchKLN = ' where ' . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  836. if (!empty($serial_no)){
  837. $updateSqlSet = " schedule_order_field = '".$schedule_order_field."',
  838. validity_type = '".$validity_type."',
  839. valid_from = ".$valid_from.",
  840. valid_to = ".$valid_to.",
  841. data_reference_field = '".$data_reference_field."',
  842. data_range_type = '".$data_range_type."',
  843. dynamic_start_offset = ".$dynamic_start_offset.",
  844. dynamic_end_offset = ".$dynamic_end_offset.",
  845. fixed_start_date = ".$fixed_start_date.",
  846. fixed_end_date = ".$fixed_end_date.",
  847. delivery_frequency = '".$delivery_frequency."',
  848. daily_time = ".$daily_time.",
  849. weekly_day = ".$weekly_day.",
  850. weekly_time = ".$weekly_time.",
  851. monthly_day = ".$monthly_day.",
  852. monthly_time = ".$monthly_time.",
  853. quarterly_month = ".$quarterly_month.",
  854. quarterly_day = ".$quarterly_day.",
  855. quarterly_time = ".$quarterly_time.",
  856. yearly_month = ".$yearly_month.",
  857. yearly_day = ".$yearly_day.",
  858. yearly_time = ".$yearly_time.",
  859. timezone = '".$timezone."',
  860. email_recipients = '".$email_recipients."',
  861. next_run_time = null,
  862. search_extend_hand = '".common::check_input($klnOceanSearchKLN)."',
  863. modify_by = '"._getLoginName()."',
  864. update_time = now()";
  865. //代表update
  866. $sql .= "update public.kln_report_template set ".$updateSqlSet."
  867. where serial_no = '$serial_no';";
  868. }
  869. if (!empty($sql)){
  870. common::excuteUpdateSql($sql);
  871. //执行成功后,处理next_run_time
  872. $config = common::excuteObjectSql("select delivery_frequency,daily_time,
  873. array_to_json(weekly_day) as weekly_day_json,weekly_time,
  874. array_to_json(monthly_day) as monthly_day_json,monthly_time,
  875. quarterly_month,quarterly_day,quarterly_time,
  876. array_to_json(yearly_month) as yearly_month_json,yearly_day,yearly_time,timezone
  877. from public.kln_report_template where serial_no = '$serial_no';");
  878. $config["weekly_day"] = json_decode($config["weekly_day_json"],true);
  879. $config["monthly_day"] = json_decode($config["monthly_day_json"],true);
  880. $config["yearly_month"] = json_decode($config["yearly_month_json"],true);
  881. $next = common::calculateNextRunTime($config);
  882. $next_run_time = $next->format('Y-m-d H:i:s');
  883. common::excuteUpdateSql("update public.kln_report_template set next_run_time = '$next_run_time' where serial_no = '$serial_no';");
  884. $data = array("msg" =>"success");
  885. }
  886. common::echo_json_encode(200,$data);
  887. exit();
  888. }
  889. }
  890. /**
  891. * 根据提交的参数动态的拼接filter sql
  892. */
  893. public function returnFilterSql($filtersList){
  894. $klnVipDb = common::getReportRealDBFiled();
  895. $vvSearchKLN = " where 1=1 ";
  896. $klnOceanSearchKLN = " where created_time > CURRENT_DATE - INTERVAL '1 years' and " . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  897. //$klnOceanSearchKLN = " where " . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  898. $ocItemSearchKLN = " where 1=1 ";
  899. foreach($filtersList as $fiter){
  900. if(!empty($_POST[$fiter['field']])){
  901. $key = array_search($fiter['field'], $klnVipDb);
  902. if($key !== false){
  903. //找到给key
  904. $temp_sql_where = "";
  905. if ($fiter['data_type'] == "string"){
  906. $temp_sql_where .= " and ".$key." = '". common::check_input($_POST[$fiter['field']])."'";
  907. } elseif ($fiter['data_type'] == "number"){
  908. $temp_arr = $_POST [$fiter['field']];
  909. if(!empty($temp_arr[0])){
  910. $temp_sql_where .= " and ".$key."::integer >= '". common::check_input($temp_arr[0])."'";
  911. }
  912. if(!empty($temp_arr[1])){
  913. $temp_sql_where .= " and ".$key."::integer <= '". common::check_input($temp_arr[1])."'";
  914. }
  915. } elseif ($fiter['data_type'] == "date"){
  916. $temp_arr = $_POST [$fiter['field']];
  917. if(!empty($temp_arr[0])){
  918. $date_from = common::check_input(common::usDate2sqlDate($temp_arr[0]) . ' 00:00:00');
  919. $temp_sql_where .= " and ".$key." >= '". $date_from."'";
  920. }
  921. if(!empty($temp_arr[1])){
  922. $date_to = common::check_input(common::usDate2sqlDate($temp_arr[1]) . ' 23:59:59');
  923. $temp_sql_where .= " and ".$key." <= '". $date_to."'";
  924. }
  925. }
  926. if(utils::startWith($key,"oc") || utils::startWith($key,"oi")){
  927. $ocItemSearchKLN .= $temp_sql_where;
  928. } else {
  929. $klnOceanSearchKLN .= $temp_sql_where;
  930. }
  931. } else {
  932. //获取POST name 用户去别名得字段,放在vvSearchKLN上
  933. $_post_field = $fiter['field'];
  934. $_post_field = strtolower($_post_field);
  935. $_post_field = preg_replace('/[^a-z0-9]+/', '_', $_post_field); // 非字母数字 → _
  936. $_post_field = trim($_post_field, '_');
  937. if ($fiter['data_type'] == "string"){
  938. $vvSearchKLN .= " and \"".$fiter['field_real']."\" ilike '%". common::check_input($_POST [$_post_field])."%'";
  939. } elseif ($fiter['data_type'] == "number"){
  940. $temp_arr = $_POST [$_post_field];
  941. if(!empty($temp_arr[0])){
  942. $vvSearchKLN .= " and \"".$fiter['field_real']."\"::integer >= '". common::check_input($temp_arr[0])."'";
  943. }
  944. if(!empty($temp_arr[1])){
  945. $vvSearchKLN .= " and \"".$fiter['field_real']."\"::integer <= '". common::check_input($temp_arr[1])."'";
  946. }
  947. } elseif ($fiter['data_type'] == "date"){
  948. $temp_arr = $_POST [$_post_field];
  949. //先判断日期字符串是否为空,这里则有做 是因为sql 整合了柜子315时间,和 milestone的时间, 只能text转date
  950. if(!empty($temp_arr[0])){
  951. $date_from = common::check_input(common::usDate2sqlDate($temp_arr[0]) . ' 00:00:00');
  952. $vvSearchKLN .= " and COALESCE(\"".$fiter['field_real']."\",''::text)<> ''::text ";
  953. $vvSearchKLN .= " and to_timestamp(\"".$fiter['field_real']."\", 'MM/DD/YYYY HH24:MI:SS') >= '". $date_from."'";
  954. }
  955. if(!empty($temp_arr[1])){
  956. $date_to = common::check_input(common::usDate2sqlDate($temp_arr[1]) . ' 23:59:59');
  957. $vvSearchKLN .= " and COALESCE(\"".$fiter['field_real']."\",''::text)<> ''::text ";
  958. $vvSearchKLN .= " and to_timestamp(\"".$fiter['field_real']."\", 'MM/DD/YYYY HH24:MI:SS') <= '". $date_to."'";
  959. }
  960. }
  961. }
  962. }
  963. }
  964. return array("vvSearchKLN"=>$vvSearchKLN,"klnOceanSearchKLN"=>$klnOceanSearchKLN,"ocItemSearchKLN"=>$ocItemSearchKLN);
  965. }
  966. /**
  967. * 根据提交的参数动态的拼接Count sql
  968. */
  969. public function returnFilterSql_v1($filtersList){
  970. $klnVipDb = common::getReportRealDBFiled();
  971. $vvSearchKLN = " where 1=1 ";
  972. $klnOceanSearchKLN = " where created_time > CURRENT_DATE - INTERVAL '1 years' and " . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  973. //$klnOceanSearchKLN = " where " . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  974. $ocItemSearchKLN = " where 1=1 ";
  975. $filterFields = array();
  976. foreach($filtersList as $fiter){
  977. if(!empty($_POST[$fiter['field']])){
  978. //这里其实可以改成field_real
  979. $key = array_search($fiter['field'], $klnVipDb);
  980. $filterFields[] = $fiter['field_real'];
  981. if($key !== false){
  982. //找到给key
  983. $temp_sql_where = "";
  984. if ($fiter['data_type'] == "string"){
  985. $temp_sql_where .= " and ".$key." ilike '%". common::check_input($_POST[$fiter['field']])."%'";
  986. } elseif ($fiter['data_type'] == "number"){
  987. $temp_arr = $_POST [$fiter['field']];
  988. if(!empty($temp_arr[0])){
  989. $temp_sql_where .= " and ".$key."::numeric >= '". common::check_input($temp_arr[0])."'";
  990. }
  991. if(!empty($temp_arr[1])){
  992. $temp_sql_where .= " and ".$key."::numeric <= '". common::check_input($temp_arr[1])."'";
  993. }
  994. } elseif ($fiter['data_type'] == "date"){
  995. $temp_arr = $_POST [$fiter['field']];
  996. if(!empty($temp_arr[0])){
  997. $date_from = common::check_input(common::usDate2sqlDate($temp_arr[0]) . ' 00:00:00');
  998. $temp_sql_where .= " and ".$key." >= '". $date_from."'";
  999. }
  1000. if(!empty($temp_arr[1])){
  1001. $date_to = common::check_input(common::usDate2sqlDate($temp_arr[1]) . ' 23:59:59');
  1002. $temp_sql_where .= " and ".$key." <= '". $date_to."'";
  1003. }
  1004. }
  1005. if (utils::startWith($key,"oc") || utils::startWith($key,"oi") || utils::startWith($key,"oe") || utils::startWith($key,"co2_r")
  1006. || $fiter['field'] == "total_distance"
  1007. || $fiter['field'] == "last_mile_delivery"
  1008. || $key == "COALESCE(oe.import_po_no,oo.po_no)"
  1009. || $key == "oo._voyage"
  1010. || $key == "oo._vessel"
  1011. || $key == "oo.new_status"){
  1012. $ocItemSearchKLN .= $temp_sql_where;
  1013. } else {
  1014. $klnOceanSearchKLN .= $temp_sql_where;
  1015. }
  1016. } else {
  1017. //获取POST name 用户去别名得字段,放在vvSearchKLN上
  1018. $_post_field = $fiter['field'];
  1019. $_post_field = strtolower($_post_field);
  1020. $_post_field = preg_replace('/[^a-z0-9]+/', '_', $_post_field); // 非字母数字 → _
  1021. $_post_field = trim($_post_field, '_');
  1022. if ($fiter['data_type'] == "string"){
  1023. $vvSearchKLN .= " and \"".$fiter['field_real']."\" ilike '%". common::check_input($_POST [$_post_field])."%'";
  1024. } elseif ($fiter['data_type'] == "number"){
  1025. $temp_arr = $_POST [$_post_field];
  1026. if(!empty($temp_arr[0])){
  1027. $vvSearchKLN .= " and \"".$fiter['field_real']."\"::numeric >= '". common::check_input($temp_arr[0])."'";
  1028. }
  1029. if(!empty($temp_arr[1])){
  1030. $vvSearchKLN .= " and \"".$fiter['field_real']."\"::numeric <= '". common::check_input($temp_arr[1])."'";
  1031. }
  1032. } elseif ($fiter['data_type'] == "date"){
  1033. $temp_arr = $_POST [$_post_field];
  1034. //先判断日期字符串是否为空,这里则有做 是因为sql 整合了柜子315时间,和 milestone的时间, 只能text转date
  1035. if(!empty($temp_arr[0])){
  1036. $date_from = common::check_input(common::usDate2sqlDate($temp_arr[0]) . ' 00:00:00');
  1037. $vvSearchKLN .= " and COALESCE(\"".$fiter['field_real']."\",''::text)<> ''::text ";
  1038. $vvSearchKLN .= " and to_timestamp(\"".$fiter['field_real']."\", 'MM/DD/YYYY HH24:MI:SS') >= '". $date_from."'";
  1039. }
  1040. if(!empty($temp_arr[1])){
  1041. $date_to = common::check_input(common::usDate2sqlDate($temp_arr[1]) . ' 23:59:59');
  1042. $vvSearchKLN .= " and COALESCE(\"".$fiter['field_real']."\",''::text)<> ''::text ";
  1043. $vvSearchKLN .= " and to_timestamp(\"".$fiter['field_real']."\", 'MM/DD/YYYY HH24:MI:SS') <= '". $date_to."'";
  1044. }
  1045. }
  1046. }
  1047. }
  1048. }
  1049. return array("vvSearchKLN"=>$vvSearchKLN,"klnOceanSearchKLN"=>$klnOceanSearchKLN,"ocItemSearchKLN"=>$ocItemSearchKLN,"filterFields"=>$filterFields);
  1050. }
  1051. /**
  1052. * 根据提交的参数动态的拼接sort by sql
  1053. */
  1054. public function returnSortBySql(){
  1055. $sortByField = $_POST['sortByField'];
  1056. $sortByOrder = $_POST['sortByOrder'];
  1057. $sort_sql_temp = " ";
  1058. if(!empty($sortByField) && !empty($sortByOrder)){
  1059. $sort_sql_temp = " order by \"".$sortByField."\" ". $sortByOrder;
  1060. }
  1061. return $sort_sql_temp;
  1062. }
  1063. }
  1064. ?>