Beispiel #1
0
        public bool AddCostAccountBeforeSplit(string poCode = "", string period = null)
        {
            //计入日期
            if (string.IsNullOrEmpty(period) || period.Length != 7)
            {
                period = DateTime.Now.AddMonths(-1).ToString("yyyy-MM");
            }


            #region 获取范围数据

            Expression <Func <PurchaseOrderLineConfirmHistory, bool> >          histExp = s => s.Status == 0 && s.Period == period;
            Expression <Func <PurchaseRequirementDeptShareRatioVersion, bool> > verExp  = s =>
                                                                                          s.Status == 0 && s.Period == period;
            if (!string.IsNullOrEmpty(poCode))
            {
                histExp = histExp.And(s => s.POCode == poCode);
                verExp  = verExp.And(s => s.POCode == poCode);
            }

            var historys = _polchRepository.Where(histExp);
            var versions = _purchaseRequirementDeptShareRatioVersionRepository.Where(verExp);
            #endregion

            var configs  = _costAccountConfigRepository.Where(s => s.Status == 0);
            var dicInfos = _commonDataService.GetDicInfos();

            //1.历史记录表
            var models = new List <PurchaseOrderLineConfirmHistory>();
            foreach (var confirmHistory in historys)
            {
                var count = versions.Count(s => s.POCode == confirmHistory.POCode && s.POLineNum == confirmHistory.POLineNum);
                if (count > 0)
                {
                    int     i         = 1;
                    decimal sum       = 0;
                    decimal allAmount = confirmHistory.Amount;
                    foreach (var ver in versions.Where(s => s.POCode == confirmHistory.POCode && s.POLineNum == confirmHistory.POLineNum))
                    {
                        var toDepartId = ver.DepartID;
                        var toRate     = ver.ShareProportion * (decimal)0.01;

                        var tem = Newtonsoft.Json.JsonConvert.DeserializeObject <PurchaseOrderLineConfirmHistory>(Newtonsoft.Json.JsonConvert.SerializeObject(confirmHistory));// JsonConvert (confirmHistory);
                        tem.DepartID       = toDepartId;
                        tem.DepartNamePath = ver.DepartNamePath;
                        tem.Amount         = i == count ? allAmount - sum : decimal.Round(confirmHistory.Amount * toRate, 2);//满足总和一致

                        tem.Ratio    = toRate;
                        tem.Formula += i == count ? $"拆分前:({allAmount}-{sum})---->" : $"拆分前:({confirmHistory.Amount}*{toRate})---->";

                        models.Add(tem);
                        i++;
                        sum += tem.Amount;
                    }
                }
                else
                {
                    models.Add(confirmHistory);
                }
            }

            var costAccounts = new List <CostAccount>();
            foreach (var item in models)
            {
                var tem = configs.Where(s => (s.CompanyCode == item.CompanyCode || s.CompanyCode == "-99") &&
                                        s.ContentType == item.ContentType &&
                                        s.ProductCat1 == item.MaterialClassCode &&
                                        (s.ProductCat2 == item.MaterialCode || s.ProductCat2 == "-99")).ToList();
                var costAccountConfig = tem.OrderByDescending(s => s.CompanyCode).ThenByDescending(s => s.ProductCat2).FirstOrDefault() ?? new CostAccountConfig();
                var ca = new CostAccount
                {
                    POCode              = item.POCode,
                    POLineNum           = item.POLineNum,
                    DepartID            = item.DepartID,
                    DepathNamePath      = item.DepartNamePath,
                    Period              = item.Period,
                    Amount              = item.Amount,
                    MaterialClassCode   = item.MaterialClassCode,
                    MaterialClassName   = item.MaterialClassName,
                    MaterialCode        = item.MaterialCode,
                    MaterialName        = item.MaterialName,
                    ExpenseItem         = item.ExpenseItem,
                    ExpenseItemName     = item.ExpenseItemName,
                    ProjectCode         = item.ProjectCode,
                    ProjectName         = item.ProjectName,
                    CompanyCode         = item.CompanyCode,
                    CompanyName         = item.CompanyName,
                    SupplierID          = item.SupplierID,
                    SupplierName        = item.SupplierName,
                    ContentType         = item.ContentType,
                    ContentTypeName     = item.ContentTypeName,
                    CostAccountConfigID = costAccountConfig.Id,
                    SubjectCode         = costAccountConfig.PayOutType1,
                    SubjectName         = costAccountConfig.PayOutType1 == 0?null:dicInfos.FirstOrDefault(s => s.DictId == costAccountConfig.PayOutType1)?.DictName,
                    SecondSubjectCode   = costAccountConfig.PayOutType2,
                    SecondSubjetName    = costAccountConfig.PayOutType2 == 0 ? null : costAccountConfig.PayOutType2 == -99?"缺省" :dicInfos.FirstOrDefault(s => s.DictId == costAccountConfig.PayOutType2)?.DictName,
                    Status              = 0,
                    CreateTime          = DateTime.Now,

                    Ratio   = item.Ratio,
                    Step    = (int)CommonEnum.EnumCostAccountSplitStep.BeforeSplit,
                    Formula = string.IsNullOrEmpty(item.Formula) ? "拆分前:计入---->" : item.Formula,


                    PRCode           = item.PRCode,
                    PRDepartID       = item.PRDepartID,
                    PRDepartNamePath = item.PRDepartNamePath,
                    POCreateTime     = item.POCreateTime,
                    POLineAmount     = item.POLineAmount,
                    POLineBeginTime  = item.POLineBeginTime,
                    POLineEndTime    = item.POLineEndTime,
                    CustID           = item.CustID,
                    CustName         = item.CustName,
                    OrderEnum        = item.OrderEnum,
                    OrderEnumName    = item.OrderEnumName,
                    TaxCode          = item.TaxCode
                };
                costAccounts.Add(ca);
            }



            using (var uow = _uowCrm.Create())
            {
                //更新状态
                Expression <Func <CostAccount, bool> > delExpression = s => s.Status == 0 && s.Period == period;
                if (!string.IsNullOrEmpty(poCode))
                {
                    delExpression = delExpression.And(s => s.POCode == poCode);
                }
                var cc = _costAccountRepository.UpdateWhere(delExpression, new CostAccount()
                {
                    Status = -1, LastUpdateTime = DateTime.Now
                }, t => t.Status, t => t.LastUpdateTime);
                var num = _costAccountRepository.Add(costAccounts);
                uow.SaveChanges();
                return(true);
            }
        }
        /// <summary>
        /// Creates credits and debits and add them to the SelectedBooking item
        /// </summary>
        /// <param name="grossNetType"></param>
        /// <param name="bookingType"></param>
        /// <param name="amount"></param>
        /// <param name="costAccountCreditor"></param>
        /// <param name="costAccountDebitor"></param>
        /// <param name="taxType"></param>
        /// <returns></returns>
        public bool CreateAndAddCreditDebit(GrossNetType grossNetType, BookingType bookingType, decimal amount, CostAccount costAccountCreditor, CostAccount costAccountDebitor, TaxType taxType)
        {
            if (costAccountCreditor == null || costAccountDebitor == null)
            {
                return(false);
            }

            CalculateTax(grossNetType, taxType, amount, out decimal tax, out decimal amountWithoutTax);

            Debit  debit  = new Debit();
            Credit credit = new Credit();

            TaxType nonTax = TaxTypes.GetById(1);

            if (bookingType == BookingType.Invoice)
            {
                if (taxType.Description.IndexOf("Umsatzsteuer", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    SelectedBooking.Credits.AddRange(CreateCredits(grossNetType, taxType, amount, costAccountCreditor));
                    SelectedBooking.Debits.AddRange(CreateDebits(grossNetType, nonTax, tax + amountWithoutTax, costAccountDebitor));
                }
                //if (taxType.Description.IndexOf("Vorsteuer", StringComparison.OrdinalIgnoreCase) >= 0)
                else
                {
                    SelectedBooking.Credits.AddRange(CreateCredits(grossNetType, nonTax, tax + amountWithoutTax, costAccountCreditor));
                    SelectedBooking.Debits.AddRange(CreateDebits(grossNetType, taxType, amount, costAccountDebitor));
                }
            }
            else if (bookingType == BookingType.CreditAdvice)
            {
                // ToDo Check with Tobias !!!

                if (taxType.Description.IndexOf("Umsatzsteuer", StringComparison.OrdinalIgnoreCase) >= 0)
                {
                    SelectedBooking.Credits.AddRange(CreateCredits(grossNetType, taxType, amount, costAccountCreditor));
                    SelectedBooking.Debits.AddRange(CreateDebits(grossNetType, nonTax, amount * (-1), costAccountDebitor));
                }
                //else (taxType.Description.IndexOf("Vorsteuer", StringComparison.OrdinalIgnoreCase) >= 0)
                else
                {
                    SelectedBooking.Credits.AddRange(CreateCredits(grossNetType, nonTax, amount * (-1), costAccountCreditor));
                    SelectedBooking.Debits.AddRange(CreateDebits(grossNetType, taxType, amount, costAccountDebitor));
                }
            }

            return(true);
        }
 public static bool Update(CostAccount CostAccount)
 {
     return(WebApi <bool> .PutAsync(controllerName, CostAccount, "Put").Result);
 }
        /// <summary>
        /// Creates credit items and return them
        /// </summary>
        /// <param name="grossNetType"></param>
        /// <param name="taxType"></param>
        /// <param name="amount"></param>
        /// <param name="costAccount"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        public List <Credit> CreateCredits(GrossNetType grossNetType, TaxType taxType, decimal amount, CostAccount costAccount, string description = "")
        {
            if (costAccount == null)
            {
                throw new Exception("Bitte Kontenrahmen angeben.");
            }

            List <Credit> credits = new List <Credit>();
            Credit        credit  = new Credit(amount, costAccount.CostAccountId, 0);

            credit.CostAccount = costAccount;

            CalculateTax(grossNetType, taxType, amount, out decimal tax, out decimal amountWithoutTax);

            credit.CreditId = tempCreditDebitId;

            if (tax > 0)
            {
                Credit creditTax = new Credit(tax, taxType.RefCostAccount, 0);
                creditTax.CostAccount = CostAccounts.GetById(taxType.RefCostAccount);
                creditTax.RefCreditId = tempCreditDebitId;
                creditTax.CreditId    = ++tempCreditDebitId;
                creditTax.IsTax       = true;
                credits.Add(creditTax);
            }

            credit.Amount      = amountWithoutTax;
            credit.Description = description;
            credits.Add(credit);
            tempCreditDebitId++;

            return(credits);
        }
 public static int Insert(CostAccount CostAccount)
 {
     return(WebApi <int> .PostAsync(controllerName, CostAccount, "SinglePost").Result);
 }