[Fix] stock_planing:remove for address field
[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)
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 class product_product(osv.osv):
178     _inherit = 'product.product'
179     _columns = {
180          'sale_line_warn' : fields.selection(WARNING_MESSAGE,'Sale Order Line', help=WARNING_HELP, required=True),
181          'sale_line_warn_msg' : fields.text('Message for Sale Order Line'),
182          'purchase_line_warn' : fields.selection(WARNING_MESSAGE,'Purchase Order Line', help=WARNING_HELP, required=True),
183          'purchase_line_warn_msg' : fields.text('Message for Purchase Order Line'),
184      }
185
186     _defaults = {
187          'sale_line_warn' : 'no-message',
188          'purchase_line_warn' : 'no-message',
189     }
190
191 product_product()
192
193 class sale_order_line(osv.osv):
194     _inherit = 'sale.order.line'
195     def product_id_change(self, cr, uid, ids, pricelist, product, qty=0,
196             uom=False, qty_uos=0, uos=False, name='', partner_id=False,
197             lang=False, update_tax=True, date_order=False, packaging=False,
198             fiscal_position=False, flag=False, context=None):
199         warning = {}
200         if not product:
201             return {'value': {'th_weight' : 0, 'product_packaging': False,
202                 'product_uos_qty': qty}, 'domain': {'product_uom': [],
203                    'product_uos': []}}
204         product_obj = self.pool.get('product.product')
205         product_info = product_obj.browse(cr, uid, product)
206         title = False
207         message = False
208
209         if product_info.sale_line_warn != 'no-message':
210             if product_info.sale_line_warn == 'block':
211                 raise osv.except_osv(_('Alert for %s !') % (product_info.name), product_info.sale_line_warn_msg)
212             title = _("Warning for %s") % product_info.name
213             message = product_info.sale_line_warn_msg
214             warning['title'] = title
215             warning['message'] = message
216
217         result =  super(sale_order_line, self).product_id_change( cr, uid, ids, pricelist, product, qty,
218             uom, qty_uos, uos, name, partner_id,
219             lang, update_tax, date_order, packaging, fiscal_position, flag, context=context)
220
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 +'\n\n'+result['warning']['message'] or result['warning']['message']
224
225         return {'value': result.get('value',{}), 'warning':warning}
226
227 sale_order_line()
228
229 class purchase_order_line(osv.osv):
230     _inherit = 'purchase.order.line'
231     def product_id_change(self,cr, uid, ids, pricelist, product, qty, uom,
232             partner_id, date_order=False, fiscal_position=False, date_planned=False,
233             name=False, price_unit=False, notes=False, context=None):
234         warning = {}
235         if not product:
236             return {'value': {'price_unit': 0.0, 'name':'','notes':'', 'product_uom' : False}, 'domain':{'product_uom':[]}}
237         product_obj = self.pool.get('product.product')
238         product_info = product_obj.browse(cr, uid, product)
239         title = False
240         message = False
241
242         if product_info.purchase_line_warn != 'no-message':
243             if product_info.purchase_line_warn == 'block':
244                 raise osv.except_osv(_('Alert for %s !') % (product_info.name), product_info.purchase_line_warn_msg)
245             title = _("Warning for %s") % product_info.name
246             message = product_info.purchase_line_warn_msg
247             warning['title'] = title
248             warning['message'] = message
249
250         result =  super(purchase_order_line, self).product_id_change(cr, uid, ids, pricelist, product, qty, uom,
251             partner_id, date_order, fiscal_position)
252
253         if result.get('warning',False):
254             warning['title'] = title and title +' & '+result['warning']['title'] or result['warning']['title']
255             warning['message'] = message and message +'\n\n'+result['warning']['message'] or result['warning']['message']
256
257         return {'value': result.get('value',{}), 'warning':warning}
258
259 purchase_order_line()
260
261
262 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: