Debian Codegraph @

addons/website_sale_collect/models/sale_order.py

raw symbols SCIP
1 # Part of Odoo. See LICENSE file for full copyright and licensing details.
2
3 import json
4
5 from odoo import models
6 from odoo.exceptions import ValidationError
7 from odoo.http import request
8
9
10 class SaleOrder(models.Model):
11 _inherit = 'sale.order'
12
13 def _compute_warehouse_id(self):
14 """ Override of `website_sale_stock` to avoid recomputations for in_store orders
15 when the warehouse was set by the pickup_location_data"""
16 in_store_orders_with_pickup_data = self.filtered(
17 lambda so: (
18 so.carrier_id.delivery_type == 'in_store' and so.pickup_location_data
19 )
20 )
21 super(SaleOrder, self - in_store_orders_with_pickup_data)._compute_warehouse_id()
22 for order in in_store_orders_with_pickup_data:
23 order.warehouse_id = order.pickup_location_data['id']
24
25 def _compute_fiscal_position_id(self):
26 """Override of `sale` to set the fiscal position matching the selected pickup location
27 for pickup in-store orders."""
28 in_store_orders = self.filtered(
29 lambda so: so.carrier_id.delivery_type == 'in_store' and so.pickup_location_data
30 )
31 AccountFiscalPosition = self.env['account.fiscal.position'].sudo()
32 for order in in_store_orders:
33 order.fiscal_position_id = AccountFiscalPosition._get_fiscal_position(
34 order.partner_id, delivery=order.warehouse_id.partner_id
35 )
36 super(SaleOrder, self - in_store_orders)._compute_fiscal_position_id()
37
38 def _set_delivery_method(self, delivery_method, rate=None):
39 """ Override of `website_sale` to recompute warehouse and fiscal position when a new
40 delivery method is not in-store anymore. """
41
42 self.ensure_one()
43 was_in_store_order = (
44 self.carrier_id.delivery_type == 'in_store'
45 and delivery_method.delivery_type != 'in_store'
46 )
47 super()._set_delivery_method(delivery_method, rate=rate)
48 if was_in_store_order:
49 self._compute_warehouse_id()
50 self._compute_fiscal_position_id()
51
52 def _set_pickup_location(self, pickup_location_data):
53 """ Override `website_sale` to set the pickup location for in-store delivery methods.
54 Set account fiscal position depending on selected pickup location to correctly calculate
55 taxes.
56 """
57 super()._set_pickup_location(pickup_location_data)
58 if self.carrier_id.delivery_type != 'in_store':
59 return
60
61 self.pickup_location_data = json.loads(pickup_location_data)
62 if self.pickup_location_data:
63 self.warehouse_id = self.pickup_location_data['id']
64 self._compute_fiscal_position_id()
65 else:
66 self._compute_warehouse_id()
67
68 def _get_pickup_locations(self, zip_code=None, country=None, **kwargs):
69 """ Override of `website_sale` to ensure that a country is provided when there is a zip
70 code.
71
72 If the country cannot be found (e.g., the GeoIP request fails), the zip code is cleared to
73 prevent the parent method's assertion to fail.
74 """
75 if zip_code and not country:
76 country_code = None
77 if self.pickup_location_data:
78 country_code = self.pickup_location_data['country_code']
79 elif request.geoip.country_code:
80 country_code = request.geoip.country_code
81 country = self.env['res.country'].search([('code', '=', country_code)], limit=1)
82 if not country:
83 zip_code = None # Reset the zip code to skip the `assert` in the `super` call.
84 return super()._get_pickup_locations(zip_code=zip_code, country=country, **kwargs)
85
86 def _get_shop_warehouse_id(self):
87 """Override of `website_sale_stock` to consider the chosen warehouse."""
88 self.ensure_one()
89 if self.carrier_id.delivery_type == 'in_store':
90 return self.warehouse_id.id
91 return super()._get_shop_warehouse_id()
92
93 def _check_cart_is_ready_to_be_paid(self):
94 """ Override of `website_sale` to check if all products are in stock in the selected
95 warehouse. """
96 if (
97 self._has_deliverable_products()
98 and self.carrier_id.delivery_type == 'in_store'
99 and not self._is_in_stock(self.warehouse_id.id)
100 ):
101 raise ValidationError(self.env._(
102 "Some products are not available in the selected store."
103 ))
104 return super()._check_cart_is_ready_to_be_paid()
105
106 # === TOOLING ===#
107
108 def _prepare_in_store_default_location_data(self):
109 """ Prepare the default pickup location values for each in-store delivery method available
110 for the order. """
111 default_pickup_locations = {}
112 for dm in self._get_delivery_methods():
113 if (
114 dm.delivery_type == 'in_store'
115 and dm.id != self.carrier_id.id
116 and len(dm.warehouse_ids) == 1
117 ):
118 pickup_location_data = dm.warehouse_ids[0]._prepare_pickup_location_data()
119 if pickup_location_data:
120 default_pickup_locations[dm.id] = {
121 'pickup_location_data': pickup_location_data,
122 'insufficient_stock_data': self._get_insufficient_stock_data(
123 pickup_location_data['id']
124 ),
125 }
126
127 return {'default_pickup_locations': default_pickup_locations}
128
129 def _is_in_stock(self, wh_id):
130 """ Check whether all storable products of the cart are in stock in the given warehouse.
131
132 :param int wh_id: The warehouse in which to check the stock, as a `stock.warehouse` id.
133 :return: Whether all storable products are in stock.
134 :rtype: bool
135 """
136 return not self._get_insufficient_stock_data(wh_id)
137
138 def _get_insufficient_stock_data(self, wh_id):
139 """Return the mapping of order lines with insufficient stock in the given warehouse to their
140 maximum available quantity in the line's UoM.
141 If there are multiple order lines for the same product, consider the sum of their
142 quantities.
143
144 :param int wh_id: The warehouse in which to check the stock, as a `stock.warehouse` id.
145 :return: The mapping of order lines to their maximum available quantity.
146 :rtype: dict
147 """
148 insufficient_stock_data = {}
149 for product, ols in self.order_line.grouped('product_id').items():
150 if not product.is_storable or product.allow_out_of_stock_order:
151 continue
152 free_qty = product.with_context(warehouse_id=wh_id).free_qty
153 for ol in ols:
154 free_qty_in_uom = max(int(product.uom_id._compute_quantity(
155 free_qty, ol.product_uom_id, rounding_method="DOWN"
156 )), 0) # Round down as only integer quantities can be sold.
157 line_qty_in_uom = ol.product_uom_qty
158 if line_qty_in_uom > free_qty_in_uom: # Not enough stock.
159 # Set a warning on the order line.
160 insufficient_stock_data[ol] = free_qty_in_uom
161 ol.shop_warning = self.env._(
162 "%(available_qty)s/%(line_qty)s available at this location",
163 available_qty=free_qty_in_uom, line_qty=int(line_qty_in_uom),
164 )
165 free_qty -= ol.product_uom_id._compute_quantity(line_qty_in_uom, product.uom_id)
166 return insufficient_stock_data
167
168 def _verify_updated_quantity(self, order_line, product_id, new_qty, uom_id, **kwargs):
169 """ Override of `website_sale_stock` to skip the verification when click and collect
170 is activated. The quantity is verified later. """
171 product = self.env['product.product'].browse(product_id)
172 if (
173 product.is_storable
174 and not product.allow_out_of_stock_order
175 and self.website_id.in_store_dm_id
176 ):
177 return new_qty, ''
178 return super()._verify_updated_quantity(order_line, product_id, new_qty, uom_id, **kwargs)