Merge pull request #2027 from odoo-dev/8.0-wmsstaging7-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 from openerp.osv import fields,osv
23 from openerp.tools.translate import _
24
25 WARNING_MESSAGE = [
26                    ('no-message','No Message'),
27                    ('warning','Warning'),
28                    ('block','Blocking Message')
29                    ]
30
31 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.')
32
33 class res_partner(osv.osv):
34     _inherit = 'res.partner'
35     _columns = {
36         'sale_warn' : fields.selection(WARNING_MESSAGE, 'Sales Order', help=WARNING_HELP, required=True),
37         'sale_warn_msg' : fields.text('Message for Sales Order'),
38         'purchase_warn' : fields.selection(WARNING_MESSAGE, 'Purchase Order', help=WARNING_HELP, required=True),
39         'purchase_warn_msg' : fields.text('Message for Purchase Order'),
40         'picking_warn' : fields.selection(WARNING_MESSAGE, 'Stock Picking', help=WARNING_HELP, required=True),
41         'picking_warn_msg' : fields.text('Message for Stock Picking'),
42         'invoice_warn' : fields.selection(WARNING_MESSAGE, 'Invoice', help=WARNING_HELP, required=True),
43         'invoice_warn_msg' : fields.text('Message for Invoice'),
44     }
45     _defaults = {
46          'sale_warn' : 'no-message',
47          'purchase_warn' : 'no-message',
48          'picking_warn' : 'no-message',
49          'invoice_warn' : 'no-message',
50     }
51
52
53
54 class sale_order(osv.osv):
55     _inherit = 'sale.order'
56     def onchange_partner_id(self, cr, uid, ids, part, context=None):
57         if not part:
58             return {'value':{'partner_invoice_id': False, 'partner_shipping_id':False, 'payment_term' : False}}
59         warning = {}
60         title = False
61         message = False
62         partner = self.pool.get('res.partner').browse(cr, uid, part, context=context)
63         if partner.sale_warn != 'no-message':
64             title =  _("Warning for %s") % partner.name
65             message = partner.sale_warn_msg
66             warning = {
67                     'title': title,
68                     'message': message,
69             }
70             if partner.sale_warn == 'block':
71                 return {'value': {'partner_id': False}, 'warning': warning}
72
73         result =  super(sale_order, self).onchange_partner_id(cr, uid, ids, part, context=context)
74
75         if result.get('warning',False):
76             warning['title'] = title and title +' & '+ result['warning']['title'] or result['warning']['title']
77             warning['message'] = message and message + ' ' + result['warning']['message'] or result['warning']['message']
78
79         return {'value': result.get('value',{}), 'warning':warning}
80
81
82 class purchase_order(osv.osv):
83     _inherit = 'purchase.order'
84     def onchange_partner_id(self, cr, uid, ids, part, context=None):
85         if not part:
86             return {'value':{'partner_address_id': False}}
87         warning = {}
88         title = False
89         message = False
90         partner = self.pool.get('res.partner').browse(cr, uid, part, context=context)
91         if partner.purchase_warn != 'no-message':
92             title = _("Warning for %s") % partner.name
93             message = partner.purchase_warn_msg
94             warning = {
95                 'title': title,
96                 'message': message
97                 }
98             if partner.purchase_warn == 'block':
99                 return {'value': {'partner_id': False}, 'warning': warning}
100
101         result =  super(purchase_order, self).onchange_partner_id(cr, uid, ids, part, context=context)
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,
115                             partner_bank_id=False, company_id=False,
116                             context=None):
117         if not partner_id:
118             return {'value': {
119             'account_id': False,
120             'payment_term': False,
121             }
122         }
123         warning = {}
124         title = False
125         message = False
126         partner = self.pool.get('res.partner').browse(cr, uid, partner_id, context=context)
127         if partner.invoice_warn != 'no-message':
128             title = _("Warning for %s") % partner.name
129             message = partner.invoice_warn_msg
130             warning = {
131                 'title': title,
132                 'message': message
133                 }
134
135             if partner.invoice_warn == 'block':
136                 return {'value': {'partner_id': False}, 'warning': warning}
137
138         result =  super(account_invoice, self).onchange_partner_id(cr, uid, ids, type, partner_id,
139             date_invoice=date_invoice, payment_term=payment_term, 
140             partner_bank_id=partner_bank_id, company_id=company_id, context=context)
141
142         if result.get('warning',False):
143             warning['title'] = title and title +' & '+ result['warning']['title'] or result['warning']['title']
144             warning['message'] = message and message + ' ' + result['warning']['message'] or result['warning']['message']
145
146         return {'value': result.get('value',{}), 'warning':warning}
147
148
149 class stock_picking(osv.osv):
150     _inherit = 'stock.picking'
151
152     def onchange_partner_in(self, cr, uid, ids, partner_id=None, context=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             title = _("Warning for %s") % partner.name
161             message = partner.picking_warn_msg
162             warning = {
163                 'title': title,
164                 'message': message
165             }
166             if partner.picking_warn == 'block':
167                 return {'value': {'partner_id': False}, 'warning': warning}
168
169         result =  super(stock_picking_in, self).onchange_partner_in(cr, uid, ids, partner_id, context)
170         if result.get('warning',False):
171             warning['title'] = title and title +' & '+ result['warning']['title'] or result['warning']['title']
172             warning['message'] = message and message + ' ' + result['warning']['message'] or result['warning']['message']
173
174         return {'value': result.get('value',{}), 'warning':warning}
175
176
177 class product_product(osv.osv):
178     _inherit = 'product.template'
179     _columns = {
180          'sale_line_warn' : fields.selection(WARNING_MESSAGE,'Sales Order Line', help=WARNING_HELP, required=True),
181          'sale_line_warn_msg' : fields.text('Message for Sales 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
192 class sale_order_line(osv.osv):
193     _inherit = 'sale.order.line'
194     def product_id_change_with_wh(self, cr, uid, ids, pricelist, product, qty=0,
195             uom=False, qty_uos=0, uos=False, name='', partner_id=False,
196             lang=False, update_tax=True, date_order=False, packaging=False,
197             fiscal_position=False, flag=False, warehouse_id=False, context=None):
198         warning = {}
199         if not product:
200             return {'value': {'th_weight' : 0, 'product_packaging': False,
201                 'product_uos_qty': qty}, 'domain': {'product_uom': [],
202                    'product_uos': []}}
203         product_obj = self.pool.get('product.product')
204         product_info = product_obj.browse(cr, uid, product)
205         title = False
206         message = False
207
208         if product_info.sale_line_warn != 'no-message':
209             title = _("Warning for %s") % product_info.name
210             message = product_info.sale_line_warn_msg
211             warning['title'] = title
212             warning['message'] = message
213             if product_info.sale_line_warn == 'block':
214                 return {'value': {'product_id': False}, 'warning': warning}
215
216         result =  super(sale_order_line, self).product_id_change_with_wh( cr, uid, ids, pricelist, product, qty,
217             uom, qty_uos, uos, name, partner_id,
218             lang, update_tax, date_order, packaging, fiscal_position, flag, warehouse_id=warehouse_id, context=context)
219
220         if result.get('warning',False):
221             warning['title'] = title and title +' & '+result['warning']['title'] or result['warning']['title']
222             warning['message'] = message and message +'\n\n'+result['warning']['message'] or result['warning']['message']
223
224         return {'value': result.get('value',{}), 'warning':warning}
225
226
227 class purchase_order_line(osv.osv):
228     _inherit = 'purchase.order.line'
229     def onchange_product_id(self,cr, uid, ids, pricelist, product, qty, uom,
230             partner_id, date_order=False, fiscal_position_id=False, date_planned=False,
231             name=False, price_unit=False, state='draft', notes=False, context=None):
232         warning = {}
233         if not product:
234             return {'value': {'price_unit': price_unit or 0.0, 'name': name or '', 'notes': notes or '', 'product_uom' : uom or False}, 'domain':{'product_uom':[]}}
235         product_obj = self.pool.get('product.product')
236         product_info = product_obj.browse(cr, uid, product)
237         title = False
238         message = False
239
240         if product_info.purchase_line_warn != 'no-message':
241             title = _("Warning for %s") % product_info.name
242             message = product_info.purchase_line_warn_msg
243             warning['title'] = title
244             warning['message'] = message
245             if product_info.purchase_line_warn == 'block':
246                 return {'value': {'product_id': False}, 'warning': warning}
247
248         result =  super(purchase_order_line, self).onchange_product_id(cr, uid, ids, pricelist, product, qty, uom,
249             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)
250
251         if result.get('warning',False):
252             warning['title'] = title and title +' & '+result['warning']['title'] or result['warning']['title']
253             warning['message'] = message and message +'\n\n'+result['warning']['message'] or result['warning']['message']
254
255         return {'value': result.get('value',{}), 'warning':warning}
256
257
258
259 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: