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

« back to all changes in this revision

Viewing changes to threshold_value/scheduler.py

  • Committer: jf
  • Date: 2011-05-16 09:55:17 UTC
  • mfrom: (129.1.1 unifield-wm)
  • Revision ID: jf@tempo4-20110516095517-giuzv2mouka39jb8
UF-270 Advance return in a currency that is not the functional currency

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) 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
 
 
22
 
from osv import osv
23
 
from tools.translate import _
24
 
 
25
 
import pooler
26
 
import netsvc
27
 
import time
28
 
 
29
 
class procurement_order(osv.osv):
30
 
    _name = 'procurement.order'
31
 
    _inherit = 'procurement.order'
32
 
    
33
 
    def run_threshold_value(self, cr, uid, use_new_cursor=False, batch_id=False, context=None):
34
 
        '''
35
 
        Creates procurement for products where real stock is under threshold value
36
 
        '''
37
 
 
38
 
        if context is None:
39
 
            context = {}
40
 
 
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
53
 
        start_date = time.strftime('%Y-%m-%d %H:%M:%S')
54
 
        
55
 
        wf_service = netsvc.LocalService("workflow")
56
 
        
57
 
        for threshold in threshold_obj.browse(cr, uid, threshold_ids, context=context):
58
 
            c = context.copy()
59
 
            c.update({'location': threshold.location_id.id, 'compute_child': True})
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):
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,
66
 
                                        'date_planned': time.strftime('%Y-%m-%d %H:%M:%S'),
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)
75
 
                    
76
 
                    created_proc.append(proc_id)
77
 
        
78
 
        created_doc = '''################################
79
 
Created documents : \n'''
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
87
 
            elif proc.purchase_id:
88
 
                created_doc += "    * %s => %s \n" % (proc.name, proc.purchase_id.name)
89
 
                
90
 
        end_date = time.strftime('%Y-%m-%d %H:%M:%S')
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
98
 
        \n %s \n  Exceptions: \n'''% (start_date, end_date, len(created_proc), report_except, len(created_proc) > 0 and created_doc or '')
99
 
        summary += '\n'.join(report)
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})
104
 
        req_id = request_obj.create(cr, uid,
105
 
                {'name': "Procurement Processing Report (Threshold values).",
106
 
                 'act_from': uid,
107
 
                 'act_to': uid,
108
 
                 'batch_id': batch_id,
109
 
                 'body': summary,
110
 
                })
111
 
        # UF-952 : Requests should be in consistent state
112
 
#        if req_id:
113
 
#            request_obj.request_send(cr, uid, [req_id])
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: