Expense.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------------------------------
  4. * GouGuOPEN [ 左手研发,右手开源,未来可期!]
  5. +-----------------------------------------------------------------------------------------------
  6. * @Copyright (c) 2021~2024 http://www.gouguoa.com All rights reserved.
  7. +-----------------------------------------------------------------------------------------------
  8. * @Licensed 勾股OA,开源且可免费使用,但并不是自由软件,未经授权许可不能去除勾股OA的相关版权信息
  9. +-----------------------------------------------------------------------------------------------
  10. * @Author 勾股工作室 <hdm58@qq.com>
  11. +-----------------------------------------------------------------------------------------------
  12. */
  13. declare (strict_types = 1);
  14. namespace app\finance\controller;
  15. use app\base\BaseController;
  16. use app\finance\model\Expense as ExpenseModel;
  17. use app\finance\validate\ExpenseValidate;
  18. use think\exception\ValidateException;
  19. use think\facade\Db;
  20. use think\facade\View;
  21. class Expense extends BaseController
  22. {
  23. /**
  24. * 构造函数
  25. */
  26. protected $model;
  27. public function __construct()
  28. {
  29. parent::__construct(); // 调用父类构造函数
  30. $this->model = new ExpenseModel();
  31. }
  32. /**
  33. * 数据列表
  34. */
  35. public function datalist()
  36. {
  37. $param = get_params();
  38. if (request()->isAjax()) {
  39. $tab = isset($param['tab']) ? $param['tab'] : 0;
  40. $uid = $this->uid;
  41. $where = array();
  42. $whereOr = array();
  43. $where[]=['delete_time','=',0];
  44. if($tab == 0){
  45. //全部
  46. $auth = isAuthExpense($uid);
  47. if($auth == 0){
  48. $whereOr[] = ['admin_id', '=', $this->uid];
  49. $whereOr[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',check_uids)")];
  50. $whereOr[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',check_history_uids)")];
  51. $whereOr[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',check_copy_uids)")];
  52. $dids_a = get_leader_departments($uid);
  53. $dids_b = get_role_departments($uid);
  54. $dids = array_merge($dids_a, $dids_b);
  55. if(!empty($dids)){
  56. $whereOr[] = ['did','in',$dids];
  57. }
  58. }
  59. }
  60. if($tab == 1){
  61. //我创建的
  62. $where[] = ['admin_id', '=', $this->uid];
  63. }
  64. if($tab == 2){
  65. //待我审核的
  66. $where[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',check_uids)")];
  67. }
  68. if($tab == 3){
  69. //我已审核的
  70. $where[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',check_history_uids)")];
  71. }
  72. if($tab == 4){
  73. //抄送给我的
  74. $where[] = ['', 'exp', Db::raw("FIND_IN_SET('{$uid}',check_copy_uids)")];
  75. }
  76. if($tab == 5){
  77. //已打款的
  78. $where[] = ['pay_status', '=', 1];
  79. $auth = isAuthExpense($uid);
  80. if($auth == 0){
  81. $dids_a = get_leader_departments($uid);
  82. $dids_b = get_role_departments($uid);
  83. $dids = array_merge($dids_a, $dids_b);
  84. if(!empty($dids)){
  85. $whereOr[] = ['did','in',$dids];
  86. }
  87. $whereOr[] = ['admin_id', '=', $this->uid];
  88. }
  89. }
  90. //按时间检索
  91. if (!empty($param['diff_time'])) {
  92. $diff_time =explode('~', $param['diff_time']);
  93. $where[] = ['income_month', 'between', [strtotime(urldecode($diff_time[0])),strtotime(urldecode($diff_time[1].' 23:59:59'))]];
  94. }
  95. if (isset($param['pay_status']) && $param['pay_status'] != "") {
  96. $where[] = ['pay_status', '=', $param['pay_status']];
  97. }
  98. if (isset($param['check_status']) && $param['check_status'] != "") {
  99. $where[] = ['check_status', '=', $param['check_status']];
  100. }
  101. $list = $this->model->datalist($param,$where,$whereOr);
  102. return table_assign(0, '', $list);
  103. }
  104. else{
  105. return view();
  106. }
  107. }
  108. /**
  109. * 添加/编辑
  110. */
  111. public function add()
  112. {
  113. $param = get_params();
  114. if (request()->isAjax()) {
  115. $param['income_month'] = isset($param['income_month']) ? strtotime(urldecode($param['income_month'])) : 0;
  116. $param['expense_time'] = isset($param['expense_time']) ? strtotime(urldecode($param['expense_time'])) : 0;
  117. $amountData = isset($param['amount']) ? $param['amount'] : '0';
  118. $cost = 0;
  119. if ($amountData == 0) {
  120. return to_assign(1,'报销金额不完善');
  121. }
  122. else{
  123. foreach ($amountData as $key => $value) {
  124. if ($value == 0) {
  125. return to_assign(1,'第' . ($key + 1) . '条报销选项的金额不能为零');
  126. }
  127. else{
  128. $cost+=$value;
  129. }
  130. }
  131. }
  132. if($cost==0){
  133. return to_assign(1,'报销金额不能为零');
  134. }
  135. $param['admin_id'] = $this->uid;
  136. $param['did'] = $this->did;
  137. $param['cost'] = $cost;
  138. $param['pay_amount'] = $cost;
  139. if (!empty($param['id']) && $param['id'] > 0) {
  140. try {
  141. validate(ExpenseValidate::class)->scene('edit')->check($param);
  142. } catch (ValidateException $e) {
  143. // 验证失败 输出错误信息
  144. return to_assign(1, $e->getError());
  145. }
  146. if(!empty($param['loan_id'])){
  147. $loan = Db::name('Loan')->where('id',$param['loan_id'])->find();
  148. $balance_cost = Db::name('Expense')->where([['delete_time','=',0],['loan_id','=',$param['loan_id']],['id','<>',$param['id']]])->sum('balance_cost');
  149. $un_balance_cost = ($loan['cost']*100 - $balance_cost*100)/100;
  150. if($un_balance_cost<0){
  151. return to_assign(1,'所选借支已经冲抵完毕,不支持冲抵');
  152. }
  153. $balance_cost = $un_balance_cost*100 - $cost*100;
  154. if($balance_cost>=0){
  155. $param['balance_cost'] = $cost;
  156. $param['pay_amount'] = 0;
  157. }
  158. else{
  159. $param['balance_cost'] = $un_balance_cost;
  160. $param['pay_amount'] = ($cost *100 - $un_balance_cost*100)/100;
  161. }
  162. }
  163. $this->model->edit($param);
  164. } else {
  165. try {
  166. validate(ExpenseValidate::class)->scene('add')->check($param);
  167. } catch (ValidateException $e) {
  168. // 验证失败 输出错误信息
  169. return to_assign(1, $e->getError());
  170. }
  171. if(!empty($param['loan_id'])){
  172. $loan = Db::name('Loan')->where('id',$param['loan_id'])->find();
  173. $un_balance_cost = ($loan['cost']*100 - $loan['balance_cost']*100)/100;
  174. if($un_balance_cost<0){
  175. return to_assign(1,'所选借支已经冲抵完毕,不支持冲抵');
  176. }
  177. $balance_cost = $un_balance_cost*100 - $cost*100;
  178. if($balance_cost>=0){
  179. $param['balance_cost'] = $cost;
  180. $param['pay_amount'] = 0;
  181. }
  182. else{
  183. $param['balance_cost'] = $un_balance_cost;
  184. $param['pay_amount'] = ($cost *100 - $un_balance_cost*100)/100;
  185. }
  186. }
  187. $this->model->add($param);
  188. }
  189. }else{
  190. $id = isset($param['id']) ? $param['id'] : 0;
  191. $is_codeno = Db::name('DataAuth')->where('name','finance_admin')->value('conf_10');
  192. View::assign('is_codeno', $is_codeno);
  193. View::assign('expense_cate', Db::name('ExpenseCate')->where(['status' => 1])->select()->toArray());
  194. View::assign('user', get_admin($this->uid));
  195. if ($id>0) {
  196. $detail = $this->model->getById($id);
  197. View::assign('detail', $detail);
  198. if(is_mobile()){
  199. return view('qiye@/finance/add_expense');
  200. }
  201. return view('edit');
  202. }
  203. $codeno='';
  204. if($is_codeno==1){
  205. $codeno = get_codeno(3);
  206. }
  207. View::assign('codeno', $codeno);
  208. if(is_mobile()){
  209. return view('qiye@/finance/add_expense');
  210. }
  211. return view();
  212. }
  213. }
  214. /**
  215. * 查看
  216. */
  217. public function view($id)
  218. {
  219. $detail = $this->model->getById($id);
  220. if (!empty($detail)) {
  221. View::assign('detail', $detail);
  222. View::assign('create_user', get_admin($detail['admin_id']));
  223. if(is_mobile()){
  224. return view('qiye@/finance/view_expense');
  225. }
  226. return view();
  227. }
  228. else{
  229. return view(EEEOR_REPORTING,['code'=>404,'warning'=>'找不到页面']);
  230. }
  231. }
  232. /**
  233. * 删除
  234. */
  235. public function del()
  236. {
  237. $param = get_params();
  238. $id = isset($param['id']) ? $param['id'] : 0;
  239. if (request()->isDelete()) {
  240. $this->model->delById($id);
  241. } else {
  242. return to_assign(1, "错误的请求");
  243. }
  244. }
  245. //报销记录
  246. public function record()
  247. {
  248. if (request()->isAjax()) {
  249. $param = get_params();
  250. $where = [];
  251. $where[]=['delete_time','=',0];
  252. $where[]=['check_status','=',2];
  253. if(isAuthExpense($this->uid)==0){
  254. $where[] = ['admin_id', '=', $this->uid];
  255. }
  256. //按时间检索
  257. if (!empty($param['diff_time'])) {
  258. $diff_time =explode('~', $param['diff_time']);
  259. $where[] = ['expense_time', 'between', [strtotime(urldecode($diff_time[0])),strtotime(urldecode($diff_time[1].' 23:59:59'))]];
  260. }
  261. if (isset($param['pay_status']) && $param['pay_status'] != "") {
  262. $where[] = ['pay_status', '=', $param['pay_status']];
  263. }
  264. $list = $this->model->datalist($param,$where);
  265. $cost = $this->model::where($where)->sum('cost');
  266. $totalRow['cost'] = sprintf("%.2f",$cost);
  267. return table_assign(0, '', $list);
  268. } else {
  269. View::assign('authExpense',isAuthExpense($this->uid));
  270. return view();
  271. }
  272. }
  273. }