/// <summary> /// 根据保存的来源类型处理 /// 例如:如果是从模板生成触发,期末检查模板要关联生成的凭证 /// </summary> /// <param name="request"></param> /// <param name="ts"></param> /// <param name="user"></param> private void DealByPZType(RequestDocAdd request, IDbTransaction ts, MM_UserInfo user) { if (request.Type == PZType.QM.ToString())//期末 { AccountSetBLL account = new AccountSetBLL(cnn); var period = account.GetActivePeriod(user.AccountId, ts); TKS_FAS_TPL2PZ newData = new TKS_FAS_TPL2PZ(); newData.Id = Guid.NewGuid().ToString(); newData.TPLId = request.TPLId; newData.PZId = request.Head.Id; newData.AccountId = user.AccountId; newData.CreateUser = user.User.UserName; newData.CreateDate = DateTime.Now; newData.PeriodId = period.Id; cnn.Insert <TKS_FAS_TPL2PZ>(newData, ts); } else if (request.Type == PZType.FP.ToString()) { InvoiceBLL bll = new FAS.InvoiceBLL(cnn); bll.LZ(request.Key, request.Head.Id, user, ts); } else if (request.Type == PZType.CHANGE.ToString()) { PZZBLL pzz = new PZZBLL(cnn); var pzzData = pzz.PZZGetById(request.Head.PZZ, ts); FixedAssetsBLL fixedAssetsBLL = new FixedAssetsBLL(cnn); fixedAssetsBLL.ChangeSetPZ(request.Head.Id, pzzData.PZZ + request.Head.PZZNO, request.Key, ts); fixedAssetsBLL.SetIsGenPZ(request.Key, user, ts); } else if (request.Type == PZType.GD.ToString()) { AccountSetBLL account = new AccountSetBLL(cnn); var period = account.GetActivePeriod(user.AccountId, ts); TKS_FAS_TPL2PZ newData = new TKS_FAS_TPL2PZ(); newData.Id = Guid.NewGuid().ToString(); newData.TPLId = request.TPLId; newData.PZId = request.Head.Id; newData.AccountId = user.AccountId; newData.CreateUser = user.User.UserName; newData.CreateDate = DateTime.Now; newData.PeriodId = period.Id; cnn.Insert <TKS_FAS_TPL2PZ>(newData, ts); //更新固定资产 FixedAssetsBLL fixedAssetsBLL = new FixedAssetsBLL(cnn); fixedAssetsBLL.Depreciation(user.AccountId, ts, period); } }
/// <summary> /// 模板加载 /// </summary> /// <param name="request"></param> /// <returns></returns> public ResponseTPLMGet TPLGet(RequestTPLMGet request) { ResponseTPLMGet response = new ResponseTPLMGet(); using (cnn = GetConnection()) { var ts = cnn.BeginTransaction(); try { var user = UserInfoGet(request.Token, ts); var head = cnn.QuerySingle <TKS_FAS_DocTPL>("select * from TKS_FAS_DocTPL where id=@Id", new { Id = request.Data.Id }, ts); AccountSetBLL account = new AccountSetBLL(cnn); var period = account.GetActivePeriod(user.AccountId, ts); //模板体明细 var detail = cnn.Query <TKS_FAS_DocDetailTPL>( @"select * from tks_fas_docDetailTPL where parentId=@ParentId", new { ParentId = head.Id }, ts).ToList(); //如果是固定资产,则需要捞取当前账套的所有固定资产的每月折旧额,生成计提折旧凭证 //需要对detail进行再包装 if (head.TPLTarget == 3) { #region 固定资产,计提折旧 FixedAssetsBLL fbll = new FixedAssetsBLL(cnn); //获取固定资产 List <TKS_FAS_FixedAssets> fixedAssets = fbll.FixedAssetsGetByAcccount(user.AccountId, period, ts); //凭证明细 List <TKS_FAS_DocDetailTPL> newDetail = new List <TKS_FAS_DocDetailTPL>(); head.AMT_DBT = 0; foreach (var item in fixedAssets) { if (item.DepreciationMethod == "2") { continue; } if (item.IsStartPeriodDepreciation == 0 && item.StartPeriod == period.Id) { continue; } if (item.DpreMonth < item.PreUseMonth) { //固定资产累计折旧科目,产生到凭证 //用固定资产表单上的科目覆盖模板的科目 for (var i = 0; i < detail.Count; i++) { //update by Hero.Zhang 每次循环前必须将借贷金额初始化 detail[i].Money_Credit = 0; detail[i].Money_Debit = 0; if (detail[i].Credit_Debit == 1) { detail[i].SubjectCode = item.ADSubjectCode; //detail[i].SubjectDescription = item.ADSubjectCode + " " + item.ADSubjectName; detail[i].SubjectDescription = item.ADSubjectName; } else if (detail[i].Credit_Debit == 0) { detail[i].SubjectCode = item.DCostSubjectCode; detail[i].SubjectDescription = item.DCostSubjectName; } } //填充金额 var d = FillDetail(item.DprePerMonth, detail); newDetail.AddRange(d); head.AMT_DBT += item.DprePerMonth; //if (item.ADSubjectCode == "1502001") //{ // test.Add(item); //} } } //分组合并 var lst = newDetail.GroupBy(x => new { x.Id, x.ParentId, x.LineNo, x.AccountId, x.Year, x.SubjectCode, x.SubjectDescription, x.Summary, x.Credit_Debit, x.PartnerCode, x.Unit, x.CurrencyCode, x.CalItem1, x.CalValue1, x.CalItem2, x.CalValue2, x.CalItem3, x.CalValue3, x.CalItem4, x.CalValue4, x.CalItem5, x.CalValue5, x.Rate, x.Quantity, x.Price, x.IsCurrency, x.IsQuantity, x.IsCalHelper, x.Balance, x.YB, x.IPercent, x.SourceType }).Select(x => new TKS_FAS_DocDetailTPL { Id = x.First().Id, ParentId = x.First().ParentId, LineNo = x.First().LineNo, AccountId = x.First().AccountId, Year = x.First().Year, SubjectCode = x.First().SubjectCode, SubjectDescription = x.First().SubjectDescription, Summary = x.First().Summary, Credit_Debit = x.First().Credit_Debit, PartnerCode = x.First().PartnerCode, Unit = x.First().Unit, CurrencyCode = x.First().CurrencyCode, CalItem1 = x.First().CalItem1, CalValue1 = x.First().CalValue1, CalItem2 = x.First().CalItem2, CalValue2 = x.First().CalValue2, CalItem3 = x.First().CalItem3, CalValue3 = x.First().CalValue3, CalItem4 = x.First().CalItem4, CalValue4 = x.First().CalValue4, CalItem5 = x.First().CalItem5, CalValue5 = x.First().CalValue5, Rate = x.First().Rate, Quantity = x.First().Quantity, Price = x.First().Price, IsCurrency = x.First().IsCurrency, IsQuantity = x.First().IsQuantity, IsCalHelper = x.First().IsCalHelper, Balance = x.First().Balance, YB = x.First().YB, IPercent = x.First().IPercent, SourceType = x.First().SourceType, Money_Credit = x.Sum(s => s.Money_Credit), Money_Debit = x.Sum(s => s.Money_Debit) }).ToList(); //detail = newDetail; detail = lst; #endregion } else if (head.TPLTarget == 7) { #region 固定资产变动单类型 var changeTypeToTpl = cnn.QuerySingleOrDefault <TKS_FAS_FixedAssetsChange>( "select * from TKS_FAS_FixedAssetsChange where Id=@Id ", new { Id = request.Key }, ts); if (changeTypeToTpl != null) { //固定资产变动类型新增的情况 string fixsql = @"select B.* from TKS_FAS_FixedAssetsChange A left join TKS_FAS_FixedAssets B on A.parentId=B.id where A.id=@ID"; TKS_FAS_FixedAssets fixedAssets = cnn.QuerySingleOrDefault <TKS_FAS_FixedAssets>(fixsql, new { ID = request.Key }, ts); if (fixedAssets == null) { throw new Exception("固定资产不存在"); } FillDetail(request.Money, detail); if (changeTypeToTpl.ChangeType == 1) { #region 新增 for (var i = 0; i < detail.Count; i++) { if (detail[i].SubjectCode == "1501") { detail[i].SubjectCode = fixedAssets.GDCode; detail[i].SubjectDescription = fixedAssets.GDName; } else if (detail[i].SubjectCode == "217100101") { if (detail[i].Credit_Debit == 0) { detail[i].Money_Debit = fixedAssets.InputVAT; } else { detail[i].Money_Credit = fixedAssets.InputVAT; } } else if (detail[i].SubjectCode == "1002") { if (detail[i].Credit_Debit == 0) { detail[i].Money_Debit = request.Money + fixedAssets.InputVAT; } else { detail[i].Money_Credit = request.Money + fixedAssets.InputVAT; } } } request.Money += fixedAssets.InputVAT; #endregion } else if (changeTypeToTpl.ChangeType == 8) { #region 报废 for (var i = 0; i < detail.Count; i++) { if (detail[i].SubjectCode == "1501") { if (detail[i].Credit_Debit == 0) { detail[i].Money_Debit = fixedAssets.InitialAssetValue; } else { detail[i].Money_Credit = fixedAssets.InitialAssetValue; } } else if (detail[i].SubjectCode == "1502") { if (detail[i].Credit_Debit == 0) { detail[i].Money_Debit = fixedAssets.AccumulativeDpre + fixedAssets.DprePerMonth;//累计折旧+每月折旧额 } else { detail[i].Money_Credit = fixedAssets.AccumulativeDpre + fixedAssets.DprePerMonth; } } else if (detail[i].SubjectCode == "1701") { if (detail[i].Credit_Debit == 0) { detail[i].Money_Debit = fixedAssets.InitialAssetValue - fixedAssets.AccumulativeDpre - fixedAssets.DprePerMonth;//减去累计折旧和每月折旧额 } else { detail[i].Money_Credit = fixedAssets.InitialAssetValue - fixedAssets.AccumulativeDpre - fixedAssets.DprePerMonth; } } } request.Money = fixedAssets.InitialAssetValue; #endregion } } else { FillDetail(request.Money, detail); } #endregion } else if (head.TPLTarget == 2) { #region 发票上传 //var fp = cnn.Query<TKS_FAS_Invoice>( // "select * from TKS_FAS_Invoice where id=@Id", new { Id =request.Data.Id}, ts); //if (fp == null) //{ // throw new NormalException("未找到对应发票"); //} FillDetail(request.Money, detail); decimal total = 0; for (var i = 0; i < detail.Count; i++) { if (detail[i].SourceType == 0) { //发生额比例,不做处理 } else if (detail[i].SourceType == 1) { //含税金额 if (detail[i].Credit_Debit == 0) { detail[i].Money_Debit = request.Money; } else { detail[i].Money_Credit = request.Money; } } else if (detail[i].SourceType == 2) { //税金 if (detail[i].Credit_Debit == 0) { detail[i].Money_Debit = request.Tax; } else { detail[i].Money_Credit = request.Tax; } } else if (detail[i].SourceType == 3) { //未税金额 if (detail[i].Credit_Debit == 0) { detail[i].Money_Debit = request.Money - request.Tax; } else { detail[i].Money_Credit = request.Money - request.Tax; } } total += detail[i].Money_Debit; } request.Money = total; #endregion } else { FillDetail(request.Money, detail); } if (request.Money > 0) { head.AMT_DBT = request.Money; } ts.Commit(); response.IsSuccess = true; response.Message = "加载完毕"; response.Head = head; response.Detail = detail; #region add by Hero.Zhang 判断科目行是否有子科目 for (int i = 0; i < detail.Count(); i++) { var item = detail[i]; List <TKS_FAS_AccountSubject> sub = cnn.Query <TKS_FAS_AccountSubject>(@"select * from TKS_FAS_AccountSubject where AccountId=@AccountId and RootCode=@RootCode", new { AccountId = user.AccountId, RootCode = item.SubjectCode }, ts).ToList(); if (sub.Count > 1) { response.CheckParent = "1"; response.Message += "</br>第" + (i + 1).ToString() + "行有子科目,请重新选择"; } } #endregion return(response); } catch (Exception ex) { ts.Rollback(); return(this.DealException(response, ex) as ResponseTPLMGet); } } }
public ResponseDocDelete DocDelete(RequestDocDelete request) { ResponseDocDelete response = new ResponseDocDelete(); using (cnn = GetConnection()) { var ts = cnn.BeginTransaction(); try { BalanceBLL balanceBLL = new BalanceBLL(cnn); var user = this.UserInfoGet(request.Token, ts); FixedAssetsBLL fixBLL = new FixedAssetsBLL(cnn); var flag = fixBLL.IsDocHasChangeRecord(request.Data.Id, ts); if (flag) { //取消此判断 update by Hero.Zhang 20180404 //throw new NormalException("该凭证通过固定资产生成,无法删除"); } string sql = @"select * from tks_fas_doc where id=@Id"; var DocDate = cnn.Query(sql, new { Id = request.Data.Id }, ts); if (DocDate.Count() <= 0) { throw new NormalException("该凭证已经删除"); } var data = cnn.QueryFirst <TKS_FAS_Doc>(sql, new { Id = request.Data.Id }, ts); PeriodBLL period = new PeriodBLL(cnn); flag = period.IsPeriodPaid(data.PeriodId, ts); if (flag) { throw new NormalException("该凭证已经期末结转,无法删除"); } #region 先回滚余额表 Hero.Zhang List <TKS_FAS_DocDetail> Detail = cnn.Query <TKS_FAS_DocDetail>("select * from TKS_FAS_DocDetail where parentId=@ParentId", new { ParentId = request.Data.Id }, ts).ToList(); var doc = cnn.QueryFirst <TKS_FAS_Doc>("select * from TKS_FAS_Doc where id=@Id", new { Id = request.Data.Id }, ts); RequestDocAdd oldDoc = new RequestDocAdd(); oldDoc.Head = doc; oldDoc.Detail = Detail; foreach (var det in oldDoc.Detail) { det.Money_Debit = -1 * det.Money_Debit; det.Money_Credit = -1 * det.Money_Credit; det.Quantity = -1 * det.Quantity; } balanceBLL.UpdateBalance(oldDoc, ts, user); #endregion sql = @" delete from TKS_FAS_Doc where id=@Id"; cnn.Execute(sql, request.Data, ts); sql = "delete from TKS_FAS_DocDetail where parentId=@ParentId"; cnn.Execute(sql, new { ParentId = request.Data.Id }, ts); //发票状态置为 递交财务 sql = "update tks_fas_invoice set status=1 where pzId=@PZId and AccountId=@AccountId"; cnn.Execute(sql, new { PZId = data.Id, AccountId = data.AccountId }, ts); sql = "delete from TKS_FAS_TPL2PZ where pzId=@PZID and AccountId=@AccountId "; cnn.Execute(sql, new { PZID = data.Id, AccountId = data.AccountId }, ts); // add by Hero.Zhang 20180404 /*判断次凭证号是否在变更记录及生成凭证表TKS_FAS_FixedAssetsChange中,如果存在,则 * 将这笔数据还原成新增并且没有生成凭证的状态;同时将固定资产表TKS_FAS_FixedAssets中这笔固定资产变更为‘未生成凭证状态’,同时将折旧相关数据回冲一个月*/ var check = "select * from TKS_FAS_FixedAssetsChange where docid=@docid and AccountId=@AccountId"; var datacheck = cnn.Query(check, new { docid = data.Id, AccountId = data.AccountId }, ts); if (datacheck.Count() > 0) { var dataUpdate = cnn.QueryFirst <TKS_FAS_FixedAssetsChange>(check, new { docid = data.Id, AccountId = data.AccountId }, ts); if (dataUpdate != null) { sql = "update TKS_FAS_FixedAssetsChange set ChangeType=1,DocId='',DocPZZ='' where id=@id and AccountId=@AccountId"; //cnn.Execute(sql, new { id = request.TKS_FAS_FixedAssetsChange_Id, AccountId = data.AccountId }, ts); cnn.Execute(sql, new { id = dataUpdate.Id, AccountId = data.AccountId }, ts); sql = "update TKS_FAS_FixedAssets set IsGenPZ=0 where id=@id and AccountId=@AccountId"; //cnn.Execute(sql, new { id = request.TKS_FAS_FixedAssets_Id, AccountId = data.AccountId }, ts); cnn.Execute(sql, new { id = dataUpdate.ParentId, AccountId = data.AccountId }, ts); } } //add by Hero.Zhang 20180903 删除计提折旧凭证,回滚固定资产计提折旧数据 if (data.Source == PZType.GD.ToString()) { //add by Hero.Zhang 删除折旧凭证判断,如果改凭证的日期之后有折旧凭证,则不允许删除 var checkPZ = @"select * from tks_fas_doc where source='GD' and PZDate>@PZDate AND AccountId=@AccountId"; var PZcheck = cnn.Query(checkPZ, new { PZDate = data.PZDate, AccountId = data.AccountId }, ts); if (PZcheck.Count() > 0) { throw new NormalException("该计提折旧凭证无法删除,请先删除下一个期间的计提折旧凭证"); } //回滚固定资产 fixBLL.RollbackFixedAssets(user.AccountId, data.PeriodId, ts); } ts.Commit(); response.IsSuccess = true; response.Message = "删除成功"; return(response); } catch (Exception ex) { ts.Rollback(); return(this.DealException(response, ex) as ResponseDocDelete); } } }