diff --git a/modules/account/account.py b/modules/account/account.py index 7a541d8..1856c08 100755 --- a/modules/account/account.py +++ b/modules/account/account.py @@ -2858,23 +2858,56 @@ class GeneralLedgerDetail(DescriptionOriginMixin, ModelSQL, ModelView): Concat(Coalesce(account.code, ''), ' - '), Coalesce(account.name, '')) + @classmethod + def _opening_balance_query(cls, context): + Line, line, move, account, company = cls._tables() + tx_currency = Coalesce(line.second_currency, company.currency) + base_amount = Coalesce(line.debit, 0) - Coalesce(line.credit, 0) + tx_amount = Case( + (line.second_currency != Null, + Coalesce(line.amount_second_currency, 0)), + else_=base_amount) + + with Transaction().set_context( + context, **cls._query_context('opening', context)): + line_query, _ = Line.query_get(line) + where = line_query & cls._extra_where( + line, move, account, tx_currency, context) + + return (line.join(move, condition=line.move == move.id) + .join(account, condition=line.account == account.id) + .join(company, condition=move.company == company.id) + .select( + line.account.as_('account'), + tx_currency.as_('transaction_currency'), + Sum(base_amount).as_('base_amount'), + Sum(tx_amount).as_('transaction_amount'), + where=where, + group_by=[line.account, tx_currency], + having=Sum(base_amount) != 0)) + @classmethod def _line_query(cls, name, context): Line, line, move, account, company = cls._tables() lot, uom = cls._lot_tables(Line) + opening_balance = cls._opening_balance_query(context) account_label = cls._account_label(account) quantity = Literal(None) unit = Literal(None) + tx_currency = Coalesce(line.second_currency, company.currency) from_ = (line.join(move, condition=line.move == move.id) .join(account, condition=line.account == account.id) .join(company, condition=move.company == company.id)) + from_ = from_.join(opening_balance, 'LEFT', + condition=( + (opening_balance.account == line.account) + & (opening_balance.transaction_currency == tx_currency))) if lot is not None: from_ = (from_ .join(lot, 'LEFT', condition=Column(line, 'lot') == lot.id) .join(uom, 'LEFT', condition=lot.lot_unit_line == uom.id)) quantity = lot.lot_qt unit = uom.symbol - tx_currency = Coalesce(line.second_currency, company.currency) base_amount = Coalesce(line.debit, 0) - Coalesce(line.credit, 0) second_amount = Coalesce(line.amount_second_currency, 0) tx_amount = Case( @@ -2929,12 +2962,14 @@ class GeneralLedgerDetail(DescriptionOriginMixin, ModelSQL, ModelView): line.debit.as_('debit_base_currency'), line.credit.as_('credit_base_currency'), base_amount.as_('balance_base_currency'), - Sum(base_amount, window=window).as_( + (Coalesce(opening_balance.base_amount, 0) + + Sum(base_amount, window=window)).as_( 'running_balance_base_currency'), debit_tx.as_('debit_transaction_currency'), credit_tx.as_('credit_transaction_currency'), tx_amount.as_('balance_transaction_currency'), - Sum(tx_amount, window=window).as_( + (Coalesce(opening_balance.transaction_amount, 0) + + Sum(tx_amount, window=window)).as_( 'running_balance_transaction_currency'), quantity.as_('payable_qty'), unit.as_('uom'),