[MERGE] addons 16 survey
[odoo/odoo.git] / addons / warning / warning.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 import time
23 from osv import fields,osv
24 from tools.translate import _
25
26 WARNING_MESSAGE = [
27                    ('no-message','No Message'),
28                    ('warning','Warning'),
29                    ('block','Blocking Message')
30                    ]
31
32 WARNING_HELP = _('Selecting the "Warning" option will notify user with the message, Selecting "Blocking Message" will throw an exception with the message and block the flow. The Message has to be written in the next field.')
33
34 class res_partner(osv.osv):
35     _inherit = 'res.partner'
36     _columns = {
37         'sale_warn' : fields.selection(WARNING_MESSAGE, 'Sale Order', help=WARNING_HELP, required=True),
38         'sale_warn_msg' : fields.text('Message for Sale Order'),
39         'purchase_warn' : fields.selection(WARNING_MESSAGE, 'Purchase Order', help=WARNING_HELP, required=True),
40         'purchase_warn_msg' : fields.text('Message for Purchase Order'),
41         'picking_warn' : fields.selection(WARNING_MESSAGE, 'Stock Picking', help=WARNING_HELP, required=True),
42         'picking_warn_msg' : fields.text('Message for Stock Picking'),
43         'invoice_warn' : fields.selection(WARNING_MESSAGE, 'Invoice', help=WARNING_HELP, required=True),
44         'invoice_warn_msg' : fields.text('Message for Invoice'),
45     }
46     _defaults = {
47          'sale_warn' : 'no-message',
48          'purchase_warn' : 'no-message',
49          'picking_warn' : 'no-message',
50          'invoice_warn' : 'no-message',
51     }
52
53 res_partner()
54
55
56 class sale_order(osv.osv):
57     _inherit = 'sale.order'
58     def onchange_partner_id(self, cr, uid, ids, part):
59         if not part:
60             return {'value':{'partner_invoice_id': False, 'partner_shipping_id':False, 'payment_term' : False}}
61         warning = {}
62         title = False
63         message = False
64         partner = self.pool.get('res.partner').browse(cr, uid, part)
65         if partner.sale_warn != 'no-message':
66             if partner.sale_warn == 'block':
67                 raise osv.except_osv(_('Alert for %s !') % (partner.name), partner.sale_warn_msg)
68             title =  _("Warning for %s") % partner.name
69             message = partner.sale_warn_msg
70             warning = {
71                     'title': title,
72                     'message': message,
73             }
74
75         result =  super(sale_order, self).onchange_partner_id(cr, uid, ids, part)
76
77         if result.get('warning',False):
78             warning['title'] = title and title +' & '+ result['warning']['title'] or result['warning']['title']
79             warning['message'] = message and message + ' ' + result['warning']['message'] or result['warning']['message']
80
81         return {'value': result.get('value',{}), 'warning':warning}
82 sale_order()
83
84
85 class purchase_order(osv.osv):
86     _inherit = 'purchase.order'
87     def onchange_partner_id(self, cr, uid, ids, part):
88         if not part:
89             return {'value':{'partner_address_id': False}}
90         warning = {}
91         title = False
92         message = False
93         partner = self.pool.get('res.partner').browse(cr, uid, part)
94         if partner.purchase_warn != 'no-message':
95             if partner.purchase_warn == 'block':
96                 raise osv.except_osv(_('Alert for %s !') % (partner.name), partner.purchase_warn_msg)
97             title = _("Warning for %s") % partner.name
98             message = partner.purchase_warn_msg
99             warning = {
100                 'title': title,
101                 'message': message
102                 }
103         result =  super(purchase_order, self).onchange_partner_id(cr, uid, ids, part)
104
105         if result.get('warning',False):
106             warning['title'] = title and title +' & '+ result['warning']['title'] or result['warning']['title']
107             warning['message'] = message and message + ' ' + result['warning']['message'] or result['warning']['message']
108
109         return {'value': result.get('value',{}), 'warning':warning}
110
111 purchase_order()
112
113
114 class account_invoice(osv.osv):
115     _inherit = 'account.invoice'
116     def onchange_partner_id(self, cr, uid, ids, type, partner_id,
117             date_invoice=False, payment_term=False, partner_bank_id=False, company_id=False):
118         if not partner_id:
119             return {'value': {
120             'account_id': False,
121             'payment_term': False,
122             }
123         }
124         warning = {}
125         title = False
126         message = False
127         partner = self.pool.get('res.partner').browse(cr, uid, partner_id)
128         if partner.invoice_warn != 'no-message':
129             if partner.invoice_warn == 'block':
130                 raise osv.except_osv(_('Alert for %s !') % (partner.name), partner.invoice_warn_msg)
131
132             title = _("Warning for %s") % partner.name
133             message = partner.invoice_warn_msg
134             warning = {
135                 'title': title,
136                 'message': message
137                 }
138         result =  super(account_invoice, self).onchange_partner_id(cr, uid, ids, type, partner_id,
139             date_invoice=False, payment_term=False, partner_bank_id=False)
140
141         if result.get('warning',False):
142             warning['title'] = title and title +' & '+ result['warning']['title'] or result['warning']['title']
143             warning['message'] = message and message + ' ' + result['warning']['message'] or result['warning']['message']
144
145         return {'value': result.get('value',{}), 'warning':warning}
146
147 account_invoice()
148
149 class stock_picking(osv.osv):
150     _inherit = 'stock.picking'
151
152     def onchange_partner_in(self, cr, uid, context, partner_id=None):
153         if not partner_id:
154             return {}
155         partner = self.pool.get('res.partner').browse(cr, uid, partner_id, context=context)
156         warning = {}
157         title = False
158         message = False
159         if partner.picking_warn != 'no-message':
160             if partner.picking_warn == 'block':
161                 raise osv.except_osv(_('Alert for %s !') % (partner.name), partner.picking_warn_msg)
162             title = _("Warning for %s") % partner.name
163             message = partner.picking_warn_msg
164             warning = {
165                 'title': title,
166                 'message': message
167             }
168         result =  super(stock_picking, self).onchange_partner_in(cr, uid, context, partner_id)
169         if result.get('warning',False):
170             warning['title'] = title and title +' & '+ result['warning']['title'] or result['warning']['title']
171             warning['message'] = message and message + ' ' + result['warning']['message'] or result['warning']['message']
172
173         return {'value': result.get('value',{}), 'warning':warning}
174
175 stock_picking()
176
177 # FIXME:(class stock_picking_in and stock_picking_out) this is a temporary workaround because of a framework bug (ref: lp:996816). 
178 # It should be removed as soon as the bug is fixed
179 class stock_picking_in(osv.osv):
180     _inherit = 'stock.picking.in'
181
182     def onchange_partner_in(self, cr, uid, context, partner_id=None):
183         if not partner_id:
184             return {}
185         partner = self.pool.get('res.partner').browse(cr, uid, partner_id, context=context)
186         warning = {}
187         title = False
188         message = False
189         if partner.picking_warn != 'no-message':
190             if partner.picking_warn == 'block':
191                 raise osv.except_osv(_('Alert for %s !') % (partner.name), partner.picking_warn_msg)
192             title = _("Warning for %s") % partner.name
193             message = partner.picking_warn_msg
194             warning = {
195                 'title': title,
196                 'message': message
197             }
198         result =  super(stock_picking_in, self).onchange_partner_in(cr, uid, context, partner_id)
199         if result.get('warning',False):
200             warning['title'] = title and title +' & '+ result['warning']['title'] or result['warning']['title']
201             warning['message'] = message and message + ' ' + result['warning']['message'] or result['warning']['message']
202
203         return {'value': result.get('value',{}), 'warning':warning}
204
205 class stock_picking_out(osv.osv):
206     _inherit = 'stock.picking.out'
207
208     def onchange_partner_in(self, cr, uid, context, partner_id=None):
209         if not partner_id:
210             return {}
211         partner = self.pool.get('res.partner').browse(cr, uid, partner_id, context=context)
212         warning = {}
213         title = False
214         message = False
215         if partner.picking_warn != 'no-message':
216             if partner.picking_warn == 'block':
217                 raise osv.except_osv(_('Alert for %s !') % (partner.name), partner.picking_warn_msg)
218             title = _("Warning for %s") % partner.name
219             message = partner.picking_warn_msg
220             warning = {
221                 'title': title,
222                 'message': message
223             }
224         result =  super(stock_picking_out, self).onchange_partner_in(cr, uid, context, partner_id)
225         if result.get('warning',False):
226             warning['title'] = title and title +' & '+ result['warning']['title'] or result['warning']['title']
227             warning['message'] = message and message + ' ' + result['warning']['message'] or result['warning']['message']
228
229         return {'value': result.get('value',{}), 'warning':warning}
230
231 class product_product(osv.osv):
232     _inherit = 'product.product'
233     _columns = {
234          'sale_line_warn' : fields.selection(WARNING_MESSAGE,'Sale Order Line', help=WARNING_HELP, required=True),
235          'sale_line_warn_msg' : fields.text('Message for Sale Order Line'),
236          'purchase_line_warn' : fields.selection(WARNING_MESSAGE,'Purchase Order Line', help=WARNING_HELP, required=True),
237          'purchase_line_warn_msg' : fields.text('Message for Purchase Order Line'),
238      }
239
240     _defaults = {
241          'sale_line_warn' : 'no-message',
242          'purchase_line_warn' : 'no-message',
243     }
244
245 product_product()
246
247 class sale_order_line(osv.osv):
248     _inherit = 'sale.order.line'
249     def product_id_change(self, cr, uid, ids, pricelist, product, qty=0,
250             uom=False, qty_uos=0, uos=False, name='', partner_id=False,
251             lang=False, update_tax=True, date_order=False, packaging=False,
252             fiscal_position=False, flag=False, context=None):
253         warning = {}
254         if not product:
255             return {'value': {'th_weight' : 0, 'product_packaging': False,
256                 'product_uos_qty': qty}, 'domain': {'product_uom': [],
257                    'product_uos': []}}
258         product_obj = self.pool.get('product.product')
259         product_info = product_obj.browse(cr, uid, product)
260         title = False
261         message = False
262
263         if product_info.sale_line_warn != 'no-message':
264             if product_info.sale_line_warn == 'block':
265                 raise osv.except_osv(_('Alert for %s !') % (product_info.name), product_info.sale_line_warn_msg)
266             title = _("Warning for %s") % product_info.name
267             message = product_info.sale_line_warn_msg
268             warning['title'] = title
269             warning['message'] = message
270
271         result =  super(sale_order_line, self).product_id_change( cr, uid, ids, pricelist, product, qty,
272             uom, qty_uos, uos, name, partner_id,
273             lang, update_tax, date_order, packaging, fiscal_position, flag, context=context)
274
275         if result.get('warning',False):
276             warning['title'] = title and title +' & '+result['warning']['title'] or result['warning']['title']
277             warning['message'] = message and message +'\n\n'+result['warning']['message'] or result['warning']['message']
278
279         return {'value': result.get('value',{}), 'warning':warning}
280
281 sale_order_line()
282
283 class purchase_order_line(osv.osv):
284     _inherit = 'purchase.order.line'
285     def onchange_product_id(self,cr, uid, ids, pricelist, product, qty, uom,
286             partner_id, date_order=False, fiscal_position_id=False, date_planned=False,
287             name=False, price_unit=False, notes=False, context=None):
288         warning = {}
289         if not product:
290             return {'value': {'price_unit': 0.0, 'name':'','notes':'', 'product_uom' : False}, 'domain':{'product_uom':[]}}
291         product_obj = self.pool.get('product.product')
292         product_info = product_obj.browse(cr, uid, product)
293         title = False
294         message = False
295
296         if product_info.purchase_line_warn != 'no-message':
297             if product_info.purchase_line_warn == 'block':
298                 raise osv.except_osv(_('Alert for %s !') % (product_info.name), product_info.purchase_line_warn_msg)
299             title = _("Warning for %s") % product_info.name
300             message = product_info.purchase_line_warn_msg
301             warning['title'] = title
302             warning['message'] = message
303
304         result =  super(purchase_order_line, self).onchange_product_id(cr, uid, ids, pricelist, product, qty, uom,
305             partner_id, date_order, fiscal_position_id)
306
307         if result.get('warning',False):
308             warning['title'] = title and title +' & '+result['warning']['title'] or result['warning']['title']
309             warning['message'] = message and message +'\n\n'+result['warning']['message'] or result['warning']['message']
310
311         return {'value': result.get('value',{}), 'warning':warning}
312
313 purchase_order_line()
314
315
316 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: