OrderInv.php 134 KB

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