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

class ResUsers(models.Model):
    _inherit = "res.users"

    enterprise_id = fields.Many2one('etourism.enterprise', 'Enterprise')
    island_ids = fields.Many2many('ecom.location', 'ecom_location_user_rel', 'user_id', 'location_id', 'Islands', track_visibility='onchange')

    @api.multi
    def etourism_get_next_sequence(self, prefix, model, sequence_field, enterprise_id=False):
        now, current_year = datetime.datetime.now(), ''
        current_year = str(now.year)
		
        if not enterprise_id:
            raise ValidationError('Missing Required field: Enterprise Id')
        
        if enterprise_id:
            enterprise = self.env['etourism.enterprise'].search([('id','=',enterprise_id)])
            
            ebusiness = enterprise.ebusiness_id
            if not ebusiness:
                raise ValidationError('Ebusiness reference not found for Enterprise %s'%(enterprise.name))
            
            sequence_code = ebusiness.sequence_code
            if not sequence_code:
                raise ValidationError('Sequence code not found for Ebusiness %s'%(ebusiness.name))

            if model == 'account.payment':
                print "IF MODEL PAYMENT:::::::::::::"
                print "CURRENT YEAR::::::::::::::::::::"
                print current_year
                sequence_format = '/'.join([prefix, sequence_code]) + '/' + current_year + '/'
                print "SEQUENCE FORMAT::::::::::::::::::::"
                print sequence_format
            else:
                sequence_format = '/'.join([prefix, sequence_code]) + '/'
            res_id = self.env[model].search([(sequence_field, 'ilike', sequence_format)], order="name desc", limit=1)
            result = self.get_next_sequence(res_id, sequence_format)

        return result

    @api.multi
    def get_next_sequence(self, res_id, sequence_format):
        result = False
        if not res_id:
            result = sequence_format + '0001'
        else:
            last_sequence = res_id.name.split('/')[-1]
            if last_sequence.isdigit():
                new_sequence = str(int(last_sequence) + 1)
                while len(new_sequence) < 4:
                    new_sequence = '0' + new_sequence
                result = sequence_format + new_sequence
        return result

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