Skip to content

Commit

Permalink
[IMP] sale_planner_calendar: Improve code
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosdauden committed Oct 23, 2024
1 parent 85e151a commit 2466b74
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
50 changes: 23 additions & 27 deletions sale_planner_calendar/models/calendar_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
from odoo import api, fields, models
from odoo.tools.safe_eval import safe_eval

# from odoo.addons.calendar.models.calendar import calendar_id2real_id


class CalendarEvent(models.Model):
_inherit = "calendar.event"
Expand Down Expand Up @@ -50,6 +48,7 @@ class CalendarEvent(models.Model):
],
default="pending",
readonly=True,
tracking=True,
)
calendar_issue_type_id = fields.Many2one(
comodel_name="sale.planner.calendar.issue.type", ondelete="restrict"
Expand Down Expand Up @@ -77,7 +76,7 @@ class CalendarEvent(models.Model):
comodel_name="sale.payment.sheet.line",
inverse_name="sale_planner_calendar_event_id",
)
# Helper fields to kanban views
# Helper fields for kanban views
partner_ref = fields.Char(related="target_partner_id.ref")
partner_name = fields.Char(compute="_compute_partner_name")
partner_commercial_name = fields.Char(
Expand Down Expand Up @@ -110,32 +109,31 @@ def _compute_sale_order_subtotal(self):
for rec in self:
rec.sale_order_subtotal = sum(rec.mapped("sale_ids.amount_untaxed"))

# @api.depends("target_partner_id")
@api.depends("target_partner_id")
def _compute_invoice_amount_residual(self):
groups = self.env["account.move"].read_group(
[
partner_ids = self.mapped("target_partner_id.commercial_partner_id").ids
groups = self.env["account.move"]._read_group(
domain=[
("state", "=", "posted"),
("payment_state", "!=", "paid"),
(
"partner_id",
"in",
self.mapped("target_partner_id.commercial_partner_id").ids,
),
("partner_id", "in", partner_ids),
],
["amount_residual_signed"],
["partner_id"],
fields=["amount_residual_signed"],
groupby=["partner_id"],
)
invoice_dic = {g["partner_id"][0]: g["amount_residual_signed"] for g in groups}
for rec in self:
amount_residual = invoice_dic.get(
rec.target_partner_id.commercial_partner_id.id, 0.0
)
rec.invoice_amount_residual = amount_residual - sum(
payment_amount = sum(
rec.payment_sheet_line_ids.filtered(
lambda p: p.sheet_id.state == "open"
).mapped("amount")
)
rec.invoice_amount_residual = amount_residual - payment_amount

@api.depends("target_partner_id")
def _compute_partner_name(self):
field_name = (
self.env["ir.config_parameter"]
Expand All @@ -153,6 +151,7 @@ def _compute_partner_name(self):
or event.target_partner_id.commercial_partner_id.name
)

@api.depends("target_partner_id")
def _compute_contact(self):
for rec in self:
contact = rec.target_partner_id.child_ids.filtered(
Expand All @@ -163,35 +162,36 @@ def _compute_contact(self):
)
rec.partner_contact_name = contact.name

@api.depends("partner_mobile")
def _compute_sanitized_partner_mobile(self):
self.sanitized_partner_mobile = False
for rec in self.filtered("partner_mobile"):
rec.sanitized_partner_mobile = re.sub(r"\W+", "", rec.partner_mobile)

@api.depends("target_partner_id")
def _compute_location_url(self):
# The url is built to access the location from a google link. This will be done
# taking into account the location of the calendar event associated with the calendar
# planner event. If this location is not defined, the client's coordinates will
# be taken into account if they are defined, otherwise the client's address
# will be taken into account.
self.location_url = False
for rec in self:
event_location = rec.location
partner_latitude = str(rec.target_partner_id.partner_latitude).replace(
for event in self:
event_location = event.location
partner_latitude = str(event.target_partner_id.partner_latitude).replace(
",", "."
)
partner_longitude = str(rec.target_partner_id.partner_longitude).replace(
partner_longitude = str(event.target_partner_id.partner_longitude).replace(
",", "."
)
partner_location = f"{rec.partner_city}+{rec.partner_street}"
partner_location = f"{event.partner_city}+{event.partner_street}"
if event_location:
self.location_url = event_location.replace(" ", "+")
event.location_url = event_location.replace(" ", "+")
elif partner_latitude != "0.0" or partner_longitude != "0.0":
self.location_url = f"{partner_latitude}%2C{partner_longitude}"
event.location_url = f"{partner_latitude}%2C{partner_longitude}"
elif partner_location:
self.location_url = partner_location.replace(" ", "+")
event.location_url = partner_location.replace(" ", "+")

# Inverse methods
def _inverse_hour(self):
for rec in self:
duration = rec.duration
Expand Down Expand Up @@ -330,8 +330,6 @@ def action_done(self):
self.write(
{
"sale_planner_state": "done",
# "date": fields.Datetime.now(),
# "comment": 'Done',
}
)

Expand All @@ -340,7 +338,6 @@ def action_cancel(self):
{
"sale_planner_state": "cancel",
"comment": "Not done",
# "date": fields.Datetime.now(),
}
)

Expand All @@ -349,7 +346,6 @@ def action_pending(self):
{
"sale_planner_state": "pending",
"comment": False,
# "date": self.calendar_event_date,
}
)

Expand Down
3 changes: 2 additions & 1 deletion sale_planner_calendar/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from datetime import timedelta

from dateutil.relativedelta import relativedelta

from odoo import _, fields, models
from odoo.exceptions import ValidationError
from odoo.tools.date_utils import relativedelta


class ResPartner(models.Model):
Expand Down
3 changes: 2 additions & 1 deletion sale_planner_calendar/models/sale_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from datetime import timedelta

from dateutil.relativedelta import relativedelta

from odoo import _, api, fields, models
from odoo.tools import relativedelta


class SaleOrder(models.Model):
Expand Down

0 comments on commit 2466b74

Please sign in to comment.