added balance to close in statement

This commit is contained in:
2023-06-21 00:32:14 -05:00
parent 68c503039c
commit 38087ce25c
16 changed files with 152 additions and 14 deletions

View File

@@ -7,7 +7,10 @@ from trytond.transaction import Transaction
from trytond.wizard import Button, StateTransition, StateView, Wizard
from decimal import Decimal
from trytond.i18n import gettext
from trytond.modules.currency.fields import Monetary
from trytond.pyson import Eval
from trytond.exceptions import UserError
from datetime import datetime
__all__ = ['Journal', 'Statement', 'Line', 'OpenStatementStart',
'OpenStatementDone', 'OpenStatement', 'CloseStatementStart',
@@ -118,7 +121,6 @@ class OpenStatementStart(ModelView):
'Open Statement'
__name__ = 'open.statement.start'
class OpenStatementDone(ModelView):
'Open Statement'
__name__ = 'open.statement.done'
@@ -143,7 +145,7 @@ class OpenStatement(Wizard):
return {
'result': self.result,
}
def transition_create_(self):
pool = Pool()
User = pool.get('res.user')
@@ -197,7 +199,9 @@ class CloseStatementStart(ModelView):
'Close Statement'
__name__ = 'close.statement.start'
statementLines = fields.One2Many('statement.line', None, 'Lines')
class CloseStatementDone(ModelView):
'Close Statement'
__name__ = 'close.statement.done'
@@ -217,17 +221,54 @@ class CloseStatement(Wizard):
'sale_payment.close_statement_done', [
Button('Done', 'end', 'tryton-ok', default=True),
])
def default_done(self, fields):
return {
'result': self.result,
}
def default_start(self, fields):
pool = Pool()
User = pool.get('res.user')
StatementLine = pool.get('statement.line')
Statement = pool.get('account.statement')
statementLines = []
user = Transaction().user
user = User(user)
device = user.sale_device
if device:
journals = [j.id for j in device.journals]
draft_statements = {
s.journal: s for s in Statement.search([
('journal', 'in', journals),
], order=[
('create_date', 'ASC'),
])}
for s in draft_statements.values():
end_balance = Decimal('0.0')
for line in s.lines:
end_balance += line.amount
line = {
'journal': s.journal.id,
'start_balance': s.start_balance,
'balance': end_balance,
'end_balance':end_balance+s.start_balance
}
statementLines.append(line)
default = {'statementLines': statementLines}
return default
def transition_validate(self):
pool = Pool()
User = pool.get('res.user')
Statement = pool.get('account.statement')
StatementLine = pool.get('account.statement.line')
user = Transaction().user
user = User(user)
device = user.sale_device
@@ -242,15 +283,36 @@ class CloseStatement(Wizard):
results = []
statements = []
for journal in device.journals:
#raise UserError(str((device.journals)))
#raise UserError(str((self.start.statementLines[0].journal)))
for journal in self.start.statementLines:
account = journal.account
transfer = journal.transfer
end_balance = journal.end_balance
journal = journal.journal
statement = draft_statements.get(journal)
if statement and statement.state == 'draft':
if not statement.start_balance:
statement.start_balance = Decimal(0)
end_balance = statement.start_balance
for line in statement.lines:
end_balance += line.amount
statement.end_balance = end_balance
if account and transfer:
end_balance = abs(end_balance) - abs(transfer)
statement.end_balance = end_balance
lines = statement.lines
conciliation = tuple([StatementLine(
date=datetime.today().date(),
amount= abs(transfer)*-1,
account=account.id)]
)
#raise UserError(str(conciliation))
statement.lines = statement.lines + conciliation
else:
end_balance = statement.start_balance
for line in statement.lines:
end_balance += line.amount
statement.end_balance = end_balance
statement.save()
statements.append(statement)
results.append(gettext('sale_payment.close_statement',
@@ -268,3 +330,41 @@ class CloseStatement(Wizard):
self.result = gettext('sale_payment.user_without_device',
user=user.rec_name)
return 'done'
class StatementLine(ModelView):
"statement Line"
__name__ = 'statement.line'
company = fields.Many2One(
'company.company', "Company", required=True, select=True)
journal = fields.Many2One('account.statement.journal', 'Journal',
required=True, select=True)
currency = fields.Many2One(
'currency.currency', "Currency")
start_balance = Monetary(
"Start Balance", currency='currency', digits='currency')
balance = Monetary(
"Balance", currency='currency', digits='currency')
transfer = Monetary(
"Transfer", currency='currency', digits='currency')
end_balance = Monetary(
"End Balance", currency='currency', digits='currency')
account = fields.Many2One(
'account.account', "Account",
domain=[
('company', '=', Eval('company', 0)),
('type', '!=', None),
('closed', '!=', True),
])
@staticmethod
def default_currency():
Company = Pool().get('company.company')
company = Transaction().context.get('company')
if company:
return Company(company).currency.id
@staticmethod
def default_company():
return Transaction().context.get('company')