private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
 {
     DataTable dtItem = new DataTable();
     Items itm = new Items();
     selectedType = radioGroup1.EditValue.ToString();
     if (selectedType == "Drug") dtItem = itm.GetAllItems(1);
     else dtItem = itm.GetAllSupply();
     PopulateCatTree(selectedType);
     PopulateItemList(dtItem);
 }
        private void ProductTree_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            Items itm = new Items();
            DataTable dtItem = new DataTable();
            string value = ProductTree.SelectedNode.Name;
            string type = value.Substring(0, 3);
            int len = value.Length - 3;
            int categoryId = Convert.ToInt32(value.Substring(3,len));
            if (type == "cat")
            {
                dtItem = itm.GetItemsByCategory(categoryId);
                lblState.Text = "All Items under " + ProductTree.SelectedNode.Text + " Category";

            }
            else if(type == "sub")
            {
                dtItem = itm.GetItemsBySubCategory(categoryId);
                lblState.Text = "All Items under " + ProductTree.SelectedNode.Text + " Sub Category";
                catID = categoryId;
            }
            else if (type == "sup")
            {
                dtItem = itm.GetSupplyByCategory(categoryId);
                lblState.Text = "All Items under " + ProductTree.SelectedNode.Text + " Category";
                catID = categoryId;
            }
            else if (type == "Als")
            {
                dtItem = itm.GetAllSupply();
                lblState.Text = "All Items under " + ProductTree.SelectedNode.Text + " Category";
                catID = categoryId;
            }
            else
            {
                dtItem = itm.GetAllItems(1);
                lblState.Text = "All Items";
            }
            PopulateItemList(dtItem);
        }
        public DataTable BalanceAllItems(int storeId, int year, int month, string selectedType)
        {
            IssueDoc iss = new IssueDoc();
            ReceiveDoc rec = new ReceiveDoc();
            Disposal dis = new Disposal();
            YearEnd yEnd = new YearEnd();
            Items itm = new Items();
            DataTable dtResult = new DataTable();
            string[] col = { "FullItemName", "BBalance", "BBAmount", "ReceivedQty", "ReceivedAmount", "IssuedQty", "IssuedAmount", "LossesQty", "LossesAmount", "AdjustedQty", "AdjustedAmount", "SOH", "SOHAmount", "Received", "ID", "CategoryId", "SubCategoryID" };
            int i = 0;
            foreach (string s in col)
            {
                if (i > 0)
                {
                    dtResult.Columns.Add(s, typeof(double));
                }
                else
                {
                    dtResult.Columns.Add(s);
                }
                i++;
            }
            DataTable dtItem = ((selectedType == "Drug") ? itm.GetAllItems(1) : itm.GetAllSupply());
            foreach (DataRow dr in dtItem.Rows)
            {
                string itemName = dr["FullItemName"].ToString();// +" - " + dr["DosageForm"].ToString() + " - " + dr["Strength"].ToString();
                int itemId = Convert.ToInt32(dr["ID"]);
                Int64 bb = yEnd.GetBBalance(year, storeId, itemId, month);
                double bbAmount = yEnd.GetBBalanceAmount(year, storeId, itemId, month);
                Int64 recQuant = rec.GetReceivedQuantityTillMonth(itemId, storeId, month, year);
                double recPrice = rec.GetReceivedAmountTillMonth(itemId, storeId, month, year);
                Int64 issuedQuant = iss.GetIssuedQuantityTillMonth(itemId, storeId, month, year);
                double issPrice = iss.GetIssuedAmountTillMonth(itemId, storeId, month, year);
                Int64 lossQuant = dis.GetLossesQuantityTillMonth(itemId, storeId, month, year);
                double lossAmount = dis.GetLossesAmountTillMonth(itemId, storeId, month, year);
                Int64 adjQuant = dis.GetAdjustedQuantityTillMonth(itemId, storeId, month, year);
                double adjAmount = dis.GetAdjustedAmountTillMonth(itemId, storeId, month, year);
                Int64 SOH = bb + recQuant + adjQuant - issuedQuant - lossQuant;
                double SOHAmount = (bbAmount + recPrice + adjAmount - issPrice) - lossAmount;
                if (SOHAmount < 0)
                {
                    ;
                }

                int Isrec = ((bb == 0 && recQuant == 0) ? 0 : 1);
                object[] obj = { itemName, bb, bbAmount, recQuant, recPrice, issuedQuant, issPrice, lossQuant, lossAmount, adjQuant, adjAmount, SOH, SOHAmount, Isrec, itemId, Convert.ToInt32(dr["CategoryId"]), Convert.ToInt32(dr["SubCategoryID"]) };
                dtResult.Rows.Add(obj);
            }
            return dtResult;
        }
        public DataTable OverStockedItems(int storeId, int year, int month, string selectedType, BackgroundWorker bw)
        {
            DataTable dtBal = new DataTable();
            GeneralInfo pipline = new GeneralInfo();
            Items itm = new Items();
            Balance bal = new Balance();
            IssueDoc iss = new IssueDoc();
            ReceiveDoc rec = new ReceiveDoc();
            string[] str = { "FullItemName", "SOH", "AMC", "MOS", "Min", "Max", "ExcessQty", "ExcessAmount", "CategoryId", "SubCategoryID", "ID" };
            foreach (string s in str)
            {
                dtBal.Columns.Add(s);
            }
            pipline.LoadAll();
            int min = pipline.Min;
            int max = pipline.Max;
            double eop = pipline.EOP;
            DataTable dtItem = new DataTable();

            dtItem = ((selectedType == "Drug") ? itm.GetAllItems(1) : itm.GetAllSupply());
            int i = 1;
            foreach (DataRow dr in dtItem.Rows)
            {

                string itemName = dr["FullItemName"].ToString();
                int yer = (month < 11) ? year : year - 1;
                Int64 AMC = bal.CalculateAMC(Convert.ToInt32(dr["ID"]), storeId, month, yer);
                Int64 MinCon = AMC * min;
                Int64 maxCon = AMC * max;
                Int64 SOH = bal.GetSOH(Convert.ToInt32(dr["ID"]), storeId, month, yer);
                if (SOH > maxCon)
                {
                    decimal MOS = (AMC != 0) ? (Convert.ToDecimal(SOH) / Convert.ToDecimal(AMC)) : 0;
                    MOS = Decimal.Round(MOS, 1);

                    Int64 excessQty = SOH - maxCon;
                    double price = rec.GetLastReceivedCost(Convert.ToInt32(dr["ID"]), storeId);
                    double excessAmount = price * excessQty;
                    object[] obj = { itemName, SOH, AMC, MOS, MinCon, maxCon, excessQty, excessAmount, Convert.ToInt32(dr["CategoryId"]), Convert.ToInt32(dr["SubCategoryID"]), Convert.ToInt32(dr["ID"]) };
                    dtBal.Rows.Add(obj);
                }
                bw.ReportProgress(Convert.ToInt32((Convert.ToDouble(i++) / dtItem.Rows.Count) * 100));
            }
            return dtBal;
        }
        //SupplyListToIssue
        public DataTable ItemListToIssue(int storeId, DateTime dtCurrent, string selectedType)
        {
            GeneralInfo pipline = new GeneralInfo();
            DataTable dtList = new DataTable();
            string[] str = { "No", "Stock Code", "FullItemName", "Unit", "SOH", "AMC", "Expired", "CategoryId", "SubCategoryID", "ID", "Status", "IsSelected" };
            foreach (string s in str)
            {
                dtList.Columns.Add(s);
            }
            dtList.Columns[11].DataType = typeof(bool);
            pipline.LoadAll();
            int min = pipline.Min;
            int max = pipline.Max;
            double eop = pipline.EOP;
            // int storeId = (cboStores.SelectedValue != null) ? Convert.ToInt32(cboStores.SelectedValue) : 1;

            int count = 1;
            Balance bal = new Balance();
            Items itmB = new Items();
            DataTable dtItem = ((selectedType == "Drug") ? itmB.GetAllItems(1) : itmB.GetAllSupply());
            int curYear = dtCurrent.Year;
            int curMonth = dtCurrent.Month;
            foreach (DataRow dr in dtItem.Rows)
            {
                int yer = (curMonth < 11) ? curYear : curYear - 1;
                Int64 AMC = bal.CalculateAMC(Convert.ToInt32(dr["ID"]), storeId, curMonth, curYear);
                Int64 MinCon = AMC * min;
                Int64 maxCon = AMC * max;
                double eopCon = AMC * (eop + 0.25);
                double beloweopCon = AMC * (eop - 0.25);
                Int64 soh = bal.GetSOH(Convert.ToInt32(dr["ID"]), storeId, curMonth, curYear);
                string status = (soh <= eopCon && soh > beloweopCon) ? "Near EOP" : ((soh > 0 && soh <= beloweopCon) ? "Below EOP" : ((soh > maxCon && maxCon != 0) ? "Excess Stock" : ((soh <= 0) ? "Stock Out" : "Normal")));//((SOH > beloweopCon && SOH <= MinCon) ? "Below Min" : ((SOH > maxCon && maxCon != 0) ? "Excess Stock" : ((SOH <= 0) ? "Stock Out" : "Normal"));
                string itemName = dr["FullItemName"].ToString();
                Int64 expAmount = itmB.GetExpiredQtyItemsByID(Convert.ToInt32(dr["ID"]), storeId);
                object[] obj = { count.ToString(), dr["StockCode"].ToString(), itemName, dr["Unit"].ToString(), soh, AMC, expAmount, Convert.ToInt32(dr["CategoryId"]), Convert.ToInt32(dr["SubCategoryID"]), Convert.ToInt32(dr["ID"]), status, DBNull.Value };
                dtList.Rows.Add(obj);
                count++;
            }
            return dtList;
        }
        /// <summary>
        /// Loads the form data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ManageItemsLoad(object sender, EventArgs e)
        {
            PopulateCatTree();
            Items itm = new Items();
            DataTable dtItem = itm.GetAllSupply();

            PopulateItemList(dtItem);
            //lblState.Text = "All Items";
        }
 private void PopulateByProgram()
 {
     if (cboSubProgram.SelectedValue != null && cboStores.SelectedValue != null)
     {
         Items itm = new Items();
         DataTable dtItem;
         if (Convert.ToInt32(cboSubProgram.SelectedValue) > 0)
         {
             if (rdDrug.EditValue!=null)
                 dtItem = ((ckExclude.Checked) ? itm.ExcludeNeverReceivedItemsByProgram(Convert.ToInt32(cboSubProgram.SelectedValue), Convert.ToInt32(cboStores.SelectedValue)) : itm.GetItemsByProgram(Convert.ToInt32(cboSubProgram.SelectedValue)));
             else
                 dtItem = ((ckExclude.Checked) ? itm.ExcludeNeverReceivedSuppliesByProgram(Convert.ToInt32(cboSubProgram.SelectedValue), Convert.ToInt32(cboStores.SelectedValue)) : itm.GetSupplyByProgram(Convert.ToInt32(cboSubProgram.SelectedValue)));
         }
         else
         {
             if (rdDrug.EditValue!=null)
                 dtItem = ((ckExclude.Checked) ? itm.ExcludeNeverReceivedItems(Convert.ToInt32(cboStores.SelectedValue),Convert.ToInt32(lkCommodityTypes.EditValue)) : itm.GetAllItems(1));
             else
                 dtItem = ((ckExclude.Checked) ? itm.ExcludeNeverReceivedSupply(Convert.ToInt32(cboStores.SelectedValue)) : itm.GetAllSupply());
         }
         PopulateItemList(dtItem);
     }
 }