OrderInv.php 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\BaseController;
  4. use app\admin\model\ReportCode;
  5. use think\App;
  6. use think\Exception;
  7. use think\facade\Config;
  8. use think\facade\Db;
  9. use think\facade\Cache;
  10. class OrderInv extends BaseController{
  11. protected $typename;
  12. protected $invoiceType;
  13. protected $invName;
  14. protected $kingInvoice;
  15. public function __construct(App $app) {
  16. parent::__construct($app);
  17. $invoice =Config::get("invoiceType");
  18. $this->invoiceType = $invoice['invoiceName'];
  19. $this->invName = $invoice['invoiceType'];
  20. $this->typename = $invoice['KingName'];
  21. $this->kingInvoice = $invoice['KingInvoice'];
  22. }
  23. /**
  24. * 新建销售单开票申请
  25. */
  26. public function create(){
  27. $companyType = isset($this->post['companyType'])&&$this->post['companyType']!=='' ? trim($this->post['companyType']):"";
  28. if ($companyType==''){
  29. return error_show(1004,"参数 companyType 不能为空");
  30. }
  31. $buy_id = isset($this->post['buy_id'])&&$this->post['buy_id']!=='' ? intval($this->post['buy_id']):"";
  32. if($buy_id==''){
  33. return error_show(1004,"参数 buy_id 不能为空");
  34. }
  35. $buyinfo =Db::name("customer_invoice")->where(['id'=>$buy_id,"is_del"=>0])->find();
  36. if($buyinfo==false){
  37. return error_show(1004,"购买方发票信息未找到");
  38. }
  39. if($this->level==1){
  40. $companyNo = isset($this->post['companyNo'])&&$this->post['companyNo']!=='' ? trim($this->post['companyNo']):"";
  41. }
  42. else{
  43. $companyNo = isset($this->post['relaComNo'])&&$this->post['relaComNo']!=='' ? trim($this->post['relaComNo'])
  44. :"";
  45. }
  46. if($companyNo==''){
  47. return error_show(1004,"业务公司不能为空");
  48. }
  49. $company = Db::name("company_info")->where(["companyNo"=>$companyNo])->find();
  50. if($company==false){
  51. return error_show(1004,"销售方信息未找到");
  52. }
  53. // $khNo = isset($this->post['khNo'])&&$this->post['khNo']!=='' ? trim($this->post['khNo']):"";
  54. // if($khNo==''){
  55. // return error_show(1004,"参数 khNo 不能为空");
  56. // }
  57. // $customer = Db::name("customer_info")->where(["companyNo"=>$khNo])->find();
  58. // if($customer==false){
  59. // return error_show(1004,"客户信息未找到");
  60. // }
  61. $invtype = isset($this->post['invtype'])&&$this->post['invtype']!=='' ? trim($this->post['invtype']):"";
  62. if($invtype==''){
  63. return error_show(1004,"参数 invtype 不能为空");
  64. }
  65. if(!in_array($invtype,$this->invName)) return error_show(1004,"发票类型有误");
  66. $email = isset($this->post['email'])&&$this->post['email']!=='' ? trim($this->post['email']):"";
  67. $remark = isset($this->post['remark'])&&$this->post['remark']!=='' ? trim($this->post['remark']):"";
  68. $exam_remark = isset($this->post['exam_remark'])&&$this->post['exam_remark']!=='' ? trim($this->post['exam_remark']):"";
  69. $orderArr = isset($this->post['orderArr'])&&!empty($this->post['orderArr']) ? $this->post['orderArr']:[];
  70. if(empty($orderArr)){
  71. return error_show(1004,"参数 orderArr 不能为空");
  72. }
  73. $temp =[];
  74. $good=[];
  75. $invNo=makeNo("INV");
  76. $invfee=array_sum(array_column($orderArr,'inv_fee'));
  77. if($company['denomination']!=''&& $invfee>intval($company['denomination'])*10000){
  78. return error_show(1004,"开票面额不能超过{$company['denomination']}万元");
  79. }
  80. Db::startTrans();
  81. try{
  82. foreach ($orderArr as $value){
  83. if(!isset($value['sequenceNo']) ||$value['sequenceNo']==''){
  84. Db::rollback();
  85. return error_show(1004,"参数 sequenceNo 不能为空");
  86. }
  87. $qrd = Db::name("qrd_info")->where(["sequenceNo"=>$value['sequenceNo']])->find();
  88. if($qrd['status']==2){
  89. Db::rollback();
  90. return error_show(1004,"确认单{$value['sequenceNo']}不参与对账");
  91. }
  92. if(!isset($value['inv_fee']) || $value['inv_fee']===''){
  93. Db::rollback();
  94. return error_show(1004,"参数 inv_fee 不能为空");
  95. }
  96. if($qrd['winv_fee']<$value['inv_fee']){
  97. Db::rollback();
  98. return error_show(1004,"确认单{$value['sequenceNo']}待开票金额不足");
  99. }
  100. $good_info = Db::name("good")->where(["spuCode"=>$qrd["goodNo"],"status"=>1])->findOrEmpty();
  101. if(empty($good_info)){
  102. Db::rollback();
  103. return error_show(1004,"确认单{$value['sequenceNo']}商品未确认开票类目");
  104. }
  105. $num = DB::name("assoc")->alias("a")->leftJoin("invoice_good b","a.viceCode=b.invNo")
  106. ->where(["a.status"=>[1,2],"a.is_del"=>0,"a.type"=>1,"a.orderCode"=>$value['sequenceNo']])->sum("b.goodNum");
  107. if($qrd['goodNum']-$num<$value['num']){
  108. Db::rollback();
  109. return error_show(1004,"确认单{$value['sequenceNo']}待开票数量不足");
  110. }
  111. $ascc=[
  112. "assocNo"=>makeNo("AS"),
  113. "apply_id"=>$this->uid,
  114. "apply_name"=>$this->uname,
  115. "type"=>1,
  116. "orderCode"=>$value['sequenceNo'],
  117. "customerNo"=>$qrd['customerNo'],
  118. "viceCode"=>$invNo,
  119. "order_total"=>$qrd['totalPrice'],
  120. "vice_total"=>$invfee,
  121. "cancel_fee"=>$value['inv_fee'],
  122. "status"=>1,
  123. "addtime"=>date("Y-m-d H:i:s"),
  124. "updatetime"=>date("Y-m-d H:i:s")
  125. ];
  126. $invoice_good=[
  127. "invNo"=>$invNo,
  128. "orderCode"=>$value['sequenceNo'],
  129. "goodNo"=>$qrd['goodNo'],
  130. "goodName"=>$good_info['inv_good_name']??$qrd['goodName'] ,
  131. "catName"=>$good_info['inv_cat_name']??$qrd['thirdCat'],
  132. "catNo"=>$good_info['inv_cat_code']??"",
  133. "goodPrice"=>round($value['inv_fee']/$value['num'],8),
  134. "unitName"=>$qrd['goodUnit'],
  135. "goodNum"=>$value['num'],
  136. "specInfo"=>$value['sequenceNo'],
  137. "totalPrice"=>$value['inv_fee'],
  138. "tax"=>$good_info['inv_tax']??$qrd['tax'],
  139. "is_discount"=>$good_info['is_discount']??0,
  140. "addTax"=>$good_info['addTax']??'',
  141. "inv_tag"=>$good_info['inv_tag']??"",
  142. "taxPrice"=>round($value['inv_fee']*($good_info['inv_tax']??$qrd['tax']),2),
  143. "addtime"=>date("Y-m-d H:i:s"),
  144. "updatetime"=>date("Y-m-d H:i:s")
  145. ];
  146. $update = [
  147. "inv_fee"=>$qrd['inv_fee']+$value['inv_fee'],
  148. "winv_fee"=>$qrd['winv_fee']-$value['inv_fee'],
  149. "inv_status"=>2,
  150. "status"=>1,
  151. "updatetime"=>date("Y-m-d H:i:s")
  152. ];
  153. $up = Db::name("qrd_info")->where($qrd)->update($update);
  154. if($up==false){
  155. Db::rollback();
  156. return error_show(1005,"确认单{$value['sequenceNo']} 更新失败");
  157. }
  158. $temp[]=$ascc;
  159. $good[]=$invoice_good;
  160. $report=ReportCode::where(["qrdNo"=>$value['sequenceNo']])->find();
  161. if($report)$report->setField("invNo",$invNo);
  162. }
  163. $associn = Db::name("assoc")->insertAll($temp);
  164. if($associn==0){
  165. Db::rollback();
  166. return error_show(1005,"确认单申请开票失败");
  167. }
  168. $goodin = Db::name("invoice_good")->insertAll($good);
  169. if($goodin==0){
  170. Db::rollback();
  171. return error_show(1005,"确认单申请开票失败");
  172. }
  173. $inv=[
  174. "invNo"=>$invNo,
  175. "inv_value"=>$invfee,
  176. "inv_out"=> $companyNo,
  177. "inv_company"=> $company['company_name'],
  178. "apply_id"=>$this->uid,
  179. "apply_name"=>$this->uname,
  180. "inv_type"=>$invtype,//发票类型 专用 普通 电子专用 电子普通 全电子票
  181. "open_type"=>0, //开票类型 金税开票 金税线下 纯线下
  182. "is_ticket"=>$company['out_ticket'], //是否支持金税开票
  183. "company_type"=> $buyinfo['company_type']??'01', //类型01 企业,02 机关事业单位,03 个人, 04 其他
  184. "remark"=>$remark, //申请备注
  185. "exam_remark"=>$exam_remark, //审核备注
  186. "ainv_fee"=>0,//已核销金额
  187. "winv_fee"=>$invfee,//未核销金额
  188. "status"=>1,// 带财务审核
  189. "is_del"=>0,
  190. "email"=>$email,
  191. "addtime"=>date("Y-m-d H:i:s"),
  192. "updatetime"=>date("Y-m-d H:i:s")
  193. ];
  194. $invin = Db::name("invoice_pool")->insert($inv);
  195. if($invin){
  196. $invinfo=[
  197. "buyer_title"=>$buyinfo['invoice_title'],
  198. "buyer_code"=>$buyinfo['invoice_code'],
  199. "buyer_addr"=>$buyinfo['invoice_addr'],
  200. "buyer_mobile"=>$buyinfo['invoice_mobile'],
  201. "buyer_bank"=>$buyinfo['invoice_bank'],
  202. "buyer_bankNo"=>$buyinfo['invoice_bankNo'],
  203. "seller_title"=>$company['company_name'],
  204. "seller_code"=>$company['company_license'],
  205. "seller_addr"=>$company['company_address'],
  206. "seller_mobile"=>$company['mobile'],
  207. "seller_bank"=>$company['bank_name'],
  208. "seller_bankNo"=>$company['bankNo'],
  209. "invNo"=>$invNo,
  210. "voider"=>$company['voider'],
  211. "payee"=>$company['payee'],
  212. "drawer"=>$company['drawer'],
  213. "reviewer"=>$company['reviewer'],
  214. "ownerPlace"=>$company['ownerPlace'],
  215. "addtime"=>date("Y-m-d H:i:s")
  216. ];
  217. $poolinfo =Db::name("invoice_pool_info")->insert($invinfo);
  218. if($poolinfo){
  219. Db::commit();
  220. return app_show(0,"确认单发票申请成功",["invNo"=>$invNo]);
  221. }
  222. }
  223. Db::rollback();
  224. return error_show(1004,"确认单发票申请失败");
  225. }catch (\Exception $e){
  226. Db::rollback();
  227. return error_show(1004,$e->getMessage()."|".$e->getLine());
  228. }
  229. }
  230. // 1财务审核通过 2 金税开票中/验票中 3 开票成功 4 财务驳回 5 开票失败 6 发票退票/废弃 7取消申请
  231. public function status(){
  232. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  233. if($invNo==""){
  234. return error_show(1004,"参数 invNo 不能为空");
  235. }
  236. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  237. if($invinfo==false){
  238. return error_show(1004,"发票申请数据未找到");
  239. }
  240. $status= isset($this->post['status'])&&$this->post['status']!==''?intval($this->post['status']):"";
  241. if($status===''){
  242. return error_show(1004,"参数 status 不能为空");
  243. }
  244. //open_type 1 金税开票 2 金税线下开票 3 线下开票
  245. $open_type = isset($this->post['open_type'])&&$this->post['open_type']!==''?intval($this->post['open_type']):"";
  246. $remark = isset($this->post['remark'])&&$this->post['remark']!=''?trim($this->post['remark']):"";
  247. $invCode =isset($this->post['invCode'])&&$this->post['invCode']!=''?trim($this->post['invCode']):"";
  248. $invNum =isset($this->post['invNum'])&&$this->post['invNum']!=''?trim($this->post['invNum']):"";
  249. $open_date =isset($this->post['open_date'])&&$this->post['open_date']!=''?trim($this->post['open_date']):"";
  250. $subtotal_fee =isset($this->post['subtotal_fee'])&&$this->post['subtotal_fee']!=''?floatval($this->post['subtotal_fee']):"";
  251. $total_fee =isset($this->post['total_fee'])&&$this->post['total_fee']!=''?floatval($this->post['total_fee']):"0";
  252. $checkCode =isset($this->post['checkCode'])&&$this->post['checkCode']!=''?trim($this->post['checkCode']):"";
  253. $seller_id =isset($this->post['seller_id'])&&$this->post['seller_id']!=''?trim($this->post['seller_id']):"";
  254. $seller_title =isset($this->post['seller_title'])&&$this->post['seller_title']!=''?trim($this->post['seller_title']):"";
  255. $seller_addr =isset($this->post['seller_addr'])&&$this->post['seller_addr']!=''?trim($this->post['seller_addr']):"";
  256. $seller_mobile =isset($this->post['seller_mobile'])&&$this->post['seller_mobile']!=''?trim($this->post['seller_mobile']):"";
  257. $seller_bank =isset($this->post['seller_bank'])&&$this->post['seller_bank']!=''?trim($this->post['seller_bank']):"";
  258. $seller_bankNo =isset($this->post['seller_bankNo'])&&$this->post['seller_bankNo']!=''?trim($this->post['seller_bankNo']):"";
  259. $buyer_id =isset($this->post['buyer_id'])&&$this->post['buyer_id']!=''?trim($this->post['buyer_id']):"";
  260. $buyer_title =isset($this->post['buyer_title'])&&$this->post['buyer_title']!=''?trim($this->post['buyer_title']):"";
  261. $buyer_addr =isset($this->post['buyer_addr'])&&$this->post['buyer_addr']!=''?trim($this->post['buyer_addr']):"";
  262. $buyer_mobile =isset($this->post['buyer_mobile'])&&$this->post['buyer_mobile']!=''?trim($this->post['buyer_mobile']):"";
  263. $buyer_bank =isset($this->post['buyer_bank'])&&$this->post['buyer_bank']!=''?trim($this->post['buyer_bank']):"";
  264. $buyer_bankNo =isset($this->post['buyer_bankNo'])&&$this->post['buyer_bankNo']!=''?trim($this->post['buyer_bankNo']):"";
  265. $tax_fee =isset($this->post['tax_fee'])&&$this->post['tax_fee']!=''?trim($this->post['tax_fee']):"";
  266. $invimg =isset($this->post['invimg'])&&$this->post['invimg']!=''?trim($this->post['invimg']):"";
  267. $update=[
  268. "status"=> $status,
  269. "exam_remark"=>$remark,
  270. "updatetime" => date("Y-m-d H:i:s")
  271. ];
  272. if($status==2){
  273. if($open_type==='')return error_show(1004,"参数 open_type 不能为空");
  274. $update['open_type']=$open_type;
  275. if($open_type!=1){
  276. if($invCode==='') return error_show(1004,"参数 invCode 不能为空");
  277. if($invNum==='') return error_show(1004,"参数 invNum 不能为空");
  278. if($open_date==='') return error_show(1004,"参数 open_date 不能为空");
  279. if(in_array($invinfo['inv_type'],['normal','roll',"toll",'electronic'])){
  280. if($checkCode==='') return error_show(1004,"参数 checkCode 不能为空");
  281. }
  282. if($subtotal_fee==='') return error_show(1004,"参数 subtotal_fee 不能为空");
  283. //全电子发票 信息需要全填补充切无法接口识别验真
  284. if(in_array($invinfo['inv_type'],['fully_digitalized_special_electronic','fully_digitalized_normal_electronic'])){
  285. if($invNum==='') return error_show(1004,"参数 invNum 不能为空");
  286. if($open_date==='') return error_show(1004,"参数 open_date 不能为空");
  287. if($subtotal_fee==='') return error_show(1004,"参数 subtotal_fee 不能为空");
  288. if($total_fee=='') return error_show(1004,"参数 total_fee 不能为空");
  289. $status=3;
  290. }
  291. }
  292. }
  293. if($status==7){
  294. if(!in_array($invinfo['status'],[4,5])) return error_show(1004,"开票申请状态有误");
  295. }
  296. $update['status']=$status;
  297. Db::startTrans();
  298. try{
  299. $invup =Db::name("invoice_pool")->where($invinfo)->update($update);
  300. if($invup){
  301. if(($status==2&& $open_type!=1) || ($invinfo['inv_type']==5 &&$status==2)){
  302. $invpool=[
  303. "invNo"=>$invinfo['invNo'],
  304. "inv_type"=>$this->kingInvoice[$invinfo['inv_type']],
  305. "inv_code"=>$invCode,
  306. "inv_number"=>$invNum,
  307. "check_code"=>$checkCode,
  308. "inv_total"=>$total_fee,
  309. "inv_subtotal"=>$subtotal_fee,
  310. "open_date"=>$open_date,
  311. 'pdfstream'=>'',
  312. "item"=>'',
  313. "status"=>$invinfo['inv_type']==5?1:0,
  314. "addtime"=>date("Y-m-d H:i:s"),
  315. "updatetime"=>date("Y-m-d H:i:s")
  316. ];
  317. if($invinfo['inv_type']==5){
  318. $invpool['seller_id'] = $seller_id??"";
  319. $invpool['seller_title'] = $seller_title??"";
  320. $invpool['seller_addr'] = $seller_addr??"";
  321. $invpool['seller_mobile'] = $seller_mobile??"";
  322. $invpool['seller_bank'] = $seller_bank??"";
  323. $invpool['seller_bankNo'] = $seller_bankNo??"";
  324. $invpool['buyer_id'] = $buyer_id??"";
  325. $invpool['buyer_title'] = $buyer_title??"";
  326. $invpool['buyer_addr'] = $buyer_addr??"";
  327. $invpool['buyer_mobile'] = $buyer_mobile??"";
  328. $invpool['buyer_bank'] = $buyer_bank??"";
  329. $invpool['buyer_bankNo'] = $buyer_bankNo??"";
  330. $invpool['tax_fee'] = $tax_fee??"";
  331. $invpool['pdfstream'] = $invimg ??"";
  332. }
  333. $in = Db::name("invoice_ticket")->insert($invpool);
  334. if($in==false){
  335. Db::rollback();
  336. return error_show(1004,"发票信息添加失败");
  337. }
  338. }
  339. if($status==2 && $open_type==1){
  340. $invpoolinfo =Db::name("invoice_pool_info")->where(["invNo"=>$invNo])->findOrEmpty();
  341. if(empty($invpoolinfo)){
  342. Db::rollback();
  343. return error_show(1004,"开票信息未找到");
  344. }
  345. $goodinfo =Db::name("invoice_good")
  346. ->where(["invNo"=>$invNo,"is_del"=>0])
  347. ->field("0 as FPHXZ,
  348. catNo as SPBM,
  349. id as ZXBM,
  350. is_discount as YHZCBS,
  351. if(inv_tag=0,'',inv_tag) as LSLBS,
  352. addTax as ZZSTSGL,
  353. goodName as XMMC,
  354. specInfo as GGXH,
  355. unitName as DW,
  356. goodNum as SPSL,
  357. goodPrice as DJ,
  358. totalPrice as JE,
  359. 1 HSBZ,
  360. tax as SL,
  361. taxPrice as SE")
  362. ->select()->toJson();
  363. if(empty($goodinfo)){
  364. Db::rollback();
  365. return error_show(1004,"开票商品信息未找到");
  366. }
  367. $data=[
  368. "invNo"=>$invNo,
  369. "KPLX"=>"0",
  370. "XHFSBH"=>$invpoolinfo['seller_code'],
  371. "XHFMC"=>$invpoolinfo['seller_title'],
  372. "XHFDZ"=>$invpoolinfo['seller_addr'],
  373. "XHFDH"=>$invpoolinfo['seller_mobile'],
  374. "XHFYH"=>$invpoolinfo['seller_bank'],
  375. "XHFZH"=>$invpoolinfo['seller_bankNo'],
  376. "GMFBM"=>"",
  377. "GMFSBH"=>$invpoolinfo['buyer_code'],
  378. "GMFMC"=>$invpoolinfo['buyer_title'],
  379. "GMFDZ"=>$invpoolinfo['buyer_addr'],
  380. "GMFDH"=>$invpoolinfo['buyer_mobile'],
  381. "GMFYH"=>$invpoolinfo['buyer_bank'],
  382. "GMFZH"=>$invpoolinfo['buyer_bankNo'],
  383. "GMFLX"=>$invinfo['company_type'],
  384. "GMFSJH"=>"",
  385. "GMFDZYX"=>$invinfo['email'],
  386. "KPR"=>$invpoolinfo['drawer'],
  387. "SKR"=>$invpoolinfo['payee'],
  388. "FHR"=>$invpoolinfo['reviewer'],
  389. "QDBZ"=>"0",
  390. "JSHJ"=>$invinfo['inv_value'],
  391. "HJJE"=>"0",
  392. "HJSE"=>"0",
  393. "BZ"=>$invinfo['remark'],
  394. "DDMXXX"=>$goodinfo,
  395. "FPLXDM"=>$this->kingInvoice[$invinfo['inv_type']],
  396. "DDQQPCH"=>"",
  397. "DDQQLSH"=>randomkeys(26).date("YmdHis"),
  398. "status"=>"0",
  399. "result"=>"",
  400. "addtime"=>date("Y-m-d H:i:s"),
  401. "updatetime"=>date("Y-m-d H:i:s")
  402. ];
  403. $invoice=Db::name("invoice_interface")->insert($data);
  404. if($invoice==false){
  405. Db::rollback();
  406. return error_show(1003,"开票信息生成失败");
  407. }
  408. Cache::push("invoiceList",$data["DDQQLSH"]);
  409. }
  410. if( $update['status']==3){
  411. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0])->column("id,orderCode,cancel_fee");
  412. if(!empty($qrdArr)){
  413. foreach ($qrdArr as $value){
  414. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  415. if($qrdinfo==false){
  416. Db::rollback();
  417. return error_show(1003,"确认单信息未找到");
  418. }
  419. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  420. Db::rollback();
  421. return error_show(1003,"确认单信息开票金额不足");
  422. }
  423. $update =[
  424. "ainv_fee"=>$qrdinfo['ainv_fee']+$value['cancel_fee'],
  425. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  426. "invtime"=>date("Y-m-d H:i:s"),
  427. "inv_status"=>$qrdinfo['winv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 3 : 2,
  428. "updatetime"=>date("Y-m-d H:i:s"),
  429. ];
  430. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  431. if($qrdup==false){
  432. Db::rollback();
  433. return error_show(1003,"确认单信息更新失败");
  434. }
  435. $assoc=["status"=>2,"assoc_time"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  436. $assocup =Db::name("assoc")->where($value)->update($assoc);
  437. if($assocup==false){
  438. Db::rollback();
  439. return error_show(1003,"确认单关联信息更新失败");
  440. }
  441. }
  442. }
  443. }
  444. if($status==7){
  445. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0])->column("id,orderCode,cancel_fee");
  446. if(!empty($qrdArr)){
  447. foreach ($qrdArr as $value){
  448. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  449. if($qrdinfo==false){
  450. Db::rollback();
  451. return error_show(1003,"确认单信息未找到");
  452. }
  453. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  454. Db::rollback();
  455. return error_show(1003,"确认单信息开票金额不足");
  456. }
  457. $update =[
  458. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  459. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  460. "inv_status"=>$qrdinfo['ainv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 1 : 2,
  461. "status"=>$qrdinfo['ainv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ?0 : 1,
  462. "updatetime"=>date("Y-m-d H:i:s"),
  463. ];
  464. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  465. if($qrdup==false){
  466. Db::rollback();
  467. return error_show(1003,"确认单信息更新失败");
  468. }
  469. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  470. $assocup =Db::name("assoc")->where($value)->update($assoc);
  471. if($assocup==false){
  472. Db::rollback();
  473. return error_show(1003,"确认单关联信息更新失败");
  474. }
  475. $report=ReportCode::where(["qrdNo"=>$value['sequenceNo']])->find();
  476. if($report)$report->rmField("invNo",$invinfo['invNo']);
  477. }
  478. }
  479. }
  480. Db::commit();
  481. return app_show(0,"审核成功");
  482. }
  483. Db::rollback();
  484. return error_show(1004,"审核失败");
  485. }catch (\Exception $e){
  486. Db::rollback();
  487. return error_show(1004,$e->getMessage());
  488. }
  489. }
  490. /**
  491. * 发票设置物流
  492. */
  493. public function SetPost(){
  494. // @param invNo postCompany postCode postFee;
  495. $invNo =isset($this->post['invNo'])&&$this->post['invNo']!="" ? trim($this->post['invNo']):"";
  496. if($invNo==''){
  497. return error_show(1004,"参数invNo不能为空");
  498. }
  499. $invinfo =Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  500. if($invinfo==false){
  501. return error_show(1004,"发票申请信息未找到");
  502. }
  503. if($invinfo['status']!=3) return error_show(1004,"发票审核未完成");
  504. $post_company =isset($this->post['post_company'])&&$this->post['post_company']!="" ? trim($this->post['post_company']):"";
  505. if($post_company==''){
  506. return error_show(1004,"参数 post_company 不能为空");
  507. }
  508. $post_code =isset($this->post['post_code'])&&$this->post['post_code']!="" ? trim($this->post['post_code']):"";
  509. if($post_code==''){
  510. return error_show(1004,"参数 post_code 不能为空");
  511. }
  512. $post_fee =isset($this->post['post_fee'])&&$this->post['post_fee']!="" ? floatval($this->post['post_fee']):"0";
  513. Db::startTrans();
  514. try{
  515. $data=[
  516. "post_company"=>$post_company,
  517. "post_code"=>$post_code,
  518. "post_fee"=>$post_fee,
  519. "updatetime"=>date("Y-m-d H:i:s")
  520. ];
  521. $up =Db::name("invoice_pool")->where($invinfo)->update($data);
  522. if($up){
  523. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0])->column("id,orderCode,cancel_fee");
  524. if(!empty($qrdArr)){
  525. foreach ($qrdArr as $value){
  526. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  527. if($qrdinfo==false){
  528. Db::rollback();
  529. return error_show(1003,"确认单信息未找到");
  530. }
  531. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  532. Db::rollback();
  533. return error_show(1003,"确认单信息开票金额不足");
  534. }
  535. $update =[
  536. "ainv_fee"=>$qrdinfo['ainv_fee']+$value['cancel_fee'],
  537. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  538. "invtime"=>date("Y-m-d H:i:s"),
  539. "inv_status"=>$qrdinfo['winv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 3 : 2,
  540. "updatetime"=>date("Y-m-d H:i:s"),
  541. ];
  542. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  543. if($qrdup==false){
  544. Db::rollback();
  545. return error_show(1003,"确认单信息更新失败");
  546. }
  547. $assoc=["status"=>2,"assoc_time"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  548. $assocup =Db::name("assoc")->where($value)->update($assoc);
  549. if($assocup==false){
  550. Db::rollback();
  551. return error_show(1003,"确认单关联信息更新失败");
  552. }
  553. }
  554. }
  555. Db::commit();
  556. return app_show(0,"更新成功");
  557. }
  558. Db::rollback();
  559. return error_show(1003,"发票申请信息更新失败");
  560. }catch (\Exception $e){
  561. Db::rollback();
  562. return error_show(1004,$e->getMessage());
  563. }
  564. }
  565. /**
  566. * 发票废弃使用
  567. */
  568. public function Discard(){
  569. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  570. if($invNo=='') return error_show(1004,"参数invNo不能为空");
  571. $invinfo =Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  572. if($invinfo==false) return error_show(1004,"未找到对应发票申请数据");
  573. if(!in_array($invinfo['status'],[1,2,3,4,5]))return error_show(1004,"发票状态无法废弃");
  574. $company =Db::name("company_info")->where(["companyNo"=>$invinfo['inv_out']])->find();
  575. if($company==false)return error_show(1004,"开票业务公司未找到");
  576. $invoiceinfo =Db::name("invoice_ticket")->where(["invNo"=>$invNo])->find();
  577. if($invoiceinfo==false)return error_show(1004,"发票详细信息未找到");
  578. Db::startTrans();
  579. try{
  580. $invoice=Config::get("invoice");
  581. $tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  582. $result=$tax->DeprecateInvoices($company['company_license'],$this->kingInvoice[$invoiceinfo['inv_type']],$invoiceinfo['inv_code'],$invoiceinfo['inv_number'],1);
  583. if(isset($result['ZTDM']) && $result['ZTDM']=='050000'){
  584. $up =Db::name("invoice_pool")->where($invinfo)->update(["status"=>6,"updatetime"=>date("Y-m-d H:i:s")]);
  585. if($up){
  586. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0,"status"=>[1,2]])->column("id,orderCode,cancel_fee");
  587. if(!empty($qrdArr)){
  588. foreach ($qrdArr as $value){
  589. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  590. if($qrdinfo==false){
  591. Db::rollback();
  592. return error_show(1003,"确认单信息未找到");
  593. }
  594. if ($invinfo['status']==3){
  595. if($qrdinfo['ainv_fee']<$value['cancel_fee']){
  596. Db::rollback();
  597. return error_show(1003,"确认单信息开票金额不足");
  598. }
  599. $update =[
  600. "ainv_fee"=>$qrdinfo['ainv_fee']-$value['cancel_fee'],
  601. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  602. "inv_status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee'] ==0 ? 1: 2,
  603. "updatetime"=>date("Y-m-d H:i:s"),
  604. ];
  605. }else{
  606. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  607. Db::rollback();
  608. return error_show(1003,"确认单信息开票金额不足");
  609. }
  610. $update =[
  611. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  612. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  613. "inv_status"=>$qrdinfo['ainv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee']) ==0 ?1 : 2,
  614. "updatetime"=>date("Y-m-d H:i:s"),
  615. ];
  616. }
  617. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  618. if($qrdup==false){
  619. Db::rollback();
  620. return error_show(1003,"确认单信息更新失败");
  621. }
  622. $assoc=["status"=>3,"assoc_time"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  623. $assocup =Db::name("assoc")->where($value)->update($assoc);
  624. if($assocup==false){
  625. Db::rollback();
  626. return error_show(1003,"确认单关联信息更新失败");
  627. }
  628. }
  629. }
  630. Db::commit();
  631. return app_show(0,"发票废弃成功");
  632. }
  633. }
  634. Db::rollback();
  635. return error_show(1004,"发票废弃失败");
  636. }catch (\Exception $e){
  637. Db::rollback();
  638. return error_show(1004,$e->getMessage());
  639. }
  640. }
  641. /**@param invNo return_reason remark 退票申请
  642. * @return \think\response\Json|void
  643. */
  644. public function ReturnAdd(){
  645. $invNo = isset($this->post['invNo']) && $this->post['invNo']!=''? trim($this->post['invNo']) :'';
  646. if($invNo==""){
  647. return error_show(1004,"参数 invNo 不能为空");
  648. }
  649. $return_reason = isset($this->post['return_reason']) && $this->post['return_reason']!=''? trim($this->post['return_reason']) :'';
  650. $remark = isset($this->post['remark']) && $this->post['remark']!=''? trim($this->post['remark']) :'';
  651. if($return_reason==""){
  652. return error_show(1004,"参数 return_reason 不能为空");
  653. }
  654. if($remark==""){
  655. return error_show(1004,"参数 remark 不能为空");
  656. }
  657. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  658. if($invinfo==false){
  659. return error_show(1004,"发票申请数据未找到");
  660. }
  661. if($invinfo['status']!=3){
  662. return error_show(1004,"发票申请未完成");
  663. }
  664. $qrd =Db::name("assoc")->where(["viceCode"=>$invNo,"status"=>2,"is_del"=>0])->column("orderCode");
  665. if(empty($qrd)){
  666. return error_show(1004,"未找到开票的销售单信息");
  667. }
  668. $returnCode = makeNo("RIN");
  669. $data=[
  670. "returnCode"=>$returnCode,
  671. "invNo"=>$invNo,
  672. "reason"=>$return_reason,
  673. "remark"=>$remark,
  674. "status"=>0,
  675. "apply_id"=>$this->uid,
  676. "apply_name"=>$this->uname,
  677. "addtime"=>date("Y-m-d H:i:s"),
  678. "updatetime"=>date("Y-m-d H:i:s")
  679. ];
  680. Db::startTrans();
  681. try{
  682. $inter = Db::name("invoice_return")->insert($data);
  683. if($inter){
  684. foreach ($qrd as $value){
  685. $report=ReportCode::where(["qrdNo"=>$value])->find();
  686. if($report)$report->setField("returnInv",$returnCode);
  687. }
  688. Db::commit();
  689. return app_show(0,"退票申请新建成功",["returnCode"=>$returnCode]);
  690. }else{
  691. Db::rollback();
  692. return error_show(1005,"退票申请新建失败");
  693. }
  694. }catch (\Exception $e){
  695. Db::rollback();
  696. return error_show(1006,$e->getMessage());
  697. }
  698. }
  699. // 0 待审核 1 待退票 2 退票成功 3 驳回4 退票失败 5 退票进行中
  700. public function ReturnStatus(){
  701. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  702. if($returnCode==""){
  703. return error_show(1005,"参数 returnCode 不能为空");
  704. }
  705. $status = isset($this->post['status'])&&$this->post['status']!="" ? intval($this->post['status']):"";
  706. if ($status==""){
  707. return error_show(1005,"参数 status 不能为空");
  708. }
  709. $remark =isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
  710. if($remark==""){
  711. return error_show(1005,"参数 remark 不能为空");
  712. }
  713. $return_reason =isset($this->post['return_reason'])&&$this->post['return_reason']!="" ? trim($this->post['return_reason']):"";
  714. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  715. if($return==false){
  716. return error_show(1005,"退票申请信息未找到");
  717. }
  718. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  719. if($invinfo==false){
  720. return error_show(1004,"发票申请数据未找到");
  721. }
  722. if($invinfo['status']!=3){
  723. return error_show(1004,"发票申请未完成");
  724. }
  725. $qrd =Db::name("assoc")->where(["viceCode"=>$return['invNo'],"status"=>2,"is_del"=>0])->column("orderCode");
  726. if(empty($qrd)){
  727. return error_show(1004,"未找到开票的销售单信息");
  728. }
  729. $ticket = Db::name("invoice_ticket")->where(["invNo"=>$return['invNo'],"type"=>0,"status"=>1,"is_del"=>0])
  730. ->findOrEmpty();
  731. if(empty($ticket)){
  732. return error_show(1004,"未找到开票的票面信息");
  733. }
  734. $ticket['inv_out'] = $invinfo['inv_out']??"";
  735. $return_type = isset($this->post['return_type'])&&$this->post['return_type']!="" ? intval($this->post['return_type']):0;
  736. if($status==1){
  737. if ($return_type==""){
  738. return error_show(1005,"参数 return_type 不能为空");
  739. }
  740. if($return_type==2){
  741. $invCode =isset($this->post['invCode'])&&$this->post['invCode']!=''?trim($this->post['invCode']):"";
  742. $invNum =isset($this->post['invNum'])&&$this->post['invNum']!=''?trim($this->post['invNum']):"";
  743. $open_date =isset($this->post['open_date'])&&$this->post['open_date']!=''?trim($this->post['open_date']):"";
  744. $checkCode =isset($this->post['checkCode'])&&$this->post['checkCode']!=''?trim($this->post['checkCode']):"";
  745. $subtotal_fee =isset($this->post['subtotal_fee'])&&$this->post['subtotal_fee']!=''?floatval($this->post['subtotal_fee']):"";
  746. if($invCode==='') return error_show(1004,"参数 invCode 不能为空");
  747. if($invNum==='') return error_show(1004,"参数 invNum 不能为空");
  748. if($open_date==='') return error_show(1004,"参数 open_date 不能为空");
  749. if(in_array($invinfo['inv_type'],['normal','roll',"toll",'electronic'])){
  750. if($checkCode==='') return error_show(1004,"参数 checkCode 不能为空");
  751. }
  752. if($subtotal_fee==='') return error_show(1004,"参数 subtotal_fee 不能为空");
  753. }
  754. }
  755. //电子普票 只能红冲 不能作废
  756. //电子专票 只能红冲 不能作废
  757. //纸质专票 当月可以作废 次月红冲
  758. //纸质普票 当月作废 次月红冲
  759. //专票 红冲先做红字申请
  760. Db::startTrans();
  761. try{
  762. $update=["status"=>$status, "return_reason"=>$return_reason,"return_type"=>$return_type,"remark"=>$remark,"updatetime"=>date("Y-m-d H:i:s")];
  763. $up =Db::name("invoice_return")->where($return)->update($update);
  764. if($up){
  765. if($status==1){
  766. if($return_type==1){
  767. if(date("Y-m",strtotime($ticket["open_date"]))==date("Y-m")){
  768. //当月纸质普通发票+纸质专用发票 作废
  769. if(in_array($ticket['inv_type'],['004','007']))$this->addDiscard($ticket,$returnCode);
  770. //当月电子普票红冲
  771. if($ticket['inv_type']=='026') $this->addRedOpen($ticket,$return_reason,$invinfo['company_type'], $returnCode);
  772. //当月电子专票红冲
  773. if($ticket['inv_type']=='028') $this->addApplyRed($ticket,$returnCode);
  774. }else{
  775. //跨月票都走红冲
  776. if(in_array($ticket['inv_type'],['007','026'])) $this->addRedOpen($ticket,$return_reason,$invinfo['company_type'], $returnCode);
  777. if(in_array($ticket['inv_type'],['004','028'])) $this->addApplyRed($ticket,$returnCode);
  778. }
  779. }else{
  780. $invpool=[
  781. "invNo"=>$invinfo['invNo'],
  782. "type"=>1,
  783. "inv_type"=>$ticket['inv_type'],
  784. "inv_code"=>$invCode,
  785. "inv_number"=>$invNum,
  786. "check_code"=>$checkCode,
  787. "inv_total"=>0,
  788. "inv_subtotal"=>$subtotal_fee,
  789. "open_date"=>$open_date,
  790. "item"=>'',
  791. 'pdfstream'=>'',
  792. "status"=>0,
  793. "addtime"=>date("Y-m-d H:i:s"),
  794. "updatetime"=>date("Y-m-d H:i:s")
  795. ];
  796. $in = Db::name("invoice_ticket")->insert($invpool);
  797. if($in==false){
  798. Db::rollback();
  799. return error_show(1004,"发票信息添加失败");
  800. }
  801. }
  802. }
  803. if($status==2){
  804. $qrdArr=Db::name("assoc")->where(["viceCode"=>$return['invNo'],"is_del"=>0,"status"=>[1,2]])->column("id,orderCode,cancel_fee");
  805. if(!empty($qrdArr)){
  806. foreach ($qrdArr as $value){
  807. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  808. if($qrdinfo==false){
  809. Db::rollback();
  810. return error_show(1003,"确认单信息未找到");
  811. }
  812. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  813. Db::rollback();
  814. return error_show(1003,"确认单信息开票金额不足");
  815. }
  816. $update =[
  817. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  818. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  819. "inv_status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']-$value['cancel_fee']==0 ? 1 : 2,
  820. "status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']-$value['cancel_fee']==0 && $qrdinfo['pay_status'] ?0 : 1,
  821. "updatetime"=>date("Y-m-d H:i:s"),
  822. ];
  823. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  824. if($qrdup==false){
  825. Db::rollback();
  826. return error_show(1003,"确认单信息更新失败");
  827. }
  828. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  829. $assocup =Db::name("assoc")->where($value)->update($assoc);
  830. if($assocup==false){
  831. Db::rollback();
  832. return error_show(1003,"确认单关联信息更新失败");
  833. }
  834. }
  835. }
  836. }
  837. if($status==3 || $status==4){
  838. foreach ($qrd as $value){
  839. $report=ReportCode::where(["qrdNo"=>$value])->find();
  840. if($report)$report->rmField("returnInv",$returnCode);
  841. }
  842. }
  843. Db::commit();
  844. return app_show(0,"退票申请信息更新成功");
  845. }
  846. Db::rollback();
  847. return error_show(1005,"退票申请信息更新失败");
  848. }catch (\Exception $e){
  849. Db::rollback();
  850. return error_show(1005,$e->getMessage());
  851. }
  852. }
  853. //红冲票
  854. public function returnRed(){
  855. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  856. if($returnCode==""){
  857. return error_show(1005,"参数 returnCode 不能为空");
  858. }
  859. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  860. if($return==false){
  861. return error_show(1005,"退票申请信息未找到");
  862. }
  863. $redinv =isset($this->post['redinv'])&&$this->post['redinv']!="" ? trim($this->post['redinv']):"";
  864. if($redinv==""){
  865. return error_show(1005,"参数 redinv 不能为空");
  866. }
  867. $red =Db::name("invoice_red")->where(["invRed"=>$redinv,"is_del"=>0])->findOrEmpty();
  868. if(empty($red)) return error_show(1005,"红冲发票信息未找到");
  869. if($red['status']!=4) return error_show(1005,"红冲发票开票未完成");
  870. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  871. if($invinfo==false){
  872. return error_show(1005,"发票申请信息未找到");
  873. }
  874. Db::startTrans();
  875. try{
  876. $update=["status"=>2,"red_inv"=>$redinv,"updatetime"=>date("Y-m-d H:i:s")];
  877. $up =Db::name("invoice_return")->where($return)->update($update);
  878. if($up){
  879. $invup = ["status"=>6,"updatetime"=>date("Y-m-d H:i:s")];
  880. $inv=Db::name("invoice_pool")->where($invinfo)->update($invup);
  881. if($inv){
  882. $qrdArr=Db::name("assoc")->where(["viceCode"=>$return['invNo'],"is_del"=>0])->column("id,orderCode,cancel_fee");
  883. if(!empty($qrdArr)){
  884. foreach ($qrdArr as $value){
  885. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  886. if($qrdinfo==false){
  887. Db::rollback();
  888. return error_show(1003,"确认单信息未找到");
  889. }
  890. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  891. Db::rollback();
  892. return error_show(1003,"确认单信息开票金额不足");
  893. }
  894. $update =[
  895. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  896. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  897. "inv_status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']-$value['cancel_fee']==0 ? 1 : 2,
  898. "status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']-$value['cancel_fee']==0 && $qrdinfo['pay_status'] ?0 : 1,
  899. "updatetime"=>date("Y-m-d H:i:s"),
  900. ];
  901. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  902. if($qrdup==false){
  903. Db::rollback();
  904. return error_show(1003,"确认单信息更新失败");
  905. }
  906. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  907. $assocup =Db::name("assoc")->where($value)->update($assoc);
  908. if($assocup==false){
  909. Db::rollback();
  910. return error_show(1003,"确认单关联信息更新失败");
  911. }
  912. }
  913. }
  914. Db::commit();
  915. return app_show(0,"退票申请信息更新成功");
  916. }
  917. }
  918. Db::rollback();
  919. return error_show(1005,"退票申请信息更新失败");
  920. }catch (\Exception $e){
  921. Db::rollback();
  922. return error_show(1005,$e->getMessage());
  923. }
  924. }
  925. //退票详情
  926. public function returnInfo(){
  927. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  928. if($returnCode==""){
  929. return error_show(1005,"参数 returnCode 不能为空");
  930. }
  931. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  932. if($return==false){
  933. return error_show(1005,"退票申请信息未找到");
  934. }
  935. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  936. $return['inv_out']= $invinfo['inv_out']??"";
  937. $return['inv_value']= $invinfo['inv_value']??"";
  938. $return['inv_apply_id']= $invinfo['apply_id']??"";
  939. $return['inv_apply_name']= $invinfo['apply_name']??"";
  940. $return['inv_type']= $invinfo['inv_type']??"";
  941. $return['open_type']= $invinfo['open_type']??"";
  942. $return['is_ticket']= $invinfo['is_ticket']??"";
  943. $return['email']= $invinfo['email']??"";
  944. $return['post_code']= $invinfo['post_code']??"";
  945. $return['post_company']= $invinfo['post_company']??"";
  946. $poolinfo = Db::name("invoice_pool_info")->where(["invNo"=>$return['invNo']])->find();
  947. $return['buyer_title'] = $poolinfo['buyer_title']??'';
  948. $return['buyer_title'] = $poolinfo['buyer_title']??'';
  949. $return['buyer_code'] = $poolinfo['buyer_code']??'';
  950. $return['buyer_addr'] = $poolinfo['buyer_addr']??'';
  951. $return['buyer_mobile'] = $poolinfo['buyer_mobile']??'';
  952. $return['buyer_bank'] = $poolinfo['buyer_bank']??'';
  953. $return['buyer_bankNo'] = $poolinfo['buyer_bankNo']??'';
  954. $return['seller_title'] = $poolinfo['seller_title']??'';
  955. $return['seller_addr'] = $poolinfo['seller_addr']??'';
  956. $return['seller_code'] = $poolinfo['seller_code']??'';
  957. $return['seller_mobile'] = $poolinfo['seller_mobile']??'';
  958. $return['seller_bank'] = $poolinfo['seller_bank']??'';
  959. $return['seller_bankNo'] = $poolinfo['seller_bankNo']??'';
  960. $return['voider'] = $poolinfo['voider']??'';
  961. $return['payee'] = $poolinfo['payee']??'';
  962. $return['drawer'] = $poolinfo['drawer']??'';
  963. $return['reviewer'] = $poolinfo['reviewer']??'';
  964. $return['ownerPlace'] = $poolinfo['ownerPlace']??'';
  965. $orderList =Db::name("invoice_good")->where(["invNo"=>$return['invNo'],"is_del"=>0])->select()->toArray();
  966. $return['goodList']=$orderList;
  967. $info =Db::name("invoice_ticket")->where(["invNo"=>$return['invNo'],"type"=>0]) ->field("inv_number,inv_code")->find();
  968. $return['inv_number']=$info['inv_number']??"";
  969. $return['inv_code']=$info['inv_code']??"";
  970. return app_show(0,"获取成功",$return);
  971. }
  972. /**
  973. *发票池列表
  974. */
  975. public function list(){
  976. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  977. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  978. $condition=[["a.is_del","=",0]];
  979. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  980. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  981. if($start!=""){
  982. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  983. }
  984. if($end!=""){
  985. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  986. }
  987. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  988. if($status!==""){
  989. $condition[]=["a.status","=",$status];
  990. }
  991. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? trim($this->post['inv_type']):"";
  992. if($inv_type!==""){
  993. $condition[]=["a.inv_type","=",$inv_type];
  994. }
  995. $inv_out= isset($this->post['inv_out'])&&$this->post['inv_out']!=="" ? trim($this->post['inv_out']):"";
  996. if($inv_out!==""){
  997. $condition[]=["a.inv_out","=",$inv_out];
  998. }
  999. // $inv_in = isset($this->post['inv_in'])&&$this->post['inv_in']!=="" ? trim($this->post['inv_in']):"";
  1000. // if($inv_in!==""){
  1001. // $condition[]=["a.inv_in","=",$inv_in];
  1002. // }
  1003. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  1004. if($invNo!==""){
  1005. $condition[]=["a.invNo","like","%$invNo%"];
  1006. }
  1007. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  1008. if($relaComNo!=""){
  1009. $condition[]=["a.inv_out","=",$relaComNo];
  1010. }
  1011. $inv_number = isset($this->post['inv_number'])&&$this->post['inv_number']!=="" ? trim($this->post['inv_number']):"";
  1012. if($inv_number!==""){
  1013. $condition[]=["c.inv_number","like","%$inv_number%"];
  1014. }
  1015. $inv_code = isset($this->post['inv_code'])&&$this->post['inv_code']!=="" ? trim($this->post['inv_code']):"";
  1016. if($inv_code!==""){
  1017. $condition[]=["c.inv_code","like","%$inv_code%"];
  1018. }
  1019. $inv_company = isset($this->post['inv_company'])&&$this->post['inv_company']!=="" ? trim($this->post['inv_company']):"";
  1020. if($inv_company!==""){
  1021. $condition[]=["a.inv_company","like","%$inv_company%"];
  1022. }
  1023. $buyer_name = isset($this->post['buyer_name'])&&$this->post['buyer_name']!=="" ? trim($this->post['buyer_name']):"";
  1024. if($buyer_name!==""){
  1025. $condition[]=["b.buyer_title","like","%$buyer_name%"];
  1026. }
  1027. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  1028. if($apply_id!==""){
  1029. $condition[]=["a.apply_id","=",$apply_id];
  1030. }
  1031. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  1032. if($apply_name!==""){
  1033. $condition[]=["a.apply_name","like","%$apply_name%"];
  1034. }
  1035. $count=Db::name("invoice_pool")->alias("a")
  1036. ->leftJoin("invoice_pool_info b","a.invNo=b.invNo")
  1037. ->leftJoin("invoice_ticket c","a.invNo=c.invNo and c.type=0")
  1038. ->where($condition)->count();
  1039. $total =ceil($count/$size);
  1040. $page= $page>$total ? intval($total):$page;
  1041. $list=Db::name("invoice_pool")->alias("a")
  1042. ->leftJoin("invoice_pool_info b","a.invNo=b.invNo")
  1043. ->leftJoin("invoice_ticket c","a.invNo=c.invNo and c.type=0")
  1044. ->where($condition)->field("b.*,a.*,c.inv_code,c.inv_number,c.open_date,c.type")
  1045. ->page($page,$size)->order("a.addtime desc")->select()->toArray();
  1046. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  1047. }
  1048. //获取发票详情
  1049. public function info(){
  1050. $invNo = isset($this->post['invNo'])&& $this->post['invNo']!='' ? trim($this->post['invNo']):"";
  1051. if($invNo==''){
  1052. return error_show(1004,"参数 invNo 不能为空");
  1053. }
  1054. $poolinfo =Db::name("invoice_pool")->where([["invNo","=",$invNo],["is_del","=",0]])->findOrEmpty();
  1055. if(empty($poolinfo)){
  1056. return error_show(1004,"为找到开票信息");
  1057. }
  1058. $pool = Db::name("invoice_pool_info")->where(["invNo"=>$invNo])->find();
  1059. $poolinfo['buyer_title'] = $pool['buyer_title']??'';
  1060. $poolinfo['buyer_code'] = $pool['buyer_code']??'';
  1061. $poolinfo['buyer_addr'] = $pool['buyer_addr']??'';
  1062. $poolinfo['buyer_mobile'] = $pool['buyer_mobile']??'';
  1063. $poolinfo['buyer_bank'] = $pool['buyer_bank']??'';
  1064. $poolinfo['buyer_bankNo'] = $pool['buyer_bankNo']??'';
  1065. $poolinfo['seller_title'] = $pool['seller_title']??'';
  1066. $poolinfo['seller_addr'] = $pool['seller_addr']??'';
  1067. $poolinfo['seller_code'] = $pool['seller_code']??'';
  1068. $poolinfo['seller_mobile'] = $pool['seller_mobile']??'';
  1069. $poolinfo['seller_bank'] = $pool['seller_bank']??'';
  1070. $poolinfo['seller_bankNo'] = $pool['seller_bankNo']??'';
  1071. $poolinfo['voider'] = $pool['voider']??'';
  1072. $poolinfo['payee'] = $pool['payee']??'';
  1073. $poolinfo['drawer'] = $pool['drawer']??'';
  1074. $poolinfo['reviewer'] = $pool['reviewer']??'';
  1075. $poolinfo['ownerPlace'] = $pool['ownerPlace']??'';
  1076. $orderList =Db::name("invoice_good")->where(["invNo"=>$invNo,"is_del"=>0])->select()->toArray();
  1077. $poolinfo['goodList']=$orderList;
  1078. $poolinfo['invinfo']=Db::name("invoice_ticket")->withoutField("pdfstream")->where(["invNo"=>$invNo,"type"=>0,"is_del"=>0])
  1079. ->findOrEmpty();
  1080. $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
  1081. ->where(["a.viceCode"=>$invNo,"a.is_del"=>0,"a.status"=>[1,2]])
  1082. ->order("a.addtime desc")
  1083. ->field("a.orderCode,c.goodName,c.catInfo,c.goodUnit,c.tax,c.goodNum,c.goodPrice,c.totalPrice,a.cancel_fee")
  1084. ->select()->toArray();
  1085. $poolinfo['orderinfo']=$orderinfo;
  1086. return app_show(0,"获取成功",$poolinfo);
  1087. }
  1088. //退票列表
  1089. public function returnList(){
  1090. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  1091. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  1092. $condition=[["b.is_del","=",0]];
  1093. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  1094. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  1095. $companyNo = isset($this->post['companyNo'])&&$this->post['companyNo']!="" ? trim($this->post['companyNo']) :"";
  1096. if($companyNo!=""){
  1097. $condition[]=["b.inv_out","=",$companyNo];
  1098. }
  1099. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  1100. if($relaComNo!=""){
  1101. $condition[]=["b.inv_out","=",$relaComNo];
  1102. }
  1103. if($start!=""){
  1104. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  1105. }
  1106. if($end!=""){
  1107. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  1108. }
  1109. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? trim($this->post['inv_type']):"";
  1110. if($inv_type!==""){
  1111. $condition[]=["b.inv_type","=",$inv_type];
  1112. }
  1113. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  1114. if($apply_id!==""){
  1115. $condition[]=["a.apply_id","=",$apply_id];
  1116. }
  1117. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  1118. if($apply_name!==""){
  1119. $condition[]=["a.apply_name","like","%$apply_name%"];
  1120. }
  1121. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  1122. if($status!==""){
  1123. $condition[]=["a.status","=",$status];
  1124. }
  1125. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  1126. if($invNo!==""){
  1127. $condition[]=["a.invNo","like","%$invNo%"];
  1128. }
  1129. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!=="" ? trim($this->post['returnCode']):"";
  1130. if($returnCode!==""){
  1131. $condition[]=["a.returnCode","like","%$returnCode%"];
  1132. }
  1133. $count=Db::name("invoice_return")->alias("a")
  1134. ->leftJoin("invoice_pool b","a.invNo=b.invNo")
  1135. ->leftJoin("invoice_pool_info c","a.invNo=c.invNo")
  1136. ->where($condition)->count();
  1137. $total =ceil($count/$size);
  1138. $page= $page>$total ? intval($total):$page;
  1139. $list=Db::name("invoice_return")->alias("a")
  1140. ->leftJoin("invoice_pool b","a.invNo=b.invNo")
  1141. ->leftJoin("invoice_pool_info c","a.invNo=c.invNo")
  1142. ->where($condition)->page($page,$size)->field("a.*,c.buyer_title,c.buyer_code,c.buyer_addr,c.buyer_mobile,c.buyer_bank,
  1143. c.buyer_bankNo,c.seller_title,c.seller_code,c.seller_addr,c.seller_mobile,c.seller_bank,c.seller_bankNo,c.voider,c.payee,
  1144. c.drawer,c.reviewer,c.ownerPlace,b.inv_value,b.inv_out,b.inv_type,b.open_type,is_ticket,b.email,
  1145. b.winv_fee,b.ainv_fee,b.post_fee,b.post_company,b.post_code")->order("id desc")->select()->toArray();
  1146. foreach ($list as &$value){
  1147. $company =Db::name("company_info")->where(["companyNo"=>$value['inv_out']])->find();
  1148. $value['companyName'] = $company['company_name']??"";
  1149. }
  1150. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  1151. }
  1152. //红冲申请
  1153. public function applyRed(){
  1154. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  1155. if($invNo=='')return error_show(1004,"参数 invNo 不能为空");
  1156. $invfo =Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  1157. if($invfo==false) return error_show(1004,"发票数据未找到");
  1158. if(!in_array($invfo['status'],[1,2,3,4,5]))return error_show(1004,"发票数据状态无法申请");
  1159. $ticket =Db::name("invoice_ticket")->where(["invNo"=>$invNo])->findOrEmpty();
  1160. if(empty($ticket)) return error_show(1004,"未找到开票信息");
  1161. $company =Db::name("company_info")->where(["companyNo"=>'GS2207150956046617'])->findOrEmpty();
  1162. if(empty($company)) return error_show(1004,"业务企业信息未找到");
  1163. $invoice=Config::get("invoice");
  1164. $Tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  1165. $redticketList=[];
  1166. $redticketArry=[
  1167. "SQBSCQQPCH"=>randomkeys(26).date("YmdHis"),
  1168. "NSRSBH"=>$company['company_license'],
  1169. "FPLXDM"=>$this->kingInvoice[$invfo['inv_type']],
  1170. ];
  1171. $goodinfo =Db::name("invoice_good")
  1172. ->where(["invNo"=>$invNo,"is_del"=>0])
  1173. ->field("0 as FPHXZ,
  1174. catNo as SPBM,
  1175. id as ZXBM,
  1176. is_discount as YHZCBS,
  1177. if(inv_tag=0,'',inv_tag) as LSLBS,
  1178. addTax as ZZSTSGL,
  1179. goodName as XMMC,
  1180. '' as DW,
  1181. '' as SPSL,
  1182. '' as DJ,
  1183. (-totalPrice) as JE,
  1184. 0 HSBZ,
  1185. tax as SL,
  1186. -taxPrice as SE")->select()->toArray();
  1187. if(empty($goodinfo)){
  1188. Db::rollback();
  1189. return error_show(1004,"开票商品信息未找到");
  1190. }
  1191. if(count($goodinfo)>8){
  1192. $len =array_unique(array_column($goodinfo,'SL'));
  1193. $qdmx=["FPHXZ"=>6,
  1194. "SPBM"=>'',
  1195. "XMMC"=>"详见对应正数发票及清单",
  1196. "YHZCBS"=>0,
  1197. "LSLBS"=>'',
  1198. "ZZSTSGL"=>'',
  1199. "DW"=>'',
  1200. "SPSL"=>'',
  1201. "DJ"=>'',
  1202. "JE"=>sprintf("%01.2f", (array_sum(array_column($goodinfo,'JE')))),
  1203. "HSBZ"=>'0',
  1204. "SL"=>count($len)>1?'':$len[0],
  1205. "SE"=>sprintf("%01.2f",array_sum(array_column($goodinfo,'SE'))),
  1206. ];
  1207. }else
  1208. {
  1209. $qdmx=$goodinfo;
  1210. }
  1211. $info=[
  1212. "SQBSCQQLSH"=>randomkeys(26).date("YmdHis"),
  1213. "YYSBZ"=> "0000000000", //营业税标志
  1214. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1215. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1216. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1217. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1218. "TKSJ"=>date("YmdHis"), //填开时间
  1219. "XHFSBH"=>$ticket['seller_id'],
  1220. "XHFMC"=>$ticket['seller_title'],
  1221. "GMFSBH"=>$ticket['buyer_id'],
  1222. "GMFMC"=> $ticket['buyer_title'],
  1223. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1224. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1225. "SQSM"=>"1100000000", //申请说明
  1226. "XXBTSBS"=>"0",//0: 正常;1:成品油发票,涉及销售数量 变更(销货退回、开票有误等原因);2: 成品油发票,仅涉及销售金额变更(销 售折让);3:机动车发票,仅涉及销售数
  1227. //量变更(销货退回、开票有误等原因);4: 机动车发票,仅涉及销售金额变更(销 售折让)
  1228. ];
  1229. $invRed=makeNo("IRE");
  1230. $data=[
  1231. 'invNo'=>$invNo,
  1232. "invRed"=>$invRed,
  1233. "inv_out"=>$invfo['inv_out']??"",
  1234. "apply_id"=>$this->uid,
  1235. "apply_name"=>$this->uname,
  1236. "status"=>0,
  1237. "remark"=>'',
  1238. "addtime"=>date("Y-m-d H:i:s"),
  1239. "updatetime"=>date("Y-m-d H:i:s"),
  1240. "SQBSCQQLSH"=>$info['SQBSCQQLSH'],
  1241. "YYSBZ"=> "0000000000", //营业税标志
  1242. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1243. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1244. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1245. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1246. "TKSJ"=>date("YmdHis"), //填开时间
  1247. "XHFSBH"=>$ticket['seller_id'],
  1248. "XHFMC"=>$ticket['seller_title'],
  1249. "GMFSBH"=>$ticket['buyer_id'],
  1250. "GMFMC"=> $ticket['buyer_title'],
  1251. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1252. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1253. "SQSM"=>"1100000000", //申请说明
  1254. "XXBTSBS"=>"0",
  1255. "DDMXXX"=>json_encode($qdmx,JSON_UNESCAPED_UNICODE),
  1256. "SQBSCQQPCH"=>$redticketArry['SQBSCQQPCH'],
  1257. "FPLXDM"=>$this->kingInvoice[$invfo['inv_type']],
  1258. ];
  1259. $redticketList[]=["HZSQDTXX"=>$info,"DDMXXX"=>$qdmx];
  1260. $result=$Tax->AllocateRedInvoiceApplication($redticketArry,$redticketList);
  1261. $data['status']=5;
  1262. $data['remark']='红冲申请提交失败';
  1263. if(isset($result['ZTDM']) && $result['ZTDM']=='060000'){
  1264. $resultChild=$result['HZSQDSCJG'][0];
  1265. if($resultChild['ZTDM']=='060000'){
  1266. $data['status']=2;
  1267. $data['XXBBH']=$resultChild['XXBBH'];
  1268. $data['remark']='红冲申请审核通过';
  1269. }
  1270. if($resultChild['ZTDM']=='060001'){
  1271. $data['status']=1;
  1272. $data['XXBBH']=$resultChild['XXBBH'];
  1273. $data['remark']='红冲申请审核中';
  1274. }
  1275. }
  1276. if($data['status']==5){
  1277. return error_show(1004,"红冲发票申请新建失败");
  1278. }
  1279. $up =Db::name("invoice_red")->insert($data);
  1280. return $up ? app_show(0,"红冲发票申请新建成功",["invRed"=>$invRed]):error_show(1004,"红冲发票申请新建失败");
  1281. }
  1282. //批量申请红冲发票
  1283. public function batchApplyRed(){
  1284. $invArr = isset($this->post['invArr'])&&!empty($this->post['invArr'])?$this->post['invArr']:[];
  1285. if(empty($invArr))return error_show(1004,"参数 invArr 不能为空");
  1286. $invfo =Db::name("invoice_pool")->where(["invNo"=>$invArr,"is_del"=>0])->select()->toArray();
  1287. if(empty($invfo)) return error_show(1004,"发票数据未找到");
  1288. $insertAll=[];
  1289. $companyCode = array_unique(array_column($invfo,"inv_out"));
  1290. $inv_type = array_unique(array_column($invfo,"inv_type"));
  1291. if(count($companyCode)>1){
  1292. return error_show(1004,"业务企业公司不统一");
  1293. }
  1294. if(count($inv_type)>1){
  1295. return error_show(1004,"发票类型不统一");
  1296. }
  1297. $company =Db::name("company_info")->where(["companyNo"=>$companyCode])->findOrEmpty();
  1298. if(empty($company)) {
  1299. return error_show(1004,"业务企业信息未找到");
  1300. }
  1301. $ticketArr=Db::name("invoice_ticket")->where(["invNo"=>$invArr,"is_del"=>0])->column("*","invNo");
  1302. if(empty($ticketArr)){
  1303. return error_show(1004,"未找到开票信息");
  1304. }
  1305. $invoice=Config::get("invoice");
  1306. $Tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  1307. Db::startTrans();
  1308. try{
  1309. $redticketArry=[
  1310. "SQBSCQQPCH"=>randomkeys(26).date("YmdHis"),
  1311. "NSRSBH"=>$company['company_license'],
  1312. "FPLXDM"=>$this->kingInvoice[$inv_type[0]],
  1313. ];
  1314. $redticketList=[];
  1315. foreach ($invfo as $value){
  1316. if(!in_array($value['status'],[1,2,3,4,5])){
  1317. Db::rollback();
  1318. return error_show(1004,"发票数据状态无法申请");
  1319. }
  1320. $ticket =$ticketArr[$value['invNo']] ??[];
  1321. if(empty($ticket)){
  1322. Db::rollback();
  1323. return error_show(1004,"未找到开票信息");
  1324. }
  1325. $goodinfo =Db::name("invoice_good")
  1326. ->where(["invNo"=>$value['invNo'],"is_del"=>0])
  1327. ->field(" 6 as FPHXZ,
  1328. catNo as SPBM,
  1329. id as ZXBM,
  1330. is_discount as YHZCBS,
  1331. if(inv_tag=0,'',inv_tag) as LSLBS,
  1332. addTax as ZZSTSGL,
  1333. goodName as XMMC,
  1334. '' as DW,
  1335. '' as SPSL,
  1336. '' as DJ,
  1337. (-totalPrice) as JE,
  1338. 0 HSBZ,
  1339. tax as SL,
  1340. -taxPrice as SE")
  1341. ->select()->toArray();
  1342. if(empty($goodinfo)){
  1343. Db::rollback();
  1344. return error_show(1004,"开票商品信息未找到");
  1345. }
  1346. $len =array_unique(array_column($goodinfo,'SL'));
  1347. $qdmx=["FPHXZ"=>6,
  1348. "SPBM"=>'',
  1349. "XMMC"=>"详见对应正数发票及清单",
  1350. "YHZCBS"=>0,
  1351. "LSLBS"=>'',
  1352. "ZZSTSGL"=>'',
  1353. "DW"=>'',
  1354. "SPSL"=>'',
  1355. "DJ"=>'',
  1356. "JE"=>sprintf("%01.2f", (array_sum(array_column($goodinfo,'JE')))),
  1357. "HSBZ"=>'0',
  1358. "SL"=>count($len)>1?'':$len[0],
  1359. "SE"=>sprintf("%01.2f",array_sum(array_column($goodinfo,'SE'))),
  1360. ];
  1361. $info=[
  1362. "SQBSCQQLSH"=>randomkeys(26).date("YmdHis"),
  1363. "YYSBZ"=> "0000000000", //营业税标志
  1364. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1365. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1366. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1367. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1368. "TKSJ"=>date("YmdHis"), //填开时间
  1369. "XHFSBH"=>$ticket['seller_id'],
  1370. "XHFMC"=>$ticket['seller_title'],
  1371. "GMFSBH"=>$ticket['buyer_id'],
  1372. "GMFMC"=> $ticket['buyer_title'],
  1373. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1374. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1375. "SQSM"=>"1100000000", //申请说明
  1376. "XXBTSBS"=>"0",//0: 正常;1:成品油发票,涉及销售数量 变更(销货退回、开票有误等原因);2: 成品油发票,仅涉及销售金额变更(销 售折让);3:机动车发票,仅涉及销售数
  1377. //量变更(销货退回、开票有误等原因);4: 机动车发票,仅涉及销售金额变更(销 售折让)
  1378. ];
  1379. $redticketList[]=["HZSQDTXX"=>$info,"DDMXXX"=>$qdmx];
  1380. $data=[
  1381. 'invNo'=>$value['invNo'],
  1382. "invRed"=>makeNo("IRE"),
  1383. "inv_out"=>$value['inv_out']??"",
  1384. "apply_id"=>$this->uid,
  1385. "apply_name"=>$this->uname,
  1386. "status"=>5,
  1387. "remark"=>'红冲申请提交失败',
  1388. "addtime"=>date("Y-m-d H:i:s"),
  1389. "updatetime"=>date("Y-m-d H:i:s"),
  1390. "SQBSCQQLSH"=>$info['SQBSCQQLSH'],
  1391. "YYSBZ"=> "0000000000", //营业税标志
  1392. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1393. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1394. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1395. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1396. "TKSJ"=>date("YmdHis"), //填开时间
  1397. "XHFSBH"=>$ticket['seller_id'],
  1398. "XHFMC"=>$ticket['seller_title'],
  1399. "GMFSBH"=>$ticket['buyer_id'],
  1400. "GMFMC"=> $ticket['buyer_title'],
  1401. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1402. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1403. "SQSM"=>"1100000000", //申请说明
  1404. "XXBTSBS"=>"0",
  1405. "DDMXXX"=>json_encode($qdmx,JSON_UNESCAPED_UNICODE),
  1406. "SQBSCQQPCH"=>$redticketArry['SQBSCQQPCH'],
  1407. "FPLXDM"=>$this->kingInvoice[$value['inv_type']],
  1408. ];
  1409. $insertAll[$info['SQBSCQQLSH']]=$data;
  1410. }
  1411. $result=$Tax->AllocateRedInvoiceApplication($redticketArry,$redticketList);
  1412. if(isset($result['ZTDM']) && $result['ZTDM']=='060000'){
  1413. if(isset($result['HZSQDSCJG']) && count($result['HZSQDSCJG'])>0){
  1414. foreach ($result['HZSQDSCJG'] as $value){
  1415. if($value['ZTDM']=='060000'){
  1416. $insertAll[$value['SQBSCQQLSH']]['status']=2;
  1417. $insertAll[$value['SQBSCQQLSH']]['remark']="红冲申请审核通过";
  1418. $insertAll[$value['SQBSCQQLSH']]['XXBBH']=$value['XXBBH'];
  1419. continue;
  1420. // $data['status']=2;
  1421. // $data['remark']='红冲申请审核通过';
  1422. }
  1423. if($value['ZTDM']=='060001'){
  1424. $insertAll[$value['SQBSCQQLSH']]['status']=1;
  1425. $insertAll[$value['SQBSCQQLSH']]['remark']="红冲申请审核中";
  1426. $insertAll[$value['SQBSCQQLSH']]['XXBBH']=$value['XXBBH'];
  1427. continue;
  1428. }
  1429. unset($insertAll[$value['SQBSCQQLSH']]);
  1430. }
  1431. }
  1432. }else{
  1433. Db::rollback();
  1434. return error_show(1004,"红冲发票申请失败");
  1435. }
  1436. $uns= Db::name("invoice_red")->insertAll($insertAll);
  1437. if($uns){
  1438. Db::commit();
  1439. return app_show(0,"红冲发票申请新建成功,成功数量{$uns}个");
  1440. }
  1441. Db::rollback();
  1442. return error_show(1004,"红冲发票申请新建失败");
  1443. }catch (\Exception $e){
  1444. Db::rollback();
  1445. return error_show(1004,$e->getMessage());
  1446. }
  1447. }
  1448. //红票申请开票
  1449. public function redOpen(){
  1450. $invRed= isset($this->post['invRed'])&&$this->post['invRed']!=''? $this->post['invRed']:"";
  1451. if($invRed==''){
  1452. return error_show(1004,"参数 invRed 不能为空");
  1453. }
  1454. $redinfo =Db::name("invoice_red")->where(["invRed"=>$invRed,"is_del"=>0])->findOrEmpty();
  1455. if(empty($redinfo)){
  1456. return error_show(1004,"红冲发票申请不存在");
  1457. }
  1458. $redreason = isset($this->post['redreason'])&&$this->post['redreason']!=''? $this->post['redreason']:"";
  1459. if($redreason==''){
  1460. return error_show(1004,"参数 redreason 不能为空");
  1461. }
  1462. if($redinfo['status']!=2){
  1463. return error_show(1004,"数据状态不可提交申请");
  1464. }
  1465. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$redinfo['invNo'],"is_del"=>0])->findOrEmpty();
  1466. if(empty($invinfo)) return error_show(1004,"红冲发票原票信息未找到");
  1467. $invpoolinfo = Db::name("invoice_ticket")->where(["invNo"=>$redinfo['invNo'],"status"=>1,"is_del"=>0])->findOrEmpty();
  1468. if(empty($invpoolinfo)) return error_show(1004,"红冲发票开票信息未找到");
  1469. Db::startTrans();
  1470. try{
  1471. $data=[
  1472. "invNo"=>$invRed,
  1473. "KPLX"=>"1",
  1474. "XHFSBH"=>$invpoolinfo['seller_id'],
  1475. "XHFMC"=>$invpoolinfo['seller_title'],
  1476. "XHFDZ"=>$invpoolinfo['seller_addr'],
  1477. "XHFDH"=>$invpoolinfo['seller_mobile'],
  1478. "XHFYH"=>$invpoolinfo['seller_bank'],
  1479. "XHFZH"=>$invpoolinfo['seller_bankNo'],
  1480. "GMFBM"=>"",
  1481. "GMFSBH"=>$invpoolinfo['buyer_id'],
  1482. "GMFMC"=>$invpoolinfo['buyer_title'],
  1483. "GMFDZ"=>$invpoolinfo['buyer_addr'],
  1484. "GMFDH"=>$invpoolinfo['buyer_mobile'],
  1485. "GMFYH"=>$invpoolinfo['buyer_bank'],
  1486. "GMFZH"=>$invpoolinfo['buyer_bankNo'],
  1487. "GMFLX"=>$invinfo['company_type'],
  1488. "GMFSJH"=>"",
  1489. "GMFDZYX"=>$invinfo['email'],
  1490. "KPR"=>$invpoolinfo['issuer'],
  1491. "SKR"=>$invpoolinfo['reciver'],
  1492. "FHR"=>$invpoolinfo['reviewer'],
  1493. "QDBZ"=>"0",
  1494. "YFPDM"=>$redinfo['YFPDM'],
  1495. "YFPHM"=>$redinfo['YFPHM'],
  1496. "YFPLX"=>$invpoolinfo['inv_type'] == '004' || $invpoolinfo['inv_type'] =='028'||$redreason=='' ?'':$invpoolinfo['inv_type'],
  1497. "YFPKPRQ"=>$invpoolinfo['inv_type'] == '004' || $invpoolinfo['inv_type'] == '028'||$redreason=='' ? '':date("Y-m-d",strtotime($redinfo['YFPKPRQ'])),
  1498. "CHYY"=>$invpoolinfo['inv_type'] == '004' || $invpoolinfo['inv_type'] == '028' ? '':$redreason,
  1499. "TSCHBZ"=>"0",
  1500. "JSHJ"=>"-".$invpoolinfo['inv_total'],
  1501. "HJJE"=>"-".$invpoolinfo['inv_subtotal'],
  1502. "HJSE"=>"-".$invpoolinfo['tax_fee'],
  1503. "BZ"=> $invpoolinfo['inv_type'] == '004' || $invpoolinfo['inv_type'] =='028'?"开具红字增值税专用发票信息表编号{$redinfo['XXBBH']}":"对应正数发票代码:{$redinfo['YFPDM']} 号码:{$redinfo['YFPHM']}",
  1504. "DDMXXX"=>$redinfo['DDMXXX'],
  1505. "FPLXDM"=>$redinfo['FPLXDM'],
  1506. "DDQQPCH"=>"",
  1507. "DDQQLSH"=>randomkeys(26).date("YmdHis"),
  1508. "status"=>"0",
  1509. "result"=>"",
  1510. "addtime"=>date("Y-m-d H:i:s"),
  1511. "updatetime"=>date("Y-m-d H:i:s")
  1512. ];
  1513. $invoice=Db::name("invoice_interface")->insert($data);
  1514. if($invoice==false){
  1515. Db::rollback();
  1516. return error_show(1003,"红冲开票信息生成失败");
  1517. }
  1518. $upred=Db::name("invoice_red")->where(["invRed"=>$invRed])->update(["status"=>3,"red_reason"=>$redreason,"updatetime"=>date("Y-m-d H:i:s")]);
  1519. if($upred==false){
  1520. Db::rollback();
  1521. return error_show(1003,"红冲发票信息更新失败");
  1522. }
  1523. Db::commit();
  1524. return app_show(0,"红冲发票信息开票提交成功");
  1525. }catch (\Exception $e){
  1526. Db::rollback();
  1527. return error_show(1003,$e->getMessage());
  1528. }
  1529. }
  1530. //红冲票申请列表
  1531. public function redList(){
  1532. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  1533. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  1534. $condition=[["is_del","=",0],["FPLXDM","in",["004",'028']]];
  1535. $invRed=isset($this->post["invRed"])&&$this->post["invRed"]!=''?trim($this->post["invRed"]):"";
  1536. if($invRed!=''){
  1537. $condition[]=["invRed","like","%$invRed%"];
  1538. }
  1539. $apply_id=isset($this->post["apply_id"])&&$this->post["apply_id"]!=''?intval($this->post["apply_id"]):"";
  1540. if($apply_id!=''){
  1541. $condition[]=["apply_id","=",$apply_id];
  1542. }
  1543. $invNo=isset($this->post["invNo"])&&$this->post["invNo"]!=''?trim($this->post["invNo"]):"";
  1544. if($invNo!=''){
  1545. $condition[]=["invNo","like","%$invNo%"];
  1546. }
  1547. $companyNo=isset($this->post["companyNo"])&&$this->post["companyNo"]!=''?trim($this->post["companyNo"]):"";
  1548. if($companyNo!=''){
  1549. $condition[]=["inv_out","=",$companyNo];
  1550. }
  1551. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  1552. if($relaComNo!=""){
  1553. $condition[]=["inv_out","=",$relaComNo];
  1554. }
  1555. $apply_name=isset($this->post["apply_name"])&&$this->post["apply_name"]!=''?trim($this->post["apply_name"]):"";
  1556. if($apply_name!=''){
  1557. $condition[]=["apply_name","like","%$apply_name%"];
  1558. }
  1559. $start = isset($this->post["start"])&&$this->post["start"]!=''?trim($this->post["start"]):"";
  1560. $end = isset($this->post["end"])&&$this->post["end"]!=''?trim($this->post["end"]):"";
  1561. if($start!=''){
  1562. $condition[]=["addtime",">=",$start];
  1563. }
  1564. if($end!=''){
  1565. $condition[]=["addtime","<=",$end];
  1566. }
  1567. $total_low=isset($this->post["total_low"])&&$this->post["total_low"]!=''?floatval($this->post["total_low"]):"";
  1568. $total_up=isset($this->post["total_up"])&&$this->post["total_up"]!=''?floatval($this->post["total_up"]):"";
  1569. if($total_low!=''){
  1570. $condition[]=["ABS(HJJE)",">=",$total_low];
  1571. }
  1572. if($total_up!=''){
  1573. $condition[]=["ABS(HJJE)","<=",$total_up];
  1574. }
  1575. $count= Db::name("invoice_red")->where($condition)->count();
  1576. $total =ceil($count/$size);
  1577. $page=$total>=$page?$page :intval($total);
  1578. $list =Db::name("invoice_red")->where($condition)->json(["DDMXXX"])->page($page,$size)->order("addtime desc")->select()
  1579. ->toArray();
  1580. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  1581. }
  1582. /**
  1583. * @return \think\response\Json|void
  1584. */
  1585. public function ticketInfo(){
  1586. $num =isset($this->post['number'])&&$this->post['number']!=''?trim($this->post['number']):"";
  1587. if($num==''){
  1588. return error_show(1004,'参数 number 不能为空');
  1589. }
  1590. $condition=[];
  1591. $condition[]=["inv_number","=",$num];
  1592. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  1593. if($invNo!=='')$condition[]=["invNo","=",$invNo];
  1594. $info =Db::name("invoice_ticket")->json(['item'])->withoutField("pdfstream")->where($condition)->findOrEmpty();
  1595. if(!empty($info)&& isset($info['item'])){
  1596. array_walk($info['item'],function (&$value){
  1597. $value['SL'] = ($value['SL']*100)."%";
  1598. });
  1599. }
  1600. $info['invtype_name'] = $this->typename[$info['inv_type']]??"";
  1601. return empty($info)?error_show(1005,"未找到数据"):app_show(0,"获取成功",$info);
  1602. }
  1603. /**
  1604. * @param array $ticket
  1605. * @return \think\response\Json|void
  1606. * @throws \think\db\exception\DataNotFoundException
  1607. * @throws \think\db\exception\DbException
  1608. * @throws \think\db\exception\ModelNotFoundException
  1609. */
  1610. private function addApplyRed($ticket=[],$returnCode){
  1611. $invoice=Config::get("invoice");
  1612. $Tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  1613. $redticketList=[];
  1614. $redticketArry=[
  1615. "SQBSCQQPCH"=>randomkeys(26).date("YmdHis"),
  1616. "NSRSBH"=>$ticket['seller_id'],
  1617. "FPLXDM"=>$ticket['inv_type'],
  1618. ];
  1619. $goodinfo =Db::name("invoice_good")
  1620. ->where(["invNo"=>$ticket['invNo'],"is_del"=>0])
  1621. ->field("0 as FPHXZ,
  1622. catNo as SPBM,
  1623. id as ZXBM,
  1624. is_discount as YHZCBS,
  1625. if(inv_tag=0,'',inv_tag) as LSLBS,
  1626. addTax as ZZSTSGL,
  1627. goodName as XMMC,
  1628. '' as DW,
  1629. '' as SPSL,
  1630. '' as DJ,
  1631. (-totalPrice) as JE,
  1632. 0 HSBZ,
  1633. tax as SL,
  1634. -taxPrice as SE")->select()->toArray();
  1635. if(empty($goodinfo)){
  1636. throw new \Exception("开票商品信息未找到", 10006);
  1637. }
  1638. if(count($goodinfo)>8){
  1639. $len =array_unique(array_column($goodinfo,'SL'));
  1640. $qdmx=["FPHXZ"=>6,
  1641. "SPBM"=>'',
  1642. "XMMC"=>"详见对应正数发票及清单",
  1643. "YHZCBS"=>0,
  1644. "LSLBS"=>'',
  1645. "ZZSTSGL"=>'',
  1646. "DW"=>'',
  1647. "SPSL"=>'',
  1648. "DJ"=>'',
  1649. "JE"=>sprintf("%01.2f", (array_sum(array_column($goodinfo,'JE')))),
  1650. "HSBZ"=>'0',
  1651. "SL"=>count($len)>1?'':$len[0],
  1652. "SE"=>sprintf("%01.2f",array_sum(array_column($goodinfo,'SE'))),
  1653. ];
  1654. }else
  1655. {
  1656. $qdmx=$goodinfo;
  1657. }
  1658. $info=[
  1659. "SQBSCQQLSH"=>randomkeys(26).date("YmdHis"),
  1660. "YYSBZ"=> "0000000000", //营业税标志
  1661. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1662. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1663. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1664. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1665. "TKSJ"=>date("YmdHis"), //填开时间
  1666. "XHFSBH"=>$ticket['seller_id'],
  1667. "XHFMC"=>$ticket['seller_title'],
  1668. "GMFSBH"=>$ticket['buyer_id'],
  1669. "GMFMC"=> $ticket['buyer_title'],
  1670. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1671. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1672. "SQSM"=>"1100000000", //申请说明
  1673. "XXBTSBS"=>"0",//0: 正常;1:成品油发票,涉及销售数量 变更(销货退回、开票有误等原因);2: 成品油发票,仅涉及销售金额变更(销 售折让);3:机动车发票,仅涉及销售数
  1674. //量变更(销货退回、开票有误等原因);4: 机动车发票,仅涉及销售金额变更(销 售折让)
  1675. ];
  1676. $invRed=makeNo("IRE");
  1677. $data=[
  1678. 'invNo'=>$ticket['invNo'],
  1679. "invRed"=>$invRed,
  1680. "inv_out"=>$ticket['inv_out'],
  1681. "returnCode"=>$returnCode,
  1682. "apply_id"=>$this->uid,
  1683. "apply_name"=>$this->uname,
  1684. "status"=>0,
  1685. "remark"=>'',
  1686. "addtime"=>date("Y-m-d H:i:s"),
  1687. "updatetime"=>date("Y-m-d H:i:s"),
  1688. "SQBSCQQLSH"=>$info['SQBSCQQLSH'],
  1689. "YYSBZ"=> "0000000000", //营业税标志
  1690. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1691. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1692. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1693. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1694. "TKSJ"=>date("YmdHis"), //填开时间
  1695. "XHFSBH"=>$ticket['seller_id'],
  1696. "XHFMC"=>$ticket['seller_title'],
  1697. "GMFSBH"=>$ticket['buyer_id'],
  1698. "GMFMC"=> $ticket['buyer_title'],
  1699. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1700. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1701. "SQSM"=>"1100000000", //申请说明
  1702. "XXBTSBS"=>"0",
  1703. "DDMXXX"=>json_encode($qdmx,JSON_UNESCAPED_UNICODE),
  1704. "SQBSCQQPCH"=>$redticketArry['SQBSCQQPCH'],
  1705. "FPLXDM"=>$ticket['inv_type'],
  1706. ];
  1707. $redticketList[]=["HZSQDTXX"=>$info,"DDMXXX"=>$qdmx];
  1708. $result=$Tax->AllocateRedInvoiceApplication($redticketArry,$redticketList);
  1709. $data['status']=5;
  1710. $data['remark']='红冲申请提交失败';
  1711. if(isset($result['ZTDM']) && $result['ZTDM']=='060000'){
  1712. $resultChild=$result['HZSQDSCJG'][0];
  1713. if($resultChild['ZTDM']=='060000'){
  1714. $data['status']=2;
  1715. $data['XXBBH']=$resultChild['XXBBH'];
  1716. $data['remark']='红冲申请审核通过';
  1717. }
  1718. if($resultChild['ZTDM']=='060001'){
  1719. $data['status']=1;
  1720. $data['XXBBH']=$resultChild['XXBBH'];
  1721. $data['remark']='红冲申请审核中';
  1722. }
  1723. }
  1724. if($data['status']==5){
  1725. throw new \Exception($result['ZTXX'], 10006);
  1726. }
  1727. $up =Db::name("invoice_red")->insert($data);
  1728. if($up) return app_show(0,"红冲发票申请新建成功",["invRed"=>$invRed]);
  1729. else throw new \Exception($result['ZTXX'], 1006);
  1730. }
  1731. /**
  1732. * @param array $ticket
  1733. * @return \think\response\Json|void
  1734. * @throws \think\Exception
  1735. */
  1736. private function addRedOpen($ticket=[],$return_reason,$company_type,$returnCode){
  1737. $goodinfo =Db::name("invoice_good")
  1738. ->where(["invNo"=>$ticket['invNo'],"is_del"=>0])
  1739. ->field("0 as FPHXZ,
  1740. catNo as SPBM,
  1741. id as ZXBM,
  1742. is_discount as YHZCBS,
  1743. if(inv_tag=0,'',inv_tag) as LSLBS,
  1744. addTax as ZZSTSGL,
  1745. goodName as XMMC,
  1746. '' as DW,
  1747. '' as SPSL,
  1748. '' as DJ,
  1749. (-totalPrice) as JE,
  1750. 0 HSBZ,
  1751. tax as SL,
  1752. -taxPrice as SE")->select()->toArray();
  1753. if(empty($goodinfo)){
  1754. throw new \Exception("开票商品信息未找到", 10006);
  1755. }
  1756. if(count($goodinfo)>8){
  1757. $len =array_unique(array_column($goodinfo,'SL'));
  1758. $qdmx=["FPHXZ"=>6,
  1759. "SPBM"=>'',
  1760. "XMMC"=>"详见对应正数发票及清单",
  1761. "YHZCBS"=>0,
  1762. "LSLBS"=>'',
  1763. "ZZSTSGL"=>'',
  1764. "DW"=>'',
  1765. "SPSL"=>'',
  1766. "DJ"=>'',
  1767. "JE"=>sprintf("%01.2f", (array_sum(array_column($goodinfo,'JE')))),
  1768. "HSBZ"=>'0',
  1769. "SL"=>count($len)>1?'':$len[0],
  1770. "SE"=>sprintf("%01.2f",array_sum(array_column($goodinfo,'SE'))),
  1771. ];
  1772. }else
  1773. {$qdmx=$goodinfo;}
  1774. $invRed=makeNo("IRE");
  1775. $reddata=[
  1776. 'invNo'=>$ticket['invNo'],
  1777. "invRed"=>$invRed,
  1778. "inv_out"=>$ticket['inv_out'],
  1779. "returnCode"=>$returnCode,
  1780. "apply_id"=>$this->uid,
  1781. "apply_name"=>$this->uname,
  1782. "status"=>3,
  1783. "remark"=>'',
  1784. "addtime"=>date("Y-m-d H:i:s"),
  1785. "updatetime"=>date("Y-m-d H:i:s"),
  1786. "SQBSCQQLSH"=>'',
  1787. "YYSBZ"=> "0000000000", //营业税标志
  1788. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1789. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1790. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1791. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1792. "TKSJ"=>date("YmdHis"), //填开时间
  1793. "XHFSBH"=>$ticket['seller_id'],
  1794. "XHFMC"=>$ticket['seller_title'],
  1795. "GMFSBH"=>$ticket['buyer_id'],
  1796. "GMFMC"=> $ticket['buyer_title'],
  1797. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1798. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1799. "SQSM"=>"1100000000", //申请说明
  1800. "XXBTSBS"=>"0",
  1801. "DDMXXX"=>json_encode($goodinfo,JSON_UNESCAPED_UNICODE),
  1802. "SQBSCQQPCH"=>'',
  1803. "FPLXDM"=>$ticket['inv_type'],
  1804. ];
  1805. $data=[ "invNo"=>$invRed,
  1806. "KPLX"=>"1",
  1807. "XHFSBH"=>$ticket['seller_id'],
  1808. "XHFMC"=>$ticket['seller_title'],
  1809. "XHFDZ"=>$ticket['seller_addr'],
  1810. "XHFDH"=>$ticket['seller_mobile'],
  1811. "XHFYH"=>$ticket['seller_bank'],
  1812. "XHFZH"=>$ticket['seller_bankNo'],
  1813. "GMFBM"=>"",
  1814. "GMFSBH"=>$ticket['buyer_id'],
  1815. "GMFMC"=>$ticket['buyer_title'],
  1816. "GMFDZ"=>$ticket['buyer_addr'],
  1817. "GMFDH"=>$ticket['buyer_mobile'],
  1818. "GMFYH"=>$ticket['buyer_bank'],
  1819. "GMFZH"=>$ticket['buyer_bankNo'],
  1820. "GMFLX"=>$company_type,
  1821. "GMFSJH"=>"",
  1822. "GMFDZYX"=>'',
  1823. "KPR"=>$ticket['issuer'],
  1824. "SKR"=>$ticket['reciver'],
  1825. "FHR"=>$ticket['reviewer'],
  1826. "QDBZ"=>"0",
  1827. "YFPDM"=>$ticket['inv_code'],
  1828. "YFPHM"=>$ticket['inv_number'],
  1829. "YFPLX"=>$ticket['inv_type'],
  1830. "YFPKPRQ"=>date("Y-m-d",strtotime($ticket['open_date'])),
  1831. "CHYY"=>$return_reason,
  1832. "TSCHBZ"=>"0",
  1833. "JSHJ"=>"-".$ticket['inv_total'],
  1834. "HJJE"=>"-".$ticket['inv_subtotal'],
  1835. "HJSE"=>"-".$ticket['tax_fee'],
  1836. "BZ"=> "对应正数发票代码:{$ticket['inv_code']} 号码:{$ticket['inv_number']}",
  1837. "DDMXXX"=>json_encode($qdmx,JSON_UNESCAPED_UNICODE),
  1838. "FPLXDM"=>$ticket['inv_type'],
  1839. "DDQQPCH"=>"",
  1840. "DDQQLSH"=>randomkeys(26).date("YmdHis"),
  1841. "status"=>"0",
  1842. "result"=>"",
  1843. "addtime"=>date("Y-m-d H:i:s"),
  1844. "updatetime"=>date("Y-m-d H:i:s")
  1845. ];
  1846. $invoice=Db::name("invoice_interface")->insert($data);
  1847. if($invoice==false){
  1848. throw new Exception("红冲提交失败",1006);
  1849. }
  1850. $upred=Db::name("invoice_red")->save($reddata);
  1851. if($upred==false){
  1852. throw new Exception("红冲提交失败",1006);
  1853. }
  1854. return app_show(0,"红冲已提交");
  1855. }
  1856. private function addDiscard($ticket=[],$returnCode){
  1857. $invoice=Config::get("invoice");
  1858. $tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  1859. $result=$tax->DeprecateInvoices($ticket['seller_id'],$ticket['inv_type'],$ticket['inv_code'],$ticket['inv_number'],1);
  1860. if(isset($result['ZTDM']) && $result['ZTDM']=='050000'){
  1861. Db::name("invoice_return")->where(['returnCode'=>$returnCode])->update(["status"=>2,"updatetime"=>date("Y-m-d H:i:s")]);
  1862. $up =Db::name("invoice_pool")->where(['invNo'=>$ticket['invNo']])->update(["status"=>6,"updatetime"=>date("Y-m-d H:i:s")]);
  1863. if($up){
  1864. return app_show(0,"发票废弃成功");
  1865. }
  1866. }else{
  1867. throw new \Exception($result['ZTXX'], 1006);
  1868. }
  1869. }
  1870. //退票列表
  1871. public function returnRedList(){
  1872. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  1873. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  1874. $condition=[];
  1875. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  1876. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  1877. if($start!=""){
  1878. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  1879. }
  1880. if($end!=""){
  1881. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  1882. }
  1883. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? trim($this->post['inv_type']):"";
  1884. if($inv_type!==""){
  1885. $condition[]=["c.inv_type","=",$inv_type];
  1886. }
  1887. $companyNo = isset($this->post['companyNo'])&&$this->post['companyNo']!=="" ? trim($this->post['companyNo']):"";
  1888. if($companyNo!==""){
  1889. $condition[]=["c.inv_out","=",$companyNo];
  1890. }
  1891. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  1892. if($relaComNo!=""){
  1893. $condition[]=["c.inv_out","=",$relaComNo];
  1894. }
  1895. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  1896. if($apply_id!==""){
  1897. $condition[]=["a.apply_id","=",$apply_id];
  1898. }
  1899. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  1900. if($apply_name!==""){
  1901. $condition[]=["a.apply_name","like","%$apply_name%"];
  1902. }
  1903. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  1904. if($status!==""){
  1905. $condition[]=["a.status","=",$status];
  1906. }
  1907. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  1908. if($invNo!==""){
  1909. $condition[]=["a.invNo","like","%$invNo%"];
  1910. }
  1911. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!=="" ? trim($this->post['returnCode']):"";
  1912. if($returnCode!==""){
  1913. $condition[]=["a.returnCode","like","%$returnCode%"];
  1914. }
  1915. $count=Db::name("invoice_return")->alias("a")
  1916. ->leftJoin("invoice_ticket b","a.invNo=b.invNo and b.type=1")
  1917. ->leftJoin("invoice_pool c","a.invNo=c.invNo")
  1918. ->where($condition)->count();
  1919. $total =ceil($count/$size);
  1920. $page= $page>$total ? intval($total):$page;
  1921. $list=Db::name("invoice_return")->alias("a")
  1922. ->leftJoin("invoice_ticket b","a.invNo=b.invNo and b.type=1")
  1923. ->leftJoin("invoice_pool c","a.invNo=c.invNo")
  1924. ->field("b.*,a.*")->withoutField("pdfstream")->json(["item"])->page($page,$size)->where($condition)->select()
  1925. ->toArray();
  1926. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  1927. }
  1928. /**
  1929. * @return \think\response\Json|void
  1930. * @throws \think\db\exception\DataNotFoundException
  1931. * @throws \think\db\exception\DbException
  1932. * @throws \think\db\exception\ModelNotFoundException
  1933. */
  1934. public function tikcetList(){
  1935. $post=$this->request->only(["invNo"=>"","companyNo"=>"","relaComNo"=>"","page"=>1,"size"=>15],"post","trim");
  1936. $where=[];
  1937. if($post["invNo"]!="") $where[]=["a.invNo","=",$post['invNo']];
  1938. if($post["companyNo"]!="") $where[]=["b.inv_out","=",$post['companyNo']];
  1939. if($post["relaComNo"]!="") $where[]=["b.inv_out","=",$post['relaComNo']];
  1940. $count=Db::name("invoice_ticket")->where($where)->count();
  1941. $total =ceil($count/$post['size']);
  1942. $page= $post['page']>$total ? intval($total):intval($post['page']);
  1943. $list = Db::name("invoice_ticket")
  1944. ->alias("a")
  1945. ->leftJoin("invoice_pool b","a.invNo=b.invNo")
  1946. ->where($where)
  1947. ->field("a.*")
  1948. ->json(["a.item"])
  1949. ->page($page,intval($post['size']))
  1950. ->order("a.id desc")
  1951. ->select()->toArray();
  1952. foreach ($list as &$value){
  1953. $value['invtype_name'] = $this->typename[$value['inv_type']]??"";
  1954. }
  1955. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  1956. }
  1957. /** @param array invNo 发票编号集合
  1958. * @return \think\response\Json|void
  1959. */
  1960. public function downorder(){
  1961. $post=$this->request->only(["invNo"=>[]],"post","trim");
  1962. $condition=[["open_type","in",[0,2,3]],["is_del","=",0],["status","=",1]];
  1963. if(!empty($post['invNo'])){
  1964. $condition[]=['invNo',"in",$post['invNo']];
  1965. }
  1966. $invice = Db::name("invoice_pool")->where($condition)->select()->toArray();
  1967. if (empty($invice)){
  1968. return error_show(1003,"未找到数据");
  1969. }
  1970. $data=[];
  1971. foreach ($invice as $value){
  1972. $temp=[];
  1973. $orderNo = Db::name("assoc")->where([["status","=",1],["is_del","=",0],['viceCode',"=",$value['invNo']]])
  1974. ->find();
  1975. if(!$orderNo){
  1976. continue;
  1977. }
  1978. $good = Db::name("qrd_info")->where([['sequenceNo',"=",$orderNo['orderCode']],["status","=",1]])
  1979. ->findOrEmpty();
  1980. if(empty($good)){
  1981. continue;
  1982. }
  1983. if(isset($value['inv_out'])&& $value['inv_out']!=''){
  1984. $invout = Db::name("company_info")->where([['companyNo',"=",$value['inv_out']]])->find();
  1985. }
  1986. $invinfo = Db::name("invoice_pool_info")->where("invNo","=",$value['invNo'])->findOrEmpty();
  1987. // foreach ($good as $val){
  1988. // $qrd = Db::name("qrd_list")->where("sequenceNo","=",$val['goodNo'])->find();
  1989. $temp['invNo'] = $value['invNo'];
  1990. $temp['qrdNo'] = $orderNo['orderCode'];
  1991. $temp['depart'] = $good['department'];
  1992. $temp['apply_name'] =$value['apply_name'];
  1993. $temp['code'] ="";
  1994. $temp['invoice_title'] =$invinfo['buyer_title']??"";
  1995. $temp['invoice_code'] =$invinfo['buyer_code']." ";
  1996. $temp['invoice_addr'] =$invinfo['buyer_addr']??"";
  1997. $temp['invoice_mobile'] =$invinfo['buyer_mobile']??"";
  1998. $temp['invoice_bank'] =$invinfo['buyer_bank']??"";
  1999. $temp['invoice_bankNo'] =$invinfo['buyer_bankNo']." ";
  2000. $temp['company'] =$good['customerName'];
  2001. $temp['goodname'] =$good['goodName'];
  2002. $temp['tax'] =$good['tax'];
  2003. $temp['inv_name'] =$good['goodName'];
  2004. $temp['diff_weight'] =$good['diff_weight'];
  2005. $temp['diff_price'] =$good['diff_fee'];
  2006. $temp['unit'] =$good['goodUnit'];
  2007. $temp['num'] =$good['goodNum'];
  2008. $temp['price'] =$good['goodPrice'];
  2009. $temp['total_fee'] =$good['totalPrice'];
  2010. $temp['inv_type']=$this->invoiceType[$value['inv_type']]?? "-";
  2011. $temp['remark'] =$value['remark'];
  2012. $temp['kp_companyName'] = $invinfo['seller_title']??"";
  2013. $temp['kp_company_address'] = $invinfo['seller_addr']??"";
  2014. $temp['kp_company_license'] = $invinfo['seller_code']." ";
  2015. $temp['kp_bank_name'] = $invinfo['seller_bank']??"";
  2016. $temp['kp_bankNo'] = $invinfo['seller_bankNo']."";
  2017. $temp['kp_contector'] = $invout['contector']??"";
  2018. $temp['kp_mobile'] = $invinfo['seller_mobile']??"";
  2019. $data[]=$temp;
  2020. }
  2021. // }
  2022. if (empty($data)){
  2023. return error_show(1003,"未找到数据");
  2024. }
  2025. $header=[
  2026. "申请编号",
  2027. "公司订单号",
  2028. "申请部门(所属平台)",
  2029. "申请人",
  2030. "代码",
  2031. "发票开具全称",
  2032. "发票开具企业纳税识别号",
  2033. "发票开具企业地址",
  2034. "发票开具企业地址联系方式",
  2035. "发票开具企业收款银行",
  2036. "发票开具企业收款账户",
  2037. "分公司",
  2038. "采购商品名",
  2039. "税率",
  2040. "发票商品名",
  2041. "工差重量",
  2042. "工差金额",
  2043. "单位",
  2044. "数量",
  2045. "单价",
  2046. "总价",
  2047. "开票种类",
  2048. "备注说明",
  2049. '开票企业名称',
  2050. '开票企业地址',
  2051. '开票企业纳税号',
  2052. '开票企业银行名称',
  2053. '开票企业银行账户',
  2054. '开票企业法人',
  2055. '开票企业联系方式'
  2056. ];
  2057. $li = excelExport("待开票",$header,$data);
  2058. }
  2059. public function importInv(){
  2060. $param =$this->request->only(["data"=>[]],"post");
  2061. if(empty($param['data']))return error_show(1004,"参数 data 不能为空");
  2062. $total=[];
  2063. $list=[];
  2064. foreach ($param['data'] as $key=>$value){
  2065. if($value['invNo']==""){
  2066. return error_show(1005,"第".($key+1)."行缺少发票申请编号数据");
  2067. }
  2068. if($value["inv_type"]===""){
  2069. return error_show(1005,"第".($key+1)."行缺少发票类型数据");
  2070. }
  2071. if(!in_array($value['inv_type'],$this->invName)) return error_show(1005,"第".($key+1)."行发票类型数据有误");
  2072. if($value["inv_code"]=="" && !in_array($value['inv_type'],[ "fully_digitalized_special_electronic", "fully_digitalized_normal_electronic"])){
  2073. return error_show(1005,"第".($key+1)."行缺少发票代码数据");
  2074. }
  2075. if($value["inv_number"]==""){
  2076. return error_show(1005,"第".($key+1)."行缺少发票号码数据");
  2077. }
  2078. if($value["inv_total"]==""&& in_array($value['inv_type'],["fully_digitalized_special_electronic", "fully_digitalized_normal_electronic"])){
  2079. return error_show(1005,"第".($key+1)."行缺少发票金额数据");
  2080. }
  2081. if($value["inv_subtotal"]==""){
  2082. return error_show(1005,"第".($key+1)."行缺少发票税后金额数据");
  2083. }
  2084. if($value["open_date"]==""){
  2085. return error_show(1005,"第".($key+1)."行缺少发票开票日期数据");
  2086. }
  2087. //电子票校验码必传
  2088. if($value["check_code"]=="" && in_array($value['inv_type'],['normal','roll',"toll",'electronic'])){
  2089. return error_show(1005,"第".($key+1)."行缺少发票校验码数据");
  2090. }
  2091. // if($value['inv_type']==="000"){
  2092. // if($value['seller_id']=="") return error_show(1005,"第".($key+1)."行缺少销售方纳税识别号数据");
  2093. // if($value['seller_title']=="") return error_show(1005,"第".($key+1)."行缺少销售方名称数据");
  2094. // if($value['seller_addr']=="") return error_show(1005,"第".($key+1)."行缺少销售方地址数据");
  2095. // if($value['seller_mobile']=="") return error_show(1005,"第".($key+1)."行缺少销售方联系方式数据");
  2096. // if($value['seller_bank']=="") return error_show(1005,"第".($key+1)."行缺少销售方银行数据");
  2097. // if($value['seller_bankNo']=="") return error_show(1005,"第".($key+1)."行缺少销售方银行账户数据");
  2098. // if($value['buyer_id']=="") return error_show(1005,"第".($key+1)."行缺少购买方纳税识别号数据");
  2099. // if($value['buyer_title']=="") return error_show(1005,"第".($key+1)."行缺少购买方名称数据");
  2100. // if($value['buyer_addr']=="") return error_show(1005,"第".($key+1)."行缺少购买方地址数据");
  2101. // if($value['buyer_mobile']=="") return error_show(1005,"第".($key+1)."行缺少购买方联系方式数据");
  2102. // if($value['buyer_bank']=="") return error_show(1005,"第".($key+1)."行缺少购买方银行数据");
  2103. // if($value['buyer_bankNo']=="") return error_show(1005,"第".($key+1)."行缺少购买方银行账户数据");
  2104. // if($value['tax_fee']=="") return error_show(1005,"第".($key+1)."行缺少发票税额数据");
  2105. // }
  2106. !isset($total[$value['invNo']]) ? $total[$value['invNo']]['total']=0:"";
  2107. $temp=[];
  2108. $temp['invNo'] = $value["invNo"];
  2109. $temp['type'] = 0;
  2110. $temp['inv_type'] =$value['inv_type'];
  2111. $temp['inv_code'] = $value['inv_code'];
  2112. $temp['inv_number'] = $value['inv_number'];
  2113. $temp['inv_subtotal'] = $value['inv_subtotal'];
  2114. $temp['inv_total'] = $value['inv_total'];
  2115. $temp['check_code'] = $value['check_code']??"";
  2116. $temp['seller_id'] = $value['seller_id']??"";
  2117. $temp['seller_title'] = $value['seller_title']??"";
  2118. $temp['seller_addr'] = $value['seller_addr']??"";
  2119. $temp['seller_mobile'] = $value['seller_mobile']??"";
  2120. $temp['seller_bank'] = $value['seller_bank']??"";
  2121. $temp['seller_bankNo'] = $value['seller_bankNo']??"";
  2122. $temp['buyer_id'] = $value['buyer_id']??"";
  2123. $temp['buyer_title'] = $value['buyer_title']??"";
  2124. $temp['buyer_addr'] = $value['buyer_addr']??"";
  2125. $temp['buyer_mobile'] = $value['buyer_mobile']??"";
  2126. $temp['buyer_bank'] = $value['buyer_bank']??"";
  2127. $temp['buyer_bankNo'] = $value['buyer_bankNo']??"";
  2128. $temp['tax_fee'] = $value['tax_fee']??"";
  2129. $temp['item'] = '';
  2130. $temp['pdfstream'] = '';
  2131. $temp['open_date'] = date("Y-m-d H:i:s",strtotime($value['open_date']));
  2132. $temp['status'] = 1;
  2133. $temp['is_del'] = 0;
  2134. $temp['addtime'] = date("Y-m-d H:i:s");
  2135. $temp['updatetime'] = date("Y-m-d H:i:s");
  2136. $total[$value["invNo"]]=$temp;
  2137. }
  2138. Db::startTrans();
  2139. try{
  2140. $keyarr =array_keys($total);
  2141. $inpool = Db::name("invoice_pool")->where([["invNo","in",$keyarr],["open_type","in",[0,2,3]],["is_del","=",0],["status","=",0]])->column("winv_fee,invNo,inv_value,inv_type","invNo");
  2142. foreach ($total as $key=>$val){
  2143. if(!isset($inpool[$key])) continue;
  2144. if(in_array($val['inv_type'],["fully_digitalized_special_electronic", "fully_digitalized_normal_electronic"]) && $inpool[$key]['winv_fee']!=$val['total']){
  2145. throw new Exception("{$key}全电发票面金额小于未开票金额");
  2146. }
  2147. if($inpool[$key]['inv_type']!= $val['inv_type']) throw new Exception("{$key}发票类型有误");
  2148. $update=[
  2149. "status"=>in_array($val['inv_type'],["fully_digitalized_special_electronic", "fully_digitalized_normal_electronic"])?3:2,
  2150. "open_type"=>$inpool[$key]['open_type']??3,
  2151. "updatetime"=>date("Y-m-d H:i:s")
  2152. ];
  2153. $up =Db::name("invoice_pool")->where([["invNo","=",$key],["is_del","=",0]])->save($update);
  2154. if(!$up){
  2155. Db::rollback();
  2156. return error_show(1005,"发票状态更新失败");
  2157. }
  2158. if($update['status']==3){
  2159. $qrdArr=Db::name("assoc")->where(["viceCode"=>$key,"is_del"=>0])->column("id,orderCode,cancel_fee");
  2160. if(!empty($qrdArr)){
  2161. foreach ($qrdArr as $value){
  2162. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  2163. if($qrdinfo==false){
  2164. Db::rollback();
  2165. return error_show(1003,"确认单信息未找到");
  2166. }
  2167. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  2168. Db::rollback();
  2169. return error_show(1003,"确认单信息开票金额不足");
  2170. }
  2171. $update =[
  2172. "ainv_fee"=>$qrdinfo['ainv_fee']+$value['cancel_fee'],
  2173. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  2174. "invtime"=>date("Y-m-d H:i:s"),
  2175. "inv_status"=>$qrdinfo['winv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 3 : 2,
  2176. "updatetime"=>date("Y-m-d H:i:s"),
  2177. ];
  2178. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  2179. if($qrdup==false){
  2180. Db::rollback();
  2181. return error_show(1003,"确认单信息更新失败");
  2182. }
  2183. $assoc=["status"=>2,"assoc_time"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  2184. $assocup =Db::name("assoc")->where($value)->update($assoc);
  2185. if($assocup==false){
  2186. Db::rollback();
  2187. return error_show(1003,"确认单关联信息更新失败");
  2188. }
  2189. }
  2190. }
  2191. $val['inv_type'] = $this->kingInvoice[$val['inv_type']];
  2192. $list[]=$val;
  2193. }
  2194. }
  2195. $inticket =Db::name("invoice_ticket")->insertAll($list);
  2196. if(!$inticket){
  2197. Db::rollback();
  2198. return error_show(1005,"发票数据录入失败");
  2199. }
  2200. Db::commit();
  2201. return app_show(0,"发票数据录入成功");
  2202. }catch (\Exception $e){
  2203. Db::rollback();
  2204. return error_show(1008,$e->getMessage());
  2205. }
  2206. }
  2207. }