public int Update(int id, FormDto form)
        {
            var model = _dbContext.VBRealizationDocuments.FirstOrDefault(entity => entity.Id == id);
            model.UpdatePosition(VBRealizationPosition.Purchasing, _identityService.Username, UserAgent);
            model.SetRemark(form.Remark);

            if (form.VBRequestDocument != null && form.VBRequestDocument.Id.GetValueOrDefault() > 0)
            {
                var vbRequest = _dbContext.VBRequestDocuments.FirstOrDefault(entity => entity.Id == form.VBRequestDocument.Id.GetValueOrDefault());
                model.Update(vbRequest);
                EntityExtension.FlagForUpdate(model, _identityService.Username, UserAgent);
            }

            var httpClientService = _serviceProvider.GetService<IHttpClientService>();

            var items = _dbContext.VBRealizationDocumentExpenditureItems.Where(entity => entity.VBRealizationDocumentId == id).ToList();
            items = items.Select(element =>
            {
                EntityExtension.FlagForDelete(element, _identityService.Username, UserAgent);
                var result = httpClientService.PutAsync($"{APIEndpoint.Purchasing}vb-request-po-external/spb/{element.UnitPaymentOrderId}?division={model.SuppliantDivisionName}", new StringContent("{}", Encoding.UTF8, General.JsonMediaType)).Result;
                return element;
            }).ToList();
            _dbContext.VBRealizationDocumentExpenditureItems.UpdateRange(items);

            var details = _dbContext.VBRealizationDocumentUnitCostsItems.Where(entity => entity.VBRealizationDocumentId == id).ToList();
            details = details.Select(element =>
            {
                EntityExtension.FlagForDelete(element, _identityService.Username, UserAgent);
                return element;
            }).ToList();
            _dbContext.VBRealizationDocumentUnitCostsItems.UpdateRange(details);

            AddItems(id, form.Items, form.SuppliantUnit.Division.Name);
            AddUnitCosts(model.Id, form.Items.SelectMany(element => element.UnitPaymentOrder.UnitCosts).ToList());

            return id;
        }
예제 #2
0
        public int Create(FormDto form)
        {
            var model = new VBRealizationDocumentModel();

            string division     = GetDivision(form);
            var    unitCode     = GetDocumentUnitCode(division, form.IsInklaring);
            var    existingData = _dbContext.VBRealizationDocuments
                                  .Where(a => a.Date.AddHours(_identityService.TimezoneOffset).Month == form.Date.GetValueOrDefault().AddHours(_identityService.TimezoneOffset).Month &&
                                         a.DocumentNo.StartsWith(unitCode))
                                  .OrderByDescending(s => s.Index)
                                  .FirstOrDefault();
            var documentNo = GetDocumentNo(form, existingData);

            var amount = form.Items.Sum(element =>
            {
                var nominal          = element.UnitPaymentOrder.Amount.GetValueOrDefault();
                var vatNominal       = (decimal)0.0;
                var incomeTaxNominal = (decimal)0.0;
                if (element.UnitPaymentOrder.UseVat.GetValueOrDefault())
                {
                    vatNominal = element.UnitPaymentOrder.Amount.GetValueOrDefault() * (decimal)0.1;
                }


                if (element.UnitPaymentOrder.UseIncomeTax.GetValueOrDefault() && element.UnitPaymentOrder.IncomeTaxBy.ToUpper() == "SUPPLIER")
                {
                    incomeTaxNominal = element.UnitPaymentOrder.Amount.GetValueOrDefault() * (decimal)element.UnitPaymentOrder.IncomeTax.Rate.GetValueOrDefault() / 100;
                }


                return(nominal + vatNominal - incomeTaxNominal);
            });

            if (form.Type == "Tanpa Nomor VB")
            {
                model = new VBRealizationDocumentModel(form.Currency, form.Date, form.SuppliantUnit, documentNo, (decimal)amount, form.Remark);
            }
            else
            {
                var vbRequest = _dbContext.VBRequestDocuments.FirstOrDefault(entity => entity.Id == form.VBRequestDocument.Id.GetValueOrDefault());

                if (vbRequest != null)
                {
                    vbRequest.SetIsRealized(true, _identityService.Username, UserAgent);
                    _dbContext.VBRequestDocuments.Update(vbRequest);
                }

                model = new VBRealizationDocumentModel(form.Date, vbRequest, documentNo, (decimal)amount, form.Remark);
            }

            EntityExtension.FlagForCreate(model, _identityService.Username, UserAgent);
            _dbContext.VBRealizationDocuments.Add(model);
            _dbContext.SaveChanges();

            AddItems(model.Id, form.Items, model.SuppliantDivisionName);

            AddUnitCosts(model.Id, form.Items.SelectMany(element => element.UnitPaymentOrder.UnitCosts).ToList());

            _dbContext.SaveChanges();
            return(model.Id);
        }