OrderInv.php 109 KB

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