OrderInvoice.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace app\cxinv\controller;
  3. use app\cxinv\model\CompanyInfo;
  4. use app\cxinv\model\CustomerInvoice;
  5. use app\cxinv\model\Good;
  6. use app\cxinv\model\InvoicePool;
  7. use app\cxinv\model\QrdInfo;
  8. use think\App;
  9. class OrderInvoice extends Base
  10. {
  11. public function __construct(App $app)
  12. {
  13. parent::__construct($app);
  14. $this->model = new InvoicePool();
  15. }
  16. public function create(){
  17. $params = $this->request->param(["is_comon"=>0,"buy_id"=>"","buyNo"=>"",'companyNo'=>"","relaComNo"=>"","invtype"=>"",
  18. "email"=>"","remark"=>"","exam_remark"=>"","orderArr"=>[]],"post","trim");
  19. $validate = $this->validate($params,[
  20. "is_comon|是否通用订单"=>"require|in:0,1",
  21. "buy_id|买方信息"=>"requireIf:is_comon,0|number",
  22. "buyNo|买方信息"=>"requireIf:is_comon,1|max:255",
  23. "invtype|发票类型"=>"require|in:".implode(",",array_keys(\app\cxinv\model\Invoice::$invoiceType)),
  24. "orderArr|开票订单信息"=>"require|array"
  25. ]);
  26. if($validate!==true) return error($validate);
  27. if($params["is_comon"]==0){
  28. $buyer = Customerinvoice::where(["id"=>$params["buy_id"],"is_del"=>0])->findOrEmpty();
  29. }else{
  30. $buyer = CompanyInfo::where(["companyNo"=>$params["buyNo"],"is_del"=>0])->field("invoice_title,company_license invoice_code,company_address invoice_addr,
  31. mobile invoice_mobile,bank_name invoice_bank,bankNo invoice_bankNo")->findOrEmpty();
  32. }
  33. if($buyer->isEmpty()) return error("购买方发票信息不存在");
  34. $companyNo = $this->level==1? $params["companyNo"]:$params["relaComNo"];
  35. if($companyNo=="") return error("业务公司不能为空");
  36. $seller = CompanyInfo::where(["companyNo"=>$companyNo,"is_del"=>0])->findOrEmpty();
  37. if($seller->isEmpty()) return error("销售方发票信息不存在");
  38. $platform_type ="";
  39. $invNo = makeNo("INV");
  40. $invfee=array_sum(array_column($params["orderArr"],'inv_fee'));
  41. if(in_array($params["invtype"],['fully_digitalized_special_electronic','fully_digitalized_normal_electronic'])){
  42. //全电发票限制金额
  43. if (floatval($invfee) > 10000000) {
  44. return error('开票面额不能超过一千万元');
  45. }
  46. } else {//其他发票限制金额
  47. if ($seller['denomination'] != '' && floatval($invfee) > intval($seller['denomination']) * 10000) {
  48. return error( "开票面额不能超过{$seller['denomination']}万元");
  49. }
  50. }
  51. $assoc =[];
  52. foreach ($params["orderArr"] as $key=>$order){
  53. $Valid= $this->validate($order,[
  54. "sequenceNo|订单号"=>"require",
  55. "inv_fee|开票金额"=>"require|float|gt:0",
  56. ]);
  57. if($Valid!==true) return error($Valid);
  58. $orderInfo = QrdInfo::where(["sequenceNo"=>$order["sequenceNo"],"is_del"=>0])->findOrEmpty();
  59. if($orderInfo->isEmpty()) return error("{$order["sequenceNo"]}订单信息不存在");
  60. if($orderInfo->statu==2) return error("{$order["sequenceNo"]}订单不参与对账");
  61. if($orderInfo->winv_fee< $order["inv_fee"]) return error("{$order["sequenceNo"]}订单待开票金额不足");
  62. if($orderInfo->is_comon!=$params["is_comon"]) return error("{$order["sequenceNo"]}类型不匹配");
  63. $isCategory = Good::where(["spuCode"=>$orderInfo->goodNo,"status"=>2])->findOrEmpty();
  64. if($isCategory->isEmpty()) return error("{$order["sequenceNo"]}商品未确认开票类目");
  65. if($platform_type==""){
  66. $platform_type = $orderInfo->platform_type;
  67. }else{
  68. if($platform_type!=$orderInfo->platform_type) return error("{$order["sequenceNo"]}订单平台类型不一致");
  69. }
  70. $ascc[]=[
  71. "assocNo"=>makeNo("AS",str_pad(strval($key+1),4,"0",STR_PAD_LEFT)),
  72. "apply_id"=>$this->uid,
  73. "apply_name"=>$this->uname,
  74. "type"=>1,
  75. "orderCode"=>$orderInfo['sequenceNo'],
  76. "customerNo"=>$orderInfo['customerNo'],
  77. "viceCode"=>$invNo,
  78. "order_total"=>$orderInfo['totalPrice'],
  79. "vice_total"=>$invfee,
  80. "cancel_fee"=>$order['inv_fee'],
  81. "status"=>1,
  82. "addtime"=>date("Y-m-d H:i:s"),
  83. "updatetime"=>date("Y-m-d H:i:s")
  84. ];
  85. $invoice_good[]=[
  86. "invNo"=>$invNo,
  87. "orderCode"=>$orderInfo['sequenceNo'],
  88. "goodNo"=>$orderInfo['goodNo'],
  89. "goodName"=>$isCategory['inv_good_name']??$orderInfo['goodName'] ,
  90. "catName"=>$isCategory['inv_cat_name']??$orderInfo['thirdCat'],
  91. "catNo"=>$isCategory['inv_cat_code']??"",
  92. "goodPrice"=>$orderInfo['goodPrice'],
  93. "unitName"=>$orderInfo['goodUnit'],
  94. "goodNum"=>bcdiv($order["inv_fee"],$orderInfo['goodPrice'],8),
  95. "specInfo"=>$orderInfo['sequenceNo'],
  96. "totalPrice"=>$order['inv_fee'],
  97. "totalTax"=>$order['inv_fee'],
  98. "tax"=>$isCategory['inv_tax']??$orderInfo['tax'],
  99. "is_discount"=>$isCategory['is_discount']??0,
  100. "addTax"=>$isCategory['addTax']??'',
  101. "inv_tag"=>$isCategory['inv_tag']??"",
  102. "taxPrice"=>round($order['inv_fee']*($isCategory['inv_tax']??$orderInfo['tax']),2),
  103. ];
  104. }
  105. $inv=[
  106. "invNo"=>$invNo,
  107. "inv_value"=>$invfee,
  108. "inv_out"=> $companyNo,
  109. "inv_company"=> $seller['invoice_title']??"",
  110. "apply_id"=>$this->uid,
  111. "apply_name"=>$this->uname,
  112. "inv_type"=>$params["invtype"],//发票类型 专用 普通 电子专用 电子普通 全电子票
  113. "open_type"=>0, //开票类型 金税开票 金税线下 纯线下
  114. "platform_type"=>0, //开票类型 金税开票 金税线下 纯线下
  115. "is_ticket"=>$seller['out_ticket'], //是否支持金税开票
  116. "company_type"=> $buyer['company_type']??'01', //类型01 企业,02 机关事业单位,03 个人, 04 其他
  117. "remark"=>$params["remark"], //申请备注
  118. "exam_remark"=>$params["exam_remark"], //审核备注
  119. "ainv_fee"=>0,//已核销金额
  120. "winv_fee"=>$invfee,//未核销金额
  121. 'is_comon'=> $params["is_comon"],//是否通用订单 ,
  122. "status"=>1,// 带财务审核
  123. "is_del"=>0,
  124. "email"=>$params["email"],
  125. ];
  126. $invinfo=[
  127. "buyer_title"=>$buyer['invoice_title'],
  128. "buyer_code"=>$buyer['invoice_code'],
  129. "buyer_addr"=>$buyer['invoice_addr'],
  130. "buyer_mobile"=>$buyer['invoice_mobile'],
  131. "buyer_bank"=>$buyer['invoice_bank'],
  132. "buyer_bankNo"=>$buyer['invoice_bankNo'],
  133. "seller_title"=>$seller['company_name'],
  134. "seller_code"=>$seller['company_license'],
  135. "seller_addr"=>$seller['company_address'],
  136. "seller_mobile"=>$seller['mobile'],
  137. "seller_bank"=>$seller['bank_name'],
  138. "seller_bankNo"=>$seller['bankNo'],
  139. "invNo"=>$invNo,
  140. "voider"=>$seller['voider'],
  141. "payee"=>$seller['payee'],
  142. "drawer"=>$seller['drawer'],
  143. "reviewer"=>$seller['reviewer'],
  144. "ownerPlace"=>$seller['ownerPlace'],
  145. "addtime"=>date("Y-m-d H:i:s")
  146. ];
  147. $this->model->startTrans();
  148. try{
  149. $create = $this->model->create($inv);
  150. if($create->isEmpty()) throw new \Exception("发票创建失败");
  151. $up= $create->PoolInfo()->save($invinfo);
  152. if($up===false) throw new \Exception("发票信息创建失败");
  153. $create->assoc()->saveAll($ascc);
  154. $create->InvoiceGood()->saveAll($invoice_good);
  155. foreach ($params["orderArr"] as $order){
  156. $orderInfo = QrdInfo::where(["sequenceNo"=>$order["sequenceNo"],"is_del"=>0])->findOrEmpty();
  157. if($orderInfo->isEmpty()) throw new \Exception("{$order["sequenceNo"]}订单信息不存在");
  158. if($orderInfo->statu==2) throw new \Exception("{$order["sequenceNo"]}订单不参与对账");
  159. if($orderInfo->winv_fee< $order["inv_fee"]) throw new \Exception("{$order["sequenceNo"]}订单待开票金额不足");
  160. $up= $orderInfo->save([
  161. "winv_fee"=>bcsub($orderInfo->winv_fee,$order["inv_fee"],2),
  162. "inv_fee"=>bcadd($orderInfo->inv_fee,$order["inv_fee"],2),
  163. "inv_status"=>2,
  164. "status"=>1,
  165. ]);
  166. if($up===false) throw new \Exception("订单信息更新失败");
  167. }
  168. $this->model->commit();
  169. }catch (\Exception $e){
  170. $this->model->rollback();
  171. return error($e->getMessage());
  172. }
  173. event("report_code",["type"=>"add","key"=>"invNo","value"=>$invNo,"main"=>['key'=>'qrdNo','value'=>array_column($params["orderArr"],'sequenceNo')]]);
  174. return success("开票成功",["invNo"=>$invNo]);
  175. }
  176. }