report.class.php 59 KB

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