OrderInv.php 110 KB

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