Payment.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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\Ticket as TicketModel;
  17. use app\finance\model\TicketPayment;
  18. use think\exception\ValidateException;
  19. use think\facade\Db;
  20. use think\facade\View;
  21. class Payment extends BaseController
  22. {
  23. /**
  24. * 构造函数
  25. */
  26. protected $model;
  27. public function __construct()
  28. {
  29. parent::__construct(); // 调用父类构造函数
  30. $this->model = new TicketModel();
  31. }
  32. public function datalist()
  33. {
  34. $auth = isAuthPayment($this->uid);
  35. if (request()->isAjax()) {
  36. $param = get_params();
  37. $where = array();
  38. $whereOr = array();
  39. $where[] = ['delete_time', '=', 0];
  40. $where[] = ['check_status', '=', 2];
  41. $where[] = ['open_status', '=', 1];
  42. $where[] = ['invoice_type','>',0];
  43. //按时间检索
  44. if (!empty($param['diff_time'])) {
  45. $diff_time =explode('~', $param['diff_time']);
  46. $where[] = ['pay_time', 'between', [strtotime(urldecode($diff_time[0])),strtotime(urldecode($diff_time[1].' 23:59:59'))]];
  47. }
  48. if (isset($param['pay_status']) && $param['pay_status']!='') {
  49. $where[] = ['pay_status', '=', $param['pay_status']];
  50. }
  51. if($auth == 0){
  52. $where[] = ['admin_id','=',$this->uid];
  53. }
  54. $list = $this->model->datalist($param,$where,$whereOr);
  55. return table_assign(0, '', $list);
  56. } else {
  57. View::assign('auth', $auth);
  58. return view();
  59. }
  60. }
  61. //查看
  62. public function add()
  63. {
  64. $param = get_params();
  65. $auth = isAuthPayment($this->uid);
  66. if (request()->isAjax()) {
  67. if($auth == 0){
  68. return to_assign(1, "你没有付款管理权限,请联系管理员或者HR");
  69. }
  70. $ticket_id = $param['ticket_id'];
  71. $admin_id = $this->uid;
  72. //计算已付款的金额
  73. $hasPay = TicketPayment::where(['ticket_id'=>$ticket_id,'status'=>1])->sum('amount');
  74. //查询发票金额
  75. $ticketAmount = $this->model->where(['id'=>$ticket_id])->value('amount');
  76. if($param['pay_type']==1){ //单个付款记录
  77. //相关内容多个数组
  78. $payPriceData=isset($param['amount'])? $param['amount'] : '';
  79. $payTimeData=isset($param['pay_time'])? $param['pay_time'] : '';
  80. $remarksData=isset($param['remarks'])? $param['remarks'] : '';
  81. //把合同协议关联的单个内容的发票入账明细重新添加
  82. if($payPriceData){
  83. $pay_price = 0;
  84. $insert = [];
  85. $time = time();
  86. foreach ($payPriceData as $key => $value) {
  87. if (!$value ) continue;
  88. $insert[] = [
  89. 'ticket_id' => $ticket_id,
  90. 'amount' => $value,
  91. 'pay_time' => $payTimeData[$key]? strtotime($payTimeData[$key]) : 0,
  92. 'remarks' => $remarksData[$key],
  93. 'admin_id' => $admin_id,
  94. 'create_time' => $time
  95. ];
  96. $pay_price += $value*100;
  97. }
  98. if(($pay_price + $hasPay*100)> $ticketAmount*100){
  99. return to_assign(1,'付款金额大于发票金额,不允许保存');
  100. }
  101. else{
  102. $res = TicketPayment::strict(false)->field(true)->insertAll($insert);
  103. if($res!==false){
  104. if(($pay_price + $hasPay*100) == $ticketAmount*100){
  105. //发票全部付款
  106. $this->model->where(['id'=>$ticket_id])->update(['pay_status'=>2,'pay_amount'=>$ticketAmount,'pay_time'=>time()]);
  107. }
  108. else if(($pay_price + $hasPay*100) < $ticketAmount*100){
  109. $payTotal=($pay_price + $hasPay*100)/100;
  110. //发票部分付款
  111. $this->model->where(['id'=>$ticket_id])->update(['pay_status'=>1,'pay_amount'=>$payTotal,'pay_time'=>time()]);
  112. }
  113. add_log('add',$ticket_id,$param);
  114. return to_assign();
  115. }
  116. else{
  117. return to_assign(1,'保存失败');
  118. }
  119. }
  120. }
  121. else{
  122. return to_assign(1,'提交的付款数据异常,请核对再提交');
  123. }
  124. }
  125. else if($param['pay_type']==2){ //全部付款记录
  126. $pay_price = ($ticketAmount*100-$hasPay*100)/100;
  127. $data = [
  128. 'ticket_id' => $ticket_id,
  129. 'amount' => $pay_price,
  130. 'pay_time' => isset($param['pay_time'])? strtotime($param['pay_time']) : 0,
  131. 'remarks' => '一次性全部付款',
  132. 'admin_id' => $admin_id,
  133. 'create_time' => time()
  134. ];
  135. $res = TicketPayment::strict(false)->field(true)->insertGetId($data);
  136. if($res!==false){
  137. //设置发票全部付款
  138. $this->model->where(['id'=>$ticket_id])->update(['pay_status'=>2,'pay_amount'=>$ticketAmount,'pay_time'=>time()]);
  139. add_log('add',$ticket_id,$param);
  140. return to_assign();
  141. }
  142. }
  143. else if ($param['pay_type']==3) {//全部反账记录
  144. //作废初始化发票付款数据
  145. $res = TicketPayment::where(['ticket_id'=>$ticket_id])->update(['status'=>'6','update_time'=>time()]);
  146. if($res!==false){
  147. //设置发票全部没付款
  148. $this->model->where(['id'=>$ticket_id])->update(['pay_status'=>0,'pay_amount'=>0,'pay_time'=>0]);
  149. add_log('tovoid',$ticket_id,$param);
  150. return to_assign();
  151. }
  152. }
  153. }
  154. else{
  155. if($auth == 0){
  156. return view(EEEOR_REPORTING,['code'=>405,'warning'=>'无权限访问']);
  157. }
  158. $id = isset($param['id']) ? $param['id']: 0 ;
  159. $detail = $this->model->getById($id);
  160. if(empty($detail)){
  161. return view(EEEOR_REPORTING,['code'=>404,'warning'=>'找不到记录']);
  162. }
  163. $detail['subject'] = Db::name('Enterprise')->where(['id' =>$detail['invoice_subject']])->value('title');
  164. $file_array = Db::name('File')->where('id','in',$detail['file_ids'])->select();
  165. $detail['file_array'] = $file_array;
  166. $other_file_array = Db::name('File')->where('id','in',$detail['other_file_ids'])->select();
  167. $detail['other_file_array'] = $other_file_array;
  168. if($detail['open_status']>0){
  169. $detail['open_admin_name'] = Db::name('Admin')->where('id','=',$detail['open_admin_id'])->value('name');
  170. }
  171. $not_pay = ($detail['amount']*100 - $detail['pay_amount']*100)/100;
  172. $detail['not_pay'] = sprintf("%.2f",$not_pay);
  173. //已付款的记录
  174. $detail['payment'] = TicketPayment::field('i.*,a.name as admin')
  175. ->alias('i')
  176. ->join('Admin a', 'a.id = i.admin_id', 'LEFT')
  177. ->where(['i.ticket_id'=>$id,'i.status'=>1])
  178. ->order('i.pay_time desc')
  179. ->select();
  180. View::assign('uid', $this->uid);
  181. View::assign('id', $id);
  182. View::assign('detail', $detail);
  183. if($detail['invoice_type'] == 0){
  184. return view('add_a');
  185. }
  186. return view();
  187. }
  188. }
  189. //查看
  190. public function view()
  191. {
  192. $id = empty(get_params('id')) ? 0 : get_params('id');
  193. $detail = $this->model->getById($id);
  194. if(empty($detail)){
  195. throw new \think\exception\HttpException(406, '找不到记录');
  196. }
  197. $detail['not_pay'] = ($detail['amount']*100 - $detail['pay_amount']*100)/100;
  198. //已付款的记录
  199. $detail['payment'] = TicketPayment::field('i.*,a.name as admin')
  200. ->alias('i')
  201. ->join('Admin a', 'a.id = i.admin_id', 'LEFT')
  202. ->where(['i.ticket_id'=>$id,'i.status'=>1])
  203. ->order('i.pay_time desc')
  204. ->select();
  205. $detail['subject'] = Db::name('Enterprise')->where(['id' =>$detail['invoice_subject']])->value('title');
  206. $file_array = Db::name('File')->where('id','in',$detail['file_ids'])->select();
  207. $detail['file_array'] = $file_array;
  208. $other_file_array = Db::name('File')->where('id','in',$detail['other_file_ids'])->select();
  209. $detail['other_file_array'] = $other_file_array;
  210. if($detail['open_status']>0){
  211. $detail['open_admin_name'] = Db::name('Admin')->where('id','=',$detail['open_admin_id'])->value('name');
  212. }
  213. View::assign('uid', $this->uid);
  214. View::assign('detail', $detail);
  215. if(is_mobile()){
  216. return view('qiye@/finance/view_payment');
  217. }
  218. return view();
  219. }
  220. //删除付款记录
  221. public function del()
  222. {
  223. $param = get_params();
  224. if (request()->isAjax()) {
  225. $payment =TicketPayment::where(['id'=>$param['id']])->find();
  226. $ticket = $this->model->where(['id'=>$payment['ticket_id']])->find();
  227. if($payment){
  228. $res = TicketPayment::where(['id'=>$param['id']])->update(['status'=>'6','update_time'=>time()]);
  229. if($res!==false){
  230. if($payment['amount']*100 == $ticket['amount']*100){
  231. //发票全部反付款
  232. $this->model->where(['id'=>$payment['ticket_id']])->update(['pay_status'=>0,'pay_amount'=>0,'pay_time'=>0]);
  233. }
  234. else if($payment['amount']*100 < $ticket['amount']*100){
  235. $payTotal=TicketPayment::where(['ticket_id'=>$payment['ticket_id'],'status'=>1])->sum('amount');
  236. //发票部分付款
  237. $this->model->where(['id'=>$payment['ticket_id']])->update(['pay_status'=>1,'pay_amount'=>$payTotal,'pay_time'=>time()]);
  238. }
  239. add_log('pay',$payment['ticket_id'],$ticket);
  240. return to_assign();
  241. }
  242. else{
  243. return to_assign(1,'操作失败');
  244. }
  245. }
  246. }
  247. }
  248. //回款记录
  249. public function record()
  250. {
  251. if (request()->isAjax()) {
  252. $param = get_params();
  253. $where = [];
  254. $where[]=['status','=',1];
  255. //按时间检索
  256. if (!empty($param['diff_time'])) {
  257. $diff_time =explode('~', $param['diff_time']);
  258. $where[] = ['pay_time', 'between', [strtotime(urldecode($diff_time[0])),strtotime(urldecode($diff_time[1].' 23:59:59'))]];
  259. }
  260. $model = new TicketPayment();
  261. $list = $model->datalist($param,$where);
  262. $amount = $model::where($where)->sum('amount');
  263. $totalRow['amount'] = sprintf("%.2f",$amount);
  264. return table_assign(0, '', $list);
  265. } else {
  266. return view();
  267. }
  268. }
  269. }