1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from osv import fields, osv
23 from tools.translate import _
27 class crm_make_sale(osv.osv_memory):
28 """ Make sale order for crm """
30 _name = "crm.make.sale"
31 _description = "Make sales"
33 def _selectPartner(self, cr, uid, context=None):
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.
45 lead_obj = self.pool.get('crm.lead')
46 active_id = context and context.get('active_id', False) or False
50 lead = lead_obj.read(cr, uid, active_id, ['partner_id'])
51 return lead['partner_id']
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)
56 def makeOrder(self, cr, uid, ids, context=None):
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.
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 []
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
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!'))
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,
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))
113 case_obj.case_close(cr, uid, data)
115 return {'type': 'ir.actions.act_window_close'}
118 'domain': str([('id', 'in', new_ids)]),
121 'res_model': 'sale.order',
123 'type': 'ir.actions.act_window',
124 'res_id': new_ids and new_ids[0]
128 'domain': str([('id', 'in', new_ids)]),
130 'view_mode': 'tree,form',
131 'res_model': 'sale.order',
133 'type': 'ir.actions.act_window',
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
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.'),
149 'shop_id': _get_shop_id,
151 'partner_id': _selectPartner,