protected void btnSave_Click(object sender, EventArgs e)
        {
            inventoryManager = new InventoryManager(this);
            Inventory inventory = new Inventory();
            original_Inventory = inventoryManager.GetInventory(Company.CompanyId, Convert.ToInt32(Page.ViewState["ProductId"]), Convert.ToInt32(Page.ViewState["DepositId"]));

            if (original_Inventory == null)
                return;
            inventory.CopyPropertiesFrom(original_Inventory);
            inventory.FiscalNumber = txtFiscalNumber.Text;
            inventory.SupplierId = Convert.ToInt32(cboSupplier.SelectedValue);
            inventory.MinimumRequired = ucCurrFieldMinimunRequired.IntValue;
            inventory.Localization = txtLocalization.Text;
            inventory.RealCost = uctxtRealCost.CurrencyValue.Value;
            inventory.Profit = uctxtProfit.CurrencyValue.Value;
            inventory.UnitPrice = uctxtUnitPrice1.CurrencyValue.Value;
            if (!String.IsNullOrEmpty(cboCurrencyRate.SelectedValue))
                inventory.CurrencyRateId = Convert.ToInt32(cboCurrencyRate.SelectedValue);
            if (uctxtUnitPrice2.CurrencyValue.HasValue)
                inventory.UnitPrice2 = uctxtUnitPrice2.CurrencyValue.Value;
            if (uctxtUnitPrice3.CurrencyValue.HasValue)
                inventory.UnitPrice3 = uctxtUnitPrice3.CurrencyValue.Value;
            if (uctxtUnitPrice4.CurrencyValue.HasValue)
                inventory.UnitPrice4 = uctxtUnitPrice4.CurrencyValue.Value;
            if (uctxtUnitPrice5.CurrencyValue.HasValue)
                inventory.UnitPrice5 = uctxtUnitPrice5.CurrencyValue.Value;

            inventoryManager.Update(original_Inventory, inventory);

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "", "parent.location='Inventories.aspx';", true);
        }
 protected void btnAdd_Click(object sender, ImageClickEventArgs e)
 {
     InventoryManager inventoryManager = new InventoryManager(this);
     InventorySerial inventorySerial = new InventorySerial();
     inventorySerial.CompanyId = Company.CompanyId;
     inventorySerial.DepositId = depositId;
     if (ucDtDueDate.DateTime.HasValue)
         inventorySerial.DueDate = ucDtDueDate.DateTime.Value.Date;
     inventorySerial.Lot = txtLot.Text;
     inventorySerial.Serial = txtSerial.Text;
     inventorySerial.InventoryId = inventoryId;
     inventoryManager.InsertInventorySerial(inventorySerial);
     grdInventorySerial.DataBind();
 }
        /// <summary>
        /// this method delete RMA
        /// </summary>
        /// <param name="RMAId"></param>
        public void DeleteRMA(int RMAId)
        {
            /*
             * This method performs all functions 
             * necessary for the exclusion of an RMA
            */

            var inventory = new Inventory();
            var rma = new InventoryRMA();
            var inventoryManager = new InventoryManager(this);
            var history = new InventoryHistory();

            rma = inventoryManager.RetrieveRMA(RMAId); //retrieve the RMA by RMAId
            inventory = inventoryManager.GetProductInventory(rma.CompanyId, rma.ProductId, rma.DepositId);
            //Retrieve The Inventory
            //set InventotyHistory
            history.CompanyId = inventory.CompanyId;
            history.CurrencyRateId = inventory.CurrencyRateId;
            history.DepositId = rma.DepositId;
            history.FiscalNumber = inventory.FiscalNumber;
            history.InventoryEntryTypeId = (int)EntryType.RMA;
            history.Localization = inventory.Localization;
            history.LogDate = DateTime.Now;
            history.MinimumRequired = inventory.MinimumRequired;
            history.ProductId = rma.ProductId;
            history.Profit = inventory.Profit;
            history.Quantity = rma.Quantity;
            history.RealCost = inventory.RealCost;
            history.SupplierId = inventory.SupplierId;
            history.UnitPrice = inventory.UnitPrice;

            //set quantity and AverageCost in inventory
            inventory.Quantity += rma.Quantity;
            inventory.AverageCosts = (((inventory.Quantity * inventory.RealCost) + (rma.Quantity * inventory.RealCost)) /
                                      (inventory.Quantity + rma.Quantity));
            //applay the changes
            inventoryManager.InsertHistory(history);
            DbContext.InventoryRMAs.DeleteOnSubmit(rma);
            DbContext.SubmitChanges();
        }
        public void InventoryDrop(Inventory currentInventory, int? quantity, Int32 dropTypeId, Int32? saleId)
        {
            Inventory originalInventory = GetProductInventory(currentInventory.CompanyId, currentInventory.ProductId,
                                                              currentInventory.DepositId);

            var inventoryManager = new InventoryManager(this);
            if (dropTypeId != (int)DropType.Sell &&
                !inventoryManager.CheckIfCanTransfer(currentInventory.CompanyId, currentInventory.ProductId,
                                                     currentInventory.DepositId, quantity.Value))
                throw new InvalidOperationException();

            //
            //insert one row in InventoryHistory
            //

            InsertHistoryFromInventory(originalInventory, dropTypeId, saleId, null);

            originalInventory.Quantity -= Convert.ToInt32(quantity);
           
            //
            //drop StockComposite
            //
           
            var productManager = new ProductManager(this);

            Product product = productManager.GetProduct(originalInventory.ProductId);

            if (product.DropCompositeInStock)
            {
                List<CompositeProduct> listCompositProduct =
                    productManager.GetChildCompositeProducts(product.ProductId).ToList();
                for (int i = 0; i < listCompositProduct.Count; i++)
                {
                    Inventory compositeInventory = GetProductInventory(currentInventory.CompanyId,
                                                                       listCompositProduct[i].ProductId,
                                                                       originalInventory.DepositId);
                    if (compositeInventory != null)
                        InventoryDrop(compositeInventory, 1, dropTypeId, saleId);
                }
            }

            DbContext.SubmitChanges();
        }
        private decimal? GetProductUnitPrice(Int32 productId, Int32 unitPriceCode)
        {
            Inventory inventory = new InventoryManager(this).GetProductInventory(Company.CompanyId, productId, Deposit.DepositId);
            switch (unitPriceCode)
            {
                case 1:
                    return inventory.UnitPrice;
                    break;
                case 2:
                    return inventory.UnitPrice2;
                    break;
                case 3:
                    return inventory.UnitPrice3;
                    break;
                case 4:
                    return inventory.UnitPrice4;
                    break;
                case 5:
                    return inventory.UnitPrice5;
                    break;

                default: throw new InvalidOperationException();
            }
        }
        protected void AddExistentSaleItem()
        {
            SaleItem saleItem = new SaleItem();
            Product product = new ProductManager(this).GetProductByName((int)Company.MatrixId, selProduct.Name);
            Inventory productInInventory = new InventoryManager(this).GetProductInventory(
                 Company.CompanyId, product.ProductId, Deposit.DepositId);

            saleItem.ProductId = product.ProductId;
            saleItem.Name = selProduct.Name;
            //se o preco na tela for != de null, então ele é convertido e arqmazenado
            //senão verificamos se o preco do produto já existe em inventário, se este for o caso, armazenamos o preco
            //senão, setamos 0 para o price 
            saleItem.Price = ucCurrFieldUnitPrice.CurrencyValue.HasValue ? ucCurrFieldUnitPrice.CurrencyValue.Value : (productInInventory != null ? Convert.ToDecimal(productInInventory.UnitPrice) : Decimal.Zero);
            saleItem.Quantity = ucCurrFieldQuantityData.IntValue;
            if (productInInventory != null)
                saleItem.UnitCost = productInInventory.RealCost;

            SaleItemList.Add(saleItem);
        }
 /// <summary>
 /// this method drop the product in Sale
 /// </summary>
 /// <param name="saleItem"></param>
 private void DropProductInSale(SaleItem saleItem)
 {
     var manager = new InventoryManager(this);
     Inventory inventory;
     if (saleItem.ProductId.HasValue && saleItem.Product.DropCompositeInStock && saleItem.Sale.DepositId.HasValue)
     {
         inventory = manager.GetProductInventory(saleItem.CompanyId, (int)saleItem.ProductId,
                                                 saleItem.Sale.DepositId.Value);
         if (inventory != null)
         {
             inventory.ProductId = saleItem.ProductId.Value;
             inventory.CompanyId = saleItem.CompanyId;
             inventory.DepositId = saleItem.Sale.DepositId.Value;
             manager.InventoryDrop(inventory, saleItem.Quantity, (int)DropType.Sell, saleItem.SaleId);
         }
     }
 }
        /// <summary>
        /// This method cancels a sale and return the products to inventory
        /// </summary>
        /// <param name="saleId"></param>
        /// <param name="matrixId"></param>
        public void CancelSale(int saleId, int matrixId, Int32 userId)
        {
            var inventoryManager = new InventoryManager(this);
            var receiptManager = new ReceiptManager(this);
            var financialManager = new FinancialManager(this);
            var parcelManager = new ParcelsManager(this);

            Sale sale = GetSale(matrixId, saleId);
            sale.IsCanceled = true;

            if (sale.ReceiptId != null)
            {
                Receipt receipt = receiptManager.GetReceipt((int)sale.ReceiptId, sale.CompanyId);
                sale.ReceiptId = null;
                receiptManager.DeleteReceipt(receipt);
                receipt.IsCanceled = true;
            }

            sale.InvoiceId = null;

            //
            // return the products to inventory
            //
            foreach (SaleItem saleItem in sale.SaleItems)
            {
                if (saleItem.ProductId != null)
                {
                    Inventory inventory = inventoryManager.GetProductInventory(saleItem.CompanyId,
                                                                               (int)saleItem.ProductId,
                                                                               (int)sale.DepositId);
                    if (inventory != null)
                        inventoryManager.StockDeposit(inventory, null, userId);
                }
            }

            //
            // Delete the invoice of sale
            //
            if (sale.InvoiceId.HasValue)
                financialManager.DeleteInvoice((int)sale.InvoiceId, sale.CompanyId);

            DbContext.SubmitChanges();
        }
        /// <summary>
        /// this method show the inventory
        /// </summary>
        private void showInventory()
        {
            inventoryManager = new InventoryManager(this);
            original_Inventory = inventoryManager.GetInventory(Company.CompanyId, Convert.ToInt32(Page.ViewState["ProductId"]), Convert.ToInt32(Page.ViewState["DepositId"]));

            lblProductName.Text = original_Inventory.Product.Name;
            lblAverageCost.Text = original_Inventory.AverageCosts.ToString();
            lblQuantity.Text = original_Inventory.Quantity.ToString();
            lblQuantityInReserve.Text = original_Inventory.QuantityInReserve.ToString();

            cboDeposit.SelectedValue = original_Inventory.DepositId.ToString();

            if (original_Inventory.SupplierId.HasValue)
                cboSupplier.SelectedValue = original_Inventory.SupplierId.ToString();

            if (original_Inventory.CurrencyRateId.HasValue)
                cboCurrencyRate.SelectedValue = original_Inventory.CurrencyRateId.ToString();

            txtFiscalNumber.Text = original_Inventory.FiscalNumber;
            txtLocalization.Text = original_Inventory.Localization;

            ucCurrFieldMinimunRequired.CurrencyValue = original_Inventory.MinimumRequired;

            uctxtRealCost.CurrencyValue = original_Inventory.RealCost;
            uctxtProfit.CurrencyValue = original_Inventory.Profit;
            uctxtUnitPrice1.CurrencyValue = original_Inventory.UnitPrice;
            uctxtUnitPrice2.CurrencyValue = original_Inventory.UnitPrice2;
            uctxtUnitPrice3.CurrencyValue = original_Inventory.UnitPrice3;
            uctxtUnitPrice4.CurrencyValue = original_Inventory.UnitPrice4;
            uctxtUnitPrice5.CurrencyValue = original_Inventory.UnitPrice5;
        }