OrderInv.php 106 KB

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