OrderInv.php 117 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515
  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],"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. ]);
  610. if($valid->check($param)==false) return error_show(1004,$valid->getError());
  611. $list =Db::name("invoice_pool")->where(["invNo"=>$param['invNos'],"status"=>1,"is_del"=>0])->column("id");
  612. if(empty($list)) return error_show(1004,"未找到有效数据");
  613. $up =Db::name("invoice_pool")
  614. ->where(["id"=>$list,"status"=>1,"is_del"=>0])
  615. ->update(["status"=>$param['status'],"open_type"=>$param['open_type'],"updatetime"=>date("Y-m-d H:i:s")]);
  616. if($up==false) return error_show(1004,"数据审核失败");
  617. return app_show(0,"数据审核成功");
  618. }
  619. /**
  620. * 发票废弃使用
  621. */
  622. public function Discard(){
  623. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  624. if($invNo=='') return error_show(1004,"参数invNo不能为空");
  625. $invinfo =Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  626. if($invinfo==false) return error_show(1004,"未找到对应发票申请数据");
  627. if(!in_array($invinfo['status'],[1,2,3,4,5,8]))return error_show(1004,"发票状态无法废弃");
  628. $company =Db::name("company_info")->where(["companyNo"=>$invinfo['inv_out']])->find();
  629. if($company==false)return error_show(1004,"开票业务公司未找到");
  630. $invoiceinfo =Db::name("invoice_ticket")->where(["invNo"=>$invNo])->find();
  631. if($invoiceinfo==false)return error_show(1004,"发票详细信息未找到");
  632. Db::startTrans();
  633. try{
  634. $invoice=Config::get("invoice");
  635. $tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  636. $result=$tax->DeprecateInvoices($company['company_license'],$this->kingInvoice[$invoiceinfo['inv_type']],$invoiceinfo['inv_code'],$invoiceinfo['inv_number'],1);
  637. if(isset($result['ZTDM']) && $result['ZTDM']=='050000'){
  638. $up =Db::name("invoice_pool")->where($invinfo)->update(["status"=>6,"updatetime"=>date("Y-m-d H:i:s")]);
  639. if($up){
  640. $qrdArr=Db::name("assoc")->where(["viceCode"=>$invNo,"is_del"=>0,"status"=>[1,2]])->column("id,orderCode,cancel_fee");
  641. if(!empty($qrdArr)){
  642. foreach ($qrdArr as $value){
  643. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  644. if($qrdinfo==false){
  645. Db::rollback();
  646. return error_show(1003,"确认单信息未找到");
  647. }
  648. if ($invinfo['status']==3){
  649. if($qrdinfo['ainv_fee']<$value['cancel_fee']){
  650. Db::rollback();
  651. return error_show(1003,"确认单信息开票金额不足");
  652. }
  653. $update =[
  654. "ainv_fee"=>$qrdinfo['ainv_fee']-$value['cancel_fee'],
  655. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  656. "inv_status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee'] ==0 ? 1: 2,
  657. "updatetime"=>date("Y-m-d H:i:s"),
  658. ];
  659. }else{
  660. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  661. Db::rollback();
  662. return error_show(1003,"确认单信息开票金额不足");
  663. }
  664. $update =[
  665. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  666. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  667. "inv_status"=>$qrdinfo['ainv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee']) ==0 ?1 : 2,
  668. "updatetime"=>date("Y-m-d H:i:s"),
  669. ];
  670. }
  671. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  672. if($qrdup==false){
  673. Db::rollback();
  674. return error_show(1003,"确认单信息更新失败");
  675. }
  676. $assoc=["status"=>3,"assoc_time"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  677. $assocup =Db::name("assoc")->where($value)->update($assoc);
  678. if($assocup==false){
  679. Db::rollback();
  680. return error_show(1003,"确认单关联信息更新失败");
  681. }
  682. }
  683. }
  684. Db::commit();
  685. return app_show(0,"发票废弃成功");
  686. }
  687. }
  688. Db::rollback();
  689. return error_show(1004,"发票废弃失败");
  690. }catch (\Exception $e){
  691. Db::rollback();
  692. return error_show(1004,$e->getMessage());
  693. }
  694. }
  695. /**@param invNo return_reason remark 退票申请
  696. * @return \think\response\Json|void
  697. */
  698. public function ReturnAdd(){
  699. $invNo = isset($this->post['invNo']) && $this->post['invNo']!=''? trim($this->post['invNo']) :'';
  700. if($invNo==""){
  701. return error_show(1004,"参数 invNo 不能为空");
  702. }
  703. $return_reason = isset($this->post['return_reason']) && $this->post['return_reason']!=''? trim($this->post['return_reason']) :'';
  704. $remark = isset($this->post['remark']) && $this->post['remark']!=''? trim($this->post['remark']) :'';
  705. if($return_reason==""){
  706. return error_show(1004,"参数 return_reason 不能为空");
  707. }
  708. if($remark==""){
  709. return error_show(1004,"参数 remark 不能为空");
  710. }
  711. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  712. if($invinfo==false){
  713. return error_show(1004,"发票申请数据未找到");
  714. }
  715. if($invinfo['status']!=4){
  716. return error_show(1004,"发票申请未完成");
  717. }
  718. $qrd =Db::name("assoc")->where(["viceCode"=>$invNo,"status"=>2,"is_del"=>0])->column("orderCode");
  719. if(empty($qrd)){
  720. return error_show(1004,"未找到开票的销售单信息");
  721. }
  722. $isT= Db::name("invoice_return")->where(["invNo"=>$invNo,"status"=>[0,1,2,5]])->findOrEmpty();
  723. if(!empty($isT)) return error_show(1004,"发票退回流程已存在");
  724. $returnCode = makeNo("RIN");
  725. $data=[
  726. "returnCode"=>$returnCode,
  727. "invNo"=>$invNo,
  728. "reason"=>$return_reason,
  729. "remark"=>$remark,
  730. "status"=>0,
  731. "apply_id"=>$this->uid,
  732. "apply_name"=>$this->uname,
  733. "addtime"=>date("Y-m-d H:i:s"),
  734. "updatetime"=>date("Y-m-d H:i:s")
  735. ];
  736. Db::startTrans();
  737. try{
  738. $inter = Db::name("invoice_return")->insert($data);
  739. if($inter){
  740. foreach ($qrd as $value){
  741. $report=ReportCode::where(["qrdNo"=>$value])->find();
  742. if($report)$report->setField("returnInv",$returnCode);
  743. }
  744. Db::commit();
  745. return app_show(0,"退票申请新建成功",["returnCode"=>$returnCode]);
  746. }else{
  747. Db::rollback();
  748. return error_show(1005,"退票申请新建失败");
  749. }
  750. }catch (\Exception $e){
  751. Db::rollback();
  752. return error_show(1006,$e->getMessage());
  753. }
  754. }
  755. // 0 待审核 1 待退票 2 退票成功 3 驳回4 退票失败 5 退票进行中
  756. // 红冲说明 1100000000 已抵扣 1010000000未抵扣 0000000100尚未寄出
  757. public function ReturnStatus(){
  758. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  759. if($returnCode==""){
  760. return error_show(1005,"参数 returnCode 不能为空");
  761. }
  762. $status = isset($this->post['status'])&&$this->post['status']!="" ? intval($this->post['status']):"";
  763. if ($status==""){
  764. return error_show(1005,"参数 status 不能为空");
  765. }
  766. $remark =isset($this->post['remark'])&&$this->post['remark']!="" ? trim($this->post['remark']):"";
  767. $return_reason =isset($this->post['return_reason'])&&$this->post['return_reason']!="" ? trim($this->post['return_reason']):"2";
  768. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  769. if($return==false){
  770. return error_show(1005,"退票申请信息未找到");
  771. }
  772. $red_desc =isset($this->post['red_desc'])&&$this->post['red_desc']!="" ? trim($this->post['red_desc']):"0000000100";
  773. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  774. if($invinfo==false){
  775. return error_show(1004,"发票申请数据未找到");
  776. }
  777. if($invinfo['status']!=4){
  778. return error_show(1004,"发票申请未完成");
  779. }
  780. $qrd =Db::name("assoc")->where(["viceCode"=>$return['invNo'],"status"=>2,"is_del"=>0])->column("orderCode");
  781. if(empty($qrd)){
  782. return error_show(1004,"未找到开票的销售单信息");
  783. }
  784. $ticket = Db::name("invoice_ticket")->where(["invNo"=>$return['invNo'],"type"=>0,"status"=>1,"is_del"=>0])
  785. ->findOrEmpty();
  786. if(empty($ticket)){
  787. return error_show(1004,"未找到开票的票面信息");
  788. }
  789. $ticket['inv_out'] = $invinfo['inv_out']??"";
  790. $return_type = isset($this->post['return_type'])&&$this->post['return_type']!="" ? intval($this->post['return_type']):0;
  791. if($status==1){
  792. if ($return_type==""){
  793. return error_show(1005,"参数 return_type 不能为空");
  794. }
  795. if ($red_desc==""){
  796. return error_show(1005,"参数 red_desc 不能为空");
  797. }
  798. if($return_type==2){
  799. $invCode =isset($this->post['invCode'])&&$this->post['invCode']!=''?trim($this->post['invCode']):"";
  800. $invNum =isset($this->post['invNum'])&&$this->post['invNum']!=''?trim($this->post['invNum']):"";
  801. $open_date =isset($this->post['open_date'])&&$this->post['open_date']!=''?trim($this->post['open_date']):"";
  802. $checkCode =isset($this->post['checkCode'])&&$this->post['checkCode']!=''?trim($this->post['checkCode']):"";
  803. $subtotal_fee =isset($this->post['subtotal_fee'])&&$this->post['subtotal_fee']!=''?floatval($this->post['subtotal_fee']):"";
  804. if($invCode==='') return error_show(1004,"参数 invCode 不能为空");
  805. if($invNum==='') return error_show(1004,"参数 invNum 不能为空");
  806. if($open_date==='') return error_show(1004,"参数 open_date 不能为空");
  807. if(in_array($invinfo['inv_type'],['normal','roll',"toll",'electronic'])){
  808. if($checkCode==='') return error_show(1004,"参数 checkCode 不能为空");
  809. }
  810. if($subtotal_fee==='') return error_show(1004,"参数 subtotal_fee 不能为空");
  811. }
  812. }
  813. //电子普票 只能红冲 不能作废
  814. //电子专票 只能红冲 不能作废
  815. //纸质专票 当月可以作废 次月红冲
  816. //纸质普票 当月作废 次月红冲
  817. //专票 红冲先做红字申请
  818. Db::startTrans();
  819. try{
  820. $isoutmonth =date("Y-m",strtotime($ticket["open_date"]))==date("Y-m")?0:1;
  821. $update=[
  822. "status"=>$status,
  823. "remark"=>$remark,
  824. "updatetime"=>date("Y-m-d H:i:s")
  825. ];
  826. if($status==1) {
  827. $update['is_out_month']=$isoutmonth;
  828. $update['red_desc']=$red_desc;
  829. $update['return_reason']=$return_reason;
  830. $update['return_type']=$return_type;
  831. }
  832. $up =Db::name("invoice_return")->where($return)->update($update);
  833. if($up){
  834. if($status==1){
  835. if($return_type==1){
  836. // $discard=0;
  837. // if(date("Y-m-d",strtotime($ticket["open_date"]))==date("Y-m-d")){
  838. // //当月纸质普通发票+纸质专用发票 作废
  839. // if(in_array($ticket['inv_type'],['004','007'])){
  840. // $discard=1;
  841. //// $result= $this->addDiscard($ticket,$returnCode);
  842. // if(in_array($ticket['inv_type'],['007','026'])) $result= $this->addRedOpen($ticket,2, $invinfo['company_type'], $returnCode);
  843. //
  844. // }
  845. //
  846. // //当月电子普票红冲
  847. // if($ticket['inv_type']=='026')$result= $this->addRedOpen($ticket,$return_reason,$invinfo['company_type'], $returnCode);
  848. // //当月电子专票红冲
  849. // if($ticket['inv_type']=='028')$result= $this->addApplyRed($ticket,$returnCode);
  850. // }else{
  851. // //跨月票都走红冲
  852. // if(in_array($ticket['inv_type'],['007','026'])) $result= $this->addRedOpen($ticket,$return_reason,$invinfo['company_type'], $returnCode);
  853. // if(in_array($ticket['inv_type'],['004','028'])) $result= $this->addApplyRed($ticket,$returnCode);
  854. // }
  855. if(in_array($ticket['inv_type'],['007','026'])) $result= $this->addRedOpen($ticket,$return_reason,$invinfo['company_type'],$returnCode,$red_desc);
  856. if(in_array($ticket['inv_type'],['004','028'])) $result= $this->addApplyRed($ticket,$return_reason,$returnCode,$red_desc);
  857. if(isset($result['code'])){
  858. if(isset($result['data']) &&$result['invRed']!='' ){
  859. $upd=Db::name("invoice_return")->where(["returnCode"=>$returnCode])->update(['red_inv'=>$result['invRed'],"updatetime"=>date("Y-m-d H:i:s")]);
  860. if($upd==false) throw new \Exception("退票更新失败");
  861. }
  862. }
  863. }else{
  864. $invpool=[
  865. "invNo"=>$invinfo['invNo'],
  866. "type"=>1,
  867. "inv_type"=>$ticket['inv_type'],
  868. "inv_code"=>$invCode,
  869. "inv_number"=>$invNum,
  870. "check_code"=>$checkCode,
  871. "inv_total"=>0,
  872. "inv_subtotal"=>$subtotal_fee,
  873. "open_date"=>$open_date,
  874. "item"=>'',
  875. 'pdfstream'=>'',
  876. "status"=>0,
  877. "addtime"=>date("Y-m-d H:i:s"),
  878. "updatetime"=>date("Y-m-d H:i:s")
  879. ];
  880. $in = Db::name("invoice_ticket")->insert($invpool);
  881. if($in==false)throw new \Exception("退票信息添加失败");
  882. }
  883. }
  884. if($status==2){
  885. $infoup =Db::name("invoice_good")->where(["invNo"=>$return['invNo'],"is_del"=>0])->update(["goodNum"=>0,"updatetime"=>date("Y-m-d H:i:s")]);
  886. if($infoup==false)throw new \Exception("退票商品信息修改失败");
  887. $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")]);
  888. if($poolup==false)throw new \Exception($return['invNo']."开票票数据更新失败");
  889. $qrdArr=Db::name("assoc")->where(["viceCode"=>$return['invNo'],"is_del"=>0,"status"=>2])->column("id,orderCode,cancel_fee");
  890. if(!empty($qrdArr)){
  891. foreach ($qrdArr as $value){
  892. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  893. if($qrdinfo==false){
  894. throw new Exception("确认单信息未找到");
  895. // return error_show(1003,"确认单信息未找到");
  896. }
  897. if($qrdinfo['ainv_fee']<$value['cancel_fee']){
  898. throw new Exception("确认单信息开票金额不足");
  899. // return error_show(1003,"确认单信息开票金额不足");
  900. }
  901. $update =[
  902. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  903. "ainv_fee"=>$qrdinfo['ainv_fee']-$value['cancel_fee'],
  904. "inv_status"=>$qrdinfo['inv_fee']==0 &&$qrdinfo['ainv_fee']-$value['cancel_fee']==0 ? 1 : 2,
  905. "status"=>$qrdinfo['ainv_fee']==0 &&$qrdinfo['inv_fee']-$value['cancel_fee']==0&&$qrdinfo['pay_status']==1 ?0 : 1,
  906. "updatetime"=>date("Y-m-d H:i:s"),
  907. ];
  908. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  909. if($qrdup==false){
  910. throw new Exception("确认单信息更新失败");
  911. // return error_show(1003,"确认单信息更新失败");
  912. }
  913. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  914. $assocup =Db::name("assoc")->where($value)->update($assoc);
  915. if($assocup==false){
  916. throw new Exception("确认单关联信息更新失败");
  917. // return error_show(1003,"确认单关联信息更新失败");
  918. }
  919. }
  920. }else{
  921. throw new Exception("未找到关联订单数据信息");
  922. }
  923. }
  924. if($status==3 || $status==4){
  925. foreach ($qrd as $value){
  926. $report=ReportCode::where(["qrdNo"=>$value])->find();
  927. if($report)$report->rmField("returnInv",$returnCode);
  928. }
  929. }
  930. Db::commit();
  931. return app_show(0,"退票申请信息更新成功");
  932. }
  933. Db::rollback();
  934. return error_show(1005,"退票申请信息更新失败");
  935. }catch (\Exception $e){
  936. Db::rollback();
  937. return error_show(1005,$e->getMessage());
  938. }
  939. }
  940. //红冲票
  941. private function addDiscard($ticket=[],$returnCode){
  942. $invoice=Config::get("invoice");
  943. $tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  944. $result=$tax->DeprecateInvoices($ticket['seller_id'],$ticket['inv_type'],$ticket['inv_code'],$ticket['inv_number'],1);
  945. if(isset($result['ZTDM']) && ($result['ZTDM']=='050000' || $result['ZTDM']=='053000')){
  946. if($result['ZTDM']=='053000'){
  947. $up= Db::name("invoice_return")->where(['returnCode'=>$returnCode])->update(["discard"=>2,"updatetime"=>date("Y-m-d H:i:s")]);
  948. if($up==false)throw new \Exception('发票废弃中,信息更新失败', 1006);
  949. return ["code"=>1,"message"=>"发票废弃中"];
  950. }else{
  951. $ups=Db::name("invoice_return")->where(['returnCode'=>$returnCode])->update(["status"=>2,"discard"=>1,"updatetime"=>date("Y-m-d H:i:s")]);
  952. if($ups==false)throw new \Exception('发票已废弃,信息更新失败', 1006);
  953. $up =Db::name("invoice_pool")->where(['invNo'=>$ticket['invNo']])->update(["status"=>6,"updatetime"=>date("Y-m-d H:i:s")]);
  954. if($up==false)throw new \Exception('发票已废弃,信息更新失败', 1006);
  955. return ["code"=>0,"message"=>"发票废弃成功"];
  956. }
  957. }else{
  958. throw new \Exception($result['ZTXX'], 1006);
  959. }
  960. }
  961. //退票详情
  962. /**
  963. * @param array $ticket
  964. * @return \think\response\Json|void
  965. * @throws \think\Exception
  966. */
  967. private function addRedOpen($ticket=[],$return_reason,$company_type,$returnCode,$red_desc){
  968. $goodinfo =Db::name("invoice_good")
  969. ->where(["invNo"=>$ticket['invNo'],"is_del"=>0])
  970. ->field("0 as FPHXZ,
  971. catNo as SPBM,
  972. id as ZXBM,
  973. is_discount as YHZCBS,
  974. if(inv_tag=0,'',inv_tag) as LSLBS,
  975. addTax as ZZSTSGL,
  976. goodName as XMMC,
  977. '' as DW,
  978. '' as SPSL,
  979. '' as DJ,
  980. (-totalPrice) as JE,
  981. 0 HSBZ,
  982. tax as SL,
  983. -taxPrice as SE")->select()->toArray();
  984. if(empty($goodinfo)){
  985. throw new \Exception("开票商品信息未找到", 10006);
  986. }
  987. if(count($goodinfo)>8){
  988. $len =array_unique(array_column($goodinfo,'SL'));
  989. $qdmx=["FPHXZ"=>6,
  990. "SPBM"=>'',
  991. "XMMC"=>"详见对应正数发票及清单",
  992. "YHZCBS"=>0,
  993. "LSLBS"=>'',
  994. "ZZSTSGL"=>'',
  995. "DW"=>'',
  996. "SPSL"=>'',
  997. "DJ"=>'',
  998. "JE"=>sprintf("%01.2f", (array_sum(array_column($goodinfo,'JE')))),
  999. "HSBZ"=>'0',
  1000. "SL"=>count($len)>1?'':$len[0],
  1001. "SE"=>sprintf("%01.2f",array_sum(array_column($goodinfo,'SE'))),
  1002. ];
  1003. }else
  1004. {$qdmx=$goodinfo;}
  1005. $invRed=makeNo("IRE");
  1006. $reddata=[
  1007. 'invNo'=>$ticket['invNo'],
  1008. "invRed"=>$invRed,
  1009. "inv_out"=>$ticket['inv_out'],
  1010. "returnCode"=>$returnCode,
  1011. "apply_id"=>$this->uid,
  1012. "apply_name"=>$this->uname,
  1013. "status"=>3,
  1014. "remark"=>'',
  1015. "addtime"=>date("Y-m-d H:i:s"),
  1016. "updatetime"=>date("Y-m-d H:i:s"),
  1017. "SQBSCQQLSH"=>'',
  1018. "YYSBZ"=> "0000000000", //营业税标志
  1019. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1020. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1021. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1022. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1023. "TKSJ"=>date("YmdHis"), //填开时间
  1024. "XHFSBH"=>$ticket['seller_id'],
  1025. "XHFMC"=>$ticket['seller_title'],
  1026. "GMFSBH"=>$ticket['buyer_id'],
  1027. "GMFMC"=> $ticket['buyer_title'],
  1028. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1029. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1030. "SQSM"=>$red_desc, //申请说明 1100000000 已抵扣 1010000000未抵扣 0000000100尚未寄出
  1031. "XXBTSBS"=>"0",
  1032. "red_reason"=>$return_reason,
  1033. "DDMXXX"=>json_encode($goodinfo,JSON_UNESCAPED_UNICODE),
  1034. "SQBSCQQPCH"=>'',
  1035. "FPLXDM"=>$ticket['inv_type'],
  1036. ];
  1037. $data=[ "invNo"=>$invRed,
  1038. "KPLX"=>"1",
  1039. "XHFSBH"=>$ticket['seller_id'],
  1040. "XHFMC"=>$ticket['seller_title'],
  1041. "XHFDZ"=>$ticket['seller_addr'],
  1042. "XHFDH"=>$ticket['seller_mobile'],
  1043. "XHFYH"=>$ticket['seller_bank'],
  1044. "XHFZH"=>$ticket['seller_bankNo'],
  1045. "GMFBM"=>"",
  1046. "GMFSBH"=>$ticket['buyer_id'],
  1047. "GMFMC"=>$ticket['buyer_title'],
  1048. "GMFDZ"=>$ticket['buyer_addr'],
  1049. "GMFDH"=>$ticket['buyer_mobile'],
  1050. "GMFYH"=>$ticket['buyer_bank'],
  1051. "GMFZH"=>$ticket['buyer_bankNo'],
  1052. "GMFLX"=>$company_type,
  1053. "GMFSJH"=>"",
  1054. "GMFDZYX"=>'',
  1055. "KPR"=>$ticket['issuer'],
  1056. "SKR"=>$ticket['reciver'],
  1057. "FHR"=>$ticket['reviewer'],
  1058. "QDBZ"=>"0",
  1059. "YFPDM"=>$ticket['inv_code'],
  1060. "YFPHM"=>$ticket['inv_number'],
  1061. "YFPLX"=>$ticket['inv_type'],
  1062. "YFPKPRQ"=>date("Y-m-d",strtotime($ticket['open_date'])),
  1063. "CHYY"=>$return_reason,
  1064. "TSCHBZ"=>"0",
  1065. "JSHJ"=>"-".$ticket['inv_total'],
  1066. "HJJE"=>"-".$ticket['inv_subtotal'],
  1067. "HJSE"=>"-".$ticket['tax_fee'],
  1068. "BZ"=> "对应正数发票代码:{$ticket['inv_code']} 号码:{$ticket['inv_number']}",
  1069. "DDMXXX"=>json_encode($qdmx,JSON_UNESCAPED_UNICODE),
  1070. "FPLXDM"=>$ticket['inv_type'],
  1071. "DDQQPCH"=>"",
  1072. "DDQQLSH"=>randomkeys(26).date("YmdHis"),
  1073. "status"=>"0",
  1074. "result"=>"",
  1075. "addtime"=>date("Y-m-d H:i:s"),
  1076. "updatetime"=>date("Y-m-d H:i:s")
  1077. ];
  1078. $invoice=Db::name("invoice_interface")->insert($data);
  1079. if($invoice==false){
  1080. throw new Exception("红冲提交失败",1006);
  1081. }
  1082. $upred=Db::name("invoice_red")->save($reddata);
  1083. if($upred==false){
  1084. throw new Exception("红冲提交失败",1006);
  1085. }
  1086. return ["code"=>0,"message"=>"红冲申请提交成功","invRed"=>$invRed];
  1087. }
  1088. /**
  1089. * @param array $ticket
  1090. * @return \think\response\Json|void
  1091. * @throws \think\db\exception\DataNotFoundException
  1092. * @throws \think\db\exception\DbException
  1093. * @throws \think\db\exception\ModelNotFoundException
  1094. */
  1095. private function addApplyRed($ticket=[],$return_reason,$returnCode,$red_desc){
  1096. $invoice=Config::get("invoice");
  1097. $Tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  1098. $redticketList=[];
  1099. $redticketArry=[
  1100. "SQBSCQQPCH"=>randomkeys(26).date("YmdHis"),
  1101. "NSRSBH"=>$ticket['seller_id'],
  1102. "FPLXDM"=>$ticket['inv_type'],
  1103. ];
  1104. $goodinfo =Db::name("invoice_good")
  1105. ->where(["invNo"=>$ticket['invNo'],"is_del"=>0])
  1106. ->field("0 as FPHXZ,
  1107. catNo as SPBM,
  1108. id as ZXBM,
  1109. is_discount as YHZCBS,
  1110. if(inv_tag=0,'',inv_tag) as LSLBS,
  1111. addTax as ZZSTSGL,
  1112. goodName as XMMC,
  1113. '' as DW,
  1114. '' as SPSL,
  1115. '' as DJ,
  1116. (-totalPrice) as JE,
  1117. 0 HSBZ,
  1118. tax as SL,
  1119. -taxPrice as SE")->select()->toArray();
  1120. if(empty($goodinfo)){
  1121. throw new \Exception("开票商品信息未找到", 10006);
  1122. }
  1123. if(count($goodinfo)>8){
  1124. $len =array_unique(array_column($goodinfo,'SL'));
  1125. $qdmx=["FPHXZ"=>6,
  1126. "SPBM"=>'',
  1127. "XMMC"=>"详见对应正数发票及清单",
  1128. "YHZCBS"=>0,
  1129. "LSLBS"=>'',
  1130. "ZZSTSGL"=>'',
  1131. "DW"=>'',
  1132. "SPSL"=>'',
  1133. "DJ"=>'',
  1134. "JE"=>sprintf("%01.2f", (array_sum(array_column($goodinfo,'JE')))),
  1135. "HSBZ"=>'0',
  1136. "SL"=>count($len)>1?'':$len[0],
  1137. "SE"=>sprintf("%01.2f",array_sum(array_column($goodinfo,'SE'))),
  1138. ];
  1139. }else
  1140. {
  1141. $qdmx=$goodinfo;
  1142. }
  1143. $info=[
  1144. "SQBSCQQLSH"=>randomkeys(26).date("YmdHis"),
  1145. "YYSBZ"=> "0000000000", //营业税标志
  1146. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1147. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1148. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1149. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1150. "TKSJ"=>date("YmdHis"), //填开时间
  1151. "XHFSBH"=>$ticket['seller_id'],
  1152. "XHFMC"=>$ticket['seller_title'],
  1153. "GMFSBH"=>$ticket['buyer_id'],
  1154. "GMFMC"=> $ticket['buyer_title'],
  1155. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1156. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1157. "SQSM"=>$red_desc, //申请说明
  1158. "XXBTSBS"=>"0",
  1159. //0: 正常;1:成品油发票,涉及销售数量 变更(销货退回、开票有误等原因);2: 成品油发票,仅涉及销售金额变更(销 售折让);3:机动车发票,仅涉及销售数
  1160. //量变更(销货退回、开票有误等原因);4: 机动车发票,仅涉及销售金额变更(销 售折让)
  1161. ];
  1162. $invRed=makeNo("IRE");
  1163. $data=[
  1164. 'invNo'=>$ticket['invNo'],
  1165. "invRed"=>$invRed,
  1166. "inv_out"=>$ticket['inv_out'],
  1167. "returnCode"=>$returnCode,
  1168. "apply_id"=>$this->uid,
  1169. "apply_name"=>$this->uname,
  1170. "status"=>0,
  1171. "remark"=>'',
  1172. "addtime"=>date("Y-m-d H:i:s"),
  1173. "updatetime"=>date("Y-m-d H:i:s"),
  1174. "SQBSCQQLSH"=>$info['SQBSCQQLSH'],
  1175. "YYSBZ"=> "0000000000", //营业税标志
  1176. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1177. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1178. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1179. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1180. "TKSJ"=>date("YmdHis"), //填开时间
  1181. "XHFSBH"=>$ticket['seller_id'],
  1182. "XHFMC"=>$ticket['seller_title'],
  1183. "GMFSBH"=>$ticket['buyer_id'],
  1184. "GMFMC"=> $ticket['buyer_title'],
  1185. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1186. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1187. "SQSM"=>$red_desc, //申请说明
  1188. "red_reason"=>$return_reason,
  1189. "XXBTSBS"=>"0",
  1190. "DDMXXX"=>json_encode($qdmx,JSON_UNESCAPED_UNICODE),
  1191. "SQBSCQQPCH"=>$redticketArry['SQBSCQQPCH'],
  1192. "FPLXDM"=>$ticket['inv_type'],
  1193. ];
  1194. $redticketList[]=["HZSQDTXX"=>$info,"DDMXXX"=>$qdmx];
  1195. $result=$Tax->AllocateRedInvoiceApplication($redticketArry,$redticketList);
  1196. $data['status']=5;
  1197. $data['remark']='红冲申请提交失败';
  1198. if(isset($result['ZTDM']) && $result['ZTDM']=='060000'){
  1199. $resultChild=$result['HZSQDSCJG'][0];
  1200. if($resultChild['ZTDM']=='060000'){
  1201. $data['status']=2;
  1202. $data['XXBBH']=$resultChild['XXBBH'];
  1203. $data['remark']='红冲申请审核通过';
  1204. }
  1205. if($resultChild['ZTDM']=='060001'){
  1206. $data['status']=1;
  1207. $data['XXBBH']=$resultChild['XXBBH'];
  1208. $data['remark']='红冲申请审核中';
  1209. }
  1210. }
  1211. if($data['status']==5){
  1212. throw new \Exception($result['ZTXX'], 10006);
  1213. }
  1214. $up =Db::name("invoice_red")->insert($data);
  1215. if($up) return ["code"=>0,"message"=>"红冲发票申请新建成功","invRed"=>$invRed];
  1216. else throw new \Exception($result['ZTXX'], 1006);
  1217. }
  1218. //获取发票详情
  1219. public function returnRed(){
  1220. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  1221. if($returnCode==""){
  1222. return error_show(1005,"参数 returnCode 不能为空");
  1223. }
  1224. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  1225. if($return==false){
  1226. return error_show(1005,"退票申请信息未找到");
  1227. }
  1228. $redinv =isset($this->post['redinv'])&&$this->post['redinv']!="" ? trim($this->post['redinv']):"";
  1229. if($redinv==""){
  1230. return error_show(1005,"参数 redinv 不能为空");
  1231. }
  1232. $red =Db::name("invoice_red")->where(["invRed"=>$redinv,"is_del"=>0])->findOrEmpty();
  1233. if(empty($red)) return error_show(1005,"红冲发票信息未找到");
  1234. if($red['status']!=4) return error_show(1005,"红冲发票开票未完成");
  1235. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  1236. if($invinfo==false){
  1237. return error_show(1005,"发票申请信息未找到");
  1238. }
  1239. Db::startTrans();
  1240. try{
  1241. $update=["status"=>2,"red_inv"=>$redinv,"updatetime"=>date("Y-m-d H:i:s")];
  1242. $up =Db::name("invoice_return")->where($return)->update($update);
  1243. if($up){
  1244. $invup = ["status"=>6,"updatetime"=>date("Y-m-d H:i:s")];
  1245. $inv=Db::name("invoice_pool")->where($invinfo)->update($invup);
  1246. if($inv){
  1247. $qrdArr=Db::name("assoc")->where(["viceCode"=>$return['invNo'],"is_del"=>0])->column("id,orderCode,cancel_fee");
  1248. if(!empty($qrdArr)){
  1249. foreach ($qrdArr as $value){
  1250. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  1251. if($qrdinfo==false){
  1252. Db::rollback();
  1253. return error_show(1003,"确认单信息未找到");
  1254. }
  1255. if($qrdinfo['ainv_fee']<$value['cancel_fee']){
  1256. Db::rollback();
  1257. return error_show(1003,"确认单信息开票金额不足");
  1258. }
  1259. $update =[
  1260. "winv_fee"=>$qrdinfo['winv_fee']+$value['cancel_fee'],
  1261. "ainv_fee"=>$qrdinfo['ainv_fee']-$value['cancel_fee'],
  1262. "inv_status"=>$qrdinfo['inv_fee']==0 &&$qrdinfo['ainv_fee']-$value['cancel_fee']==0 ?
  1263. 1 : 2,
  1264. "status"=>$qrdinfo['inv_fee']==0 &&$qrdinfo['ainv_fee']-$value['cancel_fee']==0 && $qrdinfo['pay_status']==1 ?0 : 1,
  1265. "updatetime"=>date("Y-m-d H:i:s"),
  1266. ];
  1267. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  1268. if($qrdup==false){
  1269. Db::rollback();
  1270. return error_show(1003,"确认单信息更新失败");
  1271. }
  1272. $assoc=["status"=>3,"updatetime"=>date("Y-m-d H:i:s")];
  1273. $assocup =Db::name("assoc")->where($value)->update($assoc);
  1274. if($assocup==false){
  1275. Db::rollback();
  1276. return error_show(1003,"确认单关联信息更新失败");
  1277. }
  1278. }
  1279. }
  1280. Db::commit();
  1281. return app_show(0,"退票申请信息更新成功");
  1282. }
  1283. }
  1284. Db::rollback();
  1285. return error_show(1005,"退票申请信息更新失败");
  1286. }catch (\Exception $e){
  1287. Db::rollback();
  1288. return error_show(1005,$e->getMessage());
  1289. }
  1290. }
  1291. //开票异常失败后修改票面信息
  1292. public function returnInfo(){
  1293. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!="" ? trim($this->post['returnCode']):"";
  1294. if($returnCode==""){
  1295. return error_show(1005,"参数 returnCode 不能为空");
  1296. }
  1297. $return= Db::name("invoice_return")->where(["returnCode"=>$returnCode])->find();
  1298. if($return==false){
  1299. return error_show(1005,"退票申请信息未找到");
  1300. }
  1301. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$return['invNo'],"is_del"=>0])->find();
  1302. $return['inv_out']= $invinfo['inv_out']??"";
  1303. $return['inv_value']= $invinfo['inv_value']??"";
  1304. $return['inv_apply_id']= $invinfo['apply_id']??"";
  1305. $return['inv_apply_name']= $invinfo['apply_name']??"";
  1306. $return['inv_type']= $invinfo['inv_type']??"";
  1307. $return['open_type']= $invinfo['open_type']??"";
  1308. $return['is_ticket']= $invinfo['is_ticket']??"";
  1309. $return['email']= $invinfo['email']??"";
  1310. $return['post_code']= $invinfo['post_code']??"";
  1311. $return['post_company']= $invinfo['post_company']??"";
  1312. $return['check_remark']= $invinfo['check_remark']??"";
  1313. $poolinfo = Db::name("invoice_pool_info")->where(["invNo"=>$return['invNo']])->find();
  1314. $return['buyer_title'] = $poolinfo['buyer_title']??'';
  1315. $return['buyer_title'] = $poolinfo['buyer_title']??'';
  1316. $return['buyer_code'] = $poolinfo['buyer_code']??'';
  1317. $return['buyer_addr'] = $poolinfo['buyer_addr']??'';
  1318. $return['buyer_mobile'] = $poolinfo['buyer_mobile']??'';
  1319. $return['buyer_bank'] = $poolinfo['buyer_bank']??'';
  1320. $return['buyer_bankNo'] = $poolinfo['buyer_bankNo']??'';
  1321. $return['seller_title'] = $poolinfo['seller_title']??'';
  1322. $return['seller_addr'] = $poolinfo['seller_addr']??'';
  1323. $return['seller_code'] = $poolinfo['seller_code']??'';
  1324. $return['seller_mobile'] = $poolinfo['seller_mobile']??'';
  1325. $return['seller_bank'] = $poolinfo['seller_bank']??'';
  1326. $return['seller_bankNo'] = $poolinfo['seller_bankNo']??'';
  1327. $return['voider'] = $poolinfo['voider']??'';
  1328. $return['payee'] = $poolinfo['payee']??'';
  1329. $return['drawer'] = $poolinfo['drawer']??'';
  1330. $return['reviewer'] = $poolinfo['reviewer']??'';
  1331. $return['ownerPlace'] = $poolinfo['ownerPlace']??'';
  1332. $orderList =Db::name("invoice_good")->where(["invNo"=>$return['invNo'],"is_del"=>0])->select()->toArray();
  1333. $return['goodList']=$orderList;
  1334. $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();
  1335. $return['inv_number']=$info['inv_number']??"";
  1336. $return['inv_code']=$info['inv_code']??"";
  1337. $return['open_date']=date("Y-m-d",strtotime($info['open_date']));
  1338. $return['inv_subtotal']=$info['inv_subtotal']??"";
  1339. $return['inv_total']=$info['inv_total']??"";
  1340. $return['check_code']=$info['check_code']??"";
  1341. if(!in_array($return['status'],[2,3,4,5])){
  1342. $opendate =$info['open_date']??"";;
  1343. if($opendate!=''){
  1344. $open_month =date("Y-m",strtotime($opendate));
  1345. $return['is_out_month']= $open_month==date("Y-m")?0:1;
  1346. }
  1347. }
  1348. return app_show(0,"获取成功",$return);
  1349. }
  1350. /**
  1351. *发票池列表
  1352. */
  1353. public function list(){
  1354. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  1355. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  1356. $condition=[["a.is_del","=",0]];
  1357. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  1358. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  1359. $roleid = $this->roleid;
  1360. $check = checkRole($roleid,'84');
  1361. if($check){
  1362. $condition[]=["a.apply_id","=",$this->uid];
  1363. }
  1364. if($start!=""){
  1365. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  1366. }
  1367. if($end!=""){
  1368. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  1369. }
  1370. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  1371. if($status!==""){
  1372. $condition[]=["a.status","=",$status];
  1373. }
  1374. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? trim($this->post['inv_type']):"";
  1375. if($inv_type!==""){
  1376. $condition[]=["a.inv_type","=",$inv_type];
  1377. }
  1378. $inv_out= isset($this->post['inv_out'])&&$this->post['inv_out']!=="" ? trim($this->post['inv_out']):"";
  1379. if($inv_out!==""){
  1380. $condition[]=["a.inv_out","=",$inv_out];
  1381. }
  1382. $platform_type= isset($this->post['platform_type'])&&$this->post['platform_type']!=="" ? intval($this->post['platform_type']):"";
  1383. if($platform_type!==""){
  1384. $condition[]=["a.platform_type","=",$platform_type];
  1385. }
  1386. // $inv_in = isset($this->post['inv_in'])&&$this->post['inv_in']!=="" ? trim($this->post['inv_in']):"";
  1387. // if($inv_in!==""){
  1388. // $condition[]=["a.inv_in","=",$inv_in];
  1389. // }
  1390. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  1391. if($invNo!==""){
  1392. $condition[]=["a.invNo","like","%$invNo%"];
  1393. }
  1394. $relaComNo= isset($this->post['relaComNo'])&&$this->post['relaComNo']!="" ? trim($this->post['relaComNo']) :"";
  1395. if($relaComNo!=""){
  1396. $condition[]=["a.inv_out","=",$relaComNo];
  1397. }
  1398. $inv_number = isset($this->post['inv_number'])&&$this->post['inv_number']!=="" ? trim($this->post['inv_number']):"";
  1399. if($inv_number!==""){
  1400. $condition[]=["c.inv_number","like","%$inv_number%"];
  1401. }
  1402. $inv_code = isset($this->post['inv_code'])&&$this->post['inv_code']!=="" ? trim($this->post['inv_code']):"";
  1403. if($inv_code!==""){
  1404. $condition[]=["c.inv_code","like","%$inv_code%"];
  1405. }
  1406. $inv_company = isset($this->post['inv_company'])&&$this->post['inv_company']!=="" ? trim($this->post['inv_company']):"";
  1407. if($inv_company!==""){
  1408. $condition[]=["a.inv_company","like","%$inv_company%"];
  1409. }
  1410. $buyer_name = isset($this->post['buyer_name'])&&$this->post['buyer_name']!=="" ? trim($this->post['buyer_name']):"";
  1411. if($buyer_name!==""){
  1412. $condition[]=["b.buyer_title","like","%$buyer_name%"];
  1413. }
  1414. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  1415. if($apply_id!==""){
  1416. $condition[]=["a.apply_id","=",$apply_id];
  1417. }
  1418. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  1419. if($apply_name!==""){
  1420. $condition[]=["a.apply_name","like","%$apply_name%"];
  1421. }
  1422. $count=Db::name("invoice_pool")->alias("a")
  1423. ->rightJoin("invoice_pool_info b","a.invNo=b.invNo")
  1424. ->leftJoin("invoice_ticket c","a.invNo=c.invNo and c.type=0 and c.is_del=0")
  1425. ->where($condition)->count();
  1426. $total =ceil($count/$size);
  1427. $page= $page>$total ? intval($total):$page;
  1428. $list=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)->field("a.id,
  1432. a.invNo,
  1433. a.platform_type,
  1434. a.open_type,
  1435. a.is_ticket,
  1436. a.tag_id,
  1437. a.apply_id,
  1438. a.apply_name,
  1439. a.status,
  1440. a.inv_value,
  1441. a.inv_type,
  1442. a.buyer_check,
  1443. a.seller_check,
  1444. a.exam_remark,
  1445. a.check_remark,
  1446. a.inv_company,
  1447. a.inv_out,
  1448. b.buyer_title,
  1449. b.seller_title,
  1450. a.addtime,
  1451. c.inv_code,
  1452. c.inv_number,
  1453. c.open_date,
  1454. c.type")
  1455. ->page($page,$size)->order("a.addtime desc")->select()->toArray();
  1456. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  1457. }
  1458. //获取发票详情
  1459. public function info(){
  1460. $invNo = isset($this->post['invNo'])&& $this->post['invNo']!='' ? trim($this->post['invNo']):"";
  1461. if($invNo==''){
  1462. return error_show(1004,"参数 invNo 不能为空");
  1463. }
  1464. $poolinfo =Db::name("invoice_pool")->where([["invNo","=",$invNo],["is_del","=",0]])->findOrEmpty();
  1465. if(empty($poolinfo)){
  1466. return error_show(1004,"为找到开票信息");
  1467. }
  1468. $pool = Db::name("invoice_pool_info")->where(["invNo"=>$invNo])->find();
  1469. $poolinfo['buyer_title'] = $pool['buyer_title']??'';
  1470. $poolinfo['buyer_code'] = $pool['buyer_code']??'';
  1471. $poolinfo['buyer_addr'] = $pool['buyer_addr']??'';
  1472. $poolinfo['buyer_mobile'] = $pool['buyer_mobile']??'';
  1473. $poolinfo['buyer_bank'] = $pool['buyer_bank']??'';
  1474. $poolinfo['buyer_bankNo'] = $pool['buyer_bankNo']??'';
  1475. $poolinfo['seller_title'] = $pool['seller_title']??'';
  1476. $poolinfo['seller_addr'] = $pool['seller_addr']??'';
  1477. $poolinfo['seller_code'] = $pool['seller_code']??'';
  1478. $poolinfo['seller_mobile'] = $pool['seller_mobile']??'';
  1479. $poolinfo['seller_bank'] = $pool['seller_bank']??'';
  1480. $poolinfo['seller_bankNo'] = $pool['seller_bankNo']??'';
  1481. $poolinfo['voider'] = $pool['voider']??'';
  1482. $poolinfo['payee'] = $pool['payee']??'';
  1483. $poolinfo['drawer'] = $pool['drawer']??'';
  1484. $poolinfo['reviewer'] = $pool['reviewer']??'';
  1485. $poolinfo['ownerPlace'] = $pool['ownerPlace']??'';
  1486. $orderList =Db::name("invoice_good")->where(["invNo"=>$invNo,"is_del"=>0])->column("*","orderCode");
  1487. $poolinfo['goodList']=$orderList;
  1488. $poolinfo['invinfo']=Db::name("invoice_ticket")->withoutField("pdfstream")->where(["invNo"=>$invNo,"type"=>0,"is_del"=>0])
  1489. ->findOrEmpty();
  1490. $orderinfo = Db::name("assoc")->alias("a")->leftJoin("qrd_info c","a.orderCode=c.sequenceNo")
  1491. ->where(["a.viceCode"=>$invNo,"a.is_del"=>0,"a.status"=>[1,2]])
  1492. ->order("a.addtime desc")
  1493. ->field("c.*,a.cancel_fee")
  1494. ->select()->toArray();
  1495. foreach ($orderinfo as &$item){
  1496. $item['ainv_num'] = Db::name("invoice_pool")
  1497. ->alias("a")
  1498. ->leftJoin("invoice_good b", "a.invNo=b.invNo and b.is_del=0")
  1499. ->where(["a.status" => [0, 1, 2, 3], "b.orderCode" => $item['sequenceNo']])
  1500. ->where('a.invNo', '<>', $invNo)
  1501. ->sum("b.goodNum");
  1502. $item['winv_num'] = $item['goodNum'] - $item['ainv_num'];
  1503. $item['inv_num'] = $orderList[$item['sequenceNo']]['goodNum'];
  1504. }
  1505. $poolinfo['orderinfo']=$orderinfo;
  1506. return app_show(0,"获取成功",$poolinfo);
  1507. }
  1508. //批量申请红冲发票
  1509. public function TicketEdit(){
  1510. $post=$this->request->only([
  1511. "invNo"=>"", //开票申请编号
  1512. "inv_code"=>"",//发票编码
  1513. "inv_number"=>"",//发票号码
  1514. "inv_subtotal"=>"",//发票税前额度
  1515. "inv_total"=>"",//发票额度
  1516. "open_date"=>"",//开票日期
  1517. "check_code"=>"",//发票校验码
  1518. ],"post","trim");
  1519. $valid=Validate::rule([
  1520. "invNo|开票申请编号"=>"require|max:255",
  1521. "inv_code|开票代码"=>"number",
  1522. "inv_number|开票编号"=>"number",
  1523. "inv_subtotal|不含税金额"=>"float",
  1524. "inv_total|价税合计"=>"float",
  1525. "open_date|开票日期"=>"date",
  1526. "check_code|校验码"=>"max:255",
  1527. ]);
  1528. if($valid->check($post)==false) return error_show(1004,$valid->getError());
  1529. $ticketinfo =Db::name("invoice_ticket")->where(["invNo"=>$post['invNo'],"is_del"=>0,"type"=>0])->findOrEmpty();
  1530. if(empty($ticketinfo)) return error_show(1004,"未找到开票数据");
  1531. $pool = Db::name("invoice_pool")->where(["invNo"=>$post['invNo'],"is_del"=>0,"status"=>[5,9]])->findOrEmpty();
  1532. if(empty($pool)) return error_show(1004,"开票申请数据不可修改");
  1533. if($pool['open_type']==1)return error_show(1004,"金税开票数据不可修改");
  1534. if($post['inv_number']==='') return error_show(1004,"参数 inv_number不能为空");
  1535. if($post['open_date']==='') return error_show(1004,"参数 open_date 不能为空");
  1536. if(in_array($pool['inv_type'],['normal','roll',"toll",'electronic'])){
  1537. if($post['check_code']==='') return error_show(1004,"参数 check_code不能为空");
  1538. }
  1539. //全电子发票
  1540. if(in_array($pool['inv_type'],['fully_digitalized_special_electronic','fully_digitalized_normal_electronic'])){
  1541. //全电发票传含税金额
  1542. if($post['inv_total']=='') return error_show(1004,"参数 inv_total 不能为空");
  1543. }else{
  1544. //全电发票无invCode
  1545. if($post['inv_code']==='') return error_show(1004,"参数 inv_code不能为空");
  1546. if($post['inv_subtotal']==='') return error_show(1004,"参数 inv_subtotal 不能为空");
  1547. }
  1548. Db::startTrans();
  1549. try{
  1550. $Ticket=[
  1551. "inv_code"=>$post['inv_code'],//发票编码
  1552. "inv_number"=>$post['inv_number'],//发票号码
  1553. "inv_subtotal"=>$post['inv_subtotal'],//发票税前额度
  1554. "inv_total"=>$post['inv_total'],//发票额度
  1555. "open_date"=>$post['open_date'],//开票日期
  1556. "check_code"=>$post['check_code'],//发票校验码
  1557. "status"=>0,//带校验
  1558. "updatetime"=>date("Y-m-d H:i:s")//发票校验码
  1559. ];
  1560. $ticket =Db::name("invoice_ticket")->where($ticketinfo)->update($Ticket);
  1561. if($ticket==false) throw new \Exception("开票信息更新失败");
  1562. $polup =Db::name("invoice_pool")->where($pool)->update(["status"=>3,"check_remark"=>"","updatetime"=>date("Y-m-d H:i:s")]);
  1563. if($polup==false) throw new \Exception("开票申请信息更新失败");
  1564. Db::commit();
  1565. return app_show(0,"开票信息更新成功");
  1566. }catch (Exception $e){
  1567. Db::rollback();
  1568. return error_show(1005,$e->getMessage());
  1569. }
  1570. }
  1571. //红票申请开票
  1572. public function returnList(){
  1573. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  1574. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  1575. $condition=[["b.is_del","=",0]];
  1576. $roleid = $this->roleid;
  1577. $check = checkRole($roleid,'89');
  1578. if($check){
  1579. $condition[]=["a.apply_id","=",$this->uid];
  1580. }
  1581. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  1582. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  1583. $companyNo = isset($this->post['companyNo'])&&$this->post['companyNo']!="" ? trim($this->post['companyNo']) :"";
  1584. if($companyNo!=""){
  1585. $condition[]=["b.inv_out","=",$companyNo];
  1586. }
  1587. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  1588. if($relaComNo!=""){
  1589. $condition[]=["b.inv_out","=",$relaComNo];
  1590. }
  1591. if($start!=""){
  1592. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  1593. }
  1594. if($end!=""){
  1595. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  1596. }
  1597. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? trim($this->post['inv_type']):"";
  1598. if($inv_type!==""){
  1599. $condition[]=["b.inv_type","=",$inv_type];
  1600. }
  1601. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  1602. if($apply_id!==""){
  1603. $condition[]=["a.apply_id","=",$apply_id];
  1604. }
  1605. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  1606. if($apply_name!==""){
  1607. $condition[]=["a.apply_name","like","%$apply_name%"];
  1608. }
  1609. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  1610. if($status!==""){
  1611. $condition[]=["a.status","=",$status];
  1612. }
  1613. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  1614. if($invNo!==""){
  1615. $condition[]=["a.invNo","like","%$invNo%"];
  1616. }
  1617. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!=="" ? trim($this->post['returnCode']):"";
  1618. if($returnCode!==""){
  1619. $condition[]=["a.returnCode","like","%$returnCode%"];
  1620. }
  1621. $count=Db::name("invoice_return")->alias("a")
  1622. ->leftJoin("invoice_pool b","a.invNo=b.invNo")
  1623. ->leftJoin("invoice_pool_info c","a.invNo=c.invNo")
  1624. ->where($condition)->count();
  1625. $total =ceil($count/$size);
  1626. $page= $page>$total ? intval($total):$page;
  1627. $list=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)->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();
  1631. foreach ($list as &$value){
  1632. $company =Db::name("company_info")->where(["companyNo"=>$value['inv_out']])->find();
  1633. $value['companyName'] = $company['company_name']??"";
  1634. if(!in_array($value['status'],[2,3,4,5])){
  1635. $opendate =Db::name("invoice_ticket")->where(["invNo"=>$value['invNo'],"is_del"=>0,"type"=>0])->value("open_date","");
  1636. if($opendate!=''){
  1637. $open_month =date("Y-m",strtotime($opendate));
  1638. $value['is_out_month']= $open_month==date("Y-m")?0:1;
  1639. }
  1640. }
  1641. }
  1642. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  1643. }
  1644. //红冲申请
  1645. public function applyRed(){
  1646. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  1647. if($invNo=='')return error_show(1004,"参数 invNo 不能为空");
  1648. $invfo =Db::name("invoice_pool")->where(["invNo"=>$invNo,"is_del"=>0])->find();
  1649. if($invfo==false) return error_show(1004,"发票数据未找到");
  1650. if(!in_array($invfo['status'],[1,2,3,4,5,8]))return error_show(1004,"发票数据状态无法申请");
  1651. $ticket =Db::name("invoice_ticket")->where(["invNo"=>$invNo])->findOrEmpty();
  1652. if(empty($ticket)) return error_show(1004,"未找到开票信息");
  1653. $company =Db::name("company_info")->where(["companyNo"=>'GS2207150956046617'])->findOrEmpty();
  1654. if(empty($company)) return error_show(1004,"业务企业信息未找到");
  1655. $invoice=Config::get("invoice");
  1656. $Tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  1657. $redticketList=[];
  1658. $redticketArry=[
  1659. "SQBSCQQPCH"=>randomkeys(26).date("YmdHis"),
  1660. "NSRSBH"=>$company['company_license'],
  1661. "FPLXDM"=>$this->kingInvoice[$invfo['inv_type']],
  1662. ];
  1663. $goodinfo =Db::name("invoice_good")
  1664. ->where(["invNo"=>$invNo,"is_del"=>0])
  1665. ->field("0 as FPHXZ,
  1666. catNo as SPBM,
  1667. id as ZXBM,
  1668. is_discount as YHZCBS,
  1669. if(inv_tag=0,'',inv_tag) as LSLBS,
  1670. addTax as ZZSTSGL,
  1671. goodName as XMMC,
  1672. '' as DW,
  1673. '' as SPSL,
  1674. '' as DJ,
  1675. (-totalPrice) as JE,
  1676. 0 HSBZ,
  1677. tax as SL,
  1678. -taxPrice as SE")->select()->toArray();
  1679. if(empty($goodinfo)){
  1680. Db::rollback();
  1681. return error_show(1004,"开票商品信息未找到");
  1682. }
  1683. if(count($goodinfo)>8){
  1684. $len =array_unique(array_column($goodinfo,'SL'));
  1685. $qdmx=["FPHXZ"=>6,
  1686. "SPBM"=>'',
  1687. "XMMC"=>"详见对应正数发票及清单",
  1688. "YHZCBS"=>0,
  1689. "LSLBS"=>'',
  1690. "ZZSTSGL"=>'',
  1691. "DW"=>'',
  1692. "SPSL"=>'',
  1693. "DJ"=>'',
  1694. "JE"=>sprintf("%01.2f", (array_sum(array_column($goodinfo,'JE')))),
  1695. "HSBZ"=>'0',
  1696. "SL"=>count($len)>1?'':$len[0],
  1697. "SE"=>sprintf("%01.2f",array_sum(array_column($goodinfo,'SE'))),
  1698. ];
  1699. }else
  1700. {
  1701. $qdmx=$goodinfo;
  1702. }
  1703. $info=[
  1704. "SQBSCQQLSH"=>randomkeys(26).date("YmdHis"),
  1705. "YYSBZ"=> "0000000000", //营业税标志
  1706. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1707. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1708. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1709. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1710. "TKSJ"=>date("YmdHis"), //填开时间
  1711. "XHFSBH"=>$ticket['seller_id'],
  1712. "XHFMC"=>$ticket['seller_title'],
  1713. "GMFSBH"=>$ticket['buyer_id'],
  1714. "GMFMC"=> $ticket['buyer_title'],
  1715. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1716. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1717. "SQSM"=>"1100000000", //申请说明
  1718. "XXBTSBS"=>"0",//0: 正常;1:成品油发票,涉及销售数量 变更(销货退回、开票有误等原因);2: 成品油发票,仅涉及销售金额变更(销 售折让);3:机动车发票,仅涉及销售数
  1719. //量变更(销货退回、开票有误等原因);4: 机动车发票,仅涉及销售金额变更(销 售折让)
  1720. ];
  1721. $invRed=makeNo("IRE");
  1722. $data=[
  1723. 'invNo'=>$invNo,
  1724. "invRed"=>$invRed,
  1725. "inv_out"=>$invfo['inv_out']??"",
  1726. "apply_id"=>$this->uid,
  1727. "apply_name"=>$this->uname,
  1728. "status"=>0,
  1729. "remark"=>'',
  1730. "addtime"=>date("Y-m-d H:i:s"),
  1731. "updatetime"=>date("Y-m-d H:i:s"),
  1732. "SQBSCQQLSH"=>$info['SQBSCQQLSH'],
  1733. "YYSBZ"=> "0000000000", //营业税标志
  1734. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1735. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1736. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1737. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1738. "TKSJ"=>date("YmdHis"), //填开时间
  1739. "XHFSBH"=>$ticket['seller_id'],
  1740. "XHFMC"=>$ticket['seller_title'],
  1741. "GMFSBH"=>$ticket['buyer_id'],
  1742. "GMFMC"=> $ticket['buyer_title'],
  1743. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1744. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1745. "SQSM"=>"1100000000", //申请说明
  1746. "XXBTSBS"=>"0",
  1747. "DDMXXX"=>json_encode($qdmx,JSON_UNESCAPED_UNICODE),
  1748. "SQBSCQQPCH"=>$redticketArry['SQBSCQQPCH'],
  1749. "FPLXDM"=>$this->kingInvoice[$invfo['inv_type']],
  1750. ];
  1751. $redticketList[]=["HZSQDTXX"=>$info,"DDMXXX"=>$qdmx];
  1752. $result=$Tax->AllocateRedInvoiceApplication($redticketArry,$redticketList);
  1753. $data['status']=5;
  1754. $data['remark']='红冲申请提交失败';
  1755. if(isset($result['ZTDM']) && $result['ZTDM']=='060000'){
  1756. $resultChild=$result['HZSQDSCJG'][0];
  1757. if($resultChild['ZTDM']=='060000'){
  1758. $data['status']=2;
  1759. $data['XXBBH']=$resultChild['XXBBH'];
  1760. $data['remark']='红冲申请审核通过';
  1761. }
  1762. if($resultChild['ZTDM']=='060001'){
  1763. $data['status']=1;
  1764. $data['XXBBH']=$resultChild['XXBBH'];
  1765. $data['remark']='红冲申请审核中';
  1766. }
  1767. }
  1768. if($data['status']==5){
  1769. return error_show(1004,"红冲发票申请新建失败");
  1770. }
  1771. $up =Db::name("invoice_red")->insert($data);
  1772. return $up ? app_show(0,"红冲发票申请新建成功",["invRed"=>$invRed]):error_show(1004,"红冲发票申请新建失败");
  1773. }
  1774. //批量申请红冲发票
  1775. public function batchApplyRed(){
  1776. $invArr = isset($this->post['invArr'])&&!empty($this->post['invArr'])?$this->post['invArr']:[];
  1777. if(empty($invArr))return error_show(1004,"参数 invArr 不能为空");
  1778. $invfo =Db::name("invoice_pool")->where(["invNo"=>$invArr,"is_del"=>0])->select()->toArray();
  1779. if(empty($invfo)) return error_show(1004,"发票数据未找到");
  1780. $insertAll=[];
  1781. $companyCode = array_unique(array_column($invfo,"inv_out"));
  1782. $inv_type = array_unique(array_column($invfo,"inv_type"));
  1783. if(count($companyCode)>1){
  1784. return error_show(1004,"业务企业公司不统一");
  1785. }
  1786. if(count($inv_type)>1){
  1787. return error_show(1004,"发票类型不统一");
  1788. }
  1789. $company =Db::name("company_info")->where(["companyNo"=>$companyCode])->findOrEmpty();
  1790. if(empty($company)) {
  1791. return error_show(1004,"业务企业信息未找到");
  1792. }
  1793. $ticketArr=Db::name("invoice_ticket")->where(["invNo"=>$invArr,"is_del"=>0])->column("*","invNo");
  1794. if(empty($ticketArr)){
  1795. return error_show(1004,"未找到开票信息");
  1796. }
  1797. $invoice=Config::get("invoice");
  1798. $Tax =new \TaxInvoice($invoice['appKey'],$invoice['appSecret'],$invoice['entCode']);
  1799. Db::startTrans();
  1800. try{
  1801. $redticketArry=[
  1802. "SQBSCQQPCH"=>randomkeys(26).date("YmdHis"),
  1803. "NSRSBH"=>$company['company_license'],
  1804. "FPLXDM"=>$this->kingInvoice[$inv_type[0]],
  1805. ];
  1806. $redticketList=[];
  1807. foreach ($invfo as $value){
  1808. if(!in_array($value['status'],[1,2,3,4,5,8])){
  1809. Db::rollback();
  1810. return error_show(1004,"发票数据状态无法申请");
  1811. }
  1812. $ticket =$ticketArr[$value['invNo']] ??[];
  1813. if(empty($ticket)){
  1814. Db::rollback();
  1815. return error_show(1004,"未找到开票信息");
  1816. }
  1817. $goodinfo =Db::name("invoice_good")
  1818. ->where(["invNo"=>$value['invNo'],"is_del"=>0])
  1819. ->field(" 6 as FPHXZ,
  1820. catNo as SPBM,
  1821. id as ZXBM,
  1822. is_discount as YHZCBS,
  1823. if(inv_tag=0,'',inv_tag) as LSLBS,
  1824. addTax as ZZSTSGL,
  1825. goodName as XMMC,
  1826. '' as DW,
  1827. '' as SPSL,
  1828. '' as DJ,
  1829. (-totalPrice) as JE,
  1830. 0 HSBZ,
  1831. tax as SL,
  1832. -taxPrice as SE")
  1833. ->select()->toArray();
  1834. if(empty($goodinfo)){
  1835. Db::rollback();
  1836. return error_show(1004,"开票商品信息未找到");
  1837. }
  1838. $len =array_unique(array_column($goodinfo,'SL'));
  1839. $qdmx=["FPHXZ"=>6,
  1840. "SPBM"=>'',
  1841. "XMMC"=>"详见对应正数发票及清单",
  1842. "YHZCBS"=>0,
  1843. "LSLBS"=>'',
  1844. "ZZSTSGL"=>'',
  1845. "DW"=>'',
  1846. "SPSL"=>'',
  1847. "DJ"=>'',
  1848. "JE"=>sprintf("%01.2f", (array_sum(array_column($goodinfo,'JE')))),
  1849. "HSBZ"=>'0',
  1850. "SL"=>count($len)>1?'':$len[0],
  1851. "SE"=>sprintf("%01.2f",array_sum(array_column($goodinfo,'SE'))),
  1852. ];
  1853. $info=[
  1854. "SQBSCQQLSH"=>randomkeys(26).date("YmdHis"),
  1855. "YYSBZ"=> "0000000000", //营业税标志
  1856. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1857. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1858. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1859. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1860. "TKSJ"=>date("YmdHis"), //填开时间
  1861. "XHFSBH"=>$ticket['seller_id'],
  1862. "XHFMC"=>$ticket['seller_title'],
  1863. "GMFSBH"=>$ticket['buyer_id'],
  1864. "GMFMC"=> $ticket['buyer_title'],
  1865. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1866. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1867. "SQSM"=>"1100000000", //申请说明
  1868. "XXBTSBS"=>"0",//0: 正常;1:成品油发票,涉及销售数量 变更(销货退回、开票有误等原因);2: 成品油发票,仅涉及销售金额变更(销 售折让);3:机动车发票,仅涉及销售数
  1869. //量变更(销货退回、开票有误等原因);4: 机动车发票,仅涉及销售金额变更(销 售折让)
  1870. ];
  1871. $redticketList[]=["HZSQDTXX"=>$info,"DDMXXX"=>$qdmx];
  1872. $data=[
  1873. 'invNo'=>$value['invNo'],
  1874. "invRed"=>makeNo("IRE"),
  1875. "inv_out"=>$value['inv_out']??"",
  1876. "apply_id"=>$this->uid,
  1877. "apply_name"=>$this->uname,
  1878. "status"=>5,
  1879. "remark"=>'红冲申请提交失败',
  1880. "addtime"=>date("Y-m-d H:i:s"),
  1881. "updatetime"=>date("Y-m-d H:i:s"),
  1882. "SQBSCQQLSH"=>$info['SQBSCQQLSH'],
  1883. "YYSBZ"=> "0000000000", //营业税标志
  1884. "XXBLX"=> "0",//信息表类型 0 正常;1 逾期(仅销方开具)
  1885. "YFPDM"=>$ticket['inv_code'], //红字专票购方已抵扣可不填
  1886. "YFPHM"=> $ticket['inv_number'],//红字专票购方已抵扣可不填
  1887. "YFPKPRQ"=>$ticket['open_date'], //红字专票购方已抵扣可不填
  1888. "TKSJ"=>date("YmdHis"), //填开时间
  1889. "XHFSBH"=>$ticket['seller_id'],
  1890. "XHFMC"=>$ticket['seller_title'],
  1891. "GMFSBH"=>$ticket['buyer_id'],
  1892. "GMFMC"=> $ticket['buyer_title'],
  1893. "HJJE"=> "-".$ticket['inv_subtotal'],//合计金额(不含 税)
  1894. "HJSE"=>"-".$ticket['tax_fee'],//合计税额
  1895. "SQSM"=>"1100000000", //申请说明
  1896. "XXBTSBS"=>"0",
  1897. "DDMXXX"=>json_encode($qdmx,JSON_UNESCAPED_UNICODE),
  1898. "SQBSCQQPCH"=>$redticketArry['SQBSCQQPCH'],
  1899. "FPLXDM"=>$this->kingInvoice[$value['inv_type']],
  1900. ];
  1901. $insertAll[$info['SQBSCQQLSH']]=$data;
  1902. }
  1903. $result=$Tax->AllocateRedInvoiceApplication($redticketArry,$redticketList);
  1904. if(isset($result['ZTDM']) && $result['ZTDM']=='060000'){
  1905. if(isset($result['HZSQDSCJG']) && count($result['HZSQDSCJG'])>0){
  1906. foreach ($result['HZSQDSCJG'] as $value){
  1907. if($value['ZTDM']=='060000'){
  1908. $insertAll[$value['SQBSCQQLSH']]['status']=2;
  1909. $insertAll[$value['SQBSCQQLSH']]['remark']="红冲申请审核通过";
  1910. $insertAll[$value['SQBSCQQLSH']]['XXBBH']=$value['XXBBH'];
  1911. continue;
  1912. // $data['status']=2;
  1913. // $data['remark']='红冲申请审核通过';
  1914. }
  1915. if($value['ZTDM']=='060001'){
  1916. $insertAll[$value['SQBSCQQLSH']]['status']=1;
  1917. $insertAll[$value['SQBSCQQLSH']]['remark']="红冲申请审核中";
  1918. $insertAll[$value['SQBSCQQLSH']]['XXBBH']=$value['XXBBH'];
  1919. continue;
  1920. }
  1921. unset($insertAll[$value['SQBSCQQLSH']]);
  1922. }
  1923. }
  1924. }else{
  1925. Db::rollback();
  1926. return error_show(1004,"红冲发票申请失败");
  1927. }
  1928. $uns= Db::name("invoice_red")->insertAll($insertAll);
  1929. if($uns){
  1930. Db::commit();
  1931. return app_show(0,"红冲发票申请新建成功,成功数量{$uns}个");
  1932. }
  1933. Db::rollback();
  1934. return error_show(1004,"红冲发票申请新建失败");
  1935. }catch (\Exception $e){
  1936. Db::rollback();
  1937. return error_show(1004,$e->getMessage());
  1938. }
  1939. }
  1940. //红票申请开票
  1941. public function redOpen(){
  1942. $invRed= isset($this->post['invRed'])&&$this->post['invRed']!=''? $this->post['invRed']:"";
  1943. if($invRed==''){
  1944. return error_show(1004,"参数 invRed 不能为空");
  1945. }
  1946. $redinfo =Db::name("invoice_red")->where(["invRed"=>$invRed,"is_del"=>0])->findOrEmpty();
  1947. if(empty($redinfo)){
  1948. return error_show(1004,"红冲发票申请不存在");
  1949. }
  1950. $redreason = isset($this->post['redreason'])&&$this->post['redreason']!=''? $this->post['redreason']:"";
  1951. if($redreason==''){
  1952. return error_show(1004,"参数 redreason 不能为空");
  1953. }
  1954. if($redinfo['status']!=2){
  1955. return error_show(1004,"数据状态不可提交申请");
  1956. }
  1957. $invinfo = Db::name("invoice_pool")->where(["invNo"=>$redinfo['invNo'],"is_del"=>0])->findOrEmpty();
  1958. if(empty($invinfo)) return error_show(1004,"红冲发票原票信息未找到");
  1959. $invpoolinfo = Db::name("invoice_ticket")->where(["invNo"=>$redinfo['invNo'],"status"=>1,"is_del"=>0])->findOrEmpty();
  1960. if(empty($invpoolinfo)) return error_show(1004,"红冲发票开票信息未找到");
  1961. Db::startTrans();
  1962. try{
  1963. $data=[
  1964. "invNo"=>$invRed,
  1965. "KPLX"=>"1",
  1966. "XHFSBH"=>$invpoolinfo['seller_id'],
  1967. "XHFMC"=>$invpoolinfo['seller_title'],
  1968. "XHFDZ"=>$invpoolinfo['seller_addr'],
  1969. "XHFDH"=>$invpoolinfo['seller_mobile'],
  1970. "XHFYH"=>$invpoolinfo['seller_bank'],
  1971. "XHFZH"=>$invpoolinfo['seller_bankNo'],
  1972. "GMFBM"=>"",
  1973. "GMFSBH"=>$invpoolinfo['buyer_id'],
  1974. "GMFMC"=>$invpoolinfo['buyer_title'],
  1975. "GMFDZ"=>$invpoolinfo['buyer_addr'],
  1976. "GMFDH"=>$invpoolinfo['buyer_mobile'],
  1977. "GMFYH"=>$invpoolinfo['buyer_bank'],
  1978. "GMFZH"=>$invpoolinfo['buyer_bankNo'],
  1979. "GMFLX"=>$invinfo['company_type'],
  1980. "GMFSJH"=>"",
  1981. "GMFDZYX"=>$invinfo['email'],
  1982. "KPR"=>$invpoolinfo['issuer'],
  1983. "SKR"=>$invpoolinfo['reciver'],
  1984. "FHR"=>$invpoolinfo['reviewer'],
  1985. "QDBZ"=>"0",
  1986. "YFPDM"=>$redinfo['YFPDM'],
  1987. "YFPHM"=>$redinfo['YFPHM'],
  1988. "YFPLX"=>$invpoolinfo['inv_type'] == '004' || $invpoolinfo['inv_type'] =='028'||$redreason=='' ?'':$invpoolinfo['inv_type'],
  1989. "YFPKPRQ"=>$invpoolinfo['inv_type'] == '004' || $invpoolinfo['inv_type'] == '028'||$redreason=='' ? '':date("Y-m-d",strtotime($redinfo['YFPKPRQ'])),
  1990. "CHYY"=>$invpoolinfo['inv_type'] == '004' || $invpoolinfo['inv_type'] == '028' ? '':$redreason,
  1991. "TSCHBZ"=>"0",
  1992. "JSHJ"=>"-".$invpoolinfo['inv_total'],
  1993. "HJJE"=>"-".$invpoolinfo['inv_subtotal'],
  1994. "HJSE"=>"-".$invpoolinfo['tax_fee'],
  1995. "BZ"=> $invpoolinfo['inv_type'] == '004' || $invpoolinfo['inv_type'] =='028'?"开具红字增值税专用发票信息表编号{$redinfo['XXBBH']}":"对应正数发票代码:{$redinfo['YFPDM']} 号码:{$redinfo['YFPHM']}",
  1996. "DDMXXX"=>$redinfo['DDMXXX'],
  1997. "FPLXDM"=>$redinfo['FPLXDM'],
  1998. "DDQQPCH"=>"",
  1999. "DDQQLSH"=>randomkeys(26).date("YmdHis"),
  2000. "status"=>"0",
  2001. "result"=>"",
  2002. "addtime"=>date("Y-m-d H:i:s"),
  2003. "updatetime"=>date("Y-m-d H:i:s")
  2004. ];
  2005. $invoice=Db::name("invoice_interface")->insert($data);
  2006. if($invoice==false){
  2007. Db::rollback();
  2008. return error_show(1003,"红冲开票信息生成失败");
  2009. }
  2010. $upred=Db::name("invoice_red")->where(["invRed"=>$invRed])->update(["status"=>3,"red_reason"=>$redreason,"updatetime"=>date("Y-m-d H:i:s")]);
  2011. if($upred==false){
  2012. Db::rollback();
  2013. return error_show(1003,"红冲发票信息更新失败");
  2014. }
  2015. Db::commit();
  2016. return app_show(0,"红冲发票信息开票提交成功");
  2017. }catch (\Exception $e){
  2018. Db::rollback();
  2019. return error_show(1003,$e->getMessage());
  2020. }
  2021. }
  2022. //红冲票申请列表
  2023. public function redList(){
  2024. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  2025. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  2026. $condition=[["is_del","=",0],["FPLXDM","in",["004",'028']]];
  2027. $roleid = $this->roleid;
  2028. $check = checkRole($roleid,'110');
  2029. if($check){
  2030. $condition[]=["apply_id","=",$this->uid];
  2031. }
  2032. $invRed=isset($this->post["invRed"])&&$this->post["invRed"]!=''?trim($this->post["invRed"]):"";
  2033. if($invRed!=''){
  2034. $condition[]=["invRed","like","%$invRed%"];
  2035. }
  2036. $apply_id=isset($this->post["apply_id"])&&$this->post["apply_id"]!=''?intval($this->post["apply_id"]):"";
  2037. if($apply_id!=''){
  2038. $condition[]=["apply_id","=",$apply_id];
  2039. }
  2040. $invNo=isset($this->post["invNo"])&&$this->post["invNo"]!=''?trim($this->post["invNo"]):"";
  2041. if($invNo!=''){
  2042. $condition[]=["invNo","like","%$invNo%"];
  2043. }
  2044. $companyNo=isset($this->post["companyNo"])&&$this->post["companyNo"]!=''?trim($this->post["companyNo"]):"";
  2045. if($companyNo!=''){
  2046. $condition[]=["inv_out","=",$companyNo];
  2047. }
  2048. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  2049. if($relaComNo!=""){
  2050. $condition[]=["inv_out","=",$relaComNo];
  2051. }
  2052. $apply_name=isset($this->post["apply_name"])&&$this->post["apply_name"]!=''?trim($this->post["apply_name"]):"";
  2053. if($apply_name!=''){
  2054. $condition[]=["apply_name","like","%$apply_name%"];
  2055. }
  2056. $start = isset($this->post["start"])&&$this->post["start"]!=''?trim($this->post["start"]):"";
  2057. $end = isset($this->post["end"])&&$this->post["end"]!=''?trim($this->post["end"]):"";
  2058. if($start!=''){
  2059. $condition[]=["addtime",">=",$start];
  2060. }
  2061. if($end!=''){
  2062. $condition[]=["addtime","<=",$end];
  2063. }
  2064. $total_low=isset($this->post["total_low"])&&$this->post["total_low"]!=''?floatval($this->post["total_low"]):"";
  2065. $total_up=isset($this->post["total_up"])&&$this->post["total_up"]!=''?floatval($this->post["total_up"]):"";
  2066. if($total_low!=''){
  2067. $condition[]=["ABS(HJJE)",">=",$total_low];
  2068. }
  2069. if($total_up!=''){
  2070. $condition[]=["ABS(HJJE)","<=",$total_up];
  2071. }
  2072. $count= Db::name("invoice_red")->where($condition)->count();
  2073. $total =ceil($count/$size);
  2074. $page=$total>=$page?$page :intval($total);
  2075. $list =Db::name("invoice_red")->where($condition)->json(["DDMXXX"])->page($page,$size)->order("addtime desc")->select()
  2076. ->toArray();
  2077. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  2078. }
  2079. /**
  2080. * @return \think\response\Json|void
  2081. */
  2082. public function ticketInfo(){
  2083. $num =isset($this->post['number'])&&$this->post['number']!=''?trim($this->post['number']):"";
  2084. if($num==''){
  2085. return error_show(1004,'参数 number 不能为空');
  2086. }
  2087. $condition=[["status","in",[0,1,2]]];
  2088. $condition[]=["inv_number","=",$num];
  2089. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=''?trim($this->post['invNo']):"";
  2090. if($invNo!=='')$condition[]=["invNo","=",$invNo];
  2091. $info =Db::name("invoice_ticket")->json(['item'])->withoutField("pdfstream")->where($condition)->findOrEmpty();
  2092. if(!empty($info)&& isset($info['item'])){
  2093. array_walk($info['item'],function (&$value){
  2094. if(stripos($value["SL"],"%")==false)$value['SL'] = ($value['SL']*100)."%";
  2095. });
  2096. }
  2097. $info['invtype_name'] = $this->typename[$info['inv_type']]??"";
  2098. return empty($info)?error_show(1005,"未找到数据"):app_show(0,"获取成功",$info);
  2099. }
  2100. //退票列表
  2101. public function returnRedList(){
  2102. $page=isset($this->post['page'])&&$this->post['page']!="" ? intval($this->post['page']) :1;
  2103. $size=isset($this->post['size'])&&$this->post['size']!="" ? intval($this->post['size']) :15;
  2104. $condition=[];
  2105. $start = isset($this->post['start'])&&$this->post['start']!="" ? trim($this->post['start']) :"";
  2106. $end = isset($this->post['end'])&&$this->post['end']!="" ? trim($this->post['end']) :"";
  2107. if($start!=""){
  2108. $condition[]=["a.addtime",">=",$start." 00:00:00"];
  2109. }
  2110. if($end!=""){
  2111. $condition[]=["a.addtime","<=",$end." 23:59:59"];
  2112. }
  2113. $inv_type = isset($this->post['inv_type'])&&$this->post['inv_type']!=="" ? trim($this->post['inv_type']):"";
  2114. if($inv_type!==""){
  2115. $condition[]=["c.inv_type","=",$inv_type];
  2116. }
  2117. $companyNo = isset($this->post['companyNo'])&&$this->post['companyNo']!=="" ? trim($this->post['companyNo']):"";
  2118. if($companyNo!==""){
  2119. $condition[]=["c.inv_out","=",$companyNo];
  2120. }
  2121. $relaComNo= isset($post['relaComNo'])&&$post['relaComNo']!="" ? trim($post['relaComNo']) :"";
  2122. if($relaComNo!=""){
  2123. $condition[]=["c.inv_out","=",$relaComNo];
  2124. }
  2125. $apply_id = isset($this->post['apply_id'])&&$this->post['apply_id']!=="" ? intval($this->post['apply_id']):"";
  2126. if($apply_id!==""){
  2127. $condition[]=["a.apply_id","=",$apply_id];
  2128. }
  2129. $apply_name = isset($this->post['apply_name'])&&$this->post['apply_name']!==""?trim($this->post['apply_name']) :"";
  2130. if($apply_name!==""){
  2131. $condition[]=["a.apply_name","like","%$apply_name%"];
  2132. }
  2133. $status= isset($this->post['status'])&&$this->post['status']!=="" ? intval($this->post['status']):"";
  2134. if($status!==""){
  2135. $condition[]=["a.status","=",$status];
  2136. }
  2137. $invNo = isset($this->post['invNo'])&&$this->post['invNo']!=="" ? trim($this->post['invNo']):"";
  2138. if($invNo!==""){
  2139. $condition[]=["a.invNo","like","%$invNo%"];
  2140. }
  2141. $returnCode = isset($this->post['returnCode'])&&$this->post['returnCode']!=="" ? trim($this->post['returnCode']):"";
  2142. if($returnCode!==""){
  2143. $condition[]=["a.returnCode","like","%$returnCode%"];
  2144. }
  2145. $count=Db::name("invoice_return")->alias("a")
  2146. ->leftJoin("invoice_ticket b","a.invNo=b.invNo and b.type=1")
  2147. ->leftJoin("invoice_pool c","a.invNo=c.invNo")
  2148. ->where($condition)->count();
  2149. $total =ceil($count/$size);
  2150. $page= $page>$total ? intval($total):$page;
  2151. $list=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. ->field("b.*,a.*")->withoutField("pdfstream")->json(["item"])->page($page,$size)->where($condition)->select()
  2155. ->toArray();
  2156. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  2157. }
  2158. /**
  2159. * @return \think\response\Json|void
  2160. * @throws \think\db\exception\DataNotFoundException
  2161. * @throws \think\db\exception\DbException
  2162. * @throws \think\db\exception\ModelNotFoundException
  2163. */
  2164. public function tikcetList(){
  2165. $post=$this->request->only(["invNo"=>"","companyNo"=>"","relaComNo"=>"","page"=>1,"size"=>15],"post","trim");
  2166. $where=[];
  2167. $roleid = $this->roleid;
  2168. $check = checkRole($roleid,'88');
  2169. if($check){
  2170. $where[]=["b.apply_id","=",$this->uid];
  2171. }
  2172. if($post["invNo"]!="") $where[]=["a.invNo","=",$post['invNo']];
  2173. if($post["companyNo"]!="") $where[]=["b.inv_out","=",$post['companyNo']];
  2174. if($post["relaComNo"]!="") $where[]=["b.inv_out","=",$post['relaComNo']];
  2175. $count=Db::name("invoice_ticket")->where($where)->count();
  2176. $total =ceil($count/$post['size']);
  2177. $page= $post['page']>$total ? intval($total):intval($post['page']);
  2178. $list = Db::name("invoice_ticket")
  2179. ->alias("a")
  2180. ->leftJoin("invoice_pool b","a.invNo=b.invNo")
  2181. ->where($where)
  2182. ->field("a.*")
  2183. ->json(["a.item"])
  2184. ->page($page,intval($post['size']))
  2185. ->order("a.id desc")
  2186. ->select()->toArray();
  2187. foreach ($list as &$value){
  2188. $value['invtype_name'] = $this->typename[$value['inv_type']]??"";
  2189. }
  2190. return app_show(0,"获取成功",["list"=>$list,"count"=>$count]);
  2191. }
  2192. /** @param array invNo 发票编号集合
  2193. * @return \think\response\Json|void
  2194. */
  2195. public function downorder(){
  2196. $post=$this->request->only(["invNo"=>[]],"post","trim");
  2197. $condition=[["open_type","in",[0,2,3]],["is_del","=",0],["status","=",1]];
  2198. if(!empty($post['invNo'])){
  2199. $condition[]=['invNo',"in",$post['invNo']];
  2200. }
  2201. $invice = Db::name("invoice_pool")->where($condition)->select()->toArray();
  2202. if (empty($invice)){
  2203. return error_show(1003,"未找到数据");
  2204. }
  2205. $data=[];
  2206. foreach ($invice as $value){
  2207. $temp=[];
  2208. $orderNo = Db::name("assoc")->where([["status","=",1],["is_del","=",0],['viceCode',"=",$value['invNo']]])
  2209. ->find();
  2210. if(!$orderNo){
  2211. continue;
  2212. }
  2213. $good = Db::name("qrd_info")->where([['sequenceNo',"=",$orderNo['orderCode']],["status","=",1]])
  2214. ->findOrEmpty();
  2215. if(empty($good)){
  2216. continue;
  2217. }
  2218. if(isset($value['inv_out'])&& $value['inv_out']!=''){
  2219. $invout = Db::name("company_info")->where([['companyNo',"=",$value['inv_out']]])->find();
  2220. }
  2221. $invinfo = Db::name("invoice_pool_info")->where("invNo","=",$value['invNo'])->findOrEmpty();
  2222. // foreach ($good as $val){
  2223. // $qrd = Db::name("qrd_list")->where("sequenceNo","=",$val['goodNo'])->find();
  2224. $temp['invNo'] = $value['invNo'];
  2225. $temp['qrdNo'] = $orderNo['orderCode'];
  2226. $temp['depart'] = $good['department'];
  2227. $temp['apply_name'] =$value['apply_name'];
  2228. $temp['code'] ="";
  2229. $temp['invoice_title'] =$invinfo['buyer_title']??"";
  2230. $temp['invoice_code'] =$invinfo['buyer_code']." ";
  2231. $temp['invoice_addr'] =$invinfo['buyer_addr']??"";
  2232. $temp['invoice_mobile'] =$invinfo['buyer_mobile']??"";
  2233. $temp['invoice_bank'] =$invinfo['buyer_bank']??"";
  2234. $temp['invoice_bankNo'] =$invinfo['buyer_bankNo']." ";
  2235. $temp['company'] =$good['customerName'];
  2236. $temp['goodname'] =$good['goodName'];
  2237. $temp['tax'] =$good['tax'];
  2238. $temp['inv_name'] =$good['goodName'];
  2239. $temp['diff_weight'] =$good['diff_weight'];
  2240. $temp['diff_price'] =$good['diff_fee'];
  2241. $temp['unit'] =$good['goodUnit'];
  2242. $temp['num'] =$good['goodNum'] - $good['thNum'];
  2243. $temp['price'] =$good['goodPrice'];
  2244. $temp['total_fee'] =$good['totalPrice'];
  2245. $temp['inv_type']=$this->invoiceType[$value['inv_type']]?? "-";
  2246. $temp['remark'] =$value['remark'];
  2247. $temp['kp_companyName'] = $invinfo['seller_title']??"";
  2248. $temp['kp_company_address'] = $invinfo['seller_addr']??"";
  2249. $temp['kp_company_license'] = $invinfo['seller_code']." ";
  2250. $temp['kp_bank_name'] = $invinfo['seller_bank']??"";
  2251. $temp['kp_bankNo'] = $invinfo['seller_bankNo']."";
  2252. $temp['kp_contector'] = $invout['contector']??"";
  2253. $temp['kp_mobile'] = $invinfo['seller_mobile']??"";
  2254. $data[]=$temp;
  2255. }
  2256. // }
  2257. if (empty($data)){
  2258. return error_show(1003,"未找到数据");
  2259. }
  2260. $header=[
  2261. "申请编号",
  2262. "公司订单号",
  2263. "申请部门(所属平台)",
  2264. "申请人",
  2265. "代码",
  2266. "发票开具全称",
  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. $li = excelExport("待开票",$header,$data);
  2293. }
  2294. public function importInv(){
  2295. $param =$this->request->only(["data"=>[]],"post");
  2296. if(empty($param['data']))return error_show(1004,"参数 data 不能为空");
  2297. $total=[];
  2298. $list=[];
  2299. foreach ($param['data'] as $key=>$value){
  2300. if($value['invNo']==""){
  2301. return error_show(1005,"第".($key+1)."行缺少发票申请编号数据");
  2302. }
  2303. if($value["inv_type"]===""){
  2304. return error_show(1005,"第".($key+1)."行缺少发票类型数据");
  2305. }
  2306. if(!in_array($value['inv_type'],$this->invName)) return error_show(1005,"第".($key+1)."行发票类型数据有误");
  2307. if($value["inv_code"]=="" && !in_array($value['inv_type'],[ "fully_digitalized_special_electronic", "fully_digitalized_normal_electronic"])){
  2308. return error_show(1005,"第".($key+1)."行缺少发票代码数据");
  2309. }
  2310. if($value["inv_number"]==""){
  2311. return error_show(1005,"第".($key+1)."行缺少发票号码数据");
  2312. }
  2313. if($value["inv_total"]==""&& 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_subtotal"]==""){
  2317. return error_show(1005,"第".($key+1)."行缺少发票税后金额数据");
  2318. }
  2319. if($value["open_date"]==""){
  2320. return error_show(1005,"第".($key+1)."行缺少发票开票日期数据");
  2321. }
  2322. //电子票校验码必传
  2323. if($value["check_code"]=="" && in_array($value['inv_type'],['normal','roll',"toll",'electronic'])){
  2324. return error_show(1005,"第".($key+1)."行缺少发票校验码数据");
  2325. }
  2326. // if($value['inv_type']==="000"){
  2327. // if($value['seller_id']=="") return error_show(1005,"第".($key+1)."行缺少销售方纳税识别号数据");
  2328. // if($value['seller_title']=="") return error_show(1005,"第".($key+1)."行缺少销售方名称数据");
  2329. // if($value['seller_addr']=="") return error_show(1005,"第".($key+1)."行缺少销售方地址数据");
  2330. // if($value['seller_mobile']=="") return error_show(1005,"第".($key+1)."行缺少销售方联系方式数据");
  2331. // if($value['seller_bank']=="") return error_show(1005,"第".($key+1)."行缺少销售方银行数据");
  2332. // if($value['seller_bankNo']=="") return error_show(1005,"第".($key+1)."行缺少销售方银行账户数据");
  2333. // if($value['buyer_id']=="") return error_show(1005,"第".($key+1)."行缺少购买方纳税识别号数据");
  2334. // if($value['buyer_title']=="") return error_show(1005,"第".($key+1)."行缺少购买方名称数据");
  2335. // if($value['buyer_addr']=="") return error_show(1005,"第".($key+1)."行缺少购买方地址数据");
  2336. // if($value['buyer_mobile']=="") return error_show(1005,"第".($key+1)."行缺少购买方联系方式数据");
  2337. // if($value['buyer_bank']=="") return error_show(1005,"第".($key+1)."行缺少购买方银行数据");
  2338. // if($value['buyer_bankNo']=="") return error_show(1005,"第".($key+1)."行缺少购买方银行账户数据");
  2339. // if($value['tax_fee']=="") return error_show(1005,"第".($key+1)."行缺少发票税额数据");
  2340. // }
  2341. !isset($total[$value['invNo']]) ? $total[$value['invNo']]['total']=0:"";
  2342. $temp=[];
  2343. $temp['invNo'] = $value["invNo"];
  2344. $temp['type'] = 0;
  2345. $temp['inv_type'] =$value['inv_type'];
  2346. $temp['inv_code'] = $value['inv_code'];
  2347. $temp['inv_number'] = $value['inv_number'];
  2348. $temp['inv_subtotal'] = $value['inv_subtotal'];
  2349. $temp['inv_total'] = $value['inv_total'];
  2350. $temp['check_code'] = $value['check_code']??"";
  2351. $temp['seller_id'] = $value['seller_id']??"";
  2352. $temp['seller_title'] = $value['seller_title']??"";
  2353. $temp['seller_addr'] = $value['seller_addr']??"";
  2354. $temp['seller_mobile'] = $value['seller_mobile']??"";
  2355. $temp['seller_bank'] = $value['seller_bank']??"";
  2356. $temp['seller_bankNo'] = $value['seller_bankNo']??"";
  2357. $temp['buyer_id'] = $value['buyer_id']??"";
  2358. $temp['buyer_title'] = $value['buyer_title']??"";
  2359. $temp['buyer_addr'] = $value['buyer_addr']??"";
  2360. $temp['buyer_mobile'] = $value['buyer_mobile']??"";
  2361. $temp['buyer_bank'] = $value['buyer_bank']??"";
  2362. $temp['buyer_bankNo'] = $value['buyer_bankNo']??"";
  2363. $temp['tax_fee'] = $value['tax_fee']??"";
  2364. $temp['item'] = '';
  2365. $temp['pdfstream'] = '';
  2366. $temp['open_date'] = date("Y-m-d H:i:s",strtotime($value['open_date']));
  2367. $temp['status'] = 0;
  2368. $temp['is_del'] = 0;
  2369. $temp['addtime'] = date("Y-m-d H:i:s");
  2370. $temp['updatetime'] = date("Y-m-d H:i:s");
  2371. $total[$value["invNo"]]=$temp;
  2372. }
  2373. Db::startTrans();
  2374. try{
  2375. $keyarr =array_keys($total);
  2376. $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");
  2377. foreach ($total as $key=>$val){
  2378. if(!isset($inpool[$key])) throw new Exception("{$key}发票数据不存在或状态有误");
  2379. if(in_array($val['inv_type'],["fully_digitalized_special_electronic", "fully_digitalized_normal_electronic"]) && $inpool[$key]['winv_fee']!=$val['total']){
  2380. throw new Exception("{$key}全电发票面金额小于未开票金额");
  2381. }
  2382. if($inpool[$key]['inv_type']!= $val['inv_type']) throw new Exception("{$key}发票类型有误");
  2383. $update=[
  2384. "status"=>in_array($val['inv_type'],["fully_digitalized_special_electronic", "fully_digitalized_normal_electronic"])?4:3,
  2385. "open_type"=>$inpool[$key]['open_type']??3,
  2386. "updatetime"=>date("Y-m-d H:i:s")
  2387. ];
  2388. $up =Db::name("invoice_pool")->where([["invNo","=",$key],["is_del","=",0]])->save($update);
  2389. if(!$up){
  2390. Db::rollback();
  2391. return error_show(1005,"发票状态更新失败");
  2392. }
  2393. if($update['status']==4){
  2394. $qrdArr=Db::name("assoc")->where(["viceCode"=>$key,"is_del"=>0])->column("id,orderCode,cancel_fee");
  2395. if(!empty($qrdArr)){
  2396. foreach ($qrdArr as $value){
  2397. $qrdinfo =Db::name("qrd_info")->where(["sequenceNo"=>$value['orderCode']])->find();
  2398. if($qrdinfo==false){
  2399. Db::rollback();
  2400. return error_show(1003,"确认单信息未找到");
  2401. }
  2402. if($qrdinfo['inv_fee']<$value['cancel_fee']){
  2403. Db::rollback();
  2404. return error_show(1003,"确认单信息开票金额不足");
  2405. }
  2406. $update =[
  2407. "ainv_fee"=>$qrdinfo['ainv_fee']+$value['cancel_fee'],
  2408. "inv_fee"=>$qrdinfo['inv_fee']-$value['cancel_fee'],
  2409. "invtime"=>date("Y-m-d H:i:s"),
  2410. "inv_status"=>$qrdinfo['winv_fee']==0 &&($qrdinfo['inv_fee']-$value['cancel_fee'])==0 ? 3 : 2,
  2411. "updatetime"=>date("Y-m-d H:i:s"),
  2412. ];
  2413. $qrdup = Db::name("qrd_info")->where($qrdinfo)->update($update);
  2414. if($qrdup==false){
  2415. Db::rollback();
  2416. return error_show(1003,"确认单信息更新失败");
  2417. }
  2418. $assoc=["status"=>2,"assoc_time"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
  2419. $assocup =Db::name("assoc")->where($value)->update($assoc);
  2420. if($assocup==false){
  2421. Db::rollback();
  2422. return error_show(1003,"确认单关联信息更新失败");
  2423. }
  2424. }
  2425. }
  2426. }
  2427. $val['inv_type'] = $this->kingInvoice[$val['inv_type']];
  2428. $list[]=$val;
  2429. }
  2430. $inticket =Db::name("invoice_ticket")->insertAll($list);
  2431. if(!$inticket){
  2432. Db::rollback();
  2433. return error_show(1005,"发票数据录入失败");
  2434. }
  2435. Db::commit();
  2436. return app_show(0,"发票数据录入成功");
  2437. }catch (\Exception $e){
  2438. Db::rollback();
  2439. return error_show(1008,$e->getMessage());
  2440. }
  2441. }
  2442. }