From 6752c85afef0c66eedc30cb72870140d709b671f Mon Sep 17 00:00:00 2001 From: "robin.keunen" Date: Mon, 7 Dec 2020 19:44:30 +0100 Subject: [PATCH] [IMP] emc_loan: add batch buttons for loan issue lines --- easy_my_coop_loan/__manifest__.py | 3 +- easy_my_coop_loan/data/actions.xml | 56 +++++++++++++++++++++ easy_my_coop_loan/models/loan_issue_line.py | 39 ++++++++++---- 3 files changed, 88 insertions(+), 10 deletions(-) create mode 100644 easy_my_coop_loan/data/actions.xml diff --git a/easy_my_coop_loan/__manifest__.py b/easy_my_coop_loan/__manifest__.py index f59bd67..97d24b7 100644 --- a/easy_my_coop_loan/__manifest__.py +++ b/easy_my_coop_loan/__manifest__.py @@ -16,10 +16,11 @@ """, "data": [ "security/ir.model.access.csv", + "data/actions.xml", + "data/mail_template_data.xml", "views/loan_view.xml", "views/partner_view.xml", "views/menus.xml", - "data/mail_template_data.xml", ], "demo": ["demo/coop.xml"], "installable": True, diff --git a/easy_my_coop_loan/data/actions.xml b/easy_my_coop_loan/data/actions.xml new file mode 100644 index 0000000..903ead3 --- /dev/null +++ b/easy_my_coop_loan/data/actions.xml @@ -0,0 +1,56 @@ + + + + + Validate Loans + + + code + +records.action_validate() + + + + + Request Payments + + + code + +records.action_request_payment() + + + + + Cancel Loans + + + code + +records.action_cancel() + + + + + Set as Paid + + + code + +records.action_paid() + + + + + Set to Draft + + + code + +records.action_draft() + + + diff --git a/easy_my_coop_loan/models/loan_issue_line.py b/easy_my_coop_loan/models/loan_issue_line.py index f8cafcf..9994037 100644 --- a/easy_my_coop_loan/models/loan_issue_line.py +++ b/easy_my_coop_loan/models/loan_issue_line.py @@ -4,7 +4,8 @@ from datetime import datetime -from odoo import api, fields, models +from odoo import _, api, fields, models +from odoo.exceptions import ValidationError class LoanIssueLine(models.Model): @@ -93,26 +94,41 @@ class LoanIssueLine(models.Model): @api.multi def action_draft(self): - for line in self: - line.write({"state": "draft"}) + if self.filtered(lambda l: l.state != "cancelled"): + raise ValidationError( + _("You can only set cancelled loans to draft") + ) + self.write({"state": "draft"}) @api.multi def action_validate(self): - for line in self: - line.write({"state": "subscribed"}) + if self.filtered(lambda l: l.state != "draft"): + raise ValidationError(_("You can only validate draft loans")) + self.write({"state": "subscribed"}) @api.multi def action_request_payment(self): - pay_req_mail_template = self.get_loan_pay_req_mail_template() + if self.filtered(lambda l: l.state != "subscribed"): + raise ValidationError( + _("You can only request payment for validated loans") + ) for line in self: + pay_req_mail_template = line.get_loan_pay_req_mail_template() pay_req_mail_template.send_mail(line.id) line.write({"state": "waiting"}) @api.multi def action_cancel(self): - for line in self: - line.write({"state": "cancelled"}) + allowed_states = ["draft", "subscribed", "waiting"] + if self.filtered(lambda l: l.state not in allowed_states): + raise ValidationError( + _( + "You can only cancel loans in states draft, " + "subscribed or waiting for payment." + ) + ) + self.write({"state": "cancelled"}) @api.multi def get_confirm_paid_email_template(self): @@ -123,8 +139,13 @@ class LoanIssueLine(models.Model): @api.multi def action_paid(self): + if self.filtered(lambda l: l.state != "waiting"): + raise ValidationError( + _("You can only park as paid loans waiting for payment") + ) + for line in self: - loan_email_template = self.get_confirm_paid_email_template() + loan_email_template = line.get_confirm_paid_email_template() loan_email_template.sudo().send_mail(line.id, force_send=False) line.write({"state": "paid"})