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 _
26 class crm_make_sale(osv.osv_memory):
27 """ Make sale order for crm """
29 _name = "crm.make.sale"
30 _description = "Make sales"
32 def _selectPartner(self, cr, uid, context=None):
34 This function gets default value for partner_id field.
35 @param self: The object pointer
36 @param cr: the current row, from the database cursor,
37 @param uid: the current user’s ID for security checks,
38 @param context: A standard dictionary for contextual values
39 @return: default value of partner_id field.
44 lead_obj = self.pool.get('crm.lead')
45 active_id = context and context.get('active_id', False) or False
49 lead = lead_obj.read(cr, uid, active_id, ['partner_id'])
50 return lead['partner_id']
52 def view_init(self, cr, uid, fields_list, context=None):
53 return super(crm_make_sale, self).view_init(cr, uid, fields_list, context=context)
55 def makeOrder(self, cr, uid, ids, context=None):
57 This function create Quotation on given case.
58 @param self: The object pointer
59 @param cr: the current row, from the database cursor,
60 @param uid: the current user’s ID for security checks,
61 @param ids: List of crm make sales' ids
62 @param context: A standard dictionary for contextual values
63 @return: Dictionary value of created sales order.
68 case_obj = self.pool.get('crm.lead')
69 sale_obj = self.pool.get('sale.order')
70 partner_obj = self.pool.get('res.partner')
71 data = context and context.get('active_ids', []) or []
73 for make in self.browse(cr, uid, ids, context=context):
74 partner = make.partner_id
75 partner_addr = partner_obj.address_get(cr, uid, [partner.id],
76 ['default', 'invoice', 'delivery', 'contact'])
77 pricelist = partner.property_product_pricelist.id
78 fpos = partner.property_account_position and partner.property_account_position.id or False
80 for case in case_obj.browse(cr, uid, data, context=context):
81 if not partner and case.partner_id:
82 partner = case.partner_id
83 fpos = partner.property_account_position and partner.property_account_position.id or False
84 partner_addr = partner_obj.address_get(cr, uid, [partner.id],
85 ['default', 'invoice', 'delivery', 'contact'])
86 pricelist = partner.property_product_pricelist.id
87 if False in partner_addr.values():
88 raise osv.except_osv(_('Insufficient Data!'), _('No addresse(s) defined for this customer.'))
91 'origin': _('Opportunity: %s') % str(case.id),
92 'section_id': case.section_id and case.section_id.id or False,
93 'categ_ids': [(6, 0, [categ_id.id for categ_id in case.categ_ids])],
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_shipping_id': partner_addr['delivery'],
99 'date_order': fields.date.context_today(self,cr,uid,context=context),
100 'fiscal_position': fpos,
103 vals['user_id'] = partner.user_id and partner.user_id.id or uid
104 new_id = sale_obj.create(cr, uid, vals, context=context)
105 sale_order = sale_obj.browse(cr, uid, new_id, context=context)
106 case_obj.write(cr, uid, [case.id], {'ref': 'sale.order,%s' % new_id})
107 new_ids.append(new_id)
108 message = _("Opportunity has been <b>converted</b> to the quotation <em>%s</em>.") % (sale_order.name)
109 case.message_append_note(body=message)
111 case_obj.case_close(cr, uid, data)
113 return {'type': 'ir.actions.act_window_close'}
116 'domain': str([('id', 'in', new_ids)]),
119 'res_model': 'sale.order',
121 'type': 'ir.actions.act_window',
122 'name' : _('Quotation'),
123 'res_id': new_ids and new_ids[0]
127 'domain': str([('id', 'in', new_ids)]),
129 'view_mode': 'tree,form',
130 'res_model': 'sale.order',
132 'type': 'ir.actions.act_window',
133 'name' : _('Quotation'),
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('Mark Won', help='Check this to close the opportunity after having created the sale order.'),
149 'shop_id': _get_shop_id,
151 'partner_id': _selectPartner,
156 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: