Skip to content

Commit

Permalink
temp rebasing PR 424 (ffdeae4)
Browse files Browse the repository at this point in the history
  • Loading branch information
roboadhoc committed Jan 19, 2024
2 parents e918a81 + ffdeae4 commit 2047351
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 12 deletions.
4 changes: 3 additions & 1 deletion account_payment_pro/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
"l10n_latam_invoice_document",
],
"data": [
'security/payment_security.xml',
'security/ir.model.access.csv',
'wizards/account_payment_invoice_wizard_view.xml',
'views/account_payment_view.xml',
'security/ir.model.access.csv',
'views/account_move.xml',
],
"demo": [
],
Expand Down
2 changes: 2 additions & 0 deletions account_payment_pro/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
from . import account_payment
from . import account_move_line
from . import account_move
from . import account_journal
20 changes: 20 additions & 0 deletions account_payment_pro/models/account_journal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# © 2024 ADHOC SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models, _
from odoo.exceptions import ValidationError


class AccountJournal(models.Model):

_inherit = "account.journal"

def _get_manual_payment_method_id(self, direction='inbound'):
self.ensure_one()
if direction == 'inbound':
payment_method = self.inbound_payment_method_line_ids.payment_method_id.filtered(lambda x: x.code == 'manual')
else:
payment_method = self.outbound_payment_method_line_ids.payment_method_id.filtered(lambda x: x.code == 'manual')
if not payment_method:
raise ValidationError(_('Journal must have manual method!'))
return payment_method
82 changes: 82 additions & 0 deletions account_payment_pro/models/account_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# © 2016 ADHOC SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models, api, Command, fields, _
from odoo.exceptions import ValidationError


class AccountMove(models.Model):
_inherit = "account.move"

open_move_line_ids = fields.One2many(
'account.move.line',
compute='_compute_open_move_lines'
)
pay_now_journal_id = fields.Many2one(
'account.journal',
'Pay now Journal',
help='If you set a journal here, after invoice validation, the invoice'
' will be automatically paid with this journal. As manual payment'
'method is used, only journals with manual method are shown.',
# use copy false for two reasons:
# 1. when making refund it's safer to make pay now empty (specially if automatic refund validation is enable)
# 2. on duplicating an invoice it's safer also
copy=False,
)

@api.depends('line_ids.account_id.account_type', 'line_ids.reconciled')
def _compute_open_move_lines(self):
for rec in self:
rec.open_move_line_ids = rec.line_ids.filtered(
lambda r: not r.reconciled and r.parent_state == 'posted' and
r.account_id.account_type in self.env['account.payment']._get_valid_payment_account_types())

def pay_now(self):
for rec in self.filtered(lambda x: x.pay_now_journal_id and x.state == 'posted' and
x.payment_state in ('not_paid', 'patial')):
pay_journal = rec.pay_now_journal_id
if rec.move_type in ['in_invoice', 'in_refund']:
partner_type = 'supplier'
else:
partner_type = 'customer'

payment_type = 'inbound'
payment_method = pay_journal._get_manual_payment_method_id(payment_type)

payment = rec.env[
'account.payment'].create({
'date': rec.invoice_date,
'partner_id': rec.commercial_partner_id.id,
'partner_type': partner_type,
'payment_type': payment_type,
'company_id': rec.company_id.id,
'journal_id': pay_journal.id,
'payment_method_id': payment_method.id,
'to_pay_move_line_ids': [Command.set(rec.open_move_line_ids.ids)],
})

# el difference es positivo para facturas (de cliente o proveedor) pero negativo para NC.
# para factura de proveedor o NC de cliente es outbound
# para factura de cliente o NC de proveedor es inbound
# igualmente lo hacemos con el difference y no con el type por las dudas de que facturas en negativo
if (partner_type == 'supplier' and payment.payment_difference >= 0.0 or
partner_type == 'customer' and payment.payment_difference < 0.0):
payment.payment_type = 'outbound'
payment.payment_method_id = pay_journal._get_manual_payment_method_id(payment_type).id
payment.amount = abs(payment.payment_difference)
payment.action_post()

@api.onchange('journal_id')
def _onchange_journal_reset_pay_now(self):
# while not always it should be reseted (only if changing company) it's not so usual to set pay now first
# and then change journal
self.pay_now_journal_id = False

def button_draft(self):
self.filtered(lambda x: x.state == 'posted' and x.pay_now_journal_id).write({'pay_now_journal_id': False})
return super().button_draft()

def _post(self, soft=False):
res = super()._post(soft=soft)
self.pay_now()
return res
5 changes: 3 additions & 2 deletions account_payment_pro/models/account_move_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def action_register_payment(self):
to_pay_partners = self.mapped('move_id.commercial_partner_id')
if len(to_pay_partners) > 1:
raise UserError(_('Selected recrods must be of the same partner'))

partner_type = 'customer' if to_pay_move_lines[0].account_id.account_type == 'asset_receivable' else 'supplier'
return {
'name': _('Register Payment'),
'res_model': 'account.payment',
Expand All @@ -48,7 +48,8 @@ def action_register_payment(self):
'context': {
'active_model': 'account.move.line',
'active_ids': self.ids,
'default_partner_type': 'customer' if to_pay_move_lines[0].account_id.account_type == 'asset_receivable' else 'supplier',
'default_payment_type': 'inbound' if partner_type == 'customer' else 'outbound',
'default_partner_type': partner_type,
'default_partner_id': to_pay_partners.id,
'default_to_pay_move_line_ids': to_pay_move_lines.ids,
# We set this because if became from other view and in the context has 'create=False'
Expand Down
10 changes: 5 additions & 5 deletions account_payment_pro/models/account_payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@ def _prepare_move_line_default_vals(self, write_off_line_vals=None):
})
return res

@api.model
def _get_trigger_fields_to_synchronize(self):
res = super()._get_trigger_fields_to_synchronize()
return res + ('force_amount_company_currency',)
# @api.model
# def _get_trigger_fields_to_synchronize(self):
# res = super()._get_trigger_fields_to_synchronize()
# return res + ('force_amount_company_currency',)

# TODO traer de account_ux y verificar si es necesario
# @api.depends_context('default_is_internal_transfer')
Expand Down Expand Up @@ -265,7 +265,7 @@ def _compute_matched_move_line_ids(self):
al menso podremos re-usar codigo sql para optimizar performance
"""
for rec in self:
payment_lines = rec.line_ids.filtered(lambda x: x.account_type in ['asset_receivable', 'liability_payable'])
payment_lines = rec.line_ids.filtered(lambda x: x.account_type in self._get_valid_payment_account_types())
debit_moves = payment_lines.mapped('matched_debit_ids.debit_move_id')
credit_moves = payment_lines.mapped('matched_credit_ids.credit_move_id')
debit_lines_sorted = debit_moves.filtered(lambda x: x.date_maturity != False).sorted(key=lambda x: (x.date_maturity, x.move_id.name))
Expand Down
13 changes: 13 additions & 0 deletions account_payment_pro/security/payment_security.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<data noupdate="0">
<record model="res.groups" id="group_pay_now_customer_invoices">
<field name="category_id" ref="base.module_category_hidden"/>
<field name="name">Allow pay now on customer invoices</field>
</record>
<record model="res.groups" id="group_pay_now_vendor_invoices">
<field name="category_id" ref="base.module_category_hidden"/>
<field name="name">Allow pay now on vendor invoices</field>
</record>
</data>
</odoo>
19 changes: 19 additions & 0 deletions account_payment_pro/views/account_move.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<odoo>
<record id="view_move_form" model="ir.ui.view">
<field name="name">account.move</field>
<field name="model">account.move</field>
<field name="inherit_id" ref="account.view_move_form"/>
<field name="arch" type="xml">
<group id="header_left_group">
<!-- only allow pay now if manual method available -->
<field name="pay_now_journal_id" readonly="state != 'draft'" invisible="move_type not in ('in_invoice', 'in_refund')" options="{'no_create': True}"
domain="[('company_id', '=', company_id), ('type', 'in', ['bank', 'cash']), ('outbound_payment_method_line_ids.payment_method_id.code', '=', 'manual')]"
groups="account_payment_pro.group_pay_now_vendor_invoices"/>
<field name="pay_now_journal_id" readonly="state != 'draft'" invisible="move_type not in ('out_invoice', 'out_refund')" options="{'no_create': True}"
domain="[('company_id', '=', company_id), ('type', 'in', ['bank', 'cash']), ('inbound_payment_method_line_ids.payment_method_id.code', '=', 'manual')]"
groups="account_payment_pro.group_pay_now_customer_invoices"/>
</group>
</field>
</record>

</odoo>
9 changes: 5 additions & 4 deletions account_payment_pro/views/account_payment_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
<field name="company_currency_id" invisible="True"/>
<field name="other_currency" invisible="True"/>
<field name="force_amount_company_currency" invisible="True"/>
<!-- <label for="amount_company_currency" string="(on company currency)" invisible="not other_currency"/>
<label for="amount_company_currency" string="(on company currency)" invisible="not other_currency"/>
<div name="amount_company_currency" class="o_row" invisible="not other_currency">
x <field name="exchange_rate"/> = <field name="amount_company_currency" readonly="state != 'draft'"/>
</div> -->
</div>
</div>
<group name="group2" position="after">
<group name="group3" invisible="is_internal_transfer">
Expand Down Expand Up @@ -51,9 +51,10 @@
domain="[('partner_id.commercial_partner_id', '=', commercial_partner_id),('account_id.reconcile', '=', True),('reconciled', '=', False), ('full_reconcile_id', '=', False), ('company_id', '=', company_id), ('move_id.state', '=', 'posted'), ('account_id.account_type', '=', 'asset_receivable' if partner_type == 'customer' else 'liability_payable')]"
help="Payment will be automatically matched with the oldest lines of this list (by maturity date). You can remove any line you dont want to be matched."/>
</page>
<page string="Paid" invisible="state != 'posted'">
<!-- Todo activar cuando logremos propagar contexto en las vistas a los campos calculados
<page string="Paid" invisible="state != 'posted'">
<field name="matched_move_line_ids" context="{'matched_payment_id': id, 'tree_view_ref': 'account_payment_pro.view_move_line_with_matched_tree'}"/>
</page>
</page> -->
</notebook>
</sheet>

Expand Down

0 comments on commit 2047351

Please sign in to comment.