~unifield-team/unifield-wm/us-826

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
189
190
191
192
# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2011 TeMPO Consulting, MSF 
#
#    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 tools
from osv import fields,osv
from decimal_precision import decimal_precision as dp

class auto_supply_rules_report(osv.osv):
    _name = 'auto.supply.rules.report'
    _auto = False
    _order = 'product_reference, product_name, product_id, location_id'
    
    def _get_nomen_s(self, cr, uid, ids, fields, *a, **b):
        value = {}
        for f in fields:
            value[f] = False

        ret = {}
        for id in ids:
            ret[id] = value
        return ret
    
    def _search_nomen_s(self, cr, uid, obj, name, args, context=None):

        if not args:
            return []
        narg = []
        for arg in args:
            el = arg[0].split('_')
            el.pop()
            narg=[('_'.join(el), arg[1], arg[2])]
        
        return narg
    
    def onChangeSearchNomenclature(self, cr, uid, id, position, type, nomen_manda_0, nomen_manda_1, nomen_manda_2, nomen_manda_3, num=True, context=None):
        return self.pool.get('product.product').onChangeSearchNomenclature(cr, uid, id, position, type, nomen_manda_0, nomen_manda_1, nomen_manda_2, nomen_manda_3, num=num, context=context)
    
    def _get_nomen_name(self, cr, uid, ids, field_name, args, context=None):
        '''
        Returns a string with all nomenclature levels separated by '/'
        '''
        res = {}
        
        for rule in self.browse(cr, uid, ids, context=context):
            res[rule.id] = ''
            if rule.nomen_manda_0:
                res[rule.id] = rule.nomen_manda_0.name
            if rule.nomen_manda_1:
                res[rule.id] += '/'
                res[rule.id] += rule.nomen_manda_1.name
            if rule.nomen_manda_2:
                res[rule.id] += '/'
                res[rule.id] += rule.nomen_manda_2.name
            if rule.nomen_manda_3:
                res[rule.id] += '/'
                res[rule.id] += rule.nomen_manda_3.name 
            
        return res
    
    def _get_stock(self, cr, uid, ids, field_name, args, context=None):
        '''
        Returns stock value of a product
        '''
        if context is None:
            context = {}
        res = {}
        
        ir_data = self.pool.get('ir.model.data')
        stock_location = ir_data.get_object_reference(cr, uid, 'stock', 'stock_location_stock')[1]
        # TODO: Change msf_profile by msf_location_setup module
        intermediate_stock = ir_data.get_object_reference(cr, uid, 'msf_config_locations', 'stock_location_intermediate_client_view')[1]
        consumption_stock = ir_data.get_object_reference(cr, uid, 'msf_config_locations', 'stock_location_consumption_units_view')[1]
        
        for rule in self.browse(cr, uid, ids, context=context):
            res[rule.id] = {'stock': 0.00,
                            'intermediate_stock': 0.00,
                            'consumption_stock': 0.00,
                            'amc': 0.00,
                            'fmc': 0.00}
            
            if rule.product_id:
                res[rule.id]['amc'] = rule.product_id.product_amc
                res[rule.id]['fmc'] = rule.product_id.reviewed_consumption
            
                c = context.copy()
                c.update({'location': stock_location, 'compute_child': True})
                product = self.pool.get('product.product').browse(cr, uid, rule.product_id.id, context=c)
                res[rule.id]['stock'] = product.qty_available
                
                c2 = context.copy()
                c2.update({'location': consumption_stock, 'compute_child': True})
                product2 = self.pool.get('product.product').browse(cr, uid, rule.product_id.id, context=c2)
                res[rule.id]['consumption_stock'] = product2.qty_available
                
                c3 = context.copy()
                c3.update({'location': intermediate_stock, 'compute_child': True})
                product3 = self.pool.get('product.product').browse(cr, uid, rule.product_id.id, context=c3)
                res[rule.id]['intermediate_stock'] = product3.qty_available
            
            total_stock = res[rule.id]['stock'] + res[rule.id]['intermediate_stock'] + res[rule.id]['consumption_stock']
            moh = res[rule.id]['amc'] > 0 and total_stock / res[rule.id]['amc'] or 0.00
            
            res[rule.id].update({'total_stock': total_stock, 'moh': moh})
        
        return res
    
    _columns = {
        'rule_id': fields.many2one('stock.warehouse.automatic.supply', string='Rules reference', readonly=True),
        'product_id': fields.many2one('product.product', string='Product', readonly=True),
        'location_id': fields.many2one('stock.location', string='Location', readonly=True),
        'product_reference': fields.char(size=64, string='Reference', type='char'),
        'product_name': fields.char(size=128, string='Name', type='char'),
        'nomen_manda_0': fields.many2one('product.nomenclature', 'Main Type', required=True, select=1),
        'nomen_manda_1': fields.many2one('product.nomenclature', 'Group', required=True, select=1),
        'nomen_manda_2': fields.many2one('product.nomenclature', 'Family', required=True, select=1),
        'nomen_manda_3': fields.many2one('product.nomenclature', 'Root', required=True, select=1),
        'nomen_manda_0_s': fields.function(_get_nomen_s, method=True, type='many2one', relation='product.nomenclature', string='Main Type', fnct_search=_search_nomen_s, multi="nom_s"),
        'nomen_manda_1_s': fields.function(_get_nomen_s, method=True, type='many2one', relation='product.nomenclature', string='Group', fnct_search=_search_nomen_s, multi="nom_s"),
        'nomen_manda_2_s': fields.function(_get_nomen_s, method=True, type='many2one', relation='product.nomenclature', string='Family', fnct_search=_search_nomen_s, multi="nom_s"),
        'nomen_manda_3_s': fields.function(_get_nomen_s, method=True, type='many2one', relation='product.nomenclature', string='Root', fnct_search=_search_nomen_s, multi="nom_s"),
        'nomen_name': fields.function(_get_nomen_name, method=True, type='char', string='Nomenclature level', readonly=True),
        'product_uom_id': fields.many2one('product.uom', string='UoM', readonly=True),
        'frequency_id': fields.many2one('stock.frequence', string='Frequency', readonly=True),
        'order_qty': fields.float(digits=(16,2), string='Order Qty', readonly=True),
        'total_stock': fields.function(_get_stock, method=True, type='float', string='Total stock', readonly=True, multi='stock'),
        'stock': fields.function(_get_stock, method=True, type='float', string='Stock (stock & children)', readonly=True, multi='stock'),
        'intermediate_stock': fields.function(_get_stock, method=True, type='float', string='ISi stocks', readonly=True, multi='stock'),
        'consumption_stock': fields.function(_get_stock, method=True, type='float', string='CUi stocks', readonly=True, multi='stock'),
        'amc': fields.function(_get_stock, method=True, type='float', string='AMC', readonly=True, multi='stock'),
        'moh': fields.function(_get_stock, method=True, type='float', string='MoH', readonly=True, multi='stock'),
        'fmc': fields.function(_get_stock, method=True, type='float', string='FMC', readonly=True, multi='stock'),
    }
    
    def init(self, cr):
        '''
        Creates the SQL view on database
        '''
        tools.drop_view_if_exists(cr, 'auto_supply_rules_report')
        cr.execute("""
            CREATE OR REPLACE view auto_supply_rules_report AS (
                SELECT 
                    row_number() OVER(ORDER BY line.product_id) AS id,
                    min.id AS rule_id,
                    line.product_id AS product_id,
                    min.location_id AS location_id,
                    line.product_uom_id AS product_uom_id,
                    min.frequence_id AS frequency_id,
                    line.product_qty AS order_qty,
                    prod.default_code AS product_reference,
                    temp.name AS product_name,
                    temp.nomen_manda_0 AS nomen_manda_0,
                    temp.nomen_manda_1 AS nomen_manda_1,
                    temp.nomen_manda_2 AS nomen_manda_2,
                    temp.nomen_manda_3 AS nomen_manda_3
                FROM
                    stock_warehouse_automatic_supply min
                    LEFT JOIN
                        stock_warehouse_automatic_supply_line line
                        ON
                        line.supply_id = min.id
                    LEFT JOIN
                        product_product prod
                        ON
                        line.product_id = prod.id
                    LEFT JOIN
                        product_template temp
                        ON
                        prod.product_tmpl_id = temp.id
            )
            """)
    
auto_supply_rules_report()

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: