-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproduct.py
95 lines (79 loc) · 3.33 KB
/
product.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# This file is part of the product_pack module for Tryton.
# The COPYRIGHT file at the top level of this repository contains the full
# copyright notices and license terms.
from sql import Null
from trytond.model import ModelView, ModelSQL, fields, Check
from trytond.pool import PoolMeta
from trytond.pyson import Eval
__all__ = ['ProductPack', 'Template']
class ProductPack(ModelSQL, ModelView):
'Product Pack'
__name__ = 'product.pack'
name = fields.Char('Name', required=True, translate=True)
product = fields.Many2One('product.template', 'Product',
ondelete='CASCADE', required=True)
sequence = fields.Integer('Sequence',
help='Gives the sequence order when displaying a list of packaging.')
qty = fields.Float('Quantity by Package',
digits=(16, Eval('uom_digits', 2)),
help='The total number of products you can put by packaging.')
weight = fields.Float('Empty Packaging Weight')
height = fields.Float('Height')
width = fields.Float('Width')
length = fields.Float('Length')
packages_layer = fields.Integer('Packagings by layer')
layers = fields.Integer('Number of Layers',
help='The number of layers in a pallet.')
pallet_weight = fields.Float('Pallet Weight')
total_packaging_weight = fields.Float('Total Packaging Weight',
help='The weight of packagings for a full pallet (included pallet '
'weight.')
note = fields.Text('Description')
uom = fields.Function(fields.Many2One('product.uom', 'Unit'),
'on_change_with_uom')
uom_digits = fields.Function(fields.Integer('Unit Digits'),
'on_change_with_uom_digits')
@classmethod
def __setup__(cls):
super(ProductPack, cls).__setup__()
cls._order = [('product', 'ASC'), ('sequence', 'ASC')]
t = cls.__table__()
cls._sql_constraints += [
('check_product_pack_qty_pos',
Check(t, (t.qty == Null) | (t.qty >= '0')),
'Quantity by Package of Package must be positive.'),
]
def get_rec_name(self, name=None):
rec_name = self.name
if self.qty:
rec_name = '%s (%s %s)' % (rec_name, self.qty, self.uom.rec_name)
return rec_name
@staticmethod
def order_sequence(tables):
table, _ = tables[None]
return [table.sequence == None, table.sequence]
@staticmethod
def default_sequence():
return 1
@fields.depends('product', '_parent_product.default_uom')
def on_change_with_uom(self, name=None):
if self.product and self.product.default_uom:
return self.product.default_uom.id
@fields.depends('uom')
def on_change_with_uom_digits(self, name=None):
if self.uom:
return self.uom.digits
return 2
@fields.depends('weight', 'layers', 'packages_layer',
'pallet_weight')
def on_change_with_total_packaging_weight(self, name=None):
if (not self.weight or not self.layers or not self.packages_layer
or not self.pallet_weight):
return
return (self.weight * self.layers * self.packages_layer
+ self.pallet_weight)
class Template(metaclass=PoolMeta):
__name__ = 'product.template'
packagings = fields.One2Many('product.pack', 'product', 'Packagings')
class Product(metaclass=PoolMeta):
__name__ = 'product.product'