Invoice.php 7.4 KB

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