OrderInv.php 107 KB

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