1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# Copyright (C) 2011 MSF, TeMPO Consulting
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU Affero General Public License as
8
# published by the Free Software Foundation, either version 3 of the
9
# License, or (at your option) any later version.
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU Affero General Public License for more details.
16
# You should have received a copy of the GNU Affero General Public License
17
# along with this program. If not, see <http://www.gnu.org/licenses/>.
19
##############################################################################
21
from datetime import datetime, timedelta, date
22
from dateutil.relativedelta import relativedelta, relativedelta
23
from osv import osv, fields
24
from osv.orm import browse_record, browse_null
25
from tools.translate import _
27
import decimal_precision as dp
32
from msf_order_date import TRANSPORT_TYPE
33
from msf_order_date import ZONE_SELECTION
37
class product_template(osv.osv):
38
_inherit = 'product.template'
40
def _get_delay_for_supplier(self, cr, uid, ids, fields, arg, context=None):
44
if 'delay_supplier_id' not in context:
49
partner_obj = self.pool.get('res.partner')
50
for prod in self.browse(cr, uid, ids):
52
for seller in prod.seller_ids:
53
if context['delay_supplier_id'] == seller.name.id:
54
ret[prod.id] = seller.delay
57
ret[prod.id] = int(partner_obj.read(cr, uid, context['delay_supplier_id'], ['default_delay'])['default_delay'])
62
'delay_for_supplier': fields.function(_get_delay_for_supplier, type='integer', string='Default delay for a supplier', method=True)
68
class res_partner(osv.osv):
69
_description='Partner'
70
_inherit = "res.partner"
72
def _calc_dellay(self, cr, uid, ids, fields, arg, context=None):
74
for partner in self.browse(cr, uid, ids, context=context):
75
result[partner.id] = {}
77
result[partner.id].update({field:0})
79
# get the default transport, the smallest sequence
80
value_list = self.read(cr, uid, [partner.id], ['transport_0', 'transport_0_lt',], context=context)
84
# it's a list, get the first element
86
# preferred transport values
87
transport_0 = value['transport_0']
88
transport_0_lt = value['transport_0_lt']
89
# no favorite transport selected, return 0
92
# the default transport lead time
93
result[partner.id]['default_delay'] = transport_0_lt + partner.procurement_lt
97
def get_transport_lead_time(self, cr, uid, ids, transport_name, context=None):
99
for a given transport name (road, air, sea),
103
if ids and isinstance(ids, (int, long)):
107
# get corresponding lt
108
for partner in self.browse(cr, uid, ids, context=context):
109
result[partner.id] = 0
111
for i in range(NUMBER_OF_CHOICE):
112
if getattr(partner, 'transport_%s'%i) == transport_name:
113
val = getattr(partner, 'transport_%s_lt'%i)
114
result[partner.id] = val
118
def on_change_lead_time(self, cr, uid, ids, transport_0_lt, procurement_lt, context=None):
120
change supplier_lt and customer_lt according to preferred lead time and internal lead time
122
return {'value': {'supplier_lt': transport_0_lt + procurement_lt,
123
'customer_lt': transport_0_lt + procurement_lt}}
125
_columns = {'zone': fields.selection(selection=ZONE_SELECTION, string='Zone', required=True),
126
'customer_lt': fields.integer('Customer Lead Time'),
127
'supplier_lt': fields.integer('Supplier Lead Time'),
128
'procurement_lt': fields.integer('Internal Lead Time'),
129
'transport_0_lt': fields.integer('1st Transport Lead Time'),
130
'transport_0': fields.selection(selection=TRANSPORT_TYPE, string='1st (favorite) Mode of Transport',),
131
'transport_1_lt': fields.integer('2nd Transport Lead Time'),
132
'transport_1': fields.selection(selection=TRANSPORT_TYPE, string='2nd Mode of Transport'),
133
'transport_2_lt': fields.integer('3rd Transport Lead Time'),
134
'transport_2': fields.selection(selection=TRANSPORT_TYPE, string='3nd Mode of Transport'),
135
'default_delay': fields.function(_calc_dellay, method=True, type='integer', string='Supplier Lead Time (computed)', multi="seller_delay"),
136
'po_by_project': fields.selection([
137
('all', 'All requirements'),
138
('project', 'Requirements by Project'),
139
('category', 'Requirements by Category'),
140
('category_project', 'Requirements by Category and Project'),
141
('isolated', 'Requirements by Order')],
142
string='Order creation mode',
143
help='''When option “All requirements” is set for
144
a given supplier, the system will create a PO that merge all requirements
146
If option “Requirements by Project” is set, the POs will
147
be created by original requestor (customer of the SO origin), meaning
148
system creates one PO by project for this supplier.
149
If option "Requirements by Category" is set, the system will create a PO
150
that merge all requirements by category for this supplier.
151
If option "Requirements by Category and Project" is set, the system
152
will create a PO that merge only the requirements of one customer
154
If option "Requirements by Order" is set, the system will create a PO
155
that merge lines coming from the same FO/IR.'''),
158
_defaults = {'zone': 'national',
162
'transport_0': TRANSPORT_TYPE[0][0], # empty
164
'transport_1': TRANSPORT_TYPE[0][0], # empty
166
'transport_2': TRANSPORT_TYPE[0][0], # empty
168
'po_by_project': lambda *a: 'all',
174
class purchase_order_line(osv.osv):
176
this modify the onchange function for product, set the date_planned value
178
_inherit = 'purchase.order.line'
180
def product_id_change(self, cr, uid, ids, pricelist, product, qty, uom,
181
partner_id, date_order=False, fiscal_position=False, date_planned=False,
182
name=False, price_unit=False, notes=False):
184
res = super(purchase_order_line, self).product_id_change(cr, uid, ids, pricelist, product, qty, uom,
185
partner_id, date_order, fiscal_position, date_planned,
186
name, price_unit, notes)
190
product_obj = self.pool.get('product.product')
192
product = product_obj.browse(cr, uid, product)
194
lt_date = (datetime.now() + relativedelta(days=int(product.seller_delay))).strftime('%Y-%m-%d')
195
res.update(date_planned=lt_date)
199
purchase_order_line()
202
class product_supplierinfo(osv.osv):
203
_name = 'product.supplierinfo'
204
_inherit = 'product.supplierinfo'
206
def onchange_supplier_id(self, cr, uid, ids, name):
208
set the default delay value
212
partner_obj = self.pool.get('res.partner')
214
partner = partner_obj.browse(cr, uid, name)
216
return {'value': {'delay': partner.default_delay}}
218
return {'value': {'delay': False}}
220
product_supplierinfo()