OrderInv.php 122 KB

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