report.class.php 60 KB

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