OrderInv.php 134 KB

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