OrderInv.php 123 KB

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