private void PostTransactions(int CustomerCode, DateTime Date, int UserCode, DataGridViewRow[] ProductsCollection, decimal SalesTotal, decimal SalesPaied)
        {
            TransactionHeaderManager  header = new TransactionHeaderManager();
            TransactionLineManager    lines = new TransactionLineManager();
            PostedTransactionsManager posted = new PostedTransactionsManager();
            string IssueAccount, ConsumptionAccount, RevenueAccount, SalesDiscountAccount, CustomerAccount, CashAccount;

            CustomerPostingManager.GetCustomerPostingAccountsCode(CustomerCode, out CustomerAccount, out CashAccount);

            long TransactionCode = long.Parse(header.GetCode().ToString());

            #region Add Transaction Header Details :
            header.AddHeaderDetails(TransactionCode, 3, Date, "فاتورة مبيعات", UserCode, true);
            #endregion

            for (int i = 0; i < ProductsCollection.Length; i++)
            {
                #region  Variables :

                int     PCode   = int.Parse(ProductsCollection[i].Cells[0].Value.ToString());
                int     UCode   = int.Parse(ProductsCollection[i].Cells[2].Value.ToString());
                decimal UPrice  = decimal.Parse(ProductsCollection[i].Cells[4].Value.ToString());
                decimal PQty    = decimal.Parse(ProductsCollection[i].Cells[5].Value.ToString());
                decimal PQTotal = decimal.Parse(ProductsCollection[i].Cells[6].Value.ToString());
                decimal AvgProductConsumption = (decimal)(OnHandProductManager.GetAvgCost(PCode) * PQty);
                #endregion

                ProductPostingManager.GetSalesPostingAccountsCode(PCode, out IssueAccount, out ConsumptionAccount, out RevenueAccount, out SalesDiscountAccount);

                #region Add Transaction Details :

                #region If Invoice is Cash Or Accrual :

                #region Add Transaction Line Details :
                lines.AddTransactionLineDetails(TransactionCode, Date, AccountsManager.AccountFilterationTypes.Ledger.ToString(),
                                                CustomerAccount + "-1-" + CustomerCode, PQTotal, null, "من العميل للايرادات",
                                                AccountsManager.AccountFilterationTypes.Ledger.ToString(), RevenueAccount);

                lines.AddTransactionLineDetails(TransactionCode, Date, AccountsManager.AccountFilterationTypes.Ledger.ToString(),
                                                ConsumptionAccount, AvgProductConsumption, null, "من الاستهلاك للمخزون", AccountsManager.AccountFilterationTypes.Ledger.ToString(),
                                                IssueAccount);
                #endregion

                #region Add Posted Lines Details :
                posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, CustomerAccount + "-1-" + CustomerCode, PQTotal, null);

                posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, RevenueAccount, null, PQTotal);

                posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, ConsumptionAccount, AvgProductConsumption, null);

                posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, IssueAccount, null, AvgProductConsumption);
                #endregion

                #endregion

                #endregion
            }

            #region If Invoice is Cash :
            if (radPaymentSystem.SelectedIndex == 0)
            {
                #region Add Transaction Line Details :
                lines.AddTransactionLineDetails(TransactionCode, Date, AccountsManager.AccountFilterationTypes.Ledger.ToString(),
                                                CashAccount, SalesTotal, null, "العميل سدد", AccountsManager.AccountFilterationTypes.Ledger.ToString(),
                                                CustomerAccount + "-1-" + CustomerCode);
                #endregion

                #region Add Posted Lines Details :
                posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, CashAccount, SalesTotal, null);
                posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, CustomerAccount + "-1-" + CustomerCode, null, SalesTotal);
                #endregion
            }
            #endregion

            #region If Invoice is Accrual :
            else if (radPaymentSystem.SelectedIndex == 1 && SalesPaied > 0)
            {
                #region Add Transaction Line Details :
                lines.AddTransactionLineDetails(TransactionCode, Date, AccountsManager.AccountFilterationTypes.Ledger.ToString(),
                                                CashAccount, SalesPaied, null, "العميل سدد", AccountsManager.AccountFilterationTypes.Ledger.ToString(),
                                                CustomerAccount + "-1-" + CustomerCode);
                #endregion

                #region Add Posted Lines Details :
                posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, CashAccount, SalesPaied, null);
                posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, CustomerAccount + "-1-" + CustomerCode, null, SalesPaied);
                #endregion
            }
            #endregion
        }
        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 :
                Date          = DateTime.Now;
                UserCode      = int.Parse(Thread.CurrentPrincipal.Identity.Name);
                WHCode        = int.Parse(cmbWareHouse.SelectedValue.ToString());
                CustomerCode  = int.Parse(cmbCustomer.SelectedValue.ToString());
                SalesTotal    = decimal.Parse(txtInvoiceTotal.Text);
                SalesPaied    = decimal.Parse(spInvoicePaied.Text);
                SalesReminder = decimal.Parse(txtInvoiceReminder.Text);

                #endregion


                ProductsCollection = new DataGridViewRow[dgvProducts.Rows.Count];

                dgvProducts.Rows.CopyTo(ProductsCollection, 0);

                #region Add Invoice Header :
                sales.AddInvoiceHeader(txtInvoiceCode.Text, Date, SalesTotal, SalesPaied, SalesReminder,
                                       radInvoiceType.SelectedIndex == 0 ? false : true,
                                       WHCode, CustomerCode, radPaymentSystem.SelectedIndex == 0 ? false : true, UserCode);
                #endregion



                //string IssueAccount, ConsumptionAccount, RevenueAccount, SalesDiscountAccount, CustomerAccount, CashAccount;
                //CustomerPostingManager.GetCustomerPostingAccountsCode(CustomerCode, out CustomerAccount, out CashAccount);

                //#region Add Transaction Header Details :
                //header.AddHeaderDetails(TransactionCode, 3, Date, "فاتورة مبيعات", 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());
                    decimal AvgProductConsumption = (decimal)(OnHandProductManager.GetAvgCost(PCode) * PQty);
                    #endregion

                    #region Add Purchase Details :
                    sales.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), false);
                    #endregion

                    //ProductPostingManager.GetSalesPostingAccountsCode(PCode, out IssueAccount, out ConsumptionAccount, out RevenueAccount, out SalesDiscountAccount);

                    //#region Add Transaction Details :

                    //#region If Invoice is Cash Or Accrual :

                    //#region Add Transaction Line Details :
                    //lines.AddTransactionLineDetails(TransactionCode, Date, AccountsManager.AccountFilterationTypes.Ledger.ToString(),
                    //    CustomerAccount + "-1-" + CustomerCode, PQTotal, null, "من العميل للايرادات",
                    //    AccountsManager.AccountFilterationTypes.Ledger.ToString(), RevenueAccount);

                    //lines.AddTransactionLineDetails(TransactionCode, Date, AccountsManager.AccountFilterationTypes.Ledger.ToString(),
                    //    ConsumptionAccount, AvgProductConsumption, null, "من الاستهلاك للمخزون", AccountsManager.AccountFilterationTypes.Ledger.ToString(),
                    //    IssueAccount);
                    //#endregion

                    //#region Add Posted Lines Details :
                    //posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, CustomerAccount + "-1-" + CustomerCode, PQTotal, null);

                    //posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, RevenueAccount, null, PQTotal);

                    //posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, ConsumptionAccount, AvgProductConsumption, null);

                    //posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, IssueAccount, null, AvgProductConsumption);
                    //#endregion

                    //#endregion

                    //#endregion
                }

                //#region If Invoice is Cash :
                //if (radPaymentSystem.SelectedIndex == 0)
                //{
                //    #region Add Transaction Line Details :
                //    lines.AddTransactionLineDetails(TransactionCode, Date, AccountsManager.AccountFilterationTypes.Ledger.ToString(),
                //        CashAccount, SalesTotal, null, "العميل سدد", AccountsManager.AccountFilterationTypes.Ledger.ToString(),
                //        CustomerAccount + "-1-" + CustomerCode);
                //    #endregion

                //    #region Add Posted Lines Details :
                //    posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, CashAccount, SalesTotal, null);
                //    posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, CustomerAccount + "-1-" + CustomerCode, null, SalesTotal);
                //    #endregion

                //}
                //#endregion

                //#region If Invoice is Accrual :
                //else if (radPaymentSystem.SelectedIndex == 1 && SalesPaied > 0)
                //{
                //    #region Add Transaction Line Details :
                //    lines.AddTransactionLineDetails(TransactionCode, Date, AccountsManager.AccountFilterationTypes.Ledger.ToString(),
                //        CashAccount, SalesPaied, null, "العميل سدد", AccountsManager.AccountFilterationTypes.Ledger.ToString(),
                //        CustomerAccount + "-1-" + CustomerCode);
                //    #endregion

                //    #region Add Posted Lines Details :
                //    posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, CashAccount, SalesPaied, null);
                //    posted.AddPostedTransactionDetails(TransactionCode, Date, UserCode, CustomerAccount + "-1-" + CustomerCode, null, SalesPaied);
                //    #endregion

                //}
                //#endregion
                isSaved = true;
                btnAddNew_Click(null, null);
                backgroundWorker1.RunWorkerAsync();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }