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

« back to all changes in this revision

Viewing changes to library/sale.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 import DateTime
 
24
import netsvc
 
25
from osv import fields, osv
 
26
 
 
27
 
 
28
class sale_order_line(osv.osv):
 
29
    _inherit = 'sale.order.line'
 
30
    _columns = {
 
31
        'production_lot_id': fields.many2one('stock.production.lot', 'Production Lot',),
 
32
        'customer_ref': fields.char('Customer reference', size=64),
 
33
    }
 
34
 
 
35
    _defaults = {
 
36
        'type': lambda *a: 'make_to_order',
 
37
        }
 
38
 
 
39
    def button_confirm(self, cr, uid, ids, context={}):
 
40
        lines = self.pool.get('sale.order.line').browse(cr, uid, ids)
 
41
        l_id = 0
 
42
        for line in lines:
 
43
            if line.production_lot_id:
 
44
                continue
 
45
            l_id += 1
 
46
            production_lot_dico = {
 
47
                'name': line.order_id and (str(line.order_id.name)+('/%02d'%(l_id,))) or False,
 
48
                'product_id': line.product_id.id
 
49
            }
 
50
            production_lot_id = self.pool.get('stock.production.lot').create(cr, uid, production_lot_dico)
 
51
            self.pool.get('sale.order.line').write(cr, uid, [line.id], {'production_lot_id': production_lot_id})
 
52
 
 
53
        super(sale_order_line, self).button_confirm(cr, uid, ids, context)
 
54
        return
 
55
 
 
56
    def copy(self, cr, uid, id, default=None, context={}):
 
57
        if not default:
 
58
            default = {}
 
59
        default.update({
 
60
            'production_lot_id': False,
 
61
            'customer_ref': ''
 
62
        })
 
63
        return super(sale_order_line, self).copy(cr, uid, id, default, context)
 
64
sale_order_line()
 
65
 
 
66
 
 
67
class sale_order(osv.osv):
 
68
 
 
69
    _inherit = "sale.order"
 
70
    _order = "create_date desc"
 
71
 
 
72
    _defaults = {
 
73
        'invoice_quantity': lambda *a: 'procurement',
 
74
        'picking_policy': lambda *a: 'direct',
 
75
        'order_policy': lambda *a: 'picking',
 
76
    }
 
77
 
 
78
    def action_ship_create(self, cr, uid, ids, *args):
 
79
        picking_id = False
 
80
        for order in self.browse(cr, uid, ids, context={}):
 
81
            output_id = order.shop_id.warehouse_id.lot_output_id.id
 
82
            picking_id = False
 
83
            for line in order.order_line:
 
84
                proc_id = False
 
85
                date_planned = (DateTime.now() + DateTime.RelativeDateTime(days=line.delay or 0.0)).strftime('%Y-%m-%d')
 
86
                if line.state == 'done':
 
87
                    continue
 
88
                if line.product_id and line.product_id.product_tmpl_id.type in ('product', 'consu'):
 
89
                    location_id = order.shop_id.warehouse_id.lot_stock_id.id
 
90
                    if not picking_id:
 
91
                        loc_dest_id = order.partner_id.property_stock_customer.id
 
92
                        picking_id = self.pool.get('stock.picking').create(cr, uid, {
 
93
                            'origin': order.name,
 
94
                            'type': 'out',
 
95
                            'state': 'auto',
 
96
                            'move_type': order.picking_policy,
 
97
                            'sale_id': order.id,
 
98
                            'address_id': order.partner_shipping_id.id,
 
99
                            'note': order.note,
 
100
                            'invoice_state': (order.order_policy=='picking' and '2binvoiced') or 'none',
 
101
                            'carrier_id': order.carrier_id.id,
 
102
                        })
 
103
                    move_id = self.pool.get('stock.move').create(cr, uid, {
 
104
                        'name': 'SO:' + order.name,
 
105
                        'picking_id': picking_id,
 
106
                        'product_id': line.product_id.id,
 
107
                        'date_planned': date_planned,
 
108
                        'product_qty': line.product_uom_qty,
 
109
                        'product_uom': line.product_uom.id,
 
110
                        'product_uos_qty': line.product_uos_qty,
 
111
                        'product_uos': line.product_uos.id,
 
112
                        'product_packaging': line.product_packaging.id,
 
113
                        'address_id': line.address_allotment_id.id or order.partner_shipping_id.id,
 
114
                        'location_id': location_id,
 
115
                        'location_dest_id': output_id,
 
116
                        'sale_line_id': line.id,
 
117
                        'tracking_id': False,
 
118
                        'state': 'waiting',
 
119
                        'note': line.notes,
 
120
                        'prodlot_id': line.production_lot_id.id,
 
121
                        'customer_ref': line.customer_ref,
 
122
                    })
 
123
                    proc_id = self.pool.get('mrp.procurement').create(cr, uid, {
 
124
                        'name': order.name,
 
125
                        'origin': order.name,
 
126
                        'date_planned': date_planned,
 
127
                        'product_id': line.product_id.id,
 
128
                        'product_qty': line.product_uom_qty,
 
129
                        'product_uom': line.product_uom.id,
 
130
                        'location_id': order.shop_id.warehouse_id.lot_stock_id.id,
 
131
                        'procure_method': line.type,
 
132
                        'move_id': move_id,
 
133
                        'production_lot_id': line.production_lot_id.id,
 
134
                        'customer_ref': line.customer_ref,
 
135
                    })
 
136
                    wf_service = netsvc.LocalService("workflow")
 
137
                    wf_service.trg_validate(uid, 'mrp.procurement', proc_id, 'button_confirm', cr)
 
138
                    self.pool.get('sale.order.line').write(cr, uid, [line.id], {'procurement_id': proc_id})
 
139
                elif line.product_id and line.product_id.product_tmpl_id.type == 'service':
 
140
                    proc_id = self.pool.get('mrp.procurement').create(cr, uid, {
 
141
                        'name': line.name,
 
142
                        'origin': order.name,
 
143
                        'date_planned': date_planned,
 
144
                        'product_id': line.product_id.id,
 
145
                        'product_qty': line.product_uom_qty,
 
146
                        'product_uom': line.product_uom.id,
 
147
                        'location_id': order.shop_id.warehouse_id.lot_stock_id.id,
 
148
                        'procure_method': line.type,
 
149
                    })
 
150
                    wf_service = netsvc.LocalService("workflow")
 
151
                    wf_service.trg_validate(uid, 'mrp.procurement', proc_id, 'button_confirm', cr)
 
152
                    self.pool.get('sale.order.line').write(cr, uid, [line.id], {'procurement_id': proc_id})
 
153
                else:
 
154
                    #
 
155
                    # No procurement because no product in the sale.order.line.
 
156
                    #
 
157
                    pass
 
158
 
 
159
            val = {}
 
160
            if picking_id:
 
161
                wf_service = netsvc.LocalService("workflow")
 
162
                wf_service.trg_validate(uid, 'stock.picking', picking_id, 'button_confirm', cr)
 
163
                #val = {'picking_ids':[(6,0,[picking_id])]}
 
164
 
 
165
            if order.state == 'shipping_except':
 
166
                val['state'] = 'progress'
 
167
                if (order.order_policy == 'manual') and order.invoice_ids:
 
168
                    val['state'] = 'manual'
 
169
            self.write(cr, uid, [order.id], val)
 
170
        return True
 
171
 
 
172
 
 
173
 
 
174
#   def action_ship_create(self, cr, uid, ids, *args):
 
175
#       result = super(sale_order, self).action_ship_create(cr, uid, ids, *args)
 
176
#       mids=[]
 
177
#       pids=[]
 
178
#       move_obj = self.pool.get('stock.move')
 
179
#       proc_obj = self.pool.get('mrp.procurement')
 
180
#       for order in self.browse(cr, uid, ids, context={}):
 
181
#           for line in order.order_line:
 
182
#               for move in line.move_ids :
 
183
#                   mids.append(move.id)
 
184
#                   pids.extend([p.id for p in move.procurement_ids])
 
185
#               move_obj.write(cr, uid, mids, {
 
186
#                   'prodlot_id': line.production_lot_id.id,
 
187
#                   'customer_ref': line.customer_ref
 
188
#               })
 
189
#               proc_obj.write(cr, uid, pids, {
 
190
#                   'production_lot_id': line.production_lot_id.id,
 
191
#                   'customer_ref': line.customer_ref
 
192
#               })
 
193
 
 
194
#       return result
 
195
 
 
196
sale_order()
 
197
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
198