[MERGE] Merge from trunk-wms-loconopreport-jco
[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 class stock_picking(osv.osv):
174     _inherit = 'stock.picking'
175
176     def onchange_partner_in(self, cr, uid, ids, partner_id=None, context=None):
177         if not partner_id:
178             return {}
179         partner = self.pool.get('res.partner').browse(cr, uid, partner_id, context=context)
180         warning = {}
181         title = False
182         message = False
183         if partner.picking_warn != 'no-message':
184             if partner.picking_warn == 'block':
185                 raise osv.except_osv(_('Alert for %s!') % (partner.name), partner.picking_warn_msg)
186             title = _("Warning for %s") % partner.name
187             message = partner.picking_warn_msg
188             warning = {
189                 'title': title,
190                 'message': message
191             }
192         result =  super(stock_picking_in, self).onchange_partner_in(cr, uid, ids, partner_id, context)
193         if result.get('warning',False):
194             warning['title'] = title and title +' & '+ result['warning']['title'] or result['warning']['title']
195             warning['message'] = message and message + ' ' + result['warning']['message'] or result['warning']['message']
196
197         return {'value': result.get('value',{}), 'warning':warning}
198
199 class product_product(osv.osv):
200     _inherit = 'product.product'
201     _columns = {
202          'sale_line_warn' : fields.selection(WARNING_MESSAGE,'Sales Order Line', help=WARNING_HELP, required=True),
203          'sale_line_warn_msg' : fields.text('Message for Sales Order Line'),
204          'purchase_line_warn' : fields.selection(WARNING_MESSAGE,'Purchase Order Line', help=WARNING_HELP, required=True),
205          'purchase_line_warn_msg' : fields.text('Message for Purchase Order Line'),
206      }
207
208     _defaults = {
209          'sale_line_warn' : 'no-message',
210          'purchase_line_warn' : 'no-message',
211     }
212
213
214 class sale_order_line(osv.osv):
215     _inherit = 'sale.order.line'
216     def product_id_change_with_wh(self, cr, uid, ids, pricelist, product, qty=0,
217             uom=False, qty_uos=0, uos=False, name='', partner_id=False,
218             lang=False, update_tax=True, date_order=False, packaging=False,
219             fiscal_position=False, flag=False, warehouse_id=False, context=None):
220         warning = {}
221         if not product:
222             return {'value': {'th_weight' : 0, 'product_packaging': False,
223                 'product_uos_qty': qty}, 'domain': {'product_uom': [],
224                    'product_uos': []}}
225         product_obj = self.pool.get('product.product')
226         product_info = product_obj.browse(cr, uid, product)
227         title = False
228         message = False
229
230         if product_info.sale_line_warn != 'no-message':
231             if product_info.sale_line_warn == 'block':
232                 raise osv.except_osv(_('Alert for %s!') % (product_info.name), product_info.sale_line_warn_msg)
233             title = _("Warning for %s") % product_info.name
234             message = product_info.sale_line_warn_msg
235             warning['title'] = title
236             warning['message'] = message
237
238         result =  super(sale_order_line, self).product_id_change_with_wh( cr, uid, ids, pricelist, product, qty,
239             uom, qty_uos, uos, name, partner_id,
240             lang, update_tax, date_order, packaging, fiscal_position, flag, warehouse_id=warehouse_id, context=context)
241
242         if result.get('warning',False):
243             warning['title'] = title and title +' & '+result['warning']['title'] or result['warning']['title']
244             warning['message'] = message and message +'\n\n'+result['warning']['message'] or result['warning']['message']
245
246         return {'value': result.get('value',{}), 'warning':warning}
247
248
249 class purchase_order_line(osv.osv):
250     _inherit = 'purchase.order.line'
251     def onchange_product_id(self,cr, uid, ids, pricelist, product, qty, uom,
252             partner_id, date_order=False, fiscal_position_id=False, date_planned=False,
253             name=False, price_unit=False, state='draft', notes=False, context=None):
254         warning = {}
255         if not product:
256             return {'value': {'price_unit': price_unit or 0.0, 'name': name or '', 'notes': notes or '', 'product_uom' : uom or False}, 'domain':{'product_uom':[]}}
257         product_obj = self.pool.get('product.product')
258         product_info = product_obj.browse(cr, uid, product)
259         title = False
260         message = False
261
262         if product_info.purchase_line_warn != 'no-message':
263             if product_info.purchase_line_warn == 'block':
264                 raise osv.except_osv(_('Alert for %s!') % (product_info.name), product_info.purchase_line_warn_msg)
265             title = _("Warning for %s") % product_info.name
266             message = product_info.purchase_line_warn_msg
267             warning['title'] = title
268             warning['message'] = message
269
270         result =  super(purchase_order_line, self).onchange_product_id(cr, uid, ids, pricelist, product, qty, uom,
271             partner_id, date_order=date_order, fiscal_position_id=fiscal_position_id, date_planned=date_planned, name=name, price_unit=price_unit, state=state, context=context)
272
273         if result.get('warning',False):
274             warning['title'] = title and title +' & '+result['warning']['title'] or result['warning']['title']
275             warning['message'] = message and message +'\n\n'+result['warning']['message'] or result['warning']['message']
276
277         return {'value': result.get('value',{}), 'warning':warning}
278
279
280
281 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: