OrderInv.php 122 KB

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