report.class.php 64 KB

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