Invoice.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace app\cxinv\controller;
  3. use app\cxinv\model\InvoiceInfo;
  4. use app\user\model\Business;
  5. use app\user\model\Headquarters;
  6. use think\App;
  7. use think\facade\Validate;
  8. class Invoice extends Base{
  9. public function __construct(App $app) {
  10. parent::__construct($app);
  11. $this->model = new \app\cxinv\model\Invoice();
  12. }
  13. public function List(){
  14. $param= $this->request->param(["start"=>"","end"=>"","supplierNo"=>"","companyNo"=>"","status"=>"",'relaComNo'=>'',
  15. "invoice_number"=>"","invoice_type"=>"","page"=>1,"size"=>15,"apply_id"=>""],"post","trim");
  16. $where=[];
  17. $whereOr=[];
  18. if($param['start']!=='') $where[]=['createTime','>=',startTime($param['start'])];
  19. if($param['end']!=='') $where[]=['createTime','<=',endTime($param['end'])];
  20. if($param['supplierNo']!=='') $where[]=['supplierNo','like','%'.$param['supplierNo'].'%'];
  21. if($param['companyNo']!=='') $where[]=['companyNo','like','%'.$param['companyNo'].'%'];
  22. if($param['status']!=='') $where[]=['status','=',$param['status']];
  23. if($param['invoice_number']!=='') $where[]=['invoice_number','like','%'.$param['invoice_number'].'%'];
  24. if($param['invoice_type']!=='') $where[]=['invoice_type','=',$param['invoice_type']];
  25. if($param['apply_id']!=='') $where[]=['apply_id','=',$param['apply_id']];
  26. if($param['relaComNo']!=='') $where[]=['companyNo|supplierNo','=',$param['relaComNo']];
  27. if($this->level!=1) {
  28. $whereOr[]=['apply_id','=', $this->uid];
  29. $whereOr[]=['companyNo|supplierNo','=', ''];
  30. if($param['status']!=='') $whereOr[]=['status','=', $param['status']];
  31. if($param['start']!=='') $whereOr[]=['createTime','>=',startTime($param['start'])];
  32. if($param['end']!=='') $whereOr[]=['createTime','<=',endTime($param['end'])];
  33. if($param['invoice_number']!=='') $whereOr[]=['invoice_number','like','%'.$param['invoice_number'].'%'];
  34. if($param['invoice_type']!=='') $whereOr[]=['invoice_type','=',$param['invoice_type']];
  35. if ($param['supplierNo']!=='') $whereOr[]=['supplierNo','like','%'.$param['supplierNo'].'%'];
  36. if ($param['companyNo']!=='') $whereOr[]=['companyNo','like','%'.$param['companyNo'].'%'];
  37. }
  38. $list=$this->model
  39. ->where($where)
  40. ->whereOr(function($query)use($whereOr){
  41. $query->where($whereOr);
  42. })
  43. ->order('id desc')
  44. ->paginate(['page'=>$param['page'],'list_rows'=>$param['size']]);
  45. return success('获取成功',['list'=>$list->items(),'count'=>$list->total()]);
  46. }
  47. public function create(){
  48. $param= $this->request->param(["list"=>[]],"post","trim");
  49. if (!is_array($param['list'])) return error("请传入正确参数");
  50. $valid = Validate::rule([
  51. "invoice_code|发票代码"=>"max:255",
  52. "invoice_number|发票号码"=>"require|max:255|unique:app\cxinv\model\Invoice,invoice_number",
  53. "invoice_type|发票类型"=>"require|in:".implode(",",array_keys(\app\cxinv\model\Invoice::$invoiceType)),
  54. "invoice_total|发票税前金额"=>"float",
  55. "open_date|开票日期"=>"require|date|dateFormat:Y-m-d",
  56. "invoice_subtotal|发票税后金额"=>"float",
  57. "check_code|校验码"=>"max:255"
  58. ]);
  59. $number= array_column($param['list'],'invoice_number');
  60. if (count($number)!=count(array_unique($number))) return error("发票号码重复");
  61. foreach ($param['list'] as $k=>$v){
  62. if(!$valid->check($v)) return error($valid->getError());
  63. validate_invoice_info($v,$return);
  64. if($return['code']!=0) return error($v['invoice_number'].":".$return['message']);
  65. $param['list'][$k]['InvCode']=makeNo("FP",str_pad($k,4,"0",STR_PAD_RIGHT));
  66. $param['list'][$k]['apply_id']=$this->uid;
  67. $param['list'][$k]['apply_name']=$this->uname;
  68. }
  69. $this->model->saveAll($param['list']);
  70. return success("添加成功");
  71. }
  72. //0待验证 1待确认公司信息 2 待关联数据 3 关联中 4 关联完成
  73. public function save(){
  74. $param= $this->request->param([
  75. "id"=>"",
  76. "invoice_code"=>"",
  77. "invoice_number"=>"",
  78. "invoice_type"=>"",
  79. "invoice_total"=>"",
  80. "open_date"=>"",
  81. "invoice_subtotal"=>"",
  82. "check_code"=>"",
  83. "companyNo"=>"",
  84. "status"=>0,
  85. "supplierNo"=>""],"post","trim");
  86. $valide=Validate::rule([
  87. "id|发票编号id"=>"require|max:255",
  88. "invoice_code|发票代码"=>"requireIf:status,0|max:255",
  89. "invoice_number|发票号码"=>"requireIf:status,0|max:255|unique:app\cxinv\model\Invoice,invoice_number",
  90. "invoice_type|发票类型"=>"requireIf:status,0|in:".implode(",",array_keys(\app\cxinv\model\Invoice::$invoiceType)),
  91. "invoice_total|发票税前金额"=>"float",
  92. "open_date|开票日期"=>"requireIf:status,0|date|dateFormat:Y-m-d",
  93. "invoice_subtotal|发票税后金额"=>"float",
  94. "check_code|校验码"=>"max:255",
  95. "companyNo|业务公司"=>"requireIf:status,1|max:255",
  96. "supplierNo|供应商公司"=>"requireIf:status,1|max:255"
  97. ]);
  98. $invoice=$this->model->where("id",$param['id'])->findOrEmpty();
  99. if($invoice->isEmpty()) return error("发票编号不存在");
  100. $param['status'] = $invoice->status;
  101. if(!$valide->check($param)) return error($valide->getError());
  102. if ($invoice->status>1) return error("发票当前状态无法修改");
  103. if ($param['status']==0){
  104. $invoice->invoice_code=$param['invoice_code'];
  105. $invoice->invoice_number=$param['invoice_number'];
  106. $invoice->invoice_type=$param['invoice_type'];
  107. $invoice->invoice_total=$param['invoice_total'];
  108. $invoice->open_date=$param['open_date'];
  109. $invoice->invoice_subtotal=$param['invoice_subtotal'];
  110. $invoice->check_code=$param['check_code'];
  111. }
  112. if($param['status']==1){
  113. $invoice->companyNo=$param['companyNo'];
  114. $invoice->companyName=Business::where("companyNo",$param['companyNo'])->value("company","");
  115. $invoice->supplierNo=$param['supplierNo'];
  116. $invoice->supplierName=Headquarters::where("code",$param['supplierNo'])->value("name","");
  117. $invoice->status=2;
  118. }
  119. $invoice->save();
  120. return success("保存成功");
  121. }
  122. public function delete(){
  123. $param= $this->request->param(["InvCode"=>""],"post","trim");
  124. $valide=Validate::rule([
  125. "InvCode|发票编号"=>"require|max:255"
  126. ]);
  127. if(!$valide->check($param)) return error($valide->getError());
  128. $info=$this->model->where("InvCode",$param['InvCode'])->findOrEmpty();
  129. if($info->isEmpty()) return error("发票编号不存在");
  130. if(in_array($info->status,[3,4])) return error("发票状态不允许删除");
  131. $info->delete();
  132. return success("删除成功");
  133. }
  134. public function info(){
  135. $param= $this->request->param(["InvCode"=>""],"post","trim");
  136. $valide=Validate::rule([
  137. "InvCode|发票编号"=>"require|max:255"
  138. ]);
  139. if(!$valide->check($param)) return error($valide->getError());
  140. $info=$this->model->where("InvCode",$param['InvCode'])->findOrEmpty();
  141. if($info->isEmpty()) return error("发票编号不存在");
  142. $info['ticket'] = InvoiceInfo::where("hpNo",$param['InvCode'])->findOrEmpty();
  143. return success("获取成功",$info);
  144. }
  145. }