Пример #1
0
        private accounting_journal Calculate_PurchaseInvoice(AccountingJournalDB AccountingJournalDB, purchase_invoice purchase_invoice, accounting_cycle accounting_cycle)
        {
            accounting_journal accounting_journal = new accounting_journal();

            if (purchase_invoice.accounting_journal == null)
            {
                accounting_journal.id_cycle   = accounting_cycle.id_cycle;
                accounting_journal.trans_date = purchase_invoice.trans_date;
                accounting_journal.IsSelected = true;
                accounting_journal.State      = EntityState.Added;
                accounting_journal.comment    = purchase_invoice.comment;

                List <accounting_journal_detail> accounting_journal_detailList = new List <accounting_journal_detail>();
                foreach (purchase_invoice_detail purchase_invoice_detail in purchase_invoice.purchase_invoice_detail.ToList())
                {
                    if (purchase_invoice_detail.app_cost_center.is_product && purchase_invoice_detail.item != null)
                    {
                        List <item_tag_detail> item_tag_detailLIST = purchase_invoice_detail.item.item_tag_detail.ToList();
                        if (item_tag_detailLIST != null)
                        {
                            Asset.Inventory Inventory = new Asset.Inventory();

                            accounting_chart INV_Chart = null;
                            foreach (item_tag_detail item_tag_detail in purchase_invoice_detail.item.item_tag_detail.ToList())
                            {
                                item_tag item_tag = item_tag_detail.item_tag;
                                INV_Chart = Inventory.find_Chart(AccountingJournalDB, item_tag);
                            }

                            if (INV_Chart != null)
                            {
                                accounting_journal_detail INV_accounting_journal = new accounting_journal_detail();
                                INV_accounting_journal.accounting_chart = INV_Chart;
                                INV_accounting_journal.trans_date       = purchase_invoice.trans_date;
                                INV_accounting_journal.debit            = Math.Round(purchase_invoice_detail.SubTotal, 2);
                                INV_accounting_journal.id_currencyfx    = purchase_invoice.app_currencyfx.id_currencyfx;
                                accounting_journal_detailList.Add(INV_accounting_journal);
                            }
                        }
                    }
                    else if (purchase_invoice_detail.app_cost_center.is_fixedasset)
                    {
                        //Ignore
                    }
                    else
                    {
                        Expense.AdministrationExpense AdministrationExpense = new Expense.AdministrationExpense();

                        app_cost_center  app_cost_center = purchase_invoice_detail.app_cost_center;
                        accounting_chart Exp_Chart       = AdministrationExpense.find_Chart(AccountingJournalDB, app_cost_center);
                        if (Exp_Chart != null)
                        {
                            accounting_journal_detail INV_accounting_journal = new accounting_journal_detail();
                            INV_accounting_journal.accounting_chart = Exp_Chart;
                            INV_accounting_journal.trans_date       = purchase_invoice.trans_date;
                            INV_accounting_journal.debit            = Math.Round(purchase_invoice_detail.SubTotal, 2);
                            INV_accounting_journal.id_currencyfx    = purchase_invoice.app_currencyfx.id_currencyfx;
                            accounting_journal_detailList.Add(INV_accounting_journal);
                        }
                    }
                }

                Liability.ValueAddedTax VAT = new Liability.ValueAddedTax();
                foreach (purchase_invoice_detail purchase_invoice_detail in purchase_invoice.purchase_invoice_detail.ToList())
                {
                    foreach (app_vat_group_details app_vat_group in purchase_invoice_detail.app_vat_group.app_vat_group_details)
                    {
                        accounting_chart VAT_Chart = VAT.find_Chart(AccountingJournalDB, app_vat_group.app_vat);
                        if (VAT_Chart != null)
                        {
                            accounting_journal_detail INV_accounting_journal = new accounting_journal_detail();
                            INV_accounting_journal.accounting_chart = VAT_Chart;
                            INV_accounting_journal.trans_date       = purchase_invoice.trans_date;
                            INV_accounting_journal.debit            = Math.Round(Vat.calculate_Vat((purchase_invoice_detail.unit_cost * purchase_invoice_detail.quantity), app_vat_group.app_vat.coefficient), 2);
                            INV_accounting_journal.id_currencyfx    = purchase_invoice.app_currencyfx.id_currencyfx;
                            accounting_journal_detailList.Add(INV_accounting_journal);
                        }
                    }
                }

                List <payment_schedual> payment_schedualLIST = AccountingJournalDB.payment_schedual.Where(x => x.id_purchase_invoice == purchase_invoice.id_purchase_invoice).ToList();

                if (payment_schedualLIST != null)
                {
                    ///For Loop for each Payment Schedual.
                    foreach (payment_schedual payment_schedual in payment_schedualLIST)
                    {
                        ///Example: 1000$ Invoice.
                        ///Example: 600$ Paid & 400$ NotPaid.

                        ///Payment Done -> Ex. 600$
                        if (payment_schedual.payment_detail != null && payment_schedual.debit > 0)
                        {
                            Asset.Cash       CashAccount = new Asset.Cash();
                            accounting_chart AR_Chart    = CashAccount.find_Chart(AccountingJournalDB, payment_schedual.payment_detail.app_account);

                            if (AR_Chart != null)
                            {
                                accounting_journal_detail PAYaccounting_journal_detail = new accounting_journal_detail();
                                PAYaccounting_journal_detail.accounting_chart = AR_Chart;
                                PAYaccounting_journal_detail.trans_date       = payment_schedual.trans_date;
                                PAYaccounting_journal_detail.credit           = payment_schedual.debit;
                                PAYaccounting_journal_detail.id_currencyfx    = payment_schedual.app_currencyfx.id_currencyfx;
                                accounting_journal_detailList.Add(PAYaccounting_journal_detail);
                            }
                        }
                        ///Payment Left -> Ex. 400$
                        else if (payment_schedual.payment_detail == null && payment_schedual.credit > 0)
                        {
                            //Credit Payment
                            Liability.AccountsPayable AccountsPayable = new Liability.AccountsPayable();
                            accounting_chart          AR_Chart        = AccountsPayable.find_Chart(AccountingJournalDB, purchase_invoice.contact);

                            if (AR_Chart != null)
                            {
                                accounting_journal_detail AR_accounting_journal_detail = new accounting_journal_detail();
                                AR_accounting_journal_detail.accounting_chart = AR_Chart;
                                AR_accounting_journal_detail.trans_date       = purchase_invoice.trans_date;
                                AR_accounting_journal_detail.credit           = payment_schedual.credit - payment_schedual.child.Sum(x => x.debit);
                                AR_accounting_journal_detail.id_currencyfx    = purchase_invoice.app_currencyfx.id_currencyfx;
                                accounting_journal_detailList.Add(AR_accounting_journal_detail);
                            }
                        }
                    }
                }

                ///Clean up Duplicate Accounts.
                ///If Duplicate, will sum into first of the same chart it Finds.
                foreach (accounting_journal_detail accounting_journal_detail in accounting_journal_detailList)
                {
                    int id_chart = accounting_journal_detail.accounting_chart.id_chart;
                    if (accounting_journal.accounting_journal_detail.Where(x => x.id_chart == id_chart).Count() == 0)
                    {
                        accounting_journal_detail PAYaccounting_journal_detail = new accounting_journal_detail();
                        PAYaccounting_journal_detail.id_chart         = accounting_journal_detail.accounting_chart.id_chart;
                        PAYaccounting_journal_detail.accounting_chart = accounting_journal_detail.accounting_chart;
                        PAYaccounting_journal_detail.trans_date       = accounting_journal_detail.trans_date;
                        PAYaccounting_journal_detail.credit           = Math.Round(accounting_journal_detail.credit, 2);
                        PAYaccounting_journal_detail.debit            = Math.Round(accounting_journal_detail.debit, 2);
                        PAYaccounting_journal_detail.id_currencyfx    = accounting_journal_detail.id_currencyfx;
                        accounting_journal.accounting_journal_detail.Add(PAYaccounting_journal_detail);
                    }
                    else
                    {
                        accounting_journal_detail PAYaccounting_journal_detail = accounting_journal.accounting_journal_detail.Where(x => x.id_chart == id_chart).FirstOrDefault();
                        PAYaccounting_journal_detail.credit += Math.Round(accounting_journal_detail.credit, 2);
                        PAYaccounting_journal_detail.debit  += Math.Round(accounting_journal_detail.debit, 2);
                    }
                }
                accounting_journal.purchase_invoice.Add(purchase_invoice);
            }

            return(accounting_journal);
        }
Пример #2
0
        private accounting_journal calc(AccountingJournalDB context, sales_invoice sales_invoice, accounting_cycle accounting_cycle)
        {
            //List<accounting_journal> ListAccountingJournal = new List<accounting_journal>();

            accounting_journal accounting_journal = new accounting_journal();

            if (sales_invoice.accounting_journal == null)
            {
                // accounting_journal accounting_journal = new accounting_journal();
                accounting_journal.id_cycle   = accounting_cycle.id_cycle;
                accounting_journal.comment    = sales_invoice.comment;
                accounting_journal.trans_date = sales_invoice.trans_date;
                accounting_journal.State      = EntityState.Added;

                List <accounting_journal_detail> accounting_journal_detailList = new List <accounting_journal_detail>();

                //List<sales_invoice_detail> _sales_invoice_detail = sales_invoice.sales_invoice_detail.ToList();
                Asset.Inventory Inventory = new Asset.Inventory();
                foreach (sales_invoice_detail sales_invoice_detail in sales_invoice.sales_invoice_detail.ToList())
                {
                    accounting_chart INV_Chart = null;

                    if (sales_invoice_detail.item != null)
                    {
                        if (sales_invoice_detail.item.item_tag_detail != null)
                        {
                            foreach (item_tag_detail item_tag_detail in sales_invoice_detail.item.item_tag_detail.ToList())
                            {
                                item_tag item_tag = item_tag_detail.item_tag;

                                if (Inventory.find_Chart(context, item_tag) != null)
                                {
                                    INV_Chart = Inventory.find_Chart(context, item_tag);
                                }
                            }
                        }
                    }
                    else
                    {
                        INV_Chart = Inventory.find_Chart(context, null);
                    }

                    if (INV_Chart != null)
                    {
                        accounting_journal_detail INV_accounting_journal = new accounting_journal_detail();
                        INV_accounting_journal.accounting_chart = INV_Chart;
                        if (context.app_currencyfx.Where(x => x.type == app_currencyfx.CurrencyFXTypes.Accounting &&
                                                         x.id_currency == sales_invoice.app_currencyfx.id_currency && x.timestamp <= DateTime.Now)
                            .OrderByDescending(x => x.timestamp).FirstOrDefault() != null)
                        {
                            INV_accounting_journal.id_currencyfx = context.app_currencyfx.Where(x => x.type == app_currencyfx.CurrencyFXTypes.Accounting &&
                                                                                                x.id_currency == sales_invoice.app_currencyfx.id_currency && x.timestamp <= DateTime.Now)
                                                                   .OrderByDescending(x => x.timestamp).FirstOrDefault().id_currencyfx;
                        }
                        else
                        {
                            INV_accounting_journal.id_currencyfx = sales_invoice_detail.sales_invoice.id_currencyfx;
                        }

                        INV_accounting_journal.credit     = sales_invoice_detail.SubTotal;
                        INV_accounting_journal.trans_date = sales_invoice.trans_date;
                        accounting_journal_detailList.Add(INV_accounting_journal);
                    }
                }


                foreach (sales_invoice_detail sales_invoice_detail in sales_invoice.sales_invoice_detail.ToList())
                {
                    foreach (app_vat_group_details app_vat_group_details in sales_invoice_detail.app_vat_group.app_vat_group_details)
                    {
                        Asset.ValueAddedTax VAT       = new Asset.ValueAddedTax();
                        accounting_chart    VAT_Chart = VAT.find_Chart(context, app_vat_group_details.app_vat);
                        if (VAT_Chart != null)
                        {
                            accounting_journal_detail VAT_accounting_journal = new accounting_journal_detail();
                            VAT_accounting_journal.trans_date       = sales_invoice.trans_date;
                            VAT_accounting_journal.accounting_chart = VAT_Chart;
                            VAT_accounting_journal.credit           = Vat.calculate_Vat((sales_invoice_detail.unit_price * sales_invoice_detail.quantity), app_vat_group_details.app_vat.coefficient);
                            if (context.app_currencyfx.Where(x => x.type == app_currencyfx.CurrencyFXTypes.Accounting &&
                                                             x.id_currency == sales_invoice.app_currencyfx.id_currency && x.timestamp <= DateTime.Now)
                                .OrderByDescending(x => x.timestamp).FirstOrDefault() != null)
                            {
                                VAT_accounting_journal.id_currencyfx = context.app_currencyfx.Where(x => x.type == app_currencyfx.CurrencyFXTypes.Accounting &&
                                                                                                    x.id_currency == sales_invoice.app_currencyfx.id_currency && x.timestamp <= DateTime.Now)
                                                                       .OrderByDescending(x => x.timestamp).FirstOrDefault().id_currencyfx;
                            }
                            else
                            {
                                VAT_accounting_journal.id_currencyfx = sales_invoice_detail.sales_invoice.id_currencyfx;
                            }

                            accounting_journal_detailList.Add(VAT_accounting_journal);
                        }
                    }
                }

                List <payment_schedual> payment_schedualLIST = context.payment_schedual.Where(x => x.id_sales_invoice == sales_invoice.id_sales_invoice).ToList();

                if (payment_schedualLIST != null)
                {
                    ///For Loop for each Payment Schedual.
                    foreach (payment_schedual payment_schedual in payment_schedualLIST)
                    {
                        ///Example: 1000$ Invoice.
                        ///Example: 600$ Paid & 400$ NotPaid.

                        ///Payment Done -> Ex. 600$
                        if (payment_schedual.payment_detail != null && payment_schedual.credit > 0)
                        {
                            Asset.Cash       CashAccount = new Asset.Cash();
                            accounting_chart AR_Chart    = CashAccount.find_Chart(context, payment_schedual.payment_detail.app_account);

                            if (AR_Chart != null)
                            {
                                accounting_journal_detail PAYaccounting_journal_detail = new accounting_journal_detail();
                                PAYaccounting_journal_detail.accounting_chart = AR_Chart;
                                PAYaccounting_journal_detail.trans_date       = payment_schedual.trans_date;
                                PAYaccounting_journal_detail.debit            = payment_schedual.credit;
                                if (context.app_currencyfx.Where(x => x.type == app_currencyfx.CurrencyFXTypes.Accounting &&
                                                                 x.id_currency == payment_schedual.app_currencyfx.id_currency && x.timestamp <= DateTime.Now)
                                    .OrderByDescending(x => x.timestamp).FirstOrDefault() != null)
                                {
                                    PAYaccounting_journal_detail.id_currencyfx = context.app_currencyfx.Where(x => x.type == app_currencyfx.CurrencyFXTypes.Accounting &&
                                                                                                              x.id_currencyfx == payment_schedual.id_currencyfx && x.timestamp <= DateTime.Now)
                                                                                 .OrderByDescending(x => x.timestamp).FirstOrDefault().id_currencyfx;
                                }
                                else
                                {
                                    PAYaccounting_journal_detail.id_currencyfx = payment_schedual.id_currencyfx;
                                }

                                accounting_journal_detailList.Add(PAYaccounting_journal_detail);
                            }
                        }
                        ///Payment Left -> Ex. 400$
                        else if (payment_schedual.payment_detail == null && payment_schedual.debit > 0)
                        {
                            //Credit Payment
                            Asset.AccountsReceivable AccountsReceivable = new Asset.AccountsReceivable();
                            accounting_chart         AR_Chart           = AccountsReceivable.find_Chart(context, sales_invoice.contact);

                            if (AR_Chart != null)
                            {
                                accounting_journal_detail AR_accounting_journal_detail = new accounting_journal_detail();
                                AR_accounting_journal_detail.accounting_chart = AR_Chart;
                                AR_accounting_journal_detail.trans_date       = sales_invoice.trans_date;
                                AR_accounting_journal_detail.debit            = payment_schedual.debit - payment_schedual.child.Sum(x => x.credit);
                                if (context.app_currencyfx.Where(x => x.type == app_currencyfx.CurrencyFXTypes.Accounting &&
                                                                 x.id_currency == payment_schedual.app_currencyfx.id_currency && x.timestamp <= DateTime.Now)
                                    .OrderByDescending(x => x.timestamp).FirstOrDefault() != null)
                                {
                                    AR_accounting_journal_detail.id_currencyfx = context.app_currencyfx.Where(x => x.type == app_currencyfx.CurrencyFXTypes.Accounting &&
                                                                                                              x.id_currency == payment_schedual.app_currencyfx.id_currency && x.timestamp <= DateTime.Now)
                                                                                 .OrderByDescending(x => x.timestamp).FirstOrDefault().id_currencyfx;
                                }
                                else
                                {
                                    AR_accounting_journal_detail.id_currencyfx = payment_schedual.id_currencyfx;
                                }
                                accounting_journal_detailList.Add(AR_accounting_journal_detail);
                            }
                        }
                    }
                }


                ///SUMMARIZE
                foreach (accounting_journal_detail accounting_journal_detail in accounting_journal_detailList)
                {
                    int id_chart = accounting_journal_detail.accounting_chart.id_chart;
                    if (accounting_journal.accounting_journal_detail.Where(x => x.id_chart == id_chart).Count() == 0)
                    {
                        accounting_journal_detail PAYaccounting_journal_detail = new accounting_journal_detail();
                        PAYaccounting_journal_detail.id_chart         = accounting_journal_detail.accounting_chart.id_chart;
                        PAYaccounting_journal_detail.accounting_chart = accounting_journal_detail.accounting_chart;
                        PAYaccounting_journal_detail.trans_date       = accounting_journal_detail.trans_date;
                        PAYaccounting_journal_detail.credit           = accounting_journal_detail.credit;
                        PAYaccounting_journal_detail.debit            = accounting_journal_detail.debit;
                        if (context.app_currencyfx.Where(x => x.type == app_currencyfx.CurrencyFXTypes.Accounting &&
                                                         x.id_currency == accounting_journal_detail.app_currencyfx.id_currency && x.timestamp <= DateTime.Now)
                            .OrderByDescending(x => x.timestamp).FirstOrDefault() != null)
                        {
                            PAYaccounting_journal_detail.id_currencyfx = context.app_currencyfx.Where(x => x.type == app_currencyfx.CurrencyFXTypes.Accounting &&
                                                                                                      x.id_currency == accounting_journal_detail.app_currencyfx.id_currency && x.timestamp <= DateTime.Now)
                                                                         .OrderByDescending(x => x.timestamp).FirstOrDefault().id_currencyfx;
                        }
                        else
                        {
                            PAYaccounting_journal_detail.id_currencyfx = accounting_journal_detail.id_currencyfx;
                        }
                        accounting_journal.accounting_journal_detail.Add(PAYaccounting_journal_detail);
                    }
                    else
                    {
                        accounting_journal_detail PAYaccounting_journal_detail = accounting_journal.accounting_journal_detail.Where(x => x.id_chart == id_chart).FirstOrDefault();
                        PAYaccounting_journal_detail.credit += accounting_journal_detail.credit;
                        PAYaccounting_journal_detail.debit  += accounting_journal_detail.debit;
                    }
                }

                accounting_journal.sales_invoice.Add(sales_invoice);
            }
            return(accounting_journal);
        }