# -*- coding: utf-8 -*-

from odoo import models, fields, api
from odoo.http import request
import datetime, logging


class CarDashboard(models.Model):
    _name = 'car.dashboard'
    _description = 'Car Dashboard'

    name = fields.Char("")

    @api.model
    def get_data_info(self):
        """
        The function which is called from car_report.js.

        :rtype dict
        :return: data
        """
        v = self.get_vehicles_info()
        v_cp = self.get_vehicles_contracted_pending_info()
        v_co = self.get_vehicles_contracted_ongoing_info()
        p = self.get_partners_info()
        p_all = self.get_partners_all_info()
        pay = self.get_payments_info()
        c = self.get_contracts_info()
        c_neg = self.get_contracts_negociate_info()
        c_ong = self.get_contracts_ongoing_info()
        a = self.get_contracts_amount()
        a_all = self.get_contracts_amount_all()
        currency = self.env.user.company_id.currency_id.name
        company_id = self.env.user.company_id.id
        # child_ids = self.env.user.company_id.child_ids.ids
        print(c_ong)
        logging.info(c_ong)
        a.update({
            'vehicle': v,
            'vehicle_cp': v_cp,
            'vehicle_co': v_co,
            'partner_all': p_all,
            'partner': p,
            'payment': pay,
            'contract': c,
            'amount_all': a_all,
            'contract_neg': c_neg,
            'contract_ong': c_ong,
            'currency': currency,
            'company_id': company_id
        })
        print(a)
        return [a]

    def get_vehicles_info(self):
        domain = [('state', 'in', ['validate', 'revise'])]
        g_country = self.env.ref('car.group_car_country')
        g_agency = self.env.ref('car.group_car_agency')
        user = self.env.user
        if g_country in user.groups_id or g_agency in user.groups_id:
            domain += [('country_id', '=', user.company_id.country_id.id)]
        vehicles = self.env['car.vehicle'].search(domain)
        return vehicles and vehicles.ids or []

    def get_vehicles_contracted_pending_info(self):
        user = self.env.user
        domain = [
            ('use_state', '=', 'pending'),
            ('state', 'in', ['progress']),
        ]
        g_country = self.env.ref('car.group_car_country')
        g_agency = self.env.ref('car.group_car_agency')
        if g_country in user.groups_id or g_agency in user.groups_id:
            domain += [('vehicle_id.country_id', '=', user.company_id.country_id.id)]
        vehicles = self.env['car.contract'].search(domain)
        v_ids = vehicles and [x.vehicle_id.id for x in vehicles] or []
        v_ids = list(set(v_ids))
        return v_ids

    def get_vehicles_contracted_ongoing_info(self):
        user = self.env.user
        domain = [
            ('use_state', '=', 'ongoing'),
            ('state', 'in', ['progress']),
        ]
        g_country = self.env.ref('car.group_car_country')
        g_agency = self.env.ref('car.group_car_agency')
        if g_country in user.groups_id or g_agency in user.groups_id:
            domain += [('vehicle_id.country_id', '=', user.company_id.country_id.id)]
        vehicles = self.env['car.contract'].search(domain)
        v_ids = vehicles and [x.vehicle_id.id for x in vehicles] or []
        v_ids = list(set(v_ids))
        return v_ids

    def get_partners_all_info(self):
        partners = self.env['res.partner'].search([
            ('parent_id', '=', False),
            ('customer', '=', True), '|',
            ('company_id', '=', False),
            ('company_id', 'child_of', [self.env.user.company_id.id])
        ])
        return partners and partners.ids or []

    def get_partners_info(self):
        partners = self.env['car.contract'].search([
            ('state', 'in', ['progress']), '|',
            ('company_id', '=', False),
            ('company_id', 'child_of', [self.env.user.company_id.id])
        ])
        p_ids = partners and [x.customer_id.id for x in partners] or []
        p_ids = list(set(p_ids))
        return p_ids

    def get_payments_info(self):
        payments = self.env['car.payment'].search(['|',
            ('company_id', '=', False),
            ('company_id', 'child_of', [self.env.user.company_id.id])
        ])
        return payments and payments.ids or []

    def get_contracts_info(self):
        contracts = self.env['car.contract'].search(['|',
            ('company_id', '=', False),
            ('company_id', 'child_of', [self.env.user.company_id.id]),
            ('state', 'in', ['progress']),
        ])
        return contracts and contracts.ids or []

    def get_contracts_negociate_info(self):
        contracts = self.env['car.contract'].search(['|',
            ('company_id', '=', False),
            ('company_id', 'child_of', [self.env.user.company_id.id]),
            ('state', 'in', ['revise', 'approved']),
        ])
        return contracts and contracts.ids or []

    def get_contracts_ongoing_info(self):
        contracts = self.env['car.contract'].search(['|',
            ('company_id', '=', False),
            ('company_id', 'child_of', [self.env.user.company_id.id]),
            ('state', 'in', ['progress']),
            ('use_state', '=', 'ongoing'),
        ])
        return contracts and contracts.ids or []

    def get_contracts_amount(self):
        contracts = self.env['car.contract'].search(['|',
            ('company_id', '=', False),
            ('company_id', 'child_of', [self.env.user.company_id.id]),
            ('state', 'in', ['progress']),
            ('use_state', 'in', ['ongoing']),
        ])
        company_currency = self.env.user.company_id.currency_id
        remain = 0.0
        collect = 0.0
        amount = 0.0
        late = 0.0
        currency_env = self.env['res.currency']
        for c in contracts:
            currency = c.currency_id
            remain += currency_env._compute(company_currency, currency, c.remain_amount)
            collect += currency_env._compute(company_currency, currency, c.payment_amount)
            amount += currency_env._compute(company_currency, currency, c.amount_depreciation)
            late += currency_env._compute(company_currency, currency, c.late_amount)
        late_percent = 0.0
        if remain:
            late_ratio = round(late / remain, 4)
            late_percent = late_ratio * 100
        return {'remain': remain, 'collect': collect, 'amount': amount, 'late': late, 'late_percent': late_percent}

    def get_contracts_amount_all(self):
        contracts = self.env['car.contract'].search(['|',
            ('company_id', '=', False),
            ('company_id', 'child_of', [self.env.user.company_id.id]),
            ('state', 'in', ['progress']),
        ])
        company_currency = self.env.user.company_id.currency_id
        currency_env = self.env['res.currency']
        amount = 0.0
        for c in contracts:
            currency = c.currency_id
            amount += currency_env._compute(company_currency, currency, c.amount_depreciation)
        return amount
