# -*- coding: utf-8 -*-
# (c) 2016 Alfredo de la Fuente - AvanzOSC
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
from openerp import models, fields, api


class AccountAnalyticAccount(models.Model):
    _inherit = 'account.analytic.account'

    student = fields.Many2one(
        comodel_name='res.partner', string='Student')


class AccountInvoice(models.Model):
    _inherit = 'account.invoice'

    student = fields.Many2one(
        comodel_name='res.partner', string='Student')
    event_id = fields.Many2one(
        comodel_name='event.event', string='Event')
    sale_order_id = fields.Many2one(
        comodel_name='sale.order', string='Sale order')
    event_address_id = fields.Many2one(
        comodel_name='res.partner', string='Event address')

    @api.model
    @api.returns('self', lambda value: value.id)
    def create(self, vals):
        account_obj = self.env['account.analytic.account']
        registration_obj = self.env['event.registration']
        if (vals.get('origin', False) and
                vals.get('type', False) == 'out_invoice'):
            cond = [('code', '=', vals.get('origin'))]
            account = account_obj.search(cond, limit=1)
            if account.sale.payer == 'school':
                vals['journal_id'] = self.env.ref(
                    'event_registration_analytic.school_journal').id
            if account.sale.payer == 'student':
                vals['journal_id'] = self.env.ref(
                    'event_registration_analytic.student_journal').id
                vals['student'] = account.student.id
                cond = [('analytic_account', '=', account.id)]
                registration = registration_obj.search(cond, limit=1)
                if registration.event_id:
                    vals['event_id'] = registration.event_id.id
                if registration.event_id.sale_order:
                    vals['sale_order_id'] = registration.event_id.sale_order.id
                if registration.event_id.address_id:
                    vals['event_address_id'] = (
                        registration.event_id.address_id.id)
        return super(AccountInvoice, self).create(vals)

@@ -1211,260 +1211,338 @@
     
-if account.sale.payer == 'school':%0A                vals%5B'journal_id'%5D = self.env.ref(%0A                    'event_registration_analytic.school_journal').id%0A            if account.sale.payer == 'student':%0A                vals%5B'journal_id'%5D = self.env.ref
+cond = %5B('analytic_account', '=', account.id)%5D%0A            registration = registration_obj.search(cond, limit=1)%0A            if registration.event_id.sale_order:%0A                vals%5B'sale_order_id'%5D = registration.event_id.sale_order.id%0A            if registration.event_id.address_id:%0A                vals%5B'event_address_id'%5D = 
 (%0A  
@@ -1559,23 +1559,16 @@
         
-'event_
 registra
@@ -1575,154 +1575,130 @@
 tion
-_analytic.student_journal').id%0A                vals%5B'student'%5D = account.student.id%0A                cond = %5B('analytic_account', '=', account.id)%5D
+.event_id.address_id.id)%0A            if account.sale.payer == 'school':%0A                vals%5B'journal_id'%5D = self.env.ref(
 %0A   
@@ -1702,32 +1702,43 @@
                 
+    'event_
 registration = r
@@ -1737,96 +1737,86 @@
 tion
- = registration_obj.search(cond, limit=1)%0A                if registration.event_id:%0A    
+_analytic.school_journal').id%0A            if account.sale.payer == 'student':%0A
     
@@ -1837,21 +1837,23 @@
 ls%5B'
-event
+journal
 _id'%5D = 
 regi
@@ -1852,32 +1852,21 @@
 %5D = 
-registration.event_id.id
+self.env.ref(
 %0A   
@@ -1870,35 +1870,43 @@
                 
-if 
+    'event_
 registration.eve
@@ -1905,106 +1905,88 @@
 tion
-.event_id.sale_order:%0A                    vals%5B'sale_order_id'%5D = registration.event_id.sale_order
+_analytic.student_journal').id%0A                vals%5B'student'%5D = account.student
 .id%0A
@@ -2021,35 +2021,24 @@
 ion.event_id
-.address_id
 :%0A          
@@ -2063,48 +2063,14 @@
 ent_
-address_id'%5D = (%0A                       
+id'%5D =
  reg
@@ -2084,38 +2084,26 @@
 on.event_id.
-address_id.id)
+id
 %0A        ret

Sale and event address in invoice, when de payer is the school.