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 6aa9762
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
40 changes: 18 additions & 22 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 @@ -77,7 +75,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,31 +108,29 @@ 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

def _compute_partner_name(self):
field_name = (
Expand Down Expand Up @@ -175,21 +171,21 @@ def _compute_location_url(self):
# 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):
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 6aa9762

Please sign in to comment.