public void AddLineItem(Guid productId, string productDesc, decimal unitPrice,
           decimal vatValue, decimal vatAmount, decimal vat, decimal qty, decimal totalPrice, bool isEditable)
        {
            int sequenceNo = 1;
            if (_LineItems.Count() > 0)
            {
                sequenceNo = _LineItems.Max(n => n.SequenceNo) + 1;
            }
            EditPurchaseOrderItem li;
            if (_LineItems.Any(p => p.ProductId == productId))
            {
                li = _LineItems.First(p => p.ProductId == productId);
                li.Qty = qty;
                li.LineItemVatValue = vatValue;
                li.VatAmount = vatAmount;
                li.TotalPrice = totalPrice;
            }
            else
            {
                li = new EditPurchaseOrderItem();
                _LineItems.Add(li);
                li.Qty = qty;
                li.LineItemVatValue = vatValue;
                li.VatAmount = vatAmount;
                li.TotalPrice = totalPrice;
            }

            li.SequenceNo = sequenceNo;
            li.ProductId = productId;
            li.Product = productDesc;
            li.UnitPrice = unitPrice;
            li.IsEditable = isEditable;
            li.Vat = vat;

            CalcTotals();
        }
        private void UpdateOrAddLineItem(Product product, decimal quantity, bool isEditable, Guid parentProductId, LineItemType lineItemType,bool isEdit)
        {
            decimal UnitPrice = PriceCalc(product);
            decimal UnitVat = VatCalc(product);

            decimal net = UnitPrice * quantity;
            decimal vat = UnitPrice * UnitVat;//cn
            decimal VatAmount = net * UnitVat;
            decimal TotalPrice = net + VatAmount;

            int sequenceNo = 1;
            if (LineItems.Count() > 0)
            {
                sequenceNo = LineItems.Max(n => n.SequenceNo) + 1;
            }
            EditPurchaseOrderItem li;
            if (LineItems.Any(p => p.ProductId == product.Id && p.LineItemType==lineItemType) && isEdit==false)
            {

                li = LineItems.First(p => p.ProductId == product.Id && p.LineItemType == lineItemType);
                li.Qty = li.Qty + quantity;
                li.LineItemVatValue = li.LineItemVatValue + vat;
                li.VatAmount = li.VatAmount + VatAmount;
                li.TotalPrice = li.TotalPrice + TotalPrice;

            }
            else if (LineItems.Any(p => p.ProductId == product.Id && p.LineItemType == lineItemType) && isEdit==true)
            {
                li = LineItems.First(p => p.ProductId == product.Id && p.LineItemType == lineItemType);
                li.Qty = quantity;
                li.LineItemVatValue = vat;
                li.VatAmount = VatAmount;
                li.TotalPrice = TotalPrice;
            }
            else
            {
                li = new EditPurchaseOrderItem();
                LineItems.Add(li);
                li.Qty = quantity;
                li.LineItemVatValue = vat;
                li.VatAmount = VatAmount;
                li.TotalPrice = TotalPrice;
            }

            li.SequenceNo = sequenceNo;
            li.ProductId = product.Id;
            li.Product = product.Description;
            li.UnitPrice = UnitPrice;
            li.IsEditable = isEditable;
            li.Vat = vat;
            li.ParentProductId = parentProductId;
            li.LineItemType = lineItemType;

            CalcTotals();


        }
        OrderLineItem CreateNewLineItem(EditPurchaseOrderItem item, Order order)
        {
            using (var container = NestedContainer)
            {
                
                IConfirmPurchaseOrderWFManager _confirmPurchaseOrderWFManager =
                    Using<IConfirmPurchaseOrderWFManager>(container);

                IProductRepository _productService = Using<IProductRepository>(container);
               
                var p = new OrderLineItemFactoryParameters
                            {
                                Description = item.Product,
                                Product = _productService.GetById(item.ProductId),
                                Qty = item.Qty
                            };
                var oli = _confirmPurchaseOrderWFManager.PendingDocumentLineItemFactory(order, p);
                item.LineItemId = oli.Id;
                oli.Value = item.UnitPrice;
                oli.Qty = item.Qty;
                oli.LineItemVatValue = item.Vat;
                oli.LineItemType = OrderLineItemType.DuringConfirmation;
                return oli;
            }
        }