report.class.php 56 KB

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