private void GetOrderIdToIncomeForm(DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1 || e.RowIndex == grvReceivableFromCustomer.Rows.Count - 1)
            {
                return;
            }
            DataGridViewRow row             = grvReceivableFromCustomer.Rows[e.RowIndex];
            string          amount          = row.Cells["Amount"].Value.ToString();
            string          incomeAmount    = row.Cells["IncomeAmount"].Value.ToString();
            int             purchaseOrderId = int.Parse(row.Cells["PurchaseReceiptOrderId"].Value.ToString());

            if (decimal.Parse(amount) - decimal.Parse(incomeAmount) == 0)
            {
                MessageBox.Show("Công nợ này đã thanh toán hết!");
                return;
            }

            FormCollection activeForms = Application.OpenForms;
            Form           incomeForm  = new Form();

            foreach (Form f in activeForms)
            {
                if (f.Name == "IncomeForm")
                {
                    incomeForm = f;
                }
            }
            Label lbCostId = incomeForm.Controls.Find("lbCostId", true)[0] as Label;

            lbCostId.Text = "0";

            Label lbOrderId = incomeForm.Controls.Find("lbOrderId", true)[0] as Label;

            JSManagementDataSet.ReceivableFromCustomersDataTable receivableFromCustDataTable = receivableCustomerAdapter.GetReceivableFromCustomersByPurchaseOrderId(purchaseOrderId);

            decimal totalAmount = 0;
            decimal totalIncome = 0;
            int     custId      = 0;

            foreach (DataRow r in receivableFromCustDataTable.Rows)
            {
                totalAmount += decimal.Parse(r.ItemArray[3].ToString());
                totalIncome += decimal.Parse(r.ItemArray[4].ToString());
                custId       = int.Parse(r.ItemArray[6].ToString());
            }

            lbOrderId.Text = purchaseOrderId.ToString();
            TextBox txtreason    = incomeForm.Controls.Find("txtReason", true)[0] as TextBox;
            Label   incomeHeader = incomeForm.Controls.Find(Constant.Income.LABEL_INCOME_HEADER_CONTROL_NAME, true)[0] as Label;

            txtreason.Text = string.Format("Lập {0} tiền ... ngày {1} / Mã số bưu gửi: {2} / Đơn hàng: {3}", incomeHeader.Text, DateTime.Parse(row.Cells["OrderDate"].Value.ToString()).ToShortDateString(), row.Cells["BillNumber"].Value.ToString(), row.Cells["PurchaseReceiptOrderId"].Value.ToString());
            UCTextBoxCurrency txtAmount = incomeForm.Controls.Find("ucTextBoxCurrency1", true)[0] as UCTextBoxCurrency;

            txtAmount.Value = totalAmount - totalIncome;

            CustomerSelectUserControl customerSelectUserControl = incomeForm.Controls.Find("customerSelectUserControl1", true)[0] as CustomerSelectUserControl;

            customerSelectUserControl.Enabled = false;
            customerSelectUserControl.CustId  = custId;

            this.Close();
        }
        private bool InsertIncome(int purchaseOrderId)
        {
            JSManagementDataSet.PurchaseReceiptOrderDataTable    purchaseData   = purchaseReceipOrderAdapter.GetById(purchaseOrderId);
            JSManagementDataSet.ReceivableFromCustomersDataTable receivableData = receivableCustomerAdapter.GetReceivableFromCustomersByPurchaseOrderId(purchaseOrderId);

            if (purchaseData.Rows.Count == 0 || receivableData.Rows.Count == 0)
            {
                return(false);
            }

            string   incomeNumber = purchaseData[0].BillNumber;
            DateTime incomeDate   = DateTime.Now;
            string   payerName    = purchaseData[0].BillNumber;
            string   reason       = string.Empty;
            decimal  amount       = receivableData[0].Amount - receivableData[0].IncomeAmount;

            if (amount > 0)
            {
                reason = string.Format("Thu nợ tiền hàng ngày {0} / Mã số bưu gửi: {1} / Đơn hàng: {2}", DateTime.Parse(purchaseData[0].OrderDate.ToString()).ToShortDateString(), purchaseData[0].BillNumber, purchaseData[0].PurchaseReceiptOrderId.ToString());
            }
            else
            {
                reason = string.Format("Chi tiền chênh lệch ngày {0} / Mã số bưu gửi: {1} / Đơn hàng: {2}", DateTime.Parse(purchaseData[0].OrderDate.ToString()).ToShortDateString(), purchaseData[0].BillNumber, purchaseData[0].PurchaseReceiptOrderId.ToString());
            }
            string   createdBy   = LoginInfor.UserName;
            DateTime createdDate = DateTime.Now;

            int    toBankAccountId = purchaseData[0].BankAccountId;
            string bankAccountName;

            if (toBankAccountId == 0)
            {
                bankAccountName = "Tiền mặt";
            }
            else
            {
                bankAccountName = bankAccountTableAdapter.GetDataById(toBankAccountId)[0].BankAccountName;
            }
            int custId = purchaseData[0].CustId;

            object income = new object();

            //StringBuilder sb = new StringBuilder();
            //sb.AppendLine("Bạn có chắc chắn tạo phiếu thu? ");
            //sb.AppendLine("Mã số bưu gửi: " + incomeNumber == string.Empty? purchaseData[0].CustId.ToString() : incomeNumber);
            //sb.AppendLine("Số tiền: " + amount);
            //sb.AppendLine("Bằng chữ: " + VNCurrency.ToString(amount));
            //sb.AppendLine("Tới tài khoản: " + bankAccountName);

            //DialogResult reviewConfirmMessage = MessageBox.Show(sb.ToString(), "Bạn có chắc chắn?", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

            //if (reviewConfirmMessage == System.Windows.Forms.DialogResult.Cancel)
            //{
            //    return false;
            //}

            income = incomeTableAdapter.InsertIncomeReturnId(
                incomeDate,
                incomeNumber,
                payerName,
                reason,
                amount,
                createdBy,
                createdDate,
                createdBy,
                createdDate,
                null,
                null,
                purchaseOrderId,
                null,
                toBankAccountId, custId);

            return(true);
        }