OrderInv.php 114 KB

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