Skip to content

Commit

Permalink
[REF] Move transport request into joint_buying_base module
Browse files Browse the repository at this point in the history
  • Loading branch information
legalsylvain committed Nov 21, 2023
1 parent 8764760 commit ad72527
Show file tree
Hide file tree
Showing 30 changed files with 359 additions and 268 deletions.
12 changes: 11 additions & 1 deletion joint_buying_base/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
"depends": [
"base",
"mail",
"web",
"decimal_precision",
# OCA
"base_geolocalize_company",
"res_company_code",
Expand All @@ -21,7 +23,9 @@
# GRAP
"name_search_reset_res_partner",
],
"external_dependencies": {"python": ["openupgradelib", "geopy", "bokeh", "pandas"]},
"external_dependencies": {
"python": ["openupgradelib", "geopy", "bokeh", "pandas", "treelib"]
},
"data": [
"security/ir_module_category.xml",
"security/res_groups.xml",
Expand All @@ -38,7 +42,12 @@
"views/view_joint_buying_tour_type.xml",
"views/view_joint_buying_tour.xml",
"views/view_joint_buying_tour_line.xml",
"views/view_joint_buying_transport_request.xml",
"views/view_joint_buying_transport_request_line.xml",
"wizards/view_joint_buying_wizard_set_tour.xml",
"wizards/joint_buying_wizard_find_route.xml",
"reports/report_joint_buying_tour.xml",
"reports/report.xml",
"views/templates.xml",
],
"demo": [
Expand All @@ -49,6 +58,7 @@
"demo/joint_buying_carrier.xml",
"demo/joint_buying_tour_type.xml",
"demo/joint_buying_tour.xml",
"demo/joint_buying_transport_request.xml",
],
"post_init_hook": "_create_joint_buying_partner_for_companies",
"installable": True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@
<field name="manual_total_weight">33</field>
</record>

<function model="joint.buying.transport.request" name="button_compute_tour">
<value eval="[
ref('joint_buying_base.request_vev_cda_week_1'),
ref('joint_buying_base.request_vev_che_week_1'),
ref('joint_buying_base.request_vev_edc_week_1'),
ref('joint_buying_base.request_vev_fumet_dombes_week_1'),
ref('joint_buying_base.request_vev_che_week_2'),
]"/>
</function>

</odoo>
2 changes: 1 addition & 1 deletion joint_buying_base/demo/res_partner.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<odoo>

<record id="supplier_demo_joint_buying" model="res.partner">
<field name="name">Joint Buying Supplier for Your Company / Demo User</field>
<field name="name">Joint Buying Supplier for Your Company</field>
<field name="street">street</field>
<field name="street2">street 2</field>
<field name="city">City</field>
Expand Down
2 changes: 2 additions & 0 deletions joint_buying_base/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
from . import joint_buying_tour_type
from . import joint_buying_tour
from . import joint_buying_tour_line
from . import joint_buying_transport_request
from . import joint_buying_transport_request_line
19 changes: 19 additions & 0 deletions joint_buying_base/models/joint_buying_tour_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from odoo import _, api, fields, models
from odoo.exceptions import UserError

from odoo.addons import decimal_precision as dp

from .res_partner import _JOINT_BUYING_PARTNER_CONTEXT

_TOUR_LINE_SEQUENCE_TYPES = [
Expand Down Expand Up @@ -64,6 +66,17 @@ class JointBuyingTourLine(models.Model):
compute="_compute_costs", store=True, currency_field="currency_id"
)

transport_request_line_ids = fields.One2many(
comodel_name="joint.buying.transport.request.line",
string="Transport Lines",
inverse_name="tour_line_id",
)

load = fields.Float(
compute="_compute_load",
digits=dp.get_precision("Stock Weight"),
)

@api.depends("start_date", "arrival_date")
def _compute_hours(self):
for line in self:
Expand All @@ -86,6 +99,12 @@ def _compute_costs(self):
line.salary_cost = line.duration * line.tour_id.hourly_cost
line.vehicle_cost = +line.distance * line.tour_id.kilometer_cost

def _compute_load(self):
for tour_line in self:
tour_line.load = sum(
tour_line.mapped("transport_request_line_ids.request_id.total_weight")
)

def _estimate_route_project_osrm(self):
self.ensure_one()
url = (
Expand Down
202 changes: 202 additions & 0 deletions joint_buying_base/models/joint_buying_transport_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
# Copyright (C) 2023-Today: GRAP (http://www.grap.coop)
# @author: Sylvain LE GAL (https://twitter.com/legalsylvain)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

from odoo import api, fields, models

from odoo.addons import decimal_precision as dp

from .res_partner import _JOINT_BUYING_PARTNER_CONTEXT


class JointBuyingTransportRequest(models.Model):
_name = "joint.buying.transport.request"
_description = "Joint Buying Transport Request"

name = fields.Char(readonly=True, compute="_compute_name", store=True)

state = fields.Selection(
selection=[
("to_compute", "To Compute"),
("computed", "Computed"),
("not_computable", "Not Computable"),
],
required=True,
readonly=True,
default="to_compute",
)

manual_start_date = fields.Datetime(
string="Start Date (Manual)",
)

start_date = fields.Datetime(
string="Start Date",
compute="_compute_start_date",
store=True,
)

manual_origin_partner_id = fields.Many2one(
comodel_name="res.partner",
string="Origin (Manual)",
context=_JOINT_BUYING_PARTNER_CONTEXT,
domain="[('is_joint_buying_stage', '=', True)]",
)

origin_partner_id = fields.Many2one(
comodel_name="res.partner",
compute="_compute_origin_partner_id",
string="Origin",
store=True,
context=_JOINT_BUYING_PARTNER_CONTEXT,
)

manual_destination_partner_id = fields.Many2one(
comodel_name="res.partner",
string="Destination (Manual)",
context=_JOINT_BUYING_PARTNER_CONTEXT,
domain="[('is_joint_buying_stage', '=', True)]",
)

destination_partner_id = fields.Many2one(
comodel_name="res.partner",
compute="_compute_destination_partner_id",
string="Destination",
store=True,
context=_JOINT_BUYING_PARTNER_CONTEXT,
)

manual_amount_untaxed = fields.Float(
string="Untaxed Amount (Manual)",
digits=dp.get_precision("Product Price"),
)

amount_untaxed = fields.Float(
string="Untaxed Amount",
compute="_compute_amount_untaxed",
store=True,
digits=dp.get_precision("Product Price"),
)

manual_total_weight = fields.Float(
string="Weight (Manual)",
digits=dp.get_precision("Stock Weight"),
)

total_weight = fields.Float(
string="Weight",
compute="_compute_weight",
store=True,
digits=dp.get_precision("Stock Weight"),
)

line_ids = fields.One2many(
comodel_name="joint.buying.transport.request.line",
string="Transport Lines",
inverse_name="request_id",
)

arrival_date = fields.Datetime(string="Arrival Date", readonly=True)

manual_description = fields.Html()

def _get_depends_start_date(self):
return ["manual_start_date"]

def _get_depends_origin_partner_id(self):
return ["manual_origin_partner_id"]

def _get_depends_destination_partner_id(self):
return ["manual_destination_partner_id"]

def _get_depends_amount_untaxed(self):
return ["manual_amount_untaxed"]

def _get_depends_total_weight(self):
return ["manual_total_weight"]

@api.depends("origin_partner_id", "destination_partner_id", "start_date")
def _compute_name(self):
for request in self:
request.name = (
f"{request.origin_partner_id.joint_buying_code}"
f" -> {request.destination_partner_id.joint_buying_code}"
f" ({request.start_date})"
)

@api.depends(lambda x: x._get_depends_start_date())
def _compute_start_date(self):
for request in self:
request.start_date = request.manual_start_date

@api.depends(lambda x: x._get_depends_origin_partner_id())
def _compute_origin_partner_id(self):
for request in self:
request.origin_partner_id = request.manual_origin_partner_id

@api.depends(lambda x: x._get_depends_destination_partner_id())
def _compute_destination_partner_id(self):
for request in self:
request.destination_partner_id = request.manual_destination_partner_id

@api.depends(lambda x: x._get_depends_amount_untaxed())
def _compute_amount_untaxed(self):
for request in self:
request.amount_untaxed = request.manual_amount_untaxed

@api.depends(lambda x: x._get_depends_total_weight())
def _compute_weight(self):
for request in self:
request.total_weight = request.manual_total_weight

def _set_tour_lines(self, tour_lines):
self.ensure_one()
# If lines are valid
if (
tour_lines
and tour_lines[-1].arrival_point_id == self.destination_partner_id
):
line_vals = []
previous_tour_id = False
for i, tour_line in enumerate(tour_lines):
# Compute if it is a loading at the beginning of the tour line
if previous_tour_id != tour_line.tour_id.id:
start_action_type = "loading"
else:
start_action_type = "no"
previous_tour_id = tour_line.tour_id.id

# Compute if it is an unloading at the end of the tour line
if i < len(tour_lines) - 1:
if tour_line.tour_id != tour_lines[i + 1].tour_id:
arrival_action_type = "unloading"
else:
arrival_action_type = "no"
else:
arrival_action_type = "unloading"
line_vals.append(
{
"start_action_type": start_action_type,
"arrival_action_type": arrival_action_type,
"tour_line_id": tour_line.id,
}
)

vals = {
"arrival_date": max(tour_lines.mapped("arrival_date")),
"state": "computed",
"line_ids": [(5,)] + [(0, 0, x) for x in line_vals],
}
else:
vals = {
"arrival_date": False,
"state": "not_computable",
"line_ids": [(5,)],
}
self.write(vals)

def button_compute_tour(self):
Wizard = self.env["joint.buying.wizard.find.route"]
results = Wizard.compute_tours(self)
for request in self:
request._set_tour_lines(results[request][1])
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

from odoo import fields, models

from odoo.addons.joint_buying_base.models.res_partner import (
_JOINT_BUYING_PARTNER_CONTEXT,
)
from .res_partner import _JOINT_BUYING_PARTNER_CONTEXT


class JointBuyingTransportRequestLine(models.Model):
Expand Down
14 changes: 14 additions & 0 deletions joint_buying_base/reports/report.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>

<report
id="action_report_joint_buying_tour"
string="Tour"
model="joint.buying.tour"
report_type="qweb-pdf"
file="joint_buying_base.report_joint_buying_tour"
name="joint_buying_base.report_joint_buying_tour"
print_report_name="('Tour %s' % (object.name)).replace('/', '_')"
/>

</odoo>
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,8 @@
<tr style="background-color:#EEE">
<th class="text-center" colspan="2"><h4>Loading</h4></th>
</tr>
<t t-call="joint_buying_product.report_joint_buying_tour_document_request_lines" />
</tbody>
<!-- <t t-call="joint_buying_base.report_joint_buying_tour_document_request_lines" />
--> </tbody>
</table>
</t>

Expand All @@ -176,8 +176,8 @@
<tr style="background-color:#EEE">
<th class="text-center" colspan="2"><h4>Unloading</h4></th>
</tr>
<t t-call="joint_buying_product.report_joint_buying_tour_document_request_lines" />
</tbody>
<!-- <t t-call="joint_buying_base.report_joint_buying_tour_document_request_lines" />
--> </tbody>
</table>
</t>
</t>
Expand All @@ -189,7 +189,7 @@
<template id="report_joint_buying_tour">
<t t-call="web.html_container">
<t t-foreach="docs" t-as="doc">
<t t-call="joint_buying_product.report_joint_buying_tour_document" t-lang="doc.starting_point_id.lang"/>
<t t-call="joint_buying_base.report_joint_buying_tour_document" t-lang="doc.starting_point_id.lang"/>
</t>
</t>
</template>
Expand Down
2 changes: 2 additions & 0 deletions joint_buying_base/security/ir.model.access.csv
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ access_joint_buying_tour_user,access_joint_buying_tour_user,model_joint_buying_t
access_joint_buying_tour_manager,access_joint_buying_tour_manager,model_joint_buying_tour,joint_buying_base.group_joint_buying_manager,1,1,1,1
access_joint_buying_tour_line_user,access_joint_buying_tour_line_user,model_joint_buying_tour_line,joint_buying_base.group_joint_buying_user,1,,,
access_joint_buying_tour_line_manager,access_joint_buying_tour_line_manager,model_joint_buying_tour_line,joint_buying_base.group_joint_buying_manager,1,1,1,1
access_joint_buying_transport_request_user,access_joint_buying_transport_request_user,model_joint_buying_transport_request,joint_buying_base.group_joint_buying_user,1,1,1,1
access_joint_buying_transport_request_line_user,access_joint_buying_transport_request_line_user,model_joint_buying_transport_request_line,joint_buying_base.group_joint_buying_user,1,1,1,1
access_joint_buying_carrier_user,access_joint_buying_carrier_user,model_joint_buying_carrier,joint_buying_base.group_joint_buying_user,1,,,
access_joint_buying_carrier_manager,access_joint_buying_carrier_manager,model_joint_buying_carrier,joint_buying_base.group_joint_buying_manager,1,1,1,1
access_joint_buying_tour_type_user,access_joint_buying_tour_type_user,model_joint_buying_tour_type,joint_buying_base.group_joint_buying_user,1,,,
Expand Down
1 change: 1 addition & 0 deletions joint_buying_base/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
from . import test_joint_buying_mixin
from . import test_partner_global_local
from . import test_partner_subscription
from . import test_joint_buying_wizard_find_route
from . import test_tour
Loading

0 comments on commit ad72527

Please sign in to comment.