Skip to content

Commit

Permalink
[IMP] Add the possibility to have more than 1 vehicle by PO
Browse files Browse the repository at this point in the history
    Initially, there was only one vehicle for one PO.
    Now, if the vehicle is set on the PO, all the PO lines are related to this vehicle
    But if the vehicle is not set on the PO, the user can decide to have different vehicles on every line.
    Use case is a grouped purchase of several items for several vehicles.
  • Loading branch information
lmarion-source committed Oct 16, 2024
1 parent e9bae48 commit ad097ce
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
1 change: 1 addition & 0 deletions fleet_vehicle_purchase/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from . import purchase_order
from . import fleet_vehicle
from . import purchase_order_line
12 changes: 10 additions & 2 deletions fleet_vehicle_purchase/models/fleet_vehicle.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@ class FleetVehicle(models.Model):

@api.depends("purchase_order_ids")
def _compute_purchase_order_count(self):
for record in self:
record.purchase_order_count = len(record.purchase_order_ids)
orders = self.env["purchase.order"].read_group(
[("fleet_vehicle_id", "in", self.ids)],
["fleet_vehicle_id"],
["fleet_vehicle_id"],
)
mapped_data = {
po["fleet_vehicle_id"][0]: po["fleet_vehicle_id_count"] for po in orders
}
for rec in self:
rec.purchase_order_count = mapped_data.get(rec.id, 0)

def action_view_purchase_orders(self):
self.ensure_one()
Expand Down
10 changes: 0 additions & 10 deletions fleet_vehicle_purchase/models/purchase_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,3 @@ class PurchaseOrder(models.Model):
_inherit = "purchase.order"

fleet_vehicle_id = fields.Many2one("fleet.vehicle", string="Vehicle")


class PurchaseOrderLine(models.Model):

_inherit = "purchase.order.line"

def _prepare_account_move_line(self, move=False):
result = super()._prepare_account_move_line(move)
result["vehicle_id"] = self.order_id.fleet_vehicle_id.id
return result
11 changes: 11 additions & 0 deletions fleet_vehicle_purchase/views/purchase_order.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@
<field name="partner_ref" position="after">
<field name="fleet_vehicle_id" />
</field>
<xpath
expr="//sheet//notebook//page[@name='products']//tree//field[@name='date_planned']"
position="after"
>
<field name="fleet_vehicle_from_po" invisible="1" />
<field
name="fleet_vehicle_id"
optional="hide"
attrs="{'readonly': [('fleet_vehicle_from_po', '=', True)]}"
/>
</xpath>
</field>
</record>

Expand Down

0 comments on commit ad097ce

Please sign in to comment.