Payinv.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. namespace app\cxinv\controller;
  3. use app\cxinv\model\InvoiceOrder;
  4. use app\cxinv\model\PayInvoice;
  5. use app\cxinv\model\PayReturn;
  6. use app\cxinv\model\RoleAction;
  7. use app\user\model\AccountCompany;
  8. use think\App;
  9. use think\facade\Validate;
  10. class Payinv extends Base{
  11. public function __construct(App $app) {
  12. parent::__construct($app);
  13. $this->model =new PayInvoice();
  14. }
  15. public function create(){
  16. $params = $this->request->param(["InvCode"=>""],"post","trim");
  17. $valid= Validate::rule([
  18. 'InvCode' => 'require|max:255'
  19. ]);
  20. if (!$valid->check($params))return error($valid->getError());
  21. $isT = $this->model->where(["payNo"=>$params['InvCode'],"status"=>[1,2,3,4]])->findOrEmpty();
  22. if (!$isT->isEmpty())return error("该发票单号已存在");
  23. $invoice = \app\cxinv\model\Invoice::where("InvCode",$params['InvCode'])->findOrEmpty();
  24. if ($invoice->isEmpty())return error("该发票单号不存在");
  25. if ($invoice->status!=2)return error("该发票单号状态不正确");
  26. $hpNo=makeNo('hp');
  27. $data=[
  28. 'payNo'=>$params['InvCode'],
  29. 'hpNo'=>$hpNo,
  30. 'apply_id'=>$this->uid,
  31. 'apply_name'=>$this->uname,
  32. 'inv_fee'=> $invoice->invoice_total, //核算金额
  33. 'inv_subtotal_amount'=> $invoice->invoice_subtotal,//票面金额
  34. 'inv_amount'=> $invoice->invoice_total, //票面金额
  35. 'invType'=>4,//发票申请类型 1 手工添加 2 ocr识别 3 金税扫描 4 发票池
  36. 'invoiceType'=>$invoice->invoice_type,
  37. 'inv_img'=>'',
  38. 'invName'=>'',
  39. 'invStatus'=>1,
  40. 'invoiceNumber'=>$invoice->invoice_number,
  41. 'invoiceCode'=>$invoice->invoice_code,
  42. 'status'=>11,//待关联订单
  43. 'checkNumber'=>$invoice->check_code,
  44. 'open_time'=>$invoice->open_date,
  45. 'addtime'=>date('Y-m-d H:i:s'),
  46. 'updatetime'=>date('Y-m-d H:i:s')
  47. ];
  48. $this->model->startTrans();
  49. try{
  50. $res=$this->model->save($data);
  51. if(!$res) throw new \Exception("添加失败");
  52. $invoice->status=3;
  53. $up=$invoice->save();
  54. if(!$up) throw new \Exception("更新发票状态失败");
  55. $this->model->commit();
  56. } catch (\Exception $e){
  57. $this->model->rollback();
  58. return error($e->getMessage());
  59. }
  60. return success("添加成功");
  61. }
  62. public function list(){
  63. $params = $this->request->param(["page"=>1,"size"=>20,"status"=>"","payNo"=>"","apply_id"=>"","companyNo"=>"",
  64. "supplierNo"=>"","invoiceType"=>"",'relaComNo'=>'', "open_time"=>"", "invoiceNumber"=>"","start"=>"","end"=>"","hpNo"=>""]);
  65. $where=[["a.is_del","=",0],["invType","=",4]];
  66. $check =RoleAction::checkRole($this->roleid,[82,120]);
  67. if($check){
  68. $where[]=['a.apply_id','=',$this->uid];
  69. }
  70. if ($params['status']!='')$where[]=['a.status','=',$params['status']];
  71. if ($params['payNo']!='')$where[]=['a.payNo','like','%'.$params['payNo'].'%'];
  72. if ($params['apply_id']!='')$where[]=['a.apply_id','=',$params['apply_id']];
  73. if ($params['companyNo']!='')$where[]=['b.companyNo','=',$params['companyNo']];
  74. if ($params['supplierNo']!='')$where[]=['b.supplierNo','=',$params['supplierNo']];
  75. if ($params['invoiceType']!='')$where[]=['a.invoiceType','=',$params['invoiceType']];
  76. if ($params['open_time']!='')$where[]=['a.open_time','=',$params['open_time']];
  77. if ($params['invoiceNumber']!='')$where[]=['a.invoiceNumber','like','%'.$params['invoiceNumber'].'%'];
  78. if ($params['start']!='' && $params['end']!=''){
  79. $where[]=['a.addtime','between',[startTime($params['start']),endTime($params['end'])]];
  80. }
  81. if ($params['hpNo']!='')$where[]=['a.hpNo','like','%'.$params['hpNo'].'%'];
  82. if($params['relaComNo']!='') $where[]=['b.companyNo|b.supplierNo','=',$params['relaComNo']];
  83. $list = $this->model->alias('a')
  84. ->join('invoice b','a.payNo=b.InvCode')
  85. ->field('a.*,b.supplierNo,b.supplierName,b.companyName,b.companyNo')
  86. ->where($where)
  87. ->order('a.id desc')
  88. ->paginate(["list_rows"=>$params['size'],"page"=>$params['page']]);
  89. foreach ($list->items() as $k=>&$v){
  90. $v['has_account'] = AccountCompany::SupplierHasAcount($v['supplierNo']);
  91. $v['invoiceType_cn'] = \app\cxinv\model\Invoice::$invoiceType[$v['invoiceType']]??"";
  92. $v['invStatus_cn'] =\app\cxinv\model\Invoice::$invStatus[$v['invStatus']]??'';
  93. }
  94. return success("获取成功",["list"=>$list->items(),"count"=>$list->total()]);
  95. }
  96. public function info(){
  97. $params = $this->request->param(["hpNo"=>""],"post","trim");
  98. $valid= Validate::rule([
  99. 'hpNo' => 'require|max:255'
  100. ]);
  101. if (!$valid->check($params))return error($valid->getError());
  102. $info = $this->model->alias('a')
  103. ->join('invoice b','a.payNo=b.InvCode')
  104. ->field('a.*,b.supplierNo,b.supplierName,b.companyName,b.companyNo')
  105. ->where(["a.hpNo"=>$params['hpNo']])->findOrEmpty();
  106. if ($info->isEmpty())return error("该数据不存在");
  107. $info['invoiceType_cn'] = \app\cxinv\model\Invoice::$invoiceType[$info['invoiceType']]??"";
  108. $info['invStatus_cn'] =\app\cxinv\model\Invoice::$invStatus[$info['invStatus']]??'';
  109. return success("获取成功",$info);
  110. }
  111. /** 1待系统验证 2 买方公司审核 3带买方公司认证 4 认证成功 5验证失败 6买方审核驳回 7 认证失败 8 回票流程终止 9 验证超次数 10 回票已退
  112. * 11 待关联订单 12 订单关联完毕待财务审核 13 订单关联完毕财务驳回 14 待确认发票明细
  113. **/
  114. public function status(){
  115. $params = $this->request->param(["hpNo"=>"","status"=>"","remark"=>"",'ItemRemark'=>[]]);
  116. $valid= Validate::rule([
  117. 'hpNo' => 'require|max:255',
  118. "status"=>"require|number|in:1,2,3,4,6,7,8,11,13",
  119. "remark"=>"max:255",
  120. 'ItemRemark'=>'array'
  121. ]);
  122. if (!$valid->check($params))return error($valid->getError());
  123. $info = $this->model->where(["hpNo"=>$params['hpNo']])->findOrEmpty();
  124. if ($info->isEmpty())return error("该数据不存在");
  125. if($info->invType!=4) return error("该数据不是发票池数据");
  126. $invoice = \app\cxinv\model\Invoice::where(["InvCode"=>$info['payNo']])->findOrEmpty();
  127. if ($invoice->isEmpty())return error("该发票池数据不存在");
  128. if($invoice->status!=3) return error("该发票池数据状态不正确");
  129. if($params['status']==3) $info->check_time= date('Y-m-d H:i:s');
  130. if($params['status']==11 && $info->status==14){
  131. $itemLow = InvoiceItem::where(['invoiceCode'=>$params['hpNo']])->where([["amount","<",0]])->findOrEmpty();
  132. if (!$itemLow->isEmpty()) return error("存在负数明细,请先确认明细");
  133. }
  134. $info->status=$params['status'];
  135. $info->remark=$params['remark'];
  136. $this->model->startTrans();
  137. try{
  138. $save= $info->save();
  139. if ($save===false) throw new \Exception("修改失败");
  140. if (!empty($params['ItemRemark']) &&$params['status']==3){
  141. (new InvoiceOrder)->saveAll($params['ItemRemark']);
  142. }
  143. $this->model->commit();
  144. }catch (\Exception $e){
  145. $this->model->rollback();
  146. return error($e->getMessage());
  147. }
  148. return success("修改成功");
  149. }
  150. /**批量上传待认证发票
  151. * @param hpNo array 回票申请编号集合 必传
  152. * @param relaComNo string 业务公司编号 企业账户
  153. * @param companyNo string 业务公司编号 超管账户
  154. * @return \think\response\Json|void
  155. */
  156. //回票申请批量认证
  157. public function hpBatchImport()
  158. {
  159. $post = $this->request->only(['list' => [], 'relaComNo' => '', 'companyNo' => ''], 'post');
  160. $valid = Validate::rule([
  161. 'list|回票申请编号集合' => 'require|array|max:100',
  162. 'relaComNo|关联公司' => 'requireWithout:companyNo|max:255',
  163. 'companyNo|公司编号' => 'requireWithout:relaComNo|max:255',
  164. ]);
  165. if ($valid->check($post) == false) return error($valid->getError());
  166. $companyNo = $post['relaComNo'] ?: $post['companyNo'];
  167. $val_hpNo = Validate::rule([
  168. 'hpNo|回票申请编号' => 'require',
  169. 'status|审核状态' => 'require|number|in:4,7',
  170. 'remark|审核备注' => 'requireIf:status,7|max:255'
  171. ]);
  172. $unique=[];
  173. foreach ($post['list'] as $item) {
  174. if (!$val_hpNo->check($item)) return error($val_hpNo->getError());
  175. if(in_array($item['hpNo'],$unique))return error("{$item['hpNo']} 发票重复提交");
  176. $unique[]=$item['hpNo'];
  177. }
  178. $payArr = $this->model
  179. ->alias('a')
  180. ->leftJoin('invoice b', 'a.payNo=b.InvCode')
  181. ->where(['a.is_del' => 0])
  182. ->whereIn('a.hpNo', $unique)
  183. ->column('a.id,a.payNo,a.hpNo,a.invType,a.status,b.companyNo,a.remark', 'a.hpNo');
  184. if (empty($payArr)) return error("没有需要认证的发票");
  185. $this->model->startTrans();
  186. try {
  187. $saveAll=[];
  188. foreach ($post['list'] as $val) {
  189. if (!$val_hpNo->check($val)) throw new \Exception($val_hpNo->getError());
  190. if (!isset($payArr[$val['hpNo']])) throw new \Exception("{$val['hpNo']}记录不存在");
  191. $value = $payArr[$val['hpNo']];
  192. $payinfo =\app\cxinv\model\Invoice::where(['InvCode'=>$value['payNo'],'status'=>3])->findOrEmpty();
  193. if(empty($payinfo)) throw new \Exception("{$value['hpNo']}发票信息有误");
  194. if ($payinfo['companyNo'] == '') throw new \Exception("{$value['hpNo']}发票业务公司信息有误");
  195. if ($payinfo['companyNo'] != $companyNo) throw new \Exception("{$value['hpNo']}不属于当前业务公司发票");
  196. if ($value['status'] != '3') throw new \Exception("{$value['hpNo']}状态有误");
  197. $value['status'] = $val['status'];
  198. if ($val['remark'] != '') {
  199. $value['remark'] =$val['remark'];
  200. }
  201. $saveAll[]=$value;
  202. }
  203. $this->model->saveAll($saveAll);
  204. $this->model->commit();
  205. } catch (\Exception $e) {
  206. $this->model->rollback();
  207. return error( $e->getMessage());
  208. }
  209. return success('回票申请认证成功');
  210. }
  211. public function Back(){
  212. $param= $this->request->param(['hpNo'=>'','reason'=>''],'post','trim');
  213. $valide=Validate::rule([
  214. 'hpNo|回票编号'=>'require|max:255',
  215. 'reason|备注'=>'require|max:255'
  216. ]);
  217. if(!$valide->check($param)) return error($valide->getError());
  218. $info=$this->model->where('hpNo',$param['hpNo'])->findOrEmpty();
  219. if($info->isEmpty()) return error('回票编号不存在');
  220. if ($info->invType!=4) return error('该数据不是发票池数据');
  221. $invoice = \app\cxinv\model\Invoice::where(['InvCode'=>$info['payNo']])->findOrEmpty();
  222. if ($this->level == 2) {
  223. //判断是否开通供应商账号
  224. $rebool= AccountCompany::SupplierHasAcount($invoice->supplierNo);
  225. if ($rebool) return error( '该供应商已经开通账号,不允许当前账号操作');
  226. }
  227. if ($info->status!=4) return error('回票信息流程未完成');
  228. $isr = PayReturn::where(['orderCode'=>$param['hpNo'],'status'=>[1,2],'is_del'=>0])->findOrEmpty();
  229. if (!$isr->isEmpty()) return error('该订单已申请退票,不允许重复申请');
  230. $returnCode =makeNo('RP');
  231. $data = [
  232. 'returnCode'=>$returnCode,
  233. 'returnType'=>3,
  234. 'payNo'=>$info['payNo'],
  235. 'orderCode'=>$param['hpNo'],
  236. 'reason'=>$param['reason'],
  237. 'returnImg'=>'',
  238. 'remark'=>'',
  239. 'status'=>1,
  240. 'apply_id'=>$this->uid,
  241. 'apply_name'=>$this->uname,
  242. 'is_del'=>0,
  243. 'addtime'=>date('Y-m-d H:i:s'),
  244. 'updatetime'=>date('Y-m-d H:i:s'),
  245. ];
  246. $ce= PayReturn::create($data);
  247. if (!$ce) return error('申请退票创建失败');
  248. return success('申请退票创建成功');
  249. }
  250. public function BackList(){
  251. $param =$this->request->only(['relaComNo'=>'','companyNo'=>'','supplierNo'=>'','start'=>'','end'=>'','returnCode'=>'',
  252. 'status'=>'','InvCode'=>'','hpNo'=>'','page'=>1,'size'=>15],'post','trim');
  253. $condition=[['a.is_del','=',0],['a.returnType','=',3]];
  254. $check = RoleAction::checkRole($this->roleid,[132,130]);
  255. if($check) $condition[]=['a.apply_id','=',$this->uid];
  256. if($param['relaComNo']!='') $condition[]=['c.companyNo|c.supplierNo','=',$param['relaComNo']];
  257. if($param['companyNo']!='') $condition[]=['c.companyNo','=',$param['companyNo']];
  258. if($param['supplierNo']!='') $condition[]=['c.supplierNo','=',$param['supplierNo']];
  259. if($param['start']!='') $condition[]=['a.addtime','>=',date('Y-m-d H:i:s',strtotime($param['start']))];
  260. if($param['end']!='') $condition[]=['a.addtime','<=',date('Y-m-d 23:59:59',strtotime($param['end']))];
  261. if($param['status']!='') $condition[]=['a.status','=',$param['status']];
  262. if($param['hpNo']!='') $condition[]=['a.orderCode','=',$param['hpNo']];
  263. if($param['InvCode']!='') $condition[]=['c.InvCode','=',$param['InvCode']];
  264. if($param['returnCode']!='') $condition[]=['a.returnCode','=',$param['returnCode']];
  265. $list= PayReturn::alias('a')
  266. ->join('pay_invoice b','a.orderCode=b.hpNo','left')
  267. ->join('invoice c','b.payNo=c.InvCode','left')
  268. ->where($condition)
  269. ->field('a.*,c.InvCode,c.supplierNo,c.supplierName,c.companyNo,c.companyName,c.invoice_total,c.invoice_subtotal,c.open_date,c.invoice_code,c.invoice_number')
  270. ->order('a.id desc')
  271. ->paginate(['list_row'=>$param['size'],'page'=>$param['page']]);
  272. foreach ($list as $k=>&$v){
  273. $v['has_account'] = AccountCompany::SupplierHasAcount($v['supplierNo']);
  274. }
  275. return success('获取成功',['list'=>$list->items(),'count'=>$list->total()]);
  276. }
  277. public function BackInfo(){
  278. $param =$this->request->only(['returnCode'=>''],'post','trim');
  279. $valide=Validate::rule([
  280. 'returnCode|退票申请编号'=>'require|max:255',
  281. ]);
  282. if(!$valide->check($param))return error($valide->getError());
  283. $info= PayReturn::alias('a')
  284. ->join('pay_invoice b','a.orderCode=b.hpNo','left')
  285. ->join('invoice c','b.payNo=c.InvCode','left')
  286. ->where(['returnCode'=>$param['returnCode'],'returnType'=>3,'a.is_del'=>0])
  287. ->field('a.*,c.InvCode,c.supplierNo,c.supplierName,c.companyNo,c.companyName,c.invoice_total,c.invoice_subtotal,c.open_date,c.invoice_code,c.invoice_number')
  288. ->findOrEmpty();
  289. if($info->isEmpty()) return error('退票申请编号不存在');
  290. $info['invoice'] = $this->model->with(['items'])->where(['hpNo'=>$info->orderCode])->findOrEmpty();
  291. return success('获取成功',$info);
  292. }
  293. public function BackStatus(){
  294. $param =$this->request->only(['returnCode'=>'','status'=>'','remark'=>'','returnImg'=>''],'post','trim');
  295. $valide=Validate::rule([
  296. 'returnCode|退票申请编号'=>'require|max:255',
  297. 'status|状态'=>'require|number|in:1,2,3',
  298. ]);
  299. if(!$valide->check($param))return error($valide->getError());
  300. $info= PayReturn::where(['returnCode'=>$param['returnCode'],'returnType'=>3,'is_del'=>0])->findOrEmpty();
  301. if($info->isEmpty()) return error('退票申请编号不存在');
  302. $invoice = $this->model->where(['hpNo'=>$info['orderCode'],'is_del'=>0])->findOrEmpty();
  303. if($invoice->isEmpty()) return error('发票信息不存在');
  304. if($invoice->status!=4) return error('发票申请状态不允许退票');
  305. $info->status=2;
  306. $info->remark=$param['remark'];
  307. $info->returnImg=$param['returnImg'];
  308. $info->updatetime=date('Y-m-d H:i:s');
  309. $this->model->startTrans();
  310. try{
  311. $up=$info->save();
  312. if(!$up) throw new \Exception('审批失败');
  313. if($param['status']==2){
  314. $invoice->status=10;
  315. $invoice->updatetime=date('Y-m-d H:i:s');
  316. $up2=$invoice->save();
  317. if(!$up2) throw new \Exception('审批失败');
  318. }
  319. $this->model->commit();
  320. }catch (\Exception $e){
  321. $this->model->rollback();
  322. return error('审批失败');
  323. }
  324. return success('审批成功');
  325. }
  326. }