~savoirfairelinux-openerp/openerp-usa/openerp-usa

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

import time
from report import report_sxw
from osv import osv
import pooler

class picking(report_sxw.rml_parse):
    def __init__(self, cr, uid, name, context):
        super(picking, self).__init__(cr, uid, name, context=context)
        self.localcontext.update({
            'time': time,
            'get_qtytotal':self._get_qtytotal,
            
            '_get_parent':self._get_parent,
            '_get_childs':self._get_childs,
            '_get_move_lines' : self._get_move_lines,
            
        })
        
    def _get_move_lines(self, pick_id):
        cr  = self.cr
        uid = self.uid
        context = self.localcontext
        pick = self.pool.get('stock.picking').browse(cr, uid, pick_id)
        sale = pick.sale_id
        res=[]
        
        if not sale:
            for move in pick.move_lines:
                res.append({
                                'name'      : '[' + move.product_id.default_code + '] ' + move.product_id.name,
                                'lot'       : move.prodlot_id and move.prodlot_id.name or '',
                                'state'     : move.state,
                                'location'  : move.location_id and move.location_id.name or '',
                                'quantity'  : self.formatLang(move.product_qty) + ' ' + move.product_uom.name,
                                'parent'    : False,
                                'style'     : 'normal',
                            })
            return res
        
        move_datas = []
        
#===============================================================================
#        move_lines = self.pool.get('stock.move').search(cr, uid, [('picking_id','=',pick.id)], order="parent_bom_id,id desc", offset=0,  limit=None,
#            context=context, count=False )
#        
#===============================================================================
        
        self.cr.execute("SELECT id from stock_move where picking_id='%s' order by parent_bom_id;"%(pick.id))
        fetchdata = self.cr.fetchall() or None
        if not fetchdata:
            move_lines = []
        if isinstance(fetchdata, list):
            move_lines = [t[0] for t in fetchdata]
        else:
            move_lines = fetchdata[0]
        
        moves = self.pool.get('stock.move').browse(cr, uid, move_lines, context=context)
        
        for move in moves:
            move_datas.append({
                                    'object'    : move,
                                    'state'     : 'init', # added, assigned bom
                                    'bom_type'  : '',
                                    'bom'       : False,
                                    'order_line': False,
                                    'parent_product' : False
                               })
        order_data = {}
        for i, move_data in enumerate(move_datas):
            move=move_data['object']
            for order_line in sale.order_line:
                if order_line.product_id and order_line.product_id.bom_ids:
                    bom=order_line.product_id.bom_ids[0]
                    if bom.type=='assembly':
                        f=0
                        #Check the product present in bom lines
                        for bom_line in bom.bom_lines:
                            if bom_line.product_id.id == move.product_id.id:
                               f=1
                               break
                        # if product is a sub component 
                        if f:
                            order_data[order_line.id]  = 1
                            move_datas[i]['parent_product'] =  order_line.product_id.id
                            move_datas[i]['bom'] = bom_line
                            move_datas[i]['bom_type'] = 'assembly'
                            move_datas[i]['order_line'] = order_line
            
        
        for i, move_data in enumerate(move_datas):
            move=move_data['object']
            if move_data['bom_type'] == 'assembly':
                if order_data[move_data['order_line'].id]:
                    res.append({
                                'name' : ' [' + move_data['order_line'].product_id.code + '] ' +  move_data['order_line'].product_id.name,
                                'lot'       : '',
                                'state'     : '',
                                'location'  : '',
                                'quantity'  : '',
                                'parent'    : True,
                                'style'     : 'parent',
                            })
                    order_data[move_data['order_line'].id] = 0
            
                res.append({
                                'name'      : '[' + move.product_id.default_code + '] ' + move.product_id.name,
                                'lot'       : move.prodlot_id and move.prodlot_id.name or '',
                                'state'     : move.state,
                                'location'  : move.location_id and move.location_id.name or '',
                                'quantity'  : self.formatLang(move.product_qty) + ' ' + move.product_uom.name,
                                'parent'    : False,
                                'style'     : 'child',
                            })
            else:
                res.append({
                            'name'      : '[' + move.product_id.default_code + '] ' + move.product_id.name,
                            'lot'       : move.prodlot_id and move.prodlot_id.name or '',
                            'state'     : move.state,
                            'location'  : move.location_id and move.location_id.name or '',
                            'quantity'  : self.formatLang(move.product_qty) + ' ' + move.product_uom.name,
                            'parent'    : False,
                            'style'     : 'normal',
                        })
        
        return res

    def _get_parent(self,pick_id):
        cr=self.cr
        uid=self.uid
        context=self.localcontext
        move_obj=self.pool.get('stock.move')
        move_ids=move_obj.search(cr,uid,[('picking_id','=',pick_id),("parent_bom_id","!=",False)])
        moves=move_obj.browse(cr,uid,move_ids,context=context)
        for move in moves:
            production=move.kit_id
            if production:
                product_qty=production.product_qty
                sale_id=move.sale_id.id

                parent_move_ids=move_obj.search(cr,uid,[("sale_id","=",sale_id),
                                                       ("product_qty","=",product_qty),
                                                       ("product_id","=",production.product_id.id)])
                parent_moves=move_obj.browse(cr,uid,parent_move_ids)
                #print "parent_move_ids",parent_move_ids
#                return {'child_moves':moves,"parent_moves":parent_moves}
                return parent_moves
                break
            
            
    def _get_childs(self,pick_id):
        cr=self.cr
        uid=self.uid
        context=self.localcontext
        move_obj=self.pool.get('stock.move')
        move_ids=move_obj.search(cr,uid,[('picking_id','=',pick_id),("parent_bom_id","!=",False)])
        print move_ids,'====================move_ids================================'
        return move_obj.browse(cr,uid,move_ids,context=context)

    def _get_qtytotal(self,move_lines):
        total = 0.0
        uom = move_lines[0].product_uom.name
        for move in move_lines:
            total+=move.product_qty
        return {'quantity':total,'uom':uom}

report_sxw.report_sxw('report.stock.picking.list.us','stock.picking','addons/reports_address_us/report/picking_us.rml',parser=picking)
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: