23
23
from osv import osv
24
24
from osv import fields
25
from msf_partner import PARTNER_TYPE
29
27
class res_partner(osv.osv):
30
28
_name = 'res.partner'
31
29
_inherit = 'res.partner'
33
def search_in_product(self, cr, uid, obj, name, args, context=None):
35
Search function of related field 'in_product'
41
if not context.get('product_id', False) or 'choose_supplier' not in context:
44
supinfo_obj = self.pool.get('product.supplierinfo')
45
sup_obj = self.pool.get('res.partner')
48
info_ids = supinfo_obj.search(cr, uid, [('product_product_ids', '=', context.get('product_id'))])
49
info = supinfo_obj.read(cr, uid, info_ids, ['name'])
51
sup_in = [x['name'] for x in info]
58
res = sup_obj.search(cr, uid, [('id', 'not in', sup_in)])
61
return [('id', '=', 0)]
62
return [('id', 'in', [x[0] for x in res])]
65
def _set_in_product(self, cr, uid, ids, field_name, arg, context=None):
67
Returns according to the context if the partner is in product form
73
product_obj = self.pool.get('product.product')
75
# If we aren't in the context of choose supplier on procurement list
76
if not context.get('product_id', False) or 'choose_supplier' not in context:
78
res[i] = {'in_product': False, 'min_qty': 'N/A', 'delay': 'N/A'}
80
product = product_obj.browse(cr, uid, context.get('product_id'))
83
# Get all suppliers defined on product form
84
for s in product.seller_ids:
85
seller_ids.append(s.name.id)
86
seller_info.update({s.name.id: {'min_qty': s.min_qty, 'delay': s.delay}})
87
# Check if the partner is in product form
90
res[i] = {'in_product': True, 'min_qty': '%s' %seller_info[i]['min_qty'], 'delay': '%s' %seller_info[i]['delay']}
92
res[i] = {'in_product': False, 'min_qty': 'N/A', 'delay': 'N/A'}
96
def _get_price_info(self, cr, uid, ids, fiedl_name, args, context=None):
98
Returns information from product supplierinfo if product_id is in context
103
partner_price = self.pool.get('pricelist.partnerinfo')
107
res[id] = {'price_currency': False,
109
'valide_until_date': False}
111
if context.get('product_id'):
112
for partner in self.browse(cr, uid, ids, context=context):
113
product = self.pool.get('product.product').browse(cr, uid, context.get('product_id'), context=context)
114
uom = context.get('uom', product.uom_id.id)
115
pricelist = partner.property_product_pricelist_purchase
116
context.update({'uom': uom})
117
price_list = self.pool.get('product.product')._get_partner_info_price(cr, uid, product, partner.id, context.get('product_qty', 1.00), pricelist.currency_id.id, time.strftime('%Y-%m-%d'), uom, context=context)
119
func_currency_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
120
price = self.pool.get('res.currency').compute(cr, uid, func_currency_id, pricelist.currency_id.id, product.standard_price, round=True, context=context)
121
res[partner.id] = {'price_currency': pricelist.currency_id.id,
123
'valide_until_date': False}
125
info_price = partner_price.browse(cr, uid, price_list[0], context=context)
126
partner_currency_id = pricelist.currency_id.id
127
price = self.pool.get('res.currency').compute(cr, uid, info_price.currency_id.id, partner_currency_id, info_price.price)
128
currency = partner_currency_id
129
# Uncomment the following 2 lines if you want the price in currency of the pricelist.partnerinfo instead of partner default currency
130
# currency = info_price.currency_id.id
131
# price = info_price.price
132
res[partner.id] = {'price_currency': currency,
134
'valide_until_date': info_price.valid_till}
138
## QT : Remove _get_price_unit
140
## QT : Remove _get_valide_until_date
143
32
'manufacturer': fields.boolean(string='Manufacturer', help='Check this box if the partner is a manufacturer'),
144
'partner_type': fields.selection(PARTNER_TYPE, string='Partner type', required=True),
145
'in_product': fields.function(_set_in_product, fnct_search=search_in_product, string='In product', type="boolean", readonly=True, method=True, multi='in_product'),
146
'min_qty': fields.function(_set_in_product, string='Min. Qty', type='char', readonly=True, method=True, multi='in_product'),
147
'delay': fields.function(_set_in_product, string='Delivery Lead time', type='char', readonly=True, method=True, multi='in_product'),
148
'property_product_pricelist_purchase': fields.property(
151
relation='product.pricelist',
152
domain=[('type','=','purchase')],
153
string="Purchase default currency",
157
help="This currency will be used, instead of the default one, for purchases from the current partner"),
158
'property_product_pricelist': fields.property(
161
relation='product.pricelist',
162
domain=[('type','=','sale')],
163
string="Field orders default currency",
167
help="This currency will be used, instead of the default one, for field orders to the current partner"),
168
'price_unit': fields.function(_get_price_info, method=True, type='float', string='Unit price', multi='info'),
169
'valide_until_date' : fields.function(_get_price_info, method=True, type='char', string='Valid until date', multi='info'),
170
'price_currency': fields.function(_get_price_info, method=True, type='many2one', relation='res.currency', string='Currency', multi='info'),
33
'partner_type': fields.selection([('internal', 'Internal'), ('section', 'Inter-section'),
34
('external', 'External')], string='Partner type', required=True),
175
39
'partner_type': lambda *a: 'external',
178
def create(self, cr, uid, vals, context=None):
179
if 'partner_type' in vals and vals['partner_type'] in ('internal', 'section', 'esc', 'intermission'):
180
msf_customer = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'stock_location_internal_customers')
181
msf_supplier = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'stock_location_internal_suppliers')
182
if msf_customer and not 'property_stock_customer' in vals:
183
vals['property_stock_customer'] = msf_customer[1]
184
if msf_supplier and not 'property_stock_supplier' in vals:
185
vals['property_stock_supplier'] = msf_supplier[1]
187
return super(res_partner, self).create(cr, uid, vals, context=context)
189
def on_change_partner_type(self, cr, uid, ids, partner_type, sale_pricelist, purchase_pricelist):
191
Change the procurement method according to the partner type
193
price_obj = self.pool.get('product.pricelist')
194
cur_obj = self.pool.get('res.currency')
195
user_obj = self.pool.get('res.users')
197
r = {'po_by_project': 'project'}
199
if not partner_type or partner_type in ('external', 'internal'):
200
r.update({'po_by_project': 'all'})
202
sale_authorized_price = price_obj.search(cr, uid, [('type', '=', 'sale'), ('in_search', '=', partner_type)])
203
if sale_authorized_price and sale_pricelist not in sale_authorized_price:
204
r.update({'property_product_pricelist': sale_authorized_price[0]})
206
purchase_authorized_price = price_obj.search(cr, uid, [('type', '=', 'purchase'), ('in_search', '=', partner_type)])
207
if purchase_authorized_price and purchase_pricelist not in purchase_authorized_price:
208
r.update({'property_product_pricelist_purchase': purchase_authorized_price[0]})
210
if partner_type and partner_type in ('internal', 'section', 'esc', 'intermission'):
211
msf_customer = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'stock_location_internal_customers')
213
r.update({'property_stock_customer': msf_customer[1]})
214
msf_supplier = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'stock_location_internal_suppliers')
216
r.update({'property_stock_supplier': msf_supplier[1]})
218
other_customer = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'stock_location_customers')
220
r.update({'property_stock_customer': other_customer[1]})
221
other_supplier = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'stock', 'stock_location_suppliers')
223
r.update({'property_stock_supplier': other_supplier[1]})
227
def search(self, cr, uid, args=None, offset=0, limit=None, order=None, context=None, count=False):
229
Sort suppliers to have all suppliers in product form at the top of the list
231
supinfo_obj = self.pool.get('product.supplierinfo')
238
tmp_res = super(res_partner, self).search(cr, uid, args, offset, limit, order, context=context, count=count)
239
if not context.get('product_id', False) or 'choose_supplier' not in context or count:
242
# Get all supplier in product form
243
args.append(('in_product', '=', True))
244
res_in_prod = super(res_partner, self).search(cr, uid, args, offset, limit, order, context=context, count=count)
247
# Sort suppliers by sequence in product form
248
if 'product_id' in context:
249
supinfo_ids = supinfo_obj.search(cr, uid, [('name', 'in', res_in_prod), ('product_product_ids', '=', context.get('product_id'))], order='sequence')
251
for result in supinfo_obj.read(cr, uid, supinfo_ids, ['name']):
253
tmp_res.remove(result['name'][0])
254
new_res.append(result['name'][0])
258
#return new_res # comment this line to have all suppliers (with suppliers in product form at the top of the list)
260
new_res.extend(tmp_res)
266
44
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: