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 committed Oct 22, 2024
1 parent f32db33 commit 5b436d2
Show file tree
Hide file tree
Showing 15 changed files with 157 additions and 237 deletions.
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")
60 changes: 23 additions & 37 deletions sale_planner_calendar/models/sale_planner_calendar_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,18 @@
from odoo.tools.safe_eval import safe_eval


class SalePlannerCalendarEvent(models.Model):
_name = "sale.planner.calendar.event"
_description = "Sale planner calendar event"
class CalendarEvent(models.Model):
_inherit = "calendar.event"

name = fields.Char(string="Subject")
company_id = fields.Many2one(
comodel_name="res.company", default=lambda self: self.env.company.id
)
currency_id = fields.Many2one(
sale_planner_currency_id = fields.Many2one(
comodel_name="res.currency",
related="partner_id.currency_id",
)
date = fields.Datetime(default=fields.Datetime.now)
calendar_event_id = fields.Many2one(
comodel_name="calendar.event",
related="target_partner_id.currency_id",
)
calendar_event_date = fields.Datetime(index=True)
user_id = fields.Many2one(
comodel_name="res.users",
default=lambda self: self.env.user.id,
index=True,
domain="[('share','=',False)]",
)
partner_id = fields.Many2one(comodel_name="res.partner", index=True)
sale_ids = fields.One2many(
comodel_name="sale.order",
inverse_name="sale_planner_calendar_event_id",
)
state = fields.Selection(
sale_planner_state = fields.Selection(
[
("pending", "Pending"),
("done", "Done"),
Expand All @@ -52,17 +35,20 @@ class SalePlannerCalendarEvent(models.Model):
)
comment = fields.Text()
sale_order_subtotal = fields.Monetary(
compute="_compute_sale_order_subtotal", currency_field="currency_id"
compute="_compute_sale_order_subtotal",
currency_field="sale_planner_currency_id",
)
calendar_summary_id = fields.Many2one(
comodel_name="sale.planner.calendar.summary",
copy=False,
)
invoice_amount_residual = fields.Monetary(
string="Invoice amount due",
compute="_compute_invoice_amount_residual",
compute_sudo=True,
currency_field="sale_planner_currency_id",
)
off_planning = fields.Boolean()
off_planning = fields.Boolean(copy=False)
payment_sheet_line_ids = fields.One2many(
comodel_name="sale.payment.sheet.line",
inverse_name="sale_planner_calendar_event_id",
Expand Down Expand Up @@ -151,7 +137,7 @@ def _compute_location_url(self):
# will be taken into account.
self.location_url = False
for rec in self:
event_location = rec.calendar_event_id.location
event_location = rec.location
partner_latitude = str(rec.partner_id.partner_latitude).replace(",", ".")
partner_longitude = str(rec.partner_id.partner_longitude).replace(",", ".")
partner_location = f"{rec.partner_city}+{rec.partner_street}"
Expand Down Expand Up @@ -187,14 +173,14 @@ def action_open_sale_order(self, new_order=False):
.get_param("sale_planner_calendar.create_so_to_commercial_partner", "False")
)
partner = (
self.partner_id
self.target_partner_id
if create_so_to_commercial_partner == "False"
else self.partner_id.commercial_partner_id
else self.target_partner_id.commercial_partner_id
)
action["context"] = {
"default_sale_planner_calendar_event_id": self.id,
"default_partner_id": partner.id,
"default_partner_shipping_id": self.partner_id.id,
"default_partner_shipping_id": self.target_partner_id.id,
"default_user_id": self.user_id.id,
}
if len(self.sale_ids) > 1:
Expand All @@ -211,13 +197,13 @@ def action_open_invoices(self):
ctx = safe_eval(action["context"])
ctx.update(
{
"default_partner_id": self.partner_id.id,
"default_partner_id": self.target_partner_id.id,
}
)
action["context"] = ctx
domain = safe_eval(action["domain"])
domain.append(
("partner_id", "=", self.partner_id.commercial_partner_id.id),
("partner_id", "=", self.target_partner_id.commercial_partner_id.id),
)
action["domain"] = domain
return action
Expand All @@ -226,7 +212,7 @@ def action_open_unpaid_invoice(self):
domain = [
("state", "=", "posted"),
("move_type", "in", ["out_invoice", "out_refund"]),
("partner_id", "=", self.partner_id.commercial_partner_id.id),
("partner_id", "=", self.target_partner_id.commercial_partner_id.id),
("payment_state", "!=", "paid"),
]
unpaid_invoices = self.env["account.move"].search(domain)
Expand All @@ -247,27 +233,27 @@ def action_open_unpaid_invoice(self):
def action_done(self):
self.write(
{
"state": "done",
"date": fields.Datetime.now(),
"sale_planner_state": "done",
# "date": fields.Datetime.now(),
# "comment": 'Done',
}
)

def action_cancel(self):
self.write(
{
"state": "cancel",
"sale_planner_state": "cancel",
"comment": "Not done",
"date": fields.Datetime.now(),
# "date": fields.Datetime.now(),
}
)

def action_pending(self):
self.write(
{
"state": "pending",
"sale_planner_state": "pending",
"comment": False,
"date": self.calendar_event_date,
# "date": self.calendar_event_date,
}
)

Expand Down
Loading

0 comments on commit 5b436d2

Please sign in to comment.