2010-06-26 05:49:20 +00:00
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
##############################################################################
|
|
|
|
|
#
|
|
|
|
|
# OpenERP, Open Source Management Solution
|
|
|
|
|
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
|
|
|
|
|
#
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
|
# it under the terms of the GNU Affero General Public License as
|
|
|
|
|
# published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
# License, or (at your option) any later version.
|
|
|
|
|
#
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
|
#
|
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
#
|
|
|
|
|
##############################################################################
|
|
|
|
|
|
2012-12-06 14:56:32 +00:00
|
|
|
|
from openerp.osv import fields, osv
|
|
|
|
|
from openerp.tools.translate import _
|
2010-09-23 13:50:47 +00:00
|
|
|
|
|
2010-06-26 05:49:20 +00:00
|
|
|
|
|
|
|
|
|
class crm_make_sale(osv.osv_memory):
|
|
|
|
|
""" Make sale order for crm """
|
|
|
|
|
|
|
|
|
|
_name = "crm.make.sale"
|
2010-12-13 06:07:23 +00:00
|
|
|
|
_description = "Make sales"
|
2010-08-09 05:14:49 +00:00
|
|
|
|
|
2010-06-26 05:49:20 +00:00
|
|
|
|
def _selectPartner(self, cr, uid, context=None):
|
|
|
|
|
"""
|
|
|
|
|
This function gets default value for partner_id field.
|
|
|
|
|
@param self: The object pointer
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
|
|
|
|
@param context: A standard dictionary for contextual values
|
2010-10-15 12:11:22 +00:00
|
|
|
|
@return: default value of partner_id field.
|
2010-08-09 05:14:49 +00:00
|
|
|
|
"""
|
2010-11-23 07:05:05 +00:00
|
|
|
|
if context is None:
|
2010-06-26 05:49:20 +00:00
|
|
|
|
context = {}
|
2010-10-15 12:11:22 +00:00
|
|
|
|
|
2010-08-09 05:14:49 +00:00
|
|
|
|
lead_obj = self.pool.get('crm.lead')
|
2010-06-26 05:49:20 +00:00
|
|
|
|
active_id = context and context.get('active_id', False) or False
|
|
|
|
|
if not active_id:
|
|
|
|
|
return False
|
2010-10-15 12:11:22 +00:00
|
|
|
|
|
2010-06-26 05:49:20 +00:00
|
|
|
|
lead = lead_obj.read(cr, uid, active_id, ['partner_id'])
|
2010-08-09 05:14:49 +00:00
|
|
|
|
return lead['partner_id']
|
2010-08-13 12:20:05 +00:00
|
|
|
|
|
2010-08-11 12:05:54 +00:00
|
|
|
|
def view_init(self, cr, uid, fields_list, context=None):
|
|
|
|
|
return super(crm_make_sale, self).view_init(cr, uid, fields_list, context=context)
|
2010-08-13 12:20:05 +00:00
|
|
|
|
|
2010-06-26 05:49:20 +00:00
|
|
|
|
def makeOrder(self, cr, uid, ids, context=None):
|
|
|
|
|
"""
|
|
|
|
|
This function create Quotation on given case.
|
|
|
|
|
@param self: The object pointer
|
|
|
|
|
@param cr: the current row, from the database cursor,
|
|
|
|
|
@param uid: the current user’s ID for security checks,
|
2010-12-13 06:07:23 +00:00
|
|
|
|
@param ids: List of crm make sales' ids
|
2010-06-26 05:49:20 +00:00
|
|
|
|
@param context: A standard dictionary for contextual values
|
2010-12-13 06:07:23 +00:00
|
|
|
|
@return: Dictionary value of created sales order.
|
2010-06-26 05:49:20 +00:00
|
|
|
|
"""
|
2010-11-23 07:05:05 +00:00
|
|
|
|
if context is None:
|
2010-06-26 05:49:20 +00:00
|
|
|
|
context = {}
|
2012-06-25 06:51:09 +00:00
|
|
|
|
# update context: if come from phonecall, default state values can make the quote crash lp:1017353
|
|
|
|
|
context.pop('default_state', False)
|
|
|
|
|
|
2010-06-26 05:49:20 +00:00
|
|
|
|
case_obj = self.pool.get('crm.lead')
|
|
|
|
|
sale_obj = self.pool.get('sale.order')
|
|
|
|
|
partner_obj = self.pool.get('res.partner')
|
|
|
|
|
data = context and context.get('active_ids', []) or []
|
2010-08-09 05:14:49 +00:00
|
|
|
|
|
2010-09-23 13:50:47 +00:00
|
|
|
|
for make in self.browse(cr, uid, ids, context=context):
|
|
|
|
|
partner = make.partner_id
|
2010-12-13 06:07:23 +00:00
|
|
|
|
partner_addr = partner_obj.address_get(cr, uid, [partner.id],
|
2010-09-23 13:50:47 +00:00
|
|
|
|
['default', 'invoice', 'delivery', 'contact'])
|
|
|
|
|
pricelist = partner.property_product_pricelist.id
|
|
|
|
|
fpos = partner.property_account_position and partner.property_account_position.id or False
|
2010-06-26 05:49:20 +00:00
|
|
|
|
new_ids = []
|
2010-09-23 13:50:47 +00:00
|
|
|
|
for case in case_obj.browse(cr, uid, data, context=context):
|
|
|
|
|
if not partner and case.partner_id:
|
|
|
|
|
partner = case.partner_id
|
|
|
|
|
fpos = partner.property_account_position and partner.property_account_position.id or False
|
2010-12-13 06:07:23 +00:00
|
|
|
|
partner_addr = partner_obj.address_get(cr, uid, [partner.id],
|
2010-09-17 13:36:17 +00:00
|
|
|
|
['default', 'invoice', 'delivery', 'contact'])
|
2010-09-23 13:50:47 +00:00
|
|
|
|
pricelist = partner.property_product_pricelist.id
|
2010-06-26 05:49:20 +00:00
|
|
|
|
if False in partner_addr.values():
|
2012-08-07 10:23:26 +00:00
|
|
|
|
raise osv.except_osv(_('Insufficient Data!'), _('No addresse(s) defined for this customer.'))
|
2010-08-09 05:14:49 +00:00
|
|
|
|
|
2010-06-26 05:49:20 +00:00
|
|
|
|
vals = {
|
2010-12-13 06:07:23 +00:00
|
|
|
|
'origin': _('Opportunity: %s') % str(case.id),
|
|
|
|
|
'section_id': case.section_id and case.section_id.id or False,
|
2012-07-06 11:06:21 +00:00
|
|
|
|
'categ_ids': [(6, 0, [categ_id.id for categ_id in case.categ_ids])],
|
2010-12-13 06:07:23 +00:00
|
|
|
|
'shop_id': make.shop_id.id,
|
|
|
|
|
'partner_id': partner.id,
|
|
|
|
|
'pricelist_id': pricelist,
|
|
|
|
|
'partner_invoice_id': partner_addr['invoice'],
|
|
|
|
|
'partner_shipping_id': partner_addr['delivery'],
|
2012-02-14 12:25:20 +00:00
|
|
|
|
'date_order': fields.date.context_today(self,cr,uid,context=context),
|
2010-12-13 06:07:23 +00:00
|
|
|
|
'fiscal_position': fpos,
|
2010-06-26 05:49:20 +00:00
|
|
|
|
}
|
2010-09-23 13:50:47 +00:00
|
|
|
|
if partner.id:
|
2010-06-26 05:49:20 +00:00
|
|
|
|
vals['user_id'] = partner.user_id and partner.user_id.id or uid
|
2011-10-26 14:04:45 +00:00
|
|
|
|
new_id = sale_obj.create(cr, uid, vals, context=context)
|
|
|
|
|
sale_order = sale_obj.browse(cr, uid, new_id, context=context)
|
2010-09-23 13:50:47 +00:00
|
|
|
|
case_obj.write(cr, uid, [case.id], {'ref': 'sale.order,%s' % new_id})
|
2010-06-26 05:49:20 +00:00
|
|
|
|
new_ids.append(new_id)
|
2012-04-03 07:35:33 +00:00
|
|
|
|
message = _("Opportunity has been <b>converted</b> to the quotation <em>%s</em>.") % (sale_order.name)
|
2012-08-17 10:03:02 +00:00
|
|
|
|
case.message_post(body=message)
|
2010-06-26 05:49:20 +00:00
|
|
|
|
if make.close:
|
|
|
|
|
case_obj.case_close(cr, uid, data)
|
|
|
|
|
if not new_ids:
|
2010-12-28 10:41:38 +00:00
|
|
|
|
return {'type': 'ir.actions.act_window_close'}
|
2010-06-26 05:49:20 +00:00
|
|
|
|
if len(new_ids)<=1:
|
|
|
|
|
value = {
|
2010-12-13 06:07:23 +00:00
|
|
|
|
'domain': str([('id', 'in', new_ids)]),
|
|
|
|
|
'view_type': 'form',
|
|
|
|
|
'view_mode': 'form',
|
|
|
|
|
'res_model': 'sale.order',
|
|
|
|
|
'view_id': False,
|
|
|
|
|
'type': 'ir.actions.act_window',
|
2012-06-13 09:06:57 +00:00
|
|
|
|
'name' : _('Quotation'),
|
2010-06-26 05:49:20 +00:00
|
|
|
|
'res_id': new_ids and new_ids[0]
|
|
|
|
|
}
|
|
|
|
|
else:
|
|
|
|
|
value = {
|
2010-12-13 06:07:23 +00:00
|
|
|
|
'domain': str([('id', 'in', new_ids)]),
|
|
|
|
|
'view_type': 'form',
|
|
|
|
|
'view_mode': 'tree,form',
|
|
|
|
|
'res_model': 'sale.order',
|
|
|
|
|
'view_id': False,
|
|
|
|
|
'type': 'ir.actions.act_window',
|
2012-06-13 09:06:57 +00:00
|
|
|
|
'name' : _('Quotation'),
|
2010-07-02 14:00:32 +00:00
|
|
|
|
'res_id': new_ids
|
2010-08-13 12:20:05 +00:00
|
|
|
|
}
|
2010-06-26 05:49:20 +00:00
|
|
|
|
return value
|
2010-08-13 12:20:05 +00:00
|
|
|
|
|
2010-08-10 11:13:07 +00:00
|
|
|
|
def _get_shop_id(self, cr, uid, ids, context=None):
|
|
|
|
|
cmpny_id = self.pool.get('res.users')._get_company(cr, uid, context=context)
|
|
|
|
|
shop = self.pool.get('sale.shop').search(cr, uid, [('company_id', '=', cmpny_id)])
|
|
|
|
|
return shop and shop[0] or False
|
2010-06-26 05:49:20 +00:00
|
|
|
|
|
|
|
|
|
_columns = {
|
2010-12-13 06:07:23 +00:00
|
|
|
|
'shop_id': fields.many2one('sale.shop', 'Shop', required=True),
|
2011-03-11 12:55:35 +00:00
|
|
|
|
'partner_id': fields.many2one('res.partner', 'Customer', required=True, domain=[('customer','=',True)]),
|
2012-12-21 16:48:08 +00:00
|
|
|
|
'close': fields.boolean('Mark Won', help='Check this to close the opportunity after having created the sales order.'),
|
2010-06-26 05:49:20 +00:00
|
|
|
|
}
|
|
|
|
|
_defaults = {
|
2012-06-13 09:06:57 +00:00
|
|
|
|
'shop_id': _get_shop_id,
|
|
|
|
|
'close': False,
|
|
|
|
|
'partner_id': _selectPartner,
|
2010-06-26 05:49:20 +00:00
|
|
|
|
}
|
2010-08-13 12:20:05 +00:00
|
|
|
|
|
2010-06-26 05:49:20 +00:00
|
|
|
|
crm_make_sale()
|
2011-11-22 08:51:38 +00:00
|
|
|
|
|
|
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|