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

« back to all changes in this revision

Viewing changes to procurement_auto/scheduler.py

UF-663: [IMP] first version

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
    _name = 'procurement.order'
34
34
    _inherit = 'procurement.order'
35
35
    
36
 
    def run_automatic_supply(self, cr, uid, use_new_cursor=False, batch_id=False, context=None):
 
36
    def run_automatic_supply(self, cr, uid, use_new_cursor=False, context={}):
37
37
        '''
38
38
        Create procurement on fixed date
39
39
        '''
45
45
        proc_obj = self.pool.get('procurement.order')
46
46
        freq_obj = self.pool.get('stock.frequence')
47
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
        start_date = datetime.now()
 
49
        auto_sup_ids = auto_sup_obj.search(cr, uid, [('next_date', '<=', start_date.strftime('%Y-%m-%d'))])
50
50
        
51
51
        created_proc = []
52
52
        report = []
64
64
            else:
65
65
                location_id = auto_sup.location_id.id
66
66
               
67
 
            for line in auto_sup.line_ids:
68
 
                proc_id = self.create_proc_order(cr, uid, auto_sup, line.product_id,
69
 
                                                 line.product_uom_id.id, line.product_qty,
70
 
                                                 location_id, cache=cache, context=context)
 
67
            if auto_sup.product_id:
 
68
                proc_id = self.create_proc_order(cr, uid, auto_sup, auto_sup.product_id, auto_sup.product_uom_id.id, 
 
69
                             auto_sup.product_qty, location_id, cache=cache, context=context)
71
70
                if proc_id:
72
71
                    created_proc.append(proc_id)
73
72
            
 
73
            else:
 
74
                for line in auto_sup.line_ids:
 
75
                    proc_id = self.create_proc_order(cr, uid, auto_sup, line.product_id,
 
76
                                                     line.product_uom_id.id, line.product_qty,
 
77
                                                     location_id, cache=cache, context=context)
 
78
                    if proc_id:
 
79
                        created_proc.append(proc_id)
 
80
            
74
81
            if auto_sup.frequence_id:
75
82
                freq_obj.write(cr, uid, auto_sup.frequence_id.id, {'last_run': start_date.strftime('%Y-%m-%d')})
76
83
 
77
 
        created_doc = '''################################
78
 
Created documents : \n'''
79
84
                    
80
85
        for proc in proc_obj.browse(cr, uid, created_proc):
81
86
            if proc.state == 'exception':
83
88
                               (proc.id, proc.product_qty, proc.product_uom.name,
84
89
                                proc.product_id.name,))
85
90
                report_except += 1
86
 
            elif proc.purchase_id:
87
 
                created_doc += "    * %s => %s \n" % (proc.name, proc.purchase_id.name)
88
91
                
89
 
        end_date = time.strftime('%Y-%m-%d %H:%M:%S')
 
92
        end_date = datetime.now()
90
93
                
91
94
        summary = '''Here is the procurement scheduling report for Automatic Supplies
92
95
 
94
97
        End Time: %s
95
98
        Total Procurements processed: %d
96
99
        Procurements with exceptions: %d
97
 
        
98
 
        \n %s \n  Exceptions: \n'''% (start_date, end_date, len(created_proc), report_except, len(created_proc) > 0 and created_doc or '')
99
 
        
 
100
        \n'''% (start_date, end_date, len(created_proc), report_except)
100
101
        summary += '\n'.join(report)
101
 
        if batch_id:
102
 
            self.pool.get('procurement.batch.cron').write(cr, uid, batch_id, {'last_run_on': time.strftime('%Y-%m-%d %H:%M:%S')})
103
 
            old_request = request_obj.search(cr, uid, [('batch_id', '=', batch_id), ('name', '=', 'Procurement Processing Report (Automatic supplies).')])
104
 
            request_obj.write(cr, uid, old_request, {'batch_id': False})
105
102
        req_id = request_obj.create(cr, uid,
106
 
                {'name': "Procurement Processing Report (Automatic supplies).",
 
103
                {'name': "Procurement Processing Report.",
107
104
                 'act_from': uid,
108
105
                 'act_to': uid,
109
 
                 'batch_id': batch_id,
110
106
                 'body': summary,
111
107
                })
112
 
        # UF-952 : Requests should be in consistent state
113
 
#        if req_id:
114
 
#            request_obj.request_send(cr, uid, [req_id])
 
108
        if req_id:
 
109
            request_obj.request_send(cr, uid, [req_id])
115
110
 
116
111
        if use_new_cursor:
117
112
            cr.commit()
119
114
            
120
115
        return {}
121
116
    
122
 
    def create_proc_order(self, cr, uid, auto_sup, product_id, product_uom, qty, location_id, cache=None, context=None):
 
117
    def create_proc_order(self, cr, uid, auto_sup, product_id, product_uom, qty, location_id, cache={}, context={}):
123
118
        '''
124
119
        Creates a procurement order for a product and a location
125
120
        '''
128
123
        wf_service = netsvc.LocalService("workflow")
129
124
        report = []
130
125
        proc_id = False
131
 
        if cache is None:
132
 
            cache = {}
133
126
        
134
127
        # Enter the stock location in cache to know which products has been already replenish for this location
135
128
        if not cache.get(location_id, False):