/// <summary>
 /// Handles the sorting when the user clicks on the lstStores columns
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void lstStores_ColumnClick(object sender, ColumnClickEventArgs e)
 {
     Stores str = new Stores();
     str.LoadAll();
     if (_sortStore == "ASC")
     {
         str.Sort = "StoreName ASC";
         _sortStore = "DESC";
     }
     else
     {
         str.Sort = "StoreName DESC";
         _sortStore = "ASC";
     }
     PopulateStores(str);
 }
        /// <summary>
        /// Populates the shelf information
        /// </summary>
        /// <param name="dtSlf"></param>
        private void PopulateShelves(DataView dtSlf)
        {
            int count = 1;
            int col = 0;
            lstShelfs.Items.Clear();
            foreach (DataRow dv in dtSlf.ToTable().Rows)
            {
                string[] st = { count.ToString(), dv["ShelfCode"].ToString(), dv["ShelfType"].ToString(), "", "" };
                ListViewItem lst = new ListViewItem(st) {Tag = dv["ID"]};
                if (col != 0)
                {
                    lst.BackColor = Color.FromArgb(233, 247, 248);
                    col = 0;
                }
                else
                    col++;
                lstShelfs.Items.Add(lst);
                count++;
            }

            Stores str = new Stores();
            str.LoadAll();
            cboStore.DataSource = str.DefaultView;
        }
        /// <summary>
        /// Load the drop downs and tables
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void HospitalSettings_Load(object sender, EventArgs e)
        {
            Supplier sup = new Supplier();
            sup.LoadAll();
            PopulateSupplier(sup);

            Stores str = new Stores();
            str.LoadAll();
            PopulateStores(str);

            Shelf slf = new Shelf();
            DataTable dtSlf = slf.GetShelves();
            PopulateShelves(dtSlf.DefaultView);

            ReceivingUnits recUnit = new ReceivingUnits();
            recUnit.LoadAll();
            PopulateReceivingUnit(recUnit);

            DataTable dtdumin = new DataTable();
            dtdumin.Columns.Add("Value");
            dtdumin.Columns.Add("Month");
            object[] objdumin01 = { 0.25, "1 Weeks" };
            dtdumin.Rows.Add(objdumin01);
            object[] objdumin0 = { 0.5, "2 Weeks" };
            dtdumin.Rows.Add(objdumin0);
            object[] objdumin1 = { 0.75, ("3 Weeks") };
            dtdumin.Rows.Add(objdumin1);
            object[] objdumin2 = { 1, (1 + " Month") };
            dtdumin.Rows.Add(objdumin2);
            object[] objdumin3 = { 2, (2 + " Month") };
            dtdumin.Rows.Add(objdumin3);
            cboDUMin.DataSource = dtdumin;

            DataTable dtdumax = new DataTable();
            dtdumax.Columns.Add("Value");
            dtdumax.Columns.Add("Month");
            object[] objdumax01 = { 0.25, "1 Weeks" };
            dtdumax.Rows.Add(objdumax01);
            object[] objdumax010 = { 0.5, "2 Weeks" };
            dtdumax.Rows.Add(objdumax010);
            object[] objdumax011 = { 0.75, ("3 Weeks") };
            dtdumax.Rows.Add(objdumax011);
            object[] objdumax012 = { 1, (1 + " Month") };
            dtdumax.Rows.Add(objdumax012);
            object[] objdumax013 = { 2, (2 + " Month") };
            dtdumax.Rows.Add(objdumax013);
            cboDUMax.DataSource = dtdumax;
        }
 /// <summary>
 /// Saves store information
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void btnStoresSave_Click(object sender, EventArgs e)
 {
     if (txtStore.Text != "")
     {
         Stores str = new Stores();
         if (_storeId != 0)
             str.LoadByPrimaryKey(_storeId);
         else
             str.AddNew();
         str.StoreName = txtStore.Text;
         str.IsActive = chkStoreIsActive.Checked;
         str.Save();
         str.LoadAll();
         PopulateStores(str);
         ResetStores();
     }
     else
     {
         txtStore.BackColor = Color.FromArgb(251, 214, 214);
     }
     XtraMessageBox.Show("Store Record Updated.", "Confirmation", MessageBoxButtons.OK,
                         MessageBoxIcon.Information);
 }
        private void EditIssue_Load(object sender, EventArgs e)
        {
            Stores str = new Stores();
            str.LoadAll();
            cboStores.DataSource = str.DefaultView;

            ReceivingUnits rUnit = new ReceivingUnits();
            rUnit.GetActiveDispensaries();
            cboReceivingUnit.DataSource = rUnit.DefaultView;

            if (_tranId != 0)
            {
                IssueDoc iss = new IssueDoc();
                Items itm = new Items();

                iss.LoadByPrimaryKey(_tranId);

                DataTable dtItm = itm.GetItemById(iss.ItemID);
                string itemName = dtItm.Rows[0]["ItemName"].ToString() + " - " + dtItm.Rows[0]["DosageForm"].ToString() + " - " + dtItm.Rows[0]["Strength"].ToString();

                txtRefNo.Text = iss.RefNo;
                txtBatchNo.Text = iss.BatchNo;
                try
                {
                    txtPack.Text = iss.NoOfPack.ToString();
                    txtQtyPack.Text = iss.QtyPerPack.ToString();
                    txtPrice.Text = (iss.Cost * iss.QtyPerPack).ToString();
                }
                catch
                {
                    txtPack.Text = "0";
                    txtQtyPack.Text = "0";
                    txtPrice.Text = (iss.Cost * 1).ToString();
                }
                txtQuantity.Text = iss.Quantity.ToString();
                DateTime dtDate = Convert.ToDateTime(iss.Date.ToString("MM/dd/yyyy"));
                txtDate.Text = dtDate.ToShortDateString();
                //dtIssDate.Value = DateTime.Now;
                //dtIssDate.CustomFormat = "MM/dd/yyyy";
                //DateTime dtCurrent = Convert.ToDateTime(dtIssDate.Text);

                //long tic = (DateTime.Now.Ticks - dtCurrent.Ticks);
                //DateTime dtIssG = dtDate.AddTicks(tic);

                //dtIssDate.Value = dtIssG;
                cboStores.SelectedValue = iss.StoreId;
                cboReceivingUnit.SelectedValue = iss.ReceivingUnitID;
                txtItemName.Text = itemName;
                txtReceivedBy.Text = iss.IssuedBy;
                txtRemark.Text = iss.Remark;

            }
        }
        /// <summary>
        /// Populates the balance
        /// </summary>
        private void PopulateBalance()
        {
            Stores str = new Stores();
            str.LoadAll();
            cboStore.DataSource = str.DefaultView;

            dtDate.Value = DateTime.Now;

            DateTime dtCurent = new DateTime();//
            dtDate.CustomFormat = "MM/dd/yyyy";
            try
            {
                dtCurent = Convert.ToDateTime(dtDate.Text);
            }
            catch
            {
                string dtValid = "";
                string year = "";
                if (Convert.ToInt32(dtDate.Text.Substring(0, 2)) == 13)
                {
                    dtValid = dtDate.Text;
                    year = dtValid.Substring(dtValid.Length - 4, 4);
                    dtCurent = Convert.ToDateTime("12/30/" + year);
                }
                else if (Convert.ToInt32(dtDate.Text.Substring(0, 2)) == 2)
                {
                    dtValid = dtDate.Text;
                    year = dtValid.Substring(dtValid.Length - 4, 4);
                    dtCurent = Convert.ToDateTime("2/28/" + year);
                }
            }
            for (int i = (dtCurent.Year); i > (dtCurent.Year - 11); i--)
            {
                cboYear.Items.Add(i);
            }
            cboYear.SelectedItem = dtCurent.Year;
        }
        /// <summary>
        /// Load the lookups and Prepare the form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EditReceive_Load(object sender, EventArgs e)
        {
            lkItemUnit.Enabled = VisibilitySetting.HandleUnits != 1;
            var str = new Stores();
            str.LoadAll();
            cboStores.DataSource = str.DefaultView;

            var sup = new Supplier();
            sup.GetActiveSuppliers();
            cboSupplier.DataSource = sup.DefaultView;

            if(_tranId != 0)
            {
                var rec = new ReceiveDoc();
                var itm = new Items();

                rec.LoadByPrimaryKey(_tranId);

                var dtItm = itm.GetItemById(rec.ItemID);

                var units = new ItemUnit();
                var unit = units.LoadFromSQl(rec.ItemID);
                lkItemUnit.Properties.DataSource = unit;
                lkItemUnit.Properties.DisplayMember = "Text";
                lkItemUnit.Properties.ValueMember = "ID";

                var programs = new Programs();
                var allprograms = programs.GetSubPrograms();
                lkPrograms.Properties.DataSource = allprograms;
                lkPrograms.Properties.ValueMember = "ID";
                lkPrograms.Properties.DisplayMember = "Name";

                string itemName = dtItm.Rows[0]["ItemName"].ToString() + " - " + dtItm.Rows[0]["DosageForm"].ToString() + " - " + dtItm.Rows[0]["Strength"].ToString();

                txtRefNo.Text = rec.RefNo;
                txtBatchNo.Text = rec.BatchNo;
                try
                {
                    txtPack.Text = rec.NoOfPack.ToString();
                    txtQtyPack.Text =rec.QtyPerPack.ToString();
                    txtPrice.Text = (rec.Cost * rec.QtyPerPack).ToString();
                    //txtQuantityLeft.Text = rec.QuantityLeft.ToString();
                }
                catch
                {
                    txtPack.Text = "0";
                    txtQtyPack.Text = "0";
                    txtPrice.Text = (rec.Cost * 1).ToString();
                }
                txtQuantity.Text = rec.Quantity.ToString();
                lkPrograms.EditValue = rec.SubProgramID;
                DateTime dtDate = Convert.ToDateTime(rec.Date.ToString("MM/dd/yyyy"));
                txtDate.Text = dtDate.ToShortDateString();
                dtRecDate.Value = DateTime.Now;
                dtRecDate.CustomFormat = "MM/dd/yyyy";
                if (!rec.IsColumnNull("ExpDate"))
                    dtExpiryDate.Value = rec.ExpDate;
                cboStores.SelectedValue = rec.StoreID;
                cboSupplier.SelectedValue = rec.SupplierID;

                if (!rec.IsColumnNull("UnitID"))
                    lkItemUnit.EditValue = rec.UnitID;
                txtItemName.Text = itemName;
                txtReceivedBy.Text = rec.ReceivedBy;
                txtRemark.Text = rec.Remark;

            }
        }