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

243.3.31 by Quentin THEURET
UF-404 [ADD] Added the scheduler and unit tests on threshold_value module
1
# -*- coding: utf-8 -*-
2
##############################################################################
3
#
4
#    OpenERP, Open Source Management Solution
5
#    Copyright (C) 2011 TeMPO Consulting, MSF 
6
#
7
#    This program is free software: you can redistribute it and/or modify
8
#    it under the terms of the GNU Affero General Public License as
9
#    published by the Free Software Foundation, either version 3 of the
10
#    License, or (at your option) any later version.
11
#
12
#    This program is distributed in the hope that it will be useful,
13
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
#    GNU Affero General Public License for more details.
16
#
17
#    You should have received a copy of the GNU Affero General Public License
18
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
#
20
##############################################################################
21
827.7.16 by Quentin THEURET
UF-1069 [FIX] Replenishment rules report : Move safety stock value from order cycle to order cycle line in sql_view
22
from osv import osv
243.3.31 by Quentin THEURET
UF-404 [ADD] Added the scheduler and unit tests on threshold_value module
23
from tools.translate import _
24
25
import pooler
26
import netsvc
827.7.16 by Quentin THEURET
UF-1069 [FIX] Replenishment rules report : Move safety stock value from order cycle to order cycle line in sql_view
27
import time
243.3.31 by Quentin THEURET
UF-404 [ADD] Added the scheduler and unit tests on threshold_value module
28
29
class procurement_order(osv.osv):
30
    _name = 'procurement.order'
31
    _inherit = 'procurement.order'
32
    
781.10.4 by Quentin THEURET
UF-952 [IMP] All batch_id on method definition
33
    def run_threshold_value(self, cr, uid, use_new_cursor=False, batch_id=False, context=None):
243.3.31 by Quentin THEURET
UF-404 [ADD] Added the scheduler and unit tests on threshold_value module
34
        '''
35
        Creates procurement for products where real stock is under threshold value
36
        '''
706 by jf
[FIX] context None
37
38
        if context is None:
39
            context = {}
40
243.3.31 by Quentin THEURET
UF-404 [ADD] Added the scheduler and unit tests on threshold_value module
41
        if use_new_cursor:
42
            cr = pooler.get_db(use_new_cursor).cursor()
43
            
44
        request_obj = self.pool.get('res.request')
45
        threshold_obj = self.pool.get('threshold.value')
46
        proc_obj = self.pool.get('procurement.order')
47
        
48
        threshold_ids = threshold_obj.search(cr, uid, [], context=context)
49
                
50
        created_proc = []
51
        report = []
52
        report_except = 0
781.10.5 by Quentin THEURET
UF-952 [IMP] Add hook and menu to see associated requests to procurement batches
53
        start_date = time.strftime('%Y-%m-%d %H:%M:%S')
243.3.31 by Quentin THEURET
UF-404 [ADD] Added the scheduler and unit tests on threshold_value module
54
        
55
        wf_service = netsvc.LocalService("workflow")
56
        
57
        for threshold in threshold_obj.browse(cr, uid, threshold_ids, context=context):
827.7.30 by Quentin THEURET
UF-1069 [IMP] Threshold value : Don't recompute threshold value on scheduler run
58
            c = context.copy()
827.7.32 by Quentin THEURET
UF-1069 [IMP] Threshold value : Compute the quantity to ordre according to location level and children levels
59
            c.update({'location': threshold.location_id.id, 'compute_child': True})
827.7.30 by Quentin THEURET
UF-1069 [IMP] Threshold value : Don't recompute threshold value on scheduler run
60
            line_ids = self.pool.get('threshold.value.line').search(cr, uid, [('threshold_value_id', '=', threshold.id)], context=c)
61
            for line in self.pool.get('threshold.value.line').browse(cr, uid, line_ids, context=c):
559.30.8 by Quentin THEURET
UF-873 [IMP] Rework the scheduler code
62
                if line.threshold_value >= line.product_id.virtual_available and line.product_qty > 0.00:
63
                    proc_id = proc_obj.create(cr, uid, {
64
                                        'name': _('Threshold value: %s') % (threshold.name,),
65
                                        'origin': threshold.name,
1994.4.14 by jf
UTP-124 [IMP] Replenishmenty Rules Order Cycle and Theshold Computed Values Contain Data on Main Parameters
66
                                        'date_planned': line.required_date or time.strftime('%Y-%m-%d %H:%M:%S'),
559.30.8 by Quentin THEURET
UF-873 [IMP] Rework the scheduler code
67
                                        'product_id': line.product_id.id,
68
                                        'product_qty': line.product_qty,
69
                                        'product_uom': line.product_id.uom_id.id,
70
                                        'location_id': threshold.location_id.id,
71
                                        'procure_method': 'make_to_order',
72
                    })
73
                    wf_service.trg_validate(uid, 'procurement.order', proc_id, 'button_confirm', cr)
74
                    wf_service.trg_validate(uid, 'procurement.order', proc_id, 'button_check', cr)
243.3.31 by Quentin THEURET
UF-404 [ADD] Added the scheduler and unit tests on threshold_value module
75
                    
559.30.8 by Quentin THEURET
UF-873 [IMP] Rework the scheduler code
76
                    created_proc.append(proc_id)
781.10.2 by Quentin THEURET
UF-952 [FIX] Add a hook to display created documents on request
77
        
78
        created_doc = '''################################
79
Created documents : \n'''
243.3.31 by Quentin THEURET
UF-404 [ADD] Added the scheduler and unit tests on threshold_value module
80
                    
81
        for proc in proc_obj.browse(cr, uid, created_proc):
82
            if proc.state == 'exception':
83
                report.append('PROC %d: from stock - %3.2f %-5s - %s' % \
84
                               (proc.id, proc.product_qty, proc.product_uom.name,
85
                                proc.product_id.name,))
86
                report_except += 1
781.10.2 by Quentin THEURET
UF-952 [FIX] Add a hook to display created documents on request
87
            elif proc.purchase_id:
88
                created_doc += "    * %s => %s \n" % (proc.name, proc.purchase_id.name)
243.3.31 by Quentin THEURET
UF-404 [ADD] Added the scheduler and unit tests on threshold_value module
89
                
781.10.5 by Quentin THEURET
UF-952 [IMP] Add hook and menu to see associated requests to procurement batches
90
        end_date = time.strftime('%Y-%m-%d %H:%M:%S')
243.3.31 by Quentin THEURET
UF-404 [ADD] Added the scheduler and unit tests on threshold_value module
91
                
92
        summary = '''Here is the procurement scheduling report for Threshold values
93
94
        Start Time: %s
95
        End Time: %s
96
        Total Procurements processed: %d
97
        Procurements with exceptions: %d
781.10.5 by Quentin THEURET
UF-952 [IMP] Add hook and menu to see associated requests to procurement batches
98
        \n %s \n  Exceptions: \n'''% (start_date, end_date, len(created_proc), report_except, len(created_proc) > 0 and created_doc or '')
243.3.31 by Quentin THEURET
UF-404 [ADD] Added the scheduler and unit tests on threshold_value module
99
        summary += '\n'.join(report)
781.10.5 by Quentin THEURET
UF-952 [IMP] Add hook and menu to see associated requests to procurement batches
100
        if batch_id:
101
            self.pool.get('procurement.batch.cron').write(cr, uid, batch_id, {'last_run_on': time.strftime('%Y-%m-%d %H:%M:%S')})
102
            old_request = request_obj.search(cr, uid, [('batch_id', '=', batch_id), ('name', '=', 'Procurement Processing Report (Threshold values).')])
103
            request_obj.write(cr, uid, old_request, {'batch_id': False})
243.3.31 by Quentin THEURET
UF-404 [ADD] Added the scheduler and unit tests on threshold_value module
104
        req_id = request_obj.create(cr, uid,
105
                {'name': "Procurement Processing Report (Threshold values).",
106
                 'act_from': uid,
107
                 'act_to': uid,
781.10.5 by Quentin THEURET
UF-952 [IMP] Add hook and menu to see associated requests to procurement batches
108
                 'batch_id': batch_id,
243.3.31 by Quentin THEURET
UF-404 [ADD] Added the scheduler and unit tests on threshold_value module
109
                 'body': summary,
110
                })
781.10.2 by Quentin THEURET
UF-952 [FIX] Add a hook to display created documents on request
111
        # UF-952 : Requests should be in consistent state
112
#        if req_id:
113
#            request_obj.request_send(cr, uid, [req_id])
243.3.31 by Quentin THEURET
UF-404 [ADD] Added the scheduler and unit tests on threshold_value module
114
        
115
        if use_new_cursor:
116
            cr.commit()
117
            cr.close()
118
            
119
        return {}
120
121
procurement_order()
122
123
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: