Skip to content

Commit

Permalink
[IMP] sale_planner_calendar: Refactor to use calendar.event model ins…
Browse files Browse the repository at this point in the history
…tead of sale.planner.calendar.event
  • Loading branch information
sergio-teruel authored and carlosdauden committed Oct 23, 2024
1 parent 327cc27 commit 3976360
Show file tree
Hide file tree
Showing 19 changed files with 289 additions and 259 deletions.
2 changes: 1 addition & 1 deletion sale_planner_calendar/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "Sale planner calendar",
"summary": "Sale planner calendar",
"version": "16.0.1.0.1",
"version": "16.0.2.0.0",
"development_status": "Beta",
"category": "Sale",
"website": "https://github.com/OCA/sale-workflow",
Expand Down
80 changes: 80 additions & 0 deletions sale_planner_calendar/migrations/16.0.2.0.0/post-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Copyright 2024 Tecnativa - Sergio Teruel
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from openupgradelib import openupgrade
from psycopg2.extensions import AsIs


def _sale_planner_calendar_event_to_calendar_event(env):
"""Move all data from sale_planner_calendar_event to code model calendar_event"""
sql = """
UPDATE calendar_event SET
sale_planner_state = spce.state,
calendar_issue_type_id = spce.calendar_issue_type_id,
calendar_event_profile_id = spce.calendar_event_profile_id,
comment = spce.comment,
calendar_summary_id = spce.calendar_summary_id,
off_planning = spce.off_planning
FROM sale_planner_calendar_event spce
WHERE spce.calendar_event_id = calendar_event.id
"""
openupgrade.logged_query(env.cr, sql)


def _sale_order_to_calendar_event(env):
"""Link sale order to calendar events instead of sale planner calendar event model"""
# drop_constraint(env.cr, 'sale_order', 'sale_order_sale_planner_calendar_event_id_fkey')
openupgrade.logged_query(
env.cr,
"""
UPDATE sale_order SET
sale_planner_calendar_event_id = spce.calendar_event_id
FROM sale_planner_calendar_event spce
WHERE sale_order.%(old_column)s = spce.id
""",
{
"old_column": AsIs(
openupgrade.get_legacy_name("sale_planner_calendar_event_id")
)
},
)


def _payment_sheet_to_calendar_event(env):
"""Link sale payment sheet to calendar events instead of sale planner calendar
event model
"""
# drop_constraint(env.cr, 'sale_payment_sheet_line',
# 'sale_payment_sheet_line_sale_planner_calendar_event_id_fkey')
openupgrade.logged_query(
env.cr,
"""
UPDATE sale_payment_sheet_line SET
sale_planner_calendar_event_id = spce.calendar_event_id
FROM sale_planner_calendar_event spce
WHERE sale_payment_sheet_line.%(old_column)s = spce.id
""",
{
"old_column": AsIs(
openupgrade.get_legacy_name("sale_planner_calendar_event_id")
)
},
)


def _remove_selection_field_values(env):
sql = """
DELETE FROM ir_model_fields_selection
WHERE field_id IN
(SELECT id
FROM ir_model_fields
WHERE ttype='selection' AND model='sale.planner.calendar.event')
"""
openupgrade.logged_query(env.cr, sql)


@openupgrade.migrate()
def migrate(env, version):
_sale_planner_calendar_event_to_calendar_event(env)
_sale_order_to_calendar_event(env)
_payment_sheet_to_calendar_event(env)
# _remove_selection_field_values(env)
27 changes: 27 additions & 0 deletions sale_planner_calendar/migrations/16.0.2.0.0/pre-migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2024 Tecnativa - Sergio Teruel
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
from openupgradelib import openupgrade

_column_renames = {
"sale_order": [
("sale_planner_calendar_event_id", None),
],
"sale_payment_sheet_line": [("sale_planner_calendar_event_id", None)],
}


def _remove_selection_field_values(env):
sql = """
DELETE FROM ir_model_fields_selection
WHERE field_id IN
(SELECT id
FROM ir_model_fields
WHERE ttype='selection' AND model='sale.planner.calendar.event')
"""
openupgrade.logged_query(env.cr, sql)


@openupgrade.migrate()
def migrate(env, version):
openupgrade.rename_columns(env.cr, _column_renames)
_remove_selection_field_values(env)
34 changes: 2 additions & 32 deletions sale_planner_calendar/models/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,8 @@ class CalendarEvent(models.Model):
calendar_event_profile_id = fields.Many2one(
comodel_name="sale.planner.calendar.event.profile"
)
currency_id = fields.Many2one(
comodel_name="res.currency", related="target_partner_id.currency_id"
)
hour = fields.Float(compute="_compute_hour", inverse="_inverse_hour")
target_partner_mobile = fields.Char(related="target_partner_id.mobile")
sale_planner_calendar_event_id = fields.Many2one(
comodel_name="sale.planner.calendar.event",
compute="_compute_sale_planner_calendar_event_id",
)
event_planner_state = fields.Char(
compute="_compute_sale_planner_calendar_event_id",
)
# When arrive this date we will unsubscribe user from partner documents
unsubscribe_date = fields.Date()
is_dynamic_end_date = fields.Boolean(copy=False)
Expand All @@ -59,25 +49,6 @@ def _compute_hour(self):
date = rec._get_hour_tz_offset()
rec.hour = date.hour + date.minute / 60

@api.depends("start", "target_partner_id", "user_id")
def _compute_sale_planner_calendar_event_id(self):
# TODO: Adjust order or use [:1] or [-1:] when know model order
domain = [
("calendar_event_date", "in", self.mapped("start")),
("partner_id", "in", self.mapped("target_partner_id").ids),
("user_id", "=", self.user_id.id),
]
events = self.env["sale.planner.calendar.event"].search(domain)
for rec in self:
rec.sale_planner_calendar_event_id = events.filtered(
lambda r: (
r.calendar_event_date == fields.Datetime.to_datetime(rec.start)
and r.partner_id == rec.target_partner_id
and r.user_id == rec.user_id
)
)[:1]
rec.event_planner_state = rec.sale_planner_calendar_event_id.state

def _get_hour_tz_offset(self):
timezone = self._context.get("tz") or self.env.user.partner_id.tz or "UTC"
self_tz = self.with_context(tz=timezone)
Expand All @@ -102,13 +73,12 @@ def _inverse_hour(self):
)

def _create_event_planner(self):
return self.env["sale.planner.calendar.event"].create(
return self.env["calendar.event"].create(
{
"name": self.name,
"partner_id": self.target_partner_id.id,
"user_id": self.user_id.id,
"calendar_event_date": self.start,
"calendar_event_id": self.id,
"start": self.start,
"calendar_event_profile_id": self.calendar_event_profile_id.id,
}
)
Expand Down
35 changes: 17 additions & 18 deletions sale_planner_calendar/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright 2021 Tecnativa - Sergio Teruel
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from datetime import timedelta

from odoo import _, api, fields, models
from odoo.tools import relativedelta
Expand All @@ -8,9 +9,7 @@
class SaleOrder(models.Model):
_inherit = "sale.order"

sale_planner_calendar_event_id = fields.Many2one(
comodel_name="sale.planner.calendar.event"
)
sale_planner_calendar_event_id = fields.Many2one(comodel_name="calendar.event")

def _action_confirm(self):
event_obj = self.env["calendar.event"]
Expand All @@ -32,13 +31,16 @@ def _action_confirm(self):
return super()._action_confirm()

def _prepare_calendar_event_planner(self):
categ = self.env.ref("sale_planner_calendar.event_type_commercial_visit")
return {
"name": _("Sale off planning"),
"partner_id": self.partner_id.id,
"target_partner_id": self.partner_id.id,
"user_id": self.user_id.id,
"date": self.date_order,
"start": self.date_order,
"stop": self.date_order
+ timedelta(minutes=round((categ.duration or 1.0) * 60)),
"off_planning": True,
"state": "done",
"sale_planner_state": "done",
}

def action_set_planner_calendar_event(self, planner_summary=False):
Expand All @@ -55,20 +57,17 @@ def action_set_planner_calendar_event(self, planner_summary=False):
max(order_dates), self.env.company.sale_planner_order_cut_hour
) + relativedelta(days=1)
calendar_event_domain = [
("partner_id", "in", orders.partner_id.ids),
("target_partner_id", "in", orders.partner_id.ids),
("user_id", "in", orders.user_id.ids),
("date", ">=", date_from),
("date", "<", date_to),
("start", ">=", date_from),
("start", "<", date_to),
]
if planner_summary.event_type_id:
calendar_event_domain.append(
("calendar_event_id.categ_ids", "in", planner_summary.event_type_id.ids)
("categ_ids", "in", planner_summary.event_type_id.ids)
)
calendar_events = self.env["sale.planner.calendar.event"].search(
calendar_event_domain
)
calendar_events = self.env["calendar.event"].search(calendar_event_domain)
planner_summary_domain = [
("user_id", "in", orders.user_id.ids),
("user_id", "in", orders.user_id.ids),
("date", ">=", date_from.date()),
("date", "<", date_to.date()),
Expand All @@ -85,11 +84,11 @@ def action_set_planner_calendar_event(self, planner_summary=False):
cut_time = date_from.time()
for order in orders:
event = calendar_events.filtered(
lambda ev: ev.partner_id == order.partner_id
lambda ev: ev.target_partner_id == order.partner_id
and ev.user_id == order.user_id
and (ev.date.combine(ev.date.date(), cut_time) <= order.date_order)
and (ev.start.combine(ev.start.date(), cut_time) <= order.date_order)
and (
ev.date.combine(ev.date.date(), cut_time) + relativedelta(days=1)
ev.start.combine(ev.start.date(), cut_time) + relativedelta(days=1)
> order.date_order
)
)[:1]
Expand All @@ -114,7 +113,7 @@ def action_set_planner_calendar_event(self, planner_summary=False):
continue
event_vals = order._prepare_calendar_event_planner()
event_vals["calendar_summary_id"] = event_summary.id
event = self.env["sale.planner.calendar.event"].create(event_vals)
event = self.env["calendar.event"].create(event_vals)
order.sale_planner_calendar_event_id = event

@api.model_create_multi
Expand Down
4 changes: 1 addition & 3 deletions sale_planner_calendar/models/sale_payment_sheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
class SalePaymentSheetLine(models.Model):
_inherit = "sale.payment.sheet.line"

sale_planner_calendar_event_id = fields.Many2one(
comodel_name="sale.planner.calendar.event"
)
sale_planner_calendar_event_id = fields.Many2one(comodel_name="calendar.event")
Loading

0 comments on commit 3976360

Please sign in to comment.