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

« back to all changes in this revision

Viewing changes to threshold_value/scheduler.py

UF-663: [IMP] first version

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
import pooler
28
28
import netsvc
29
29
 
30
 
from mx.DateTime import *
 
30
 
31
31
 
32
32
class procurement_order(osv.osv):
33
33
    _name = 'procurement.order'
34
34
    _inherit = 'procurement.order'
35
35
    
36
 
    def run_threshold_value(self, cr, uid, use_new_cursor=False, batch_id=False, context=None):
 
36
    def run_threshold_value(self, cr, uid, use_new_cursor=False, context={}):
37
37
        '''
38
38
        Creates procurement for products where real stock is under threshold value
39
39
        '''
40
 
 
41
 
        if context is None:
42
 
            context = {}
43
 
 
44
40
        if use_new_cursor:
45
41
            cr = pooler.get_db(use_new_cursor).cursor()
46
42
            
47
43
        request_obj = self.pool.get('res.request')
48
44
        threshold_obj = self.pool.get('threshold.value')
49
45
        proc_obj = self.pool.get('procurement.order')
 
46
        product_obj = self.pool.get('product.product')
50
47
        
51
48
        threshold_ids = threshold_obj.search(cr, uid, [], context=context)
52
49
                
53
50
        created_proc = []
54
51
        report = []
55
52
        report_except = 0
56
 
        start_date = time.strftime('%Y-%m-%d %H:%M:%S')
 
53
        start_date = datetime.now()
57
54
        
58
55
        wf_service = netsvc.LocalService("workflow")
59
56
        
60
57
        for threshold in threshold_obj.browse(cr, uid, threshold_ids, context=context):
61
 
            for line in threshold.line_ids:
62
 
                if line.threshold_value >= line.product_id.virtual_available and line.product_qty > 0.00:
 
58
            # Set location_id in context for tre AMC calculation
 
59
            context.update({'location_id': threshold.location_id and threshold.location_id.id or False})
 
60
            
 
61
            # Set the product list according to the category or product defined in the rule
 
62
            products = []
 
63
            if threshold.category_id:
 
64
                products.extend(product_obj.search(cr, uid, [('categ_id', '=', threshold.category_id.id)], context=context))
 
65
            elif threshold.product_id:
 
66
                products.append(threshold.product_id.id)
 
67
            
 
68
            # Set different data by products for calculation
 
69
            for product_id in products:
 
70
                product = product_obj.browse(cr, uid, product_id, context=context)
 
71
                amc = product_obj.compute_amc(cr, uid, product_id, context=context)
 
72
                
 
73
                # Set lead time according to choices in threshold rule (supplier or manual lead time)
 
74
                lead_time = threshold.supplier_lt and float(product.seller_delay)/30.0 or threshold.lead_time
 
75
                
 
76
                # Compute the threshold value
 
77
                threshold_value = threshold.threshold_manual_ok and threshold.threshold_value or amc * (lead_time + threshold.safety_month)
 
78
                threshold_value = self.pool.get('product.uom')._compute_qty(cr, uid, product.uom_id.id, threshold_value, product.uom_id.id)
 
79
                
 
80
                # Check if the quantity in stock needs a supply of products or not
 
81
                if product.virtual_available <= threshold_value:
 
82
                    # Compute the quantity to re-order
 
83
                    qty_to_order = threshold.qty_order_manual_ok and threshold.qty_to_order \
 
84
                                    or amc * (threshold.frequency + lead_time + threshold.safety_month)\
 
85
                                    - product.qty_available + product.incoming_qty - product.outgoing_qty 
 
86
                    qty_to_order = self.pool.get('product.uom')._compute_qty(cr, uid, threshold.uom_id and \
 
87
                                                                             threshold.uom_id.id or product.uom_id.id, qty_to_order,\
 
88
                                                                             product.uom_id.id)
 
89
                    
63
90
                    proc_id = proc_obj.create(cr, uid, {
64
91
                                        'name': _('Threshold value: %s') % (threshold.name,),
65
92
                                        'origin': threshold.name,
66
93
                                        '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,
 
94
                                        'product_id': product.id,
 
95
                                        'product_qty': qty_to_order,
 
96
                                        'product_uom': product.uom_id.id,
70
97
                                        'location_id': threshold.location_id.id,
71
98
                                        'procure_method': 'make_to_order',
72
99
                    })
74
101
                    wf_service.trg_validate(uid, 'procurement.order', proc_id, 'button_check', cr)
75
102
                    
76
103
                    created_proc.append(proc_id)
77
 
        
78
 
        created_doc = '''################################
79
 
Created documents : \n'''
 
104
                
 
105
                
80
106
                    
81
107
        for proc in proc_obj.browse(cr, uid, created_proc):
82
108
            if proc.state == 'exception':
84
110
                               (proc.id, proc.product_qty, proc.product_uom.name,
85
111
                                proc.product_id.name,))
86
112
                report_except += 1
87
 
            elif proc.purchase_id:
88
 
                created_doc += "    * %s => %s \n" % (proc.name, proc.purchase_id.name)
89
113
                
90
 
        end_date = time.strftime('%Y-%m-%d %H:%M:%S')
 
114
        end_date = datetime.now()
91
115
                
92
116
        summary = '''Here is the procurement scheduling report for Threshold values
93
117
 
95
119
        End Time: %s
96
120
        Total Procurements processed: %d
97
121
        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 '')
 
122
        \n'''% (start_date, end_date, len(created_proc), report_except)
99
123
        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
124
        req_id = request_obj.create(cr, uid,
105
125
                {'name': "Procurement Processing Report (Threshold values).",
106
126
                 'act_from': uid,
107
127
                 'act_to': uid,
108
 
                 'batch_id': batch_id,
109
128
                 'body': summary,
110
129
                })
111
 
        # UF-952 : Requests should be in consistent state
112
 
#        if req_id:
113
 
#            request_obj.request_send(cr, uid, [req_id])
 
130
        if req_id:
 
131
            request_obj.request_send(cr, uid, [req_id])
114
132
        
115
133
        if use_new_cursor:
116
134
            cr.commit()