~vauxoo/openerp-intercompany/trunk

« back to all changes in this revision

Viewing changes to extra_UOM/model/stock.py

  • Committer: jose@vauxoo.com
  • Date: 2012-01-03 15:48:48 UTC
  • Revision ID: jose@vauxoo.com-20120103154848-cxr6hkzdpiwtfysy

[FIX] Split of module intercompany_tcv

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
 
#
6
 
#    This program is free software: you can redistribute it and/or modify
7
 
#    it under the terms of the GNU Affero General Public License as
8
 
#    published by the Free Software Foundation, either version 3 of the
9
 
#    License, or (at your option) any later version.
10
 
#
11
 
#    This program is distributed in the hope that it will be useful,
12
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 
#    GNU Affero General Public License for more details.
15
 
#
16
 
#    You should have received a copy of the GNU Affero General Public License
17
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 
#
19
 
##############################################################################
20
 
import time
21
 
from datetime import datetime
22
 
from dateutil.relativedelta import relativedelta
23
 
from datetime import datetime, timedelta
24
 
from osv import osv, fields
25
 
import netsvc
26
 
import pooler
27
 
from tools.translate import _
28
 
import decimal_precision as dp
29
 
from osv.orm import browse_record, browse_null
30
 
 
31
 
class stock_move(osv.osv):
32
 
    _inherit = 'stock.move'
33
 
    
34
 
    def _compute_pieces(self, cr, uid, ids, name, arg, context=None):
35
 
        move = self.browse(cr, uid, ids)        
36
 
        res = {}
37
 
        for m in move:
38
 
            if m.product_id.stock_driver in ('tile','slab','block'):
39
 
                uom = self.pool.get('product.uom')
40
 
                pieces = uom._compute_pieces(cr, uid, m.product_id.stock_driver, m.product_qty, m.prodlot_id.lot_factor, context)
41
 
                res[m.id] = pieces
42
 
            else:          
43
 
                res[m.id] = 0               
44
 
        return res
45
 
    
46
 
    _columns = {
47
 
       'pieces_qty': fields.integer('Pieces'),
48
 
        'length': fields.float('Length (m)',digits_compute=dp.get_precision('Product UoM')),
49
 
        'width': fields.float('Width (m)',digits_compute=dp.get_precision('Product UoM')),
50
 
        'heigth': fields.float('Heigth (m)',digits_compute=dp.get_precision('Product UoM')),
51
 
        'company_id': fields.many2one('res.company','Company'),
52
 
        
53
 
        
54
 
   
55
 
    }
56
 
 
57
 
stock_move()
58
 
 
59
 
class stock_inventory_line(osv.osv):
60
 
    """
61
 
    stock_inventory_line
62
 
    """
63
 
    
64
 
    def _compute_pieces(self, cr, uid, ids, name, arg, context=None):
65
 
        line = self.browse(cr, uid, ids)        
66
 
        res = {}
67
 
        for l in line:
68
 
            if l.product_id.stock_driver in ('tile','slab','block'):
69
 
                uom = self.pool.get('product.uom')     
70
 
                pieces = uom._compute_pieces(cr, uid, l.product_id.stock_driver, l.product_qty, l.prod_lot_id.lot_factor, context)
71
 
                res[l.id] = pieces
72
 
            else:          
73
 
                res[l.id] = 0               
74
 
        return res
75
 
    
76
 
    _inherit = 'stock.inventory.line'
77
 
 
78
 
    _columns = {
79
 
        'pieces_qty': fields.function(_compute_pieces, method=True, type="integer", string='Pieces'),
80
 
    }
81
 
    
82
 
stock_inventory_line()
83
 
 
84
 
class stock_picking(osv.osv):
85
 
    
86
 
      
87
 
    def _invoice_line_hook(self, cr, uid, move_line, invoice_line_id):
88
 
        ail_obj = self.pool.get('account.invoice.line')
89
 
        ail_obj.write(cr,uid,invoice_line_id,{'prod_lot_id':move_line.prodlot_id and move_line.prodlot_id.id,'pieces':move_line.pieces_qty},context=None)
90
 
        return super(stock_picking,self)._invoice_line_hook(cr, uid, move_line, invoice_line_id)
91
 
  
92
 
  
93
 
    _inherit = 'stock.picking'
94
 
    _columns = {
95
 
        'pieces_qty':fields.integer('Pieces'),
96
 
        'state_rw':fields.integer('State'),
97
 
        'company_id': fields.many2one('res.company','Company'),
98
 
        'picking_origin_ids':fields.many2many('stock.picking','picking_intercompany_rel','picking_origin_id','picking_sale_id','Picking'),
99
 
        'picking_sale_ids':fields.many2many('stock.picking','picking_intercompany_rel','picking_sale_id','picking_origin_id','Picking'),
100
 
    
101
 
    }
102
 
    
103
 
    _defaults = {
104
 
        'state_rw':0,
105
 
    }
106
 
    
107
 
    def draft_validate(self, cr, uid, ids, context=None):
108
 
        '''
109
 
        Added the state chage to can do changes in the picking(INVOICE)
110
 
        '''
111
 
        if context is None:
112
 
            context = {}
113
 
        self.write(cr,uid,ids,{'state_rw':0},context=context)
114
 
        res = super(stock_picking, self).draft_validate(cr, uid, ids, context)
115
 
        
116
 
        
117
 
        return res
118
 
    
119
 
    def action_invoice_create(self, cr, uid, ids, journal_id=False,group=False, type='out_invoice', context=None):
120
 
        '''
121
 
        Method that generate invoices in the companies related with this transaction
122
 
        '''
123
 
        #~ TODO: Revisar los impuestos en las facturas generadas
124
 
        if context is None: 
125
 
            context = {}
126
 
        stock_partial_picking_obj = self.pool.get('stock.partial.picking')
127
 
        stock_invoice_onshiping_obj = self.pool.get('stock.invoice.onshipping')
128
 
        self.write(cr,uid,ids,{'state_rw':0},context=context)
129
 
        pickng_brw = self.browse(cr,uid,ids,context=context)
130
 
        res = super(stock_picking, self).action_invoice_create(cr, uid, ids,journal_id=journal_id,
131
 
            group=group, type=type, context=context)
132
 
 
133
 
 
134
 
        if context.get('inter',False):
135
 
            pass
136
 
        else:
137
 
            for i in pickng_brw[0].picking_sale_ids:
138
 
                if i.id == ids[0]:
139
 
                    continue
140
 
                else:
141
 
                    self.draft_validate(cr,i.company_id.user_in_id.id,[i.id],context)
142
 
                    context.update({'inter':True})
143
 
                    journal_ids = self.pool.get('account.journal').search(cr,uid,[('company_id','=',i.company_id.id),('type','=','purchase')],context=context)
144
 
                    pickng_brw = self.browse(cr,uid,i.id,context=context)
145
 
                    if pickng_brw.type == 'in':
146
 
                        type = 'in_invoice' 
147
 
                    else:
148
 
                        type = 'out_invoice' 
149
 
                    self.action_invoice_create(cr, i.company_id.user_in_id.id, [i.id],journal_id=journal_ids[0],group=False, type=type, context=context)
150
 
        return res
151
 
 
152
 
 
153
 
    def write(self, cr, uid, ids, vals, context=None):
154
 
        """
155
 
        Chages for not write in a picking intercompany
156
 
        """
157
 
        if context is None:
158
 
            context = {}
159
 
        picking_brw = self.browse(cr,uid,ids,context=context)
160
 
        for val in vals.keys():
161
 
            if val == 'state_rw':
162
 
                True
163
 
            else:
164
 
                for i in picking_brw:
165
 
                    if i.state_rw !=0 and i.state == 'done':
166
 
                        raise osv.except_osv(_('Invalid action !'),_('Cannot write this picking because they are a transactions intercompany !'))
167
 
        
168
 
        return super(stock_picking, self).write(cr, uid, ids, vals, context=context)
169
 
    
170
 
    def unlink(self, cr, uid, ids, context=None):
171
 
        """
172
 
         Modified to avoid delete a line of a picking intercompany
173
 
        """
174
 
        
175
 
        picking_brw = self.browse(cr, uid, ids,context=context)
176
 
        unlink_ids = []
177
 
        for s in picking_brw:
178
 
            if s.state_rw !=0 or s.state == 'done' :
179
 
                raise osv.except_osv(_('Invalid action !'),_('Cannot delete this picking because they are a transactions intercompany !'))
180
 
            else:
181
 
                unlink_ids.append((s.id))   
182
 
    
183
 
        return osv.osv.unlink(self, cr, uid, unlink_ids, context=context)
184
 
        
185
 
    def copy(self, cr, uid, id, default=None, context=None):
186
 
        """
187
 
       Modified to avoid doubling a picking intercompany
188
 
        
189
 
        """
190
 
        if not default:
191
 
            default = {}
192
 
        
193
 
        picking_brw = self.browse(cr,uid,id,context=context)
194
 
        if picking_brw.state_rw != 0:
195
 
            default.update({
196
 
                'ref': [],
197
 
                'move_ids': [],
198
 
                'revisions': [],
199
 
                
200
 
            })
201
 
 
202
 
        return super(stock_picking, self).copy(cr, uid, id, default, context=context)
203
 
    
204
 
stock_picking()
205
 
 
206