[IMP] sale_crm:

* Convert make_sale wizard into osv_memory

bzr revid: hmo@tinyerp.com-20100626054920-b5zarru7w875h6tv
This commit is contained in:
Mod2 Team(OpenERP) 2010-06-26 11:19:20 +05:30 committed by Harry (OpenERP)
parent 48ea0fe62f
commit 5ee9f602a6
7 changed files with 229 additions and 196 deletions

View File

@ -38,7 +38,7 @@ crm modules.
'website': 'http://www.openerp.com',
'depends': ['sale', 'crm'],
'init_xml': [],
'update_xml': ['sale_crm_wizard.xml',
'update_xml': ['wizard/crm_make_sale_view.xml',
'sale_crm_view.xml',
'board_sale_crm_view.xml',
'process/sale_crm_process.xml',

View File

@ -9,7 +9,7 @@
<field name="inherit_id" ref="crm.crm_case_form_view_oppor"/>
<field name="arch" type="xml">
<field name="priority" position="after">
<button string="Convert to Sale" icon="gtk-go-forward" name="%(sale_crm_wizard)d" type="action"/>
<button string="Convert to Sale" icon="gtk-go-forward" name="%(action_crm_make_sale)d" type="action"/>
</field>
</field>
</record>

View File

@ -1,14 +0,0 @@
<?xml version="1.0" ?>
<openerp>
<data>
<wizard
string="Make Quotation"
model="crm.opportunity"
name="crm.opportunity.make_order"
keyword="client_action_multi"
multi="True"
id="sale_crm_wizard"/>
</data>
</openerp>

View File

@ -19,7 +19,7 @@
#
##############################################################################
import makesale
import crm_make_sale
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -0,0 +1,183 @@
# -*- 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/>.
#
##############################################################################
from osv import fields, osv
from tools.translate import _
from mx.DateTime import now
class crm_make_sale(osv.osv_memory):
""" Make sale order for crm """
_name = "crm.make.sale"
_description = "Make sale"
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 users ID for security checks,
@param context: A standard dictionary for contextual values
@return : default value of partner_id field.
"""
if not context:
context = {}
active_id = context and context.get('active_id', False) or False
if not active_id:
return False
lead_obj = self.pool.get('crm.lead')
lead = lead_obj.read(cr, uid, active_id, ['partner_id'])
return lead['partner_id']
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 users ID for security checks,
@param ids: List of crm make sale' ids
@param context: A standard dictionary for contextual values
@return : Dictionary value of created sale order.
"""
if not context:
context = {}
mod_obj = self.pool.get('ir.model.data')
result = mod_obj._get_id(cr, uid, 'sale', 'view_sales_order_filter')
id = mod_obj.read(cr, uid, result, ['res_id'])
case_obj = self.pool.get('crm.lead')
sale_obj = self.pool.get('sale.order')
partner_obj = self.pool.get('res.partner')
sale_line_obj = self.pool.get('sale.order.line')
data = context and context.get('active_ids', []) or []
for make in self.browse(cr, uid, ids):
default_partner_addr = partner_obj.address_get(cr, uid, [make.partner_id.id],
['invoice', 'delivery', 'contact'])
default_pricelist = partner_obj.browse(cr, uid, make.partner_id.id,
context).property_product_pricelist.id
fpos_data = partner_obj.browse(cr, uid, make.partner_id.id, context).property_account_position
new_ids = []
for case in case_obj.browse(cr, uid, data):
if case.partner_id and case.partner_id.id:
partner_id = case.partner_id.id
fpos = case.partner_id.property_account_position and case.partner_id.property_account_position.id or False
partner_addr = partner_obj.address_get(cr, uid, [case.partner_id.id],
['invoice', 'delivery', 'contact'])
pricelist = partner_obj.browse(cr, uid, case.partner_id.id,
context).property_product_pricelist.id
else:
partner_id = make.partner_id.id
fpos = fpos_data and fpos_data.id or False
partner_addr = default_partner_addr
pricelist = default_pricelist
if False in partner_addr.values():
raise osv.except_osv(_('Data Insufficient!'),_('Customer has no addresses defined!'))
vals = {
'origin': 'CRM-Opportunity:%s' % str(case.id),
'section_id': case.section_id and case.section_id.id or False,
'picking_policy': make.picking_policy,
'shop_id': make.shop_id.id,
'partner_id': partner_id,
'pricelist_id': pricelist,
'partner_invoice_id': partner_addr['invoice'],
'partner_order_id': partner_addr['contact'],
'partner_shipping_id': partner_addr['delivery'],
'order_policy': 'manual',
'date_order': now(),
'fiscal_position': fpos,
}
if partner_id:
partner = partner_obj.browse(cr, uid, partner_id, context=context)
vals['user_id'] = partner.user_id and partner.user_id.id or uid
if make.analytic_account.id:
vals['project_id'] = make.analytic_account.id
new_id = sale_obj.create(cr, uid, vals)
for product_id in make.product_ids:
value = sale_line_obj.product_id_change(cr, uid, [], pricelist,
product_id.id, qty=1, partner_id=partner_id, fiscal_position=fpos)['value']
value['product_id'] = product_id.id
value['order_id'] = new_id
value['tax_id'] = [(6,0,value['tax_id'])]
sale_line_obj.create(cr, uid, value)
case_obj.write(cr, uid, [case.id], {'ref': 'sale.order,%s' % new_id})
new_ids.append(new_id)
if make.close:
case_obj.case_close(cr, uid, data)
if not new_ids:
return {}
if len(new_ids)<=1:
value = {
'domain': str([('id', 'in', new_ids)]),
'view_type': 'form',
'view_mode': 'form',
'res_model': 'sale.order',
'view_id': False,
'type': 'ir.actions.act_window',
'res_id': new_ids and new_ids[0]
}
else:
value = {
'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',
'res_id':new_ids
}
return value
_columns = {
'shop_id': fields.many2one('sale.shop', 'Shop', required = True),
'partner_id': fields.many2one('res.partner', 'Customer', required = True, help = 'Use this partner if there is no partner on the Opportunity'),
'picking_policy': fields.selection([('direct','Partial Delivery'),
('one','Complete Delivery')], 'Picking Policy', required = True),
'product_ids': fields.many2many('product.product', 'product_sale_rel',\
'sale_id', 'product_id', 'Products'),
'analytic_account': fields.many2one('account.analytic.account', 'Analytic Account'),
'close': fields.boolean('Close Case', help = 'Check this to close the case after having created the sale order.'),
}
_defaults = {
'partner_id': _selectPartner,
'close': lambda *a: 1
}
crm_make_sale()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -0,0 +1,43 @@
<openerp>
<data>
<!-- crm make sale's view -->
<record id="view_crm_make_sale" model="ir.ui.view">
<field name="name">crm.make.sale.form</field>
<field name="model">crm.make.sale</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Create a Sale Order" >
<group colspan="4">
<field name="partner_id" required="1"/>
<field name="shop_id" required="1" widget="selection"/>
<field name="analytic_account"/>
<field name="picking_policy"/>
<field name="close" required="1"/>
</group>
<separator string="Products" colspan="4"/>
<field name="product_ids" colspan="4" nolabel="1"/>
<separator colspan="4"/>
<group col="4" colspan="4">
<label string="" colspan="2"/>
<button special="cancel" string="_Close" icon="gtk-cancel"/>
<button name="makeOrder" string="_Ok" type="object" icon='gtk-ok'/>
</group>
</form>
</field>
</record>
<!-- crm make sale's action -->
<record id="action_crm_make_sale" model="ir.actions.act_window">
<field name="name">Make Quotation</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">crm.make.sale</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="target">new</field>
</record>
</data>
</openerp>

View File

@ -1,179 +0,0 @@
# -*- 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/>.
#
##############################################################################
from mx.DateTime import now
import wizard
import netsvc
import ir
import pooler
from tools.translate import _
sale_form = """<?xml version="1.0"?>
<form string="Convert to Sale Order">
<field name="partner_id" required="True"/>
<field name="shop_id" required="True" widget="selection"/>
<field name="analytic_account"/>
<field name="picking_policy" required="True"/>
<field name="close"/>
<newline/>
<field name="products" colspan="4"/>
</form>"""
sale_fields = {
'shop_id': {'string': 'Shop', 'type': 'many2one', 'relation': 'sale.shop'},
'partner_id': {'string': 'Customer', 'type': 'many2one',
'relation': 'res.partner',
'help': 'Use this partner if there is no partner on the case'},
'picking_policy': {'string': 'Picking Policy', 'type': 'selection',
'selection': [('direct', 'Partial Delivery'), ('one', 'Complete Delivery')]},
'products': {'string': 'Products', 'type': 'many2many',
'relation': 'product.product'},
'analytic_account': {'string': 'Analytic Account', 'type': 'many2one',
'relation': 'account.analytic.account'},
'close': {'string': 'Close Case', 'type': 'boolean', 'default': lambda *a: 1,
'help': 'Check this to close the case after having created the sale order.'},
}
class make_sale(wizard.interface):
def _selectPartner(self, cr, uid, data, context):
case_obj = pooler.get_pool(cr.dbname).get('crm.lead')
case = case_obj.read(cr, uid, data['ids'], ['partner_id'])
return {'partner_id': case[0]['partner_id']}
def _makeOrder(self, cr, uid, data, context):
pool = pooler.get_pool(cr.dbname)
mod_obj = pool.get('ir.model.data')
result = mod_obj._get_id(cr, uid, 'sale', 'view_sales_order_filter')
id = mod_obj.read(cr, uid, result, ['res_id'])
case_obj = pool.get('crm.lead')
sale_obj = pool.get('sale.order')
partner_obj = pool.get('res.partner')
sale_line_obj = pool.get('sale.order.line')
default_partner_addr = partner_obj.address_get(cr, uid, [data['form']['partner_id']],
['invoice', 'delivery', 'contact'])
default_pricelist = partner_obj.browse(cr, uid, data['form']['partner_id'],
context).property_product_pricelist.id
fpos_data = partner_obj.browse(cr, uid, data['form']['partner_id'],context).property_account_position
new_ids = []
for case in case_obj.browse(cr, uid, data['ids']):
if case.partner_id and case.partner_id.id:
partner_id = case.partner_id.id
fpos = case.partner_id.property_account_position and case.partner_id.property_account_position.id or False
partner_addr = partner_obj.address_get(cr, uid, [case.partner_id.id],
['invoice', 'delivery', 'contact'])
pricelist = partner_obj.browse(cr, uid, case.partner_id.id,
context).property_product_pricelist.id
else:
partner_id = data['form']['partner_id']
fpos = fpos_data and fpos_data.id or False
partner_addr = default_partner_addr
pricelist = default_pricelist
if False in partner_addr.values():
raise wizard.except_wizard(_('Data Insufficient!'),_('Customer has no addresses defined!'))
vals = {
'origin': 'CRM-Opportunity:%s' % str(case.id),
'section_id': case.section_id and case.section_id.id or False,
'picking_policy': data['form']['picking_policy'],
'shop_id': data['form']['shop_id'],
'partner_id': partner_id,
'pricelist_id': pricelist,
'partner_invoice_id': partner_addr['invoice'],
'partner_order_id': partner_addr['contact'],
'partner_shipping_id': partner_addr['delivery'],
'order_policy': 'manual',
'date_order': now(),
'fiscal_position': fpos,
}
if partner_id:
partner = partner_obj.browse(cr, uid, partner_id, context=context)
vals['user_id'] = partner.user_id and partner.user_id.id or uid
if data['form']['analytic_account']:
vals['project_id'] = data['form']['analytic_account']
vals.update( sale_obj.onchange_partner_id(cr, uid, [], partner_id).get('value',{}) )
new_id = sale_obj.create(cr, uid, vals)
if data['form']['products']:
for product_id in data['form']['products'][0][2]:
value = {
'price_unit': 0.0,
'product_id': product_id,
'order_id': new_id,
}
value.update( sale_line_obj.product_id_change(cr, uid, [], pricelist,product_id, qty=1, partner_id=partner_id, fiscal_position=fpos)['value'] )
value['tax_id'] = [(6,0,value['tax_id'])]
sale_line_obj.create(cr, uid, value)
case_obj.write(cr, uid, [case.id], {'ref': 'sale.order,%s' % new_id})
new_ids.append(new_id)
if data['form']['close']:
case_obj.case_close(cr, uid, data['ids'])
if not new_ids:
return {}
if len(new_ids)<=1:
value = {
'domain': str([('id', 'in', new_ids)]),
'view_type': 'form',
'view_mode': 'form',
'res_model': 'sale.order',
'view_id': False,
'type': 'ir.actions.act_window',
'res_id': new_ids and new_ids[0]
}
else:
value = {
'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',
'res_id':new_ids
}
return value
states = {
'init': {
'actions': [_selectPartner],
'result': {'type': 'form', 'arch': sale_form, 'fields': sale_fields,
'state' : [('end', 'Cancel', 'gtk-cancel'),('order', 'Create', 'gtk-apply')]}
},
'order': {
'actions': [],
'result': {'type': 'action', 'action': _makeOrder, 'state': 'end'}
}
}
make_sale('crm.opportunity.make_order')
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: