purchase_advance_payment/wizard/link_advance_payment_wizard.py
2025-09-23 14:15:11 +07:00

100 lines
3.7 KiB
Python

from odoo import models, fields, api
from odoo.exceptions import UserError
class LinkAdvancePaymentWizard(models.TransientModel):
_name = 'link.advance.payment.wizard'
_description = 'Link Advance Payment Wizard'
purchase_order_id = fields.Many2one(
'purchase.order',
string='Purchase Order'
)
payment_id = fields.Many2one(
'account.payment',
string='Payment',
domain=[('state', '=', 'draft')]
)
amount = fields.Monetary(
string='Amount'
)
currency_id = fields.Many2one(
'res.currency',
string='Currency'
)
@api.model
def default_get(self, fields):
res = super().default_get(fields)
# Handle context from purchase order
if self._context.get('active_model') == 'purchase.order' and self._context.get('active_id'):
po = self.env['purchase.order'].browse(self._context['active_id'])
res['purchase_order_id'] = po.id
res['currency_id'] = po.currency_id.id
if 'amount' in fields:
res['amount'] = po.amount_residual
# Handle context from payment
elif self._context.get('active_model') == 'account.payment' and self._context.get('active_id'):
payment = self.env['account.payment'].browse(self._context['active_id'])
res['payment_id'] = payment.id
res['currency_id'] = payment.currency_id.id
if 'amount' in fields:
res['amount'] = payment.amount
return res
@api.onchange('payment_id')
def _onchange_payment_id(self):
if self.payment_id:
self.currency_id = self.payment_id.currency_id.id
self.amount = self.payment_id.amount
# Filter purchase orders by partner when payment is selected
if self.payment_id.partner_id:
purchase_orders = self.env['purchase.order'].search([
('partner_id', '=', self.payment_id.partner_id.id),
('state', 'in', ['purchase', 'done'])
])
return {'domain': {'purchase_order_id': [('id', 'in', purchase_orders.ids)]}}
return {'domain': {'purchase_order_id': []}}
@api.onchange('purchase_order_id')
def _onchange_purchase_order_id(self):
if self.purchase_order_id:
self.currency_id = self.purchase_order_id.currency_id.id
self.amount = self.purchase_order_id.amount_residual
# Filter payments by partner when purchase order is selected
if self.purchase_order_id.partner_id:
payments = self.env['account.payment'].search([
('partner_id', '=', self.purchase_order_id.partner_id.id),
('state', '=', 'draft'),
('payment_type', '=', 'outbound')
])
return {'domain': {'payment_id': [('id', 'in', payments.ids)]}}
return {'domain': {'payment_id': []}}
def action_link_payment(self):
self.ensure_one()
if self.amount <= 0:
raise UserError("Amount must be greater than zero.")
# Check if we have both payment and purchase order
if not self.payment_id:
raise UserError("Payment must be specified.")
if not self.purchase_order_id:
raise UserError("Purchase order must be specified.")
# Link the payment to the purchase order
self.payment_id.write({
'purchase_order_id': self.purchase_order_id.id,
'is_advance_payment': True,
'amount': self.amount
})
return {
'type': 'ir.actions.act_window_close'
}