OrderInv.php 122 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630
  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. if(in_array($ticket['inv_type'],['007','026'])) $result= $this->addRedOpen($ticket,$return_reason,$invinfo['company_type'],$returnCode,$red_desc);
  916. if(in_array($ticket['inv_type'],['004','028'])) $result= $this->addApplyRed($ticket,$return_reason,$returnCode,$red_desc);
  917. if(isset($result['code'])){
  918. if(isset($result['data']) &&$result['invRed']!='' ){
  919. $upd=Db::name("invoice_return")->where(["returnCode"=>$returnCode])->update(['red_inv'=>$result['invRed'],"updatetime"=>date("Y-m-d H:i:s")]);
  920. if($upd==false) throw new \Exception("退票更新失败");
  921. }
  922. }
  923. }else{
  924. $invpool=[
  925. "invNo"=>$invinfo['invNo'],
  926. "type"=>1,
  927. "inv_type"=>$ticket['inv_type'],
  928. "inv_code"=>$invCode,
  929. "inv_number"=>$invNum,
  930. "check_code"=>$checkCode,
  931. "inv_total"=>0,
  932. "inv_subtotal"=>$subtotal_fee,
  933. "open_date"=>$open_date,
  934. "item"=>'',
  935. 'pdfstream'=>'',
  936. "status"=>0,
  937. "addtime"=>date("Y-m-d H:i:s"),
  938. "updatetime"=>date("Y-m-d H:i:s")
  939. ];
  940. $in = Db::name("invoice_ticket")->insert($invpool);
  941. if($in==false)throw new \Exception("退票信息添加失败");
  942. }
  943. }
  944. if($status==2){
  945. $infoup =Db::name("invoice_good")->where(["invNo"=>$return['invNo'],"is_del"=>0])->update(["goodNum"=>0,"updatetime"=>date("Y-m-d H:i:s")]);
  946. if($infoup==false)throw new \Exception("退票商品信息修改失败");
  947. $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")]);
  948. if($poolup==false)throw new \Exception($return['invNo']."开票票数据更新失败");
  949. $qrdArr=Db::name("assoc")->where(["viceCode"=>$return['invNo'],"is_del"=>0,"status"=>2])->column("id,orderCode,cancel_fee");
  950. if(!empty($qrdArr)){
  951. foreach ($qrdArr as $value){
  952. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  953. if($qrdinfo==false){
  954. throw new Exception("确认单信息未找到");
  955. // return error_show(1003,"确认单信息未找到");
  956. }
  957. if($qrdinfo['ainv_fee']<$value['cancel_fee']){
  958. throw new Exception("确认单信息开票金额不足");
  959. // return error_show(1003,"确认单信息开票金额不足");
  960. }
  961. $update =[
  962. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  963. "ainv_fee"=>$qrdinfo['ainv_fee']-$value['cancel_fee'],
  964. "inv_status"=>$qrdinfo['inv_fee']==0 &&$qrdinfo['ainv_fee']-$value['cancel_fee']==0 ? 1 : 2,
  965. "status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']-$value['cancel_fee']==0&&$qrdinfo['pay_status']==1 ?0 : 1,
  966. "updatetime"=>date("Y-m-d H:i:s"),
  967. ];
  968. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  969. if($qrdup==false){
  970. throw new Exception("确认单信息更新失败");
  971. // return error_show(1003,"确认单信息更新失败");
  972. }
  973. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  974. $assocup =Db::name("assoc")->where($value)->update($assoc);
  975. if($assocup==false){
  976. throw new Exception("确认单关联信息更新失败");
  977. // return error_show(1003,"确认单关联信息更新失败");
  978. }
  979. }
  980. }else{
  981. throw new Exception("未找到关联订单数据信息");
  982. }
  983. }
  984. if($status==3 || $status==4){
  985. foreach ($qrd as $value){
  986. $report=ReportCode::where(["qrdNo"=>$value])->find();
  987. if($report)$report->rmField("returnInv",$returnCode);
  988. }
  989. }
  990. Db::commit();
  991. return app_show(0,"退票申请信息更新成功");
  992. }
  993. Db::rollback();
  994. return error_show(1005,"退票申请信息更新失败");
  995. }catch (\Exception $e){
  996. Db::rollback();
  997. return error_show(1005,$e->getMessage());
  998. }
  999. }
  1000. //红冲票
  1001. private function addDiscard($ticket=[],$returnCode){
  1002. $invoiceConf=Config::get('invoice');
  1003. if(!isset($invoiceConf[$ticket['seller_id']]))throw new \Exception('金税配置不存在', 1006);
  1004. $invoice = $invoiceConf[$ticket['seller_id']];
  1005. $tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  1006. $result=$tax->DeprecateInvoices($ticket['seller_id'],$ticket['inv_type'],$ticket['inv_code'],$ticket['inv_number'],1);
  1007. if(isset($result['ZTDM']) && ($result['ZTDM']=='050000' || $result['ZTDM']=='053000')){
  1008. if($result['ZTDM']=='053000'){
  1009. $up= Db::name("invoice_return")->where(['returnCode'=>$returnCode])->update(["discard"=>2,"updatetime"=>date("Y-m-d H:i:s")]);
  1010. if($up==false)throw new \Exception('发票废弃中,信息更新失败', 1006);
  1011. return ["code"=>1,"message"=>"发票废弃中"];
  1012. }else{
  1013. $ups=Db::name("invoice_return")->where(['returnCode'=>$returnCode])->update(["status"=>2,"discard"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  1014. if($ups==false)throw new \Exception('发票已废弃,信息更新失败', 1006);
  1015. $up =Db::name("invoice_pool")->where(['invNo'=>$ticket['invNo']])->update(["status"=>6,"updatetime"=>date("Y-m-d H:i:s")]);
  1016. if($up==false)throw new \Exception('发票已废弃,信息更新失败', 1006);
  1017. return ["code"=>0,"message"=>"发票废弃成功"];
  1018. }
  1019. }else{
  1020. throw new \Exception($result['ZTXX'], 1006);
  1021. }
  1022. }
  1023. //退票详情
  1024. /**
  1025. * @param array $ticket
  1026. * @return \think\response\Json|void
  1027. * @throws \think\Exception
  1028. */
  1029. private function addRedOpen($ticket=[],$return_reason,$company_type,$returnCode,$red_desc){
  1030. $goodinfo =Db::name("invoice_good")
  1031. ->where(["invNo"=>$ticket['invNo'],"is_del"=>0])
  1032. ->field("0 as FPHXZ,
  1033. catNo as SPBM,
  1034. id as ZXBM,
  1035. is_discount as YHZCBS,
  1036. if(inv_tag=0,'',inv_tag) as LSLBS,
  1037. addTax as ZZSTSGL,
  1038. goodName as XMMC,
  1039. '' as DW,
  1040. '' as SPSL,
  1041. '' as DJ,
  1042. (-totalPrice) as JE,
  1043. 0 HSBZ,
  1044. tax as SL,
  1045. -taxPrice as SE")->select()->toArray();
  1046. if(empty($goodinfo)){
  1047. throw new \Exception("开票商品信息未找到", 10006);
  1048. }
  1049. if(count($goodinfo)>8){
  1050. $len =array_unique(array_column($goodinfo,'SL'));
  1051. $qdmx=["FPHXZ"=>6,
  1052. "SPBM"=>'',
  1053. "XMMC"=>"详见对应正数发票及清单",
  1054. "YHZCBS"=>0,
  1055. "LSLBS"=>'',
  1056. "ZZSTSGL"=>'',
  1057. "DW"=>'',
  1058. "SPSL"=>'',
  1059. "DJ"=>'',
  1060. "JE"=>sprintf("%01.2f", (array_sum(array_column($goodinfo,'JE')))),
  1061. "HSBZ"=>'0',
  1062. "SL"=>count($len)>1?'':$len[0],
  1063. "SE"=>sprintf("%01.2f",array_sum(array_column($goodinfo,'SE'))),
  1064. ];
  1065. }else
  1066. {$qdmx=$goodinfo;}
  1067. $invRed=makeNo("IRE");
  1068. $reddata=[
  1069. 'invNo'=>$ticket['invNo'],
  1070. "invRed"=>$invRed,
  1071. "inv_out"=>$ticket['inv_out'],
  1072. "returnCode"=>$returnCode,
  1073. "apply_id"=>$this->uid,
  1074. "apply_name"=>$this->uname,
  1075. "status"=>3,
  1076. "remark"=>'',
  1077. "addtime"=>date("Y-m-d H:i:s"),
  1078. "updatetime"=>date("Y-m-d H:i:s"),
  1079. "SQBSCQQLSH"=>'',
  1080. "YYSBZ"=> "0000000000", //营业税标志
  1081. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1082. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1083. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1084. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1085. "TKSJ"=>date("YmdHis"), //填开时间
  1086. "XHFSBH"=>$ticket['seller_id'],
  1087. "XHFMC"=>$ticket['seller_title'],
  1088. "GMFSBH"=>$ticket['buyer_id'],
  1089. "GMFMC"=> $ticket['buyer_title'],
  1090. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1091. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1092. "SQSM"=>$red_desc, //申请说明 1100000000 已抵扣 1010000000未抵扣 0000000100尚未寄出
  1093. "XXBTSBS"=>"0",
  1094. "red_reason"=>$return_reason,
  1095. "DDMXXX"=>json_encode($goodinfo,JSON_UNESCAPED_UNICODE),
  1096. "SQBSCQQPCH"=>'',
  1097. "FPLXDM"=>$ticket['inv_type'],
  1098. ];
  1099. $data=[ "invNo"=>$invRed,
  1100. "KPLX"=>"1",
  1101. "XHFSBH"=>$ticket['seller_id'],
  1102. "XHFMC"=>$ticket['seller_title'],
  1103. "XHFDZ"=>$ticket['seller_addr'],
  1104. "XHFDH"=>$ticket['seller_mobile'],
  1105. "XHFYH"=>$ticket['seller_bank'],
  1106. "XHFZH"=>$ticket['seller_bankNo'],
  1107. "GMFBM"=>"",
  1108. "GMFSBH"=>$ticket['buyer_id'],
  1109. "GMFMC"=>$ticket['buyer_title'],
  1110. "GMFDZ"=>$ticket['buyer_addr'],
  1111. "GMFDH"=>$ticket['buyer_mobile'],
  1112. "GMFYH"=>$ticket['buyer_bank'],
  1113. "GMFZH"=>$ticket['buyer_bankNo'],
  1114. "GMFLX"=>$company_type,
  1115. "GMFSJH"=>"",
  1116. "GMFDZYX"=>'',
  1117. "KPR"=>$ticket['issuer'],
  1118. "SKR"=>$ticket['reciver'],
  1119. "FHR"=>$ticket['reviewer'],
  1120. "QDBZ"=>"0",
  1121. "YFPDM"=>$ticket['inv_code'],
  1122. "YFPHM"=>$ticket['inv_number'],
  1123. "YFPLX"=>$ticket['inv_type'],
  1124. "YFPKPRQ"=>date("Y-m-d",strtotime($ticket['open_date'])),
  1125. "CHYY"=>$return_reason,
  1126. "TSCHBZ"=>"0",
  1127. "JSHJ"=>"-".$ticket['inv_total'],
  1128. "HJJE"=>"-".$ticket['inv_subtotal'],
  1129. "HJSE"=>"-".$ticket['tax_fee'],
  1130. "BZ"=> "对应正数发票代码:{$ticket['inv_code']} 号码:{$ticket['inv_number']}",
  1131. "DDMXXX"=>json_encode($qdmx,JSON_UNESCAPED_UNICODE),
  1132. "FPLXDM"=>$ticket['inv_type'],
  1133. "DDQQPCH"=>"",
  1134. "DDQQLSH"=>randomkeys(26).date("YmdHis"),
  1135. "status"=>"0",
  1136. "result"=>"",
  1137. "addtime"=>date("Y-m-d H:i:s"),
  1138. "updatetime"=>date("Y-m-d H:i:s")
  1139. ];
  1140. $invoice=Db::name("invoice_interface")->insert($data);
  1141. if($invoice==false){
  1142. throw new Exception("红冲提交失败",1006);
  1143. }
  1144. $upred=Db::name("invoice_red")->save($reddata);
  1145. if($upred==false){
  1146. throw new Exception("红冲提交失败",1006);
  1147. }
  1148. return ["code"=>0,"message"=>"红冲申请提交成功","invRed"=>$invRed];
  1149. }
  1150. /**
  1151. * @param array $ticket
  1152. * @return \think\response\Json|void|array
  1153. * @throws \think\db\exception\DataNotFoundException
  1154. * @throws \think\db\exception\DbException
  1155. * @throws \think\db\exception\ModelNotFoundException
  1156. */
  1157. private function addApplyRed($ticket=[],$return_reason,$returnCode,$red_desc){
  1158. $invoiceConf=Config::get('invoice');
  1159. if(!isset($invoiceConf[$ticket['seller_id']]))throw new \Exception('金税配置不存在', 1006);
  1160. $invoice = $invoiceConf[$ticket['seller_id']];
  1161. $Tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  1162. $redticketList=[];
  1163. $redticketArry=[
  1164. "SQBSCQQPCH"=>randomkeys(26).date("YmdHis"),
  1165. "NSRSBH"=>$ticket['seller_id'],
  1166. "FPLXDM"=>$ticket['inv_type'],
  1167. ];
  1168. $goodinfo =Db::name("invoice_good")
  1169. ->where(["invNo"=>$ticket['invNo'],"is_del"=>0])
  1170. ->field("0 as FPHXZ,
  1171. catNo as SPBM,
  1172. id as ZXBM,
  1173. is_discount as YHZCBS,
  1174. if(inv_tag=0,'',inv_tag) as LSLBS,
  1175. addTax as ZZSTSGL,
  1176. goodName as XMMC,
  1177. '' as DW,
  1178. '' as SPSL,
  1179. '' as DJ,
  1180. (-totalPrice) as JE,
  1181. 0 HSBZ,
  1182. tax as SL,
  1183. -taxPrice as SE")->select()->toArray();
  1184. if(empty($goodinfo)){
  1185. throw new \Exception("开票商品信息未找到", 10006);
  1186. }
  1187. if(count($goodinfo)>8){
  1188. $len =array_unique(array_column($goodinfo,'SL'));
  1189. $qdmx=["FPHXZ"=>6,
  1190. "SPBM"=>'',
  1191. "XMMC"=>"详见对应正数发票及清单",
  1192. "YHZCBS"=>0,
  1193. "LSLBS"=>'',
  1194. "ZZSTSGL"=>'',
  1195. "DW"=>'',
  1196. "SPSL"=>'',
  1197. "DJ"=>'',
  1198. "JE"=>sprintf("%01.2f", (array_sum(array_column($goodinfo,'JE')))),
  1199. "HSBZ"=>'0',
  1200. "SL"=>count($len)>1?'':$len[0],
  1201. "SE"=>sprintf("%01.2f",array_sum(array_column($goodinfo,'SE'))),
  1202. ];
  1203. }else
  1204. {
  1205. $qdmx=$goodinfo;
  1206. }
  1207. $info=[
  1208. "SQBSCQQLSH"=>randomkeys(26).date("YmdHis"),
  1209. "YYSBZ"=> "0000000000", //营业税标志
  1210. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1211. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1212. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1213. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1214. "TKSJ"=>date("YmdHis"), //填开时间
  1215. "XHFSBH"=>$ticket['seller_id'],
  1216. "XHFMC"=>$ticket['seller_title'],
  1217. "GMFSBH"=>$ticket['buyer_id'],
  1218. "GMFMC"=> $ticket['buyer_title'],
  1219. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1220. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1221. "SQSM"=>$red_desc, //申请说明
  1222. "XXBTSBS"=>"0",
  1223. //0: 正常;1:成品油发票,涉及销售数量 变更(销货退回、开票有误等原因);2: 成品油发票,仅涉及销售金额变更(销 售折让);3:机动车发票,仅涉及销售数
  1224. //量变更(销货退回、开票有误等原因);4: 机动车发票,仅涉及销售金额变更(销 售折让)
  1225. ];
  1226. $invRed=makeNo("IRE");
  1227. $data=[
  1228. 'invNo'=>$ticket['invNo'],
  1229. "invRed"=>$invRed,
  1230. "inv_out"=>$ticket['inv_out'],
  1231. "returnCode"=>$returnCode,
  1232. "apply_id"=>$this->uid,
  1233. "apply_name"=>$this->uname,
  1234. "status"=>0,
  1235. "remark"=>'',
  1236. "addtime"=>date("Y-m-d H:i:s"),
  1237. "updatetime"=>date("Y-m-d H:i:s"),
  1238. "SQBSCQQLSH"=>$info['SQBSCQQLSH'],
  1239. "YYSBZ"=> "0000000000", //营业税标志
  1240. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1241. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1242. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1243. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1244. "TKSJ"=>date("YmdHis"), //填开时间
  1245. "XHFSBH"=>$ticket['seller_id'],
  1246. "XHFMC"=>$ticket['seller_title'],
  1247. "GMFSBH"=>$ticket['buyer_id'],
  1248. "GMFMC"=> $ticket['buyer_title'],
  1249. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1250. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1251. "SQSM"=>$red_desc, //申请说明
  1252. "red_reason"=>$return_reason,
  1253. "XXBTSBS"=>"0",
  1254. "DDMXXX"=>json_encode($qdmx,JSON_UNESCAPED_UNICODE),
  1255. "SQBSCQQPCH"=>$redticketArry['SQBSCQQPCH'],
  1256. "FPLXDM"=>$ticket['inv_type'],
  1257. ];
  1258. $redticketList[]=["HZSQDTXX"=>$info,"DDMXXX"=>$qdmx];
  1259. $result=$Tax->AllocateRedInvoiceApplication($redticketArry,$redticketList);
  1260. $data['status']=5;
  1261. $data['remark']='红冲申请提交失败';
  1262. if(isset($result['ZTDM'])){
  1263. // $resultChild=$result['HZSQDSCJG'][0];
  1264. if($result['ZTDM']=='060000'){
  1265. $data['status']=1;
  1266. // $data['XXBBH']=$resultChild['XXBBH'];
  1267. $data['remark']='红冲申请审核通过';
  1268. }
  1269. if($result['ZTDM']=='060001'){
  1270. $data['status']=1;
  1271. // $data['XXBBH']=$resultChild['XXBBH'];
  1272. $data['remark']='红冲申请审核中';
  1273. }
  1274. }
  1275. if($data['status']==5){
  1276. throw new \Exception($result['ZTXX'], 10006);
  1277. }
  1278. $up =Db::name("invoice_red")->insert($data);
  1279. if($up) return ["code"=>0,"message"=>"红冲发票申请新建成功","invRed"=>$invRed];
  1280. else throw new \Exception($result['ZTXX'], 1006);
  1281. }
  1282. //获取发票详情
  1283. public function returnRed(){
  1284. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  1285. if($returnCode==""){
  1286. return error_show(1005,"参数 returnCode 不能为空");
  1287. }
  1288. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  1289. if($return==false){
  1290. return error_show(1005,"退票申请信息未找到");
  1291. }
  1292. $redinv =isset($this->post['redinv'])&&$this->post['redinv']!="" ? trim($this->post['redinv']):"";
  1293. if($redinv==""){
  1294. return error_show(1005,"参数 redinv 不能为空");
  1295. }
  1296. $red =Db::name("invoice_red")->where(["invRed"=>$redinv,"is_del"=>0])->findOrEmpty();
  1297. if(empty($red)) return error_show(1005,"红冲发票信息未找到");
  1298. if($red['status']!=4) return error_show(1005,"红冲发票开票未完成");
  1299. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  1300. if($invinfo==false){
  1301. return error_show(1005,"发票申请信息未找到");
  1302. }
  1303. Db::startTrans();
  1304. try{
  1305. $update=["status"=>2,"red_inv"=>$redinv,"updatetime"=>date("Y-m-d H:i:s")];
  1306. $up =Db::name("invoice_return")->where($return)->update($update);
  1307. if($up){
  1308. $invup = ["status"=>6,"updatetime"=>date("Y-m-d H:i:s")];
  1309. $inv=Db::name("invoice_pool")->where($invinfo)->update($invup);
  1310. if($inv){
  1311. $qrdArr=Db::name("assoc")->where(["viceCode"=>$return['invNo'],"is_del"=>0])->column("id,orderCode,cancel_fee");
  1312. if(!empty($qrdArr)){
  1313. foreach ($qrdArr as $value){
  1314. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  1315. if($qrdinfo==false){
  1316. Db::rollback();
  1317. return error_show(1003,"确认单信息未找到");
  1318. }
  1319. if($qrdinfo['ainv_fee']<$value['cancel_fee']){
  1320. Db::rollback();
  1321. return error_show(1003,"确认单信息开票金额不足");
  1322. }
  1323. $update =[
  1324. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  1325. "ainv_fee"=>$qrdinfo['ainv_fee']-$value['cancel_fee'],
  1326. "inv_status"=>$qrdinfo['inv_fee']==0 &&$qrdinfo['ainv_fee']-$value['cancel_fee']==0 ?
  1327. 1 : 2,
  1328. "status"=>$qrdinfo['inv_fee']==0 &&$qrdinfo['ainv_fee']-$value['cancel_fee']==0 && $qrdinfo['pay_status']==1 ?0 : 1,
  1329. "updatetime"=>date("Y-m-d H:i:s"),
  1330. ];
  1331. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  1332. if($qrdup==false){
  1333. Db::rollback();
  1334. return error_show(1003,"确认单信息更新失败");
  1335. }
  1336. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  1337. $assocup =Db::name("assoc")->where($value)->update($assoc);
  1338. if($assocup==false){
  1339. Db::rollback();
  1340. return error_show(1003,"确认单关联信息更新失败");
  1341. }
  1342. }
  1343. }
  1344. Db::commit();
  1345. return app_show(0,"退票申请信息更新成功");
  1346. }
  1347. }
  1348. Db::rollback();
  1349. return error_show(1005,"退票申请信息更新失败");
  1350. }catch (\Exception $e){
  1351. Db::rollback();
  1352. return error_show(1005,$e->getMessage());
  1353. }
  1354. }
  1355. //开票异常失败后修改票面信息
  1356. public function returnInfo(){
  1357. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  1358. if($returnCode==""){
  1359. return error_show(1005,"参数 returnCode 不能为空");
  1360. }
  1361. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  1362. if($return==false){
  1363. return error_show(1005,"退票申请信息未找到");
  1364. }
  1365. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  1366. $return['inv_out']= $invinfo['inv_out']??"";
  1367. $return['inv_value']= $invinfo['inv_value']??"";
  1368. $return['inv_apply_id']= $invinfo['apply_id']??"";
  1369. $return['inv_apply_name']= $invinfo['apply_name']??"";
  1370. $return['inv_type']= $invinfo['inv_type']??"";
  1371. $return['open_type']= $invinfo['open_type']??"";
  1372. $return['is_ticket']= $invinfo['is_ticket']??"";
  1373. $return['email']= $invinfo['email']??"";
  1374. $return['post_code']= $invinfo['post_code']??"";
  1375. $return['post_company']= $invinfo['post_company']??"";
  1376. $return['check_remark']= $invinfo['check_remark']??"";
  1377. $poolinfo = Db::name("invoice_pool_info")->where(["invNo"=>$return['invNo']])->find();
  1378. $return['buyer_title'] = $poolinfo['buyer_title']??'';
  1379. $return['buyer_title'] = $poolinfo['buyer_title']??'';
  1380. $return['buyer_code'] = $poolinfo['buyer_code']??'';
  1381. $return['buyer_addr'] = $poolinfo['buyer_addr']??'';
  1382. $return['buyer_mobile'] = $poolinfo['buyer_mobile']??'';
  1383. $return['buyer_bank'] = $poolinfo['buyer_bank']??'';
  1384. $return['buyer_bankNo'] = $poolinfo['buyer_bankNo']??'';
  1385. $return['seller_title'] = $poolinfo['seller_title']??'';
  1386. $return['seller_addr'] = $poolinfo['seller_addr']??'';
  1387. $return['seller_code'] = $poolinfo['seller_code']??'';
  1388. $return['seller_mobile'] = $poolinfo['seller_mobile']??'';
  1389. $return['seller_bank'] = $poolinfo['seller_bank']??'';
  1390. $return['seller_bankNo'] = $poolinfo['seller_bankNo']??'';
  1391. $return['voider'] = $poolinfo['voider']??'';
  1392. $return['payee'] = $poolinfo['payee']??'';
  1393. $return['drawer'] = $poolinfo['drawer']??'';
  1394. $return['reviewer'] = $poolinfo['reviewer']??'';
  1395. $return['ownerPlace'] = $poolinfo['ownerPlace']??'';
  1396. $orderList =Db::name("invoice_good")->where(["invNo"=>$return['invNo'],"is_del"=>0])->select()->toArray();
  1397. $return['goodList']=$orderList;
  1398. $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();
  1399. $return['inv_number']=$info['inv_number']??"";
  1400. $return['inv_code']=$info['inv_code']??"";
  1401. $return['open_date']=date("Y-m-d",strtotime($info['open_date']));
  1402. $return['inv_subtotal']=$info['inv_subtotal']??"";
  1403. $return['inv_total']=$info['inv_total']??"";
  1404. $return['check_code']=$info['check_code']??"";
  1405. if(!in_array($return['status'],[2,3,4,5])){
  1406. $opendate =$info['open_date']??"";;
  1407. if($opendate!=''){
  1408. $open_month =date("Y-m",strtotime($opendate));
  1409. $return['is_out_month']= $open_month==date("Y-m")?0:1;
  1410. }
  1411. }
  1412. return app_show(0,"获取成功",$return);
  1413. }
  1414. /**
  1415. *发票池列表
  1416. */
  1417. public function list(){
  1418. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  1419. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  1420. $condition=[["a.is_del","=",0]];
  1421. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  1422. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  1423. $roleid = $this->roleid;
  1424. $check = checkRole($roleid,'84');
  1425. if($check){
  1426. $condition[]=["a.apply_id","=",$this->uid];
  1427. }
  1428. if($start!=""){
  1429. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  1430. }
  1431. if($end!=""){
  1432. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  1433. }
  1434. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  1435. if($status!==""){
  1436. $condition[]=["a.status","=",$status];
  1437. }
  1438. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? trim($this->post['inv_type']):"";
  1439. if($inv_type!==""){
  1440. $condition[]=["a.inv_type","=",$inv_type];
  1441. }
  1442. $inv_out= isset($this->post['inv_out'])&&$this->post['inv_out']!=="" ? trim($this->post['inv_out']):"";
  1443. if($inv_out!==""){
  1444. $condition[]=["a.inv_out","=",$inv_out];
  1445. }
  1446. $platform_type= isset($this->post['platform_type'])&&$this->post['platform_type']!=="" ? intval($this->post['platform_type']):"";
  1447. if($platform_type!==""){
  1448. $condition[]=["a.platform_type","=",$platform_type];
  1449. }
  1450. // $inv_in = isset($this->post['inv_in'])&&$this->post['inv_in']!=="" ? trim($this->post['inv_in']):"";
  1451. // if($inv_in!==""){
  1452. // $condition[]=["a.inv_in","=",$inv_in];
  1453. // }
  1454. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  1455. if($invNo!==""){
  1456. $condition[]=["a.invNo","like","%$invNo%"];
  1457. }
  1458. $relaComNo= isset($this->post['relaComNo'])&&$this->post['relaComNo']!="" ? trim($this->post['relaComNo']) :"";
  1459. if($relaComNo!=""){
  1460. $condition[]=["a.inv_out","=",$relaComNo];
  1461. }
  1462. $inv_number = isset($this->post['inv_number'])&&$this->post['inv_number']!=="" ? trim($this->post['inv_number']):"";
  1463. if($inv_number!==""){
  1464. $condition[]=["c.inv_number","like","%$inv_number%"];
  1465. }
  1466. $inv_code = isset($this->post['inv_code'])&&$this->post['inv_code']!=="" ? trim($this->post['inv_code']):"";
  1467. if($inv_code!==""){
  1468. $condition[]=["c.inv_code","like","%$inv_code%"];
  1469. }
  1470. $inv_company = isset($this->post['inv_company'])&&$this->post['inv_company']!=="" ? trim($this->post['inv_company']):"";
  1471. if($inv_company!==""){
  1472. $condition[]=["a.inv_company","like","%$inv_company%"];
  1473. }
  1474. $buyer_name = isset($this->post['buyer_name'])&&$this->post['buyer_name']!=="" ? trim($this->post['buyer_name']):"";
  1475. if($buyer_name!==""){
  1476. $condition[]=["b.buyer_title","like","%$buyer_name%"];
  1477. }
  1478. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  1479. if($apply_id!==""){
  1480. $condition[]=["a.apply_id","=",$apply_id];
  1481. }
  1482. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  1483. if($apply_name!==""){
  1484. $condition[]=["a.apply_name","like","%$apply_name%"];
  1485. }
  1486. $count=Db::name("invoice_pool")->alias("a")
  1487. ->rightJoin("invoice_pool_info b","a.invNo=b.invNo")
  1488. ->leftJoin("invoice_ticket c","a.invNo=c.invNo and c.type=0 and c.is_del=0")
  1489. ->where($condition)->count();
  1490. $total =ceil($count/$size);
  1491. $page= $page>$total ? intval($total):$page;
  1492. $list=Db::name("invoice_pool")->alias("a")
  1493. ->rightJoin("invoice_pool_info b","a.invNo=b.invNo")
  1494. ->leftJoin("invoice_ticket c","a.invNo=c.invNo and c.type=0 and c.is_del=0")
  1495. ->where($condition)->field("a.id,
  1496. a.invNo,
  1497. a.platform_type,
  1498. a.open_type,
  1499. a.is_ticket,
  1500. a.tag_id,
  1501. a.apply_id,
  1502. a.apply_name,
  1503. a.status,
  1504. a.inv_value,
  1505. a.inv_type,
  1506. a.buyer_check,
  1507. a.seller_check,
  1508. a.exam_remark,
  1509. a.check_remark,
  1510. a.inv_company,
  1511. a.inv_out,
  1512. b.buyer_title,
  1513. b.seller_title,
  1514. a.addtime,
  1515. c.inv_code,
  1516. c.inv_number,
  1517. c.open_date,
  1518. c.type")
  1519. ->page($page,$size)->order("a.addtime desc")->select()->toArray();
  1520. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  1521. }
  1522. //获取发票详情
  1523. public function info(){
  1524. $invNo = isset($this->post['invNo'])&& $this->post['invNo']!='' ? trim($this->post['invNo']):"";
  1525. if($invNo==''){
  1526. return error_show(1004,"参数 invNo 不能为空");
  1527. }
  1528. $poolinfo =Db::name("invoice_pool")->where([["invNo","=",$invNo],["is_del","=",0]])->findOrEmpty();
  1529. if(empty($poolinfo)){
  1530. return error_show(1004,"为找到开票信息");
  1531. }
  1532. $pool = Db::name("invoice_pool_info")->where(["invNo"=>$invNo])->find();
  1533. $poolinfo['buyer_title'] = $pool['buyer_title']??'';
  1534. $poolinfo['buyer_code'] = $pool['buyer_code']??'';
  1535. $poolinfo['buyer_addr'] = $pool['buyer_addr']??'';
  1536. $poolinfo['buyer_mobile'] = $pool['buyer_mobile']??'';
  1537. $poolinfo['buyer_bank'] = $pool['buyer_bank']??'';
  1538. $poolinfo['buyer_bankNo'] = $pool['buyer_bankNo']??'';
  1539. $poolinfo['seller_title'] = $pool['seller_title']??'';
  1540. $poolinfo['seller_addr'] = $pool['seller_addr']??'';
  1541. $poolinfo['seller_code'] = $pool['seller_code']??'';
  1542. $poolinfo['seller_mobile'] = $pool['seller_mobile']??'';
  1543. $poolinfo['seller_bank'] = $pool['seller_bank']??'';
  1544. $poolinfo['seller_bankNo'] = $pool['seller_bankNo']??'';
  1545. $poolinfo['voider'] = $pool['voider']??'';
  1546. $poolinfo['payee'] = $pool['payee']??'';
  1547. $poolinfo['drawer'] = $pool['drawer']??'';
  1548. $poolinfo['reviewer'] = $pool['reviewer']??'';
  1549. $poolinfo['ownerPlace'] = $pool['ownerPlace']??'';
  1550. $orderList =Db::name("invoice_good")->where(["invNo"=>$invNo,"is_del"=>0])->column("*","orderCode");
  1551. $poolinfo['goodList']=$orderList;
  1552. $poolinfo['invinfo']=Db::name("invoice_ticket")->withoutField("pdfstream")->where(["invNo"=>$invNo,"type"=>0,"is_del"=>0])
  1553. ->findOrEmpty();
  1554. $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
  1555. ->where(["a.viceCode"=>$invNo,"a.is_del"=>0,"a.status"=>[1,2]])
  1556. ->order("a.addtime desc")
  1557. ->field("c.*,a.cancel_fee")
  1558. ->select()->toArray();
  1559. foreach ($orderinfo as &$item){
  1560. $item['ainv_num'] = Db::name("invoice_pool")
  1561. ->alias("a")
  1562. ->leftJoin("invoice_good b", "a.invNo=b.invNo and b.is_del=0")
  1563. ->where(["a.status" => [0, 1, 2, 3], "b.orderCode" => $item['sequenceNo']])
  1564. ->where('a.invNo', '<>', $invNo)
  1565. ->sum("b.goodNum");
  1566. $item['winv_num'] = $item['goodNum'] - $item['ainv_num'];
  1567. $item['inv_num'] = $orderList[$item['sequenceNo']]['goodNum'];
  1568. }
  1569. $poolinfo['orderinfo']=$orderinfo;
  1570. return app_show(0,"获取成功",$poolinfo);
  1571. }
  1572. //批量申请红冲发票
  1573. public function TicketEdit(){
  1574. $post=$this->request->only([
  1575. "invNo"=>"", //开票申请编号
  1576. "inv_code"=>"",//发票编码
  1577. "inv_number"=>"",//发票号码
  1578. "inv_subtotal"=>"",//发票税前额度
  1579. "inv_total"=>"",//发票额度
  1580. "open_date"=>"",//开票日期
  1581. "check_code"=>"",//发票校验码
  1582. ],"post","trim");
  1583. $valid=Validate::rule([
  1584. "invNo|开票申请编号"=>"require|max:255",
  1585. "inv_code|开票代码"=>"number",
  1586. "inv_number|开票编号"=>"number",
  1587. "inv_subtotal|不含税金额"=>"float",
  1588. "inv_total|价税合计"=>"float",
  1589. "open_date|开票日期"=>"date",
  1590. "check_code|校验码"=>"max:255",
  1591. ]);
  1592. if($valid->check($post)==false) return error_show(1004,$valid->getError());
  1593. $ticketinfo =Db::name("invoice_ticket")->where(["invNo"=>$post['invNo'],"is_del"=>0,"type"=>0])->findOrEmpty();
  1594. if(empty($ticketinfo)) return error_show(1004,"未找到开票数据");
  1595. $pool = Db::name("invoice_pool")->where(["invNo"=>$post['invNo'],"is_del"=>0,"status"=>[5,9]])->findOrEmpty();
  1596. if(empty($pool)) return error_show(1004,"开票申请数据不可修改");
  1597. if($pool['open_type']==1)return error_show(1004,"金税开票数据不可修改");
  1598. if($post['inv_number']==='') return error_show(1004,"参数 inv_number不能为空");
  1599. if($post['open_date']==='') return error_show(1004,"参数 open_date 不能为空");
  1600. if(in_array($pool['inv_type'],['normal','roll',"toll",'electronic'])){
  1601. if($post['check_code']==='') return error_show(1004,"参数 check_code不能为空");
  1602. }
  1603. //全电子发票
  1604. if(in_array($pool['inv_type'],['fully_digitalized_special_electronic','fully_digitalized_normal_electronic'])){
  1605. //全电发票传含税金额
  1606. if($post['inv_total']=='') return error_show(1004,"参数 inv_total 不能为空");
  1607. }else{
  1608. //全电发票无invCode
  1609. if($post['inv_code']==='') return error_show(1004,"参数 inv_code不能为空");
  1610. if($post['inv_subtotal']==='') return error_show(1004,"参数 inv_subtotal 不能为空");
  1611. }
  1612. Db::startTrans();
  1613. try{
  1614. $Ticket=[
  1615. "inv_code"=>$post['inv_code'],//发票编码
  1616. "inv_number"=>$post['inv_number'],//发票号码
  1617. "inv_subtotal"=>$post['inv_subtotal'],//发票税前额度
  1618. "inv_total"=>$post['inv_total'],//发票额度
  1619. "open_date"=>$post['open_date'],//开票日期
  1620. "check_code"=>$post['check_code'],//发票校验码
  1621. "status"=>0,//带校验
  1622. "updatetime"=>date("Y-m-d H:i:s")//发票校验码
  1623. ];
  1624. $ticket =Db::name("invoice_ticket")->where($ticketinfo)->update($Ticket);
  1625. if($ticket==false) throw new \Exception("开票信息更新失败");
  1626. $polup =Db::name("invoice_pool")->where($pool)->update(["status"=>3,"check_remark"=>"","updatetime"=>date("Y-m-d H:i:s")]);
  1627. if($polup==false) throw new \Exception("开票申请信息更新失败");
  1628. Db::commit();
  1629. return app_show(0,"开票信息更新成功");
  1630. }catch (Exception $e){
  1631. Db::rollback();
  1632. return error_show(1005,$e->getMessage());
  1633. }
  1634. }
  1635. //红票申请开票
  1636. public function returnList(){
  1637. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  1638. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  1639. $condition=[["b.is_del","=",0]];
  1640. $roleid = $this->roleid;
  1641. $check = checkRole($roleid,'89');
  1642. if($check){
  1643. $condition[]=["a.apply_id","=",$this->uid];
  1644. }
  1645. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  1646. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  1647. $companyNo = isset($this->post['companyNo'])&&$this->post['companyNo']!="" ? trim($this->post['companyNo']) :"";
  1648. if($companyNo!=""){
  1649. $condition[]=["b.inv_out","=",$companyNo];
  1650. }
  1651. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  1652. if($relaComNo!=""){
  1653. $condition[]=["b.inv_out","=",$relaComNo];
  1654. }
  1655. if($start!=""){
  1656. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  1657. }
  1658. if($end!=""){
  1659. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  1660. }
  1661. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? trim($this->post['inv_type']):"";
  1662. if($inv_type!==""){
  1663. $condition[]=["b.inv_type","=",$inv_type];
  1664. }
  1665. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  1666. if($apply_id!==""){
  1667. $condition[]=["a.apply_id","=",$apply_id];
  1668. }
  1669. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  1670. if($apply_name!==""){
  1671. $condition[]=["a.apply_name","like","%$apply_name%"];
  1672. }
  1673. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  1674. if($status!==""){
  1675. $condition[]=["a.status","=",$status];
  1676. }
  1677. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  1678. if($invNo!==""){
  1679. $condition[]=["a.invNo","like","%$invNo%"];
  1680. }
  1681. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!=="" ? trim($this->post['returnCode']):"";
  1682. if($returnCode!==""){
  1683. $condition[]=["a.returnCode","like","%$returnCode%"];
  1684. }
  1685. $count=Db::name("invoice_return")->alias("a")
  1686. ->leftJoin("invoice_pool b","a.invNo=b.invNo")
  1687. ->leftJoin("invoice_pool_info c","a.invNo=c.invNo")
  1688. ->where($condition)->count();
  1689. $total =ceil($count/$size);
  1690. $page= $page>$total ? intval($total):$page;
  1691. $list=Db::name("invoice_return")->alias("a")
  1692. ->leftJoin("invoice_pool b","a.invNo=b.invNo")
  1693. ->leftJoin("invoice_pool_info c","a.invNo=c.invNo")
  1694. ->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();
  1695. foreach ($list as &$value){
  1696. $company =Db::name("company_info")->where(["companyNo"=>$value['inv_out']])->find();
  1697. $value['companyName'] = $company['company_name']??"";
  1698. if(!in_array($value['status'],[2,3,4,5])){
  1699. $opendate =Db::name("invoice_ticket")->where(["invNo"=>$value['invNo'],"is_del"=>0,"type"=>0])->value("open_date","");
  1700. if($opendate!=''){
  1701. $open_month =date("Y-m",strtotime($opendate));
  1702. $value['is_out_month']= $open_month==date("Y-m")?0:1;
  1703. }
  1704. }
  1705. }
  1706. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  1707. }
  1708. //红冲申请
  1709. public function applyRed(){
  1710. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  1711. if($invNo=='')return error_show(1004,"参数 invNo 不能为空");
  1712. $invfo =Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  1713. if($invfo==false) return error_show(1004,"发票数据未找到");
  1714. if(!in_array($invfo['status'],[1,2,3,4,5,8]))return error_show(1004,"发票数据状态无法申请");
  1715. $ticket =Db::name("invoice_ticket")->where(["invNo"=>$invNo])->findOrEmpty();
  1716. if(empty($ticket)) return error_show(1004,"未找到开票信息");
  1717. $company =Db::name("company_info")->where(["companyNo"=>'GS2207150956046617'])->findOrEmpty();
  1718. if(empty($company)) return error_show(1004,"业务企业信息未找到");
  1719. $invoiceConf=Config::get('invoice');
  1720. if(!isset($invoiceConf[$company['company_license']]))throw new \Exception('金税配置不存在', 1006);
  1721. $invoice = $invoiceConf[$company['company_license']];
  1722. $Tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  1723. $redticketList=[];
  1724. $redticketArry=[
  1725. "SQBSCQQPCH"=>randomkeys(26).date("YmdHis"),
  1726. "NSRSBH"=>$company['company_license'],
  1727. "FPLXDM"=>$this->kingInvoice[$invfo['inv_type']],
  1728. ];
  1729. $goodinfo =Db::name("invoice_good")
  1730. ->where(["invNo"=>$invNo,"is_del"=>0])
  1731. ->field("0 as FPHXZ,
  1732. catNo as SPBM,
  1733. id as ZXBM,
  1734. is_discount as YHZCBS,
  1735. if(inv_tag=0,'',inv_tag) as LSLBS,
  1736. addTax as ZZSTSGL,
  1737. goodName as XMMC,
  1738. '' as DW,
  1739. '' as SPSL,
  1740. '' as DJ,
  1741. (-totalPrice) as JE,
  1742. 0 HSBZ,
  1743. tax as SL,
  1744. -taxPrice as SE")->select()->toArray();
  1745. if(empty($goodinfo)){
  1746. Db::rollback();
  1747. return error_show(1004,"开票商品信息未找到");
  1748. }
  1749. if(count($goodinfo)>8){
  1750. $len =array_unique(array_column($goodinfo,'SL'));
  1751. $qdmx=["FPHXZ"=>6,
  1752. "SPBM"=>'',
  1753. "XMMC"=>"详见对应正数发票及清单",
  1754. "YHZCBS"=>0,
  1755. "LSLBS"=>'',
  1756. "ZZSTSGL"=>'',
  1757. "DW"=>'',
  1758. "SPSL"=>'',
  1759. "DJ"=>'',
  1760. "JE"=>sprintf("%01.2f", (array_sum(array_column($goodinfo,'JE')))),
  1761. "HSBZ"=>'0',
  1762. "SL"=>count($len)>1?'':$len[0],
  1763. "SE"=>sprintf("%01.2f",array_sum(array_column($goodinfo,'SE'))),
  1764. ];
  1765. }else
  1766. {
  1767. $qdmx=$goodinfo;
  1768. }
  1769. $info=[
  1770. "SQBSCQQLSH"=>randomkeys(26).date("YmdHis"),
  1771. "YYSBZ"=> "0000000000", //营业税标志
  1772. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1773. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1774. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1775. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1776. "TKSJ"=>date("YmdHis"), //填开时间
  1777. "XHFSBH"=>$ticket['seller_id'],
  1778. "XHFMC"=>$ticket['seller_title'],
  1779. "GMFSBH"=>$ticket['buyer_id'],
  1780. "GMFMC"=> $ticket['buyer_title'],
  1781. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1782. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1783. "SQSM"=>"1100000000", //申请说明
  1784. "XXBTSBS"=>"0",//0: 正常;1:成品油发票,涉及销售数量 变更(销货退回、开票有误等原因);2: 成品油发票,仅涉及销售金额变更(销 售折让);3:机动车发票,仅涉及销售数
  1785. //量变更(销货退回、开票有误等原因);4: 机动车发票,仅涉及销售金额变更(销 售折让)
  1786. ];
  1787. $invRed=makeNo("IRE");
  1788. $data=[
  1789. 'invNo'=>$invNo,
  1790. "invRed"=>$invRed,
  1791. "inv_out"=>$invfo['inv_out']??"",
  1792. "apply_id"=>$this->uid,
  1793. "apply_name"=>$this->uname,
  1794. "status"=>0,
  1795. "remark"=>'',
  1796. "addtime"=>date("Y-m-d H:i:s"),
  1797. "updatetime"=>date("Y-m-d H:i:s"),
  1798. "SQBSCQQLSH"=>$info['SQBSCQQLSH'],
  1799. "YYSBZ"=> "0000000000", //营业税标志
  1800. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1801. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1802. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1803. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1804. "TKSJ"=>date("YmdHis"), //填开时间
  1805. "XHFSBH"=>$ticket['seller_id'],
  1806. "XHFMC"=>$ticket['seller_title'],
  1807. "GMFSBH"=>$ticket['buyer_id'],
  1808. "GMFMC"=> $ticket['buyer_title'],
  1809. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1810. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1811. "SQSM"=>"1100000000", //申请说明
  1812. "XXBTSBS"=>"0",
  1813. "DDMXXX"=>json_encode($qdmx,JSON_UNESCAPED_UNICODE),
  1814. "SQBSCQQPCH"=>$redticketArry['SQBSCQQPCH'],
  1815. "FPLXDM"=>$this->kingInvoice[$invfo['inv_type']],
  1816. ];
  1817. $redticketList[]=["HZSQDTXX"=>$info,"DDMXXX"=>$qdmx];
  1818. $result=$Tax->AllocateRedInvoiceApplication($redticketArry,$redticketList);
  1819. $data['status']=5;
  1820. $data['remark']='红冲申请提交失败';
  1821. if(isset($result['ZTDM']) && $result['ZTDM']=='060000'){
  1822. $resultChild=$result['HZSQDSCJG'][0];
  1823. if($resultChild['ZTDM']=='060000'){
  1824. $data['status']=2;
  1825. $data['XXBBH']=$resultChild['XXBBH'];
  1826. $data['remark']='红冲申请审核通过';
  1827. }
  1828. if($resultChild['ZTDM']=='060001'){
  1829. $data['status']=1;
  1830. $data['XXBBH']=$resultChild['XXBBH'];
  1831. $data['remark']='红冲申请审核中';
  1832. }
  1833. }
  1834. if($data['status']==5){
  1835. return error_show(1004,"红冲发票申请新建失败");
  1836. }
  1837. $up =Db::name("invoice_red")->insert($data);
  1838. return $up ? app_show(0,"红冲发票申请新建成功",["invRed"=>$invRed]):error_show(1004,"红冲发票申请新建失败");
  1839. }
  1840. //批量申请红冲发票
  1841. public function batchApplyRed(){
  1842. $invArr = isset($this->post['invArr'])&&!empty($this->post['invArr'])?$this->post['invArr']:[];
  1843. if(empty($invArr))return error_show(1004,"参数 invArr 不能为空");
  1844. $invfo =Db::name("invoice_pool")->where(["invNo"=>$invArr,"is_del"=>0])->select()->toArray();
  1845. if(empty($invfo)) return error_show(1004,"发票数据未找到");
  1846. $insertAll=[];
  1847. $companyCode = array_unique(array_column($invfo,"inv_out"));
  1848. $inv_type = array_unique(array_column($invfo,"inv_type"));
  1849. if(count($companyCode)>1){
  1850. return error_show(1004,"业务企业公司不统一");
  1851. }
  1852. if(count($inv_type)>1){
  1853. return error_show(1004,"发票类型不统一");
  1854. }
  1855. $company =Db::name("company_info")->where(["companyNo"=>$companyCode])->findOrEmpty();
  1856. if(empty($company)) {
  1857. return error_show(1004,"业务企业信息未找到");
  1858. }
  1859. $ticketArr=Db::name("invoice_ticket")->where(["invNo"=>$invArr,"is_del"=>0])->column("*","invNo");
  1860. if(empty($ticketArr)){
  1861. return error_show(1004,"未找到开票信息");
  1862. }
  1863. $invoiceConf=Config::get('invoice');
  1864. if(!isset($invoiceConf[$company['company_license']]))throw new \Exception('金税配置不存在', 1006);
  1865. $invoice = $invoiceConf[$company['company_license']];
  1866. $Tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  1867. Db::startTrans();
  1868. try{
  1869. $redticketArry=[
  1870. "SQBSCQQPCH"=>randomkeys(26).date("YmdHis"),
  1871. "NSRSBH"=>$company['company_license'],
  1872. "FPLXDM"=>$this->kingInvoice[$inv_type[0]],
  1873. ];
  1874. $redticketList=[];
  1875. foreach ($invfo as $value){
  1876. if(!in_array($value['status'],[1,2,3,4,5,8])){
  1877. Db::rollback();
  1878. return error_show(1004,"发票数据状态无法申请");
  1879. }
  1880. $ticket =$ticketArr[$value['invNo']] ??[];
  1881. if(empty($ticket)){
  1882. Db::rollback();
  1883. return error_show(1004,"未找到开票信息");
  1884. }
  1885. $goodinfo =Db::name("invoice_good")
  1886. ->where(["invNo"=>$value['invNo'],"is_del"=>0])
  1887. ->field(" 6 as FPHXZ,
  1888. catNo as SPBM,
  1889. id as ZXBM,
  1890. is_discount as YHZCBS,
  1891. if(inv_tag=0,'',inv_tag) as LSLBS,
  1892. addTax as ZZSTSGL,
  1893. goodName as XMMC,
  1894. '' as DW,
  1895. '' as SPSL,
  1896. '' as DJ,
  1897. (-totalPrice) as JE,
  1898. 0 HSBZ,
  1899. tax as SL,
  1900. -taxPrice as SE")
  1901. ->select()->toArray();
  1902. if(empty($goodinfo)){
  1903. Db::rollback();
  1904. return error_show(1004,"开票商品信息未找到");
  1905. }
  1906. $len =array_unique(array_column($goodinfo,'SL'));
  1907. $qdmx=["FPHXZ"=>6,
  1908. "SPBM"=>'',
  1909. "XMMC"=>"详见对应正数发票及清单",
  1910. "YHZCBS"=>0,
  1911. "LSLBS"=>'',
  1912. "ZZSTSGL"=>'',
  1913. "DW"=>'',
  1914. "SPSL"=>'',
  1915. "DJ"=>'',
  1916. "JE"=>sprintf("%01.2f", (array_sum(array_column($goodinfo,'JE')))),
  1917. "HSBZ"=>'0',
  1918. "SL"=>count($len)>1?'':$len[0],
  1919. "SE"=>sprintf("%01.2f",array_sum(array_column($goodinfo,'SE'))),
  1920. ];
  1921. $info=[
  1922. "SQBSCQQLSH"=>randomkeys(26).date("YmdHis"),
  1923. "YYSBZ"=> "0000000000", //营业税标志
  1924. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1925. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1926. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1927. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1928. "TKSJ"=>date("YmdHis"), //填开时间
  1929. "XHFSBH"=>$ticket['seller_id'],
  1930. "XHFMC"=>$ticket['seller_title'],
  1931. "GMFSBH"=>$ticket['buyer_id'],
  1932. "GMFMC"=> $ticket['buyer_title'],
  1933. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1934. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1935. "SQSM"=>"1100000000", //申请说明
  1936. "XXBTSBS"=>"0",//0: 正常;1:成品油发票,涉及销售数量 变更(销货退回、开票有误等原因);2: 成品油发票,仅涉及销售金额变更(销 售折让);3:机动车发票,仅涉及销售数
  1937. //量变更(销货退回、开票有误等原因);4: 机动车发票,仅涉及销售金额变更(销 售折让)
  1938. ];
  1939. $redticketList[]=["HZSQDTXX"=>$info,"DDMXXX"=>$qdmx];
  1940. $data=[
  1941. 'invNo'=>$value['invNo'],
  1942. "invRed"=>makeNo("IRE"),
  1943. "inv_out"=>$value['inv_out']??"",
  1944. "apply_id"=>$this->uid,
  1945. "apply_name"=>$this->uname,
  1946. "status"=>5,
  1947. "remark"=>'红冲申请提交失败',
  1948. "addtime"=>date("Y-m-d H:i:s"),
  1949. "updatetime"=>date("Y-m-d H:i:s"),
  1950. "SQBSCQQLSH"=>$info['SQBSCQQLSH'],
  1951. "YYSBZ"=> "0000000000", //营业税标志
  1952. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1953. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1954. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1955. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1956. "TKSJ"=>date("YmdHis"), //填开时间
  1957. "XHFSBH"=>$ticket['seller_id'],
  1958. "XHFMC"=>$ticket['seller_title'],
  1959. "GMFSBH"=>$ticket['buyer_id'],
  1960. "GMFMC"=> $ticket['buyer_title'],
  1961. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1962. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1963. "SQSM"=>"1100000000", //申请说明
  1964. "XXBTSBS"=>"0",
  1965. "DDMXXX"=>json_encode($qdmx,JSON_UNESCAPED_UNICODE),
  1966. "SQBSCQQPCH"=>$redticketArry['SQBSCQQPCH'],
  1967. "FPLXDM"=>$this->kingInvoice[$value['inv_type']],
  1968. ];
  1969. $insertAll[$info['SQBSCQQLSH']]=$data;
  1970. }
  1971. $result=$Tax->AllocateRedInvoiceApplication($redticketArry,$redticketList);
  1972. if(isset($result['ZTDM']) && $result['ZTDM']=='060000'){
  1973. if(isset($result['HZSQDSCJG']) && count($result['HZSQDSCJG'])>0){
  1974. foreach ($result['HZSQDSCJG'] as $value){
  1975. if($value['ZTDM']=='060000'){
  1976. $insertAll[$value['SQBSCQQLSH']]['status']=2;
  1977. $insertAll[$value['SQBSCQQLSH']]['remark']="红冲申请审核通过";
  1978. $insertAll[$value['SQBSCQQLSH']]['XXBBH']=$value['XXBBH'];
  1979. continue;
  1980. // $data['status']=2;
  1981. // $data['remark']='红冲申请审核通过';
  1982. }
  1983. if($value['ZTDM']=='060001'){
  1984. $insertAll[$value['SQBSCQQLSH']]['status']=1;
  1985. $insertAll[$value['SQBSCQQLSH']]['remark']="红冲申请审核中";
  1986. $insertAll[$value['SQBSCQQLSH']]['XXBBH']=$value['XXBBH'];
  1987. continue;
  1988. }
  1989. unset($insertAll[$value['SQBSCQQLSH']]);
  1990. }
  1991. }
  1992. }else{
  1993. Db::rollback();
  1994. return error_show(1004,"红冲发票申请失败");
  1995. }
  1996. $uns= Db::name("invoice_red")->insertAll($insertAll);
  1997. if($uns){
  1998. Db::commit();
  1999. return app_show(0,"红冲发票申请新建成功,成功数量{$uns}个");
  2000. }
  2001. Db::rollback();
  2002. return error_show(1004,"红冲发票申请新建失败");
  2003. }catch (\Exception $e){
  2004. Db::rollback();
  2005. return error_show(1004,$e->getMessage());
  2006. }
  2007. }
  2008. //红票申请开票
  2009. public function redOpen(){
  2010. $invRed= isset($this->post['invRed'])&&$this->post['invRed']!=''? $this->post['invRed']:"";
  2011. if($invRed==''){
  2012. return error_show(1004,"参数 invRed 不能为空");
  2013. }
  2014. $redinfo =Db::name("invoice_red")->where(["invRed"=>$invRed,"is_del"=>0])->findOrEmpty();
  2015. if(empty($redinfo)){
  2016. return error_show(1004,"红冲发票申请不存在");
  2017. }
  2018. $redreason = isset($this->post['redreason'])&&$this->post['redreason']!=''? $this->post['redreason']:"";
  2019. if($redreason==''){
  2020. return error_show(1004,"参数 redreason 不能为空");
  2021. }
  2022. if($redinfo['status']!=2){
  2023. return error_show(1004,"数据状态不可提交申请");
  2024. }
  2025. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$redinfo['invNo'],"is_del"=>0])->findOrEmpty();
  2026. if(empty($invinfo)) return error_show(1004,"红冲发票原票信息未找到");
  2027. $invpoolinfo = Db::name("invoice_ticket")->where(["invNo"=>$redinfo['invNo'],"status"=>1,"is_del"=>0])->findOrEmpty();
  2028. if(empty($invpoolinfo)) return error_show(1004,"红冲发票开票信息未找到");
  2029. Db::startTrans();
  2030. try{
  2031. $data=[
  2032. "invNo"=>$invRed,
  2033. "KPLX"=>"1",
  2034. "XHFSBH"=>$invpoolinfo['seller_id'],
  2035. "XHFMC"=>$invpoolinfo['seller_title'],
  2036. "XHFDZ"=>$invpoolinfo['seller_addr'],
  2037. "XHFDH"=>$invpoolinfo['seller_mobile'],
  2038. "XHFYH"=>$invpoolinfo['seller_bank'],
  2039. "XHFZH"=>$invpoolinfo['seller_bankNo'],
  2040. "GMFBM"=>"",
  2041. "GMFSBH"=>$invpoolinfo['buyer_id'],
  2042. "GMFMC"=>$invpoolinfo['buyer_title'],
  2043. "GMFDZ"=>$invpoolinfo['buyer_addr'],
  2044. "GMFDH"=>$invpoolinfo['buyer_mobile'],
  2045. "GMFYH"=>$invpoolinfo['buyer_bank'],
  2046. "GMFZH"=>$invpoolinfo['buyer_bankNo'],
  2047. "GMFLX"=>$invinfo['company_type'],
  2048. "GMFSJH"=>"",
  2049. "GMFDZYX"=>$invinfo['email'],
  2050. "KPR"=>$invpoolinfo['issuer'],
  2051. "SKR"=>$invpoolinfo['reciver'],
  2052. "FHR"=>$invpoolinfo['reviewer'],
  2053. "QDBZ"=>"0",
  2054. "YFPDM"=>$redinfo['YFPDM'],
  2055. "YFPHM"=>$redinfo['YFPHM'],
  2056. "YFPLX"=>$invpoolinfo['inv_type'] == '004' || $invpoolinfo['inv_type'] =='028'||$redreason=='' ?'':$invpoolinfo['inv_type'],
  2057. "YFPKPRQ"=>$invpoolinfo['inv_type'] == '004' || $invpoolinfo['inv_type'] == '028'||$redreason=='' ? '':date("Y-m-d",strtotime($redinfo['YFPKPRQ'])),
  2058. "CHYY"=>$invpoolinfo['inv_type'] == '004' || $invpoolinfo['inv_type'] == '028' ? '':$redreason,
  2059. "TSCHBZ"=>"0",
  2060. "JSHJ"=>"-".$invpoolinfo['inv_total'],
  2061. "HJJE"=>"-".$invpoolinfo['inv_subtotal'],
  2062. "HJSE"=>"-".$invpoolinfo['tax_fee'],
  2063. "BZ"=> $invpoolinfo['inv_type'] == '004' || $invpoolinfo['inv_type'] =='028'?"开具红字增值税专用发票信息表编号{$redinfo['XXBBH']}":"对应正数发票代码:{$redinfo['YFPDM']} 号码:{$redinfo['YFPHM']}",
  2064. "DDMXXX"=>$redinfo['DDMXXX'],
  2065. "FPLXDM"=>$redinfo['FPLXDM'],
  2066. "DDQQPCH"=>"",
  2067. "DDQQLSH"=>randomkeys(26).date("YmdHis"),
  2068. "status"=>"0",
  2069. "result"=>"",
  2070. "addtime"=>date("Y-m-d H:i:s"),
  2071. "updatetime"=>date("Y-m-d H:i:s")
  2072. ];
  2073. $invoice=Db::name("invoice_interface")->insert($data);
  2074. if($invoice==false){
  2075. Db::rollback();
  2076. return error_show(1003,"红冲开票信息生成失败");
  2077. }
  2078. $upred=Db::name("invoice_red")->where(["invRed"=>$invRed])->update(["status"=>3,"red_reason"=>$redreason,"updatetime"=>date("Y-m-d H:i:s")]);
  2079. if($upred==false){
  2080. Db::rollback();
  2081. return error_show(1003,"红冲发票信息更新失败");
  2082. }
  2083. Db::commit();
  2084. return app_show(0,"红冲发票信息开票提交成功");
  2085. }catch (\Exception $e){
  2086. Db::rollback();
  2087. return error_show(1003,$e->getMessage());
  2088. }
  2089. }
  2090. //红冲票申请列表
  2091. public function redList(){
  2092. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  2093. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  2094. $condition=[["is_del","=",0],["FPLXDM","in",["004",'028']]];
  2095. $roleid = $this->roleid;
  2096. $check = checkRole($roleid,'110');
  2097. if($check){
  2098. $condition[]=["apply_id","=",$this->uid];
  2099. }
  2100. $invRed=isset($this->post["invRed"])&&$this->post["invRed"]!=''?trim($this->post["invRed"]):"";
  2101. if($invRed!=''){
  2102. $condition[]=["invRed","like","%$invRed%"];
  2103. }
  2104. $apply_id=isset($this->post["apply_id"])&&$this->post["apply_id"]!=''?intval($this->post["apply_id"]):"";
  2105. if($apply_id!=''){
  2106. $condition[]=["apply_id","=",$apply_id];
  2107. }
  2108. $invNo=isset($this->post["invNo"])&&$this->post["invNo"]!=''?trim($this->post["invNo"]):"";
  2109. if($invNo!=''){
  2110. $condition[]=["invNo","like","%$invNo%"];
  2111. }
  2112. $companyNo=isset($this->post["companyNo"])&&$this->post["companyNo"]!=''?trim($this->post["companyNo"]):"";
  2113. if($companyNo!=''){
  2114. $condition[]=["inv_out","=",$companyNo];
  2115. }
  2116. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  2117. if($relaComNo!=""){
  2118. $condition[]=["inv_out","=",$relaComNo];
  2119. }
  2120. $apply_name=isset($this->post["apply_name"])&&$this->post["apply_name"]!=''?trim($this->post["apply_name"]):"";
  2121. if($apply_name!=''){
  2122. $condition[]=["apply_name","like","%$apply_name%"];
  2123. }
  2124. $start = isset($this->post["start"])&&$this->post["start"]!=''?trim($this->post["start"]):"";
  2125. $end = isset($this->post["end"])&&$this->post["end"]!=''?trim($this->post["end"]):"";
  2126. if($start!=''){
  2127. $condition[]=["addtime",">=",$start];
  2128. }
  2129. if($end!=''){
  2130. $condition[]=["addtime","<=",$end];
  2131. }
  2132. $total_low=isset($this->post["total_low"])&&$this->post["total_low"]!=''?floatval($this->post["total_low"]):"";
  2133. $total_up=isset($this->post["total_up"])&&$this->post["total_up"]!=''?floatval($this->post["total_up"]):"";
  2134. if($total_low!=''){
  2135. $condition[]=["ABS(HJJE)",">=",$total_low];
  2136. }
  2137. if($total_up!=''){
  2138. $condition[]=["ABS(HJJE)","<=",$total_up];
  2139. }
  2140. $count= Db::name("invoice_red")->where($condition)->count();
  2141. $total =ceil($count/$size);
  2142. $page=$total>=$page?$page :intval($total);
  2143. $list =Db::name("invoice_red")->where($condition)->json(["DDMXXX"])->page($page,$size)->order("addtime desc")->select()
  2144. ->toArray();
  2145. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  2146. }
  2147. /**
  2148. * @return \think\response\Json|void
  2149. */
  2150. public function ticketInfo(){
  2151. $num =isset($this->post['number'])&&$this->post['number']!=''?trim($this->post['number']):"";
  2152. if($num==''){
  2153. return error_show(1004,'参数 number 不能为空');
  2154. }
  2155. $condition=[["status","in",[0,1,2]]];
  2156. $condition[]=["inv_number","=",$num];
  2157. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  2158. if($invNo!=='')$condition[]=["invNo","=",$invNo];
  2159. $info =Db::name("invoice_ticket")->json(['item'])->withoutField("pdfstream")->where($condition)->findOrEmpty();
  2160. if(!empty($info)&& isset($info['item'])){
  2161. array_walk($info['item'],function (&$value){
  2162. if(stripos($value["SL"],"%")==false)$value['SL'] = ($value['SL']*100)."%";
  2163. });
  2164. }
  2165. $info['invtype_name'] = $this->typename[$info['inv_type']]??"";
  2166. return empty($info)?error_show(1005,"未找到数据"):app_show(0,"获取成功",$info);
  2167. }
  2168. //退票列表
  2169. public function returnRedList(){
  2170. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  2171. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  2172. $condition=[];
  2173. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  2174. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  2175. if($start!=""){
  2176. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  2177. }
  2178. if($end!=""){
  2179. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  2180. }
  2181. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? trim($this->post['inv_type']):"";
  2182. if($inv_type!==""){
  2183. $condition[]=["c.inv_type","=",$inv_type];
  2184. }
  2185. $companyNo = isset($this->post['companyNo'])&&$this->post['companyNo']!=="" ? trim($this->post['companyNo']):"";
  2186. if($companyNo!==""){
  2187. $condition[]=["c.inv_out","=",$companyNo];
  2188. }
  2189. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  2190. if($relaComNo!=""){
  2191. $condition[]=["c.inv_out","=",$relaComNo];
  2192. }
  2193. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  2194. if($apply_id!==""){
  2195. $condition[]=["a.apply_id","=",$apply_id];
  2196. }
  2197. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  2198. if($apply_name!==""){
  2199. $condition[]=["a.apply_name","like","%$apply_name%"];
  2200. }
  2201. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  2202. if($status!==""){
  2203. $condition[]=["a.status","=",$status];
  2204. }
  2205. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  2206. if($invNo!==""){
  2207. $condition[]=["a.invNo","like","%$invNo%"];
  2208. }
  2209. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!=="" ? trim($this->post['returnCode']):"";
  2210. if($returnCode!==""){
  2211. $condition[]=["a.returnCode","like","%$returnCode%"];
  2212. }
  2213. $count=Db::name("invoice_return")->alias("a")
  2214. ->leftJoin("invoice_ticket b","a.invNo=b.invNo and b.type=1")
  2215. ->leftJoin("invoice_pool c","a.invNo=c.invNo")
  2216. ->where($condition)->count();
  2217. $total =ceil($count/$size);
  2218. $page= $page>$total ? intval($total):$page;
  2219. $list=Db::name("invoice_return")->alias("a")
  2220. ->leftJoin("invoice_ticket b","a.invNo=b.invNo and b.type=1")
  2221. ->leftJoin("invoice_pool c","a.invNo=c.invNo")
  2222. ->field("b.*,a.*")->withoutField("pdfstream")->json(["item"])->page($page,$size)->where($condition)->select()
  2223. ->toArray();
  2224. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  2225. }
  2226. /**
  2227. * @return \think\response\Json|void
  2228. * @throws \think\db\exception\DataNotFoundException
  2229. * @throws \think\db\exception\DbException
  2230. * @throws \think\db\exception\ModelNotFoundException
  2231. */
  2232. public function tikcetList(){
  2233. $post=$this->request->only(["invNo"=>"","companyNo"=>"","relaComNo"=>"","page"=>1,"size"=>15],"post","trim");
  2234. $where=[];
  2235. $roleid = $this->roleid;
  2236. $check = checkRole($roleid,'88');
  2237. if($check){
  2238. $where[]=["b.apply_id","=",$this->uid];
  2239. }
  2240. if($post["invNo"]!="") $where[]=["a.invNo","=",$post['invNo']];
  2241. if($post["companyNo"]!="") $where[]=["b.inv_out","=",$post['companyNo']];
  2242. if($post["relaComNo"]!="") $where[]=["b.inv_out","=",$post['relaComNo']];
  2243. $count=Db::name("invoice_ticket")->where($where)->count();
  2244. $total =ceil($count/$post['size']);
  2245. $page= $post['page']>$total ? intval($total):intval($post['page']);
  2246. $list = Db::name("invoice_ticket")
  2247. ->alias("a")
  2248. ->leftJoin("invoice_pool b","a.invNo=b.invNo")
  2249. ->where($where)
  2250. ->field("a.*")
  2251. ->json(["a.item"])
  2252. ->page($page,intval($post['size']))
  2253. ->order("a.id desc")
  2254. ->select()->toArray();
  2255. foreach ($list as &$value){
  2256. $value['invtype_name'] = $this->typename[$value['inv_type']]??"";
  2257. }
  2258. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  2259. }
  2260. /** @param array invNo 发票编号集合
  2261. * @return \think\response\Json|void
  2262. */
  2263. public function downorder(){
  2264. $post=$this->request->only(["invNo"=>[]],"post","trim");
  2265. $condition=[["open_type","in",[0,2,3]],["is_del","=",0],["status","=",1]];
  2266. if(!empty($post['invNo'])){
  2267. $condition[]=['invNo',"in",$post['invNo']];
  2268. }
  2269. $invice = Db::name("invoice_pool")->where($condition)->select()->toArray();
  2270. if (empty($invice)){
  2271. return error_show(1003,"未找到数据");
  2272. }
  2273. $data=[];
  2274. foreach ($invice as $value){
  2275. $temp=[];
  2276. $orderNo = Db::name("assoc")->where([["status","=",1],["is_del","=",0],['viceCode',"=",$value['invNo']]])
  2277. ->find();
  2278. if(!$orderNo){
  2279. continue;
  2280. }
  2281. $good = Db::name("qrd_info")->where([['sequenceNo',"=",$orderNo['orderCode']],["status","=",1]])
  2282. ->findOrEmpty();
  2283. if(empty($good)){
  2284. continue;
  2285. }
  2286. if(isset($value['inv_out'])&& $value['inv_out']!=''){
  2287. $invout = Db::name("company_info")->where([['companyNo',"=",$value['inv_out']]])->find();
  2288. }
  2289. $invinfo = Db::name("invoice_pool_info")->where("invNo","=",$value['invNo'])->findOrEmpty();
  2290. // foreach ($good as $val){
  2291. // $qrd = Db::name("qrd_list")->where("sequenceNo","=",$val['goodNo'])->find();
  2292. $temp['invNo'] = $value['invNo'];
  2293. $temp['qrdNo'] = $orderNo['orderCode'];
  2294. $temp['depart'] = $good['department'];
  2295. $temp['apply_name'] =$value['apply_name'];
  2296. $temp['code'] ="";
  2297. $temp['invoice_title'] =$invinfo['buyer_title']??"";
  2298. $temp['invoice_code'] =$invinfo['buyer_code']." ";
  2299. $temp['invoice_addr'] =$invinfo['buyer_addr']??"";
  2300. $temp['invoice_mobile'] =$invinfo['buyer_mobile']??"";
  2301. $temp['invoice_bank'] =$invinfo['buyer_bank']??"";
  2302. $temp['invoice_bankNo'] =$invinfo['buyer_bankNo']." ";
  2303. $temp['company'] =$good['customerName'];
  2304. $temp['goodname'] =$good['goodName'];
  2305. $temp['tax'] =$good['tax'];
  2306. $temp['inv_name'] =$good['goodName'];
  2307. $temp['diff_weight'] =$good['diff_weight'];
  2308. $temp['diff_price'] =$good['diff_fee'];
  2309. $temp['unit'] =$good['goodUnit'];
  2310. $temp['num'] =$good['goodNum'] - $good['thNum'];
  2311. $temp['price'] =$good['goodPrice'];
  2312. $temp['total_fee'] =$good['totalPrice'];
  2313. $temp['inv_type']=$this->invoiceType[$value['inv_type']]?? "-";
  2314. $temp['remark'] =$value['remark'];
  2315. $temp['kp_companyName'] = $invinfo['seller_title']??"";
  2316. $temp['kp_company_address'] = $invinfo['seller_addr']??"";
  2317. $temp['kp_company_license'] = $invinfo['seller_code']." ";
  2318. $temp['kp_bank_name'] = $invinfo['seller_bank']??"";
  2319. $temp['kp_bankNo'] = $invinfo['seller_bankNo']."";
  2320. $temp['kp_contector'] = $invout['contector']??"";
  2321. $temp['kp_mobile'] = $invinfo['seller_mobile']??"";
  2322. $data[]=$temp;
  2323. }
  2324. // }
  2325. if (empty($data)){
  2326. return error_show(1003,"未找到数据");
  2327. }
  2328. $header=[
  2329. "申请编号",
  2330. "公司订单号",
  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. excelExport("待开票",$header,$data);
  2361. }
  2362. public function importInv(){
  2363. $param =$this->request->only(["data"=>[]],"post");
  2364. if(empty($param['data']))return error_show(1004,"参数 data 不能为空");
  2365. $total=[];
  2366. $list=[];
  2367. foreach ($param['data'] as $key=>$value){
  2368. if($value['invNo']==""){
  2369. return error_show(1005,"第".($key+1)."行缺少发票申请编号数据");
  2370. }
  2371. if($value["inv_type"]===""){
  2372. return error_show(1005,"第".($key+1)."行缺少发票类型数据");
  2373. }
  2374. if(!in_array($value['inv_type'],$this->invName)) return error_show(1005,"第".($key+1)."行发票类型数据有误");
  2375. if($value["inv_code"]=="" && !in_array($value['inv_type'],[ "fully_digitalized_special_electronic", "fully_digitalized_normal_electronic"])){
  2376. return error_show(1005,"第".($key+1)."行缺少发票代码数据");
  2377. }
  2378. if($value["inv_number"]==""){
  2379. return error_show(1005,"第".($key+1)."行缺少发票号码数据");
  2380. }
  2381. if($value["inv_total"]==""&& in_array($value['inv_type'],["fully_digitalized_special_electronic", "fully_digitalized_normal_electronic"])){
  2382. return error_show(1005,"第".($key+1)."行缺少发票金额数据");
  2383. }
  2384. if($value["inv_subtotal"]==""){
  2385. return error_show(1005,"第".($key+1)."行缺少发票税后金额数据");
  2386. }
  2387. if($value["open_date"]==""){
  2388. return error_show(1005,"第".($key+1)."行缺少发票开票日期数据");
  2389. }
  2390. //电子票校验码必传
  2391. if($value["check_code"]=="" && in_array($value['inv_type'],['normal','roll',"toll",'electronic'])){
  2392. return error_show(1005,"第".($key+1)."行缺少发票校验码数据");
  2393. }
  2394. // if($value['inv_type']==="000"){
  2395. // if($value['seller_id']=="") return error_show(1005,"第".($key+1)."行缺少销售方纳税识别号数据");
  2396. // if($value['seller_title']=="") return error_show(1005,"第".($key+1)."行缺少销售方名称数据");
  2397. // if($value['seller_addr']=="") return error_show(1005,"第".($key+1)."行缺少销售方地址数据");
  2398. // if($value['seller_mobile']=="") return error_show(1005,"第".($key+1)."行缺少销售方联系方式数据");
  2399. // if($value['seller_bank']=="") return error_show(1005,"第".($key+1)."行缺少销售方银行数据");
  2400. // if($value['seller_bankNo']=="") return error_show(1005,"第".($key+1)."行缺少销售方银行账户数据");
  2401. // if($value['buyer_id']=="") return error_show(1005,"第".($key+1)."行缺少购买方纳税识别号数据");
  2402. // if($value['buyer_title']=="") return error_show(1005,"第".($key+1)."行缺少购买方名称数据");
  2403. // if($value['buyer_addr']=="") return error_show(1005,"第".($key+1)."行缺少购买方地址数据");
  2404. // if($value['buyer_mobile']=="") return error_show(1005,"第".($key+1)."行缺少购买方联系方式数据");
  2405. // if($value['buyer_bank']=="") return error_show(1005,"第".($key+1)."行缺少购买方银行数据");
  2406. // if($value['buyer_bankNo']=="") return error_show(1005,"第".($key+1)."行缺少购买方银行账户数据");
  2407. // if($value['tax_fee']=="") return error_show(1005,"第".($key+1)."行缺少发票税额数据");
  2408. // }
  2409. !isset($total[$value['invNo']]) ? $total[$value['invNo']]['total']=0:"";
  2410. $temp=[];
  2411. $temp['invNo'] = $value["invNo"];
  2412. $temp['type'] = 0;
  2413. $temp['inv_type'] =$value['inv_type'];
  2414. $temp['inv_code'] = $value['inv_code'];
  2415. $temp['inv_number'] = $value['inv_number'];
  2416. $temp['inv_subtotal'] = $value['inv_subtotal'];
  2417. $temp['inv_total'] = $value['inv_total'];
  2418. $temp['check_code'] = $value['check_code']??"";
  2419. $temp['seller_id'] = $value['seller_id']??"";
  2420. $temp['seller_title'] = $value['seller_title']??"";
  2421. $temp['seller_addr'] = $value['seller_addr']??"";
  2422. $temp['seller_mobile'] = $value['seller_mobile']??"";
  2423. $temp['seller_bank'] = $value['seller_bank']??"";
  2424. $temp['seller_bankNo'] = $value['seller_bankNo']??"";
  2425. $temp['buyer_id'] = $value['buyer_id']??"";
  2426. $temp['buyer_title'] = $value['buyer_title']??"";
  2427. $temp['buyer_addr'] = $value['buyer_addr']??"";
  2428. $temp['buyer_mobile'] = $value['buyer_mobile']??"";
  2429. $temp['buyer_bank'] = $value['buyer_bank']??"";
  2430. $temp['buyer_bankNo'] = $value['buyer_bankNo']??"";
  2431. $temp['tax_fee'] = $value['tax_fee']??"";
  2432. $temp['item'] = '';
  2433. $temp['pdfstream'] = '';
  2434. $temp['open_date'] = date("Y-m-d H:i:s",strtotime($value['open_date']));
  2435. $temp['status'] = 0;
  2436. $temp['is_del'] = 0;
  2437. $temp['addtime'] = date("Y-m-d H:i:s");
  2438. $temp['updatetime'] = date("Y-m-d H:i:s");
  2439. $total[$value["invNo"]]=$temp;
  2440. }
  2441. Db::startTrans();
  2442. try{
  2443. $keyarr =array_keys($total);
  2444. $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");
  2445. foreach ($total as $key=>$val){
  2446. if(!isset($inpool[$key])) throw new Exception("{$key}发票数据不存在或状态有误");
  2447. if(in_array($val['inv_type'],["fully_digitalized_special_electronic", "fully_digitalized_normal_electronic"]) && $inpool[$key]['winv_fee']!=$val['total']){
  2448. throw new Exception("{$key}全电发票面金额小于未开票金额");
  2449. }
  2450. if($inpool[$key]['inv_type']!= $val['inv_type']) throw new Exception("{$key}发票类型有误");
  2451. $update=[
  2452. "status"=>in_array($val['inv_type'],["fully_digitalized_special_electronic", "fully_digitalized_normal_electronic"])?4:3,
  2453. "open_type"=>$inpool[$key]['open_type']??3,
  2454. "updatetime"=>date("Y-m-d H:i:s")
  2455. ];
  2456. $up =Db::name("invoice_pool")->where([["invNo","=",$key],["is_del","=",0]])->save($update);
  2457. if(!$up){
  2458. Db::rollback();
  2459. return error_show(1005,"发票状态更新失败");
  2460. }
  2461. if($update['status']==4){
  2462. $qrdArr=Db::name("assoc")->where(["viceCode"=>$key,"is_del"=>0])->column("id,orderCode,cancel_fee");
  2463. if(!empty($qrdArr)){
  2464. foreach ($qrdArr as $value){
  2465. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  2466. if($qrdinfo==false){
  2467. Db::rollback();
  2468. return error_show(1003,"确认单信息未找到");
  2469. }
  2470. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  2471. Db::rollback();
  2472. return error_show(1003,"确认单信息开票金额不足");
  2473. }
  2474. $update =[
  2475. "ainv_fee"=>$qrdinfo['ainv_fee']+$value['cancel_fee'],
  2476. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  2477. "invtime"=>date("Y-m-d H:i:s"),
  2478. "inv_status"=>$qrdinfo['winv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 3 : 2,
  2479. "updatetime"=>date("Y-m-d H:i:s"),
  2480. ];
  2481. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  2482. if($qrdup==false){
  2483. Db::rollback();
  2484. return error_show(1003,"确认单信息更新失败");
  2485. }
  2486. $assoc=["status"=>2,"assoc_time"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  2487. $assocup =Db::name("assoc")->where($value)->update($assoc);
  2488. if($assocup==false){
  2489. Db::rollback();
  2490. return error_show(1003,"确认单关联信息更新失败");
  2491. }
  2492. }
  2493. }
  2494. }
  2495. $val['inv_type'] = $this->kingInvoice[$val['inv_type']];
  2496. $list[]=$val;
  2497. }
  2498. $inticket =Db::name("invoice_ticket")->insertAll($list);
  2499. if(!$inticket){
  2500. Db::rollback();
  2501. return error_show(1005,"发票数据录入失败");
  2502. }
  2503. Db::commit();
  2504. return app_show(0,"发票数据录入成功");
  2505. }catch (\Exception $e){
  2506. Db::rollback();
  2507. return error_show(1008,$e->getMessage());
  2508. }
  2509. }
  2510. //金税开票列表
  2511. public function InvInterfaceList(){
  2512. $param=$this->request->param(["page"=>1,"size"=>15,"kpfs"=>"","kplx"=>"","invNo"=>"","companyNo"=>"",
  2513. "customerName"=>"","fphm"=>"","fplxdm"=>"","status"=>""],"post","trim");
  2514. $where=[];
  2515. $model = new InvoiceInterface();
  2516. $param['kpfs']==''?:$where[]=['KPFS','=',$param['kpfs']];
  2517. $param['kplx'] == ''?:$where[]=['KPLX','=',$param['kplx']];
  2518. $param['fplxdm'] == ''?:$where[]=['FPLXDM','=',$param['fplxdm']];
  2519. $param['status'] === ''?:$where[]=['status','=',$param['status']];
  2520. $param['invNo'] == ''?:$where[]=['invNo','like',"%{$param['invNo']}%"];
  2521. if($param['companyNo']!=''){
  2522. $sbh = Db::name('company_info')->where('companyNo',$param['companyNo'])->value('company_license','');
  2523. $where[]=['XHFSBH','=',$sbh];
  2524. }
  2525. $param['customerName'] == ''?:$where[]=['GMFMC','like',"%{$param['customerName']}%"];
  2526. $param['fphm'] == ''?:$where[]=['YFPHM','like',"%{$param['fphm']}%"];
  2527. $list = $model->where($where)
  2528. ->field("id,invNo,KPLX,KPFS,FPLXDM,XHFMC,XHFSBH,GMFSBH,GMFMC,KPR,SKR,FHR,YFPDM,YFPHM,YFPLX,YFPKPRQ,JSHJ,
  2529. HJJE,HJSE,status,remark,updatetime,addtime")->order("id desc")->paginate(["list_rows"=>$param["size"],"page"=>$param['page']]);
  2530. return app_show(0,'获取成功',['list'=>$list->items(),'count'=>$list->total()]);
  2531. }
  2532. //金税开票列表
  2533. public function InvInterfaceCheck(){
  2534. $param=$this->request->param(["id"=>"","status"=>""],'post','trim');
  2535. $model = new InvoiceInterface();
  2536. $valid=Validate::rule([
  2537. "id|数据信息id"=>"require|number|gt:0",
  2538. "status|状态"=>"require|number|in:0,1"
  2539. ]);
  2540. if($valid->check($param)==false)return error_show(1004,$valid->getError());
  2541. $info= $model->findOrEmpty($param['id']);
  2542. if($info->isEmpty())return error_show(1004,"未找到数据");
  2543. if(in_array($info->status,[0,1,2]))return error_show(1004,'数据处理中');
  2544. $info->status=$param['status'];
  2545. $info->is_check=0;
  2546. if($info->status==0){
  2547. $info->DDQQPCH =randomkeys(26).date('YmdHis');
  2548. }
  2549. $up= $info->save();
  2550. return $up? app_show(0,"修改成功"):error_show(1004,"修改失败");
  2551. }
  2552. }