OrderInv.php 109 KB

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