Files
tryton/demo/account_statement.py
T
2024-04-28 13:58:17 +02:00

70 lines
2.1 KiB
Python

# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import random
from decimal import Decimal
from dateutil.relativedelta import relativedelta
from proteus import Model
def setup(config, modules, company):
Journal = Model.get('account.statement.journal')
Statement = Model.get('account.statement')
AccountJournal = Model.get('account.journal')
Account = Model.get('account.account')
Sequence = Model.get('ir.sequence')
SequenceType = Model.get('ir.sequence.type')
Invoice = Model.get('account.invoice')
sequence_type, = SequenceType.find([('name', '=', "Account Journal")])
sequence = Sequence(name='Statement',
sequence_type=sequence_type,
company=company)
sequence.save()
cash, = Account.find([
('name', '=', 'Main Cash'),
('company', '=', company.id),
])
account_journal = AccountJournal(name='Bank',
type='statement',
sequence=sequence)
account_journal.save()
journal = Journal(name='Bank',
journal=account_journal,
account=cash,
validation='balance')
journal.save()
invoices = Invoice.find([
('state', '=', 'posted'),
])
invoices = random.sample(invoices, len(invoices) * 2 // 3)
total_amount = Decimal(0)
statement = Statement(name='001',
journal=journal,
start_balance=Decimal(0))
for i, invoice in enumerate(invoices):
if not invoice.amount_to_pay:
continue
line = statement.lines.new()
line.number = str(i)
line.date = invoice.invoice_date + relativedelta(
days=random.randint(1, 20))
amount = invoice.amount_to_pay
if invoice.type in ('in_invoice', 'out_credit_note'):
amount = - amount
line.amount = amount
line.party = invoice.party
if random.random() < 2. / 3.:
line.related_to = invoice
total_amount += line.amount
statement.end_balance = total_amount
statement.click('validate_statement')