/// <summary>
        /// Delete the Invoice and the related Parcels from the Database.
        /// </summary>
        /// <param name="billId"></param>
        /// <param name="companyId"></param>
        public void DeleteInvoice(int invoiceId, int companyId)
        {
            //
            //If exists a sale by this invoice, then set invoiceId with null
            //
            var saleManager = new SaleManager(this);
            Sale original_sale = saleManager.GetSaleByInvoice(companyId, invoiceId);
            if (original_sale != null)
            {
                var sale = new Sale();
                sale.CopyPropertiesFrom(original_sale);
                sale.InvoiceId = null;

                saleManager.Update(original_sale, sale);
            }

            var parcelsManager = new ParcelsManager(this);
            parcelsManager.DeleteInvoiceParcels(invoiceId);

            DbContext.Invoices.DeleteOnSubmit(
                DbContext.Invoices.Where(i => i.CompanyId == companyId && i.InvoiceId == invoiceId).FirstOrDefault());
            DbContext.SubmitChanges();
        }
        /// <summary>
        /// this method create and return a Sale 
        /// </summary>
        /// <returns></returns>
        private Sale CreateSale()
        {
            Sale sale = new Sale();
            sale.CompanyId = Company.CompanyId;
            sale.DepositId = Deposit.DepositId;
            
            sale.SaleDate = DateTime.Now;
            sale.OrderDate = DateTime.Now;
            sale.ShipDate = DateTime.Now;
            if (Page.ViewState["CustomerId"] != null)
                sale.CustomerId = Convert.ToInt32(Page.ViewState["CustomerId"]);
            if (Page.ViewState["BudgetId"] != null)
                sale.BudgetId = Convert.ToInt16(Page.ViewState["BudgetId"]);

            sale.Discount = Convert.ToDecimal(Page.ViewState["discount"]);
            if (!String.IsNullOrEmpty(cboEmployee.SelectedValue))
                sale.VendorId = Convert.ToInt16(cboEmployee.SelectedValue);

            return sale;
        }
        /// <summary>
        /// Method to insert a Item in the saleItem
        /// This method verifies if you are trying to insert a composite product, and treat it
        /// insert a customerEquipment If the customer exists
        /// This method also, drop off the quantity in the inventory
        /// </summary>
        /// <param name="item"></param>
        /// <param name="sale"></param>
        public void InsertSaleItem(SaleItem saleItem, Sale sale)
        {
            DbContext.SaleItems.InsertOnSubmit(saleItem);
            DbContext.SubmitChanges();

            var product = new ProductManager(this).GetProduct(Convert.ToInt32(saleItem.ProductId),
                                                                             saleItem.CompanyId);
            if (product != null)
                saleItem.ProductId = product.ProductId;
            DbContext.SubmitChanges();

            SaveSaleItemAsCustomerEquipment(saleItem);
            DropProductInSale(saleItem);
        }
 /// <summary>
 /// this method save the Saleitems of Sale
 /// </summary>
 /// <param name="sale"></param>
 /// <param name="list"></param>
 private void SaveSaleItems(Sale sale, List<SaleItem> list)
 {
     foreach (SaleItem item in list)
     {
         item.SaleId = sale.SaleId;
         //item.Sale = sale;
         InsertSaleItem(item, sale);
     }
 }
        /// <summary>
        /// this method save the invoice created by Sale
        /// </summary>
        /// <param name="sale"></param>
        private void SaveSaleInvoice(Sale sale, Invoice invoice, List<Parcel> lstParcels)
        {
            //
            // Save Invoice
            //
            var financialManager = new FinancialManager(this);
            sale.InvoiceId = financialManager.InsertRetrievingId(invoice);
            DbContext.SubmitChanges();


            //
            // Save Parcels
            //
            var parcelsManager = new ParcelsManager(this);
            var accountManager = new AccountManager(this);

            var firstParcel = lstParcels.FirstOrDefault();
            var condition = accountManager.GetFinancierConditionByParcelCount(firstParcel.CompanyId, firstParcel.FinancierOperationId.Value, lstParcels.Count());

            foreach (Parcel parcel in lstParcels)
            {
                parcel.InvoiceId = invoice.InvoiceId;
                parcelsManager.Insert(parcel, condition);
            }


        }
        public Sale SaveSale(Sale sale, Budget budget, Int32 userId, DateTime firstDatePayment, FinancierCondition condition)
        {
            var saleItemlist = (sale.SaleItems != null) ? sale.SaleItems.ToList() : new List<SaleItem>();

            //
            // Save Budget
            //
            if (budget != null)
            {
                Budget originalBudget = GetBudget(budget.BudgetId, budget.CompanyId);
                if (originalBudget != null)
                {
                    originalBudget.CopyPropertiesFrom(budget);
                    DbContext.SubmitChanges();
                }
                else
                    InsertBudget(budget, null);

                sale.BudgetId = budget.BudgetId;

                saleItemlist = CreateSaleItemList(budget.BudgetItems.ToList());
            }


            //
            // Save Sale
            //            
            var parcelList = CreateParcelList(saleItemlist.Sum(i => i.Quantity * i.UnitPrice), firstDatePayment, condition);

            return SaveSale(sale, saleItemlist, userId, parcelList);
        }
        /// <summary>
        /// this method execute the sale(save items and insert an invoice)
        /// </summary>
        /// <param name="sale"></param>
        /// <param name="list"></param>
        /// <param name="userId"></param>
        /// <param name="invoice"></param>
        /// <param name="lstParcels"></param>
        /// <returns></returns>
        public Sale SaveSale(Sale sale, List<SaleItem> list, Int32 userId, List<Parcel> lstParcels)
        {
            sale.CreatedDate = sale.ModifiedDate = DateTime.Now;
            Insert(sale);

            SaveSaleItems(sale, list);

            if (sale.CustomerId.HasValue)
            {
                var invoice = new Invoice();
                invoice.CompanyId = sale.CompanyId;
                invoice.EntryDate = DateTime.Now;
                invoice.CustomerId = sale.CustomerId;
                //invoice.DocumentNumber = sale.SaleId.ToString();
                invoice.Description = "VENDA";
                SaveSaleInvoice(sale, invoice, lstParcels);
            }

            return sale;
        }