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

« back to all changes in this revision

Viewing changes to procurement_auto/scheduler.py

UF-73: [MERGE] Merge with unifield-wm branch

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
#
20
20
##############################################################################
21
21
 
22
 
from osv import osv
 
22
from osv import osv, fields
23
23
from datetime import datetime
24
24
from tools.translate import _
25
25
 
28
28
import netsvc
29
29
 
30
30
 
 
31
# Cache for product/location
 
32
cache = {}
31
33
 
32
34
class procurement_order(osv.osv):
33
35
    _name = 'procurement.order'
34
36
    _inherit = 'procurement.order'
35
37
    
36
 
    def run_automatic_supply(self, cr, uid, use_new_cursor=False, batch_id=False, context=None):
 
38
    def run_automatic_supply(self, cr, uid, use_new_cursor=False, context={}):
37
39
        '''
38
40
        Create procurement on fixed date
39
41
        '''
43
45
        request_obj = self.pool.get('res.request')
44
46
        auto_sup_obj = self.pool.get('stock.warehouse.automatic.supply')
45
47
        proc_obj = self.pool.get('procurement.order')
46
 
        freq_obj = self.pool.get('stock.frequence')
47
 
 
48
 
        start_date = time.strftime('%Y-%m-%d %H:%M:%S')
49
 
        auto_sup_ids = auto_sup_obj.search(cr, uid, [('next_date', '<=', datetime.now())])
 
48
        
 
49
        auto_sup_ids = auto_sup_obj.search(cr, uid, [('next_date', '=', datetime.today().strftime('%Y-%m-%d')), ('product_id', '=', False)])
50
50
        
51
51
        created_proc = []
52
52
        report = []
53
53
        report_except = 0
54
54
        
 
55
        start_date = datetime.now()
 
56
        
55
57
        # We start with only category Automatic Supply
56
58
        for auto_sup in auto_sup_obj.browse(cr, uid, auto_sup_ids):
57
59
            # We define the replenish location
60
62
                location_id = auto_sup.warehouse_id.lot_input_id.id
61
63
            else:
62
64
                location_id = auto_sup.location_id.id
63
 
               
64
 
            # We create a procurement order for each line of the rule
 
65
                
65
66
            for line in auto_sup.line_ids:
66
67
                proc_id = self.create_proc_order(cr, uid, auto_sup, line.product_id,
67
68
                                                 line.product_uom_id.id, line.product_qty,
68
69
                                                 location_id, context=context)
69
 
                # If a procurement has been created, add it to the list
70
70
                if proc_id:
71
71
                    created_proc.append(proc_id)
72
 
            
73
 
            # Update the frequence to save the date of the last run
74
 
            if auto_sup.frequence_id:
75
 
                freq_obj.write(cr, uid, auto_sup.frequence_id.id, {'last_run': datetime.now()})
76
 
 
77
 
 
78
 
        ###
79
 
        # Add created document and exception in a request
80
 
        ###
81
 
        created_doc = '''################################
82
 
Created documents : \n'''
 
72
        
 
73
        # Next, for one product automatic supply
 
74
        auto_sup_ids = auto_sup_obj.search(cr, uid, [('next_date', '=', datetime.today().strftime('%Y-%m-%d')), ('product_id', '!=', False)])
 
75
        for auto_sup in auto_sup_obj.browse(cr, uid, auto_sup_ids):
 
76
            # We define the replenish location
 
77
            location_id = False
 
78
            if not auto_sup.location_id or not auto_sup.location_id.id:
 
79
                location_id = auto_sup.warehouse_id.lot_input_id.id
 
80
            else:
 
81
                location_id = auto_sup.location_id.id
 
82
                
 
83
            proc_id = self.create_proc_order(cr, uid, auto_sup, auto_sup.product_id, auto_sup.product_uom_id.id, 
 
84
                                             auto_sup.product_qty, location_id, context)
 
85
            if proc_id:
 
86
                created_proc.append(proc_id)
83
87
                    
84
88
        for proc in proc_obj.browse(cr, uid, created_proc):
85
89
            if proc.state == 'exception':
87
91
                               (proc.id, proc.product_qty, proc.product_uom.name,
88
92
                                proc.product_id.name,))
89
93
                report_except += 1
90
 
            elif proc.purchase_id:
91
 
                created_doc += "    * %s => %s \n" % (proc.name, proc.purchase_id.name)
92
94
                
93
 
        end_date = time.strftime('%Y-%m-%d %H:%M:%S')
 
95
        end_date = datetime.now()
94
96
                
95
97
        summary = '''Here is the procurement scheduling report for Automatic Supplies
96
98
 
98
100
        End Time: %s
99
101
        Total Procurements processed: %d
100
102
        Procurements with exceptions: %d
101
 
        
102
 
        \n %s \n  Exceptions: \n'''% (start_date, end_date, len(created_proc), report_except, len(created_proc) > 0 and created_doc or '')
103
 
        
 
103
 
 
104
        Exceptions:\n'''% (start_date, end_date, len(created_proc), report_except)
104
105
        summary += '\n'.join(report)
105
 
        if batch_id:
106
 
            self.pool.get('procurement.batch.cron').write(cr, uid, batch_id, {'last_run_on': time.strftime('%Y-%m-%d %H:%M:%S')})
107
 
            old_request = request_obj.search(cr, uid, [('batch_id', '=', batch_id), ('name', '=', 'Procurement Processing Report (Automatic supplies).')])
108
 
            request_obj.write(cr, uid, old_request, {'batch_id': False})
109
106
        request_obj.create(cr, uid,
110
 
                {'name': "Procurement Processing Report (Automatic supplies).",
 
107
                {'name': "Procurement Processing Report.",
111
108
                 'act_from': uid,
112
109
                 'act_to': uid,
113
 
                 'batch_id': batch_id,
114
110
                 'body': summary,
115
111
                })
116
 
        # UF-952 : Requests should be in consistent state
117
 
#        if req_id:
118
 
#            request_obj.request_send(cr, uid, [req_id])
119
 
 
 
112
        
120
113
        if use_new_cursor:
121
114
            cr.commit()
122
115
            cr.close()
123
116
            
124
117
        return {}
125
118
    
126
 
    def create_proc_order(self, cr, uid, auto_sup, product_id, product_uom, qty, location_id, context=None):
 
119
    def create_proc_order(self, cr, uid, auto_sup, product_id, product_uom, qty, location_id, context={}):
127
120
        '''
128
121
        Creates a procurement order for a product and a location
129
122
        '''
130
123
        proc_obj = self.pool.get('procurement.order')
131
124
        auto_sup_obj = self.pool.get('stock.warehouse.automatic.supply')
132
125
        wf_service = netsvc.LocalService("workflow")
 
126
        report = []
133
127
        proc_id = False
134
128
        
135
 
        if product_id:
 
129
        # Enter the stock location in cache to know which products has been already replenish for this location
 
130
        if not cache.get(location_id, False):
 
131
            cache.update({location_id: []})
 
132
            
 
133
        if product_id.id not in cache.get(location_id):
136
134
            newdate = datetime.today()
137
135
            proc_id = proc_obj.create(cr, uid, {
138
136
                                        'name': _('Automatic Supply: %s') % (auto_sup.name,),
147
145
            wf_service.trg_validate(uid, 'procurement.order', proc_id, 'button_confirm', cr)
148
146
            wf_service.trg_validate(uid, 'procurement.order', proc_id, 'button_check', cr)
149
147
            auto_sup_obj.write(cr, uid, [auto_sup.id], {'procurement_id': proc_id}, context=context)
 
148
            
 
149
            # Fill the cache
 
150
            cache.get(location_id).append(product_id.id)
150
151
        
151
152
        return proc_id
152
 
    
153
 
    def _hook_product_type_consu(self, cr, uid, *args, **kwargs):
154
 
        '''
155
 
        kwargs['op'] is the current min/max rule
156
 
        '''
157
 
        return True
158
153
        
159
154
procurement_order()
160
155
 
161
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
156
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
 
 
b'\\ No newline at end of file'