report.class.php 60 KB

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