fix some journal movement

This commit is contained in:
admin.suherdy 2025-12-05 19:38:25 +07:00
parent ec53246e23
commit f482d93b97
5 changed files with 92 additions and 1 deletions

View File

@ -16,6 +16,27 @@ class AccountPayment(models.Model):
help='Identifies if this payment is an advance payment for a purchase order'
)
@api.model_create_multi
def create(self, vals_list):
"""Override create to ensure payment_method_line_id is correctly set for advance payments"""
for vals in vals_list:
# If this is an advance payment and payment_method_line_id is set,
# ensure it matches the journal's payment methods
if vals.get('is_advance_payment') and vals.get('journal_id') and vals.get('payment_method_line_id'):
journal = self.env['account.journal'].browse(vals['journal_id'])
payment_method_line = self.env['account.payment.method.line'].browse(vals['payment_method_line_id'])
# Verify the payment method line belongs to this journal
if payment_method_line not in journal.outbound_payment_method_line_ids:
# If not, find the correct one
correct_method = journal.outbound_payment_method_line_ids.filtered(
lambda l: l.payment_method_id == payment_method_line.payment_method_id
)[:1]
if correct_method:
vals['payment_method_line_id'] = correct_method.id
return super().create(vals_list)
@api.onchange('purchase_order_id')
def _onchange_purchase_order_id(self):
if self.purchase_order_id:

View File

@ -27,6 +27,20 @@ class CreateAdvancePaymentWizard(models.TransientModel):
domain=[('type', 'in', ['bank', 'cash'])]
)
payment_method_line_id = fields.Many2one(
'account.payment.method.line',
string='Payment Method',
domain="[('id', 'in', available_payment_method_line_ids)]",
help='Manual: Pay by any method outside of Odoo.\n'
'Check: Pay by check and print it from Odoo.\n'
'Batch Deposit: Collect several checks at once.'
)
available_payment_method_line_ids = fields.Many2many(
'account.payment.method.line',
compute='_compute_available_payment_method_line_ids'
)
amount = fields.Monetary(
string='Amount',
required=True,
@ -67,6 +81,32 @@ class CreateAdvancePaymentWizard(models.TransientModel):
res['memo'] = f'Advance payment for {po.name}'
return res
@api.depends('journal_id')
def _compute_available_payment_method_line_ids(self):
"""Compute available payment methods based on selected journal"""
for wizard in self:
if wizard.journal_id:
wizard.available_payment_method_line_ids = wizard.journal_id.outbound_payment_method_line_ids
else:
wizard.available_payment_method_line_ids = False
@api.onchange('journal_id')
def _onchange_journal_id(self):
"""Reset payment method when journal changes"""
if self.journal_id:
# Auto-select the first available payment method (usually 'manual')
available_methods = self.journal_id.outbound_payment_method_line_ids
if available_methods:
# Prefer 'manual' payment method
manual_method = available_methods.filtered(
lambda l: l.payment_method_id.code == 'manual'
)[:1]
self.payment_method_line_id = manual_method or available_methods[:1]
else:
self.payment_method_line_id = False
else:
self.payment_method_line_id = False
@api.depends('purchase_order_id')
def _compute_expense_account(self):
"""Get expense account from default advance payment product"""
@ -118,15 +158,38 @@ class CreateAdvancePaymentWizard(models.TransientModel):
f"Outstanding Payments Account"
)
# Use the selected payment method line or get the appropriate one
payment_method_line = self.payment_method_line_id
if not payment_method_line:
# Fallback: try to get manual payment method
payment_method_line = self.journal_id.outbound_payment_method_line_ids.filtered(
lambda l: l.payment_method_id.code == 'manual'
)[:1]
if not payment_method_line:
# Fallback to first available outbound payment method
payment_method_line = self.journal_id.outbound_payment_method_line_ids[:1]
if not payment_method_line:
raise UserError(
f"No outbound payment method is configured for journal '{self.journal_id.name}'.\n"
f"Please configure a payment method in: Accounting > Configuration > Journals > "
f"{self.journal_id.name} > Outgoing Payments tab"
)
# Create payment
# CRITICAL: Set payment_type and partner_type FIRST, then journal_id, then payment_method_line_id
# This ensures Odoo's onchange logic correctly filters and sets the payment method
payment_vals = {
'payment_type': 'outbound',
'partner_type': 'supplier',
'partner_id': self.partner_id.id,
'journal_id': self.journal_id.id,
'payment_method_line_id': payment_method_line.id,
'amount': self.amount,
'currency_id': self.currency_id.id,
'date': self.date,
'journal_id': self.journal_id.id,
'ref': self.memo or f'Advance payment for {self.purchase_order_id.name}',
'purchase_order_id': self.purchase_order_id.id,
'is_advance_payment': True,
@ -134,6 +197,11 @@ class CreateAdvancePaymentWizard(models.TransientModel):
payment = self.env['account.payment'].create(payment_vals)
# Force recompute of payment method line to ensure it's correctly set
# This is necessary because Odoo might have changed it during create
if payment.payment_method_line_id != payment_method_line:
payment.write({'payment_method_line_id': payment_method_line.id})
# Create deposit line in purchase order immediately
self._create_deposit_line()

View File

@ -12,6 +12,8 @@
<field name="purchase_order_id" readonly="1"/>
<field name="partner_id" readonly="1"/>
<field name="journal_id" required="1"/>
<field name="available_payment_method_line_ids" invisible="1"/>
<field name="payment_method_line_id" required="1"/>
</group>
<group>
<field name="amount" required="1"/>