from odoo import models, fields, api,_ from odoo.exceptions import UserError class PaymentRequest(models.Model): _inherit = 'xf.doc.approval.document.package' _description = 'Payment Request' # Additional Fields for Payment Request amount = fields.Float(string="Amount") partner_id = fields.Many2one('res.partner', string="Vendor") bill_id = fields.Many2one('account.move', string="Vendor Bill", readonly=True) payment_order_id = fields.Many2one('account.payment', string="Payment Order", readonly=True) payment_amount = fields.Monetary( string="Payment Amount", currency_field='currency_id', # Links to the currency field ) currency_id = fields.Many2one( 'res.currency', string="Currency", default=lambda self: self.env.company.currency_id, # Default to company currency readonly=True, ) type_request = fields.Selection([('1','សំណើអនុម័តឯកសារ'),('2','សំណើសុំអនុម័តថវិកា'),('3','សំណើសុំអនុម័តគោលការណ៍ និងថវិកា')],string="Request type") # Override action_finish_approval to include vendor bill creation def action_finish_approval(self): """ Finish the approval process and generate a vendor bill if approved. """ for record in self: if record.approval_state == 'approved': record.state = 'approved' # Generate the draft vendor bill record._generate_vendor_bill() else: raise UserError(_('Document Package must be fully approved!')) def _generate_vendor_bill(self): """ Generate a draft vendor bill in Accounting. """ for record in self: if not record.partner_id and record.type_request != '2': raise UserError(_("No vendor selected for this payment request.")) # Dynamically fetch a product or create one if none exists product = self.env['product.product'].search([], limit=1) if not product: product = self.env['product.product'].create({ 'name': 'Default Service', 'type': 'service', 'list_price': record.payment_amount, }) # Create the draft vendor bill bill = self.env['account.move'].create({ 'move_type': 'in_invoice', 'partner_id': record.partner_id.id, 'invoice_date': fields.Date.today(), 'invoice_line_ids': [(0, 0, { 'product_id': product.id, 'quantity': 1, 'price_unit': record.payment_amount, 'name': f"Payment Request - {record.name}", })], }) record.write({'bill_id': bill.id}) def action_request_bank_payment(self): """ Request bank payment after vendor bill is created. """ for record in self: if not record.bill_id: raise UserError(_("No vendor bill exists for this payment request.")) # Create a payment order payment = self.env['account.payment'].create({ 'payment_type': 'outbound', 'partner_id': record.partner_id.id, 'amount': record.payment_amount, 'date': fields.Date.today(), 'journal_id': self.env['account.journal'].search([('type', '=', 'bank')], limit=1).id, 'payment_method_id': self.env.ref('account.account_payment_method_manual_out').id, }) record.write({'payment_order_id': payment.id}) return { 'type': 'ir.actions.client', 'tag': 'display_notification', 'params': { 'title': 'Request Bank Payment', 'message': f'you are request bank payment done.', 'sticky': False, } } def action_post_vendor_bill(self): """ Post the vendor bill in Accounting. """ for record in self: if not record.bill_id: raise UserError(_("No vendor bill exists for this payment request.")) if record.bill_id.state != 'draft': raise UserError(_("The vendor bill is already posted or canceled.")) # Post the vendor bill record.bill_id.action_post() if record.bill_id: return { 'type': 'ir.actions.client', 'tag': 'display_notification', 'params': { 'title': 'Post Vendor Bill', 'message': f'you are Post Vendor Bill done.', 'sticky': False, } } class ProposalPayment(models.Model): _name = 'p.payment' _description = 'សំណើសុំអនុម័តគោលការណ៍' name = fields.Char() amount = fields.Monetary(string="Payment Amount", currency_field='currency_id') currency_id = fields.Many2one( 'res.currency', string="Currency", default=lambda self: self.env.company.currency_id, # Default to company currency readonly=True, )