report.class.php 47 KB

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