protected void btnGeneratePurchaseOrder_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(Request["requestItems"]))
            {
                ShowError("É necessário escolher produtos para avançar!");
                return;
            }

            
            int previousCity = -1;
            var productManager = new ProductManager(this);
            foreach (string requestItem in Request["requestItems"].Split(','))
            {
                string[] item = requestItem.Trim().Split('|');
                int productId = Convert.ToInt32(item[0]);
                //int productPackageId = Convert.ToInt32(item[1]);
                //int productManufacturerId = Convert.ToInt32(item[2]);
                //int requestAmount = Convert.ToInt32(item[3]);
                //string centerCost = item[4];
                int cityId = Convert.ToInt32("0" + item[5]);


                Product product = productManager.GetProduct(productId, Company.CompanyId);
                if (product != null)
                    if (product.RequiresAuthorization.Value && (Employee.CentralBuyer != true))
                    {
                        ShowError(product.Name + " é comprado apenas pela sede, requisição encaminhada ao setor de compras!");
                        return;
                    }

                if (previousCity != -1 && cityId != previousCity)
                {
                    ShowError("Não pode gerar um processo de compra para cidades diferentes!");
                    return;
                }

                previousCity = cityId;
            }

            Context.Items["PurchaseRequest"] = true;
            Server.Transfer("PurchaseOrder.aspx");
        }
        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 void ShowProduct()
        {
            _productManager = new ProductManager(this);
            Product product = _productManager.GetProduct(Convert.ToInt32(Page.ViewState["ProductId"]));

            if (product == null)
                product = _productManager.GetTempProduct(Convert.ToInt32(Page.ViewState["ProductId"]), Company.CompanyId);

            if (product == null)
                return;

            if (product.IsTemp == true)
                btnApproveProduct.Visible = true;

            txtProduct.Name = product.Name;
            txtProductCode.Text = product.ProductCode;
            txtBarCode.Text = product.BarCode;
            txtIdentificationOrPlaca.Text = product.IdentificationOrPlaca;
            txtPatrimonioOrRenavam.Text = product.PatrimonioOrRenavam;
            txtSerialNumberOrChassi.Text = product.SerialNumberOrChassi;
            DescriptionTextBox.Value = product.Description;
            ucCurrFieldIPI.CurrencyValue = product.IPI;
            ucCurrFieldICMS.CurrencyValue = product.ICMS;
            ucCurrFieldWarrantyDays.CurrencyValue = product.WarrantyDays;
            txtFiscalClass.Text = product.FiscalClass;
            chkAddCustomerEquipment.Checked = Convert.ToBoolean(product.AddCustomerEquipmentInSale);
            chkAllowNegativeStock.Checked = Convert.ToBoolean(product.AllowNegativeStock);
            chkAllowSaleBelowCost.Checked = Convert.ToBoolean(product.AllowSaleBelowCost);
            chkDropCompositeInStock.Checked = Convert.ToBoolean(product.DropCompositeInStock);
            ChkIsActive.Checked = Convert.ToBoolean(product.IsActive);


            #region non required/opcional fields

            /*
             * Add fields here that may not be used or only used in some companies(_companies)
             * Here is made a verification if there's value in the field of the DataBank to not assign a
             * null value to a control and prevent exceptions
             */


            if (cboCategories != null && product.CategoryId.HasValue)
                cboCategories.SelectedValue = product.CategoryId.ToString();

            if (txtManufacturer != null && product.ManufacturerId.HasValue)
                txtManufacturer.Text = product.Manufacturer.Name;

            if (cboBarCodeType != null && product.BarCodeTypeId.HasValue)
                cboBarCodeType.SelectedValue = product.BarCodeTypeId.ToString();

            if (chkRequiresAuthorization != null && product.RequiresAuthorization.HasValue)
                chkRequiresAuthorization.Checked = (bool)product.RequiresAuthorization;

            if (txtUnit != null)
                txtUnit.Text = product.Unit;

            if (txtPackage != null)
                txtPackage.Text = product.Package;

            if (txtKeyWord != null)
                txtKeyWord.Text = product.Keywords;

            if (chkIsCasting != null)
                chkIsCasting.Checked = Convert.ToBoolean(product.IsCasting);

            if (chkIsEmphasizeInHome != null)
                chkIsEmphasizeInHome.Checked = Convert.ToBoolean(product.EmphasizeInHome);

            if (ucCurrFieldWheight != null)
                ucCurrFieldWheight.CurrencyValue = product.Weight;

            #endregion
        }
 public static bool DeleteProduct(int productid)
 {
     bool result = true;
     using (ProductManager productManager = new ProductManager(null))
     {
         try
         {
             productManager.Delete(productManager.GetProduct(productid));
         }
         catch (System.Data.SqlClient.SqlException e)
         {
             result = false;
         }
     }
     return result;
 }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            _productManager = new ProductManager(this);
            product = new Product();

            if (Page.ViewState["ProductId"] != null)
            {
                originalProduct = _productManager.GetProduct(Convert.ToInt32(Page.ViewState["ProductId"]));
            }
            else if (_productManager.CheckIfProductCodeExists(Company.CompanyId, txtProductCode.Text))
            {
                ShowError(Exception.DuplicateCode);
                return;
            }

            if (originalProduct == null)
                originalProduct = _productManager.GetTempProduct(Convert.ToInt32(Page.ViewState["ProductId"]), Company.CompanyId);

            if (originalProduct != null)
                product.CopyPropertiesFrom(originalProduct);

            product.CompanyId = (Int32)Company.MatrixId;

            if (!String.IsNullOrEmpty(cboCategories.SelectedValue))
                product.CategoryId = Convert.ToInt32(cboCategories.SelectedValue);

            product.Name = txtProduct.Name;
            InsertManufacturer(product);

            product.ProductCode = txtProductCode.Text;
                        
            product.BarCode = txtBarCode.Text;
            product.IdentificationOrPlaca = txtIdentificationOrPlaca.Text;
            product.PatrimonioOrRenavam = txtPatrimonioOrRenavam.Text;

            product.SerialNumberOrChassi = txtSerialNumberOrChassi.Text;
            product.IPI = ucCurrFieldIPI.CurrencyValue;
            product.ICMS = ucCurrFieldICMS.CurrencyValue;
            product.FiscalClass = txtFiscalClass.Text;
            product.WarrantyDays = ucCurrFieldWarrantyDays.IntValue;

            product.Description = DescriptionTextBox.Value;
            product.IsActive = ChkIsActive.Checked;
            product.DropCompositeInStock = Convert.ToBoolean(chkDropCompositeInStock.Checked);
            product.AddCustomerEquipmentInSale = Convert.ToBoolean(chkAddCustomerEquipment.Checked);
            product.AllowNegativeStock = Convert.ToBoolean(chkAllowNegativeStock.Checked);

            product.AllowSaleBelowCost = Convert.ToBoolean(chkAllowSaleBelowCost.Checked);
            product.ModifiedDate = DateTime.Now;
            product.RequiresAuthorization = chkRequiresAuthorization.Checked;

            #region Optional Fields
            /*
             * Add fields here that may not be used or only used in some companies(_companies)
             * Here is made a verification if there's value in the control to not assign a value
             * to the DataBank from a null control and prevent exceptions
             */

            if (!String.IsNullOrEmpty(cboBarCodeType.SelectedValue))
                product.BarCodeTypeId = Convert.ToInt32(cboBarCodeType.SelectedValue);

            if (txtUnit != null)
                product.Unit = txtUnit.Text;

            if (txtKeyWord != null)
                product.Keywords = txtKeyWord.Text;

            if (txtPackage != null)
                product.Package = txtPackage.Text;

            if (chkIsEmphasizeInHome != null)
                product.EmphasizeInHome = chkIsEmphasizeInHome.Checked;

            if (chkIsCasting != null)
                product.IsCasting = chkIsCasting.Checked;

            if (ucCurrFieldWheight != null)
                product.Weight = ucCurrFieldWheight.CurrencyValue;



            #endregion

            if (!String.IsNullOrEmpty(Request["IsTemp"]))
                product.IsTemp = true;

            string redirectUrl = "";

            if (originalProduct == null)
            {
                product.CreatedByUser = User.Identity.UserName;
                _productManager.Insert(product);
            }
            else
            {
                product.ModifiedByUser = User.Identity.UserName;
                _productManager.Update(originalProduct, product);
                redirectUrl = "parent.";
            }

            if (((WebControl)sender).ID == "btnSaveAndNew")
                redirectUrl += "location='Product_General.aspx';";
            else
            {
                if (product.IsTemp == true)
                    redirectUrl += "location='../Purchasing/PurchaseRequests.aspx';";
                else
                    redirectUrl += "location='Product.aspx?ProductId=" + product.ProductId + "'";
            }

            Page.ClientScript.RegisterClientScriptBlock(GetType(), "", redirectUrl, true);
        }