OrderInv.php 112 KB

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