I use Odoo14 community version,
I have a custom module that can create an invoice for student course registration, here is the code to create student invoice:
def create_invoice(self):and I also have state for the student status, here is the code:
invoice = self.env['account.move'].create({
'move_type': 'out_invoice',
'partner_id': self.student_id.partner_id.id,
'invoice_line_ids': (
{
'product_id': self.course_id.product_id,
'quantity': 1,
'price_unit': self.course_id.product_id.lst_price,
'tax_ids': False
},
{
'product_id': self.subject_ids.product_id,
'quantity': 1,
'price_unit': self.subject_ids.product_id.lst_price,
'tax_ids': False
}
)
})
state = fields.Selection(string="Student Status", selection=[when we triggered the create_invoice method on a button, the state will move to payment_process.
('waiting', 'Waiting List'),
('has_been_contacted', 'Has been Contacted'),
('payment_process', 'Payment Process'),
('registered_student', 'Registered'),
('active_student', 'Active Student'),
('unactive_student', 'Unactive Student'),
], default='waiting')
Now, I want if the created invoice payment state is paid, wether my user is access the invoice from my custom module or directly from Invoice/Accounting module, I want the state on my module is move automatically to registered_student.
My question are, how to do that?
Any help, source or tutorial how to do that will be very appreciated,
Edit:
I also create a field to save the registration ID on my models:
class AqurStudentCourse(models.Model):
_name = "aqur.student.course"
registration_invoice_id = fields.Integer(string='Registration Invoice ID')
Then I insert value on the registration_invoice_id when I create invoice:
def create_invoice(self):
invoice = self.env['account.move'].create({
'move_type': 'out_invoice',
'partner_id': self.student_id.partner_id.id,
# 'invoice_date': date_invoice,
'invoice_line_ids': (
{
'product_id': self.course_id.product_id,
'quantity': 1,
'price_unit': self.course_id.product_id.lst_price,
'tax_ids': False
},
{
'product_id': self.subject_ids.product_id,
'quantity': 1,
'price_unit': self.subject_ids.product_id.lst_price,
'tax_ids': False
}
)
})
self.registration_invoice_id = invoice.id
Here is the screenshot how I create the invoice from button:
When I create the Registration Fee button, it will create an invoice, then the state will move to Payment Process. And here is the created invoice:
Now , I want if the field invoice_payment_state from the account.move model if the status has been paid:
I want the state from my custom module automatically change to registered_student:
I try to apply the overide function code that suggest by @Niyas, here is the code:
def write(self, vals):I call the above function from my AqurStudentCourse model, and I got the result from the invoice_payment_state.
invoice_payment_state = self.env['account.move'].search(
[('partner_id.id', '=', self.student_id.partner_id.id), ('id', '=', self.registration_invoice_id),
('payment_state', '=', 'paid')])
if 'state' in vals and vals['state']:
if invoice_payment_state.payment_state:
self.state = 'registered_student'
return super(AqurStudentCourse, self).write(vals)
But, when I test the function how it works, nothings is happen on my student course state when I try to Register Payment on the Invoice and get the Invoice status to Paid.
Even, I got this error message when I try to change the state manually:
Traceback (most recent call last): File "/opt/odoo/odoo14/odoo/http.py", line 640, in _handle_exception return super(JsonRequest, self)._handle_exception(exception) File "/opt/odoo/odoo14/odoo/http.py", line 316, in _handle_exception raise exception.with_traceback(None) from new_cause RecursionError: maximum recursion depth exceeded while calling a Python objectSo, what is wrong with my overide method?
Please, any help would be very appreciated.
Thank you,
Tri Nanda
EDIT 2:
I try to override the AccountMove models now with this code:
class AccountMove(models.Model):but, nothing is happen when the invoice has paid, the student course state still not update anythings, is there anothers solutions?
_inherit = "account.move"
def write(self, vals):
student_course_registration_id = self.env['aqur.student.course'].search(
[('registration_invoice_id', '=', self.id)])
course_registration_status = self.env['account.move'].search(
[('id', '=', student_course_registration_id.registration_invoice_id)])
if course_registration_status.payment_state == 'paid':
self.env['aqur.student.course'].search(
[('registration_invoice_id', '=', course_registration_status.id)]).write(
{'state': 'registered_student'})
return super(AccountMove, self).write(vals)
Thanks,
Tri Nanda
Yes @Niyas, I try that logic, but doesnt affect anything, may you sugges me other way with other example code?, or source or tutorial how to do that?, thank you Niyas