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/>.
#
##############################################################################
2010-08-09 05:14:49 +00:00
import time
2010-06-26 05:49:20 +00:00
from osv import fields , osv
from tools . translate import _
2010-07-13 07:09:55 +00:00
import decimal_precision as dp
2010-06-26 05:49:20 +00:00
class crm_make_sale ( osv . osv_memory ) :
""" Make sale order for crm """
_name = " crm.make.sale "
_description = " Make sale "
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
@return : default value of partner_id field .
2010-08-09 05:14:49 +00:00
"""
2010-06-26 05:49:20 +00:00
if not context :
context = { }
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
lead = lead_obj . read ( cr , uid , active_id , [ ' partner_id ' ] )
2010-08-09 05:14:49 +00:00
return lead [ ' partner_id ' ]
2010-08-11 12:05:54 +00:00
def view_init ( self , cr , uid , fields_list , context = None ) :
if context is None :
context = { }
if context . get ( ' active_ids ' , False ) and context [ ' active_ids ' ] :
oppr = self . pool . get ( ' crm.lead ' ) . browse ( cr , uid , context [ ' active_ids ' ] )
for line in oppr :
if not line . section_id :
raise osv . except_osv ( _ ( ' Warning ! ' ) , _ ( ' Sales Team is not specified. ' ) )
return super ( crm_make_sale , self ) . view_init ( cr , uid , fields_list , context = context )
2010-08-09 05:14:49 +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 ,
@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 = { }
2010-08-09 05:14:49 +00:00
2010-06-26 05:49:20 +00:00
mod_obj = self . pool . get ( ' ir.model.data ' )
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 ' )
2010-08-09 05:14:49 +00:00
2010-07-02 14:00:32 +00:00
result = mod_obj . _get_id ( cr , uid , ' sale ' , ' view_sales_order_filter ' )
id = mod_obj . read ( cr , uid , result , [ ' res_id ' ] )
2010-08-09 05:14:49 +00:00
2010-06-26 05:49:20 +00:00
data = context and context . get ( ' active_ids ' , [ ] ) or [ ]
2010-08-09 05:14:49 +00:00
for make in self . browse ( cr , uid , ids ) :
2010-06-26 05:49:20 +00:00
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 = [ ]
2010-08-09 05:14:49 +00:00
2010-06-26 05:49:20 +00:00
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
2010-08-09 05:14:49 +00:00
2010-06-26 05:49:20 +00:00
if False in partner_addr . values ( ) :
raise osv . except_osv ( _ ( ' Data Insufficient! ' ) , _ ( ' Customer has no addresses defined! ' ) )
2010-08-09 05:14:49 +00:00
2010-06-26 05:49:20 +00:00
vals = {
2010-07-18 19:43:57 +00:00
' origin ' : ' Opportunity: %s ' % str ( case . id ) ,
2010-06-26 05:49:20 +00:00
' section_id ' : case . section_id and case . section_id . id or False ,
' 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 ' ] ,
2010-07-18 19:43:57 +00:00
' date_order ' : time . strftime ( ' % Y- % m- %d ' ) ,
2010-06-26 05:49:20 +00:00
' fiscal_position ' : fpos ,
}
2010-08-09 05:14:49 +00:00
2010-06-26 05:49:20 +00:00
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
2010-08-09 05:14:49 +00:00
2010-06-26 05:49:20 +00:00
if make . analytic_account . id :
vals [ ' project_id ' ] = make . analytic_account . id
new_id = sale_obj . create ( cr , uid , vals )
2010-07-13 07:09:55 +00:00
for line in make . sale_order_line :
2010-07-18 19:43:57 +00:00
value = { }
2010-06-26 05:49:20 +00:00
value [ ' order_id ' ] = new_id
2010-07-18 19:43:57 +00:00
value [ ' name ' ] = line . name
value [ ' delay ' ] = line . delay
value [ ' product_id ' ] = line . product_id and line . product_id . id or False
value [ ' price_unit ' ] = line . price_unit
value [ ' tax_id ' ] = line . tax_id and [ ( 6 , 0 , map ( lambda x : x . id , line . tax_id ) ) ] or False
value [ ' type ' ] = line . type
2010-07-13 07:09:55 +00:00
value [ ' product_uom_qty ' ] = line . product_uom_qty
value [ ' product_uom ' ] = line . product_uom . id
2010-07-18 19:43:57 +00:00
value [ ' product_uos_qty ' ] = line . product_uos_qty
value [ ' product_uos ' ] = line . product_uos and line . product_uos . id or False
value [ ' product_packaging ' ] = line . product_packaging and line . product_packaging . id or False
value [ ' discount ' ] = line . discount
value [ ' notes ' ] = line . notes
2010-06-26 05:49:20 +00:00
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 )
2010-07-08 10:14:24 +00:00
message = _ ( ' Opportunity ' ) + " ' " + case . name + " ' " + _ ( " is converted to Sales Quotation. " )
self . log ( cr , uid , case . id , message )
2010-08-09 05:14:49 +00:00
2010-06-26 05:49:20 +00:00
if make . close :
case_obj . case_close ( cr , uid , data )
2010-08-09 05:14:49 +00:00
2010-06-26 05:49:20 +00:00
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 ' ,
2010-07-02 14:00:32 +00:00
' res_id ' : new_ids
2010-06-26 05:49:20 +00:00
}
return value
2010-08-10 11:13:07 +00:00
def _get_shop_id ( self , cr , uid , ids , context = None ) :
if context is None :
context = { }
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-07-02 14:00:32 +00:00
' shop_id ' : fields . many2one ( ' sale.shop ' , ' Shop ' , required = True ) ,
2010-07-18 19:43:57 +00:00
' partner_id ' : fields . many2one ( ' res.partner ' , ' Customer ' , required = True ) ,
' sale_order_line ' : fields . one2many ( ' sale.order.make.line ' , ' opportunity_order_id ' , ' Product Line ' ) ,
2010-08-09 05:14:49 +00:00
' 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. ' ) ,
2010-06-26 05:49:20 +00:00
}
_defaults = {
2010-08-10 11:13:07 +00:00
' shop_id ' : _get_shop_id ,
2010-06-26 05:49:20 +00:00
' partner_id ' : _selectPartner ,
2010-07-02 14:00:32 +00:00
' close ' : 1
2010-06-26 05:49:20 +00:00
}
2010-08-10 11:13:07 +00:00
2010-06-26 05:49:20 +00:00
crm_make_sale ( )
2010-07-13 07:09:55 +00:00
class sale_order_make_line ( osv . osv_memory ) :
2010-08-09 05:14:49 +00:00
2010-07-18 19:43:57 +00:00
def product_id_change ( self , cr , uid , ids , product , qty = 0 ,
2010-07-13 07:09:55 +00:00
uom = False , qty_uos = 0 , uos = False , name = ' ' , partner_id = False ,
2010-07-18 19:43:57 +00:00
lang = False , update_tax = True , packaging = False , flag = False ) :
2010-07-13 07:09:55 +00:00
if not partner_id :
raise osv . except_osv ( _ ( ' No Customer Defined ! ' ) , _ ( ' You have to select a customer in the sale form ! \n Please set one customer before choosing a product. ' ) )
2010-08-09 05:14:49 +00:00
date_order = time . strftime ( ' % Y- % m- %d ' )
2010-07-18 19:43:57 +00:00
part = self . pool . get ( ' res.partner ' ) . browse ( cr , uid , partner_id )
pricelist = part . property_product_pricelist and part . property_product_pricelist . id or False
fiscal_position = part . property_account_position and part . property_account_position . id or False
return self . pool . get ( ' sale.order.line ' ) . product_id_change ( cr , uid , ids , pricelist , product , qty , uom , qty_uos , uos , name , partner_id , lang , update_tax , date_order , packaging , fiscal_position , flag )
2010-08-09 05:14:49 +00:00
2010-07-13 07:09:55 +00:00
_name = ' sale.order.make.line '
2010-07-18 19:43:57 +00:00
_description = ' Opportunity Sale Order Line '
2010-07-13 07:09:55 +00:00
_columns = {
2010-07-18 19:43:57 +00:00
' opportunity_order_id ' : fields . many2one ( ' crm.make.sale ' , ' Order Reference ' , required = True , ondelete = ' cascade ' , select = True , readonly = True , ) ,
' name ' : fields . char ( ' Description ' , size = 256 , required = True , select = True , ) ,
' delay ' : fields . float ( ' Delivery Lead Time ' , required = True , help = " Number of days between the order confirmation the the shipping of the products to the customer " ) ,
2010-07-13 07:09:55 +00:00
' product_id ' : fields . many2one ( ' product.product ' , ' Product ' , domain = [ ( ' sale_ok ' , ' = ' , True ) ] , change_default = True ) ,
2010-07-18 19:43:57 +00:00
' price_unit ' : fields . float ( ' Unit Price ' , required = True , digits_compute = dp . get_precision ( ' Sale Price ' ) ) ,
' tax_id ' : fields . many2many ( ' account.tax ' , ' sale_order_tax ' , ' order_line_id ' , ' tax_id ' , ' Taxes ' ) ,
' type ' : fields . selection ( [ ( ' make_to_stock ' , ' from stock ' ) , ( ' make_to_order ' , ' on order ' ) ] , ' Procurement Method ' , required = True ) ,
' product_uom_qty ' : fields . float ( ' Quantity (UoM) ' , digits = ( 16 , 2 ) , required = True , ) ,
' product_uom ' : fields . many2one ( ' product.uom ' , ' Unit of Measure ' , required = True , ) ,
' product_uos_qty ' : fields . float ( ' Quantity (UoS) ' ) ,
2010-07-13 07:09:55 +00:00
' product_uos ' : fields . many2one ( ' product.uom ' , ' Product UoS ' ) ,
' product_packaging ' : fields . many2one ( ' product.packaging ' , ' Packaging ' ) ,
2010-07-18 19:43:57 +00:00
' discount ' : fields . float ( ' Discount ( % ) ' , digits = ( 16 , 2 ) ) ,
2010-07-13 07:09:55 +00:00
' notes ' : fields . text ( ' Notes ' ) ,
}
_order = ' sequence, id '
_defaults = {
' discount ' : 0.0 ,
' delay ' : 0.0 ,
' product_uom_qty ' : 1 ,
' product_uos_qty ' : 1 ,
' type ' : ' make_to_stock ' ,
' product_packaging ' : False
}
2010-08-09 05:14:49 +00:00
sale_order_make_line ( )
2010-07-13 07:09:55 +00:00
2010-08-09 05:14:49 +00:00
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: