OrderInv.php 107 KB

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