OrderInv.php 123 KB

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