Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

452 linhas
20 KiB

  1. <?php
  2. /**
  3. * User: yimazsq
  4. * Date: 2016/10/28
  5. * Time: 11:17
  6. * 组合线路票种相关
  7. */
  8. class groupFinance extends base
  9. {
  10. /**
  11. * 获取出团报账列表
  12. * @param $param
  13. */
  14. public function getFinanceList($param)
  15. {
  16. #region 获取渠道权限
  17. $user_id = $this->user_id;
  18. $opera_org_id_sql = "select opera_org_id from base_user where id = " . $user_id . " and cancel_flag = 0";
  19. $opera_org_id_result = $this->query($opera_org_id_sql);
  20. $opera_org_id = $opera_org_id_result[0]['opera_org_id'];
  21. if ($opera_org_id == '') {
  22. $and_sql = '';
  23. } else {
  24. $and_sql = " and l.org_id in (" . $opera_org_id . ") ";
  25. }
  26. #endregion
  27. $page_size = isset($param['page_size']) ? trim($param['page_size']) : false;//每页展示条数
  28. $current_page = isset($param['current_page']) ? trim($param['current_page']) : false;//当前页
  29. $line_code = isset($param['line_code']) ? (empty($param['line_code']) ? "''" : "'" . trim($param['line_code']) . "'") : false;//线路编码或名称
  30. $start_date = isset($param['start_date']) ? (empty($param['start_date']) ? date('Y-m-d', time()) : trim($param['start_date'])) : false;//出发日期
  31. $end_date = isset($param['end_date']) ? (empty($param['end_date']) ? date('Y-m-d', time()) : trim($param['end_date'])) : false;//截止日期
  32. $bill_type = isset($param['bill_type']) ? trim($param['bill_type']) : false;//账单类型
  33. if (!$start_date || !$end_date || false === $bill_type) {
  34. $json['code'] = '2';
  35. $json['info'] = '缺少必要参数';
  36. return $json;
  37. }
  38. if (date('Y-m-d', strtotime($start_date)) > date('Y-m-d', time()) || date('Y-m-d', strtotime($end_date)) > date('Y-m-d', time())) {
  39. $end_date = date('Y-m-d', time());
  40. }
  41. $start_row = ($current_page - 1) * $page_size;
  42. $sql = "SELECT temp.code_id,temp.run_id,temp.run_bus_order_id as bus_order_id,temp.run_date,temp.run_time,group_concat(temp.line_code_name) AS line_code_name,temp.send_bus_no as bus_no,temp.send_tour_guide_name as guide_name,temp.send_driver_name as driver_name,concat(temp.seat_count,'座') as seat_count,temp.saled_count,temp.finance_type,temp.finance_status
  43. FROM(SELECT m.run_id,m.run_bus_order_id,m.run_date,m.run_time,
  44. m.parent_prod_id,
  45. (SELECT concat(line_code, ' ', line_name) FROM opera_line WHERE line_id in (select parent_prod_id from order_main where order_id = m.parent_order_id)) AS line_code_name,
  46. b.send_bus_no,b.send_tour_guide_name,b.seat_count,b.saled_count,b.id AS code_id,b.finance_status,b.send_driver_name,
  47. (SELECT type_name FROM dict_type WHERE id = b.finance_status) AS finance_type
  48. FROM order_main AS m,run_bus AS b
  49. WHERE m.cancel_flag = 0 AND b.cancel_flag = 0 AND m.run_date between '{$start_date}' and '{$end_date}' AND m.run_id = b.run_id AND m.run_bus_order_id = b.bus_order_id AND m.run_id != 0 AND m.cancel_flag = 0 AND b.cancel_flag = 0
  50. AND m.run_id not in (SELECT main.run_id from run_main main,run_bus bus where main.run_id=bus.run_id and main.PROD_ID>0 AND bus.group_line_flag=1)
  51. GROUP BY m.run_id,m.run_bus_order_id,m.parent_prod_id,run_date,run_time
  52. ) AS temp,
  53. opera_line as l
  54. WHERE temp.parent_prod_id = l.line_id
  55. " . $and_sql ."
  56. AND temp.run_date between '{$start_date}' and '{$end_date}'
  57. AND IF(TRIM({$line_code})='',0=0,(temp.line_code_name LIKE CONCAT('%',{$line_code},'%') OR code_id LIKE CONCAT('%',{$line_code},'%')))
  58. AND IF({$bill_type}=-1,0=0,temp.finance_status ={$bill_type})
  59. GROUP BY temp.run_id,temp.run_bus_order_id,temp.run_date,temp.run_time
  60. order by run_date desc";
  61. $sql_query = $sql . " limit " . $start_row . "," . $page_size; //查询数据列表
  62. $sql_count = "SELECT COUNT(*) as count from(" . $sql . ") as a"; //查询总条数
  63. $finance_list = $this->query($sql_query);
  64. if (false === $finance_list) {
  65. $json['code'] = '0';
  66. $json['info'] = '没有相关数据信息';
  67. $json['finance_list'] = array();
  68. $json['page']['page_size'] = $page_size;
  69. $json['page']['current_page'] = $current_page;
  70. $json['page']['total_count'] = '0';
  71. $json['page']['total_page'] = '0';
  72. return $json;
  73. }
  74. $total_count = $this->query($sql_count);
  75. $total_count = $total_count[0]['count'];
  76. $total_page = ceil($total_count / $page_size);
  77. $user_id = $this->user_id;
  78. if ($user_id == 143 || $user_id == 144) {
  79. $finance_status = 'check';
  80. } else {
  81. $finance_status = 'update';
  82. }
  83. $json['code'] = '0';
  84. $json['info'] = '返回列表信息成功';
  85. $json['finance_list'] = $finance_list;
  86. $json['finance_status'] = $finance_status;
  87. $json['page']['page_size'] = $page_size;
  88. $json['page']['current_page'] = $current_page;
  89. $json['page']['total_count'] = $total_count;
  90. $json['page']['total_page'] = $total_page;
  91. return $json;
  92. }
  93. /**
  94. * 报账配置数据
  95. * @return mixed
  96. */
  97. public function getAddFinanceBase($param)
  98. {
  99. $run_id = isset($param['run_id']) ? trim($param['run_id']) : false;//班次id
  100. $bus_order_id = isset($param['bus_order_id']) ? trim($param['bus_order_id']) : false;//车号
  101. if (!$run_id || !$bus_order_id) {
  102. $json['code'] = '2';
  103. $json['info'] = '缺少必要参数';
  104. return $json;
  105. }
  106. $run_bus_info = $this->getFinanceByRun($run_id, $bus_order_id);
  107. //1.收支类型
  108. $payment_type = Dictionary::payment_type();
  109. //2.费用科目
  110. $fee_type_sql = 'SELECT
  111. id,
  112. type_name
  113. FROM
  114. dict_type
  115. WHERE
  116. parent_id = 356';
  117. $fee_type = $this->query($fee_type_sql);
  118. //3.结算单位
  119. $supply_list_sql = 'SELECT
  120. id,
  121. supplier_name
  122. FROM
  123. base_supplier
  124. WHERE
  125. supplier_type = 187
  126. AND cancel_flag = 0';
  127. $supply_list = $this->query($supply_list_sql);
  128. //4.结算方式
  129. $settle_type = Dictionary::settle_type();
  130. $json['code'] = '0';
  131. $json['info'] = '返回配置数据成功';
  132. $json['payment_type'] = $payment_type;
  133. $json['fee_type'] = $fee_type;
  134. $json['supply_list'] = $supply_list;
  135. $json['settle_type'] = $settle_type;
  136. $json['run_bus_info'] = $run_bus_info;
  137. return $json;
  138. }
  139. /**
  140. * 提交报账
  141. * @param $param
  142. * @return mixed
  143. */
  144. public function addFinance($param)
  145. {
  146. $run_id = isset($param['run_id']) ? $param['run_id'] : false;//班次id
  147. $bus_order_id = isset($param['bus_order_id']) ? $param['bus_order_id'] : false;//车次id
  148. $finance_str = isset($param['finance_str']) ? $param['finance_str'] : false;//车次id
  149. $user_id = $this->user_id;
  150. $create_time = date('Y-m-d', time());
  151. if (!$run_id || !$bus_order_id || !$finance_str) {
  152. $json['code'] = '2';
  153. $json['info'] = "缺少必要参数";
  154. return $json;
  155. }
  156. $finance_list = substr($finance_str, 1, -1);
  157. $finance_list = explode('}{', $finance_list);
  158. $status = 0;
  159. foreach ($finance_list as $key => $value) {
  160. $value = substr($value, 0, -1);
  161. $finance_one = explode(',', $value);
  162. foreach ($finance_one as $k => $v) {
  163. if ($v == '') {
  164. $status = 1;
  165. break;
  166. }
  167. }
  168. $finance_list[$key] = $finance_one;
  169. }
  170. //有必填项没填
  171. if ($status == 1) {
  172. $json['code'] = '1';
  173. $json['info'] = '有必填项为空';
  174. return $json;
  175. }
  176. $insert_sql = 'INSERT INTO run_finance (create_user_id,create_time,update_user_id,update_time,cancel_flag,run_id,bus_order_id,payment_type,fee_type,supplier_id,unit_price,num,total_price,
  177. finance_date,finance_man,settle_type,sign_order_id,remark) VALUES';
  178. foreach ($finance_list as $key => $v) {
  179. $value1[] = '(' . $user_id . ',\'' . $create_time . '\',' . $user_id . ',\'' . $create_time . '\',0,' . $run_id . ',' . $bus_order_id . ',' . $v[0] . ',' . $v[1] . ',' . $v[2] . ',\'' . $v[3] . '\',' . $v[4] . ',\'' . $v[5] . '\',\'' . $v[6] . '\',\'' . $v[7] . '\',' . $v[8] . ',\'' . $v[9] . '\',\'' . $v[10] . '\')';
  180. }
  181. $insert_sql .= implode(',', $value1);
  182. $update_sql = "UPDATE run_bus
  183. SET finance_status = 137,
  184. update_user_id = " . $user_id . ",
  185. update_time = '" . $create_time . "'
  186. WHERE
  187. run_id = " . $run_id . "
  188. AND bus_order_id = " . $bus_order_id . "
  189. AND cancel_flag = 0";
  190. $insert_sql .= ';' . $update_sql;
  191. $result = $this->exec($insert_sql);
  192. if ($result) {
  193. $json['code'] = '0';
  194. $json['info'] = '提交报账成功';
  195. } else {
  196. $json['code'] = '1';
  197. $json['info'] = '数据库原因,提交报账失败';
  198. }
  199. return $json;
  200. }
  201. /**
  202. * 通过run_id和bus_order_id获取详情
  203. * @param $run_id
  204. * @param $bus_order_id
  205. * @return array
  206. */
  207. private function getFinanceByRun($run_id, $bus_order_id)
  208. {
  209. $sql = "SELECT
  210. run_id,run_bus_order_id as bus_order_id,run_date,run_time,
  211. group_concat(line_code_name) AS line_code_name,
  212. saled_count
  213. FROM
  214. (
  215. SELECT
  216. m.run_id,m.run_bus_order_id,m.run_date,m.run_time,
  217. (SELECT concat(line_code, ' ', line_name) FROM opera_line WHERE line_id = m.parent_prod_id) AS line_code_name,
  218. b.saled_count
  219. FROM
  220. order_main AS m,
  221. run_bus AS b
  222. WHERE
  223. m.run_id = b.run_id
  224. AND m.run_bus_order_id = b.bus_order_id
  225. AND m.run_id != 0
  226. AND m.cancel_flag = 0
  227. AND b.cancel_flag = 0
  228. AND m.run_id = " . $run_id . "
  229. AND b.bus_order_id = " . $bus_order_id . "
  230. GROUP BY
  231. m.run_id,m.run_bus_order_id,m.parent_prod_id,run_date,run_time
  232. ) AS temp
  233. GROUP BY
  234. run_id,run_bus_order_id,run_date,run_time
  235. LIMIT 1";
  236. $run_bus_info = $this->query($sql);
  237. $run_bus_info = $run_bus_info[0];
  238. return $run_bus_info;
  239. }
  240. /**
  241. * 获取报账详情
  242. * @param $param
  243. * @return mixed
  244. */
  245. public function getFinanceInfo($param)
  246. {
  247. $run_id = isset($param['run_id']) ? trim($param['run_id']) : false;//班次id
  248. $bus_order_id = isset($param['bus_order_id']) ? trim($param['bus_order_id']) : false;//车号
  249. if (!$run_id || !$bus_order_id) {
  250. $json['code'] = '2';
  251. $json['info'] = '缺少必要参数';
  252. return $json;
  253. }
  254. $run_bus_array = array('run_id' => $run_id, 'bus_order_id' => $bus_order_id);
  255. $data_info = $this->getAddFinanceBase($run_bus_array);
  256. unset($data_info['code']);
  257. unset($data_info['info']);
  258. $sql = "SELECT
  259. id,run_id,bus_order_id,payment_type,fee_type,supplier_id,unit_price,num,
  260. total_price,finance_date,finance_man,settle_type,sign_order_id,remark
  261. FROM
  262. run_finance
  263. WHERE
  264. cancel_flag = 0
  265. AND run_id = " . $run_id . "
  266. AND bus_order_id = " . $bus_order_id;
  267. $finance_list = $this->query($sql);
  268. $json['code'] = '0';
  269. $json['info'] = '获取报账信息成功';
  270. $json['finance_list'] = $finance_list;
  271. $json['data_info'] = $data_info;
  272. return $json;
  273. }
  274. /**
  275. * 修改或审核报账信息
  276. * @param $param
  277. */
  278. public function updateFinanceInfo($param)
  279. {
  280. $operate = isset($param['operate']) ? $param['operate'] : false;//修改update,审核check
  281. $run_id = isset($param['run_id']) ? $param['run_id'] : false;//班次id
  282. $bus_order_id = isset($param['bus_order_id']) ? $param['bus_order_id'] : false;//车次id
  283. $finance_str = isset($param['finance_str']) ? $param['finance_str'] : false;//报账的字符串
  284. $user_id = $this->user_id;
  285. $update_time = date('Y-m-d H:i:s', time());
  286. if (!$run_id || !$bus_order_id || !$finance_str || !$operate) {
  287. $json['code'] = '2';
  288. $json['info'] = "缺少必要参数";
  289. return $json;
  290. }
  291. $data_array = substr($finance_str, 1, -1);
  292. $data_array = explode('}{', $data_array);
  293. $i = -1;
  294. foreach ($data_array as $k => $v) {
  295. $v = explode(',', $v);
  296. if ($v[0] == 0) {
  297. $data_finance_array[$i]['id'] = $v[0];
  298. $data_finance_array[$i]['run_id'] = $run_id;
  299. $data_finance_array[$i]['bus_order_id'] = $bus_order_id;
  300. $data_finance_array[$i]['payment_type'] = $v[1];
  301. $data_finance_array[$i]['fee_type'] = $v[2];
  302. $data_finance_array[$i]['supplier_id'] = $v[3];
  303. $data_finance_array[$i]['unit_price'] = $v[4];
  304. $data_finance_array[$i]['num'] = $v[5];
  305. $data_finance_array[$i]['total_price'] = $v[6];
  306. $data_finance_array[$i]['finance_date'] = $v[7];
  307. $data_finance_array[$i]['finance_man'] = $v[8];
  308. $data_finance_array[$i]['settle_type'] = $v[9];
  309. $data_finance_array[$i]['sign_order_id'] = $v[10];
  310. $data_finance_array[$i]['remark'] = $v[11];
  311. } else {
  312. $data_finance_array[$v[0]]['id'] = $v[0];
  313. $data_finance_array[$v[0]]['run_id'] = $run_id;
  314. $data_finance_array[$v[0]]['bus_order_id'] = $bus_order_id;
  315. $data_finance_array[$v[0]]['payment_type'] = $v[1];
  316. $data_finance_array[$v[0]]['fee_type'] = $v[2];
  317. $data_finance_array[$v[0]]['supplier_id'] = $v[3];
  318. $data_finance_array[$v[0]]['unit_price'] = $v[4];
  319. $data_finance_array[$v[0]]['num'] = $v[5];
  320. $data_finance_array[$v[0]]['total_price'] = $v[6];
  321. $data_finance_array[$v[0]]['finance_date'] = $v[7];
  322. $data_finance_array[$v[0]]['finance_man'] = $v[8];
  323. $data_finance_array[$v[0]]['settle_type'] = $v[9];
  324. $data_finance_array[$v[0]]['sign_order_id'] = $v[10];
  325. $data_finance_array[$v[0]]['remark'] = $v[11];
  326. }
  327. }
  328. $run_bus = array('run_id' => $run_id, 'bus_order_id' => $bus_order_id);
  329. $db_array = $this->getFinanceInfo($run_bus);
  330. $db_array = $db_array['finance_list'];
  331. foreach ($db_array as $k => $v) {
  332. $db_finance_array[$v['id']] = $v;
  333. }
  334. $update_array = $this->compare($data_finance_array, $db_finance_array);
  335. //操作数据库
  336. if (isset($update_array['update']) && count($update_array['update']) > 0)//修改
  337. {
  338. $update_finance_array = array();
  339. foreach ($update_array['update'] as $key => $value) {
  340. unset($value['id']);
  341. $set_str = $this->getUpdateSet($value);
  342. $update_finance_array[] = "UPDATE run_finance
  343. SET update_user_id = " . $user_id . ",update_time = '" . $update_time . "'," . $set_str . "
  344. WHERE id = " . $key;
  345. }
  346. $update_finance_sql = implode(";", $update_finance_array);
  347. $update_sql[] = $update_finance_sql;
  348. }
  349. if (isset($update_array['insert']) && count($update_array['insert']) > 0)//增加
  350. {
  351. $insert_finance_sql = 'INSERT INTO run_finance (create_user_id,create_time,update_user_id,update_time,cancel_flag,run_id,bus_order_id,payment_type,fee_type,supplier_id,unit_price,num,total_price,finance_date,finance_man,
  352. settle_type,sign_order_id,remark) VALUES';
  353. foreach ($update_array['insert'] as $key => $v) {
  354. $finance_sql_array[] = "(" . $user_id . ",'" . $update_time . "'," . $user_id . ",'" . $update_time . "',0," . $v['run_id'] . "," . $v['bus_order_id'] . "," . $v['payment_type'] . "," . $v['fee_type'] . "," . $v['supplier_id'] . ",'" . $v['unit_price'] . "'," . $v['num'] . ",'" . $v['total_price'] . "','" . $v['finance_date'] . "','" . $v['finance_man'] . "'," . $v['settle_type'] . ",'" . $v['sign_order_id'] . "','" . $v['remark'] . "')";
  355. }
  356. $insert_finance_sql .= implode(",", $finance_sql_array);
  357. $update_sql[] = $insert_finance_sql;
  358. }
  359. if (isset($update_array['delete']) && count($update_array['delete']) > 0)//删除
  360. {
  361. $delete_finance_array = array();
  362. foreach ($update_array['delete'] as $key => $value) {
  363. $delete_finance_array[] = "UPDATE run_finance
  364. SET update_user_id = " . $user_id . ",update_time = '" . $update_time . "',cancel_flag = 1
  365. WHERE
  366. id = " . $key;
  367. }
  368. $delete_finance_sql = implode(";", $delete_finance_array);
  369. $update_sql[] = $delete_finance_sql;
  370. }
  371. if ($operate == 'check') { //管理员审核(包括修改)
  372. $update_run_bus_sql = "UPDATE run_bus
  373. SET finance_status = 341,update_user_id = " . $user_id . ",update_time = '" . $update_time . "'
  374. WHERE
  375. run_id = " . $run_id . " AND bus_order_id = " . $bus_order_id . " AND cancel_flag = 0";
  376. $update_sql[] = $update_run_bus_sql;
  377. $operate = '审核';
  378. } else {
  379. $operate = '修改';
  380. }
  381. $update_sql = implode(';', $update_sql);
  382. zzcsUtils::writeLog($update_sql);
  383. $result = $this->exec($update_sql);
  384. if ($result) {
  385. $json['code'] = '0';
  386. $json['info'] = $operate . '报账成功';
  387. } else {
  388. $json['code'] = '1';
  389. $json['info'] = '数据库异常,' . $operate . '报账失败';
  390. }
  391. return $json;
  392. }
  393. //比较数组(前台数组,数据库数组)
  394. private function compare($arr, $db_arr)
  395. {
  396. $result = array();
  397. if (count($db_arr) == 0 && count($arr) == 0) {
  398. return $result;
  399. } elseif (count($db_arr) == 0) {
  400. $result['insert'] = $arr;
  401. return $result;
  402. } elseif (count($arr) == 0) {
  403. $result['delete'] = $db_arr;
  404. return $result;
  405. } else {
  406. foreach ($arr as $key => $value) {
  407. if (isset($db_arr[$key])) {
  408. if ($arr != $db_arr) {
  409. $result['update'][$key] = $value;
  410. }
  411. } else {
  412. $result['insert'][$key] = $value;
  413. }
  414. }
  415. foreach ($db_arr as $key => $value) {
  416. if (!isset($arr[$key])) {
  417. $result['delete'][$key] = $value;
  418. }
  419. }
  420. return $result;
  421. }
  422. }
  423. //获取update操作中的set数据
  424. private function getUpdateSet($arr)
  425. {
  426. $set_array = array();
  427. foreach ($arr as $k => $v) {
  428. $set_array[] = $k . " = '" . $v . "'";
  429. }
  430. $set_str = implode(",", $set_array);
  431. return $set_str;
  432. }
  433. }