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. //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_template where serial_no = '$serial_no'");
  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 name,level,schedule_order_field,data_reference_field,data_range_type,
  675. dynamic_start_offset,dynamic_end_offset,
  676. fixed_start_date,fixed_end_date,
  677. report_sql,count_sql
  678. from public.kln_report_template where serial_no = '$serial_no'");
  679. $report_sql = $schedule_search["report_sql"];
  680. //$count_sql = $schedule_search["count_sql"];
  681. $filterFields = [];
  682. $count_sql = common::buildCountSql($filterFields,$schedule_search['level']);
  683. //$klnOceanSearchKLN = " where " . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  684. $klnOceanSearchKLN = " where created_time > CURRENT_DATE - INTERVAL '1 years' and " . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  685. $orderby = "";
  686. if(!empty($data_range_type)){
  687. //代表有用户设置的查询参数
  688. if($data_range_type == "dynamic_rolling"){
  689. $klnOceanSearchKLN .= " and ".$data_reference_field." >= CURRENT_DATE - ".$dynamic_start_offset." and ".$data_reference_field." <= CURRENT_DATE + ".$dynamic_end_offset."";
  690. } else {
  691. if(!empty($fixed_start_date)){
  692. $klnOceanSearchKLN .= " and ".$data_reference_field." >= '".common::usDate2sqlDate($fixed_start_date)."'";
  693. }
  694. if(!empty($fixed_end_date)){
  695. $klnOceanSearchKLN .= " and ".$data_reference_field." <= '".common::usDate2sqlDate($fixed_end_date)."'";
  696. }
  697. }
  698. if(!empty($schedule_order_field)){
  699. $orderby = " order by \"".strtoupper($schedule_order_field)."\"";
  700. }
  701. } else {
  702. if($schedule_search["data_range_type"] == "dynamic_rolling"){
  703. $field_db = strtolower($schedule_search["data_reference_field"]);
  704. $dynamic_start_offset_db = empty($schedule_search["dynamic_start_offset"]) ? 0 : $schedule_search["dynamic_start_offset"];
  705. $dynamic_end_offset_db = empty($schedule_search["dynamic_end_offset"]) ? 0 : $schedule_search["dynamic_end_offset"];
  706. $klnOceanSearchKLN .= " and ".$field_db." >= CURRENT_DATE - ".$dynamic_start_offset_db." and ".$field_db." <= CURRENT_DATE + ".$dynamic_end_offset_db."";
  707. } else {
  708. $field_db = strtolower($schedule_search["data_reference_field"]);
  709. $fixed_start_date_db = $schedule_search["fixed_start_date"];
  710. $fixed_end_date_db = $schedule_search["fixed_end_date"];
  711. if(!empty($fixed_start_date_db)){
  712. $klnOceanSearchKLN .= " and ".$field_db." >= '".$fixed_start_date_db."'";
  713. }
  714. if(!empty($fixed_end_date_db)){
  715. $klnOceanSearchKLN .= " and ".$field_db." <= '".$fixed_end_date_db."'";
  716. }
  717. }
  718. if(!empty($schedule_search["schedule_order_field"])){
  719. $orderby = " order by \"".strtoupper($schedule_search["schedule_order_field"])."\"";
  720. }
  721. }
  722. $count_sql = str_replace('<{klnOceanSearchKLN}>', $klnOceanSearchKLN, $count_sql);
  723. $count_sql = str_replace('<{ocItemSearchKLN}>', " ", $count_sql);
  724. $count_sql = str_replace('<{vvSearchKLN}>', " ", $count_sql);
  725. $count_sql = str_replace('<{orderby}>', "", $count_sql);
  726. error_log($schedule_search['name'].": report_schedule_count, ".$count_sql);
  727. $report_sql = str_replace('<{klnOceanSearchKLN}>', $klnOceanSearchKLN, $report_sql);
  728. $report_sql = str_replace('<{ocItemSearchKLN}>', " ", $report_sql);
  729. $report_sql = str_replace('<{CustomFiled}>', $CustomFiled, $report_sql);
  730. $report_sql = str_replace('<{vvSearchKLN}>', " ", $report_sql);
  731. $report_sql = str_replace('<{orderby}>', $orderby, $report_sql);
  732. //查询sql
  733. $cp = common::check_input($_POST ['cp']); //current_page
  734. $ps = common::check_input($_POST ['ps']); //ps
  735. if (empty($ps))
  736. $ps = 10;
  737. if (empty($cp))
  738. $cp = 1;
  739. $rc = $_POST ['rc'];
  740. if ($rc == -1) {
  741. $rc = common::excuteOneSql($count_sql);
  742. }
  743. $tp = ceil($rc / $ps);
  744. if ($rc > 0) {
  745. $tmp_search_without_limit = $report_sql;
  746. $report_sql .= " limit " . $ps . " offset " . ($cp - 1) * $ps;
  747. error_log($schedule_search['name'].": report_schedule_search, ".$report_sql);
  748. $rs = common::excuteListSql($report_sql);
  749. $arrTmp = array('searchData' => $rs,
  750. 'rc' => intval($rc),
  751. 'ps' => intval($ps),
  752. 'cp' => intval($cp),
  753. 'tp' => intval($tp));
  754. }else{
  755. $arrTmp = array('searchData' => array(),
  756. 'rc' => intval($rc),
  757. 'ps' => intval($ps),
  758. 'cp' => intval($cp),
  759. 'tp' => intval($tp));
  760. }
  761. $dataReturn['tableData'] = $arrTmp;
  762. common::echo_json_encode(200, $dataReturn);
  763. exit();
  764. exit;
  765. }
  766. if ($operate == "report_schedule_save"){
  767. $serial_no = common::check_input($_POST ['serial_no']);
  768. $validity_type = common::check_input($_POST ['validityPeriodType']);
  769. $valid_from = "null";
  770. $valid_to = "null";
  771. if($validity_type == "custom"){
  772. $valid_from = "'".common::check_input($_POST ['validityPeriodStartDate'])."'";
  773. $valid_to = "'".common::check_input($_POST ['validityPeriodEndDate'])."'";
  774. }
  775. $data_reference_field = strtolower(common::check_input($_POST ['fieldType']));
  776. $data_range_type = common::check_input($_POST ['type']);
  777. if($data_range_type == "dynamic_rolling"){
  778. $dynamic_start_offset = "'".common::check_input($_POST ['startDate'])."'";
  779. $dynamic_end_offset = "'".common::check_input($_POST ['endDate'])."'";
  780. $fixed_start_date = "null";
  781. $fixed_end_date = "null";
  782. }else{
  783. $dynamic_start_offset = "null";
  784. $dynamic_end_offset = "null";
  785. $fixed_start_date = "'".common::check_input($_POST ['startDate'])."'";
  786. $fixed_end_date = "'".common::check_input($_POST ['endDate'])."'";
  787. }
  788. $schedule_order_field = common::check_input($_POST ['orderBy']);
  789. if(empty($schedule_order_field)){
  790. $schedule_order_field = "ETD";
  791. }
  792. $email_recipients = common::check_input($_POST ['emailRecipients']);
  793. $timezone = common::check_input($_POST ['timezone']);
  794. $daily_time = "null";
  795. $weekly_day = "null";
  796. $weekly_time = "null";
  797. $monthly_day = "null";
  798. $monthly_time = "null";
  799. $quarterly_month= "null";
  800. $quarterly_day= "null";
  801. $quarterly_time = "null";
  802. $yearly_month = "null";
  803. $yearly_day = "null";
  804. $yearly_time = "null";
  805. $delivery_frequency = common::check_input($_POST ['deliveryFrequency']);
  806. if($delivery_frequency == 'daily'){
  807. $daily_time = "'".common::check_input($_POST ['time'])."'";
  808. }elseif($delivery_frequency == 'weekly'){
  809. $weekly_day= common::toPgTextArrayLiteral($_POST ['week']);
  810. $weekly_time = "'".common::check_input($_POST ['time'])."'";
  811. }elseif($delivery_frequency == 'monthly'){
  812. $monthly_day= common::toPgTextArrayLiteral($_POST ['monthlyDay']);
  813. $monthly_time = "'".common::check_input($_POST ['time'])."'";
  814. }elseif($delivery_frequency == 'quarterly'){
  815. //检查数组是否有值
  816. $quarterly_month = "'".common::check_input($_POST ['quarterMonth'])."'";
  817. $quarterly_day = "'".common::check_input($_POST ['day'])."'";
  818. $quarterly_time = "'".common::check_input($_POST ['time'])."'";
  819. }elseif($delivery_frequency == 'yearly'){
  820. $yearly_month= common::toPgTextArrayLiteral($_POST ['yearlyMonth']);
  821. $yearly_day = "'".common::check_input($_POST ['day'])."'";
  822. $yearly_time = "'".common::check_input($_POST ['time'])."'";
  823. }
  824. $sql = "";
  825. $klnOceanSearchKLN = ' where ' . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  826. if (!empty($serial_no)){
  827. $updateSqlSet = " schedule_order_field = '".$schedule_order_field."',
  828. validity_type = '".$validity_type."',
  829. valid_from = ".$valid_from.",
  830. valid_to = ".$valid_to.",
  831. data_reference_field = '".$data_reference_field."',
  832. data_range_type = '".$data_range_type."',
  833. dynamic_start_offset = ".$dynamic_start_offset.",
  834. dynamic_end_offset = ".$dynamic_end_offset.",
  835. fixed_start_date = ".$fixed_start_date.",
  836. fixed_end_date = ".$fixed_end_date.",
  837. delivery_frequency = '".$delivery_frequency."',
  838. daily_time = ".$daily_time.",
  839. weekly_day = ".$weekly_day.",
  840. weekly_time = ".$weekly_time.",
  841. monthly_day = ".$monthly_day.",
  842. monthly_time = ".$monthly_time.",
  843. quarterly_month = ".$quarterly_month.",
  844. quarterly_day = ".$quarterly_day.",
  845. quarterly_time = ".$quarterly_time.",
  846. yearly_month = ".$yearly_month.",
  847. yearly_day = ".$yearly_day.",
  848. yearly_time = ".$yearly_time.",
  849. timezone = '".$timezone."',
  850. email_recipients = '".$email_recipients."',
  851. next_run_time = null,
  852. search_extend_hand = '".common::check_input($klnOceanSearchKLN)."',
  853. modify_by = '"._getLoginName()."',
  854. update_time = now()";
  855. //代表update
  856. $sql .= "update public.kln_report_template set ".$updateSqlSet."
  857. where serial_no = '$serial_no';";
  858. }
  859. if (!empty($sql)){
  860. common::excuteUpdateSql($sql);
  861. //执行成功后,处理next_run_time
  862. $config = common::excuteObjectSql("select delivery_frequency,daily_time,
  863. array_to_json(weekly_day) as weekly_day_json,weekly_time,
  864. array_to_json(monthly_day) as monthly_day_json,monthly_time,
  865. quarterly_month,quarterly_day,quarterly_time,
  866. array_to_json(yearly_month) as yearly_month_json,yearly_day,yearly_time,timezone
  867. from public.kln_report_template where serial_no = '$serial_no';");
  868. $config["weekly_day"] = json_decode($config["weekly_day_json"],true);
  869. $config["monthly_day"] = json_decode($config["monthly_day_json"],true);
  870. $config["yearly_month"] = json_decode($config["yearly_month_json"],true);
  871. $next = common::calculateNextRunTime($config);
  872. $next_run_time = $next->format('Y-m-d H:i:s');
  873. common::excuteUpdateSql("update public.kln_report_template set next_run_time = '$next_run_time' where serial_no = '$serial_no';");
  874. $data = array("msg" =>"success");
  875. }
  876. common::echo_json_encode(200,$data);
  877. exit();
  878. }
  879. }
  880. /**
  881. * 根据提交的参数动态的拼接filter sql
  882. */
  883. public function returnFilterSql($filtersList){
  884. $klnVipDb = common::getReportRealDBFiled();
  885. $vvSearchKLN = " where 1=1 ";
  886. $klnOceanSearchKLN = " where created_time > CURRENT_DATE - INTERVAL '1 years' and " . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  887. //$klnOceanSearchKLN = " where " . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  888. $ocItemSearchKLN = " where 1=1 ";
  889. foreach($filtersList as $fiter){
  890. if(!empty($_POST[$fiter['field']])){
  891. $key = array_search($fiter['field'], $klnVipDb);
  892. if($key !== false){
  893. //找到给key
  894. $temp_sql_where = "";
  895. if ($fiter['data_type'] == "string"){
  896. $temp_sql_where .= " and ".$key." = '". common::check_input($_POST[$fiter['field']])."'";
  897. } elseif ($fiter['data_type'] == "number"){
  898. $temp_arr = $_POST [$fiter['field']];
  899. if(!empty($temp_arr[0])){
  900. $temp_sql_where .= " and ".$key."::integer >= '". common::check_input($temp_arr[0])."'";
  901. }
  902. if(!empty($temp_arr[1])){
  903. $temp_sql_where .= " and ".$key."::integer <= '". common::check_input($temp_arr[1])."'";
  904. }
  905. } elseif ($fiter['data_type'] == "date"){
  906. $temp_arr = $_POST [$fiter['field']];
  907. if(!empty($temp_arr[0])){
  908. $date_from = common::check_input(common::usDate2sqlDate($temp_arr[0]) . ' 00:00:00');
  909. $temp_sql_where .= " and ".$key." >= '". $date_from."'";
  910. }
  911. if(!empty($temp_arr[1])){
  912. $date_to = common::check_input(common::usDate2sqlDate($temp_arr[1]) . ' 23:59:59');
  913. $temp_sql_where .= " and ".$key." <= '". $date_to."'";
  914. }
  915. }
  916. if(utils::startWith($key,"oc") || utils::startWith($key,"oi")){
  917. $ocItemSearchKLN .= $temp_sql_where;
  918. } else {
  919. $klnOceanSearchKLN .= $temp_sql_where;
  920. }
  921. } else {
  922. //获取POST name 用户去别名得字段,放在vvSearchKLN上
  923. $_post_field = $fiter['field'];
  924. $_post_field = strtolower($_post_field);
  925. $_post_field = preg_replace('/[^a-z0-9]+/', '_', $_post_field); // 非字母数字 → _
  926. $_post_field = trim($_post_field, '_');
  927. if ($fiter['data_type'] == "string"){
  928. $vvSearchKLN .= " and \"".$fiter['field_real']."\" ilike '%". common::check_input($_POST [$_post_field])."%'";
  929. } elseif ($fiter['data_type'] == "number"){
  930. $temp_arr = $_POST [$_post_field];
  931. if(!empty($temp_arr[0])){
  932. $vvSearchKLN .= " and \"".$fiter['field_real']."\"::integer >= '". common::check_input($temp_arr[0])."'";
  933. }
  934. if(!empty($temp_arr[1])){
  935. $vvSearchKLN .= " and \"".$fiter['field_real']."\"::integer <= '". common::check_input($temp_arr[1])."'";
  936. }
  937. } elseif ($fiter['data_type'] == "date"){
  938. $temp_arr = $_POST [$_post_field];
  939. //先判断日期字符串是否为空,这里则有做 是因为sql 整合了柜子315时间,和 milestone的时间, 只能text转date
  940. if(!empty($temp_arr[0])){
  941. $date_from = common::check_input(common::usDate2sqlDate($temp_arr[0]) . ' 00:00:00');
  942. $vvSearchKLN .= " and COALESCE(\"".$fiter['field_real']."\",''::text)<> ''::text ";
  943. $vvSearchKLN .= " and to_timestamp(\"".$fiter['field_real']."\", 'MM/DD/YYYY HH24:MI:SS') >= '". $date_from."'";
  944. }
  945. if(!empty($temp_arr[1])){
  946. $date_to = common::check_input(common::usDate2sqlDate($temp_arr[1]) . ' 23:59:59');
  947. $vvSearchKLN .= " and COALESCE(\"".$fiter['field_real']."\",''::text)<> ''::text ";
  948. $vvSearchKLN .= " and to_timestamp(\"".$fiter['field_real']."\", 'MM/DD/YYYY HH24:MI:SS') <= '". $date_to."'";
  949. }
  950. }
  951. }
  952. }
  953. }
  954. return array("vvSearchKLN"=>$vvSearchKLN,"klnOceanSearchKLN"=>$klnOceanSearchKLN,"ocItemSearchKLN"=>$ocItemSearchKLN);
  955. }
  956. /**
  957. * 根据提交的参数动态的拼接Count sql
  958. */
  959. public function returnFilterSql_v1($filtersList){
  960. $klnVipDb = common::getReportRealDBFiled();
  961. $vvSearchKLN = " where 1=1 ";
  962. $klnOceanSearchKLN = " where created_time > CURRENT_DATE - INTERVAL '1 years' and " . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  963. //$klnOceanSearchKLN = " where " . common::searchExtendHand_KLN("ocean", $_SESSION["ONLINE_USER"]);
  964. $ocItemSearchKLN = " where 1=1 ";
  965. $filterFields = array();
  966. foreach($filtersList as $fiter){
  967. if(!empty($_POST[$fiter['field']])){
  968. //这里其实可以改成field_real
  969. $key = array_search($fiter['field'], $klnVipDb);
  970. $filterFields[] = $fiter['field_real'];
  971. if($key !== false){
  972. //找到给key
  973. $temp_sql_where = "";
  974. if ($fiter['data_type'] == "string"){
  975. $temp_sql_where .= " and ".$key." ilike '%". common::check_input($_POST[$fiter['field']])."%'";
  976. } elseif ($fiter['data_type'] == "number"){
  977. $temp_arr = $_POST [$fiter['field']];
  978. if(!empty($temp_arr[0])){
  979. $temp_sql_where .= " and ".$key."::numeric >= '". common::check_input($temp_arr[0])."'";
  980. }
  981. if(!empty($temp_arr[1])){
  982. $temp_sql_where .= " and ".$key."::numeric <= '". common::check_input($temp_arr[1])."'";
  983. }
  984. } elseif ($fiter['data_type'] == "date"){
  985. $temp_arr = $_POST [$fiter['field']];
  986. if(!empty($temp_arr[0])){
  987. $date_from = common::check_input(common::usDate2sqlDate($temp_arr[0]) . ' 00:00:00');
  988. $temp_sql_where .= " and ".$key." >= '". $date_from."'";
  989. }
  990. if(!empty($temp_arr[1])){
  991. $date_to = common::check_input(common::usDate2sqlDate($temp_arr[1]) . ' 23:59:59');
  992. $temp_sql_where .= " and ".$key." <= '". $date_to."'";
  993. }
  994. }
  995. if (utils::startWith($key,"oc") || utils::startWith($key,"oi") || utils::startWith($key,"oe") || utils::startWith($key,"co2_r")
  996. || $fiter['field'] == "total_distance"
  997. || $fiter['field'] == "last_mile_delivery"
  998. || $key == "COALESCE(oe.import_po_no,oo.po_no)"
  999. || $key == "oo._voyage"
  1000. || $key == "oo._vessel"
  1001. || $key == "oo.new_status"){
  1002. $ocItemSearchKLN .= $temp_sql_where;
  1003. } else {
  1004. $klnOceanSearchKLN .= $temp_sql_where;
  1005. }
  1006. } else {
  1007. //获取POST name 用户去别名得字段,放在vvSearchKLN上
  1008. $_post_field = $fiter['field'];
  1009. $_post_field = strtolower($_post_field);
  1010. $_post_field = preg_replace('/[^a-z0-9]+/', '_', $_post_field); // 非字母数字 → _
  1011. $_post_field = trim($_post_field, '_');
  1012. if ($fiter['data_type'] == "string"){
  1013. $vvSearchKLN .= " and \"".$fiter['field_real']."\" ilike '%". common::check_input($_POST [$_post_field])."%'";
  1014. } elseif ($fiter['data_type'] == "number"){
  1015. $temp_arr = $_POST [$_post_field];
  1016. if(!empty($temp_arr[0])){
  1017. $vvSearchKLN .= " and \"".$fiter['field_real']."\"::numeric >= '". common::check_input($temp_arr[0])."'";
  1018. }
  1019. if(!empty($temp_arr[1])){
  1020. $vvSearchKLN .= " and \"".$fiter['field_real']."\"::numeric <= '". common::check_input($temp_arr[1])."'";
  1021. }
  1022. } elseif ($fiter['data_type'] == "date"){
  1023. $temp_arr = $_POST [$_post_field];
  1024. //先判断日期字符串是否为空,这里则有做 是因为sql 整合了柜子315时间,和 milestone的时间, 只能text转date
  1025. if(!empty($temp_arr[0])){
  1026. $date_from = common::check_input(common::usDate2sqlDate($temp_arr[0]) . ' 00:00:00');
  1027. $vvSearchKLN .= " and COALESCE(\"".$fiter['field_real']."\",''::text)<> ''::text ";
  1028. $vvSearchKLN .= " and to_timestamp(\"".$fiter['field_real']."\", 'MM/DD/YYYY HH24:MI:SS') >= '". $date_from."'";
  1029. }
  1030. if(!empty($temp_arr[1])){
  1031. $date_to = common::check_input(common::usDate2sqlDate($temp_arr[1]) . ' 23:59:59');
  1032. $vvSearchKLN .= " and COALESCE(\"".$fiter['field_real']."\",''::text)<> ''::text ";
  1033. $vvSearchKLN .= " and to_timestamp(\"".$fiter['field_real']."\", 'MM/DD/YYYY HH24:MI:SS') <= '". $date_to."'";
  1034. }
  1035. }
  1036. }
  1037. }
  1038. }
  1039. return array("vvSearchKLN"=>$vvSearchKLN,"klnOceanSearchKLN"=>$klnOceanSearchKLN,"ocItemSearchKLN"=>$ocItemSearchKLN,"filterFields"=>$filterFields);
  1040. }
  1041. /**
  1042. * 根据提交的参数动态的拼接sort by sql
  1043. */
  1044. public function returnSortBySql(){
  1045. $sortByField = $_POST['sortByField'];
  1046. $sortByOrder = $_POST['sortByOrder'];
  1047. $sort_sql_temp = " ";
  1048. if(!empty($sortByField) && !empty($sortByOrder)){
  1049. $sort_sql_temp = " order by \"".$sortByField."\" ". $sortByOrder;
  1050. }
  1051. return $sort_sql_temp;
  1052. }
  1053. }
  1054. ?>