﻿# -*- coding: utf-8 -*-
##############################################################################
#
#    Copyright (C) 2014 Compassion CH (http://www.compassion.ch)
#    Releasing children from poverty in Jesus' name
#    @author: Emanuel Cino <ecino@compassion.ch>
#
#    The licence is in the file __manifest__.py
#
##############################################################################

from odoo import api, models
from odoo.tools import mod10r
from odoo.addons.sponsorship_compassion.models.product import GIFT_NAMES

import logging

logger = logging.getLogger(__name__)


class GenerateGiftWizard(models.TransientModel):
    """ This wizard generates a Gift Invoice for a given contract. """
    _inherit = 'generate.gift.wizard'

    @api.multi
    def _setup_invoice(self, contract, invoice_date):
        res = super(GenerateGiftWizard, self)._setup_invoice(contract,
                                                             invoice_date)
        res['reference'] = self.generate_bvr_reference(
            contract, self.product_id)
        return res

    @api.model
    def generate_bvr_reference(self, contract, product):
        product = product.with_context(lang='en_US')
        ref = contract.gift_partner_id.ref
        bvr_reference = '0' * (9 + (7 - len(ref))) + ref
        commitment_number = str(contract.commitment_number)
        bvr_reference += '0' * (5 - len(commitment_number)) + commitment_number
        # Type of gift
        bvr_reference += str(GIFT_NAMES.index(product.name) + 1)
        bvr_reference += '0' * 4

        if contract.payment_mode_id and 'LSV' in contract.payment_mode_id.name:
            # Get company BVR adherent number
            user = self.env.user
            bank_obj = self.env['res.partner.bank']
            company_bank = bank_obj.search([
                ('partner_id', '=', user.company_id.partner_id.id),
                ('lsv_identifier', '!=', False)])
            if company_bank:
                bvr_reference = company_bank.bvr_adherent_num +\
                    bvr_reference[9:]
        if len(bvr_reference) == 26:
            return mod10r(bvr_reference)

        return False

@@ -396,17 +396,16 @@
 #######%0A
-%0A
 from odo
@@ -424,16 +424,24 @@
 , models
+, fields
 %0Afrom od
@@ -1073,18 +1073,741 @@
     
-return res
+self._maybe_update_date_for_lsv_dd(res)%0A        return res%0A%0A    @api.model%0A    def _maybe_update_date_for_lsv_dd(self, res):%0A        %22%22%22 Invoices for gift should be set at the beginning of the month to be%0A        processed together with the sponsorship for the month.%22%22%22%0A        payment_mode_id = res%5B'payment_mode_id'%5D%0A        lsv = self.env.ref('sponsorship_switzerland.payment_mode_lsv').ids%0A        dd = self.env.ref(%0A            'sponsorship_switzerland.payment_mode_postfinance_dd').ids%0A        if payment_mode_id in lsv + dd:%0A            date_invoice = fields.Date.from_string(res%5B'date_invoice'%5D)%0A            start_of_month = date_invoice.replace(day=1)%0A            res%5B'date_invoice'%5D = fields.Date.to_string(start_of_month)
 %0A%0A  
@@ -2757,16 +2757,17 @@
 nt_num +
+ 
 %5C%0A      

Modify the invoice_date for gift payed with lsv and dd.