private void PopulateByProgram()
        {
            if (cboSubProgram.SelectedValue != null && cboStores.SelectedValue != null)
            {
                Items itm = new Items();
                DataTable dtItem = new DataTable();
                if (Convert.ToInt32(cboSubProgram.SelectedValue) > 0)
                {

                    dtItem = ((ckExclude.Checked) ? itm.ExcludeNeverReceivedItemsByProgram(Convert.ToInt32(cboSubProgram.SelectedValue), Convert.ToInt32(cboStores.SelectedValue)) : itm.GetItemsByProgram(Convert.ToInt32(cboSubProgram.SelectedValue)));

                }
                else
                {
                      dtItem = (((ckExcNeverIssued.Checked && ckExclude.Checked) ? itm.GetReceivedNotIssuedItems(Convert.ToInt32(cboStores.SelectedValue),Convert.ToInt32(lkCommodityTypes.EditValue)) : (ckExclude.Checked) ? itm.ExcludeNeverReceivedItems(Convert.ToInt32(cboStores.SelectedValue), Convert.ToInt32(lkCommodityTypes.EditValue)) : itm.GetAllItems(1)));
                }
                PopulateItemList(dtItem);
            }
        }
        public DataTable GetECLSItemsStockOut(int storeId, int month, int year)
        {
            Items itm = new Items();
            Programs prog = new Programs();
            prog.GetProgramByName("Family Planning");
            DataTable dtItem = itm.GetItemsByProgram(prog.ID);
            GeneralInfo pipline = new GeneralInfo();
            pipline.LoadAll();
            int min = pipline.Min;
            int max = pipline.Max;
            double eop = pipline.EOP;

            DataTable dt = new DataTable();
            string[] cols = { "ID", "FullItemName", "DosageForm", "Strength", "Unit", "StockCode" };
            foreach (string st in cols)
            {
                dt.Columns.Add(st);
            }
            Balance bal = new Balance();
            foreach (DataRow dr in dtItem.Rows)
            {
                Int64 AMC = bal.CalculateAMC(Convert.ToInt32(dr["ID"]), storeId, month, year);
                Int64 MinCon = AMC * min;
                Int64 maxCon = AMC * max;
                double eopCon = AMC * (eop + 0.25);
                Int64 SOH = bal.GetSOH(Convert.ToInt32(dr["ID"]), storeId, month, year);
                decimal MOS = (AMC != 0) ? (SOH / AMC) : 0;
                Int64 reorder = (maxCon > SOH) ? maxCon - SOH : 0;

                if (SOH == 0)
                {
                    object[] obb = { dr["ID"], dr["ItemName"], dr["DosageForm"], dr["Strength"], dr["Unit"], dr["StockCode"] };
                    dt.Rows.Add(obb);
                }
                //string status = (SOH <= eopCon && SOH > 0) ? "Near EOP" : ((SOH > maxCon) ? "Excess Stock" : ((SOH <= 0) ? "Stock Out" : "Normal"));
            }
            return dt;
        }
        private void PopulateSStatusReport()
        {
            if (curMont != 0 && curYear != 0)
            {
                var storeId = Convert.ToInt32(cboStores.EditValue);
                curYear = Convert.ToInt32(cboYear.EditValue);

                Balance blnc = new Balance();
                DataTable dtbl = new DataTable();
                if (storeId == 0)
                {
                    dtbl = blnc.GetSOHForAllStores(curMont, Convert.ToInt32(cboYear.EditValue));
                }
                else
                {
                    dtbl = blnc.GetSOH(storeId, curMont, Convert.ToInt32(cboYear.EditValue));
                }

                Items itm = new Items();
                Balance bal = new Balance();
                ReceiveDoc rec = new ReceiveDoc();

                Programs prog = new Programs();
                prog.GetProgramByName("Family Planning");
                DataTable dtItm = itm.GetItemsByProgram(prog.ID);
                int totalECLS = dtItm.Rows.Count;
                int stockin, stockout, overstock, nearEOP, belowEOP, freeStockOut;

                if (Convert.ToInt32(lkCategory.EditValue) != 0)
                {
                    stockin = (from m in dtbl.AsEnumerable()
                                   where m["Status"].ToString() == "Normal"
                                   && Convert.ToInt32(m["TypeID"]) == Convert.ToInt32(lkCategory.EditValue) && ((!ckExclude.Checked) || Convert.ToInt32(m["EverReceived"]) == 1)
                                   select m).Count();

                    stockout = (from m in dtbl.AsEnumerable()
                                    where m["Status"].ToString() == "Stock Out"
                                    && Convert.ToInt32(m["TypeID"]) == Convert.ToInt32(lkCategory.EditValue) && ((!ckExclude.Checked) || Convert.ToInt32(m["EverReceived"]) == 1)
                                    select m).Count();

                    overstock = (from m in dtbl.AsEnumerable()
                                     where m["Status"].ToString() == "Over Stocked"
                                     && Convert.ToInt32(m["TypeID"]) == Convert.ToInt32(lkCategory.EditValue) && ((!ckExclude.Checked) || Convert.ToInt32(m["EverReceived"]) == 1)
                                     select m).Count();

                    nearEOP = (from m in dtbl.AsEnumerable()
                                   where m["Status"].ToString() == "Near EOP"
                                   && Convert.ToInt32(m["TypeID"]) == Convert.ToInt32(lkCategory.EditValue) && ((!ckExclude.Checked) || Convert.ToInt32(m["EverReceived"]) == 1)
                                   select m).Count();

                    belowEOP = (from m in dtbl.AsEnumerable()
                                    where m["Status"].ToString() == "Below EOP"
                                    && Convert.ToInt32(m["TypeID"]) == Convert.ToInt32(lkCategory.EditValue) && ((!ckExclude.Checked) || Convert.ToInt32(m["EverReceived"]) == 1)
                                    select m).Count();

                    freeStockOut = (from m in dtbl.AsEnumerable()
                                        where m["Status"].ToString() == "Stock Out"
                                        && Convert.ToInt32(m["TypeID"]) == Convert.ToInt32(lkCategory.EditValue) && ((!ckExclude.Checked) || Convert.ToInt32(m["EverReceived"]) == 1)
                                        select m).Count();
                }
                else
                {
                    stockin = (from m in dtbl.AsEnumerable()
                               where m["Status"].ToString() == "Normal"
                               && ((!ckExclude.Checked) || Convert.ToInt32(m["EverReceived"]) == 1)
                               select m).Count();

                    stockout = (from m in dtbl.AsEnumerable()
                                where m["Status"].ToString() == "Stock Out"
                                && ((!ckExclude.Checked) || Convert.ToInt32(m["EverReceived"]) == 1)
                                select m).Count();

                    overstock = (from m in dtbl.AsEnumerable()
                                 where m["Status"].ToString() == "Over Stocked"
                                 && ((!ckExclude.Checked) || Convert.ToInt32(m["EverReceived"]) == 1)
                                 select m).Count();

                    nearEOP = (from m in dtbl.AsEnumerable()
                               where m["Status"].ToString() == "Near EOP"
                               && ((!ckExclude.Checked) || Convert.ToInt32(m["EverReceived"]) == 1)
                               select m).Count();

                    belowEOP = (from m in dtbl.AsEnumerable()
                                where m["Status"].ToString() == "Below EOP"
                                && ((!ckExclude.Checked) || Convert.ToInt32(m["EverReceived"]) == 1)
                                select m).Count();

                    freeStockOut = (from m in dtbl.AsEnumerable()
                                    where m["Status"].ToString() == "Stock Out"
                                    && ((!ckExclude.Checked) || Convert.ToInt32(m["EverReceived"]) == 1)
                                    select m).Count();
                }

                object[] obj = { stockin, stockout, overstock, nearEOP, belowEOP };
                int totalItm = stockin + stockout + nearEOP + overstock;

                decimal percen = ((totalItm != 0) ? (Convert.ToDecimal(stockin) / Convert.ToDecimal(totalItm)) * 100 : 0);
                percen = Decimal.Round(percen, 0);

                percen = ((totalItm != 0) ? (Convert.ToDecimal(stockout) / Convert.ToDecimal(totalItm)) * 100 : 0);
                percen = Decimal.Round(percen, 0);
                percen = ((totalItm != 0) ? (Convert.ToDecimal(overstock) / Convert.ToDecimal(totalItm)) * 100 : 0);
                percen = Decimal.Round(percen, 0);
                percen = ((totalItm != 0) ? (Convert.ToDecimal(nearEOP) / Convert.ToDecimal(totalItm)) * 100 : 0);
                percen = Decimal.Round(percen, 0);
                percen = ((totalItm != 0) ? (Convert.ToDecimal(belowEOP) / Convert.ToDecimal(totalItm)) * 100 : 0);
                percen = Decimal.Round(percen, 0);
                int totalFree = itm.CountFreeItems();
                percen = ((totalFree != 0) ? (Convert.ToDecimal(freeStockOut) / Convert.ToDecimal(totalFree)) * 100 : 0);
                percen = Decimal.Round(percen, 0);
                totalFree = itm.CountVitalItems();
                GenerateStockStatusPieChart(obj);
            }
        }
 public int CountECLSItemsStockOut(int storeId, int month, int year)
 {
     Items itm = new Items();
     Programs prog = new Programs();
     prog.GetProgramByName("Family Planning");
     DataTable dtItem = itm.GetItemsByProgram(prog.ID);
     GeneralInfo pipline = new GeneralInfo();
     pipline.LoadAll();
     int min = pipline.Min;
     int max = pipline.Max;
     double eop = pipline.EOP;
     int count = 0;
     Balance bal = new Balance();
     foreach (DataRow dr in dtItem.Rows)
     {
         Int64 AMC = bal.CalculateAMC(Convert.ToInt32(dr["ID"]), storeId, month, year);
         Int64 MinCon = AMC * min;
         Int64 maxCon = AMC * max;
         double eopCon = AMC * (eop + 0.25);
         Int64 SOH = bal.GetSOH(Convert.ToInt32(dr["ID"]), storeId, month, year);
         decimal MOS = (AMC != 0) ? (SOH / AMC) : 0;
         Int64 reorder = (maxCon > SOH) ? maxCon - SOH : 0;
         if (SOH == 0)
             count++;
         //string status = (SOH <= eopCon && SOH > 0) ? "Near EOP" : ((SOH > maxCon) ? "Excess Stock" : ((SOH <= 0) ? "Stock Out" : "Normal"));
     }
     return count;
 }
        private void cboStores_SelectedValueChanged(object sender, EventArgs e)
        {
            if (cboStores.SelectedValue != null)
            {
                dtDate.Value = DateTime.Now;
                dtDate.CustomFormat = "MM/dd/yyyy";
                DateTime dtCur = new DateTime();// Convert.ToDateTime(dtDate.Text);
                try
                {
                    dtCur = Convert.ToDateTime(dtDate.Text);
                }
                catch
                {
                    string dtValid = "";
                    string yer = "";
                    if (Convert.ToInt32(dtDate.Text.Substring(0, 2)) == 13)
                    {
                        dtValid = dtDate.Text;
                        yer = dtValid.Substring(dtValid.Length - 4, 4);
                        dtCur = Convert.ToDateTime("12/30/" + yer);
                    }
                    else if (Convert.ToInt32(dtDate.Text.Substring(0, 2)) == 2)
                    {
                        dtValid = dtDate.Text;
                        yer = dtValid.Substring(dtValid.Length - 4, 4);
                        dtCur = Convert.ToDateTime("2/28/" + yer);
                    }
                }
                int month = Convert.ToInt32(cboMonth.SelectedValue);
                int year = (month < 11) ? dtCur.Year : dtCur.Year - 1;

                Programs prog = new Programs();
                prog.GetProgramByName("Family Planning");
                Items itm = new Items();
                DataTable dtItm = itm.GetItemsByProgram(prog.ID);
                PopulateItemListByMonth(dtItm, month,year);
               // PopulateItemListBlance(dtItm);
            }
        }
        private void PopulateSStatusByCategoryAndYearForAllStore()
        {
            if (curMont != 0 && curYear != 0)
            {
                curYear = Convert.ToInt32(cboYear.EditValue);

                Balance blnc = new Balance();
                DataTable dtbl = blnc.GetSOHForAllStores(curMont, curYear);

                Items itm = new Items();
                Balance bal = new Balance();
                ReceiveDoc rec = new ReceiveDoc();

                Programs prog = new Programs();
                prog.GetProgramByName("Family Planning");
                DataTable dtItm = itm.GetItemsByProgram(prog.ID);
                int totalECLS = dtItm.Rows.Count;
                lblNoECLS.Text = totalECLS.ToString();
                int neverRec = rec.CountNeverReceivedItemsByCateogryAndYear(storeId, Convert.ToInt32(lkCategory.EditValue), Convert.ToInt32(cboYear.EditValue));
                int stockin = (from m in dtbl.AsEnumerable()
                               where m["Status"].ToString() == "Normal" && Convert.ToInt32(m["TypeID"]) == Convert.ToInt32(lkCategory.EditValue)
                               && ((ckExclude.Checked)? Convert.ToInt32(m["EverReceived"]) == 1 : true)
                               select m).Count();
                if (stockin == 0)
                {
                    listInStock.Visible = false;
                }
                else
                {
                    listInStock.Visible = true;
                }

                //progressBar1.PerformStep();
                int stockout = (from m in dtbl.AsEnumerable()
                                where m["Status"].ToString() == "Stock Out" && Convert.ToInt32(m["TypeID"]) == Convert.ToInt32(lkCategory.EditValue)
                                && ((ckExclude.Checked) ? Convert.ToInt32(m["EverReceived"]) == 1 : true)
                                select m).Count();
                if (stockout == 0)
                {
                    listStockOut.Visible = false;
                }
                else
                {
                    listStockOut.Visible = true;
                }
                //((ckExclude.Checked)? (bal.CountStockOut(storeId, curMont, curYear)- neverRec) : bal.CountStockOut(storeId, curMont, curYear));
                //progressBar1.PerformStep();
                int overstock = (from m in dtbl.AsEnumerable()
                                 where m["Status"].ToString() == "Over Stocked" && Convert.ToInt32(m["TypeID"]) == Convert.ToInt32(lkCategory.EditValue)
                                 && ((ckExclude.Checked) ? Convert.ToInt32(m["EverReceived"]) == 1 : true)
                                 select m).Count();
                if (overstock == 0)
                {
                    listOverstock.Visible = false;
                }
                else
                {
                    listOverstock.Visible = true;
                }
                // progressBar1.PerformStep();
                int nearEOP = (from m in dtbl.AsEnumerable()
                               where m["Status"].ToString() == "Near EOP" && Convert.ToInt32(m["TypeID"]) == Convert.ToInt32(lkCategory.EditValue)
                               && ((ckExclude.Checked) ? Convert.ToInt32(m["EverReceived"]) == 1 : true)
                               select m).Count();
                if (nearEOP == 0)
                {
                    listNearEOP.Visible = false;
                }
                else
                {
                    listNearEOP.Visible = true;
                }
                //progressBar1.PerformStep();
                int belowEOP = (from m in dtbl.AsEnumerable()
                                where m["Status"].ToString() == "Below EOP" && Convert.ToInt32(m["TypeID"]) == Convert.ToInt32(lkCategory.EditValue)
                                && ((ckExclude.Checked) ? Convert.ToInt32(m["EverReceived"]) == 1 : true)
                                select m).Count();
                if (belowEOP == 0)
                {
                    listBelowEOP.Visible = false;
                }
                else
                {
                    listNearEOP.Visible = true;
                }
                // progressBar1.PerformStep();
                int belowMin = 0;//bal.CountBelowMin(storeId, curMont, curYear);
                // progressBar1.PerformStep();
                int freeStockOut = (from m in dtbl.AsEnumerable()
                                    where m["Status"].ToString() == "Stock Out" && Convert.ToInt32(m["TypeID"]) == Convert.ToInt32(lkCategory.EditValue)
                                    && ((ckExclude.Checked) ? Convert.ToInt32(m["EverReceived"]) == 1 : true)
                                    select m).Count();
                if (freeStockOut == 0)
                {
                    lblFreeStockedout.Visible = false;
                }
                else
                {
                    lblFreeStockedout.Visible = true;
                }
                // progressBar1.PerformStep();
                int vitalStockOut = bal.CountVitalItemsStockOutForAllStore(curMont, curYear);
                //int eclsStockout = bal.CountECLSItemsStockOut(storeId, curMont, curYear);
                // progressBar1.PerformStep();
                object[] obj = { stockin, stockout, overstock, nearEOP, belowEOP };
                int totalItm = stockin + stockout + nearEOP + overstock;

                decimal percen = ((totalItm != 0) ? (Convert.ToDecimal(stockin) / Convert.ToDecimal(totalItm)) * 100 : 0);
                percen = Decimal.Round(percen, 0);
                lblInStock.Text = stockin.ToString() + " (" + percen.ToString("#.0") + "%)";

                percen = ((totalItm != 0) ? (Convert.ToDecimal(stockout) / Convert.ToDecimal(totalItm)) * 100 : 0);
                percen = Decimal.Round(percen, 0);
                lblStockOut.Text = stockout.ToString() + " (" + percen.ToString("#.0") + "%)";

                percen = ((totalItm != 0) ? (Convert.ToDecimal(overstock) / Convert.ToDecimal(totalItm)) * 100 : 0);
                percen = Decimal.Round(percen, 0);
                lblOverStocked.Text = overstock.ToString() + " (" + percen.ToString("#.0") + "%)";

                percen = ((totalItm != 0) ? (Convert.ToDecimal(nearEOP) / Convert.ToDecimal(totalItm)) * 100 : 0);
                percen = Decimal.Round(percen, 0);
                lblNearEOP.Text = nearEOP.ToString() + " (" + percen.ToString("#.0") + "%)";

                percen = ((totalItm != 0) ? (Convert.ToDecimal(belowEOP) / Convert.ToDecimal(totalItm)) * 100 : 0);
                percen = Decimal.Round(percen, 0);
                lblBelowEOP.Text = belowEOP.ToString() + " (" + percen.ToString("#.0") + "%)";

                percen = ((totalItm != 0) ? (Convert.ToDecimal(belowMin) / Convert.ToDecimal(totalItm)) * 100 : 0);
                percen = Decimal.Round(percen, 0);
                lblBelowMin.Text = belowMin.ToString() + " (" + percen.ToString("#.0") + "%)";

                lblFreeStockedout.Text = freeStockOut.ToString();
                if (freeStockOut == 0)
                {
                    linkLabel6.Visible = false;
                }
                else
                {
                    linkLabel6.Visible = true;
                }
                int totalFree = itm.CountFreeItems();
                percen = ((totalFree != 0) ? (Convert.ToDecimal(freeStockOut) / Convert.ToDecimal(totalFree)) * 100 : 0);
                percen = Decimal.Round(percen, 0);
                lblFreeStock.Text = freeStockOut.ToString() + " (" + percen.ToString("#.0") + "%)";

                lblVitalStockedout.Text = vitalStockOut.ToString();
                if (vitalStockOut == 0)
                {
                    linkLabel7.Visible = false;
                }
                else
                {
                    linkLabel7.Visible = true;
                }
                totalFree = itm.CountVitalItems();
                percen = ((totalFree != 0) ? (Convert.ToDecimal(vitalStockOut) / Convert.ToDecimal(totalFree)) * 100 : 0);
                percen = Decimal.Round(percen, 0);
                lblVitalStock.Text = vitalStockOut.ToString() + " (" + percen.ToString("#.0") + "%)";

                //totalFree = itm.CountVitalItems();
                //percen = ((totalECLS != 0)?(Convert.ToDecimal(eclsStockout) / Convert.ToDecimal(totalECLS)) * 100:0);
                //percen = Decimal.Round(percen, 0);
                //lblEclsStock.Text = eclsStockout.ToString() + " (" + percen.ToString("#.0") + "%)";

                GenerateStockStatusPieChart(obj);
            }
        }
 private void linkLabel20_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
 {
     Items itm = new Items();
     Programs prog = new Programs();
     prog.GetProgramByName("Family Planning");
     DataTable dtItm = itm.GetItemsByProgram(prog.ID);
     //groupList.Text = "ECLS Items ";
     PopulateList(dtItm, lstDetail);
 }
 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);
     }
 }