# -*- 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, _

class EcomViewCustomers(models.Model):
    _name = 'ecom.view.customers'
    _description = "View Customers"

    name = fields.Char(string='Name of the contract', required=True)
    nif = fields.Integer(string='NIF')
    account_number = fields.Char(string='Account Number')
    business_registration = fields.Char(string='Business Registration')
    id_number = fields.Char(string='ID Number')

    @api.multi
    def action_load_customers(self):
        """Load customers
        """
        customers_ids = self.env['ecom.view.customers'].search([('id','>',0)])

        for customer in customers_ids:
            customer_id=customer.id
            self.env['ecom.customer'].create({
                    'name': customer.name,
                    'nif': customer.nif,
                    'account_number': customer.account_number,
                    'business_registration': customer.business_registration,
                    'id_number': customer.id_number,
                        })	

class EcomViewEbusiness(models.Model):
    _name = 'ecom.view.ebusiness'
    _description = "View Ebusiness"

    name = fields.Char(string='Name of the contract', required=True)
    state = fields.Char(string='State')
    site = fields.Char(string='Site')
    type = fields.Char(string='Type')
    customer = fields.Char(string='Customer')
    country = fields.Char(string='Country')
    island = fields.Char(string='Island')
    county = fields.Char(string='County')
    parish = fields.Char(string='Parish')
    local = fields.Char(string='Local')
    neighborhood = fields.Char(string='Neighborhood')
    pos_id = fields.Char(string='Pos id')
    posautcode = fields.Char(string='Pos Auth Code')
    business_registration = fields.Char(string='Business registration')
    nif = fields.Char(string='NIF')
    published = fields.Boolean(string='Published')
    date_online = fields.Date(string='Date Online')
    contract = fields.Boolean(string='Contract')
    online = fields.Boolean(string='Online')
    has_site = fields.Boolean(string='Has site')
    one_page = fields.Boolean(string='One page')
    ebusiness_id = fields.Char(string='Ebusiness ID')
    enterprise_id = fields.Char(string='Enterprise ID')

    @api.multi
    def action_load_ebusiness(self):
        """Load customers
        """
        ebusiness_ids = self.env['ecom.view.ebusiness'].search([('id','>',1349)])
        published=False
        online=False
        contract=False
        has_site=False
        one_page=False
        customer_id=False
        type=False
        country_id=False
        island_id=False
        county_id=False
        parish_id=False
        local_id=False
        neighborhood_id=False
       

        for ebusiness in ebusiness_ids:
            type=ebusiness.type
            customer=ebusiness.customer
            country=ebusiness.country
            island=ebusiness.island
            county=ebusiness.county
            parish=ebusiness.parish
            local=ebusiness.local
            neighborhood=ebusiness.neighborhood
            type_ids = self.env['ecom.ebusiness.type'].search([('name','=',type)],limit=1)
            customers_ids = self.env['ecom.customer'].search([('name','=',customer)],limit=1)
            country_ids = self.env['ecom.location'].search([('name','=',country)],limit=1)
            island_ids = self.env['ecom.location'].search([('name','=',island),('level','=','2')],limit=1)
            county_ids = self.env['ecom.location'].search([('name','=',county),('level','=','3')],limit=1)
            parish_ids = self.env['ecom.location'].search([('name','=',parish),('level','=','4')],limit=1)
            local_ids = self.env['ecom.location'].search([('name','=',local),('level','=','5')],limit=1)
            neighborhood_ids = self.env['ecom.location'].search([('name','=',neighborhood),('level','=','6')],limit=1)

            if ebusiness.published == 1:
                published = True
            if ebusiness.online == 1:
                online = True
            if ebusiness.contract == 1:
                contract = True
            if ebusiness.has_site == 1:
                has_site = True
            if ebusiness.one_page == 1:
                one_page = True

            if type_ids:
                for tp in type_ids:
                    type=tp.id

            if customers_ids:
                for cust in customers_ids:
                    customer_id=cust.id

            if country_ids:
                for ct in country_ids:
                    country_id=ct.id

            if island_ids:
                for isl in island_ids:
                    island_id=isl.id

            if county_ids:
                for count in county_ids:
                    county_id=count.id

            if parish_ids:
                for par in parish_ids:
                    parish_id=par.id

            if local_ids:
                for loc in local_ids:
                    local_id=loc.id

            if neighborhood_ids:
                for nei in neighborhood_ids:
                    neighborhood_id=nei.id

            self.env['ecom.ebusiness'].create({
                    'name': ebusiness.name,
                    'state': ebusiness.state,
                    'site': ebusiness.site,
                    'ebusiness_type_id': type,
                    'customer': customer_id,
                    'country_id': country_id,
                    'island_id': island_id,
                    'county_id': county_id,
                    'parish_id': parish_id,
                    'local_id': local_id,
                    'neighborhood_id': neighborhood_id,
                    'pos_id': ebusiness.pos_id,
                    'posautcode': ebusiness.posautcode,
                    'business_registration': ebusiness.business_registration,
                    'nif': ebusiness.nif,
                    'date_online':ebusiness.date_online,
                    'published': published,
                    'online': online,
                    'contract': contract,
                    'has_site': has_site,
                    'one_page': one_page,
                        })	
						
class EcomViewContractsEbusiness(models.Model):
    _name = 'ecom.view.contracts.ebusiness'
    _description = "View Contracts Ebusiness"

    contract = fields.Char(string='Contract', required=True)
    ebusiness = fields.Char(string='Ebusiness', required=True)

    @api.multi
    def action_load_contracts(self):
        """Load customers
        """
        contract_ebusiness__ids = self.env['ecom.view.contracts.ebusiness'].search([('id','>',0)])
        contract_id=False
        ebusiness_id=False
        for cont_eb in contract_ebusiness__ids:
            contract_name=cont_eb.contract
            ebusiness_name=cont_eb.ebusiness
            contract_ids = self.env['ecom.contract'].search([('name','=',contract_name)])
            if contract_ids:
                for contract in contract_ids:
                    contract_id=contract.id

            partner_ids = self.env['res.partner'].search([('name','=',ebusiness_name)])
            if partner_ids:
                for partner in partner_ids:
                    partner_id=partner.id

            ebusiness_ids = self.env['ecom.ebusiness'].search([('partner_id','=',partner_id)])
            if ebusiness_ids:
                for ebusiness in ebusiness_ids:
                    ebusiness_id=ebusiness.id

            if contract_id and ebusiness_id:
                self._cr.execute("""INSERT INTO ecom_ebusiness_contract_rel (ebusiness_id,contract_id) VALUES (%s,%s)"""%(ebusiness_id,contract_id))


class EcomViewContacts(models.Model):
    _name = 'ecom.view.contacts'
    _description = "View Contacts"

    name = fields.Char(string='Name', required=True)
    ebusiness_id = fields.Char(string='Ebusiness')
    street = fields.Char(string='Street')
    street2 = fields.Char(string='Street 2')
    phone = fields.Char(string='Phone')
    mobile = fields.Char(string='Mobile')
    fax = fields.Char(string='Fax')
    email = fields.Char(string='Email')
    default_contact = fields.Boolean(string='Default Contact')
    nif = fields.Char(string='NIF')
    id_number = fields.Char(string='Id number')
    type = fields.Char(string='Type')

    @api.multi
    def action_load_contacts(self):
        """Load contacts
        """
        contact_ids = self.env['ecom.view.contacts'].search([('id','>',0)])
        for contact in contact_ids:
            contact_name=contact.name
            contact_ebusiness=contact.ebusiness_id
            contact_street=contact.street
            contact_street2=contact.street2
            contact_phone=contact.phone
            contact_mobile=contact.mobile
            contact_fax=contact.fax
            contact_email=contact.email
            contact_default_contact=contact.default_contact
            contact_id_number=contact.id_number
            contact_nif=contact.nif
            
            self.env['res.partner'].create({
                    'name': contact_name,
                    'ebusiness_id': contact_ebusiness,
                    'street': contact_street,
                    'street2': contact_street2,
                    'phone': contact_phone,
                    'mobile': contact_mobile,
                    'fax': contact_fax,
                    'email': contact_email,
                    'default_contact': contact_default_contact,
                    'id_number': contact_id_number,
                    'nif': contact_nif,
                    'customer': False
                        })	