80 lines
3.2 KiB
Python
80 lines
3.2 KiB
Python
from odoo import models, fields, api, _
|
|
from odoo.exceptions import UserError
|
|
|
|
class HrExpensePaymentWizard(models.TransientModel):
|
|
_name = 'hr.expense.payment.wizard'
|
|
_description = 'Expense Payment Wizard'
|
|
|
|
expense_id = fields.Many2one('hr.expense', string='Expense', required=True)
|
|
amount = fields.Monetary(string='Payment Amount', required=True, readonly=True)
|
|
currency_id = fields.Many2one('res.currency', related='expense_id.currency_id')
|
|
company_id = fields.Many2one('res.company', related='expense_id.company_id')
|
|
|
|
partner_id = fields.Many2one(
|
|
'res.partner',
|
|
string='Vendor',
|
|
required=True,
|
|
domain="['|', ('company_id', '=', False), ('company_id', '=', company_id)]"
|
|
)
|
|
journal_id = fields.Many2one(
|
|
'account.journal',
|
|
string='Payment Journal',
|
|
required=True,
|
|
domain="[('type', 'in', ('bank', 'cash')), ('company_id', '=', company_id)]"
|
|
)
|
|
payment_method_line_id = fields.Many2one(
|
|
'account.payment.method.line',
|
|
string='Payment Method',
|
|
required=True,
|
|
domain="[('journal_id', '=', journal_id), ('payment_type', '=', 'outbound')]"
|
|
)
|
|
payment_date = fields.Date(string='Payment Date', default=fields.Date.context_today, required=True)
|
|
|
|
@api.onchange('journal_id')
|
|
def _onchange_journal_id(self):
|
|
if self.journal_id:
|
|
available_payment_methods = self.journal_id.outbound_payment_method_line_ids
|
|
if available_payment_methods:
|
|
self.payment_method_line_id = available_payment_methods[0].id
|
|
else:
|
|
self.payment_method_line_id = False
|
|
self.payment_method_line_id = False
|
|
|
|
def action_create_payment(self):
|
|
self.ensure_one()
|
|
|
|
# Find 115101 account (Uang Muka Operasional)
|
|
uang_muka_account = self.env['account.account'].search([
|
|
('code', '=', '115101'),
|
|
('company_id', '=', self.company_id.id)
|
|
], limit=1)
|
|
|
|
if not uang_muka_account:
|
|
raise UserError(_("Account 115101 Uang Muka Operasional not found for this company!"))
|
|
|
|
payment_vals = {
|
|
'date': self.payment_date,
|
|
'amount': self.amount,
|
|
'payment_type': 'outbound',
|
|
'partner_type': 'supplier',
|
|
'partner_id': self.partner_id.id,
|
|
'journal_id': self.journal_id.id,
|
|
'currency_id': self.currency_id.id,
|
|
'payment_method_line_id': self.payment_method_line_id.id,
|
|
'ref': self.expense_id.name,
|
|
'destination_account_id': uang_muka_account.id,
|
|
'expense_ids': [Command.set(self.expense_id.ids)],
|
|
}
|
|
|
|
payment = self.env['account.payment'].create(payment_vals)
|
|
payment.action_post()
|
|
|
|
# In Odoo 19, the payment move is linked to the expense via expense_ids on the payment.
|
|
# Standard Odoo should handle the state update if we followed the right hooks,
|
|
# but since we are doing a custom flow, let's trigger it.
|
|
|
|
# Update expense status
|
|
self.expense_id.action_recompute_state()
|
|
|
|
return {'type': 'ir.actions.act_window_close'}
|