Income.php 12 KB

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