public Task <TransactionProduct> CreateAsync(Product.Product product, Transaction.Transaction transaction)
 {
     return(Task.Factory.StartNew(() =>
     {
         var transactionProduct = new TransactionProduct(product, transaction);
         return transactionProduct;
     }));
 }
        private TransactionProduct AddProductToTransaction(Product product, byte numberOf, short?points, bool isForSell)
        {
            short?sellPoints = points ?? product.PointsWorth;

            if (!sellPoints.HasValue)
            {
                throw new InvalidOperationException("There is no default points for this product, while no points were given");
            }

            TransactionProduct transactionProduct = new TransactionProduct()
            {
                IsForSell       = isForSell,
                ProductId       = product.Id,
                Points          = sellPoints.Value,
                NumberOfProduct = numberOf,
                TransactionId   = ActiveTransaction.TransactionId,
            };

            _dataContext.TransactionProducts.Add(transactionProduct);
            _dataContext.SaveChanges();

            return(transactionProduct);
        }
        // GET: Transaction/Create
        public IActionResult Create()
        {
            TransactionCreate transactionCreate = new TransactionCreate()
            {
                TypesOfPayment = new List <SelectListItem>
                {
                    new SelectListItem {
                        Value = "0", Text = "Gotówka"
                    },
                    new SelectListItem {
                        Value = "1", Text = "Karta"
                    }
                }
            };

            transactionCreate.TransactionProduct = new List <TransactionProduct>();
            Invoice last = _transactionService.LastInvoice();

            transactionCreate.Transaction         = new Transaction();
            transactionCreate.Transaction.Invoice = new Invoice();

            transactionCreate.Transaction.Invoice.InvoiceNumber = last.InvoiceNumber + 1;

            foreach (var product in _productService.GetAllProductsWithoutFuel().ToList())
            {
                TransactionProduct model = new TransactionProduct
                {
                    ProductId          = product.ProductId,
                    Name               = product.Name,
                    Price              = product.Price,
                    LoyaltyPointsPrice = product.LoyaltyPointsPrice,
                    IsDiscountIncluded = false,
                    MaxAmountOfProduct = product.Stock
                };

                Discount discount = _discountService.GetDiscountForProduct(model.ProductId);

                if (discount != null)
                {
                    switch (discount.Type)
                    {
                    case 0:
                        model.IsDiscountIncluded = true;
                        break;

                    case 1:
                        model.IsDiscountIncluded = true;
                        break;

                    default:
                        model.IsDiscountIncluded = false;
                        break;
                    }
                }

                transactionCreate.TransactionProduct.Add(model);
            }
            transactionCreate.DistributorInTransaction = new List <DistributorInTransaction>();

            foreach (var distributor in _distributorService.GetAllDistributors().ToList())
            {
                DistributorInTransaction model = new DistributorInTransaction();

                List <TankDistributor> listOfTankDistributor = _tankService.GetTanksByDistributor(distributor.DistributorId).ToList();

                List <int> listOfTanksIds = new List <int>();
                foreach (var item in listOfTankDistributor)
                {
                    listOfTanksIds.Add(item.TankId);
                }
                listOfTanksIds.Sort();
                Random r            = new Random();
                int    randomTankId = r.Next(listOfTanksIds[0], listOfTanksIds[^ 1]);
        protected List <TransactionProduct> GetProductsFromDataGrid(TransactionType transactionType)
        {
            var products = new List <TransactionProduct>();

            for (var i = 0; i < OperationsViewService.GetRowsCount(); i++)
            {
                //var row = this.OperationsViewService.DataGrid.Rows[i];
                var     transactionNumber       = OperationsViewService.GetDataAtRow(i, TransactionNumber);
                var     productId               = -1;
                var     selectedProductQuantity = 0D;
                Product product;
                try
                {
                    var prodIdCell = OperationsViewService.GetDataAtRow(i, ProductId);
                    if (prodIdCell == null)
                    {
                        continue;
                    }
                    productId = (int)prodIdCell;
                    product   = ProductDbService.FindById(productId);
                    if (product == null)
                    {
                        throw new Exception();
                    }
                    selectedProductQuantity = double.Parse(OperationsViewService.GetDataAtRow(i, ProductQuantity) + "");
                }
                catch (Exception)
                {
                    throw new ArgumentException($"{ThereWasAnErrorOnRow} {transactionNumber}");
                }

                if (selectedProductQuantity <= 0D)
                {
                    throw new ArgumentException($"{ChooseValidQuantity} {transactionNumber}");
                }

                if (transactionType == TransactionType.SALE)
                {
                    if (product.Quantity < selectedProductQuantity)
                    {
                        throw new ArgumentException($"{InsufficientAmount} {transactionNumber}");
                    }
                }

                var subTotal = 0.0;
                switch (transactionType)
                {
                case TransactionType.SALE:
                    subTotal = product.SellPrice * selectedProductQuantity;
                    break;

                case TransactionType.DELIVERY:
                    subTotal = product.ImportPrice * selectedProductQuantity;
                    break;
                }

                var productTransaction = new TransactionProduct
                {
                    ProductId       = product.Id,
                    ProductQuantity = selectedProductQuantity,
                    SubTotalPrice   = subTotal
                };
                products.Add(productTransaction);
            }

            return(products);
        }