private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                /*
                 * RadInvoiceType ==0    ---> مشتريات (false)
                 * RadInvoiceType ==1    ---> مرتجع مشتريات (true)
                 * RadPaymentSystem ==0  --->كاش (false)
                 * RadPaymentSystem ==1  --->أجل (false)
                 */
                if (dgvProducts.Rows.Count <= 0)
                {
                    return;
                }

                #region Variables :
                DateTime Date             = DateTime.Now;
                int      UserCode         = int.Parse(Thread.CurrentPrincipal.Identity.Name);
                int      WHCode           = int.Parse(cmbWareHouse.SelectedValue.ToString());
                int      VendorCode       = int.Parse(cmbVendor.SelectedValue.ToString());
                decimal  PurchaseTotal    = decimal.Parse(txtInvoiceTotal.Text);
                decimal  PurchasePaied    = decimal.Parse(spInvoicePaied.Text);
                decimal  PurchaseReminder = decimal.Parse(txtInvoiceReminder.Text);
                long     TransactionCode  = long.Parse(header.GetCode().ToString());
                #endregion

                #region Add Invoice Header :
                purchase.AddInvoiceHeader(txtInvoiceCode.Text, Date, PurchaseTotal, PurchasePaied, PurchaseReminder,
                                          radInvoiceType.SelectedIndex == 0 ? false : true,
                                          WHCode, VendorCode, radPaymentSystem.SelectedIndex == 0 ? false : true, UserCode);
                #endregion


                string ProductRecieptAccount, VendorAccount, CashAccount, PurchaseDiscountAccount;
                VendorPostingManager.GetVendorPostingAccountsCode(VendorCode, out VendorAccount, out CashAccount);

                #region Add Transaction Header Details :
                header.AddHeaderDetails(TransactionCode, radInvoiceType.SelectedIndex == 0 ? 1 : 2, Date, radInvoiceType.SelectedIndex == 0 ? "فاتورة مشتريات" : "مرتجع مشتريات", UserCode, true);
                #endregion

                for (int i = 0; i < dgvProducts.Rows.Count; i++)
                {
                    #region  Variables :
                    int     PCode   = int.Parse(dgvProducts.Rows[i].Cells[ProductCode.Name].Value.ToString());
                    int     UCode   = int.Parse(dgvProducts.Rows[i].Cells[UnitCode.Name].Value.ToString());
                    decimal UPrice  = decimal.Parse(dgvProducts.Rows[i].Cells[Price.Name].Value.ToString());
                    decimal PQty    = decimal.Parse(dgvProducts.Rows[i].Cells[Qty.Name].Value.ToString());
                    decimal PQTotal = decimal.Parse(dgvProducts.Rows[i].Cells[Total.Name].Value.ToString());
                    #endregion

                    #region Add Purchase Details :
                    purchase.AddInvoiceDetails(txtInvoiceCode.Text, PCode, UCode, (double)UPrice, PQty, PQTotal);
                    #endregion

                    #region Add OnHandProduct Details :
                    new OnHandProductManager().AddOnHandDetails(PCode, WHCode, PQty, UCode, UPrice, radInvoiceType.SelectedIndex == 0 ? false : true, true);
                    #endregion


                    ProductPostingManager.GetPurchasePostingAccountsCode(PCode, out ProductRecieptAccount, out PurchaseDiscountAccount);

                    #region Add Transaction Details :

                    #region If Invoice is Cash Or Accrual :

                    #region Add Transaction Line Details :


                    if (radInvoiceType.SelectedIndex == 0)
                    {
                        lines.AddTransactionLineDetails(TransactionCode, Date, AccountsManager.AccountFilterationTypes.Ledger.ToString(), ProductRecieptAccount,
                                                        PQTotal, null, "قيد مشتريات", AccountsManager.AccountFilterationTypes.Ledger.ToString(),
                                                        VendorAccount + "-2-" + VendorCode);
                    }
                    else if (radInvoiceType.SelectedIndex == 1)
                    {
                        lines.AddTransactionLineDetails(TransactionCode, Date, AccountsManager.AccountFilterationTypes.Ledger.ToString(), VendorAccount + "-2-" + VendorCode,
                                                        PQTotal, null, "قيد مرتجع مشتريات", AccountsManager.AccountFilterationTypes.Ledger.ToString(),
                                                        ProductRecieptAccount);
                    }


                    #endregion

                    #region Add Posted Lines Details :
                    if (radInvoiceType.SelectedIndex == 0)
                    {
                        posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, ProductRecieptAccount, PQTotal, null);

                        posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, VendorAccount + "-2-" + VendorCode, null, PQTotal);
                    }
                    else if (radInvoiceType.SelectedIndex == 1)
                    {
                        posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, VendorAccount + "-2-" + VendorCode, PQTotal, null);

                        posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, ProductRecieptAccount, null, PQTotal);
                    }
                    #endregion

                    #endregion

                    #endregion
                }

                #region If Invoice is Cash :
                if (radPaymentSystem.SelectedIndex == 0)
                {
                    if (radInvoiceType.SelectedIndex == 0)
                    {
                        #region Add Transaction Line Details :
                        lines.AddTransactionLineDetails(TransactionCode, Date, AccountsManager.AccountFilterationTypes.Ledger.ToString(),
                                                        VendorAccount + "-2-" + VendorCode, PurchaseTotal, null, "سداد المورد", AccountsManager.AccountFilterationTypes.Ledger.ToString(),
                                                        CashAccount);
                        #endregion


                        #region Add Posted Lines Details :
                        posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, VendorAccount + "-2-" + VendorCode, PurchaseTotal, null);

                        posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, CashAccount, null, PurchaseTotal);

                        #endregion
                    }
                    else if (radInvoiceType.SelectedIndex == 1)
                    {
                        #region Add Transaction Line Details :
                        lines.AddTransactionLineDetails(TransactionCode, Date, AccountsManager.AccountFilterationTypes.Ledger.ToString(),
                                                        CashAccount, PurchaseTotal, null,
                                                        "سداد المورد", AccountsManager.AccountFilterationTypes.Ledger.ToString(),
                                                        VendorAccount + "-2-" + VendorCode);
                        #endregion


                        #region Add Posted Lines Details :
                        posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, CashAccount, PurchaseTotal, null);

                        posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, VendorAccount + "-2-" + VendorCode, null, PurchaseTotal);

                        #endregion
                    }
                }
                #endregion

                #region If Invoice is Accrual :
                else if (radPaymentSystem.SelectedIndex == 1 && PurchasePaied > 0)
                {
                    if (radInvoiceType.SelectedIndex == 0)
                    {
                        #region Add Transaction Line Details :
                        lines.AddTransactionLineDetails(TransactionCode, Date, AccountsManager.AccountFilterationTypes.Ledger.ToString(),
                                                        VendorAccount + "-2-" + VendorCode, PurchasePaied, null, "سداد المورد", AccountsManager.AccountFilterationTypes.Ledger.ToString(),
                                                        CashAccount);
                        #endregion

                        #region Add Posted Lines Details :
                        posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, VendorAccount + "-2-" + VendorCode, PurchasePaied, null);

                        posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, CashAccount, null, PurchasePaied);

                        #endregion
                    }
                    else if (radInvoiceType.SelectedIndex == 0)
                    {
                        #region Add Transaction Line Details :
                        lines.AddTransactionLineDetails(TransactionCode, Date, AccountsManager.AccountFilterationTypes.Ledger.ToString(),
                                                        CashAccount, PurchasePaied, null, "سداد المورد", AccountsManager.AccountFilterationTypes.Ledger.ToString(),
                                                        VendorAccount + "-2-" + VendorCode);
                        #endregion

                        #region Add Posted Lines Details :
                        posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, CashAccount, PurchasePaied, null);

                        posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, VendorAccount + "-2-" + VendorCode, null, PurchasePaied);

                        #endregion
                    }
                }
                #endregion

                MessageBox.Show("Success");

                btnAddNew.Enabled = true;
                btnSave.Enabled   = false;
                btnPrint.Enabled  = true;
                btnAddNew_Click(null, null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }