Ejemplo n.º 1
0
        public async Task <IActionResult> Create(VatDocumentDto vatDocumentDto)
        {
            if (ModelState.IsValid)
            {
                await vatDocumentService.CreateVatDocumentAsync(vatDocumentDto);

                if (vatDocumentDto.HasErrors)
                {
                    await SetViewBagDataAsync();

                    if (!userCompanyTemp.IsVatRegistered)
                    {
                        return(View("CreateWithoutVat", vatDocumentDto));
                    }
                    return(View(vatDocumentDto));
                }

                return(RedirectToAction("ViewVatDocument", "ViewVatDocument", new { id = vatDocumentDto.Id }));
            }
            foreach (var modelState in ModelState.Values)
            {
                foreach (var error in modelState.Errors)
                {
                    vatDocumentDto.ErrorMassages.Add(error.ErrorMessage);
                }
            }
            await SetViewBagDataAsync();

            if (!userCompanyTemp.IsVatRegistered)
            {
                return(View("CreateWithoutVat", vatDocumentDto));
            }
            return(View(vatDocumentDto));
        }
Ejemplo n.º 2
0
        public async Task <VatDocumentDto> CreateVatDocumentAsync(VatDocumentDto vatDocumentDto)
        {
            var documentId = await GetNewVatDocumentIdAsync(vatDocumentDto);

            if (documentId > vatDocumentDto.Id)
            {
                vatDocumentDto.ErrorMassages.Add($"Има вече издаден документ с №{new string('0', 10 - vatDocumentDto.Id.ToString().Length) + vatDocumentDto.Id.ToString()}, документа ще бъде издаден с № {new string('0', 10 - documentId.ToString().Length) + documentId.ToString()}");
                vatDocumentDto.Id = documentId;
                return(vatDocumentDto);
            }
            if (vatDocumentDto.PartnerId == 0)
            {
                vatDocumentDto.ErrorMassages.Add($"Моля въведете контрагент!");
                return(vatDocumentDto);
            }

            var companyId = await vatDocumentRepo.Context.Companies.OrderBy(c => c.Id).Select(c => c.Id).LastAsync();

            var document = new VatDocument();

            document.Id = vatDocumentDto.Id;
            document.CompanyObjectId = await vatDocumentRepo.Context.CompanyObjects
                                       .Where(o => o.GUID == userCompanyTemp.CompanyObjectGUID)
                                       .Select(o => o.Id)
                                       .FirstOrDefaultAsync();

            document.PartnerId        = vatDocumentDto.PartnerId;
            document.CompanyId        = companyId;
            document.WriterEmployeeId = await employeeService.GetOrSetEmployeeIdByNameAsync(vatDocumentDto.WriterEmployee);

            document.RecipientEmployeeId = await partnerEmployeeService.GetOrSetEmployeeIdByNameAsync(vatDocumentDto.RecipientEmployee, vatDocumentDto.PartnerId);

            document.CreatedDate   = SetDate(vatDocumentDto.CreatedDate, vatDocumentDto);
            document.VatReasonDate = SetDate(vatDocumentDto.VatReasonDate, vatDocumentDto);
            document.PaymentTypeId = vatDocumentDto.PaymentTypeId;
            document.BankAccountId = await GetBankAccountIdIfRequare(vatDocumentDto);

            document.Type        = (Data.CompanyData.Models.Enums.VatDocumentTypes)vatDocumentDto.Type;
            document.FreeText    = vatDocumentDto.FreeText;
            document.Description = vatDocumentDto.Description;

            await ProcessingSales(vatDocumentDto);

            document.SubTottal = vatDocumentDto.SubTottal;
            document.Vat       = vatDocumentDto.Vat;
            document.Tottal    = vatDocumentDto.Tottal;

            if (vatDocumentDto.HasErrors)
            {
                return(vatDocumentDto);
            }
            await vatDocumentRepo.AddAsync(document);

            await vatDocumentRepo.SaveChangesAsync();

            return(vatDocumentDto);
        }
Ejemplo n.º 3
0
        private async Task <int?> GetBankAccountIdIfRequare(VatDocumentDto vatDocumentDto)
        {
            var paymentType = await vatDocumentRepo.Context.PaymentTypes.Where(pt => pt.Id == vatDocumentDto.PaymentTypeId).FirstOrDefaultAsync();

            if (paymentType.RequireBankAccount)
            {
                return(vatDocumentDto.PaymentTypeId);
            }
            return(null);
        }
Ejemplo n.º 4
0
        private DateTime SetDate(string date, VatDocumentDto vatDocumentDto)
        {
            DateTime parsedDate;
            var      isValidDate = DateTime.TryParse(date, new CultureInfo("bg-BG"), DateTimeStyles.None, out parsedDate);

            if (isValidDate)
            {
                return(parsedDate);
            }
            vatDocumentDto.ErrorMassages.Add("Формата на датата е грешен!");
            return(parsedDate);
        }
Ejemplo n.º 5
0
        public async Task <VatDocumentDto> PrepareVatDocumentModelAsync(VatDocumentTypes vatDocumentType)
        {
            var model = new VatDocumentDto();

            model.Id = await GetNewVatDocumentIdAsync(model);

            model.Type           = vatDocumentType;
            model.CreatedDate    = DateTime.Now.ToString("dd.MM.yyyy");
            model.VatReasonDate  = DateTime.Now.ToString("dd.MM.yyyy");
            model.WriterEmployee = (await employeeService.GetActiveCompanyEmployee())?.FullName;

            return(model);
        }
Ejemplo n.º 6
0
        private async Task <long> GetNewVatDocumentIdAsync(VatDocumentDto vatDocumentDto)
        {
            var companyObject = await vatDocumentRepo.Context.CompanyObjects
                                .Where(o => o.GUID == userCompanyTemp.CompanyObjectGUID)
                                .FirstOrDefaultAsync();

            var documentId = await vatDocumentRepo.AllAsNoTracking().Where(vt => vt.CompanyObjectId == companyObject.Id).OrderBy(vt => vt.Id).Select(vt => vt.Id).LastOrDefaultAsync();

            if (documentId > companyObject.EndNum)
            {
                vatDocumentDto.ErrorMassages.Add($"Номерацията в обект {companyObject.Name} е изчерпана!");
            }
            if (documentId == 0)
            {
                return(companyObject.StartNum);
            }

            return(documentId + 1);
        }
Ejemplo n.º 7
0
        private async Task ProcessingSales(VatDocumentDto vatDocumentDto)
        {
            vatDocumentDto.Vat = 0M;
            if (vatDocumentDto.Sales.Count == 0)
            {
                vatDocumentDto.ErrorMassages.Add("Няма въведени артикули");
                return;
            }

            var context = vatDocumentRepo.Context;

            foreach (var sale in vatDocumentDto.Sales)
            {
                if (sale.IsProduct && sale.ProductId != 0)
                {
                    var product = await context.Products.Include(p => p.VatType).Where(p => p.Id == sale.ProductId).FirstOrDefaultAsync();

                    if (product is null)
                    {
                        vatDocumentDto.ErrorMassages.Add($"Липсва продукт{sale.Name}!");
                        continue;
                    }
                    else
                    {
                        var newSale = new Sales();
                        newSale.ProductId = sale.ProductId;
                        newSale.Quantity  = sale.Quantity;
                        newSale.UnitPrice = product.Price;

                        var tottal = sale.Quantity * product.Price;
                        vatDocumentDto.SubTottal += tottal;
                        newSale.Total             = tottal;

                        var vat = tottal * (product.VatType.Percantage / 100);
                        vatDocumentDto.Vat += vat;
                        newSale.Vat         = vat;

                        var tottalWithVat = tottal + vat;
                        vatDocumentDto.Tottal += tottalWithVat;
                        newSale.TottalWithVat  = tottalWithVat;

                        newSale.VatDocumentId = vatDocumentDto.Id;
                        product.Quantity     -= sale.Quantity;

                        await context.Sales.AddAsync(newSale);

                        context.Products.Update(product);
                    }
                }
                else if (!sale.IsProduct)
                {
                    var vatType = await context.VatTypes.FindAsync(sale.VatTypeId);

                    if (String.IsNullOrEmpty(sale.Name) || String.IsNullOrEmpty(sale.ProductType))
                    {
                        vatDocumentDto.ErrorMassages.Add("Има грешка в продукти!");
                        continue;
                    }
                    if (vatType is null)
                    {
                        vatDocumentDto.ErrorMassages.Add($"Продукт: {sale.Name} има грешно ДДС!");
                    }
                    var freeProduct = new FreeProduct();
                    freeProduct.Name         = sale.Name;
                    freeProduct.QuantityType = sale.ProductType;
                    freeProduct.Quantity     = sale.Quantity;
                    freeProduct.Price        = sale.Price;
                    freeProduct.VatTypeId    = sale.VatTypeId;


                    var newSale = new Sales();
                    newSale.FreeProduct = freeProduct;
                    newSale.Quantity    = sale.Quantity;
                    newSale.UnitPrice   = sale.Price;

                    var tottal = sale.Quantity * sale.Price;
                    vatDocumentDto.SubTottal += tottal;
                    newSale.Total             = tottal;

                    var vat = tottal * (vatType.Percantage / 100);
                    vatDocumentDto.Vat += vat;
                    newSale.Vat         = vat;

                    var tottalWithVat = tottal + vat;
                    vatDocumentDto.Tottal += tottalWithVat;
                    newSale.TottalWithVat  = tottalWithVat;

                    newSale.VatDocumentId = vatDocumentDto.Id;

                    await context.Sales.AddAsync(newSale);
                }
                else
                {
                    vatDocumentDto.ErrorMassages.Add($"Има грешка в продукт {sale.Name}, {sale.ProductType}, {sale.Quantity}, {sale.Price}, {sale.TottalPrice}");
                }
            }
        }
Ejemplo n.º 8
0
        private async Task ProcessingEditSales(VatDocumentDto vatDocumentDto)
        {
            vatDocumentDto.Vat = 0M;
            var context = vatDocumentRepo.Context;
            var sales   = await context.Sales.Where(s => s.VatDocumentId == vatDocumentDto.Id).ToListAsync();

            foreach (var sale in sales)
            {
                var newSale = vatDocumentDto.Sales.Where(x => x.SaleId == sale.Id).FirstOrDefault();
                if (newSale is null)
                {
                    if (sale.ProductId != null && sale.ProductId != 0)
                    {
                        var product = await context.Products.FindAsync(sale.ProductId);

                        product.Quantity += sale.Quantity;
                        context.Products.Update(product);
                    }
                    else if (sale.FreeProductId != null && sale.FreeProductId != 0)
                    {
                        var product = await context.FreeProducts.FindAsync(sale.FreeProductId);

                        context.FreeProducts.Remove(product);
                    }
                    context.Sales.Remove(sale);
                }
                else
                {
                    if (sale.ProductId != null && sale.ProductId != 0)
                    {
                        if (sale.ProductId == newSale.ProductId)
                        {
                            if (Decimal.Compare(sale.Quantity, newSale.Quantity) != 0 || Decimal.Compare(sale.Total, newSale.TottalPrice) != 0)
                            {
                                var product = await context.Products.FindAsync(sale.ProductId);

                                product.Quantity += sale.Quantity;
                                context.Products.Update(product);
                                context.Sales.Remove(sale);
                                newSale.SaleId = 0;
                            }
                            else
                            {
                                vatDocumentDto.SubTottal += sale.Total;
                                vatDocumentDto.Vat       += sale.Vat;
                                vatDocumentDto.Tottal    += sale.TottalWithVat;
                            }
                        }
                        else
                        {
                            var product = await context.Products.FindAsync(sale.ProductId);

                            product.Quantity += sale.Quantity;
                            context.Products.Update(product);
                            context.Sales.Remove(sale);
                            newSale.SaleId = 0;
                        }
                    }
                    else if (sale.FreeProductId != null && sale.FreeProductId != 0)
                    {
                        if (sale.FreeProductId == newSale.FreeProductID)
                        {
                            var freeProduct = await context.FreeProducts.FindAsync(sale.FreeProductId);

                            var vatType = await context.VatTypes.FindAsync(newSale.VatTypeId);

                            freeProduct.Name         = newSale.Name;
                            freeProduct.QuantityType = newSale.ProductType;
                            freeProduct.Quantity     = newSale.Quantity;
                            freeProduct.Price        = newSale.Price;
                            freeProduct.VatTypeId    = newSale.VatTypeId;
                            context.FreeProducts.Update(freeProduct);


                            var tottal = newSale.Quantity * newSale.Price;
                            vatDocumentDto.SubTottal += tottal;
                            sale.Total = tottal;

                            var vat = tottal * (vatType.Percantage / 100);
                            vatDocumentDto.Vat += vat;
                            sale.Vat            = vat;

                            var tottalWithVat = tottal + vat;
                            vatDocumentDto.Tottal += tottalWithVat;
                            sale.TottalWithVat     = tottalWithVat;

                            context.Sales.Update(sale);
                        }
                        else
                        {
                            context.Sales.Remove(sale);
                            newSale.SaleId = 0;
                        }
                    }
                }
            }

            await AddNewSalesOnEdit(vatDocumentDto);
        }