void BringToWall(LineItem lineItem)
 {
     LineItemVatValue = lineItem.LineItemVatValue;
     LiTotalNet = lineItem.LiTotalNet;
     Vat = lineItem.Vat;
     VatAmount = lineItem.VatAmount;
     UnitPrice = lineItem.UnitPrice;
     TotalPrice = lineItem.TotalPrice;
 }
        void AddProducts()
        {
            if (SelectedProduct.ProductType == "ConsolidatedProduct")
            {
                AddLineItem(SelectedProduct.ProductId,
                            SelectedProduct.ProductDesc,
                            SelectedProduct.ProductType,
                            Qty,
                            UnitPrice,
                            LineItemVatValue,
                            Vat,
                            VatAmount,
                            LiTotalNet,
                            TotalPrice,
                            Guid.Empty,
                            BackOrder,
                            LostSale,
                            IsNew,
                            0);
            }
            else
            {
                var product = LoadedProducts.First(n => n.Id == SelectedProduct.ProductId);
                if (product.GetType() == typeof(SaleProduct))
                    SaleProduct = BuildSaleProduct();
                SaleProductReturnable = BuildSaleProductReturnable();
                BulkSaleContainer = BuildBulkSaleContainer();

                LineItem existingSaleProd = null;
                if (AddedLineItems.Count > 0)
                {
                    try
                    {
                        existingSaleProd = AddedLineItems.First(n => n.ProductId == SaleProduct.ProductId);
                    }
                    catch { }
                }

                if (!ReceiveReturnables)
                {

                    AddSaleProduct();
                    //add bottles
                    AddFirstReturnable();
                    if (SellInBulk)
                    {
                        //add crate
                        AddCrate();
                    }
                    else if (CrateCapacity > 1)
                    {
                        //if qty can fit in a crate, give him in a f*#@* crate
                        decimal reqQty = RequiredQty; 
                        if (existingSaleProd != null)
                        {
                            if (existingSaleProd.Qty < CrateCapacity) //item had no crate
                            {
                                reqQty += existingSaleProd.Qty;
                                Qty = reqQty;
                                BulkSaleContainer = BuildBulkSaleContainer();
                            }
                        }

                        if ((reqQty / CrateCapacity) >= 1)
                        {

                            //add crate
                            AddCrate();
                        }
                    }
                }
                else
                {
                    AddSaleProduct();
                    if (SellInBulk)
                    {
                        //add crate
                        AddCrate();
                    }
                }
            }
        }
        LineItem BuildReturnableProduct(ReturnableProduct returnableLi, bool isSecondaryReturnable = false)
        {
            var returnableProduct = new LineItem
            {
                ProductId = returnableLi.Id,
                ProductDesc = returnableLi.Description,
                SequenceNo = LineItems.Count() + 1
            };
            ReturnableId = returnableLi.Id;
            returnableProduct.Qty = RequiredQty;
            returnableProduct.ProductType = returnableLi.GetType().ToString().Split('.').Last();

            returnableProduct.IsNew = IsNew;
            //get pricing details
            returnableProduct = PriceCalc(returnableLi, returnableProduct);
            return returnableProduct;
        }
        LineItem BuildUIProduct()
        {
            LineItem uiProduct = null;

            SaleProduct = BuildSaleProduct();
            uiProduct = new LineItem
            {
                ProductId = SaleProduct.ProductId,
                IsNew = IsNew,
                LineItemVatValue = SaleProduct.LineItemVatValue,
                LiTotalNet = SaleProduct.LiTotalNet,
                ProductDesc = SaleProduct.ProductDesc,
                Qty = SaleProduct.Qty,
                Vat = SaleProduct.Vat,
                UnitPrice = SaleProduct.UnitPrice,
                VatAmount = SaleProduct.VatAmount,
                TotalPrice = SaleProduct.TotalPrice,
            };
            SaleProductReturnable = BuildSaleProductReturnable();
            if (SaleProductReturnable != null)
            {
                uiProduct.LiTotalNet += SaleProductReturnable.LiTotalNet;
                //uiProduct.Qty += SaleProductReturnable.Qty;
                uiProduct.Vat += SaleProductReturnable.Vat;
                uiProduct.UnitPrice += SaleProductReturnable.UnitPrice;
                uiProduct.VatAmount += SaleProductReturnable.VatAmount;
                uiProduct.TotalPrice += SaleProductReturnable.TotalPrice;
            }

            BulkSaleContainer = BuildBulkSaleContainer();
            if (BulkSaleContainer != null)
                if (SellInBulk || SaleProduct.Qty >= BulkSaleContainer.Capacity)
                {
                    uiProduct.LiTotalNet += BulkSaleContainer.LiTotalNet;
                    //uiProduct.Qty += BulkSaleContainer.Qty;
                    uiProduct.Vat += BulkSaleContainer.Vat;
                    uiProduct.UnitPrice += BulkSaleContainer.UnitPrice;
                    uiProduct.VatAmount += BulkSaleContainer.VatAmount;
                    uiProduct.TotalPrice += BulkSaleContainer.TotalPrice;
                }

            return uiProduct;
        }
 private void UpdateLineItem(object sender, LineItem item)
 {
     if (_vm == null)
         _vm = DataContext as ApproveSalesmanOrderViewModel;
     if (sender.GetType() == typeof(SOLineItemModal))
     {
         _vm.UpdateLineItem(item.SequenceNo,
                            item.Qty,
                            item.LineItemVatValue,
                            item.VatAmount,
                            item.TotalPrice,
                            item.BackOrder,
                            item.LostSale,
                            true);
     }
     else if (sender.GetType() == typeof(AmmendOrderLineItemModal))
     {
         _vm.UpdateLineItem(item.SequenceNo,
                            item.Qty,
                            item.LineItemVatValue,
                            item.VatAmount,
                            item.TotalPrice,
                            item.BackOrder,
                            item.LostSale,
                            true);
     }
 }
        private LineItem BuildBulkSaleContainer(ReturnableProduct returnableLi)
        {
            var returnableProduct = new LineItem
            {
                ProductId = returnableLi.Id,
                ProductDesc = returnableLi.Description
            };
            ReturnableId = returnableLi.Id;
            returnableProduct.Qty = RequiredQty;
            returnableProduct.ProductType = returnableLi.GetType().ToString().Split('.').Last();

            //if we are building the original container when editing then we divide by default loaded qty which is for sale product.
            if (!editReturnable)
            {
                if (buildingOrigCrate)
                    returnableProduct.Qty = (int)((Approve / returnableLi.Capacity) <= 0 ? 1 : Approve / returnableLi.Capacity);
                else
                {
                    if (SellInBulk)//sell in bulk option is selected
                        returnableProduct.Qty = Approve;
                    else if (SellInUnits)
                        returnableProduct.Qty = (int)((Approve / returnableLi.Capacity) <= 0 ? 1 : Approve / returnableLi.Capacity);
                }
            }


            returnableProduct.IsNew = IsNew;

            //get pricing details
            returnableProduct = PriceCalc(returnableLi, returnableProduct);
            return returnableProduct;
        }
 private LineItem BuildSaleProduct()
 {
     Product product = LoadedProducts.First(n => n.Id == SelectedProduct.ProductId);
     LineItem saleProduct = new LineItem
     {
         ProductId = SelectedProduct.ProductId,
         ProductDesc = SelectedProduct.ProductDesc,
         ProductType = SelectedProduct.ProductType,
         Qty = RequiredQty,
         IsNew = IsNew,
         BackOrder = BackOrder,
         LostSale = LostSale,
         ReturnableId =
             ((SaleProduct)product).ReturnableProduct == null
                 ? Guid.Empty
                 : ((SaleProduct)product).ReturnableProduct.Id,
         SequenceNo = LineItems.Count() + 1
     };
     saleProduct = PriceCalc(product, saleProduct);
     return saleProduct;
 }
        void UpdateProducts()
        {
            if (editReturnable || SelectedProduct.ProductType == "ConsolidatedProduct")
            {
                AddLineItem(SelectedProduct.ProductId,
                            SelectedProduct.ProductDesc,
                            SelectedProduct.ProductType,
                            RequiredQty,
                            UnitPrice,
                            LineItemVatValue,
                            Vat,
                            VatAmount,
                            LiTotalNet,
                            TotalPrice,Guid.Empty,
                            BackOrder,
                            LostSale,
                            IsNew,
                            SequenceNo);
            }
            else
            {
                var product = LoadedProducts.First(n => n.Id == SelectedProduct.ProductId);
                if (product.GetType() == typeof(SaleProduct))
                {
                    SaleProduct = BuildSaleProduct();
                    SaleProduct.SequenceNo = SequenceNo;

                    SaleProductReturnable = BuildSaleProductReturnable();

                    AddSaleProduct();

                    if (SaleProductReturnable != null)
                    {
                        var originalFirstReturnable =
                            OriginalLineItems.FirstOrDefault(n => n.ProductId == SaleProductReturnable.ProductId);
                        AddLineItem(
                            SaleProductReturnable.ProductId,
                            SaleProductReturnable.ProductDesc,
                            SaleProductReturnable.ProductType,
                            SaleProductReturnable.Qty - originalFirstReturnable.Qty,
                            SaleProductReturnable.UnitPrice,
                            SaleProductReturnable.LineItemVatValue,
                            SaleProductReturnable.Vat,
                            SaleProductReturnable.VatAmount - originalFirstReturnable.VatAmount,
                            SaleProductReturnable.LiTotalNet - originalFirstReturnable.LiTotalNet,
                            SaleProductReturnable.TotalPrice - originalFirstReturnable.TotalPrice,
                            SaleProductReturnable.ReturnableId,
                            CalcBackOrder(SaleProductReturnable),
                            CalcLostSale(SaleProductReturnable),
                            true);
                    }
                }

                //update crate
                if (BulkSaleContainer == null) return;
                var origCrate = OriginalLineItems.FirstOrDefault(n => n.ProductId == BulkSaleContainer.ProductId);
                if (enforceAddCrate)
                {
                    if (RequiredQty / CrateCapacity >= 1)
                    {
                        AddLineItem(BulkSaleContainer.ProductId,
                                    BulkSaleContainer.ProductDesc,
                                    BulkSaleContainer.ProductType,
                                    BulkSaleContainer.Qty,
                                    BulkSaleContainer.UnitPrice,
                                    BulkSaleContainer.LineItemVatValue,
                                    BulkSaleContainer.Vat,
                                    BulkSaleContainer.VatAmount,
                                    BulkSaleContainer.LiTotalNet,
                                    BulkSaleContainer.TotalPrice,
                                    BulkSaleContainer.ReturnableId,
                                    CalcBackOrder(BulkSaleContainer),
                                    CalcLostSale(BulkSaleContainer),
                                    true);
                        enforceAddCrate = false;
                    }
                }
                else
                {
                    if (RequiredQty / CrateCapacity < 1)
                    {
                        AddLineItem(
                            BulkSaleContainer.ProductId,
                            BulkSaleContainer.ProductDesc,
                            BulkSaleContainer.ProductType,
                            0 - origCrate.Qty,
                            BulkSaleContainer.UnitPrice,
                            BulkSaleContainer.LineItemVatValue,
                            BulkSaleContainer.Vat,
                            BulkSaleContainer.VatAmount - origCrate.VatAmount,
                            BulkSaleContainer.LiTotalNet - origCrate.LiTotalNet,
                            BulkSaleContainer.TotalPrice - origCrate.TotalPrice,
                            BulkSaleContainer.ReturnableId,
                            CalcBackOrder(BulkSaleContainer),
                            CalcLostSale(BulkSaleContainer),
                            true);
                    }
                    else
                    {
                        AddLineItem(
                            BulkSaleContainer.ProductId,
                            BulkSaleContainer.ProductDesc,
                            BulkSaleContainer.ProductType,
                            BulkSaleContainer.Qty - origCrate.Qty,
                            BulkSaleContainer.UnitPrice,
                            BulkSaleContainer.LineItemVatValue,
                            BulkSaleContainer.Vat,
                            BulkSaleContainer.VatAmount - origCrate.VatAmount,
                            BulkSaleContainer.LiTotalNet - origCrate.LiTotalNet,
                            BulkSaleContainer.TotalPrice - origCrate.TotalPrice,
                            BulkSaleContainer.ReturnableId,
                            CalcBackOrder(BulkSaleContainer),
                            CalcLostSale(BulkSaleContainer),
                            true);
                    }
                }
            }
        }
        void BuildAllProducts()
        {
            SaleProduct = null;
            BulkSaleContainer = null; //e.g crate
            SaleProductReturnable = null; //e.g bottle
            UIProduct = null;

            var product = LoadedProducts.First(n => n.Id == SelectedProduct.ProductId);
            if (product.GetType() == typeof(SaleProduct))
                SaleProduct = BuildSaleProduct();
            SaleProductReturnable = BuildSaleProductReturnable();
            BulkSaleContainer = BuildBulkSaleContainer();
            UIProduct = BuildUIProduct();
        }
 void GetBulkSaleContainerCapacity()
 {
     if (BulkSaleContainer == null)
         BulkSaleContainer = BuildBulkSaleContainer();
     try
     {
         CrateCapacity = BulkSaleContainer.Capacity;
     }
     catch
     {
         CrateCapacity = 1;
     }
 }
 decimal CalcBackOrder(LineItem lineItem)
 {
     decimal backOrder = 0;
     var productItem = Products.First(n => n.ProductId == lineItem.ProductId);
     backOrder = lineItem.Qty - productItem.AvailableInventory;
     return backOrder < 0 ? 0 : backOrder;
 }
 decimal CalcLostSale(LineItem lineItem)
 {
     decimal lostSale = 0;
     var oli = OriginalLineItems.First(n => n.ProductId == lineItem.ProductId);
     lostSale = oli.Qty - lineItem.Qty;
     return lostSale < 0 ? 0 : lostSale;
 }
        public LineItem RecalcTotal(LineItem lineItem)
        {
            decimal net = lineItem.UnitPrice * lineItem.Qty;
            //VatAmount = .17m * net;
            lineItem.Vat = lineItem.UnitPrice * lineItem.LineItemVatValue;//vat for single item
            lineItem.LiTotalNet = lineItem.UnitPrice * lineItem.Qty;
            lineItem.VatAmount = net * lineItem.LineItemVatValue;//total vat amount
            lineItem.TotalPrice = net + lineItem.VatAmount;

            return lineItem;
        }
 void AddCrate()
 {
     if (BulkSaleContainer == null) return;
     AddLineItem(
         BulkSaleContainer.ProductId,
         BulkSaleContainer.ProductDesc,
         BulkSaleContainer.ProductType,
         BulkSaleContainer.Qty,
         BulkSaleContainer.UnitPrice,
         BulkSaleContainer.LineItemVatValue,
         BulkSaleContainer.Vat,
         BulkSaleContainer.VatAmount,
         BulkSaleContainer.LiTotalNet,
         BulkSaleContainer.TotalPrice,
         BulkSaleContainer.ReturnableId,
         BulkSaleContainer.BackOrder,
         BulkSaleContainer.LostSale,
         IsNew);
     BulkSaleContainer = null;
 }
        private LineItem BuildSaleProductReturnable()
        {
            ReturnableProduct myReturnable = null;
            ReturnableProduct myCrate = null;
            Product product = null;
            List<ReturnableProduct> returnables = null;
            if (SelectedProduct.ProductId !=Guid.Empty)
            {
                product = LoadedProducts.FirstOrDefault(n => n.Id == SelectedProduct.ProductId);
                if (product.GetType() == typeof(SaleProduct))
                {
                    var rp = ((SaleProduct)product).ReturnableProduct;
                    if (rp != null)
                        myReturnable = rp;
                }
            }
            if (myReturnable != null)
            {
                SelectedProduct.HasReturnable = true;
                //add new returnable product to LineItems
                LineItem returnableProduct = BuildReturnableProduct(myReturnable);
                //if (SellInBulk)
                //{
                try
                {
                    myCrate =
                        ((SaleProduct)LoadedProducts.FirstOrDefault(n => n.Id == SelectedProduct.ProductId)).
                            ReturnableProduct.
                            ReturnAbleProduct;
                }
                catch
                {
                    myCrate = null;
                }
                //}

                var firstReturnable = new LineItem
                {
                    ProductId = returnableProduct.ProductId,
                    ProductDesc = returnableProduct.ProductDesc,
                    ProductType = returnableProduct.ProductType,
                    Qty = returnableProduct.Qty,
                    UnitPrice = returnableProduct.UnitPrice,
                    LineItemVatValue = returnableProduct.LineItemVatValue,
                    Vat = returnableProduct.Vat,
                    VatAmount = returnableProduct.VatAmount,
                    LiTotalNet = returnableProduct.LiTotalNet,
                    TotalPrice = returnableProduct.TotalPrice,
                    ReturnableId = myCrate != null ? myCrate.Id : Guid.Empty,
                    Capacity = false ? 0 : myReturnable.Capacity,
                    IsNew = true,
                    SequenceNo = LineItems.Count() + 1
                };
                return firstReturnable;
            }
            SelectedProduct.HasReturnable = false;
            return null;
        }
 void AddFirstReturnable()
 {
     if (SaleProductReturnable != null)
     {
         AddLineItem(
             SaleProductReturnable.ProductId,
             SaleProductReturnable.ProductDesc,
             SaleProductReturnable.ProductType,
             SaleProductReturnable.Qty,
             SaleProductReturnable.UnitPrice,
             SaleProductReturnable.LineItemVatValue,
             SaleProductReturnable.Vat,
             SaleProductReturnable.VatAmount,
             SaleProductReturnable.LiTotalNet,
             SaleProductReturnable.TotalPrice,
             SaleProductReturnable.ReturnableId,
             SaleProductReturnable.BackOrder,
             SaleProductReturnable.LostSale,
             IsNew);
         SaleProductReturnable = null;
     }
 }
        //sell in bulk
        private LineItem BuildBulkSaleContainer()
        {
            //var myCrate = _productService.GetAll().OfType<ReturnableProduct>()
            //    .Where(n => n.Packaging.Id == SelectedProduct.PackagingId && n.PackagingType.Id == SelectedProduct.PackagingTypeId && n.Capacity > 1)
            //        .ToList().FirstOrDefault();
            ReturnableProduct myContainer = null;
            if (SelectedProduct.ProductType == "ReturnableProduct")
            {
                myContainer = (ReturnableProduct) LoadedProducts.FirstOrDefault(n => n.Id == SelectedProduct.ProductId);
            }
            else
            {
                try
                {
                    var re =
                        ((SaleProduct) LoadedProducts.FirstOrDefault(n => n.Id == SelectedProduct.ProductId)).
                            ReturnableProduct;
                    myContainer = re.ReturnAbleProduct;
                }
                catch
                {
                    myContainer = null;
                }
            }

            if (myContainer != null)
            {
                SelectedProduct.HasReturnable = true;
                //LineItem crate = BuildReturnableProduct(myContainer, true);
                LineItem crate = BuildBulkSaleContainer(myContainer);
                var mycrate = new LineItem
                {
                    ProductId = crate.ProductId,
                    ProductDesc = crate.ProductDesc,
                    ProductType = crate.ProductType,
                    Qty = crate.Qty,
                    UnitPrice = crate.UnitPrice,
                    LineItemVatValue = crate.LineItemVatValue,
                    Vat = crate.Vat,
                    VatAmount = crate.VatAmount,
                    TotalPrice = crate.TotalPrice,
                    LiTotalNet = crate.LiTotalNet,
                    ReturnableId = Guid.Empty,
                    Capacity = myContainer.Capacity,
                    IsNew = IsNew,
                    SequenceNo = LineItems.Count() + 1
                };
                CrateCapacity = mycrate.Capacity;
                return mycrate;
            }
            SelectedProduct.HasReturnable = false;
            return null;
        }
        LineItem PriceCalc(ReturnableProduct product, LineItem lineItem)
        {
            using (var container = NestedContainer)
            {
                IDiscountProWorkflow _discountProService = Using<IDiscountProWorkflow>(container);
               
                lineItem.UnitPrice = _discountProService.GetUnitPrice(product, SelectedOutlet.OutletProductPricingTier);

                LineItemVatValue = _discountProService.GetVATRate(product, SelectedOutlet);
                lineItem.LineItemVatValue = LineItemVatValue;

                lineItem = RecalcTotal(lineItem);
                //BindTree(product);
                return lineItem;
            }
        }
        private void AddLineItem(object sender, LineItem item)
        {
            if (_vm == null)
                _vm = DataContext as ApproveSalesmanOrderViewModel;

            _vm.AddAmmendedLineItem(item.ProductId, item.Qty);

            if (sender.GetType() == typeof(AmmendOrderLineItemModal))
            {
                var lineItem =
                    _vm.LineItems.First(
                        n => n.ProductId == item.ProductId && n.OrderLineItemType != OrderLineItemType.Discount);

                _vm.AddAmmendedLineItem(lineItem.ProductId, lineItem.Qty, lineItem.SequenceNo);
            }

            _vm.ProcessDiscounts();
        }