public ReceiptProduct MapReceiptProduct(Product product)
        {
            if (product == null)
            {
                throw new NullReferenceException($"Receveived product is null!");
            }

            var productReceipt = new ReceiptProduct();

            try
            {
                productReceipt.ProductName              = product.ProductName;
                productReceipt.Barcode                  = product.Barcode;
                productReceipt.Amount                   = product.Amount;
                productReceipt.Discount                 = product.Discount;
                productReceipt.ProductPrice             = product.Price;
                productReceipt.ProductPriceWithDiscount = _calculateProductPrice.Calculate(product, 1);
                productReceipt.Total = _calculateProductPrice.Calculate(product, product.Amount);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                Console.WriteLine(ex.Source);
                Console.WriteLine(ex.InnerException);
                throw;
            }
            return(productReceipt);
        }
        private void CreateReceiptProduct(Guid receiptId, Guid productId, int productCount)
        {
            var receiptInQuestion = _dbCashierRegisterContext.Receipts.Find(receiptId);
            var productInQuestion = _dbCashierRegisterContext.Products.Single(product => product.Id == productId);

            if (receiptInQuestion == null || productInQuestion == null)
            {
                throw new Exception("Receipt or product not exists");
            }

            if (productInQuestion.CountInStorage < productCount)
            {
                throw new Exception("Product count in storage less then product count.");
            }

            var newReceiptProduct = new ReceiptProduct
            {
                Product      = productInQuestion,
                ProductId    = productId,
                Receipt      = receiptInQuestion,
                ReceiptId    = receiptId,
                ProductCount = productCount,
                ProductDirectPercentageAtCreation = _dbCashierRegisterContext.Taxes.Single(tax => tax.TaxType == TaxType.Direct && tax.ProductTaxes.Any(product => product.ProductId == productId)).Percentage,
                ProductExcisePercentageAtCreation = _dbCashierRegisterContext.Taxes.Single(tax => tax.TaxType == TaxType.Excise && tax.ProductTaxes.Any(product => product.ProductId == productId)).Percentage,
                ProductPriceAtCreation            = productInQuestion.Price
            };

            _dbCashierRegisterContext.ReceiptProducts.Add(newReceiptProduct);
            _dbCashierRegisterContext.SaveChanges();

            productInQuestion.CountInStorage -= productCount;

            _dbCashierRegisterContext.SaveChanges();
        }
Exemplo n.º 3
0
        public async Task <IActionResult> PutReceiptProduct([FromRoute] int id, [FromBody] ReceiptProduct receiptProduct)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != receiptProduct.ID)
            {
                return(BadRequest());
            }

            _context.Entry(receiptProduct).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ReceiptProductExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemplo n.º 4
0
        public async Task <IActionResult> AttivaCarta(CardDetailViewModel model)
        {
            SVCOperationViewModel svcBody = new SVCOperationViewModel();

            svcBody.StoredValueCardCodeCollection.Add(model.SerialNumber);

            Issuer issuer = new Issuer();

            issuer.Name      = "Vittorio";
            issuer.Address   = "via centomani 11";
            issuer.Telephone = "0000000";
            issuer.EMail     = "*****@*****.**";
            issuer.VATNumber = "222222";

            svcBody.StoredValueCardReceipt.IssuerData = issuer;

            ReceiptProduct receiptProduct = new ReceiptProduct();

            receiptProduct.Code     = model.SerialNumber;
            receiptProduct.Name     = "VittorioCard";
            receiptProduct.Price    = 0;
            receiptProduct.Quantity = 1;
            receiptProduct.Family   = "";
            receiptProduct.Mode     = "";

            svcBody.StoredValueCardReceipt.Products.Add(receiptProduct);

            Payment payment = new Payment();

            payment.CardCode = model.SerialNumber;

            svcBody.StoredValueCardReceipt.Payments.Add(payment);

            ShopReceipt shopReceipt = new ShopReceipt();

            shopReceipt.IssueDate = DateTime.Now;
            shopReceipt.UserData  = "";

            svcBody.StoredValueCardReceipt.IssueDate = DateTime.Now;
            svcBody.StoredValueCardReceipt.UserData  = "";

            SenderData senderData = new SenderData();

            senderData.Shop       = Constants.SHOP;
            senderData.Terminal   = Constants.TERMINAL;
            senderData.Type       = 0;
            senderData.Number     = 0;
            senderData.CashDrawer = 0;
            senderData.Operator   = "";

            svcBody.StoredValueCardReceipt.SenderData = senderData;

            SVCOperationResultViewModel result = await _svcService.Activate(svcBody);

            if (result.ResultStatus == Constants.MSG_RESULT_STATUS_OK)
            {
                model = await _svcService.Status(model.SerialNumber);
            }
            return(View("Status", model));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> ChargeSvc(string cardCode, decimal price)
        {
            SVCChargeOperationViewModel svcBody = new SVCChargeOperationViewModel();

            svcBody.StoredValueCardCodeCollection.Add(cardCode);

            svcBody.CashbackType = SVCCashbackType.None;

            Issuer issuer = new Issuer();

            issuer.Name      = "Vittorio";
            issuer.Address   = "via centomani 11";
            issuer.Telephone = "0000000";
            issuer.EMail     = "*****@*****.**";
            issuer.VATNumber = "222222";

            svcBody.StoredValueCardReceipt.IssuerData = issuer;

            ReceiptProduct receiptProduct = new ReceiptProduct();

            receiptProduct.Code     = cardCode;
            receiptProduct.Name     = "VittorioCard";
            receiptProduct.Price    = price;
            receiptProduct.Quantity = 1;
            receiptProduct.Family   = "";
            receiptProduct.Mode     = "";

            svcBody.StoredValueCardReceipt.Products.Add(receiptProduct);

            Payment payment = new Payment();

            payment.CardCode = cardCode;

            svcBody.StoredValueCardReceipt.Payments.Add(payment);

            ShopReceipt shopReceipt = new ShopReceipt();

            shopReceipt.IssueDate = DateTime.Now;
            shopReceipt.UserData  = "";

            svcBody.StoredValueCardReceipt.IssueDate = DateTime.Now;
            svcBody.StoredValueCardReceipt.UserData  = "";

            SenderData senderData = new SenderData();

            senderData.Shop       = Constants.SHOP;
            senderData.Terminal   = Constants.TERMINAL;
            senderData.Type       = 0;
            senderData.Number     = 0;
            senderData.CashDrawer = 0;
            senderData.Operator   = "";

            svcBody.StoredValueCardReceipt.SenderData = senderData;

            var charge = await _svcService.Charge(svcBody);

            return(View("AssociaCarta", charge));
        }
Exemplo n.º 6
0
        public async Task SetProduct_WithInvalidData_ShouldReturnNull()
        {
            var category = await this.dbContext.Categories.FirstOrDefaultAsync();

            var receiptProduct = new ReceiptProduct();

            var actual = await this.productService.SetProductAsync(receiptProduct, "asd");

            Assert.IsNull(actual);
        }
Exemplo n.º 7
0
 private bool CompareProperties(ReceiptProduct expected, ReceiptProduct actual)
 {
     return
         (
         expected.Number == actual.Number &&
         expected.PriceIn == actual.PriceIn &&
         expected.ProductId == actual.ProductId &&
         expected.ReceiptId == actual.ReceiptId
         );
 }
 private string PrintDiscount(ReceiptProduct product)
 {
     if (product.Discount == Discount.NoDiscount)
     {
         return("");
     }
     else
     {
         return($"-{(product.Amount * product.ProductPrice) - (product.Amount * product.ProductPriceWithDiscount)}  ");
     }
 }
Exemplo n.º 9
0
        public async Task <int> Create(ReceiptInputModel model)
        {
            var receiptProducts = new List <ReceiptProduct>();

            foreach (var product in model.Products)
            {
                int productId = context.Products
                                .Where(p => p.Barcode == product.Barcode)
                                .Select(p => p.Id)
                                .FirstOrDefault();

                var receiptProduct = new ReceiptProduct()
                {
                    Quantity = product.Quantity
                };

                if (productId != default)
                {
                    receiptProduct.ProductId = productId;
                }
                else
                {
                    var newProduct = product.To <Product>();
                    receiptProduct.Product = newProduct;
                }

                receiptProducts.Add(receiptProduct);
            }

            var receipt   = model.To <Receipt>();
            int companyId = context.Companies
                            .Where(c => c.Name == model.CompanyName)
                            .Select(c => c.Id)
                            .FirstOrDefault();

            if (companyId != default)
            {
                receipt.CompanyId = companyId;
            }
            else
            {
                receipt.Company = new Company
                {
                    Name = model.CompanyName
                };
            }

            receipt.Products = receiptProducts;
            await context.Receipts.AddAsync(receipt);

            await context.SaveChangesAsync();

            return(receipt.Id);
        }
Exemplo n.º 10
0
        public async Task <IActionResult> PostReceiptProduct([FromBody] ReceiptProduct receiptProduct)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.ReceiptProducts.Add(receiptProduct);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetReceiptProduct", new { id = receiptProduct.ID }, receiptProduct));
        }
Exemplo n.º 11
0
        public async Task <ProductServiceModel> SetProductAsync(ReceiptProduct receiptProduct, string id)
        {
            var product = await this.db.Products.FirstOrDefaultAsync(x => x.Id == id);

            if (product == null)
            {
                return(null);
            }

            receiptProduct.Product = product;

            return(product.MapTo <ProductServiceModel>());
        }
Exemplo n.º 12
0
        public async Task SetProduct_WithValidData_ShouldReturnCorrectProduct()
        {
            var category = await this.dbContext.Categories.FirstOrDefaultAsync();

            var expected = await
                           this.productService.CreateAsync("product", 2m, 4, "81234567891131", category.Id, category.WareHouseId);

            var receiptProduct = new ReceiptProduct();

            var actual = await this.productService.SetProductAsync(receiptProduct, expected.Id);

            Assert.AreEqual(expected.Id, actual.Id);
        }
Exemplo n.º 13
0
        public void UpdateReceipt(int userId, int id, ReceiptEdit receiptEdit)
        {
            var receipt = _context.Receipts.FirstOrDefault(r => r.UserID == userId && r.ID == id);

            receipt.PurchaseDate = DateTime.Parse(receiptEdit.Date);
            receipt.ShopID       = receiptEdit.ShopId;

            //Check products

            //Current products
            var currentProducts = _context.ReceiptProducts.Where(rp => rp.ReceiptID == id).ToList();

            foreach (var receiptProduct in receiptEdit.Products)
            {
                //Add if new
                if (receiptProduct.ReceiptProductId == -1)
                {
                    var receiptProductObj = new ReceiptProduct()
                    {
                        Amount    = receiptProduct.Amount,
                        Price     = receiptProduct.Price,
                        ProductID = receiptProduct.ProductId,
                        ReceiptID = id
                    };
                    _context.ReceiptProducts.Add(receiptProductObj);
                    _context.SaveChanges();
                }
                //Only edit
                else
                {
                    var current =
                        _context.ReceiptProducts.FirstOrDefault(rp => rp.ID == receiptProduct.ReceiptProductId);
                    current.Amount    = receiptProduct.Amount;
                    current.Price     = receiptProduct.Price;
                    current.ProductID = receiptProduct.ProductId;
                    _context.SaveChanges();
                }
            }

            //Look for products to delete

            foreach (var currentProduct in currentProducts)
            {
                if (receiptEdit.Products.All(p => p.ReceiptProductId != currentProduct.ID))
                {
                    _context.ReceiptProducts.Remove(currentProduct);
                    _context.SaveChanges();
                }
            }
        }
Exemplo n.º 14
0
        public async Task UpdateProduct_WithInvalidData_ShouldReturnNull()
        {
            var category = await this.dbContext.Categories.FirstOrDefaultAsync();

            var result = await
                         this.productService.CreateAsync("product", 2m, 4, "81234567891131", category.Id, category.WareHouseId);

            var receiptProduct = new ReceiptProduct();

            var actual = await this.productService.UpdateAsync("asd", "updatedProduct",
                                                               2.2m, 200, "81234567891138", category.Id);

            Assert.IsNull(actual);
        }
Exemplo n.º 15
0
 public void CreateReceiptProducts(ICollection <ReceiptProductPost> products, int receiptId)
 {
     foreach (var product in products)
     {
         var receiptproduct = new ReceiptProduct()
         {
             ReceiptID = receiptId,
             ProductID = product.ProductId,
             Amount    = product.Amount,
             Price     = product.Price
         };
         _context.ReceiptProducts.Add(receiptproduct);
         _context.SaveChanges();
     }
 }
Exemplo n.º 16
0
        public async Task UpdateProduct_WithValidData_ShouldReturnCorrectProduct()
        {
            var category = await this.dbContext.Categories.FirstOrDefaultAsync();

            var result = await
                         this.productService.CreateAsync("product", 2m, 4, "81234567891131", category.Id, category.WareHouseId);

            var receiptProduct = new ReceiptProduct();

            var actual = await this.productService.UpdateAsync(result.Id, "updatedProduct",
                                                               2.2m, 200, "81234567891138", category.Id);

            Assert.AreEqual(result.Id, actual.Id);
            Assert.AreEqual("updatedProduct", actual.Name);
            Assert.AreEqual(2.2m, actual.Price);
            Assert.AreEqual(200, actual.Quantity);
            Assert.AreEqual(category.Id, actual.CategoryId);
        }
Exemplo n.º 17
0
        public async Task <ReceiptProductServiceModel> AddProductToOpenedReceiptByUsernameAsync(string username, string productId, double quantity)
        {
            var receipt = await this.dbContext.Receipts
                          .Include(x => x.User)
                          .Include(x => x.ReceiptProducts)
                          .ThenInclude(x => x.Product)
                          .Where(x => x.User.UserName == username && x.IssuedOn == null)
                          .FirstOrDefaultAsync();

            if (receipt == null)
            {
                return(null);
            }

            var receiptProduct = new ReceiptProduct
            {
                AddedOn  = DateTime.UtcNow,
                Receipt  = receipt,
                Quantity = quantity,
            };

            await this.productService.SetProductAsync(receiptProduct, productId);

            if (receiptProduct.Product == null)
            {
                return(null);
            }

            receiptProduct.Total = (decimal)quantity * receiptProduct.Product.Price;

            await this.dbContext.AddAsync(receiptProduct);

            await this.dbContext.SaveChangesAsync();

            return(receiptProduct.MapTo <ReceiptProductServiceModel>());
        }
Exemplo n.º 18
0
 partial void OnTypeChanging(ReceiptProduct.TypeX value);
Exemplo n.º 19
0
	private void detach_ReceiptProducts(ReceiptProduct entity)
	{
		this.SendPropertyChanging();
		entity.Receipt = null;
	}
Exemplo n.º 20
0
	private void attach_ReceiptProducts(ReceiptProduct entity)
	{
		this.SendPropertyChanging();
		entity.Receipt = this;
	}
Exemplo n.º 21
0
 partial void DeleteReceiptProduct(ReceiptProduct instance);
Exemplo n.º 22
0
 partial void UpdateReceiptProduct(ReceiptProduct instance);
Exemplo n.º 23
0
 partial void InsertReceiptProduct(ReceiptProduct instance);
        public static void Seed(CashierRegisterContext dbContext)
        {
            var receiptCount = dbContext.Receipts.Count();

            if (receiptCount != 0)
            {
                return;
            }

            var cashRegisterWithId = dbContext.CashRegisters.Find(1);
            var cashierWithId      = dbContext.Cashiers.Find(1);
            var cashRegisterCount  = dbContext.CashRegisterCashiers.Count();
            CashRegisterCashier cashRegisterCashier;

            if (cashRegisterCount == 0)
            {
                var newCashRegisterCashier = new CashRegisterCashier
                {
                    Cashier        = cashierWithId,
                    CashierId      = cashierWithId.Id,
                    CashRegister   = cashRegisterWithId,
                    CashRegisterId = cashRegisterWithId.Id,
                    StartOfShift   = DateTime.Now
                };
                dbContext.Add(newCashRegisterCashier);
                dbContext.SaveChanges();
                cashRegisterCashier = newCashRegisterCashier;
            }
            else
            {
                cashRegisterCashier = dbContext.CashRegisterCashiers.First();
            }


            var allProducts = dbContext.Products.ToList();

            for (var receiptIndex = 0; receiptIndex < 10; receiptIndex++)
            {
                var receipt = new Receipt
                {
                    Id = new Guid(),
                    DateTimeCreated     = DateTime.Now,
                    CashRegisterCashier = cashRegisterCashier
                };
                dbContext.Add(receipt);

                dbContext.SaveChanges();

                var productsOnReceipt = new List <Product>();

                var productIndexes = new List <int>();
                do
                {
                    var randomIndex = new Random().Next(1, allProducts.Count);
                    var any         = productIndexes.Any(index => index == randomIndex);
                    if (!any)
                    {
                        productIndexes.Add(randomIndex);
                    }
                } while (productIndexes.Count < 3);

                foreach (var productIndex in productIndexes)
                {
                    productsOnReceipt.Add(allProducts[productIndex]);
                }

                var receiptProductList = new List <ReceiptProduct>();

                foreach (var product in productsOnReceipt)
                {
                    var newReceiptProduct = new ReceiptProduct
                    {
                        Product      = product,
                        ProductId    = product.Id,
                        Receipt      = receipt,
                        ReceiptId    = receipt.Id,
                        ProductCount = 5,
                        ProductDirectPercentageAtCreation = dbContext.Taxes.Single(tax =>
                                                                                   tax.TaxType == TaxType.Direct &&
                                                                                   tax.ProductTaxes.Any(prd => prd.ProductId == product.Id))
                                                            .Percentage,
                        ProductExcisePercentageAtCreation = dbContext.Taxes.Single(tax =>
                                                                                   tax.TaxType == TaxType.Excise &&
                                                                                   tax.ProductTaxes.Any(prd => prd.ProductId == product.Id))
                                                            .Percentage,
                        ProductPriceAtCreation = product.Price
                    };

                    receiptProductList.Add(newReceiptProduct);

                    dbContext.ReceiptProducts.Add(newReceiptProduct);
                    dbContext.SaveChanges();

                    product.CountInStorage -= 5;
                    dbContext.SaveChanges();
                }

                var preTaxTotal = receiptProductList.Sum(product => product.Product.Price * product.ProductCount);
                var exciseTotal = receiptProductList.Sum(product =>
                                                         product.Product.Price *
                                                         dbContext.Taxes.Single(tax =>
                                                                                tax.TaxType == TaxType.Excise &&
                                                                                tax.ProductTaxes.Any(prd => prd.ProductId == product.Product.Id)).Percentage / 100 *
                                                         product.ProductCount);
                var directTotal = receiptProductList.Sum(product =>
                                                         product.Product.Price *
                                                         dbContext.Taxes.Single(tax =>
                                                                                tax.TaxType == TaxType.Direct &&
                                                                                tax.ProductTaxes.Any(prd => prd.ProductId == product.Product.Id)).Percentage / 100 *
                                                         product.ProductCount);
                var postTaxTotal = preTaxTotal + exciseTotal + directTotal;

                if (preTaxTotal != null)
                {
                    receipt.PreTaxPriceAtCreation = (int)preTaxTotal;
                }
                if (exciseTotal != null)
                {
                    receipt.ExciseTaxAtCreation = (int)exciseTotal;
                }
                if (directTotal != null)
                {
                    receipt.DirectTaxAtCreation = (int)directTotal;
                }
                if (postTaxTotal != null)
                {
                    receipt.PostTaxPriceAtCreation = (int)postTaxTotal;
                }

                dbContext.SaveChanges();
            }
        }