OrderInv.php 112 KB

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