report.class.php 47 KB

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