# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero 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 Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp import models, fields, api


class SaleOrder(models.Model):
    _inherit = 'sale.order'

    @api.onchange('project_id')
    def _analytic_leaves(self):
        project = self.env['project.hierarchy'].browse(self._context.get('proj_id'))
        if not project and self.project_id:
            project = self.env['project.hierarchy'].search([('account_id', '=', self.project_id.id)])
            self.proj_id = project.id
        if project:
            self.project_id = project.account_id
            self.hotel = project.account_id
            self.hotel_name = self.hotel.name
            self.project = project.account_id.get_recursive_parent().pop()
            self.partner_id = self.project_id.partner_id
            res = self.onchange_partner_id(self.partner_id.id, context=self._context)['value']
            self.partner_invoice_id = self.partner_id
            if 'pricelist_id' in res:
                self.pricelist_id = self.env['product.pricelist'].browse(res['pricelist_id'])
            if 'user_id' in res:
                self.user_id = self.env['res.users'].browse(res['user_id'])
            self.partner_shipping_id = self.partner_id
            self.payment_term = False

        return {'domain': {'project_id': [('child_ids', '=', False)]}}

    @api.model
    def create(self, vals):
        oid = super(SaleOrder, self).create(vals)
        hotel = oid.proj_id
        hotel.write({'child_ids': [(0, 0, {'name': oid.name})]})
        return oid

    proj_id = fields.Many2one(comodel_name='project.hierarchy', string='Contract / Analytic')
    project_id = fields.Many2one('account.analytic.account', 'Contract / Analytic', readonly=True,
                                 states={'draft': [('readonly', False)], 'sent': [('readonly', False)]},
                                 help="The analytic account related to a sales order.")
    project = fields.Many2one('account.analytic.account', 'Project', required=True, readonly=True,
                              states={'draft': [('readonly', False)], 'sent': [('readonly', False)]},
                              help="The analytic account related to a sales order.")
    hotel = fields.Many2one('account.analytic.account', 'Hotel / Department', readonly=True,
                            states={'draft': [('readonly', False)], 'sent': [('readonly', False)]},
                            help="The analytic account related to a sales order.")
    hotel_name = fields.Char('Hotel / Department')
