~openerp-spain-team/openerp-spain/6.0-git

« back to all changes in this revision

Viewing changes to extra_addons/sale_group/sale_group.py

  • Committer: Borja L.S.
  • Date: 2010-10-18 10:04:25 UTC
  • Revision ID: git-v1:271c47a993616dbba60585d48b8b98d603199d93
[REF] *: Refactorización para portar a 6.0 - Paso 1.

- Se han renombrado los módulos para usar la nomenclatura propuesta
  por OpenERP: l10n_es para el módulo base de localización (plan de 
  cuentas), l10n_es_* para el resto de módulos.

- Se eliminan los módulos extra_addons/* que deberían moverse a 
  los extra-addons genéricos (no son específicos de España).

- Se renombran los __terp__.py por __openerp__.py

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) 2008 Pablo Rocandio. 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
 
import time
24
 
import netsvc
25
 
from osv import fields, osv
26
 
import ir
27
 
from mx import DateTime
28
 
from tools import config
29
 
 
30
 
class sale_order(osv.osv):
31
 
 
32
 
    def _get_picking_ids(self, cr, uid, ids, field_name, arg, context):
33
 
        res = {}
34
 
        for order in self.pool.get('sale.order').browse(cr, uid, ids, context={}):
35
 
            res[order.id]=[]
36
 
            for line in order.order_line:
37
 
                for move in line.move_ids:
38
 
                    if move.picking_id.id not in res[order.id]:
39
 
                        res[order.id].append(move.picking_id.id)
40
 
        # res example: {2: [], 4: [7], 5: [8], 6: [9], 7: [10, 11, 12, 13]}
41
 
        return res
42
 
 
43
 
    _inherit = "sale.order"
44
 
    _columns = {
45
 
        'picking_ids': fields.function(_get_picking_ids, method=True, string='Related Packings', type="one2many", relation='stock.picking'),
46
 
        'order_policy': fields.selection([
47
 
            ('prepaid','Invoice before delivery'),
48
 
            ('manual','Shipping & Manual Invoice'),
49
 
            ('postpaid','Automatic Invoice after delivery'),
50
 
            ('picking','Invoice from the packings'),
51
 
            ('allmanual','Manual shipping and invoice'),
52
 
        ], 'Shipping Policy', required=True, readonly=True, states={'draft':[('readonly',False)]},),
53
 
                }
54
 
 
55
 
    _defaults = {
56
 
        'order_policy': lambda *a: 'allmanual',
57
 
                }
58
 
 
59
 
    def action_ship_activity(self, cr, uid, ids, *args):
60
 
        if len(ids) > 1:
61
 
            for order in self.browse(cr, uid, ids, context={}):
62
 
                if order.order_policy <> 'allmanual':
63
 
                    return True
64
 
            self.action_ship_create(cr, uid, ids, *args)
65
 
        else:
66
 
            for order in self.browse(cr, uid, ids, context={}):
67
 
                if order.order_policy <> 'allmanual':
68
 
                    self.action_ship_create(cr, uid, ids, *args)   
69
 
 
70
 
    def action_ship_create(self, cr, uid, ids, *args, **argv):
71
 
        # args: Group or not picking. Ej: (0,) 
72
 
        # argv: Picking from lines. Ej: {'lines_ids': {2: [1], 3: [2, 3]}}
73
 
        picking_id=False
74
 
        for order in self.browse(cr, uid, ids, context={}):
75
 
            output_id = order.shop_id.warehouse_id.lot_output_id.id
76
 
            if args and (args[0] == 0): # Agrupa o no los albaranes
77
 
                picking_id = False        # 0: No agrupar        1: Agrupar
78
 
            if argv and (len(argv['lines_ids'])>0): # Picking from sale order lines
79
 
                lines_ids = argv['lines_ids'][order.id]
80
 
                browse_lines = self.pool.get('sale.order.line').browse(cr, uid, lines_ids, context={})    
81
 
            else:
82
 
                browse_lines = order.order_line # Picking from sale order
83
 
            for line in browse_lines:
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: # Create new picking
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
 
                            'loc_move_id': loc_dest_id,
98
 
                            'sale_id': order.id,
99
 
                            'address_id': order.partner_shipping_id.id,
100
 
                            'note': order.note,
101
 
                            #'invoice_state': (order.order_policy=='picking' and '2binvoiced') or 'none',
102
 
                            'invoice_state': (order.order_policy in ('picking', 'allmanual') and '2binvoiced') or 'none',
103
 
                        })
104
 
 
105
 
                    move_id = self.pool.get('stock.move').create(cr, uid, {
106
 
                        'name': line.name[:64],
107
 
                        'picking_id': picking_id,
108
 
                        'product_id': line.product_id.id,
109
 
                        'date_planned': date_planned,
110
 
                        'product_qty': line.product_uom_qty,
111
 
                        'product_uom': line.product_uom.id,
112
 
                        'product_uos_qty': line.product_uos_qty,
113
 
                        'product_uos': (line.product_uos and line.product_uos.id)\
114
 
                                or line.product_uom.id,
115
 
                        'product_packaging' : line.product_packaging.id,
116
 
                        'address_id' : line.address_allotment_id.id or order.partner_shipping_id.id,
117
 
                        'location_id': location_id,
118
 
                        'location_dest_id': output_id,
119
 
                        'sale_line_id': line.id,
120
 
                        #'sale_line_ids':[(6,0,[line.id])],
121
 
                        'tracking_id': False,
122
 
                        'state': 'waiting',
123
 
                        'note': line.notes,
124
 
                    })
125
 
                    proc_id = self.pool.get('mrp.procurement').create(cr, uid, {
126
 
                        'name': order.name,
127
 
                        'origin': order.name,
128
 
                        'date_planned': date_planned,
129
 
                        'product_id': line.product_id.id,
130
 
                        'product_qty': line.product_uom_qty,
131
 
                        'product_uom': line.product_uom.id,
132
 
                        'location_id': order.shop_id.warehouse_id.lot_stock_id.id,
133
 
                        'procure_method': line.type,
134
 
                        'move_id': move_id,
135
 
                        'property_ids': [(6, 0, [x.id for x in line.property_ids])],
136
 
                    })
137
 
                    wf_service = netsvc.LocalService("workflow")
138
 
                    wf_service.trg_validate(uid, 'mrp.procurement', proc_id, 'button_confirm', cr)
139
 
                    #self.pool.get('sale.order.line').write(cr, uid, [line.id], {'procurement_id': proc_id})
140
 
                    self.pool.get('sale.order.line').write(cr, uid, [line.id], {'procurement_id': proc_id, 'state':'done'})
141
 
                elif line.product_id and line.product_id.product_tmpl_id.type=='service':
142
 
                    proc_id = self.pool.get('mrp.procurement').create(cr, uid, {
143
 
                        'name': line.name,
144
 
                        'origin': order.name,
145
 
                        'date_planned': date_planned,
146
 
                        'product_id': line.product_id.id,
147
 
                        'product_qty': line.product_uom_qty,
148
 
                        'product_uom': line.product_uom.id,
149
 
                        'location_id': order.shop_id.warehouse_id.lot_stock_id.id,
150
 
                        'procure_method': line.type,
151
 
                        'property_ids': [(6, 0, [x.id for x in line.property_ids])],
152
 
                    })
153
 
                    wf_service = netsvc.LocalService("workflow")
154
 
                    wf_service.trg_validate(uid, 'mrp.procurement', proc_id, 'button_confirm', cr)
155
 
                    self.pool.get('sale.order.line').write(cr, uid, [line.id], {'procurement_id': proc_id})
156
 
                else:
157
 
                    #
158
 
                    # No procurement because no product in the sale.order.line.
159
 
                    #
160
 
                    pass
161
 
 
162
 
            val = {}
163
 
            if picking_id:
164
 
                wf_service = netsvc.LocalService("workflow")
165
 
                wf_service.trg_validate(uid, 'stock.picking', picking_id, 'button_confirm', cr)
166
 
                #val = {'picking_ids':[(6,0,[picking_id])]}  # Linea originalmente comentada. 
167
 
 
168
 
            if order.state=='shipping_except':
169
 
                val['state'] = 'progress'
170
 
                if (order.order_policy == 'manual') and order.invoice_ids:
171
 
                    val['state'] = 'manual'
172
 
            self.write(cr, uid, [order.id], val)
173
 
        return True
174
 
sale_order()
175
 
 
176
 
 
177
 
class sale_order_line(osv.osv):
178
 
    def _picked_sum(self, cr, uid, ids, field_name, arg, context):
179
 
        res = {}
180
 
        for sale_line in self.browse(cr, uid, ids):
181
 
            qty_sum = 0
182
 
            for picking_line in sale_line.move_ids:
183
 
                qty_sum += picking_line.product_qty
184
 
            res[sale_line.id] = qty_sum
185
 
        return res
186
 
    _inherit = "sale.order.line"
187
 
    _columns = {    
188
 
        'picked_sum': fields.function(_picked_sum, method=True, type='integer', string='Picked qty'),
189
 
        'delivery_date':fields.date('Delivery Date', select=1)
190
 
               }
191
 
sale_order_line()
192
 
 
193
 
 
194
 
class stock_picking(osv.osv):
195
 
    _inherit = "stock.picking"
196
 
 
197
 
    def unlink(self, cr, uid, ids):
198
 
        line_ids = []
199
 
        fields=['move_lines']
200
 
        for picking in self.read(cr, uid, ids, fields, context={}):
201
 
            line_ids += picking['move_lines']
202
 
        self.pool.get('stock.move').unlink(cr, uid, line_ids)
203
 
        return super(stock_picking, self).unlink(cr, uid, ids)
204
 
stock_picking()
205
 
 
206
 
 
207
 
class stock_move(osv.osv):
208
 
    _inherit = "stock.move"
209
 
    _columns = {
210
 
        'picking_id': fields.many2one('stock.picking', 'Packing list', select=True, ondelete='cascade'), # ondelete='cascade' added
211
 
               }
212
 
    def unlink(self, cr, uid, ids):
213
 
        print "Dentro de Unlink de lineas"        # Pendiente de modificar el estado de las sale order line
214
 
        return super(stock_move, self).unlink(cr, uid, ids)
215
 
stock_move()
216
 
 
217
 
 
218
 
 
219
 
 
220
 
 
221