Skip to content

Commit

Permalink
[ADD] joint_buying_product : new transport.request model
Browse files Browse the repository at this point in the history
- add hook on (grouped).orders to create / unlink related transport requests at the good moment
- add transport request demo data
[REF] joint_buying_base: Simplification, adding joint_buying_code on res.partner model
  • Loading branch information
legalsylvain committed Oct 24, 2023
1 parent ad47ce0 commit aff9e34
Show file tree
Hide file tree
Showing 11 changed files with 302 additions and 11 deletions.
14 changes: 3 additions & 11 deletions joint_buying_base/models/joint_buying_tour.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,19 +195,11 @@ def _compute_summary(self):
for line in tour.line_ids.filtered(lambda x: x.sequence_type == "journey"):
if not codes:
codes = [
line.starting_point_id.joint_buying_company_id
and line.starting_point_id.joint_buying_company_id.code
or line.starting_point_id.name,
line.arrival_point_id.joint_buying_company_id
and line.arrival_point_id.joint_buying_company_id.code
or line.arrival_point_id.name,
line.starting_point_id.joint_buying_code,
line.arrival_point_id.joint_buying_code,
]
else:
codes.append(
line.arrival_point_id.joint_buying_company_id
and line.arrival_point_id.joint_buying_company_id.code
or line.arrival_point_id.name,
)
codes.append(line.arrival_point_id.joint_buying_code)
tour.summary = f"{' -> '.join(codes)}"

def _compute_description(self):
Expand Down
12 changes: 12 additions & 0 deletions joint_buying_base/models/res_partner.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ class ResPartner(models.Model):
help="Check this box if that address can be a step of a tour",
)

joint_buying_code = fields.Char(
help="Code diplayed for tour",
compute="_compute_joint_buying_code",
)

joint_buying_description = fields.Html(string="Complete Description")

joint_buying_is_mine_pivot = fields.Boolean(
Expand Down Expand Up @@ -157,6 +162,13 @@ def _default_joint_buying_subscribed_company_ids(self):
return companies.ids

# Compute Section
def _compute_joint_buying_code(self):
for partner in self:
if partner.joint_buying_company_id:
partner.joint_buying_code = partner.joint_buying_company_id.code
else:
partner.joint_buying_code = partner.name

def _compute_joint_buying_display_name_step(self):
if self.env.context.get("active_model") != "joint.buying.tour":
for partner in self:
Expand Down
2 changes: 2 additions & 0 deletions joint_buying_product/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"views/view_res_users.xml",
"views/view_joint_buying_purchase_order_grouped.xml",
"views/view_joint_buying_purchase_order.xml",
"views/view_joint_buying_transport_request.xml",
"views/view_res_partner.xml",
"reports/report_template.xml",
"reports/report.xml",
Expand All @@ -51,6 +52,7 @@
"demo/product_product.xml",
"demo/res_partner.xml",
"demo/joint_buying_frequency.xml",
"demo/joint_buying_transport_request.xml",
"demo/ir_cron.xml",
],
"installable": True,
Expand Down
13 changes: 13 additions & 0 deletions joint_buying_product/demo/joint_buying_transport_request.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>

<odoo>

<record id="request_vev_cda" model="joint.buying.transport.request">
<field name="manual_origin_partner_id" model="res.partner" eval="obj().env.ref('joint_buying_base.company_VEV').joint_buying_partner_id.id"/>
<field name="manual_destination_partner_id" model="res.partner" eval="obj().env.ref('joint_buying_base.company_CDA').joint_buying_partner_id.id"/>
<field name="manual_start_date" eval="(DateTime.today() + relativedelta(day=8)).strftime('%Y-%m-%d 07:00')"/>
<field name="manual_amount_untaxed">200</field>
<field name="manual_total_weight">50</field>
</record>

</odoo>
1 change: 1 addition & 0 deletions joint_buying_product/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
from . import joint_buying_purchase_order_grouped_line
from . import joint_buying_purchase_order
from . import joint_buying_purchase_order_line
from . import joint_buying_transport_request
from . import res_config_settings
30 changes: 30 additions & 0 deletions joint_buying_product/models/joint_buying_purchase_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ class JointBuyingPurchaseOrder(models.Model):
selection=_PURCHASE_STATE, required=True, default="draft", track_visibility=True
)

request_id = fields.Many2one(
comodel_name="joint.buying.transport.request",
string="Transport Request",
readonly=True,
)

pivot_company_id = fields.Many2one(
comodel_name="res.company",
string="Pivot Company",
Expand Down Expand Up @@ -294,6 +300,30 @@ def _prepare_order_vals(self, supplier, customer, categories):
res["line_ids"].append((0, 0, vals))
return res

def _hook_state_changed(self):
# Create transport requests, if not exists, and state != closed / deposited
orders_request_to_create = self.filtered(
lambda x: x.state not in ["closed", "deposited"]
).filtered(lambda x: not x.request_id)
if orders_request_to_create:
vals_list = [{"order_id": x.id} for x in orders_request_to_create]
requests = self.env["joint.buying.transport.request"].create(vals_list)
for (order, request) in zip(orders_request_to_create, requests):
order.write({"request_id": request.id})

# Unlink transport request, if exist and is null, and state == closed / deposited
orders_request_to_unlink = self.filtered(
lambda x: x.state in ["closed", "deposited"]
).filtered(lambda x: not (x.total_weight or x.amount_untaxed))
if orders_request_to_unlink:
orders_request_to_unlink.mapped("request_id").unlink()

@api.model_create_multi
def create(self, vals_list):
orders = super().create(vals_list)
orders._hook_state_changed()
return orders

def action_confirm_purchase(self):
for order in self.filtered(lambda x: x.purchase_state == "draft"):
if order.purchase_ok == "no_line":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ def update_state_value(self, check_all=False):
grouped_order.with_context(update_state_value=True).write(
{"state": correct_state}
)
grouped_order.mapped("order_ids")._hook_state_changed()
if correct_state == "closed":
grouped_order.mapped("order_ids").correct_purchase_state()

Expand Down
148 changes: 148 additions & 0 deletions joint_buying_product/models/joint_buying_transport_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
# Copyright (C) 2021-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 odoo.addons.joint_buying_base.models.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"),
],
required=True,
readonly=True,
default="to_compute",
)

order_id = fields.Many2one(
comodel_name="joint.buying.purchase.order",
string="Order",
readonly=True,
ondelete="cascade",
)

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"),
)

tour_line_ids = fields.Many2many(
comodel_name="joint.buying.tour.line",
string="Route Lines",
)

# Compute Section
@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("manual_start_date", "order_id.deposit_date")
def _compute_start_date(self):
for request in self.filtered(lambda x: x.manual_start_date):
request.start_date = request.manual_start_date
for request in self.filtered(lambda x: not x.manual_start_date):
request.start_date = request.order_id.deposit_date

@api.depends("manual_origin_partner_id", "order_id.deposit_partner_id")
def _compute_origin_partner_id(self):
for request in self.filtered(lambda x: x.manual_origin_partner_id):
request.origin_partner_id = request.manual_origin_partner_id
for request in self.filtered(lambda x: not x.manual_origin_partner_id):
request.origin_partner_id = request.order_id.deposit_partner_id

@api.depends("manual_destination_partner_id", "order_id.deposit_partner_id")
def _compute_destination_partner_id(self):
for request in self.filtered(lambda x: x.manual_destination_partner_id):
request.destination_partner_id = request.manual_destination_partner_id
for request in self.filtered(lambda x: not x.manual_destination_partner_id):
request.destination_partner_id = request.order_id.customer_id

@api.depends("manual_amount_untaxed", "order_id.amount_untaxed")
def _compute_amount_untaxed(self):
for request in self.filtered(lambda x: x.manual_amount_untaxed):
request.amount_untaxed = request.manual_amount_untaxed
for request in self.filtered(lambda x: not x.manual_amount_untaxed):
request.amount_untaxed = request.order_id.amount_untaxed

@api.depends("manual_total_weight", "order_id.total_weight")
def _compute_weight(self):
for request in self.filtered(lambda x: x.manual_total_weight):
request.total_weight = request.manual_total_weight
for request in self.filtered(lambda x: not x.manual_total_weight):
request.total_weight = request.order_id.total_weight
1 change: 1 addition & 0 deletions joint_buying_product/security/ir.model.access.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ access_joint_buying_purchase_order,access_joint_buying_purchase_order,model_join
access_joint_buying_purchase_order_grouped,access_joint_buying_purchase_order_grouped,model_joint_buying_purchase_order_grouped,joint_buying_base.group_joint_buying_user,1,1,1,
access_joint_buying_purchase_order_grouped_manager,access_joint_buying_purchase_order_grouped_manager,model_joint_buying_purchase_order_grouped,joint_buying_base.group_joint_buying_manager,1,1,1,1
access_joint_buying_frequency_user,access_joint_buying_frequency_user,model_joint_buying_frequency,joint_buying_base.group_joint_buying_user,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_category_user,access_joint_buying_category_user,model_joint_buying_category,joint_buying_base.group_joint_buying_user,1,1,1,1
access_product_product,access_product_product,product.model_product_product,joint_buying_base.group_joint_buying_user,1,1,1,1
access_product_template,access_product_template,product.model_product_template,joint_buying_base.group_joint_buying_user,1,1,1,1
Expand Down
7 changes: 7 additions & 0 deletions joint_buying_product/views/menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,11 @@ License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
sequence="1"
/>

<menuitem id="menu_transport"
name="Transports"
parent="joint_buying_base.menu_root"
sequence="10"
/>


</odoo>
84 changes: 84 additions & 0 deletions joint_buying_product/views/view_joint_buying_transport_request.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) 2021 - 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).
-->
<odoo>

<record id="view_joint_buying_transport_request_search" model="ir.ui.view">
<field name="model">joint.buying.transport.request</field>
<field name="arch" type="xml">
<search>
<field name="origin_partner_id"/>
<field name="destination_partner_id"/>
<field name="order_id"/>
<field name="state"/>
</search>
</field>
</record>

<record id="view_joint_buying_transport_request_tree" model="ir.ui.view">
<field name="model">joint.buying.transport.request</field>
<field name="arch" type="xml">
<tree decoration-muted="amount_untaxed == 0 and total_weight == 0" decoration-info="state == 'to_compute'">
<field name="start_date"/>
<field name="origin_partner_id"/>
<field name="destination_partner_id"/>
<field name="amount_untaxed"/>
<field name="total_weight"/>
<field name="order_id"/>
<field name="state"/>
</tree>
</field>
</record>

<record id="view_joint_buying_transport_request_form" model="ir.ui.view">
<field name="model">joint.buying.transport.request</field>
<field name="arch" type="xml">
<form>
<header />
<sheet>
<div class="oe_button_box" name="button_box">
</div>
<div class="oe_title">
<h1>
<field name="name"/>
</h1>
</div>
<group col="4">
<field name="order_id"/>
<newline/>
<field name="start_date"/>
<field name="manual_start_date"/>
<field name="origin_partner_id"/>
<field name="manual_origin_partner_id"/>
<field name="destination_partner_id"/>
<field name="manual_destination_partner_id"/>
<field name="total_weight"/>
<field name="manual_total_weight"/>
<field name="amount_untaxed"/>
<field name="manual_amount_untaxed"/>
</group>
</sheet>
</form>
</field>
</record>

<record id="action_joint_buying_transport_request" model="ir.actions.act_window">
<field name="name">Transport Requests</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">joint.buying.transport.request</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>

<menuitem id="menu_joint_buying_transport_request"
name="Transport Requests"
parent="joint_buying_product.menu_transport"
action="action_joint_buying_transport_request"
sequence="1"
groups="joint_buying_base.group_joint_buying_user"
/>

</odoo>

0 comments on commit aff9e34

Please sign in to comment.