report.class.php 57 KB

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