Launchpad automatic translations update.
[odoo/odoo.git] / addons / sale_crm / wizard / crm_make_sale.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 osv import fields, osv
23 from tools.translate import _
24 import time
25
26
27 class crm_make_sale(osv.osv_memory):
28     """ Make sale  order for crm """
29
30     _name = "crm.make.sale"
31     _description = "Make sales"
32
33     def _selectPartner(self, cr, uid, context=None):
34         """
35         This function gets default value for partner_id field.
36         @param self: The object pointer
37         @param cr: the current row, from the database cursor,
38         @param uid: the current user’s ID for security checks,
39         @param context: A standard dictionary for contextual values
40         @return: default value of partner_id field.
41         """
42         if context is None:
43             context = {}
44
45         lead_obj = self.pool.get('crm.lead')
46         active_id = context and context.get('active_id', False) or False
47         if not active_id:
48             return False
49
50         lead = lead_obj.read(cr, uid, active_id, ['partner_id'])
51         return lead['partner_id']
52
53     def view_init(self, cr, uid, fields_list, context=None):
54         return super(crm_make_sale, self).view_init(cr, uid, fields_list, context=context)
55
56     def makeOrder(self, cr, uid, ids, context=None):
57         """
58         This function  create Quotation on given case.
59         @param self: The object pointer
60         @param cr: the current row, from the database cursor,
61         @param uid: the current user’s ID for security checks,
62         @param ids: List of crm make sales' ids
63         @param context: A standard dictionary for contextual values
64         @return: Dictionary value of created sales order.
65         """
66         if context is None:
67             context = {}
68
69         case_obj = self.pool.get('crm.lead')
70         sale_obj = self.pool.get('sale.order')
71         partner_obj = self.pool.get('res.partner')
72         data = context and context.get('active_ids', []) or []
73
74         for make in self.browse(cr, uid, ids, context=context):
75             partner = make.partner_id
76             partner_addr = partner_obj.address_get(cr, uid, [partner.id],
77                     ['default', 'invoice', 'delivery', 'contact'])
78             pricelist = partner.property_product_pricelist.id
79             fpos = partner.property_account_position and partner.property_account_position.id or False
80             new_ids = []
81             for case in case_obj.browse(cr, uid, data, context=context):
82                 if not partner and case.partner_id:
83                     partner = case.partner_id
84                     fpos = partner.property_account_position and partner.property_account_position.id or False
85                     partner_addr = partner_obj.address_get(cr, uid, [partner.id],
86                             ['default', 'invoice', 'delivery', 'contact'])
87                     pricelist = partner.property_product_pricelist.id
88                 if False in partner_addr.values():
89                     raise osv.except_osv(_('Data Insufficient!'), _('Customer has no addresses defined!'))
90
91                 vals = {
92                     'origin': _('Opportunity: %s') % str(case.id),
93                     'section_id': case.section_id and case.section_id.id or False,
94                     'shop_id': make.shop_id.id,
95                     'partner_id': partner.id,
96                     'pricelist_id': pricelist,
97                     'partner_invoice_id': partner_addr['invoice'],
98                     'partner_order_id': partner_addr['contact'],
99                     'partner_shipping_id': partner_addr['delivery'],
100                     'date_order': time.strftime('%Y-%m-%d'),
101                     'fiscal_position': fpos,
102                 }
103                 if partner.id:
104                     vals['user_id'] = partner.user_id and partner.user_id.id or uid
105                 new_id = sale_obj.create(cr, uid, vals)
106                 case_obj.write(cr, uid, [case.id], {'ref': 'sale.order,%s' % new_id})
107                 new_ids.append(new_id)
108                 message = _("Opportunity  '%s' is converted to Quotation.") % (case.name)
109                 self.log(cr, uid, case.id, message)
110                 case_obj._history(cr, uid, [case], _("Converted to Sales Quotation(id: %s).") % (new_id))
111
112             if make.close:
113                 case_obj.case_close(cr, uid, data)
114             if not new_ids:
115                 return {'type': 'ir.actions.act_window_close'}
116             if len(new_ids)<=1:
117                 value = {
118                     'domain': str([('id', 'in', new_ids)]),
119                     'view_type': 'form',
120                     'view_mode': 'form',
121                     'res_model': 'sale.order',
122                     'view_id': False,
123                     'type': 'ir.actions.act_window',
124                     'res_id': new_ids and new_ids[0]
125                 }
126             else:
127                 value = {
128                     'domain': str([('id', 'in', new_ids)]),
129                     'view_type': 'form',
130                     'view_mode': 'tree,form',
131                     'res_model': 'sale.order',
132                     'view_id': False,
133                     'type': 'ir.actions.act_window',
134                     'res_id': new_ids
135                 }
136             return value
137
138     def _get_shop_id(self, cr, uid, ids, context=None):
139         cmpny_id = self.pool.get('res.users')._get_company(cr, uid, context=context)
140         shop = self.pool.get('sale.shop').search(cr, uid, [('company_id', '=', cmpny_id)])
141         return shop and shop[0] or False
142
143     _columns = {
144         'shop_id': fields.many2one('sale.shop', 'Shop', required=True),
145         'partner_id': fields.many2one('res.partner', 'Customer', required=True, domain=[('customer','=',True)]),
146         'close': fields.boolean('Close Opportunity', help='Check this to close the opportunity after having created the sale order.'),
147     }
148     _defaults = {
149          'shop_id': _get_shop_id,
150          'close': True,
151          'partner_id': _selectPartner,
152     }
153
154 crm_make_sale()