~ajite/openobject-addons/elico-7.0-add-0003

« back to all changes in this revision

Viewing changes to stock_pack_wizard/wizard/wizard_picking_tracking.py

  • Committer:
  • Date: 2014-04-07 11:05:57 UTC
  • Revision ID: elicoidal@hotmail.com-20140407110557-s61xq131n6f5ljj9
[ADD] several logistic modules

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
##############################################################################
 
3
#
 
4
#    OpenERP, Open Source Management Solution
 
5
#    Copyright (c) 2010-2013 Elico Corp. All Rights Reserved.
 
6
#     Jon Chow <jon.chow@elico-corp.com>
 
7
#
 
8
#    This program is free software: you can redistribute it and/or modify
 
9
#    it under the terms of the GNU Affero General Public License as
 
10
#    published by the Free Software Foundation, either version 3 of the
 
11
#    License, or (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 Affero General Public License for more details.
 
17
#
 
18
#    You should have received a copy of the GNU Affero General Public License
 
19
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
20
#
 
21
##############################################################################
 
22
 
 
23
from openerp.osv import fields,osv
 
24
from openerp.tools.translate import _
 
25
 
 
26
 
 
27
class  wizard_picking_tracking_line(osv.osv_memory):
 
28
    _name = 'wizard.picking.tracking.line'
 
29
    
 
30
    _columns = {
 
31
        'name':          fields.char('name', size=32),
 
32
        'wizard_id':        fields.many2one('wizard.picking.tracking', 'WPT'),
 
33
        'qty':           fields.float('Pack Quantity'),
 
34
        'stock_move_id': fields.many2one('stock.move',   'Stock move'),
 
35
        'product_id':    fields.related('stock_move_id', 'product_id',  type='many2one',relation='product.product', string='Product', readonly=True),
 
36
        'tracking_id':   fields.related('stock_move_id', 'tracking_id', type='many2one',relation='stock.tracking',  string='Pack',    readonly=True),
 
37
        'product_qty':   fields.related('stock_move_id', 'product_qty', type='float',   string='Delivery Quantity', readonly=True),
 
38
    }
 
39
    
 
40
    def onchange_stock_move_id(self, cr, uid, ids, stock_move_id, context=None):
 
41
        data = {'product_id': False, 'tracking_id':False, 'product_qty':0, 'qty':0 }
 
42
        if stock_move_id:
 
43
            stock_move  = self.pool.get('stock.move').browse(cr, uid, stock_move_id)
 
44
            product_id  = stock_move.product_id.id
 
45
            tracking_id = stock_move.tracking_id and stock_move.tracking_id.id or False
 
46
            product_qty = stock_move.product_qty
 
47
            qty         = not tracking_id and product_qty or 0.0
 
48
            data.update( {'product_id':product_id, 'tracking_id':tracking_id, 'product_qty':product_qty, 'qty':qty})
 
49
        return {'value': data}
 
50
 
 
51
wizard_picking_tracking_line()
 
52
 
 
53
 
 
54
 
 
55
class  wizard_picking_tracking(osv.osv_memory):
 
56
    _name = 'wizard.picking.tracking'
 
57
    
 
58
    def _get_serial(self, cr, uid, context=None):
 
59
        src_model  = context.get('active_model')
 
60
        src_record = self.pool.get(src_model).browse(cr, uid, context.get('active_id',False))
 
61
        
 
62
        if src_model == 'stock.picking.out':
 
63
            return  src_record.partner_id.ref
 
64
        elif src_model == 'stock.move':
 
65
            return  src_record.picking_id.partner_id.ref
 
66
        else:
 
67
            return None
 
68
    
 
69
    
 
70
    def _get_lines(self, cr, uid, context=None):
 
71
        src_model  = context.get('active_model')
 
72
        src_record = self.pool.get(src_model).browse(cr, uid, context.get('active_id',False))
 
73
        
 
74
        data = []
 
75
        if src_model == 'stock.picking.out':
 
76
            picking = src_record
 
77
            for move in picking.move_lines:
 
78
                data.append( (0, 0, {
 
79
                   'stock_move_id': move.id, 
 
80
                   'product_id':    move.product_id.id, 
 
81
                   'product_qty':   move.product_qty,
 
82
                   'tracking_id':   move.tracking_id and move.tracking_id.id or False,
 
83
                   'qty':           not move.tracking_id and move.product_qty or 0.0 ,
 
84
                }))
 
85
        
 
86
        elif src_model == 'stock.move':
 
87
            move = src_record
 
88
            data.append( (0, 0, {
 
89
                   'stock_move_id': move.id, 
 
90
                   'product_id':    move.product_id.id, 
 
91
                   'product_qty':   move.product_qty,
 
92
                   'tracking_id':   move.tracking_id and move.tracking_id.id or False,
 
93
                   'qty':           not move.tracking_id and move.product_qty or 0.0 ,
 
94
            }))
 
95
        
 
96
        return data
 
97
    
 
98
    
 
99
    def _get_picking(self, cr, uid, context=None):
 
100
        src_model = context.get('active_model')
 
101
        if src_model == 'stock.picking.out':
 
102
            return context.get('active_id', False)
 
103
        elif src_model == 'stock.move':
 
104
            stock_move=self.pool.get(src_model).browse(cr, uid, context.get('active_id', False))
 
105
            return stock_move.picking_id.id 
 
106
    
 
107
    
 
108
    def _get_pack_address(self, cr, uid, context=None):
 
109
        src_model  = context.get('active_model')
 
110
        src_record = self.pool.get(src_model).browse(cr, uid, context.get('active_id',False))
 
111
        if src_model == 'stock.picking.out':
 
112
            picking_out = src_record
 
113
            sale_order  = picking_out.sale_id
 
114
            return sale_order.partner_shipping_id and sale_order.partner_shipping_id.name or False
 
115
        
 
116
        elif src_model == 'stock.move':
 
117
            move       = src_record
 
118
            sale_order = move.picking_id.sale_id
 
119
            return sale_order.partner_shipping_id and sale_order.partner_shipping_id.name or False
 
120
    
 
121
    
 
122
    _columns={
 
123
        'name': fields.char('name', size=32, readonly=True),
 
124
        'picking_id': fields.many2one('stock.picking','Picking'),
 
125
        'ul_id': fields.many2one('product.ul', 'Pack Template', required=True),
 
126
        'lines': fields.one2many('wizard.picking.tracking.line', 'wizard_id', 'Lines'),
 
127
        
 
128
        'pack_address': fields.char('Address', size=128),
 
129
        'pack_note':    fields.char('Note', size=128),
 
130
        'gross_weight': fields.float('GW (Kg)'),
 
131
        #'net_weight':   fields.float('NW (Kg)'),
 
132
    }
 
133
    
 
134
    _defaults={
 
135
        'lines': lambda self,cr,uid,context: self._get_lines(cr, uid, context=context), 
 
136
        'picking_id': lambda self,cr,uid,context: self._get_picking(cr, uid, context=context), 
 
137
        'pack_address': lambda self,cr,uid,context: self._get_pack_address(cr, uid, context=context), 
 
138
    }
 
139
    
 
140
    
 
141
    def _action_check(self, wizard):
 
142
        if wizard.picking_id.state == 'done':
 
143
            raise osv.except_osv(_('Warning!'), _("Wizard split pack can not be used when state is done !"))
 
144
        for line in wizard.lines:
 
145
            if line.qty > line.product_qty:
 
146
                raise osv.except_osv(_('Warning!'), _("New pack quantity can not bigger than stock move quantity"))
 
147
        return True
 
148
    
 
149
    
 
150
    def action_split(self, cr, uid, ids, context=None):
 
151
        
 
152
        
 
153
        tracking_obj    = self.pool.get('stock.tracking')
 
154
        stock_move_obj  = self.pool.get('stock.move')
 
155
        procurement_obj = self.pool.get('procurement.order')
 
156
        wizard          = self.browse(cr,uid,ids[0],context=context)
 
157
        self._action_check(wizard)
 
158
        
 
159
        tracking_values = {
 
160
            'ul_id':  wizard.ul_id.id,
 
161
            'pack_address': wizard.pack_address,
 
162
            'pack_note':    wizard.pack_note,
 
163
            'gross_weight': wizard.gross_weight,
 
164
            'move_ids':     [],
 
165
         }
 
166
        
 
167
        new_pack_id = tracking_obj.create(cr, uid, tracking_values)
 
168
        
 
169
        for line in wizard.lines:
 
170
            stock_move  = line.stock_move_id
 
171
            old_pack_id = stock_move.tracking_id and stock_move.tracking_id.id or False
 
172
            
 
173
            if not line.qty:
 
174
                continue
 
175
            
 
176
            res_product_qty = stock_move.product_qty - line.qty
 
177
            
 
178
            if res_product_qty:
 
179
                # update old stock move count procurement count
 
180
                stock_move_obj.write(cr, uid, stock_move.id, {'product_qty':res_product_qty})
 
181
                
 
182
                procurement_id = stock_move.procurements and stock_move.procurements[0].id
 
183
                
 
184
                if procurement_id:
 
185
                    procurement_obj.write(cr, uid, procurement_id, {'product_uos_qty':res_product_qty, 'product_qty':res_product_qty})
 
186
                
 
187
                new_move_data = stock_move_obj.copy_data(cr, uid, stock_move.id, default = {
 
188
                    'product_qty':     line.qty , 
 
189
                    'product_uos_qty': line.qty, 
 
190
                    'tracking_id':     new_pack_id,  
 
191
                    'procurements':    False,                                                       
 
192
                }) 
 
193
                new_move_id = stock_move_obj.create(cr, uid, new_move_data)
 
194
                
 
195
                if procurement_id:
 
196
                    procurement_data = procurement_obj.copy_data(cr, uid, procurement_id, default = {
 
197
                        'product_uos_qty': line.qty,
 
198
                        'product_qty':     line.qty,
 
199
                        'move':            new_move_id,
 
200
                    })
 
201
                    new_procurement_id = procurement_obj.create(cr, uid, procurement_data)
 
202
            
 
203
            else:
 
204
                #if not res_product_qty, only to change the package
 
205
                stock_move_obj.write(cr, uid, stock_move.id, {'tracking_id':new_pack_id})
 
206
        
 
207
        #return new_pack_id
 
208
        return {
 
209
          'name': _('Pack Split'),
 
210
          'view_type': 'form',
 
211
          "view_mode": 'form',
 
212
          'res_model': 'stock.picking.out',
 
213
          'res_id':    wizard.picking_id.id,
 
214
          'type':      'ir.actions.act_window',
 
215
        }
 
216
    
 
217
wizard_picking_tracking()
 
218
 
 
219
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
 
b'\\ No newline at end of file'