private InvoiceLineItem CreateNewInvoiceLineItem(EditPOSSaleLineItem item)
        {
            using (StructureMap.IContainer cont = NestedContainer)
            {
                InvoiceLineItem invli = null;
                Guid id;
                if (invli == null)
                    id = Guid.NewGuid();
                else
                {
                    var li = Using<IInvoiceRepository>(cont).GetLineItemById(invli.Id);
                    id = li == null ? Guid.NewGuid() : li.Id;
                }

                var ili = Using<IInvoiceFactory>(cont).CreateLineItem(
                    item.ProductId,
                    item.Qty,
                    item.IsReceivedReturnable ? -item.UnitPrice : item.UnitPrice,
                    item.Product,
                    1,
                    item.LineItemVatValue,
                    item.ProductDiscount,
                    item.LineItemDiscountType
                    );
                    
                if (item.OrderLineItemType == OrderLineItemType.Discount)
                    ili.LineItemType = OrderLineItemType.Discount;
                return ili;
            }
        }
        private OrderLineItem CreateNewLineItem(EditPOSSaleLineItem item, bool isReceivedReturnable)
        {
            using (StructureMap.IContainer cont = NestedContainer)
            {
                OrderLineItem li = null;
                var oli = Using<IOrderFactory>(cont)
                    .CreateLineItem(item.ProductId,
                                    item.Qty,
                                    (item.IsReceivedReturnable ? -item.UnitPrice : item.UnitPrice),
                                    item.Product,
                                    1,
                                    item.LineItemVatValue,
                                    (item.OrderLineItemType == OrderLineItemType.Discount
                                         ? OrderLineItemType.Discount
                                         : OrderLineItemType.DuringConfirmation),
                                    item.ProductDiscount, item.LineItemDiscountType);

                item.LineItemId = oli.Id;
                return oli;
            }
        }
        private void AddCertainProductQuantityCertainProductDiscount(EditPOSSaleLineItem lineItem)
        {
            using (StructureMap.IContainer cont = NestedContainer)
            {
                List<ProductAsDiscount> productAsDiscounts = Using<IDiscountProWorkflow>(cont).GetFOCCertainProduct(
                    lineItem.ProductId, lineItem.Qty);

                foreach (ProductAsDiscount productAsDiscount in productAsDiscounts)
                {
                    if (productAsDiscount != null)
                        DisplayProductAsDiscountAndAddToDiscountLineItems(productAsDiscount);
                }
            }
        }
        public void AddLineItem(Guid productId, string productDesc, decimal unitPrice, decimal vatValue,
                                decimal vatAmount, decimal qty,
                                decimal totalPrice, bool sellInBulk, bool isEditable, Guid lineItemId,
                                Guid parentProductId, OrderLineItemType orderLineItemType, DiscountType discounType,
                                decimal productDiscount, string productType = "")
        {
            using(StructureMap.IContainer cont = NestedContainer)
            {
                if (loadingFromDb)
                {
                    ReceiveReturnable = unitPrice < 0;
                }
                //are we receiving returnables ...
                if (!loadingFromDb)
                {
                    if (ReceiveReturnable)
                    {
                        vatAmount = -vatAmount;
                        totalPrice = -totalPrice;
                        vatValue = -vatValue;
                    }
                }

                int sequenceNo = 1;
                if (LineItems.Count > 0)
                {
                    sequenceNo = LineItems.Max(n => n.SequenceNo) + 1;
                }

                bool isReturnableProduct = false;
                if (!LoadForViewing)
                {
                    isReturnableProduct = Using<IProductRepository>(cont).GetById(productId).GetType() ==
                                          typeof (ReturnableProduct);
                }

                //Check the line item exists by product
                EditPOSSaleLineItem li = null;
                if (ReceiveReturnable)
                {
                    if (LineItems.Any(n => n.ProductId == productId && n.IsReceivedReturnable == true))
                    {
                        li = LineItems.FirstOrDefault(n => n.ProductId == productId && n.IsReceivedReturnable == true);
                    }
                }
                else
                {
                    if (
                        LineItems.Any(
                            n =>
                            n.ProductId == productId && !n.IsReceivedReturnable &&
                            n.OrderLineItemType == orderLineItemType))
                    {
                        li =
                            LineItems.FirstOrDefault(
                                n =>
                                n.ProductId == productId && !n.IsReceivedReturnable &&
                                n.OrderLineItemType == orderLineItemType);
                    }
                }

                //if found update it
                if (li != null)
                {
                    li.Qty += qty;
                    li.VatAmount += vatAmount;
                    li.LineItemVatValue += vatValue;
                    li.TotalPrice += totalPrice;
                    li.ProductDiscount = productDiscount;
                    li.LineItemTotalProductDiscount += (productDiscount*qty);
                }
                else
                {
                    li = new EditPOSSaleLineItem(Using<IOtherUtilities>(cont))
                             {
                                 SequenceNo = sequenceNo,
                                 ProductId = productId,
                                 Product = productDesc,
                                 UnitPrice = unitPrice < 0 ? -unitPrice : unitPrice,
                                 //display +ve value for received returnables
                                 LineItemVatValue = vatValue,
                                 VatAmount = vatAmount,
                                 Qty = qty,
                                 TotalPrice = totalPrice,
                                 LineItemId = (Guid) lineItemId,
                                 IsReceivedReturnable = ReceiveReturnable,
                                 CanEditLineItem = true,
                                 CanRemoveLineItem = true,
                                 ProductType = productType,
                                 OrderLineItemType = orderLineItemType,
                                 LineItemDiscountType = discounType,
                                 ProductDiscount = productDiscount,
                                 LineItemTotalProductDiscount = (productDiscount*qty),
                             };
                    if (li.OrderLineItemType == OrderLineItemType.Discount)
                    {
                        li.CanEditLineItem = false;
                        li.CanRemoveLineItem = false;
                    }

                    if (!ReceiveReturnable)
                    {
                        if (isReturnableProduct && li.TotalPrice >= 0) li.CanEditLineItem = false;
                        if (isReturnableProduct && li.TotalPrice >= 0) li.CanRemoveLineItem = false;

                        if (li.LineItemDiscountType == DiscountType.FreeOfChargeDiscount)
                        {
                            li.CanEditLineItem = true;
                            li.CanRemoveLineItem = true;
                        }
                    }

                    if (Status != "New")
                    {
                        li.CanEditLineItem = false;
                        li.CanRemoveLineItem = false;
                    }
                    LineItems.Add(li);
                }
                CalcTotals();
            }
        }