OrderInv.php 109 KB

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