OrderInv.php 123 KB

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