private void SaveRecord()
		{
            Security.AccessUserDetails clsDetails = (Security.AccessUserDetails)Session["AccessUserDetails"];
            DateTime dteChangeDate = DateTime.Now;

            Products clsProduct = new Products();
            clsProduct.GetConnection();

            InvAdjustment clsInvAdjustment = new InvAdjustment(clsProduct.Connection, clsProduct.Transaction);
            InvAdjustmentDetails clsInvAdjustmentDetails;

            long lngProductID = long.Parse(cboProductCode.SelectedValue);

            if (lstVariationMatrix.Items.Count > 0)
            {
                ProductVariationsMatrix clsProductVariationsMatrix = new ProductVariationsMatrix(clsProduct.Connection, clsProduct.Transaction);
                foreach (DataListItem e in lstVariationMatrix.Items)
                {
                    HtmlInputCheckBox chkMatrixID = (HtmlInputCheckBox)e.FindControl("chkMatrixID");
                    Label lblVariationDesc = (Label)e.FindControl("lblVariationDesc");
                    Label lblUnitNameMatrix = (Label)e.FindControl("lblUnitCode");
                    TextBox txtQuantityBeforeMatrix = (TextBox)e.FindControl("txtQuantityBefore");
                    //TextBox txtDifferenceMatrix = (TextBox)e.FindControl("txtDifference");
                    TextBox txtQuantityNowMatrix = (TextBox)e.FindControl("txtQuantityNow");
                    TextBox txtMinThresholdMatrix = (TextBox)e.FindControl("txtMinThreshold");
                    TextBox txtMaxThresholdMatrix = (TextBox)e.FindControl("txtMaxThreshold");

                    decimal decQuantityBeforeMatrix = decimal.Parse(txtQuantityBeforeMatrix.Text);
                    //decimal decDifferenceMatrix = decimal.Parse(txtDifference.Text);
                    decimal decQuantityNowMatrix = decimal.Parse(txtQuantityNowMatrix.Text);
                    decimal decMinThresholdMatrixBefore = decimal.Parse(txtMinThresholdMatrix.ToolTip);
                    decimal decMinThresholdMatrixNow = decimal.Parse(txtMinThresholdMatrix.Text);
                    decimal decMaxThresholdMatrixBefore = decimal.Parse(txtMaxThresholdMatrix.ToolTip);
                    decimal decMaxThresholdMatrixNow = decimal.Parse(txtMaxThresholdMatrix.Text);

                    if (decQuantityBeforeMatrix != decQuantityNowMatrix || decMinThresholdMatrixBefore != decMinThresholdMatrixNow || decMaxThresholdMatrixBefore != decMaxThresholdMatrixNow)
                    {
                        clsInvAdjustmentDetails = new InvAdjustmentDetails();
                        clsInvAdjustmentDetails.UID = clsDetails.UID;
                        clsInvAdjustmentDetails.InvAdjustmentDate = dteChangeDate;
                        clsInvAdjustmentDetails.ProductID = long.Parse(cboProductCode.SelectedValue);
                        clsInvAdjustmentDetails.ProductCode = cboProductCode.SelectedItem.Text;
                        clsInvAdjustmentDetails.Description = cboProductCode.SelectedItem.Text;
                        clsInvAdjustmentDetails.VariationMatrixID = long.Parse(chkMatrixID.Value);
                        clsInvAdjustmentDetails.MatrixDescription = lblVariationDesc.Text;
                        clsInvAdjustmentDetails.UnitID = int.Parse(lblUnitNameMatrix.ToolTip);
                        clsInvAdjustmentDetails.UnitCode = lblUnitNameMatrix.Text;
                        clsInvAdjustmentDetails.QuantityBefore = decQuantityBeforeMatrix;
                        clsInvAdjustmentDetails.QuantityNow = decQuantityNowMatrix;
                        clsInvAdjustmentDetails.MinThresholdBefore = decMinThresholdMatrixBefore;
                        clsInvAdjustmentDetails.MinThresholdNow = decMinThresholdMatrixNow;
                        clsInvAdjustmentDetails.MaxThresholdBefore = decMaxThresholdMatrixBefore;
                        clsInvAdjustmentDetails.MaxThresholdNow = decMaxThresholdMatrixNow;
                        clsInvAdjustmentDetails.Remarks = txtRemarks.Text;
                        clsInvAdjustment.Insert(clsInvAdjustmentDetails);

                        if (decQuantityBeforeMatrix > decQuantityNowMatrix)
                        { clsProduct.UpdateInvDetails(int.Parse(cboBranch.SelectedItem.Value), lngProductID, long.Parse(chkMatrixID.Value), decQuantityNowMatrix, decMinThresholdMatrixNow, decMaxThresholdMatrixNow, Products.getPRODUCT_INVENTORY_MOVEMENT_VALUE(PRODUCT_INVENTORY_MOVEMENT.DEDUCT_INVENTORY_ADJUSTMENT), dteChangeDate, "SYS-ADJ" + dteChangeDate.ToString("yyyyMMddHHmmss"), clsDetails.Name); }
                        else if (decQuantityBeforeMatrix < decQuantityNowMatrix)
                        { clsProduct.UpdateInvDetails(int.Parse(cboBranch.SelectedItem.Value), lngProductID, long.Parse(chkMatrixID.Value), decQuantityNowMatrix, decMinThresholdMatrixNow, decMaxThresholdMatrixNow, Products.getPRODUCT_INVENTORY_MOVEMENT_VALUE(PRODUCT_INVENTORY_MOVEMENT.ADD_INVENTORY_ADJUSTMENT), dteChangeDate, "SYS-ADJ" + dteChangeDate.ToString("yyyyMMddHHmmss"), clsDetails.Name); }

                    }
                }
            }
            clsProduct.CommitAndDispose();

		}
		private void SetDataSource(ReportDocument Report)
		{
            ReportDataset rptds = new ReportDataset();

            //long lngProductGroupName = long.Parse(cboProductGroup.SelectedItem.Value);
            //long lngSubGroupName = long.Parse(cboSubGroup.SelectedItem.Value);

            string ProductGroupName = string.Empty;
            if (cboProductGroup.SelectedItem.Value != Constants.ZERO_STRING) ProductGroupName = cboProductGroup.SelectedItem.Value;
            string SubGroupName = string.Empty;
            if (cboSubGroup.SelectedItem.Value != Constants.ZERO_STRING) SubGroupName = cboSubGroup.SelectedItem.Value;

            string ReportType = cboReportType.SelectedItem.Text;

            ProductColumns clsProductColumns = new ProductColumns();
            #region clsProductColumns
            clsProductColumns.ProductCode = true;
            clsProductColumns.BarCode = true;
            clsProductColumns.BarCode2 = true;
            clsProductColumns.BarCode3 = true;
            clsProductColumns.ProductDesc = true;
            clsProductColumns.ProductSubGroupName = true;
            clsProductColumns.BaseUnitName = true;
            clsProductColumns.UnitName = true;
            clsProductColumns.ProductGroupName = true;
            clsProductColumns.DateCreated = true;
            clsProductColumns.Price = true;
            clsProductColumns.Quantity = true;
            clsProductColumns.MinThreshold = true;
            clsProductColumns.MaxThreshold = true;
            clsProductColumns.PurchasePrice = true;
            clsProductColumns.SupplierName = true;
            clsProductColumns.QuantityIN = true;
            clsProductColumns.QuantityOUT = true;
            clsProductColumns.RIDMinThreshold = true;
            clsProductColumns.RIDMaxThreshold = true;
            clsProductColumns.RID = true;
            //clsProductColumns.BranchActualQuantity = true;
            //clsProductColumns.BranchQuantity = true;
            //clsProductColumns.BranchQuantityIN = true;
            //clsProductColumns.BranchQuantityOUT = true;
            #endregion

            ProductDetails clsSearchKey = new ProductDetails();
            #region Search Key
            clsSearchKey.BranchID = Convert.ToInt32(cboBranch.SelectedItem.Value);
            clsSearchKey.SupplierID = Convert.ToInt32(cboContact.SelectedItem.Value);
            clsSearchKey.ProductGroupID = Convert.ToInt64(cboProductGroup.SelectedItem.Value);
            clsSearchKey.ProductSubGroupID = Convert.ToInt64(cboSubGroup.SelectedItem.Value);
            clsSearchKey.ProductCode = txtProductCode.Text;
            #endregion

            Products clsProduct = new Products();
            clsProduct.GetConnection();
            ProductInventories clsProductInventories = new ProductInventories(clsProduct.Connection, clsProduct.Transaction);

            
            DataTable dt;
            string ProductIDs = null;

            switch (cboReportType.SelectedValue)
            {
                case ReportTypes.ProductList:
                    #region Products List
                    dt = clsProductInventories.ListAsDataTable(Int32.Parse(cboBranch.SelectedItem.Value), SupplierID: long.Parse(cboContact.SelectedItem.Value), ProductGroupID: long.Parse(cboProductGroup.SelectedItem.Value), ProductSubGroupID: long.Parse(cboSubGroup.SelectedItem.Value), ProductCode: txtProductCode.Text);
                    clsProduct.CommitAndDispose();

                    foreach (System.Data.DataRow dr in dt.Rows)
                    {
                        DataRow drNew = rptds.Products.NewRow();

                        foreach (DataColumn dc in rptds.Products.Columns)
                            drNew[dc] = dr[dc.ColumnName];

                        rptds.Products.Rows.Add(drNew);
                    }
                    break;
                    #endregion

                case ReportTypes.ProductPriceList:
                    #region Products Price List
                    dt = clsProductInventories.ListAsDataTable(int.Parse(cboBranch.SelectedItem.Value), SupplierID: long.Parse(cboContact.SelectedItem.Value), ProductGroupID: long.Parse(cboProductGroup.SelectedItem.Value), ProductSubGroupID: long.Parse(cboSubGroup.SelectedItem.Value), ProductCode: txtProductCode.Text);
                    clsProduct.CommitAndDispose();
                    foreach (DataRow dr in dt.Rows)
                    {
                        DataRow drNew = rptds.Products.NewRow();

                        foreach (DataColumn dc in rptds.Products.Columns)
                            drNew[dc] = dr[dc.ColumnName];

                        rptds.Products.Rows.Add(drNew);
                        ProductIDs += dr["ProductID"].ToString() + ",";
                    }
                    break;
                    #endregion

                case ReportTypes.ProductListWithInvalidMatrix:
                    #region Products List With Invalid Unit Matrix
                    dt = clsProductInventories.ListAsDataTable(int.Parse(cboBranch.SelectedItem.Value), SupplierID: long.Parse(cboContact.SelectedItem.Value), ProductGroupID: long.Parse(cboProductGroup.SelectedItem.Value), ProductSubGroupID: long.Parse(cboSubGroup.SelectedItem.Value), ProductCode: txtProductCode.Text, ShowOnlyWithInvalidUnitMatrix: true);
                    clsProduct.CommitAndDispose();

                    foreach (System.Data.DataRow dr in dt.Rows)
                    {
                        DataRow drNew = rptds.Products.NewRow();

                        foreach (DataColumn dc in rptds.Products.Columns)
                            drNew[dc] = dr[dc.ColumnName];

                        rptds.Products.Rows.Add(drNew);
                    }
                    break;
                    #endregion
                case ReportTypes.WeightedProductsForWeighingScale:
                case ReportTypes.CountedProductsForWeighingScale:
                    #region Weighted and Counted Products For Weighing Scale
                    dt = clsProductInventories.ListAsDataTable(int.Parse(cboBranch.SelectedItem.Value), SupplierID: long.Parse(cboContact.SelectedItem.Value), ProductGroupID: long.Parse(cboProductGroup.SelectedItem.Value), ProductSubGroupID: long.Parse(cboSubGroup.SelectedItem.Value), ProductCode: txtProductCode.Text);
                    clsProduct.CommitAndDispose();
                    foreach (DataRow dr in dt.Rows)
                    {
                        if (dr[ProductColumnNames.BarCode].ToString() != null && dr[ProductColumnNames.BarCode].ToString() != string.Empty)
                        {
                            DataRow drNew = rptds.Products.NewRow();

                            foreach (DataColumn dc in rptds.Products.Columns)
                                drNew[dc] = dr[dc.ColumnName];

                            rptds.Products.Rows.Add(drNew);
                            ProductIDs += dr["ProductID"].ToString() + ",";
                        }
                    }
                    break;
                    #endregion

                case ReportTypes.ProductsInDemoReport:
                    #region Products In Demo
                    DateTime StartTransactionDate = DateTime.TryParse(txtStartDate.Text + " " + txtStartTime.Text, out StartTransactionDate) ? StartTransactionDate : DateTime.MinValue;
                    DateTime EndTransactionDate = DateTime.TryParse(txtEndDate.Text + " " + txtEndTime.Text, out EndTransactionDate) ? EndTransactionDate : DateTime.MinValue;

                    SalesTransactionItems clsSalesTransactionItemsDemo = new SalesTransactionItems();
                    System.Data.DataTable dtDemo = clsSalesTransactionItemsDemo.ProductsInDemoReport(Int32.Parse(cboBranch.SelectedItem.Value), Int64.Parse(cboContact.SelectedItem.Value), cboProductGroup.SelectedItem.Text, cboSubGroup.SelectedItem.Text, txtProductCode.Text, "", StartTransactionDate, EndTransactionDate);
                    clsSalesTransactionItemsDemo.CommitAndDispose();
                    foreach (DataRow dr in dtDemo.Rows)
                    {
                        DataRow drNew = rptds.ProductsInDemo.NewRow();

                        foreach (DataColumn dc in rptds.ProductsInDemo.Columns)
                            drNew[dc] = dr[dc.ColumnName];

                        rptds.ProductsInDemo.Rows.Add(drNew);
                    }
                    break;
                    #endregion
                default:
                    return;

            }

            Report.SetDataSource(rptds); 
			SetParameters(Report);
		}
        private void SynchronizeFromBranch()
        {
            try
            {
                lblError.Text = string.Empty;

                Branch clsBranch = new Branch();
                BranchDetails clsBranchDetails = clsBranch.Details(Convert.ToInt16(cboSynchronizeFromBranch.SelectedItem.Value.ToString()));
                clsBranch.CommitAndDispose();

                if (IPAddress.IsOpen(clsBranchDetails.DBIP, int.Parse(clsBranchDetails.DBPort)) == false)
                {
                    lblError.Text = "Sorry cannot connect to Branch '" + cboSynchronizeFromBranch.SelectedItem.Text + "'. Please check you connection to IP Address :" + clsBranchDetails.DBIP + ". <br /><br />";
                    lblError.Text += "HOW TO CHECK : <br /><br />";
                    lblError.Text += "  1. Open command prompt<br />";
                    lblError.Text += "  2. Type ping[space][IP Address]<br /><br />";
                    lblError.Text += "If the answer is 'Request timed out.' then contact system administrator.<br />";
                    lblError.Text += "Else if the answer is 'Reply...' Follow the next steps.<br /><br />";
                    lblError.Text += "  3. Type telnet[space][IP Address][sapce][IP Port]<br /><br />";

                    return;
                }

                Session.Timeout = 60 * 60 * 30;

                Products clsProduct = new Products();
                clsProduct.GetConnection();
                ProductDetails clsProductDetails;
                ContactGroups clsContactGroup = new ContactGroups(clsProduct.Connection, clsProduct.Transaction);
                Contacts clsContact = new Contacts(clsProduct.Connection, clsProduct.Transaction);
                Data.Unit clsUnit = new Data.Unit(clsProduct.Connection, clsProduct.Transaction);
                Data.ProductGroup clsProductGroup = new Data.ProductGroup(clsProduct.Connection, clsProduct.Transaction);
                Data.ProductSubGroup clsProductSubGroup = new Data.ProductSubGroup(clsProduct.Connection, clsProduct.Transaction);
                Data.Variation clsVariation = new Variation(clsProduct.Connection, clsProduct.Transaction);

                RemoteBranchInventory clsBranchInventory = new RemoteBranchInventory();
                clsBranchInventory.GetConnectionToBranch(clsBranchDetails.DBIP, clsBranchDetails.DBPort);

                Products clsBranchProduct = new Products(clsBranchInventory.Connection, clsBranchInventory.Transaction);
                ProductDetails[] arrBranchProductDetails = clsBranchProduct.List();

                ContactGroups clsBranchContactGroup = new ContactGroups(clsBranchInventory.Connection, clsBranchInventory.Transaction);
                Contacts clsBranchContact = new Contacts(clsBranchInventory.Connection, clsBranchInventory.Transaction);
                Data.Unit clsBranchUnit = new Data.Unit(clsBranchInventory.Connection, clsBranchInventory.Transaction);
                Data.ProductGroup clsBranchProductGroup = new Data.ProductGroup(clsBranchInventory.Connection, clsBranchInventory.Transaction);
                Data.ProductSubGroup clsBranchProductSubGroup = new Data.ProductSubGroup(clsBranchInventory.Connection, clsBranchInventory.Transaction);
                Data.Variation clsBranchVariation = new Variation(clsBranchInventory.Connection, clsBranchInventory.Transaction);
                
                foreach (ProductDetails clsBranchProductDetails in arrBranchProductDetails)
                {
                    clsProductDetails = clsBranchProductDetails;
                    try
                    {
                        clsProductDetails.ProductID = clsProduct.Details(clsProductDetails.BarCode).ProductID;
                        if (clsProductDetails.ProductID != 0)
                        {
                            lblError.Text += clsProductDetails.BarCode + " already exist.<br /><br />";
                            clsProduct.UpdatePurchasing(clsProductDetails.ProductID, clsBranchProductDetails.MatrixID, clsProductDetails.SupplierID, clsProductDetails.BaseUnitID, clsProductDetails.PurchasePrice);
                            clsProduct.UpdateSellingPrice(clsProductDetails.ProductID, clsBranchProductDetails.MatrixID, clsProductDetails.SupplierID, clsProductDetails.BaseUnitID, clsProductDetails.Price, clsProductDetails.Price1, clsProductDetails.Price2, clsProductDetails.Price3, clsProductDetails.Price4, clsProductDetails.Price5);
                        }
                        else
                        {
                            clsProductDetails.ProductID = clsProduct.DetailsByCode(Constants.BRANCH_ID_MAIN, clsProductDetails.BarCode).ProductID;
                            if (clsProductDetails.ProductID != 0)
                            {
                                lblError.Text += clsProductDetails.ProductCode + " already exist.<br /><br />";
                                clsProduct.UpdateBarcode(clsProductDetails.ProductID, clsProductDetails.BarCode);
                                clsProduct.UpdatePurchasing(clsProductDetails.ProductID, clsBranchProductDetails.MatrixID, clsProductDetails.SupplierID, clsProductDetails.BaseUnitID, clsProductDetails.PurchasePrice);
                                clsProduct.UpdateSellingPrice(clsProductDetails.ProductID, clsBranchProductDetails.MatrixID, clsProductDetails.SupplierID, clsProductDetails.BaseUnitID, clsProductDetails.Price, clsProductDetails.Price1, clsProductDetails.Price2, clsProductDetails.Price3, clsProductDetails.Price4, clsProductDetails.Price5);
                            }
                            else
                            {
                                clsProductDetails.SupplierID = clsContact.Details(clsProductDetails.SupplierCode).ContactID;
                                if (clsProductDetails.SupplierID == 0)
                                {
                                    ContactDetails clsBranchContactDetails = clsBranchContact.Details(clsProductDetails.SupplierCode);
                                    if (clsContactGroup.Details(clsBranchContactDetails.ContactGroupID).ContactGroupID == 0)
                                    {
                                        ContactGroupDetails clsBranchContactGroupDetails = clsBranchContactGroup.Details(clsBranchContactDetails.ContactGroupID);
                                        clsBranchContactDetails.ContactGroupID = clsContactGroup.Insert(clsBranchContactGroupDetails);
                                    }
                                    clsProductDetails.SupplierID = clsContact.Insert(clsBranchContactDetails);
                                }

                                clsProductDetails.BaseUnitID = clsUnit.Details(clsProductDetails.BaseUnitCode).UnitID;
                                if (clsProductDetails.BaseUnitID == 0)
                                {
                                    UnitDetails clsBranchUnitDetails = clsBranchUnit.Details(clsBranchProductDetails.BaseUnitID);
                                    clsProductDetails.BaseUnitID = clsUnit.Insert(clsBranchUnitDetails);
                                }

                                clsProductDetails.ProductGroupID = clsProductGroup.Details(clsProductDetails.ProductGroupCode).ProductGroupID;
                                if (clsProductDetails.ProductGroupID == 0)
                                {
                                    ProductGroupDetails clsBranchProductGroupDetails = clsBranchProductGroup.Details(clsBranchProductDetails.ProductGroupID);
                                    clsProductDetails.ProductGroupID = clsProductGroup.Insert(clsBranchProductGroupDetails);
                                }

                                clsProductDetails.ProductSubGroupID = clsProductSubGroup.Details(clsProductDetails.ProductSubGroupCode).ProductSubGroupID;
                                if (clsProductDetails.ProductSubGroupID == 0)
                                {
                                    ProductSubGroupDetails clsBranchProductSubGroupDetails = clsBranchProductSubGroup.Details(clsBranchProductDetails.ProductSubGroupID);
                                    clsProductDetails.ProductSubGroupID = clsProductSubGroup.Insert(clsBranchProductSubGroupDetails);
                                }

                                clsProductDetails.Quantity = 0;
                                clsProductDetails.QuantityIN = 0;
                                clsProductDetails.QuantityOUT = 0;

                                try
                                {
                                    clsProductDetails.ProductID = clsProduct.Insert(clsProductDetails);
                                    lblError.Text += clsProductDetails.ProductCode + " inserted.<br /><br />";
                                }
                                catch (Exception exProduct){
                                    lblError.Text += "<div class=ms-alternating> ERROR INSERTING ITEM: " + clsProductDetails.ProductCode + " err: " + exProduct.Message + ".</div><br /><br />";
                                    if (clsProduct.Connection.State == ConnectionState.Closed)
                                    {
                                        clsProduct = new Products();
                                        clsProduct.GetConnection();
                                        clsContactGroup = new ContactGroups(clsProduct.Connection, clsProduct.Transaction);
                                        clsContact = new Contacts(clsProduct.Connection, clsProduct.Transaction);
                                        clsUnit = new Data.Unit(clsProduct.Connection, clsProduct.Transaction);
                                        clsProductGroup = new Data.ProductGroup(clsProduct.Connection, clsProduct.Transaction);
                                        clsProductSubGroup = new Data.ProductSubGroup(clsProduct.Connection, clsProduct.Transaction);
                                        clsVariation = new Variation(clsProduct.Connection, clsProduct.Transaction);
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception exProducts)
                    {
                        lblError.Text += "<div class=ms-alternating> ERROR INSERTING ITEM: " + clsProductDetails.ProductCode + " err: " + exProducts.Message + ".</div><br /><br />";
                        if (clsProduct.Connection.State == ConnectionState.Closed)
                        {
                            clsProduct = new Products();
                            clsProduct.GetConnection();
                            clsContactGroup = new ContactGroups(clsProduct.Connection, clsProduct.Transaction);
                            clsContact = new Contacts(clsProduct.Connection, clsProduct.Transaction);
                            clsUnit = new Data.Unit(clsProduct.Connection, clsProduct.Transaction);
                            clsProductGroup = new Data.ProductGroup(clsProduct.Connection, clsProduct.Transaction);
                            clsProductSubGroup = new Data.ProductSubGroup(clsProduct.Connection, clsProduct.Transaction);
                            clsVariation = new Variation(clsProduct.Connection, clsProduct.Transaction);
                        }
                    }
                }

                clsProduct.CommitAndDispose();
                clsBranchInventory.CommitAndDispose();

                lblError.Text = "Done synchronizing products from Branch: " + clsBranchDetails.BranchCode + "<br /><br />" + lblError.Text;
            }
            catch (Exception ex)
            {
                lblError.Text += "ERROR WHILE CREATING INSERT STATEMENT: " + ex.Message;
            }
        }