public Boolean InitializeData(string InvoiceId)
        {
            Boolean IsSuccess = true;
            AccountsRecievableDao DAO = new AccountsRecievableDao();
            DataTable dt = DAO.SelectQuery(@"
                                            SELECT

                                            INV.INVOICE_ID,
                                            INV.INVOICE_NO,
                                            INVOICE_DATE,
                                            INV.RECEIVABLE_STATUS,

                                            INV.PERSONNEL_ID,
                                            INV.REMARKS,
                                            INV.DUE_DATE,
                                            INV.DISCOUNT,

                                            INV.ENTRY_DATE,
                                            INV.DATE_UPDATED,
                                            INV.UPDATED_BY,
                                            INV.PAYMENT_TERMS,

                                            INV.ADDED_BY,
                                            INV.AMOUNT,

                                            PER.NAME AS SALES_REPRESENTATIVE,
                                            PER.BRANCH,
                                            CUS.NAME AS CUSTOMER_NAME,
                                            CUS.ADDRESS,
                                            CUS.CONTACT_NO,
                                            CUS.CUSTOMER_ID

                                            FROM RECEIVABLES INV
                                            INNER JOIN PERSONNEL PER ON PER.PERSONNEL_ID = INV.PERSONNEL_ID
                                            INNER JOIN CUSTOMER CUS ON CUS.CUSTOMER_ID = INV.CUSTOMER_ID
                                            WHERE INV.INVOICE_ID = '" + InvoiceId + "'");
            if (dt.Rows.Count < 1)
            {
                IsSuccess = false;
            }
            else
            {
                InvoiceId = dt.Rows[0][0].ToString();
                txtInvoiceNumber.Text = dt.Rows[0][1].ToString();
                pkrInvoiceDate.Value = DateTime.Parse(dt.Rows[0][2].ToString());
                ddlRecievableStatus.Text = GetRecievableStatusDisplay(dt.Rows[0][3].ToString());

                PersonnelId = int.Parse(dt.Rows[0][4].ToString());
                txtRemarks.Text = dt.Rows[0][5].ToString();
                DueDate = DateTime.Parse(dt.Rows[0][6].ToString());
                txtInvoiceDiscount.Text = dt.Rows[0][7].ToString();

                pkrEntryDate.Value = DateTime.Parse(dt.Rows[0][8].ToString());
                DateUpdated = DateTime.Parse(dt.Rows[0][9].ToString());
                UpdatedBy = dt.Rows[0][10].ToString();
                txtPaymentTerms.Value = Int32.Parse(dt.Rows[0][11].ToString());

                AddedBy = dt.Rows[0][12].ToString();
                txtInvoiceTotal.Text = decimal.Parse(dt.Rows[0][13].ToString()).ToString("#,##0.00#");
                txtTotalInvoice.Text = decimal.Parse(dt.Rows[0][13].ToString()).ToString("#,##0.00#");

                txtSalesRepresentative.Text = dt.Rows[0][14].ToString();
                txtBranch.Text = dt.Rows[0][15].ToString();
                txtCustomerName.Text = dt.Rows[0][16].ToString();
                txtAddress.Text = dt.Rows[0][17].ToString();
                txtContactNumber.Text = dt.Rows[0][18].ToString();
                CustomerId = int.Parse(dt.Rows[0][19].ToString());

                DataTable DtItems = DAO.SelectQuery(@"
                                            SELECT

                                            TRA.TRANSACTION_NO,
                                            TRA.PRODUCT_ID,
                                            TRA.QUANTITY,
                                            TRA.DISCOUNT,

                                            PRO.BRAND,
                                            PRO.PRODUCT_CODE,
                                            PRO.UNIT_PRICE

                                            FROM TRANSACTIONS TRA
                                            INNER JOIN PRODUCT PRO ON PRO.PRODUCT_ID = TRA.PRODUCT_ID
                                            WHERE TRA.INVOICE_ID = '" + InvoiceId + "'");

                foreach (DataRow row in DtItems.Rows)
                {
                    string amount = ((decimal.Parse(row[2].ToString()) * decimal.Parse(row[6].ToString())) - decimal.Parse(row[3].ToString())).ToString("#,##0.00#");

                    InvoiceItems.Rows.Add(
                        null,
                        int.Parse(row[2].ToString()),
                        row[5].ToString(),
                        row[4].ToString(),
                        row[6].ToString(),
                        row[3].ToString(),
                        amount,
                        row[0].ToString()
                        );
                }

                DataTable PaymentItems = DAO.SelectQuery(@"
                                            SELECT

                                            PAY.INVOICE_NO,
                                            PAY.PAYMENT_DATE,
                                            PAY.PAYMENT_TYPE,
                                            PAY.REMARKS,
                                            PAY.AMOUNT,
                                            PAY.CHECK_NO,
                                            PAY.CHECK_DATE
                                            FROM PAYMENTS PAY
                                            INNER JOIN RECEIVABLES INV ON INV.INVOICE_NO = PAY.INVOICE_NO
                                                                      AND PAY.INVOICE_NO = '" + txtInvoiceNumber.Text + "'");

                foreach (DataRow row in PaymentItems.Rows)
                {
                    string amount = decimal.Parse(row[4].ToString()).ToString("#,##0.00#");
                    string CheckDetails = "";
                    if (!String.IsNullOrEmpty(row[5].ToString()))
                    {
                        CheckDetails = " - " + row[5].ToString() + " - " + DateTime.Parse(row[6].ToString()).ToString("MMMM dd, yyyy");
                    }

                    dgvPaymentDetails.Rows.Add(
                        null,
                        DateTime.Parse(row[1].ToString()).ToString("MMMM dd, yyyy"),
                        GetPaymentTypeDisplay(row[2].ToString()) + CheckDetails,
                        amount,
                        row[3].ToString()
                        );
                }
                RecalculatePayments();

            }
            return IsSuccess;
        }
        private void SaveInvoiceFunction()
        {
            string ErrorMessage = "";
            ErrorMessage = ValidateInvoice();
            if (ErrorMessage.Length > 0)
            {
                MessageBox.Show("Incomplete / Invalid Invoice Details: \n" + ErrorMessage);
            }
            else
            {
                AccountsRecievableDao DAO = new AccountsRecievableDao();
                if (!DAO.CustomerExist(txtCustomerName.Text))
                {
                    CustomerId = DAO.InsertCustomer(
                        txtCustomerName.Text.ToString().Trim(),
                        txtAddress.Text.ToString().Trim(),
                        Convert.ToInt32(txtContactNumber.Text.ToString().Trim())
                        );
                }
                else
                {
                    var CustomerDetails = new Dictionary<string, object>();
                    CustomerDetails["ADDRESS"] = txtAddress.Text;
                    CustomerDetails["CONTACT_NO"] = txtContactNumber.Text;

                    DAO.UpdateQuery(Constants.TableNames.CUSTOMER, CustomerDetails, "CUSTOMER_ID", CustomerId.ToString());
                }
                if (!DAO.PersonnelExist(txtSalesRepresentative.Text))
                {
                    PersonnelId = DAO.InsertPersonnel(
                       txtSalesRepresentative.Text.ToString().Trim(),
                       txtBranch.Text.ToString().Trim()
                       );
                }
                else
                {
                    var PersonnelDetails = new Dictionary<string, object>();
                    PersonnelDetails["BRANCH"] = txtBranch.Text;

                    DAO.UpdateQuery(Constants.TableNames.PERSONNEL, PersonnelDetails, "PERSONNEL_ID", PersonnelId.ToString());
                }
                //update invoice
                var InvoiceDetails = new Dictionary<string, object>();
                InvoiceDetails["ENTRY_DATE"] = pkrEntryDate.Value;
                InvoiceDetails["INVOICE_DATE"] = pkrInvoiceDate.Value;
                InvoiceDetails["PAYMENT_TERMS"] = txtPaymentTerms.Value;
                InvoiceDetails["RECEIVABLE_STATUS"] = GetRecievableStatusCode(ddlRecievableStatus.SelectedItem.ToString());
                InvoiceDetails["DISCOUNT"] = txtInvoiceDiscount.Text;
                InvoiceDetails["DUE_DATE"] = pkrInvoiceDate.Value.AddDays(int.Parse(txtPaymentTerms.Value.ToString()));
                InvoiceDetails["REMARKS"] = txtRemarks.Text;
                InvoiceDetails["AMOUNT"] = txtInvoiceTotal.Text.Replace(",","");
                InvoiceDetails["PERSONNEL_ID"] = PersonnelId;
                InvoiceDetails["CUSTOMER_ID"] = CustomerId;
                InvoiceDetails["DATE_UPDATED"] = DateTime.Now;
                InvoiceDetails["UPDATED_BY"] = DAO.GetUserId(UserId);

                DAO.UpdateQuery(Constants.TableNames.RECEIVABLES, InvoiceDetails, "INVOICE_NO", txtInvoiceNumber.Text);

                //update transactions
                DAO.ExecuteSQL("DELETE FROM " + Constants.TableNames.TRANSACTIONS + " WHERE INVOICE_ID = '" +InvoiceId.ToString()+ "'");
                int InvoiceDiscount = 0;
                int ProductId = 0;
                int InvoiceItemDiscount = 0;
                int.TryParse(txtInvoiceDiscount.Text, out InvoiceDiscount);
                foreach (DataGridViewRow Row in InvoiceItems.Rows)
                {
                    InvoiceDiscount = 0;
                    if (Row.Cells[Constants.RowColumnCodeForAddInvoice.DISCOUNT].Value != null)
                    {
                        int.TryParse(Row.Cells[Constants.RowColumnCodeForAddInvoice.DISCOUNT].Value.ToString().Trim(), out InvoiceDiscount);
                    }
                    if (Row.Cells[1].Value != null)
                    {
                        if (!DAO.ItemExist(Row.Cells[Constants.RowColumnCodeForAddInvoice.ITEM_CODE].Value.ToString().Trim(), Convert.ToDecimal(Row.Cells[Constants.RowColumnCodeForAddInvoice.PRICE].Value.ToString().Trim())))
                        {
                            ProductId = DAO.InsertItem(Row.Cells[Constants.RowColumnCodeForAddInvoice.ITEM_CODE].Value.ToString().Trim(),
                                Row.Cells[Constants.RowColumnCodeForAddInvoice.BRAND].Value.ToString().Trim(),
                                Convert.ToDecimal(Row.Cells[Constants.RowColumnCodeForAddInvoice.PRICE].Value.ToString().Trim()));
                        }
                        else
                        {
                            ProductId = Convert.ToInt32(DAO.SelectQuery("SELECT PRODUCT_ID FROM " + Constants.TableNames.PRODUCT + " WHERE PRODUCT_CODE ='" + Row.Cells[Constants.RowColumnCodeForAddInvoice.ITEM_CODE].Value.ToString().Trim() + "'").Rows[0][0].ToString());
                        }
                        DAO.InsertTransaction(InvoiceId, ProductId, Convert.ToInt32(Row.Cells[Constants.RowColumnCodeForAddInvoice.QUANTITY].Value.ToString().Trim()), InvoiceDiscount);
                    }

                }
                MessageBox.Show(this, "Invoice Saved.", "Success", MessageBoxButtons.OK);
                this.Close();
            }
        }