report.class.php 62 KB

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