OrderInv.php 133 KB

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