added pay to invoice supplier with wizard

This commit is contained in:
2023-06-21 11:28:46 -05:00
parent a5705f1818
commit 1168dffad6
9 changed files with 171 additions and 20 deletions

View File

@@ -4,11 +4,11 @@
from trytond.model import fields, ModelView
from trytond.pool import Pool, PoolMeta
from trytond.transaction import Transaction
from trytond.wizard import Button, StateTransition, StateView, Wizard
from trytond.wizard import Button, StateTransition, StateAction, 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.pyson import Bool, Eval, If
from trytond.exceptions import UserError
from datetime import datetime
@@ -284,8 +284,6 @@ class CloseStatement(Wizard):
results = []
statements = []
#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
@@ -305,7 +303,6 @@ class CloseStatement(Wizard):
amount= abs(transfer)*-1,
account=account.id)]
)
#raise UserError(str(conciliation))
statement.lines = statement.lines + conciliation
else:
@@ -335,11 +332,13 @@ class CloseStatement(Wizard):
class StatementLine(ModelView):
"statement Line"
__name__ = 'statement.line'
_states = {'readonly': True}
company = fields.Many2One(
'company.company', "Company", required=True, select=True)
journal = fields.Many2One('account.statement.journal', 'Journal',
required=True, select=True)
required=True, select=True,
states=_states)
currency = fields.Many2One(
'currency.currency', "Currency")
start_balance = Monetary(
@@ -350,13 +349,13 @@ class StatementLine(ModelView):
"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),
])
account = fields.Many2One('account.account', "Account",
domain=[
('company', '=', Eval('company', 0)),
('type', '!=', None),
('closed', '!=', True),
],
states={'required': If(Eval('transfer', True), True)})
@staticmethod
def default_currency():
@@ -368,3 +367,117 @@ class StatementLine(ModelView):
@staticmethod
def default_company():
return Transaction().context.get('company')
class PayInvoiceSupplierStart(ModelView):
'Payment Invoice To Supplier'
__name__ = 'pay_invoice.statement.start'
invoice_to_pay = fields.One2Many('line_invoice.pay', None, 'Lines To Pay')
class PayInvoiceSupplier(Wizard):
'Payment Invoice To Supplier'
__name__ = 'pay_invoice.statement'
start = StateView('pay_invoice.statement.start',
'sale_payment.pay_invoice_statement_start', [
Button('Cancel', 'end', 'tryton-cancel'),
Button('Pay', 'pay', 'tryton-ok', default=True),
])
pay = StateAction('account_statement.act_statement_form')
def do_pay(self, action):
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
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 pay in self.start.invoice_to_pay:
journal = pay.journal
account = pay.account
invoice = pay.invoice
party = pay.party
amount = pay.amount
statement = draft_statements.get(journal)
lines = statement.lines
pay_to_add = tuple([StatementLine(
date=datetime.today().date(),
party=party,
related_to=invoice,
amount= abs(amount)*-1,
account=account.id)])
statement.lines = statement.lines + pay_to_add
statement.save()
class LinesInvoiceToPay(ModelView):
"Lines Invoice To Pay"
__name__ = 'line_invoice.pay'
company = fields.Many2One(
'company.company', "Company", required=True, select=True)
journal = fields.Many2One('account.statement.journal', 'Journal',
required=True, select=True)
amount = Monetary(
"Amount", currency='currency', digits='currency', required=True)
party = fields.Many2One(
'party.party', "Party",
context={
'company': Eval('company', -1),
},)
invoice = fields.Reference(
"Related To", 'get_relations', required=True,
domain={
'account.invoice': [
('company', '=', Eval('company', -1)),
('type', '=', 'in'),
If(Bool(Eval('party')),
('party', '=', Eval('party')),
()),
If(Bool(Eval('account')),
('account', '=', Eval('account')),
()),
If(Eval('statement_state') == 'draft',
('state', '=', 'posted'),
('state', '!=', '')),
],},
context={'with_payment': False})
account = fields.Many2One('account.account', "Account",
domain=[
('company', '=', Eval('company', 0)),
('type', '!=', None),
('closed', '!=', True),
],)
description = fields.Char("Description")
@staticmethod
def default_company():
return Transaction().context.get('company')
@classmethod
def _get_relations(cls):
"Return a list of Model names for related_to Reference"
return ['account.invoice']
@classmethod
def get_relations(cls):
Model = Pool().get('ir.model')
get_name = Model.get_name
models = cls._get_relations()
return [(m, get_name(m)) for m in models]