# -*- coding: utf-8 -*-
###############################################################################
#
#    Tech-Receptives Solutions Pvt. Ltd.
#    Copyright (C) 2009-TODAY Tech-Receptives(<http://www.techreceptives.com>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################

from odoo.osv import expression
from odoo.tools.float_utils import float_round as round
from odoo.tools import DEFAULT_SERVER_DATETIME_FORMAT
from odoo.exceptions import UserError, ValidationError
from odoo import api, fields, models, _
from odoo import tools

import odoo.addons.decimal_precision as dp
			
class ViewEbusinessKpiTree(models.Model):
    _name = 'view.ebusiness.kpi.tree'
    _auto = False
    _rec_name = 'ebusiness_id'
    _description = "View Ebusiness KPI Tree"

    id = fields.Integer('Id')
    country_id = fields.Many2one('ecom.location', string='Country id')
    island_id = fields.Many2one('ecom.location', string='Island id', readonly=True)
    county_id = fields.Many2one('ecom.location', string='County id', readonly=True)
    parish_id = fields.Many2one('ecom.location', string='Parish id')
    partner_id = fields.Many2one('res.partner', string='Partner')
    type_id = fields.Many2one('ecom.ebusiness.type', string='Ebusiness type')
    enterprise_type_id = fields.Many2one('ecom.ebusiness.type', string='Enterprise type', readonly=True)
    site = fields.Char(string='Site')
    ebusiness_id = fields.Many2one('ecom.ebusiness', string='Ebusiness', readonly=True)
    sequence_code = fields.Char('Sequence Code', readonly=True)
    num_rooms = fields.Integer('Number of rooms', readonly=True)
    avg_price_rooms = fields.Float('Rooms Average Price', readonly=True)
    occupancy_rate = fields.Float('Ocuppancy Rate', readonly=True)
    market_share_rate = fields.Float('Market Share Rate', readonly=True)
    goal_rate = fields.Float('Goal Rate', readonly=True)
    contract_start_date = fields.Date('Contract start date', readonly=True)
    name_type_ebusiness = fields.Char(string='Name type ebusiness')
    name_type_enterprise = fields.Char(string='Name type enterprise')
    name_enterprise = fields.Char(string='Name Enterprise')
    comment = fields.Text(string='Description')
    phone = fields.Char(string='Phone contact')
    mobile = fields.Char(string='Mobile contact')
    email = fields.Char(string='Email contact')
    nif = fields.Char(string='NIF')
    island = fields.Char(string='Island')
    county = fields.Char(string='County')
    parish = fields.Char(string='Parish')
    captcha_key = fields.Char(string='Captcha Key')
    api_key = fields.Char(string='API Key')
    policies = fields.Text(string='Policies')

    @api.model_cr
    def init(self):
        cr = self._cr
        tools.drop_view_if_exists(cr, 'view_ebusiness_kpi_tree')

        cr.execute("""
CREATE OR REPLACE VIEW view_ebusiness_kpi_tree AS (		
SELECT e.id, e.country_id, e.island_id, e.county_id, e.parish_id, eb.partner_id, e.type_id,e.enterprise_type_id,eb.site 
,eb.id AS ebusiness_id,eb.num_rooms,eb.avg_price_rooms,eb.sequence_code,eb.occupancy_rate,eb.market_share_rate,eb.goal_rate,eb.contract_start_date
,te.name AS name_type_ebusiness,te2.name as name_type_enterprise
, e.name AS name_enterprise,e.comment
, ct.phone,ct.mobile, ct.email, ct.nif
, l.name AS island, l2.name AS county, l3.name AS parish,eb.captcha_key,eb.api_key,e.policies
   FROM etourism_enterprise e
   LEFT JOIN ecom_ebusiness_type te ON e.type_id = te.id
   LEFT JOIN ecom_ebusiness_type te2 ON e.enterprise_type_id = te2.id
   LEFT JOIN ecom_ebusiness eb ON e.ebusiness_id = eb.id
   LEFT JOIN res_partner p ON eb.partner_id = p.id
   LEFT JOIN ecom_ebusiness_contact ct ON (ct.ebusiness_id = eb.id and ct.default_contact is true)
   LEFT JOIN ecom_location l ON e.island_id = l.id
   LEFT JOIN ecom_location l2 ON e.county_id = l2.id
   LEFT JOIN ecom_location l3 ON e.parish_id = l3.id
  WHERE te.code = 'ALO'
            )""")

class ViewBookingKpiTree(models.Model):
    _name = 'view.booking.kpi.tree'
    _auto = False
    _rec_name = 'reservation_no'
    _description = "View Booking KPI Tree"

    id = fields.Integer('Id')
    enterprise_id = fields.Many2one('etourism.enterprise', 'Enterprise', readonly=True)
    reservation_no = fields.Char('Reservation No', size=64, readonly=True)
    booking_date = fields.Datetime('Booking Date', required=True, readonly=True)
    checkin = fields.Datetime(string='Checkin', readonly=True)
    checkout = fields.Datetime(string='Checkout', readonly=True)
    duration = fields.Integer(string='Duration', readonly=True)
    state = fields.Selection([('request', 'Request'),('pending', 'Pending'),
                              ('confirmed', 'Confirmed'),
                              ('no_show', 'No Show'),
                              ('cancelled', 'Cancelled'),
                              ('hosted', 'Checkin'),
                              ('checkout', 'Checkout')
                              ], 'State', default='request', track_visibility='onchange',translate=False, readonly=True)
    num_rooms = fields.Integer(string='Number of rooms', readonly=True)
    num_adults = fields.Integer(string='Number of adults', readonly=True)
    num_children = fields.Integer(string='Number of children', readonly=True)
    num_extra_bed = fields.Integer(string='Number of extra beds', readonly=True)
    total_amount = fields.Float(string='Total Amount', readonly=True)
    guest_id= fields.Many2one('res.partner', 'Client Name', readonly=True)
    origin = fields.Selection([('site', 'Site'),
                              ('pms', 'PMS')], 'Origin', default='pms',readonly=True)
    has_folio = fields.Boolean(string='Has folio', readonly=True)
    has_order = fields.Boolean(string='Has order', readonly=True)
    has_invoice = fields.Boolean(string='Has invoices', readonly=True)
    motive = fields.Char(string='Motive', readonly=True)
    ebusiness_id = fields.Many2one('ecom.ebusiness', string='Ebusiness', readonly=True)
    contract_percentage = fields.Integer(string='Commission Percentage', readonly=True)
    prime_commission = fields.Float(string='Commission', readonly=True)
    country_id = fields.Many2one('ecom.location', string='Country id', readonly=True)
    island_id = fields.Many2one('ecom.location', string='Island id', readonly=True)
    county_id = fields.Many2one('ecom.location', string='County id', readonly=True)
    parish_id = fields.Many2one('ecom.location', string='Parish id', readonly=True)
    country_name = fields.Char(string='Country name', readonly=True)
    island_name = fields.Char(string='Island name', readonly=True)
    county_name = fields.Char(string='County name', readonly=True)
    parish_name = fields.Char(string='Parish name', readonly=True)
    enterprise_name = fields.Char(string='Enterprise name', readonly=True)

    @api.model_cr
    def init(self):
        cr = self._cr
        tools.drop_view_if_exists(cr, 'view_booking_kpi_tree')

        cr.execute("""
CREATE OR REPLACE VIEW view_booking_kpi_tree AS (		
select b.id,b.enterprise_id,b.reservation_no,b.booking_date,b.checkin,b.checkout,b.duration,b.state,b.num_rooms,b.num_adults,b.num_children,b.num_extra_bed
,b.total_amount,b.guest_id,b.origin,b.has_folio,b.has_order,b.has_invoice,b.motive,b.ebusiness_id
,b.contract_percentage,b.prime_commission,e.country_id,e.island_id,e.county_id,e.parish_id,l.name as country_name,l2.name as island_name
,l3.name as county_name, l4.name as parish_name,e.name as enterprise_name
from etourism_booking_group b
LEFT JOIN etourism_enterprise e ON (e.id=b.enterprise_id) 
LEFT JOIN ecom_location l ON (e.country_id=l.id)
LEFT JOIN ecom_location l2 ON (e.island_id=l2.id)
LEFT JOIN ecom_location l3 ON (e.county_id=l3.id)
LEFT JOIN ecom_location l4 ON (e.parish_id=l4.id)
where e.enterprise_id is not null
            )""")


# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
