Skip to content

Commit

Permalink
[IMP] account_credit_control: pre-commit
Browse files Browse the repository at this point in the history
Update patch imported from OCA#86 to new style guidelines.
  • Loading branch information
Jairo Llopis authored and Naglis Jonaitis committed Jul 28, 2021
1 parent 288e5fe commit 4e09ed9
Show file tree
Hide file tree
Showing 14 changed files with 181 additions and 130 deletions.
12 changes: 6 additions & 6 deletions account_credit_control/data/data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
<record id="email_template_credit_control_base" model="mail.template">
<field name="name">Credit Control Email</field>
<field name="email_from">${user.company_id.email or ''}</field>
<field name="subject">Credit Control: (${object.policy_level_id.name or 'n/a'})</field>
<field
name="subject"
>Credit Control: (${object.policy_level_id.name or 'n/a'})</field>
<field name="partner_to">${object.get_emailing_contact().id or ''}</field>
<field name="model_id" ref="model_credit_control_communication" />
<field name="auto_delete" eval="False" />
Expand Down Expand Up @@ -222,15 +224,13 @@
<record id="base.main_company" model="res.company">
<field name="credit_policy_id" ref="credit_control_3_time" />
</record>

<!-- Mail subtypes -->
<record id="mt_request" model="mail.message.subtype">
<field name="name">Credit control</field>
<field name="description">Credit control notification</field>
<field name="res_model">account.credit.control.communication</field>
<field name="default" eval="True"/>
<field name="hidden" eval="False"/>
<field name="internal" eval="False"/>
<field name="default" eval="True" />
<field name="hidden" eval="False" />
<field name="internal" eval="False" />
</record>

</odoo>
10 changes: 5 additions & 5 deletions account_credit_control/demo/res_users.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<!-- Copyright 2020 Tecnativa - Jairo Llopis
License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -->

<data noupdate="1">

<record id="base.user_demo" model="res.users">
<field name="groups_id" eval="[(4, ref('group_account_credit_control_user'))]"/>
<field
name="groups_id"
eval="[(4, ref('group_account_credit_control_user'))]"
/>
</record>

</data>
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def migrate(env, version):
# manually changed if desired althoug they'll keep working as there's
# backward compatibility with the old method
communication_template = env.ref(
"account_credit_control.email_template_credit_control_base")
"account_credit_control.email_template_credit_control_base"
)
communication_template.email_to = False
communication_template.partner_to = (
"${object.get_emailing_contact().id or ''}")
communication_template.partner_to = "${object.get_emailing_contact().id or ''}"
9 changes: 2 additions & 7 deletions account_credit_control/migrations/12.0.3.0.0/pre-migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,9 @@ def migrate(env, version):
mail_message_legacy = openupgrade.get_legacy_name("mail_message_id")
openupgrade.copy_columns(
env.cr,
{
"credit_control_line": [
("mail_message_id", mail_message_legacy, None),
]
},
{"credit_control_line": [("mail_message_id", mail_message_legacy, None)]},
)
# Vacuum meaningless transient data from credit.control.communication
openupgrade.logged_query(
env.cr,
"DELETE FROM credit_control_communication",
env.cr, "DELETE FROM credit_control_communication",
)
47 changes: 28 additions & 19 deletions account_credit_control/models/credit_control_communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,25 @@ class CreditControlCommunication(models.Model):
comodel_name="res.partner", string="Partner", required=True, index=True
)
policy_id = fields.Many2one(
related='policy_level_id.policy_id',
store=True,
index=True,
related="policy_level_id.policy_id", store=True, index=True,
)
policy_level_id = fields.Many2one(
comodel_name="credit.control.policy.level", string="Level", required=True, index=True
comodel_name="credit.control.policy.level",
string="Level",
required=True,
index=True,
)
currency_id = fields.Many2one(
comodel_name="res.currency", string="Currency", required=True, index=True
)
credit_control_line_ids = fields.One2many(
comodel_name="credit.control.line", inverse_name="communication_id", string="Credit Lines"
comodel_name="credit.control.line",
inverse_name="communication_id",
string="Credit Lines",
)
contact_address_id = fields.Many2one(
comodel_name="res.partner", readonly=True, index=True
)
contact_address_id = fields.Many2one(comodel_name="res.partner", readonly=True, index=True)
report_date = fields.Date(default=lambda self: fields.Date.context_today(self))

company_id = fields.Many2one(
Expand All @@ -41,15 +46,18 @@ class CreditControlCommunication(models.Model):
index=True,
)
user_id = fields.Many2one(
comodel_name="res.users", default=lambda self: self.env.user, string="User", index=True,
comodel_name="res.users",
default=lambda self: self.env.user,
string="User",
index=True,
)
total_invoiced = fields.Float(compute="_compute_total")
total_due = fields.Float(compute="_compute_total")

@api.model
def _default_company(self):
company_obj = self.env['res.company']
return company_obj._company_default_get('credit.control.policy')
company_obj = self.env["res.company"]
return company_obj._company_default_get("credit.control.policy")

@api.model
def _get_total(self):
Expand All @@ -75,13 +83,13 @@ def _compute_total(self):
def _onchange_partner_id(self):
"""Update address when partner changes."""
for one in self:
partners = one.env["res.partner"].search([
("id", "child_of", one.partner_id.id),
])
partners = one.env["res.partner"].search(
[("id", "child_of", one.partner_id.id)]
)
if one.contact_address_id in partners:
# Contact is already child of partner
return
address_ids = one.partner_id.address_get(adr_pref=['invoice'])
address_ids = one.partner_id.address_get(adr_pref=["invoice"])
one.contact_address_id = address_ids["invoice"]

def get_emailing_contact(self):
Expand Down Expand Up @@ -149,8 +157,9 @@ def _aggregate_credit_lines(self, lines):
group["currency_id"],
group["company_id"],
)
company_currency = self.env["res.company"].browse(
group["company_id"]).currency_id
company_currency = (
self.env["res.company"].browse(group["company_id"]).currency_id
)
data["credit_control_line_ids"] = [(6, 0, level_lines.ids)]
data["partner_id"] = group["partner_id"]
data["policy_level_id"] = group["policy_level_id"]
Expand Down Expand Up @@ -179,12 +188,12 @@ def _generate_emails(self):
notify=True,
subtype_id=self.env.ref("account_credit_control.mt_request").id,
)
comm.credit_control_line_ids \
.filtered(lambda line: line.state == 'to_be_sent') \
.write({"state": "queued"})
comm.credit_control_line_ids.filtered(
lambda line: line.state == "to_be_sent"
).write({"state": "queued"})

@api.returns("credit.control.line")
def _mark_credit_line_as_sent(self):
lines = self.mapped('credit_control_line_ids')
lines = self.mapped("credit_control_line_ids")
lines.write({"state": "sent"})
return lines
10 changes: 5 additions & 5 deletions account_credit_control/models/credit_control_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def _default_policies(self):
compute="_compute_credit_control_count", string="# of Credit Control Lines"
)
credit_control_communication_count = fields.Integer(
compute='_compute_credit_control_count',
string='# of Credit Control Communications',
compute="_compute_credit_control_count",
string="# of Credit Control Communications",
)
hide_change_state_button = fields.Boolean()
company_id = fields.Many2one(
Expand Down Expand Up @@ -182,11 +182,11 @@ def open_credit_communications(self):
"""Open the generated communications."""
self.ensure_one()
action = self.env.ref(
'account_credit_control.credit_control_communication_action'
"account_credit_control.credit_control_communication_action"
)
action = action.read()[0]
action['domain'] = [
('id', 'in', self.mapped("line_ids.communication_id").ids),
action["domain"] = [
("id", "in", self.mapped("line_ids.communication_id").ids),
]
return action

Expand Down
5 changes: 1 addition & 4 deletions account_credit_control/models/mail_mail.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ def _postprocess_sent_message(
mt_request = self.env.ref("account_credit_control.mt_request")
if self.subtype_id == mt_request:
lines = self.env["credit.control.line"].search(
[
("communication_id", "=", msg.res_id),
("state", "=", "queued"),
]
[("communication_id", "=", msg.res_id), ("state", "=", "queued")]
)
new_state = "sent" if self.state == "sent" else "email_error"
lines.write({"state": new_state})
Expand Down
21 changes: 12 additions & 9 deletions account_credit_control/models/mail_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@


class MailMessage(models.Model):
_inherit = 'mail.message'
_inherit = "mail.message"

def message_format(self):
result = super().message_format()
credit_control = self.env['ir.model.data'].xmlid_to_res_id(
'account_credit_control.mt_request'
credit_control = self.env["ir.model.data"].xmlid_to_res_id(
"account_credit_control.mt_request"
)
for message in result:
message.update({
'is_discussion': message['is_discussion'] or (
message['subtype_id'] and
message['subtype_id'][0] == credit_control
)
})
message.update(
{
"is_discussion": message["is_discussion"]
or (
message["subtype_id"]
and message["subtype_id"][0] == credit_control
)
}
)
return result
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ def _get_sql_query(self):
return """
CREATE VIEW credit_control_analysis
AS
(SELECT DISTINCT ON (%s) %s
%s
ORDER BY %s)
""" % (
(SELECT DISTINCT ON ({}) {}
{}
ORDER BY {})
""".format(
self._distinct_fields(),
self._fields_to_select(),
self._from_tables(),
Expand Down
13 changes: 9 additions & 4 deletions account_credit_control/security/account_security.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@
</record>
<record id="credit_control_communication_rule" model="ir.rule">
<field name="name">Credit Control Communication</field>
<field name="model_id" ref="model_credit_control_communication"/>
<field name="global" eval="True"/>
<field name="domain_force">['|',('company_id','=',False),('company_id','in',company_ids)]</field>
<field name="model_id" ref="model_credit_control_communication" />
<field name="global" eval="True" />
<field
name="domain_force"
>['|',('company_id','=',False),('company_id','in',company_ids)]</field>
</record>
<record id="credit_control_line_rule" model="ir.rule">
<field name="name">Credit Control Line</field>
Expand Down Expand Up @@ -82,7 +84,10 @@
>['|',('company_id','=',False),('company_id','in',company_ids)]</field>
</record>
<record id="base.user_admin" model="res.users">
<field name="groups_id" eval="[(4, ref('group_account_credit_control_manager'))]"/>
<field
name="groups_id"
eval="[(4, ref('group_account_credit_control_manager'))]"
/>
</record>
</data>
</odoo>
Loading

0 comments on commit 4e09ed9

Please sign in to comment.