# -*- coding: utf-8 -*-
##############################################################################
#
#    Odoo, Open Source Management Solution
#    Copyright (C) 2016-TODAY Prime Consulting SA, Cape Verde (<http://prime.cv>).
#
##############################################################################
import time
from datetime import datetime, timedelta

from odoo import api, fields, models, _


class ReportDailyReport(models.TransientModel):
    _name = "report.daily.report"

    report_date = fields.Date(string='Date', default=fields.Date.context_today, required=True, copy=False)
	
    @api.multi
    def action_print_daily_report(self):
        return self.env['report'].get_action(self, 'etourism_reports.report_daily_report') 

    @api.multi
    def get_current_bookings(self,today):
        result,booking_state,num_invoices,total_amount,total_discount = [],'',0,0,0
        context = self._context
        return_header = ''
        enterprise_id = self.env['res.users'].browse([self.env.uid])[0].enterprise_id
        company_currency = self.env.user.company_id.currency_id.symbol
        count = 0
        #today = datetime.now().date()
        if enterprise_id:
            self._cr.execute("""select b.id,b.reservation_no,b.num_rooms,pr.name as client_name,b.date_checkin,b.date_checkout,b.total_amount,b.state
,(select count(*) from account_invoice_line l,account_invoice i,product_product p where l.invoice_id=i.id and l.product_id=p.id and p.isroom = true and i.booking_group_id=b.id) as num_invoices
from etourism_booking_group b, res_partner pr
where b.guest_id=pr.id and (b.date_booking='%s' or b.date_checkout='%s') and b.enterprise_id=%s 
order by b.checkin
"""%(today,today,enterprise_id.id))
            result = self._cr.fetchall()
            if result:
                for res in result:
                    if count % 2 == 0:
                        return_header += """<tr style="background-color:#FFFFFF;">"""
                    else:
                        return_header += """<tr style="background-color:#F0F0F0;">"""
                    booking_state = str(res[7])
                    num_invoices = res[8]
                    if num_invoices > 0:
                        self._cr.execute("""select sum(l.price_subtotal) as total_amount,sum(discount) as total_discount
from account_invoice_line l, account_invoice i,product_product p
where i.id=l.invoice_id and p.id=l.product_id and p.isroom=true and i.booking_group_id=%s
"""%(res[0]))
                        result2 = self._cr.fetchall()
                        if result2:
                            for res2 in result2:
                                total_amount = res2[0]
                                total_discount = res2[1]
                    else:
                        total_amount = res[6]
                    if booking_state == 'confirmed':
                        booking_state = _('Confirmed')
                    elif booking_state == 'hosted':
                        booking_state = _('Checkin')
                    elif booking_state == 'checkout':
                        booking_state = _('Checkout')
                    elif booking_state == 'cancelled':
                        booking_state = _('Cancelled')
                    elif booking_state == 'no_show':
                        booking_state = _('No Show')

                    return_header += """<td>"""+ str(res[1]) + """</td>"""
                    return_header += """<td>"""+ str(res[2]) + """</td>"""
                    return_header += """<td>"""+ str(res[3]) + """</td>"""
                    return_header += """<td>"""+ str(res[4]) + """</td>"""
                    return_header += """<td>"""+ str(res[5]) + """</td>"""
                    return_header += """<td>"""+ str(total_amount) + ' ' + company_currency + """</td>"""
                    return_header += """<td>"""+ str(total_discount) + """%</td>"""
                    return_header += """<td>"""+ str(booking_state) + """</td>"""
                    return_header += """</tr>"""
                    count += 1
        return return_header

    @api.multi
    def get_current_guests_stats(self,today,input=''):
        result,num_rooms_enterprise = [],0
        context = self._context
        value,rooms_list,is_all,occupied_rooms = 0,[],False,[]
        str_today = str(today) + ' 15:00:00'
        enterprise_id = self.env['res.users'].browse([self.env.uid])[0].enterprise_id
        if enterprise_id:
            self._cr.execute("""select count(*) as counter from etourism_room r where r.enterprise_id=%s"""%(enterprise_id.id))
            all_rooms = self._cr.fetchone()
            if all_rooms:
                num_rooms_enterprise = all_rooms[0]

            self._cr.execute("""select r.id from etourism_room r where r.enterprise_id=%s and r.status='Unvailable'
"""%(enterprise_id.id))
            rooms_unavailable = self._cr.fetchall()
            if rooms_unavailable:
                for room in rooms_unavailable:
                    rooms_list.append(room[0])

            self._cr.execute("""select ss.id,ss.room_type_id from etourism_stop_sales ss where ss.enterprise_id=%s and '%s' between start_date and end_date
"""%(enterprise_id.id,today))
            stop_sales = self._cr.fetchall()
            if stop_sales:
                for ss in stop_sales:
                    if not ss[1]:
                        is_all = True
                    else:
                        self._cr.execute("""select r.id from etourism_room r where r.enterprise_id=%s and r.room_type_id=%s"""%(enterprise_id.id,ss[1]))
                        categ_rooms = self._cr.fetchall()
                        if categ_rooms:
                            for ct_room in categ_rooms:
                                rooms_list.append(ct_room[0])

            if is_all:
                if input == 'total_rooms_occupied':
                    value = 0
                elif input == 'total_rooms_unavailable':
                    value = num_rooms_enterprise
                elif input == 'total_rooms_available':
                    value = 0
            else:
                rooms_list = list(set(rooms_list))
                counter = 0
                self._cr.execute("""select distinct b.room_id from etourism_booking b,etourism_booking_res_partner_rel r 
					where r.booking_id=b.id and b.enterprise_id=%s and b.state in ('hosted')"""%(enterprise_id.id))
                result = self._cr.fetchall()
                if result:
                    for res in result:
                        occupied_rooms.append(res[0])
                    while counter < len(occupied_rooms):
                        rooms_list = [x for x in rooms_list if x != occupied_rooms[counter]]
                        counter += 1
                if input == 'total_rooms_occupied':
                    value=len(occupied_rooms)
                elif input == 'total_rooms_unavailable':
                    value = len(rooms_list)
                elif input == 'total_rooms_available':
                    value = num_rooms_enterprise - len(occupied_rooms) - len(rooms_list)
        return value
		
    @api.multi
    def get_total_bookings_state(self,today,state=''):
        result = []
        context = self._context
        value,v_return = 0,''
        total = 'price'
        enterprise_id = self.env['res.users'].browse([self.env.uid])[0].enterprise_id
        company_currency = self.env.user.company_id.currency_id.symbol
        if enterprise_id:
            self._cr.execute("""select total_amount_bookings(%s, '%s', '%s', '%s')"""%(enterprise_id.id,today,state,total))
            result = self._cr.fetchall()
            if result:
                for res in result:
                    if res[0]:
                        value += res[0]
            v_return = str(value) + ' ' + company_currency
        return v_return

		
    @api.multi
    def get_invoices(self,today):
        result,invoice_state = [],''
        context = self._context
        return_header = ''
        enterprise_id = self.env['res.users'].browse([self.env.uid])[0].enterprise_id
        company_currency = self.env.user.company_id.currency_id.symbol
        count = 0
        if enterprise_id:
            self._cr.execute("""select i.number,b.reservation_no,i.date_invoice,i.partner_id,p.name as name_client,(i.amount_total - i.residual) as total_paid,i.residual,i.state
from account_invoice i
LEFT JOIN etourism_booking_group b ON (b.id=i.booking_group_id)
LEFT JOIN res_partner p ON (p.id=i.partner_id)
where i.enterprise_id=%s and i.state in ('paid') and i.type='out_invoice' and i.date_invoice='%s' and i.id in (
select r.invoice_id from account_invoice_payment_rel r, account_invoice i, account_payment p
where p.id=r.payment_id and i.id=r.invoice_id and p.payment_date='%s' and i.enterprise_id=%s)
union
select i.number,b.reservation_no,i.date_invoice,i.partner_id,p.name as name_client,(i.amount_total - i.residual) as total_paid,i.residual,i.state
from account_invoice i
LEFT JOIN etourism_booking_group b ON (b.id=i.booking_group_id)
LEFT JOIN res_partner p ON (p.id=i.partner_id)
where i.enterprise_id=%s and i.state in ('open') and i.type='out_invoice' and i.date_invoice <= '%s'
"""%(enterprise_id.id,today,today,enterprise_id.id,enterprise_id.id,today))
            result = self._cr.fetchall()
            if result:
                for res in result:
                    if count % 2 == 0:
                        return_header += """<tr style="background-color:#FFFFFF;">"""
                    else:
                        return_header += """<tr style="background-color:#F0F0F0;">"""
                    invoice_state = str(res[7])
                    if invoice_state == 'open':
                        invoice_state = _('Open')
                    elif invoice_state == 'paid':
                        invoice_state = _('Paid')

                    return_header += """<td>"""+ str(res[0]) + """</td>"""
                    return_header += """<td>"""+ str(res[1]) + """</td>"""
                    return_header += """<td>"""+ str(res[2]) + """</td>"""
                    return_header += """<td>"""+ str(res[4]) + """</td>"""
                    return_header += """<td>"""+ str(res[5]) + ' ' + company_currency + """</td>"""
                    return_header += """<td>"""+ str(res[6]) + ' ' + company_currency + """</td>"""
                    return_header += """<td>"""+ str(invoice_state) + """</td>"""
                    return_header += """</tr>"""
                    count += 1
        return return_header

    @api.multi
    def get_total_invoices(self,today,type=''):
        result = []
        context = self._context
        value,v_return = 0.0,''
        enterprise_id = self.env['res.users'].browse([self.env.uid])[0].enterprise_id
        company_currency = self.env.user.company_id.currency_id.symbol
        if enterprise_id:
            if type == 'paid':
                self._cr.execute("""select sum(v.total_paid) as total from (select i.number,b.reservation_no,i.date_invoice,i.partner_id,p.name as name_client,(i.amount_total - i.residual) as total_paid,i.residual,i.state
from account_invoice i
LEFT JOIN etourism_booking_group b ON (b.id=i.booking_group_id)
LEFT JOIN res_partner p ON (p.id=i.partner_id)
where i.enterprise_id=%s and i.state in ('paid') and i.type='out_invoice' and i.date_invoice='%s' and i.id in (
select r.invoice_id from account_invoice_payment_rel r, account_invoice i, account_payment p
where p.id=r.payment_id and i.id=r.invoice_id and p.payment_date='%s' and i.enterprise_id=%s)
union
select i.number,b.reservation_no,i.date_invoice,i.partner_id,p.name as name_client,(i.amount_total - i.residual) as total_paid,i.residual,i.state
from account_invoice i
LEFT JOIN etourism_booking_group b ON (b.id=i.booking_group_id)
LEFT JOIN res_partner p ON (p.id=i.partner_id)
where i.enterprise_id=%s and i.state in ('open') and i.type='out_invoice' and i.date_invoice <= '%s') v
"""%(enterprise_id.id,today,today,enterprise_id.id,enterprise_id.id,today))
            elif type == 'residual':
                self._cr.execute("""select sum(v.residual) as total from (select i.number,b.reservation_no,i.date_invoice,i.partner_id,p.name as name_client,(i.amount_total - i.residual) as total_paid,i.residual,i.state
from account_invoice i
LEFT JOIN etourism_booking_group b ON (b.id=i.booking_group_id)
LEFT JOIN res_partner p ON (p.id=i.partner_id)
where i.enterprise_id=%s and i.state in ('paid') and i.type='out_invoice' and i.date_invoice='%s' and i.id in (
select r.invoice_id from account_invoice_payment_rel r, account_invoice i, account_payment p
where p.id=r.payment_id and i.id=r.invoice_id and p.payment_date='%s' and i.enterprise_id=%s)
union
select i.number,b.reservation_no,i.date_invoice,i.partner_id,p.name as name_client,(i.amount_total - i.residual) as total_paid,i.residual,i.state
from account_invoice i
LEFT JOIN etourism_booking_group b ON (b.id=i.booking_group_id)
LEFT JOIN res_partner p ON (p.id=i.partner_id)
where i.enterprise_id=%s and i.state in ('open') and i.type='out_invoice' and i.date_invoice <= '%s') v
"""%(enterprise_id.id,today,today,enterprise_id.id,enterprise_id.id,today))
            result = self._cr.fetchall()
            if result:
                for res in result:
                    if res[0]:
                        value += res[0]
            #v_return = str(value) + ' ' + company_currency
        return value

    @api.multi
    def get_pos_info(self,today):
        result = []
        context = self._context
        return_header = ''
        count = 0
        company_id = self.env.user.company_id.id
        company_currency = self.env.user.company_id.currency_id.symbol
        if company_id:
            self._cr.execute("""select v.name_product,v.qty,v.price_unit
,(v.qty * v.price_unit) as price_total
,((v.tax / 100) * v.price_unit) as tax_unit
,((v.tax / 100) * v.qty * v.price_unit) as tax_total
,v.name_pos
from (select l.product_id,l.qty,l.order_id,l.price_unit,o.state,c.name as name_pos,t.name as name_product
, (select tx.amount from account_tax tx, product_taxes_rel rel where tx.id=rel.tax_id and rel.prod_id=t.id and tx.company_id=%s limit 1) as tax
from pos_order o, pos_order_line l, pos_session s, pos_config c, product_product p, product_template t
where o.id=l.order_id and o.session_id=s.id and c.id=s.config_id and l.product_id=p.id and p.product_tmpl_id=t.id 
and o.state in ('paid','invoiced','done') and o.company_id=%s and o.date_order::date='%s'
) v order by v.order_id
"""%(company_id,company_id,today))
            result = self._cr.fetchall()
            if result:
                for res in result:
                    if count % 2 == 0:
                        return_header += """<tr style="background-color:#FFFFFF;">"""
                    else:
                        return_header += """<tr style="background-color:#F0F0F0;">"""

                    return_header += """<td>"""+ str(res[0]) + """</td>"""
                    return_header += """<td>"""+ str(res[1]) + """</td>"""
                    return_header += """<td>"""+ str(res[2]) + ' ' + company_currency + """</td>"""
                    return_header += """<td>"""+ str(res[3]) + ' ' + company_currency + """</td>"""
                    return_header += """<td>"""+ str(res[4]) + """</td>"""
                    return_header += """<td>"""+ str(res[5]) + ' ' + company_currency + """</td>"""
                    return_header += """<td>"""+ str(res[6]) + """</td>"""
                    return_header += """</tr>"""
                    count += 1
        return return_header

    @api.multi
    def get_total_pos(self,today,type=''):
        result = []
        context = self._context
        value,v_return = 0.0,''
        company_id = self.env.user.company_id.id
        company_currency = self.env.user.company_id.currency_id.symbol
        if company_id:
            if type == 'price':
                self._cr.execute("""select sum(v2.price_total) as price_total from (select v.name_product,v.qty,v.price_unit
,(v.qty * v.price_unit) as price_total
,((v.tax / 100) * v.price_unit) as tax_unit
,((v.tax / 100) * v.qty * v.price_unit) as tax_total
,v.name_pos
from (select l.product_id,l.qty,l.order_id,l.price_unit,o.state,c.name as name_pos,t.name as name_product
, (select tx.amount from account_tax tx, product_taxes_rel rel where tx.id=rel.tax_id and rel.prod_id=t.id and tx.company_id=%s limit 1) as tax
from pos_order o, pos_order_line l, pos_session s, pos_config c, product_product p, product_template t
where o.id=l.order_id and o.session_id=s.id and c.id=s.config_id and l.product_id=p.id and p.product_tmpl_id=t.id 
and o.state in ('paid','invoiced','done') and o.company_id=%s and o.date_order::date='%s'
) v) v2
"""%(company_id,company_id,today))
            elif type == 'tax':
                self._cr.execute("""select sum(v2.tax_total) as tax_total from (select v.name_product,v.qty,v.price_unit
,(v.qty * v.price_unit) as price_total
,((v.tax / 100) * v.price_unit) as tax_unit
,((v.tax / 100) * v.qty * v.price_unit) as tax_total
,v.name_pos
from (select l.product_id,l.qty,l.order_id,l.price_unit,o.state,c.name as name_pos,t.name as name_product
, (select tx.amount from account_tax tx, product_taxes_rel rel where tx.id=rel.tax_id and rel.prod_id=t.id and tx.company_id=%s limit 1) as tax
from pos_order o, pos_order_line l, pos_session s, pos_config c, product_product p, product_template t
where o.id=l.order_id and o.session_id=s.id and c.id=s.config_id and l.product_id=p.id and p.product_tmpl_id=t.id 
and o.state in ('paid','invoiced','done') and o.company_id=%s and o.date_order::date='%s'
) v) v2
"""%(company_id,company_id,today))
            result = self._cr.fetchall()
            if result:
                for res in result:
                    if res[0]:
                        value += res[0]
            v_return = str(value) + ' ' + company_currency
        return v_return

    @api.multi
    def get_payments_info(self,today):
        result = []
        context = self._context
        return_header = ''
        count = 0
        enterprise_id = self.env['res.users'].browse([self.env.uid])[0].enterprise_id
        company_currency = self.env.user.company_id.currency_id.symbol
        #today = datetime.now().date()
        if enterprise_id:
            self._cr.execute("""select v2.amount
,(select j.name from account_journal j where j.id=v2.journal_id) as name_journal
from (
select sum(v.amount) as amount,v.journal_id
from (
select p.amount,p.journal_id
from account_payment p
where p.enterprise_id=%s and p.payment_date='%s'
union all
select b.amount,b.journal_id
from account_bank_statement_line b, pos_order o
where o.id=b.pos_statement_id and o.enterprise_id=%s and o.date_order::date = '%s'
) v
group by v.journal_id) v2
"""%(enterprise_id.id,today,enterprise_id.id,today))
            result = self._cr.fetchall()
            if result:
                for res in result:
                    if count % 2 == 0:
                        return_header += """<tr style="background-color:#FFFFFF;">"""
                    else:
                        return_header += """<tr style="background-color:#F0F0F0;">"""

                    return_header += """<td>"""+ str(res[1]) + """</td>"""
                    return_header += """<td>"""+ str(res[0]) + ' ' + company_currency + """</td>"""
                    return_header += """</tr>"""
                    count += 1
        return return_header

    @api.multi
    def get_total_payments(self,today):
        result = []
        context = self._context
        value,v_return = 0.0,''
        company_id = self.env.user.company_id.id
        enterprise_id = self.env['res.users'].browse([self.env.uid])[0].enterprise_id
        company_currency = self.env.user.company_id.currency_id.symbol
        #today = datetime.now().date()
        if company_id and enterprise_id:
            self._cr.execute("""select sum(v3.amount) from (select v2.amount
,(select j.name from account_journal j where j.id=v2.journal_id) as name_journal
from (
select sum(v.amount) as amount,v.journal_id
from (
select p.amount,p.journal_id
from account_payment p
where p.enterprise_id=%s and p.payment_date='%s'
union all
select b.amount,b.journal_id
from account_bank_statement_line b, pos_order o
where o.id=b.pos_statement_id and o.enterprise_id=%s and o.date_order::date = '%s'
) v
group by v.journal_id) v2) v3
"""%(enterprise_id.id,today,enterprise_id.id,today))
            result = self._cr.fetchall()
            if result:
                for res in result:
                    if res[0]:
                        value += res[0]
            #v_return = str(value) + ' ' + company_currency
        return value

    @api.multi
    def get_laundry_minibar_info(self,today):
        result = []
        context = self._context
        return_header = ''
        count = 0
        company_id = self.env.user.company_id.id
        enterprise_id = self.env['res.users'].browse([self.env.uid])[0].enterprise_id
        company_currency = self.env.user.company_id.currency_id.symbol
        #today = datetime.now().date()
        if company_id and enterprise_id:
            self._cr.execute("""select v.name_product,v.quantity,v.price_unit
,(v.quantity * v.price_unit) as price_total
,((v.tax / 100) * v.price_unit) as tax_unit
,((v.tax / 100) * v.quantity * v.price_unit) as tax_total
,v.type
from (select t.name as name_product, l.quantity, pd.default_code as type, l.price_unit
, (select tx.amount from account_tax tx, product_taxes_rel rel where tx.id=rel.tax_id and rel.prod_id=t.id and tx.company_id=%s limit 1) as tax
from account_payment p, account_invoice_line l, account_invoice i, account_invoice_payment_rel rel, product_product pd, product_template t
where p.id=rel.payment_id and i.id=rel.invoice_id and i.id=l.invoice_id and l.product_id=pd.id and pd.product_tmpl_id=t.id 
and (pd.default_code = 'Lavandaria' or pd.default_code = 'Minibar')
and i.enterprise_id=%s and p.payment_date='%s'
) v
"""%(company_id,enterprise_id.id,today))
            result = self._cr.fetchall()
            if result:
                for res in result:
                    if count % 2 == 0:
                        return_header += """<tr style="background-color:#FFFFFF;">"""
                    else:
                        return_header += """<tr style="background-color:#F0F0F0;">"""

                    return_header += """<td>"""+ str(res[0]) + """</td>"""
                    return_header += """<td>"""+ str(res[1]) + """</td>"""
                    return_header += """<td>"""+ str(res[2]) + ' ' + company_currency + """</td>"""
                    return_header += """<td>"""+ str(res[3]) + ' ' + company_currency + """</td>"""
                    return_header += """<td>"""+ str(res[4]) + """</td>"""
                    return_header += """<td>"""+ str(res[5]) + ' ' + company_currency + """</td>"""
                    return_header += """<td>"""+ str(res[6]) + """</td>"""
                    return_header += """</tr>"""
                    count += 1
        return return_header

    @api.multi
    def get_total_laundry_minibar(self,today,type=''):
        result = []
        context = self._context
        value,v_return = 0.0,''
        company_id = self.env.user.company_id.id
        enterprise_id = self.env['res.users'].browse([self.env.uid])[0].enterprise_id
        company_currency = self.env.user.company_id.currency_id.symbol
        #today = datetime.now().date()
        if company_id and enterprise_id:
            if type == 'price':
                self._cr.execute("""select sum(v2.price_total) as price_total from (select v.name_product,v.quantity,v.price_unit
,(v.quantity * v.price_unit) as price_total
,((v.tax / 100) * v.price_unit) as tax_unit
,((v.tax / 100) * v.quantity * v.price_unit) as tax_total
,v.type
from (select t.name as name_product, l.quantity, pd.default_code as type, l.price_unit
, (select tx.amount from account_tax tx, product_taxes_rel rel where tx.id=rel.tax_id and rel.prod_id=t.id and tx.company_id=%s limit 1) as tax
from account_payment p, account_invoice_line l, account_invoice i, account_invoice_payment_rel rel, product_product pd, product_template t
where p.id=rel.payment_id and i.id=rel.invoice_id and i.id=l.invoice_id and l.product_id=pd.id and pd.product_tmpl_id=t.id 
and (pd.default_code = 'Lavandaria' or pd.default_code = 'Minibar')
and i.enterprise_id=%s and p.payment_date='%s'
) v) v2
"""%(company_id,enterprise_id.id,today))
            elif type == 'tax':
                self._cr.execute("""select sum(v2.tax_total) as tax_total from (select v.name_product,v.quantity,v.price_unit
,(v.quantity * v.price_unit) as price_total
,((v.tax / 100) * v.price_unit) as tax_unit
,((v.tax / 100) * v.quantity * v.price_unit) as tax_total
,v.type
from (select t.name as name_product, l.quantity, pd.default_code as type, l.price_unit
, (select tx.amount from account_tax tx, product_taxes_rel rel where tx.id=rel.tax_id and rel.prod_id=t.id and tx.company_id=%s limit 1) as tax
from account_payment p, account_invoice_line l, account_invoice i, account_invoice_payment_rel rel, product_product pd, product_template t
where p.id=rel.payment_id and i.id=rel.invoice_id and i.id=l.invoice_id and l.product_id=pd.id and pd.product_tmpl_id=t.id 
and (pd.default_code = 'Lavandaria' or pd.default_code = 'Minibar')
and i.enterprise_id=%s and p.payment_date='%s'
) v) v2
"""%(company_id,enterprise_id.id,today))
            result = self._cr.fetchall()
            if result:
                for res in result:
                    if res[0]:
                        value += res[0]
            v_return = str(value) + ' ' + company_currency
        return v_return

		
    @api.multi
    def get_tourist_tax_info(self,today):
        result = []
        context = self._context
        return_header = ''
        count,amount,num = 0,'',''
        enterprise_id = self.env['res.users'].browse([self.env.uid])[0].enterprise_id
        company_currency = self.env.user.company_id.currency_id.symbol
        #today = datetime.now().date()
        if enterprise_id:
            self._cr.execute("""select count(*) as num_guests, sum(v.price_subtotal) as total from (select l.price_subtotal
from account_invoice i, account_invoice_line l, product_product p, product_template t
where l.invoice_id=i.id and l.product_id=p.id and p.product_tmpl_id=t.id 
and t.tourist_tax is true and i.enterprise_id=%s and i.date_invoice='%s') v
"""%(enterprise_id.id,today))
            result = self._cr.fetchall()
            if result:
                for res in result:
                    if count % 2 == 0:
                        return_header += """<tr style="background-color:#FFFFFF;">"""
                    else:
                        return_header += """<tr style="background-color:#F0F0F0;">"""
                    if res[0]:
                        num = str(res[0])
                    else:
                        num = '-'

                    if res[1]:
                        amount = str(res[1]) + ' ' + company_currency
                    else:
                        amount = '-'
                    return_header += """<td>"""+ num + """</td>"""
                    return_header += """<td>"""+ amount + """</td>"""
                    return_header += """</tr>"""
                    count += 1
        return return_header

    @api.multi
    def get_currency(self):
        context = self._context
        company_currency = ' ' + self.env.user.company_id.currency_id.symbol
        return company_currency

    @api.multi
    def get_total_amount_iva(self,today):
        value,total_payments,total_invoices_residual = 0.0,0,0
        total_payments = self.get_total_payments(today)
        total_invoices_residual = self.get_total_invoices(today,'residual')
        value = total_payments + total_invoices_residual
        return value

    @api.multi
    def get_taxable_amount_iva(self,today):
        value,total_amount = 0.0,0
        total_amount = self.get_total_amount_iva(today)
        value = round((total_amount / 1.15),2)
        return value

    @api.multi
    def get_amount_iva(self,today):
        value,total_amount,taxable_amount = 0.0,0,0
        total_amount = self.get_total_amount_iva(today)
        taxable_amount = self.get_taxable_amount_iva(today)
        value = total_amount - taxable_amount
        return value

    @api.multi
    def get_ebusiness_contacts(self,field=''):
        email,phone,mobile = '','',''
        enterprise_id = self.env['res.users'].browse([self.env.uid])[0].enterprise_id
        ebusiness_id = enterprise_id.ebusiness_id
        for contact in ebusiness_id.contacts_line:
            if contact.default_contact:
                email = contact.email
                phone = contact.phone
                mobile = contact.mobile
        if field == 'email':
            return email
        elif field == 'phone':
            return phone
        elif field == 'mobile':
            return mobile

