1
# -*- coding: utf-8 -*-
2
##############################################################################
4
# OpenERP, Open Source Management Solution
5
# Copyright (C) 2011 MSF, TeMPO Consulting
7
# This program is free software: you can redistribute it and/or modify
8
# it under the terms of the GNU Affero General Public License as
9
# published by the Free Software Foundation, either version 3 of the
10
# License, or (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU Affero General Public License for more details.
17
# You should have received a copy of the GNU Affero General Public License
18
# along with this program. If not, see <http://www.gnu.org/licenses/>.
20
##############################################################################
23
from osv import fields
25
from tools.translate import _
28
('make_to_stock', 'from stock'),
29
('make_to_order', 'on order'),]
32
('po', 'Purchase Order'),
33
('dpo', 'Direct Purchase Order'),
37
class multiple_sourcing_wizard(osv.osv_memory):
38
_name = 'multiple.sourcing.wizard'
41
'line_ids': fields.many2many('sourcing.line', 'source_sourcing_line_rel', 'line_id', 'wizard_id',
42
string='Sourcing lines'),
43
'type': fields.selection(_SELECTION_TYPE, string='Procurement Method', required=True),
44
'po_cft': fields.selection(_SELECTION_PO_CFT, string='PO/CFT'),
45
'supplier': fields.many2one('res.partner', 'Supplier', help='If you have choose lines coming from Field Orders, only External/ESC suppliers will be available.'),
46
'company_id': fields.many2one('res.company', 'Current company'),
47
'error_on_lines': fields.boolean('If there is line without need sourcing on selected lines'),
50
def default_get(self, cr, uid, fields, context=None):
52
Set lines with the selected lines to source
57
if not context.get('active_ids') or len(context.get('active_ids')) < 2:
58
raise osv.except_osv(_('Error'), _('You should select at least two lines to process.'))
60
res = super(multiple_sourcing_wizard, self).default_get(cr, uid, fields, context=context)
63
res['error_on_lines'] = False
64
res['type'] = 'make_to_order'
67
# Ignore all lines which have already been sourced, if there are some alredy sourced lines, a message
68
# will be displayed at the top of the wizard
69
for line in self.pool.get('sourcing.line').browse(cr, uid, context.get('active_ids'), context=context):
70
if line.state == 'draft' and line.sale_order_state == 'validated':
71
res['line_ids'].append(line.id)
73
res['error_on_lines'] = True
75
if not res['line_ids']:
76
raise osv.except_osv(_('Error'), _('No non-sourced lines are selected. Please select non-sourced lines'))
78
res['company_id'] = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id
82
def save_lines(self, cr, uid, ids, context=None):
84
Set values to sourcing lines
89
line_obj = self.pool.get('sourcing.line')
91
for wiz in self.browse(cr, uid, ids, context=context):
92
if wiz.type == 'make_to_order':
94
raise osv.except_osv(_('Error'), _('The Procurement method should be filled !'))
95
elif wiz.po_cft != 'cft' and not wiz.supplier:
96
raise osv.except_osv(_('Error'), _('You should select a supplier !'))
98
for line in wiz.line_ids:
99
if line.sale_order_id.procurement_request and wiz.po_cft == 'dpo':
100
raise osv.except_osv(_('Error'), _('You cannot choose Direct Purchase Order as method to source Internal Request lines.'))
102
line_obj.write(cr, uid, [line.id], {'type': wiz.type,
103
'po_cft': wiz.po_cft,
104
'supplier': wiz.supplier and wiz.supplier.id or False}, context=context)
106
return {'type': 'ir.actions.act_window_close'}
108
def source_lines(self, cr, uid, ids, context=None):
115
line_obj = self.pool.get('sourcing.line')
117
for wiz in self.browse(cr, uid, ids, context=context):
118
for line in wiz.line_ids:
119
if line.sale_order_id.procurement_request and wiz.po_cft == 'dpo':
120
raise osv.except_osv(_('Error'), _('You cannot choose Direct Purchase Order as method to source Internal Request lines.'))
122
line_obj.confirmLine(cr, uid, [line.id], context=context)
124
return {'type': 'ir.actions.act_window_close'}
126
def save_source_lines(self, cr, uid, ids, context=None):
128
Set values to sourcing lines and confirm them
133
self.save_lines(cr, uid, ids, context=context)
134
self.source_lines(cr, uid, ids, context=context)
136
return {'type': 'ir.actions.act_window_close'}
139
def change_type(self, cr, uid, ids, type, context=None):
141
Unset the other fields if the type is 'from stock'
143
if type == 'make_to_stock':
144
return {'value': {'po_cft': False, 'supplier': False}}
148
def change_po_cft(self, cr, uid, ids, po_cft, context=None):
150
Unset the supplier if tender is choosen
153
return {'value': {'supplier': False}}
157
multiple_sourcing_wizard()
160
#############################################################
162
# This modification on res.partner avoid the selection #
163
# of internal/inter-section/intermission partners #
164
# if a line on multiple sourcing wizard coming from a FO #
166
#############################################################
167
class res_partner(osv.osv):
168
_name = 'res.partner'
169
_inherit = 'res.partner'
171
def _get_dummy(self, cr, uid, ids, field_name, args, context=None):
178
def _src_contains_fo(self, cr, uid, obj, name, args, context=None):
181
if arg[0] == 'line_contains_fo':
182
if type(arg[2]) == type(list()):
183
for line in self.pool.get('sourcing.line').browse(cr, uid, arg[2][0][2], context=context):
184
if not line.procurement_request:
185
res.append(('partner_type', 'in', ['external', 'esc']))
190
'line_contains_fo': fields.function(_get_dummy, fnct_search=_src_contains_fo, method=True, string='Lines contains FO', type='boolean', store=False),
196
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: