OrderInv.php 117 KB

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