OrderInv.php 107 KB

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