====================
Group Lines Scenario
====================

Imports::

    >>> import datetime as dt
    >>> from decimal import Decimal
    >>> from proteus import Model, Wizard
    >>> from trytond.tests.tools import activate_modules
    >>> from trytond.modules.currency.tests.tools import get_currency
    >>> from trytond.modules.company.tests.tools import create_company, \
    ...     get_company
    >>> from trytond.modules.account.tests.tools import create_fiscalyear, \
    ...     create_chart, get_accounts

Install account::

    >>> config = activate_modules('account')

Create company::

    >>> usd = get_currency('USD')
    >>> eur = get_currency('EUR')
    >>> _ = create_company(currency=usd)
    >>> company = get_company()

Create fiscal year::

    >>> fiscalyear = create_fiscalyear(company)
    >>> fiscalyear.click('create_period')
    >>> period = fiscalyear.periods[0]

Create chart of accounts::

    >>> _ = create_chart(company)
    >>> accounts = get_accounts(company)
    >>> payable = accounts['payable']
    >>> receivable = accounts['receivable']
    >>> revenue = accounts['revenue']
    >>> expense = accounts['expense']

Create parties::

    >>> Party = Model.get('party.party')
    >>> party = Party(name="Party")
    >>> party.save()

Create Lines to group::

    >>> Journal = Model.get('account.journal')
    >>> Move = Model.get('account.move')
    >>> journal_revenue, = Journal.find([
    ...         ('code', '=', 'REV'),
    ...         ])
    >>> journal_expense, = Journal.find([
    ...         ('code', '=', 'EXP'),
    ...         ])

    >>> move = Move()
    >>> move.journal = journal_revenue
    >>> move.date = period.start_date
    >>> line = move.lines.new()
    >>> line.account = revenue
    >>> line.credit = Decimal(100)
    >>> line = move.lines.new()
    >>> line.account = receivable
    >>> line.party = party
    >>> line.debit = Decimal(100)
    >>> line.second_currency = eur
    >>> line.amount_second_currency = Decimal(90)
    >>> line.maturity_date = period.start_date + dt.timedelta(days=2)
    >>> move.save()

    >>> move = Move()
    >>> move.journal = journal_expense
    >>> move.date = period.start_date
    >>> line = move.lines.new()
    >>> line.account = expense
    >>> line.debit = Decimal(50)
    >>> line = move.lines.new()
    >>> line.account = payable
    >>> line.party = party
    >>> line.credit = Decimal(50)
    >>> line.second_currency = eur
    >>> line.amount_second_currency = Decimal(45)
    >>> line.maturity_date = period.start_date + dt.timedelta(days=4)
    >>> move.save()

    >>> receivable.reload()
    >>> receivable.balance, receivable.amount_second_currency
    (Decimal('100.00'), Decimal('90.00'))
    >>> payable.reload()
    >>> payable.balance, payable.amount_second_currency
    (Decimal('-50.00'), Decimal('-45.00'))

Group lines::

    >>> journal_cash, = Journal.find([
    ...         ('code', '=', 'CASH'),
    ...         ])

    >>> Line = Model.get('account.move.line')
    >>> lines = Line.find([('account', 'in', [payable.id, receivable.id])])
    >>> len(lines)
    2
    >>> group = Wizard('account.move.line.group', lines)
    >>> group.form.journal = journal_cash
    >>> group.form.description = "Group lines"
    >>> group.execute('group')

    >>> receivable.reload()
    >>> receivable.balance, receivable.amount_second_currency
    (Decimal('50.00'), Decimal('45.00'))
    >>> payable.reload()
    >>> payable.balance, payable.amount_second_currency
    (Decimal('0.00'), Decimal('0.00'))

    >>> Reconciliation = Model.get('account.move.reconciliation')
    >>> reconciliations = Reconciliation.find([])
    >>> len(reconciliations)
    2
    >>> all(r.delegate_to for r in reconciliations)
    True
    >>> delegate_to = reconciliations[0].delegate_to
    >>> delegate_to.account == receivable
    True
    >>> delegate_to.debit
    Decimal('50')
    >>> delegate_to.maturity_date == period.start_date + dt.timedelta(days=2)
    True
