private void btnUpdate_Click(object sender, EventArgs e)
        {
            var dis =new Disposal();
            var dtbl=dis.GetTransactionByRefNo(_refno);
                if (dis.RowCount > 0)
                {
                    foreach (DataRow datarow in dtbl.Rows)
                    {
                        datarow["RefNo"] = txtRefNo.Text;
                        datarow["Date"] = txtDate.Text;
                    }
                    dis.Save();
                    Close();
                    XtraMessageBox.Show("Refrence No and Date is successfully updated", "Success");
                }

                else
                {
                    XtraMessageBox.Show("There is no refrence no to edit");
                    return;
                }
                this.Close();
        }
        private void deleteToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            var us = new User();
            var userID = MainWindow.LoggedinId;
            us.LoadByPrimaryKey(userID);

            var dr = (DataRowView)lstTree.GetDataRecordByNode(lstTree.FocusedNode);
            if (dr == null) return;
            if (us.UserName != "admin")
            {
                XtraMessageBox.Show("You don't have the privilege to update reference number!", "Caution");
                return;
            }
            if (XtraMessageBox.Show("Are You Sure, You want to delete this?", "Confirmation", MessageBoxButtons.YesNo,
                                    MessageBoxIcon.Question) == DialogResult.Yes)
            {
               var dis = new Disposal();
               DataTable dtbl =  dis.GetTransactionByRefNo((string) dr["RefNo"]);
                foreach (DataRow dataRow in dtbl.Rows)
                {
                    AddlossDeleted(dataRow);
                    dataRow.Delete();
                }
                dis.MarkAsDeleted();
                dis.Save();
                XtraMessageBox.Show("Item successfully deleted.","Success");
            }
        }
        /// <summary>
        /// Delete a row of adjustmen
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var us = new User();
            var userID = MainWindow.LoggedinId;
            us.LoadByPrimaryKey(userID);

            var dataRow = gridView1.GetFocusedDataRow();
            if (dataRow == null) return;

            if (us.UserName != "admin")
            {
                XtraMessageBox.Show("You don't have the privilege to update reference number!", "Caution");
                return;
            }
            //get the primary key of the row
            var ID = Convert.ToInt32(dataRow["ID"]);

            var disposal = new Disposal();
            var receiveDoc = new ReceiveDoc();

            //Retrieve the adjustment with the value of the primary key(id)
            disposal.LoadByPrimaryKey(ID);

             int recieveID = disposal.RecID;

            receiveDoc.LoadByPrimaryKey(recieveID);
            if (XtraMessageBox.Show("Are You Sure, You want to delete this?", "Confirmation", MessageBoxButtons.YesNo,
                                    MessageBoxIcon.Question) == DialogResult.Yes)
            {
                //check for losss

                if (disposal.Losses) //it was loss
                {
                    receiveDoc.LoadByPrimaryKey(recieveID);
                    var s = receiveDoc.Quantity;
                    receiveDoc.Quantity = receiveDoc.Quantity + disposal.Quantity;
                    receiveDoc.QuantityLeft = receiveDoc.QuantityLeft + disposal.Quantity;
                    if (receiveDoc.Out)
                        receiveDoc.Out = false;
                    disposal.Quantity = 0;
                }
                else // it was adjustment
                {
                    receiveDoc.LoadByPrimaryKey(recieveID);
                    var s = receiveDoc.Quantity;
                    receiveDoc.Quantity = receiveDoc.Quantity - disposal.Quantity;
                    receiveDoc.QuantityLeft = receiveDoc.QuantityLeft - disposal.Quantity;
                    if (receiveDoc.Quantity == 0)
                        receiveDoc.Out = true;
                    disposal.Quantity = 0;
                }

                // proceed deletion and make the necessary changes on the database tables.
                DisposalDelete ddel;
                AddDeletedDisposal(disposal, out ddel);

                receiveDoc.Save();
                disposal.MarkAsDeleted();
                disposal.Save();

                //Repopulate the grid
                DataTable dtRec;
                dtFrom.CustomFormat = "MM/dd/yyyy";
                dtTo.CustomFormat = "MM/dd/yyyy";
                DateTime from = ConvertDate.DateConverter(dtFrom.Text);
                DateTime to = ConvertDate.DateConverter(dtTo.Text);
                dtRec = disposal.GetTransactionByDateRange(Convert.ToInt32(cboStores.EditValue), from, to);
                gridAdjustments.DataSource = dtRec;
            }
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            string valid = ValidateFields();
            if (valid == "true")
            {
                if (XtraMessageBox.Show("Are You Sure, You want to save this Transaction?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    Disposal dis = new Disposal();
                    Balance bal = new Balance();
                    ReceiveDoc rec = new ReceiveDoc();
                    DataTable dtAdjVal = (DataTable)AdjustmentGrid.DataSource;
                    for (int i = 0; i < dtAdjVal.Rows.Count; i++)
                    {

                        dis.AddNew();
                        dis.StoreId = Convert.ToInt32(cboStores.EditValue);
                        dis.ItemID = Convert.ToInt32(dtAdjVal.Rows[i]["ID"]);
                        dis.ApprovedBy = txtApprovedBy.Text;
                        DateTime xx = dtAdjustDate.Value;
                        dtAdjustDate.CustomFormat = "MM/dd/yyyy";
                        DateTime dtRec = new DateTime();

                        dis.Date = ConvertDate.DateConverter(dtAdjustDate.Text);
                        dtRec = ConvertDate.DateConverter(dtAdjustDate.Text);

                        dis.RefNo = txtRefNo.Text;
                        dis.BatchNo = dtAdjVal.Rows[i]["Batch No"].ToString();
                        double price = ((Convert.ToDouble(dtAdjVal.Rows[i]["BU Qty"]) != 0) ? (Convert.ToDouble(dtAdjVal.Rows[i]["Price"]) / Convert.ToDouble(dtAdjVal.Rows[i]["BU Qty"])) : 0);
                        dis.Cost = price;
                        dis.Remark = txtRemark.Text;
                        if (dtAdjVal.Rows[i]["Losses"].ToString() != "")
                        {
                            if (Convert.ToInt64(dtAdjVal.Rows[i]["Losses"]) <= Convert.ToInt64(dtAdjVal.Rows[i]["BU Qty"]))
                            {
                                dis.Losses = true;
                                dis.Quantity = Convert.ToInt64(dtAdjVal.Rows[i]["Losses"]);
                            }
                            else
                            {
                                XtraMessageBox.Show("You can't loss more quantity than what you have in the store!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                                ResetFields();
                                return;
                            }
                        }
                        else
                        {
                            dis.Losses = false;
                            dis.Quantity = Convert.ToInt64(dtAdjVal.Rows[i]["Adjustment"]);
                        }
                        dis.ReasonId = Convert.ToInt32(dtAdjVal.Rows[i]["Reason"]);
                        if (VisibilitySetting.HandleUnits == 1)
                        {
                            dis.UnitID = 0;
                        }
                        else if (VisibilitySetting.HandleUnits == 2)
                        {
                            dis.UnitID = Convert.ToInt32(dtRecGrid.Rows[i]["UnitID"]);
                        }
                        else if (VisibilitySetting.HandleUnits == 3)
                        {
                            dis.UnitID = Convert.ToInt32(dtRecGrid.Rows[i]["UnitID"]);
                        }
                        dis.RecID = Convert.ToInt32(dtAdjVal.Rows[i]["RecID"]);
                        dis.EurDate = dtAdjustDate.Value;
                        dis.Save();

                        rec.LoadByPrimaryKey(Convert.ToInt32(dtAdjVal.Rows[i]["RecID"]));

                        if (rec.RowCount > 0)
                        {
                            if (dis.Losses)
                            {
                                rec.QuantityLeft = rec.QuantityLeft - dis.Quantity;
                                if (rec.QuantityLeft == 0)
                                    rec.Out = true;
                                else
                                    rec.Out = false;
                                //  rec.UnitID = Convert.ToInt32(dtAdjVal.Rows[i]["UnitID"]);
                                rec.Save();
                                //Log Activity, ActivityID for save is 1
                                // logger.SaveAction(1, 1, "Transaction\\LossesAdjustment.cs", "Loss/Adjustmet of " + dis.Quantity +" LOSS has been made.");
                            }
                            else
                            {
                                rec.QuantityLeft = rec.QuantityLeft + dis.Quantity;
                                if (rec.QuantityLeft != 0)
                                    rec.Out = false;
                                else
                                    rec.Out = true;
                                rec.Save();
                                //Log Activity, ActivityID for save is 1
                                //logger.SaveAction(1, 1, "Transaction\\LossesAdjustment.cs", "Loss/Adjustmet of " + dis.Quantity + " ADJUSTMENT has been made.");
                            }
                        }

                        dtAdjustDate.Value = xx;
                    }
                    XtraMessageBox.Show("Transaction successfully Saved!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    ResetFields();
                }
            }
            else
            {
                XtraMessageBox.Show(valid, "Validation", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }