[REF][IMP]GOLEM Resource Report : huge refactoring of report model and data with representation per date, then, resource, then dates, then periods

This commit is contained in:
Fabien BOURGEOIS 2018-05-25 16:32:44 +02:00
parent 66a46d65f6
commit 654d2de6b4
3 changed files with 69 additions and 42 deletions

View File

@ -20,7 +20,9 @@
import time import time
from random import randint from random import randint
from odoo import models, api from odoo import models, fields, api
# FIXME: usage of Odoo fields will be needed for i18n awareness
def get_client_color(partner_number): def get_client_color(partner_number):
""" Get Client Color """ """ Get Client Color """
@ -44,13 +46,13 @@ class GolemResevationReport(models.AbstractModel):
def get_data(self, data): def get_data(self, data):
""" Get Resevation Data """ """ Get Resevation Data """
res = []
domain = [('date_start', '>', data['date_start']), domain = [('date_start', '>', data['date_start']),
('date_stop', '<', data['date_stop']), ('date_stop', '<', data['date_stop']),
('resource_id', 'in', data['resource_ids'])] ('resource_id', 'in', data['resource_ids'])]
reservations = self.env['golem.resource.reservation'].search(domain, order='date_start') reservations = self.env['golem.resource.reservation'].search(domain, order='date_start')
total_reservations = len(reservations) total_reservations = len(reservations)
resources = list(reservations.mapped('resource_id.name')) resources = {r.resource_id.id: r.resource_id.name for r in reservations}
days = sorted(list(set(reservations.mapped('day_start'))))
partner_ids = reservations.mapped('partner_id.id') partner_ids = reservations.mapped('partner_id.id')
partner_colors = {} partner_colors = {}
@ -59,36 +61,47 @@ class GolemResevationReport(models.AbstractModel):
partner_colors[str(partner_id)] = get_client_color(partner_number) partner_colors[str(partner_id)] = get_client_color(partner_number)
partner_number += 1 partner_number += 1
res = {} # List of multi-levels : group by resource, then, day_start
for reservation in reservations: for reservation in reservations:
resource = reservation.resource_id.id
day_start = reservation.day_start
if not resource in res:
res[resource] = {}
if not day_start in res[resource]:
res[resource][day_start] = []
line = { line = {
'name': reservation.name, 'name': reservation.name,
'resource_name': reservation.resource_id.name, 'resource_name': reservation.resource_id.name,
'client': reservation.partner_id.name, 'partner': reservation.partner_id.name,
'date_start': reservation.date_start, 'date_start': reservation.date_start,
'date_stop': reservation.date_stop, 'date_stop': reservation.date_stop,
'day_start': reservation.day_start, 'day_start': reservation.day_start,
'bgcolor': partner_colors[str(reservation.partner_id.id)] 'day_stop': fields.Datetime.from_string(reservation.date_stop).strftime('%Y-%m-%d'),
'bgcolor': partner_colors[str(reservation.partner_id.id)],
'note': reservation.note
} }
res.append(line) res[resource][day_start].append(line)
return res, total_reservations, resources return res, total_reservations, resources, days
@api.model @api.model
def render_html(self, docids, data=None): def render_html(self, docids, data=None):
""" Render HTML """ """ Render HTML """
model = self.env.context.get('active_model') model = self.env.context.get('active_model')
docs = self.env[model].browse(self.env.context.get('active_id')) docs = self.env[model].browse(self.env.context.get('active_id'))
_data, total_reservations, resources = self.get_data(data) _data, total_reservations, resources, days = self.get_data(data)
docargs = { docargs = {
'doc_ids': self.ids, 'doc_ids': self.ids,
'doc_model': model, 'doc_model': model,
'docs': docs, 'docs': docs,
'company': self.env.ref('base.main_company'),
'time': time, 'time': time,
'data': data, 'data': data,
'date_start': data['date_start'], 'date_start': '%s 00:00:00' % data['date_start'],
'date_stop': data['date_stop'], 'date_stop': '%s 23:59:59' % data['date_stop'],
'get_total_reservation': total_reservations, 'total_reservations': total_reservations,
'get_data': _data, 'datas': _data,
'get_resource': resources 'resources': resources,
'days': days
} }
return self.env['report'] \ return self.env['report'] \
.render('golem_resource_report.golem_reservation_report', docargs) .render('golem_resource_report.golem_reservation_report', docargs)

View File

@ -24,32 +24,48 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
<t t-call="report.external_layout"> <t t-call="report.external_layout">
<div class="font"> <div class="font">
<div class="page"> <div class="page">
<br></br> <h3 class="text-center">
<table border="1" width="100%" cellpadding="0" bgcolor="#5F8CA3" > <span t-esc="company.name" /> : <span t-esc="total_reservations" /> reservations<br />
<tbody> <small>
<tr width="100%"> <strong>From Date: </strong>
<td class="text-center" width="100%"> <span t-esc="time.strftime('%d/%m/%Y',time.strptime(date_start,'%Y-%m-%d %H:%M:%S'))"/>
<center><b>Maison Phare toutes les reservations</b><br/> <strong>To Date:</strong>
<b>From Date: </b> <span t-esc="time.strftime('%d/%m/%Y',time.strptime(date_stop,'%Y-%m-%d %H:%M:%S'))"/>
<span t-esc="from_date" /> </small>
<span t-raw="'%s' % date_start if date_start else ''" /> </h3>
<b>To Date:</b> <table class="table table-striped">
<span t-raw="'%s' % date_stop if date_stop else ''" /></center> <thead>
</td> <tr>
<th></th>
<t t-foreach="days" t-as="day">
<th class="text-center" t-esc="time.strftime('%A %d/%m',time.strptime(day,'%Y-%m-%d'))"/>
</t>
</tr> </tr>
</tbody> </thead>
</table>
<table border="1" width="100%" cellpadding="0" bgcolor="#ededed" style="padding: 20px; background-color: #ededed; border-collapse:separate;">
<tbody> <tbody>
<tr t-foreach="get_resource" t-as="resource"> <tr t-foreach="resources" t-as="resource_id">
<td><span t-esc="resource"/></td> <td><span t-esc="resources[resource_id]" /></td>
<t t-foreach="get_data" t-as="data"> <t t-foreach="days" t-as="day">
<t t-if="data['resource_name']==resource"> <td>
<td t-attf-style="background-color:{{data['bgcolor']}}!important;"> <t t-if="datas.get(resource_id, {}).get(day)">
<b>Date :</b><span t-esc="data['day_start']"/><br/> <t t-set="reservations" t-value="datas[resource_id][day]" />
<b>On behalf of :</b><span t-esc="data['client']"/><br/> <t t-foreach="reservations" t-as="resa">
</td> <div t-attf-style="background-color:{{resa['bgcolor']}}!important;border-top: 1px solid #333333;">
</t> <t t-if="resa['day_start']==resa['day_stop']">
<span t-esc="time.strftime('%Hh%M',time.strptime(resa['date_start'],'%Y-%m-%d %H:%M:%S'))"/> -
<span t-esc="time.strftime('%Hh%M',time.strptime(resa['date_stop'],'%Y-%m-%d %H:%M:%S'))"/>
</t>
<t t-else="">
From <span t-esc="time.strftime('%d/%m %Hh%M',time.strptime(resa['date_start'],'%Y-%m-%d %H:%M:%S'))"/><br />
To <span t-esc="time.strftime('%d/%m %Hh%M',time.strptime(resa['date_stop'],'%Y-%m-%d %H:%M:%S'))"/>
</t>
<p t-if="resa.get('note')" t-esc="resa['note']" />
<br />
<em>For <span t-esc="resa['partner']"/></em>
</div>
</t>
</t>
</td>
</t> </t>
</tr> </tr>
</tbody> </tbody>

View File

@ -26,17 +26,15 @@ class GolemResourceReportWizard(models.TransientModel):
_name = "golem.resource.report.wizard" _name = "golem.resource.report.wizard"
resource_ids = fields.Many2many('golem.resource') resource_ids = fields.Many2many('golem.resource')
date_start = fields.Datetime(required=True) date_start = fields.Date(required=True)
date_stop = fields.Datetime(required=True) date_stop = fields.Date(required=True)
@api.multi @api.multi
def print_resource_report(self): def print_resource_report(self):
""" Print Report """ """ Print Report """
self.ensure_one() self.ensure_one()
record = self[0] record = self[0]
start_date = fields.Datetime.from_string(record.date_start) if record.date_start > record.date_stop:
stop_date = fields.Datetime.from_string(record.date_stop)
if start_date > stop_date:
raise ValidationError(_('Stop Date cannot be set before Start Date.')) raise ValidationError(_('Stop Date cannot be set before Start Date.'))
else: else:
data = self.read( data = self.read(