~openerp-commiter/openobject-addons/stable-sja-branch

« back to all changes in this revision

Viewing changes to sale_intercompany/wizard/makesale.py

  • Committer: sja-axelor
  • Date: 2009-10-13 09:52:57 UTC
  • Revision ID: suniljagyasi@gmail.com-20091013095257-8u26ww0r20z9y6ey
add

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
 
6
#    $Id$
 
7
#
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU General Public License as published by
 
10
#    the Free Software Foundation, either version 3 of the License, or
 
11
#    (at your option) any later version.
 
12
#
 
13
#    This program is distributed in the hope that it will be useful,
 
14
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
#    GNU General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
##############################################################################
 
22
 
 
23
from mx.DateTime import now
 
24
 
 
25
import wizard
 
26
import netsvc
 
27
import ir
 
28
import pooler
 
29
 
 
30
class make_sale(wizard.interface):
 
31
 
 
32
    def _selectPartner(self, cr, uid, data, context):
 
33
        case_obj = pooler.get_pool(cr.dbname).get('crm.case')
 
34
        case = case_obj.read(cr, uid, data['ids'], ['partner_id'])
 
35
        return {'partner_id': case[0]['partner_id']}
 
36
 
 
37
    def _makeOrder(self, cr, uid, data, context):
 
38
        pool = pooler.get_pool(cr.dbname)
 
39
        purchase_obj = pool.get('purchase.order')
 
40
        sale_obj = pool.get('sale.order')
 
41
 
 
42
        shop_obj = pool.get('sale.shop')
 
43
        shop_id = shop_obj.search(cr, uid, [])[0]
 
44
 
 
45
        partner_obj = pool.get('res.partner')
 
46
        sale_line_obj = pool.get('sale.order.line')
 
47
 
 
48
 
 
49
        new_ids = []
 
50
 
 
51
        user = pool.get('res.users').browse(cr, uid, uid)
 
52
        partner_id = user.company_id.partner_id.id
 
53
        partner_addr = partner_obj.address_get(cr, uid, [partner_id],
 
54
                ['invoice', 'delivery', 'contact'])
 
55
        default_pricelist = partner_obj.browse(cr, uid, partner_id,
 
56
                    context).property_product_pricelist.id
 
57
        fpos = partner_obj.browse(cr, uid, partner_id,
 
58
                    context).property_account_position
 
59
        fpos_id = fpos and fpos.id or False
 
60
 
 
61
        for purchase in purchase_obj.browse(cr, uid, data['ids']):
 
62
            vals = {
 
63
                'origin': 'PO:%s' % str(purchase.name),
 
64
                'picking_policy': 'direct',
 
65
                'shop_id': shop_id,
 
66
                'partner_id': partner_id,
 
67
                'pricelist_id': default_pricelist,
 
68
                'partner_invoice_id': partner_addr['invoice'],
 
69
                'partner_order_id': partner_addr['contact'],
 
70
                'partner_shipping_id': partner_addr['delivery'],
 
71
                'order_policy': 'manual',
 
72
                'date_order': now(),
 
73
                'fiscal_position': fpos_id
 
74
            }
 
75
            new_id = sale_obj.create(cr, uid, vals)
 
76
            fpos = user.company_id.partner_id.property_account_position and user.company_id.partner_id.property_account_position.id or False
 
77
            for line in purchase.order_line:
 
78
                value = sale_line_obj.product_id_change(cr, uid, [], default_pricelist,
 
79
                        line.product_id.id, qty=line.product_qty, partner_id=partner_id, fiscal_position=fpos)['value']
 
80
                value['price_unit'] = line.price_unit
 
81
                value['product_id'] = line.product_id.id
 
82
                value['product_uos'] = value.get('product_uos', [False,False])[0]
 
83
                value['product_uom_qty'] = line.product_qty
 
84
                value['order_id'] = new_id
 
85
                sale_line_obj.create(cr, uid, value)
 
86
 
 
87
        return {}
 
88
 
 
89
    states = {
 
90
        'init': {
 
91
            'actions': [_makeOrder],
 
92
            'result': {'type': 'state', 'state': 'end'}
 
93
        }
 
94
    }
 
95
 
 
96
make_sale('purchase.order.interco')
 
97
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
98