OrderInv.php 133 KB

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