private void BtnCreateCSVFile_Click(object sender, EventArgs e)
        {
            if (!CheckUtility.SearchConditionCheck(this, lblDate.LabelText, txtBilling_Date.Text.Trim(), true, Utility.DataType.YEARMONTH, 7, 6))
            {
                return;
            }
            try
            {
                SaveFileDialog saveCSV = new SaveFileDialog();
                saveCSV.Filter = "CSV file (*.csv)|*.csv| All Files (*.*)|*.*";
                DateTime yearMonth  = Convert.ToDateTime(txtBilling_Date.Text);
                String   YEAR_MONTH = yearMonth.ToString("yyyyMM");
                saveCSV.FileName = "NCS" + YEAR_MONTH + "-" + System.DateTime.Now.ToString("yyyyMMddHHmmss");

                frmInvoiceListController oController = new frmInvoiceListController();
                DataTable dt = oController.GetInvoiceListForCSVCreate(txtBilling_Date.Text);

                try
                {
                    string return_message = "";
                    return_message = dt.Rows[0]["Error Message"].ToString();
                    if (!string.IsNullOrEmpty(return_message))
                    {
                        MetroMessageBox.Show(this, "\n" + return_message, "Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                catch (Exception)
                {
                    //Exporting to Excel
                    if (dt.Rows.Count > 0)
                    {
                        if (saveCSV.ShowDialog() == DialogResult.OK) // save as dialog
                        {
                            Utility.WriteToCsvFile(dt, saveCSV.FileName);
                            //MetroMessageBox.Show(this, "\n" + Messages.ComparisonResultDetail.CSVDownloaded, "CSV Downloaded", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                    else
                    {
                        MetroMessageBox.Show(this, "\n" + Messages.ComparisonResultDetail.NoRecordToDownload, "Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
            catch (System.TimeoutException)
            {
                MetroMessageBox.Show(this, "\n" + Messages.General.ServerTimeOut, "Download Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (System.Net.WebException)
            {
                MetroMessageBox.Show(this, "\n" + Messages.General.NoConnection, "Download Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                Utility.WriteErrorLog(ex.Message, ex, false);
                MetroMessageBox.Show(this, "\n" + Messages.General.ThereWasAnError, "Download Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void BtnCreateInvoiceData_Click(object sender, EventArgs e)
        {
            if (!CheckUtility.SearchConditionCheck(this, lblDate.LabelText, txtBilling_Date.Text.Trim(), true, Utility.DataType.YEARMONTH, 7, 6))
            {
                return;
            }
            string status = "0";
            frmInvoiceListController oController = new frmInvoiceListController();
            DataTable dt = oController.CreateInvoiceData(txtBilling_Date.Text, status);

            string return_message = "";
            string count          = "";
            string strMsg         = "";

            try
            {
                return_message = dt.Rows[0]["Error Message"].ToString();
                count          = dt.Rows[0]["Count"].ToString();
            }
            catch (Exception)
            {
            }

            if (!string.IsNullOrEmpty(return_message) && count == "1")
            {
                var confirmResult = MetroMessageBox.Show(this, "\n" + return_message, "Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                if (confirmResult == DialogResult.OK)
                {
                    status = "1";
                    //send to web service
                    frmInvoiceListController controller = new frmInvoiceListController();
                    DataTable dtResult = oController.CreateInvoiceData(txtBilling_Date.Text, status);


                    try
                    {
                        strMsg = dtResult.Rows[0]["Error Message"].ToString();
                        count  = dtResult.Rows[0]["Count"].ToString();
                        //messageInfo = dtResult.Rows[0]["Message Info"].ToString();
                    }
                    catch (Exception ex)
                    {
                    }
                    if (!string.IsNullOrEmpty(strMsg) && !string.IsNullOrEmpty(count))
                    {
                        if (count == "2")
                        {
                            MetroMessageBox.Show(this, "\n" + strMsg, "Success", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                        }

                        if (count == "0")
                        {
                            MetroMessageBox.Show(this, "\n" + strMsg, "Fail", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                        }
                    }
                }
            }
            else
            {
                if (count == "2")
                {
                    MetroMessageBox.Show(this, "\n" + return_message, "Success", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                }
                if (count == "0")
                {
                    MetroMessageBox.Show(this, "\n" + return_message, "Fail", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                }
            }
        }
        private void BindGrid()
        {
            try
            {
                if (!CheckUtility.SearchConditionCheck(this, lblDate.LabelText, txtBilling_Date.Text.Trim(), true, Utility.DataType.YEARMONTH, 7, 6))
                {
                    return;
                }
                //assign search keywords
                DateTime YEAR_MONTH   = Convert.ToDateTime(txtBilling_Date.Text);
                String   strYearMonth = YEAR_MONTH.ToString("yyyyMM");

                //assign search keywords
                frmInvoiceListController oController = new frmInvoiceListController();
                DataSet   ds      = oController.GetInvoiceList(strYearMonth, uIUtility.MetaData.Offset, uIUtility.MetaData.Limit, out uIUtility.MetaData); //need to add more parameter
                DataTable dtList  = ds.Tables[0];
                DataTable dtTotal = ds.Tables[1];

                InvoiceAmountTotal                  = 0;
                keySourceTotal                      = 0;
                SupplierMonthlyUsageFeeTotal        = 0;
                SupplierExpenseTotal                = 0;
                SupplierBrowsingInitialExpenseTotal = 0;
                YearlyUsageFeeTotal                 = 0;
                PostalMailTotal                     = 0;
                WebTotal        = 0;
                EmailTotal      = 0;
                CreditCardTotal = 0;
                OtherTotal      = 0;

                foreach (DataRow row in dtTotal.Rows)
                {
                    keySourceTotal                      += Convert.ToDecimal(row["KEY_SOURCE_MONTHLY_USAGE_FEE"].ToString());
                    SupplierExpenseTotal                += Convert.ToDecimal(row["SUPPLIER_INITIAL_EXPENSE"].ToString());
                    SupplierMonthlyUsageFeeTotal        += Convert.ToDecimal(row["SUPPLIER_MONTHLY_USAGE_FEE"].ToString());
                    SupplierBrowsingInitialExpenseTotal += Convert.ToDecimal(row["BROWSING_INITIAL_EXPENSE"].ToString());
                    YearlyUsageFeeTotal                 += Convert.ToDecimal(row["YEARLY_USAGE_FEE"].ToString());
                    InvoiceAmountTotal                   = keySourceTotal + SupplierExpenseTotal + SupplierMonthlyUsageFeeTotal + SupplierBrowsingInitialExpenseTotal + YearlyUsageFeeTotal;

                    if (row["POSTAL_MAIL"].ToString() == "●")
                    {
                        PostalMailTotal++;
                    }
                    if (row["WEB"].ToString() == "●")
                    {
                        WebTotal++;
                    }
                    if (row["Email"].ToString() == "●")
                    {
                        EmailTotal++;
                    }
                    if (row["CREDIT_CARD"].ToString() == "●")
                    {
                        CreditCardTotal++;
                    }
                    if (row["OTHER"].ToString() == "●")
                    {
                        OtherTotal++;
                    }
                }

                //dgvList.Columns["colCOMPANY_NAME"].HeaderText = "請求金額計";
                //dgvList.Columns["colKEY_SOURCE_MONTHLY_USAGE_FEE"].HeaderText = keySourceTotal.ToString();
                //dgvList.Columns["coLSUPPLIER_INITIAL_EXPENSE"].HeaderText = SupplierExpenseTotal.ToString();
                //dgvList.Columns["colSUPPLIER_MONTHLY_USAGE_FEE"].HeaderText = SupplierMonthlyUsageFeeTotal.ToString();
                //dgvList.Columns["colBROWSING_INITIAL_EXPENSE"].HeaderText = SupplierBrowsingInitialExpenseTotal.ToString();
                //dgvList.Columns["colYEARLY_USAGE_FEE"].HeaderText = YearlyUsageFeeTotal.ToString();

                //dgvList.Columns["coLEmail"].HeaderText = EmailTotal.ToString(); //coLEmail
                //dgvList.Columns["coLPOSTAL_MAIL"].HeaderText = PostalMailTotal.ToString(); //coLEmail
                //dgvList.Columns["colWEB"].HeaderText = WebTotal.ToString(); //coLEmail
                //dgvList.Columns["colCREDIT_CARD"].HeaderText = CreditCardTotal.ToString(); //coLEmail
                //dgvList.Columns["colOTHER"].HeaderText = OtherTotal.ToString(); //coLEmail

                if (dtList.Rows.Count > 0)
                {
                    uIUtility.dtList   = dtList;
                    dgvList.DataSource = uIUtility.dtList;
                    uIUtility.dtOrigin = uIUtility.dtList.Copy();

                    //pagination
                    uIUtility.CalculatePagination(lblcurrentPage, lblTotalPages, lblTotalRecords);
                }
                else
                {
                    //clear data except headers
                    uIUtility.ClearDataGrid();
                    uIUtility.CalculatePagination(lblcurrentPage, lblTotalPages, lblTotalRecords);
                }

                uIUtility.CheckPagination(btnFirst, btnPrev, btnNext, btnLast, lblcurrentPage.Text, lblTotalPages.Text);
            }
            catch (System.TimeoutException)
            {
                MetroMessageBox.Show(this, "\n" + Messages.General.ServerTimeOut, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (System.Net.WebException)
            {
                MetroMessageBox.Show(this, "\n" + Messages.General.NoConnection, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                Utility.WriteErrorLog(ex.Message, ex, false);
                MetroMessageBox.Show(this, "\n" + Messages.General.ThereWasAnError, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }