[REMOVE]: mx.Date from trunk
[odoo/odoo.git] / addons / mrp / report / workcenter_load.py
index 820654b..15e94bd 100644 (file)
@@ -1,41 +1,43 @@
-# -*- encoding: utf-8 -*-
+# -*- coding: utf-8 -*-
 ##############################################################################
 #
-#    OpenERP, Open Source Management Solution  
-#    Copyright (C) 2004-2008 Tiny SPRL (<http://tiny.be>). All Rights Reserved
-#    $Id$
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
 #
 #    This program is free software: you can redistribute it and/or modify
-#    it under the terms of the GNU General Public License as published by
-#    the Free Software Foundation, either version 3 of the License, or
-#    (at your option) any later version.
+#    it under the terms of the GNU Affero General Public License as
+#    published by the Free Software Foundation, either version 3 of the
+#    License, or (at your option) any later version.
 #
 #    This program is distributed in the hope that it will be useful,
 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-#    GNU General Public License for more details.
+#    GNU Affero General Public License for more details.
 #
-#    You should have received a copy of the GNU General Public License
+#    You should have received a copy of the GNU Affero General Public License
 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 ##############################################################################
 
-from report.render import render 
+from report.render import render
 from report.interface import report_int
 from pychart import *
 from mx.DateTime import *
+import time
+from datetime import datetime
+from dateutil.relativedelta import relativedelta
+
 from report.misc import choice_colors
-import time, mx
+
 import random
 import StringIO
 
 
 theme.use_color = 1
-#theme.scale = 2
 random.seed(0)
 
 #
-# Pinky: Bad code, seems buggy, TO CHECK !
+# TODO: Bad code, seems buggy, TO CHECK !
 #
 
 class external_pdf(render):
@@ -43,7 +45,7 @@ class external_pdf(render):
         render.__init__(self)
         self.pdf = pdf
         self.output_type='pdf'
-        
+
     def _render(self):
         return self.pdf
 
@@ -64,8 +66,8 @@ class report_custom(report_int):
                 months = {1:"January",2:"February",3:"March",4:"April",5:"May",6:"June",7:"July",8:"August",9:"September",10:"October",11:"November",12:"December"}
                 dates[i] = {
                     'name' :months[month],
-                    'start':(Date(year, month, 2) + RelativeDateTime(day=1)).strftime('%Y-%m-%d'),
-                    'stop' :(Date(year, month, 2) + RelativeDateTime(day=-1)).strftime('%Y-%m-%d'),
+                    'start':(datetime.date(year, month, 2) + relativedelta(day=1)).strftime('%Y-%m-%d'),
+                    'stop' :(datetime.date(year, month, 2) + relativedelta(day=-1)).strftime('%Y-%m-%d'),
                 }
             return dates
         elif time_unit == 'week':
@@ -92,25 +94,26 @@ class report_custom(report_int):
                     'start':i.strftime('%Y-%m-%d'),
                     'stop' :i.strftime('%Y-%m-%d'),
                 }
-                i = i + RelativeDateTime(days=+1)
+                i = i + relativedelta(days=+1)
             return dates
         return {}
 
     def create(self, cr, uid, ids, datas, context={}):
         assert len(ids), 'You should provide some ids!'
         colors = choice_colors(len(ids))
-        ids_str = ','.join(map(str, ids))
         cr.execute(
-            "SELECT MAX(date_planned) AS stop "\
+            "SELECT MAX(mrp_production.date_planned) AS stop,MIN(mrp_production.date_planned) AS start "\
             "FROM mrp_workcenter, mrp_production, mrp_production_workcenter_line "\
             "WHERE mrp_production_workcenter_line.production_id=mrp_production.id "\
             "AND mrp_production_workcenter_line.workcenter_id=mrp_workcenter.id "\
             "AND mrp_production.state NOT IN ('cancel','done') "\
-            "AND mrp_workcenter.id IN (%s)" % ids_str)
+            "AND mrp_workcenter.id IN %s",(tuple(ids),))
         res = cr.dictfetchone()
         if not res['stop']:
-            res['stop'] = time.strftime('%Y-%m-%d')
-        dates = self._compute_dates(datas['form']['time_unit'], time.strftime('%Y-%m-%d'), res['stop'][:10])
+            res['stop'] = time.strftime('%Y-%m-%d %H:%M:%S')
+        if not res['start']:
+            res['start'] = time.strftime('%Y-%m-%d %H:%M:%S')
+        dates = self._compute_dates(datas['form']['time_unit'], res['start'][:10], res['stop'][:10])
         dates_list = dates.keys()
         dates_list.sort()
         x_index = []
@@ -134,9 +137,9 @@ class report_custom(report_int):
 
         # select workcenters
         cr.execute(
-            "SELECT id, name FROM mrp_workcenter " \
-            "WHERE id in (%s) "\
-            "ORDER BY mrp_workcenter.id" % ids_str)
+            "SELECT mw.id, rs.name FROM mrp_workcenter mw, resource_resource rs " \
+            "WHERE mw.id IN %s and mw.resource_id=rs.id " \
+            "ORDER BY mw.id" ,(tuple(ids),))
         workcenters = cr.dictfetchall()
 
         data = []
@@ -144,13 +147,14 @@ class report_custom(report_int):
             vals = []
             for workcenter in workcenters:
                 cr.execute("SELECT SUM(mrp_production_workcenter_line.hour) AS hours, SUM(mrp_production_workcenter_line.cycle) AS cycles, \
-                                mrp_workcenter.name AS name, mrp_workcenter.id AS id \
-                            FROM mrp_production_workcenter_line, mrp_production, mrp_workcenter \
+                                resource_resource.name AS name, mrp_workcenter.id AS id \
+                            FROM mrp_production_workcenter_line, mrp_production, mrp_workcenter, resource_resource \
                             WHERE (mrp_production_workcenter_line.production_id=mrp_production.id) \
                                 AND (mrp_production_workcenter_line.workcenter_id=mrp_workcenter.id) \
-                                AND (mrp_workcenter.id=%d) \
+                                AND (mrp_workcenter.resource_id=resource_resource.id) \
+                                AND (mrp_workcenter.id=%s) \
                                 AND (mrp_production.date_planned BETWEEN %s AND %s) \
-                            GROUP BY mrp_production_workcenter_line.workcenter_id, mrp_workcenter.name, mrp_workcenter.id \
+                            GROUP BY mrp_production_workcenter_line.workcenter_id, resource_resource.name, mrp_workcenter.id \
                             ORDER BY mrp_workcenter.id", (workcenter['id'], dates[date]['start'] + ' 00:00:00', dates[date]['stop'] + ' 23:59:59'))
                 res = cr.dictfetchall()
                 if not res:
@@ -160,7 +164,7 @@ class report_custom(report_int):
                         vals.append(res[0]['cycles'] or 0.0)
                     else:
                         vals.append(res[0]['hours'] or 0.0)
-                    
+
             toto = [dates[date]['name']]
             for val in vals:
                 toto.append(val)
@@ -173,9 +177,8 @@ class report_custom(report_int):
             ar.add_plot(bar_plot.T(label=workcenter['name'], data=data, fill_style=f, hcol=workcenter_num+1, cluster=(workcenter_num, len(res))))
             workcenter_num += 1
 
-        #plot = bar_plot.T(label=workcenter['name'], data=data, hcol=1, fill_style=fill_style.white, cluster=(color_index,len(ids)))
-        if (len(data[0]) <= 1):
-            ar = self._empty_graph(data[0][0])
+        if (not data) or (len(data[0]) <= 1):
+            ar = self._empty_graph(time.strftime('%Y-%m-%d'))
         ar.draw(can)
         # close canvas so that the file is written to "disk"
         can.close()
@@ -183,7 +186,7 @@ class report_custom(report_int):
         self.obj.render()
         pdf_string.close()
         return (self.obj.pdf, 'pdf')
-    
+
     def _empty_graph(self, date):
         data = [[date, 0]]
         ar = area.T(x_coord = category_coord.T(data, 0), y_range = (0, None),