File: location.py

package info (click to toggle)
tryton-modules-stock-product-location 5.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 352 kB
  • sloc: python: 233; xml: 50; makefile: 6; sh: 3
file content (66 lines) | stat: -rw-r--r-- 2,426 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# This file is part of Tryton.  The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from trytond.model import (ModelView, ModelSQL, MatchMixin, fields,
    sequence_ordered)
from trytond.pyson import If, Eval, Bool
from trytond.pool import PoolMeta

__all__ = ['ProductLocation', 'Move', 'ShipmentIn', 'ShipmentOutReturn']


class ProductLocation(sequence_ordered(), ModelSQL, ModelView, MatchMixin):
    '''
    Product Location
    It defines the default storage location by warehouse for a product.
    '''
    __name__ = 'stock.product.location'
    product = fields.Many2One('product.product', 'Product', required=True,
        select=True, ondelete='CASCADE')
    warehouse = fields.Many2One('stock.location', 'Warehouse', required=True,
        domain=[('type', '=', 'warehouse')], ondelete='CASCADE')
    location = fields.Many2One('stock.location', 'Storage Location',
        required=True, ondelete='CASCADE',
        domain=[
            ('type', '=', 'storage'),
            ('parent', 'child_of', If(Bool(Eval('warehouse')),
                    [Eval('warehouse')], [])),
            ], depends=['warehouse'])


class Move(metaclass=PoolMeta):
    __name__ = 'stock.move'

    def set_product_location(self, field='to_location', **pattern):
        assert field in {'from_location', 'to_location'}
        if getattr(self, 'shipment', None):
            pattern.setdefault('warehouse', self.shipment.warehouse.id)

        for product_location in self.product.locations:
            if product_location.match(pattern):
                setattr(self, field, product_location.location)
                break


class ShipmentIn(metaclass=PoolMeta):
    __name__ = 'stock.shipment.in'

    @classmethod
    def _get_inventory_moves(cls, incoming_move):
        move = super(ShipmentIn, cls)._get_inventory_moves(incoming_move)
        if move:
            move.set_product_location(
                warehouse=incoming_move.shipment.warehouse.id)
        return move


class ShipmentOutReturn(metaclass=PoolMeta):
    __name__ = 'stock.shipment.out.return'

    @classmethod
    def _get_inventory_moves(cls, incoming_move):
        move = super(ShipmentOutReturn, cls)._get_inventory_moves(
            incoming_move)
        if move:
            move.set_product_location(
                warehouse=incoming_move.shipment.warehouse.id)
        return move