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