OrderInv.php 133 KB

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