<commit_before># © 2014-2016 <NAME> (http://www.barroux.org)
# © 2014-2016 Akretion France (<NAME> <<EMAIL>>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models, fields, api, _
from odoo.exceptions import UserError


class DonationTaxReceipt(models.Model):
    _name = 'donation.tax.receipt'
    _inherit = ['mail.thread']
    _description = "Tax Receipt for Donations"
    _order = 'id desc'
    _rec_name = 'number'

    number = fields.Char(string='Receipt Number')
    date = fields.Date(
        string='Date',
        required=True,
        default=fields.Date.context_today,
        index=True
    )
    donation_date = fields.Date(
        string='Donation Date'
    )
    amount = fields.Monetary(
        string='Amount',
        currency_field='currency_id'
    )
    currency_id = fields.Many2one(
        'res.currency',
        string='Currency',
        required=True,
        ondelete='restrict',
        default=lambda self: self.env.user.company_id.currency_id.id
    )
    partner_id = fields.Many2one(
        'res.partner',
        string='Donor',
        required=True,
        ondelete='restrict',
        domain=[('parent_id', '=', False)],
        index=True
    )
    company_id = fields.Many2one(
        'res.company',
        string='Company',
        required=True,
        default=lambda self: self.env['res.company'].
        _company_default_get('donation.tax.receipt')
    )
    print_date = fields.Date(string='Print Date')
    type = fields.Selection([
        ('each', 'One-Time Tax Receipt'),
        ('annual', 'Annual Tax Receipt')],
        string='Type',
        required=True
    )

    # Maybe we can drop that code with the new seq management on v9
    @api.model
    def create(self, vals=None):
        if vals is None:
            vals = {}
        date = vals.get('donation_date')
        vals['number'] = self.env['ir.sequence'].with_context(
            date=date).next_by_code('donation.tax.receipt')
        return super(DonationTaxReceipt, self).create(vals)

    @api.model
    def update_tax_receipt_annual_dict(
            self, tax_receipt_annual_dict, start_date, end_date,
            precision_rounding):
        '''This method is inherited in donation and donation_sale
        It is called by the tax.receipt.annual.create wizard'''

    @api.multi
    def action_send_tax_receipt(self):
        self.ensure_one()
        if not self.partner_id.email:
            raise UserError(_(
                "Missing email on partner '%s'.")
                % self.partner_id.name_get()[0][1])
        template = self.env.ref('donation_base.tax_receipt_email_template')
        compose_form = self.env.ref('mail.email_compose_message_wizard_form')
        ctx = dict(
            default_model='donation.tax.receipt',
            default_res_id=self.id,<commit_msg>[FIX] tax.receipt creation sequence<commit_after># © 2014-2016 <NAME> (http://www.barroux.org)
# © 2014-2016 Akretion France (<NAME> <<EMAIL>>)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models, fields, api, _
from odoo.exceptions import UserError


class DonationTaxReceipt(models.Model):
    _name = 'donation.tax.receipt'
    _inherit = ['mail.thread']
    _description = "Tax Receipt for Donations"
    _order = 'id desc'
    _rec_name = 'number'

    number = fields.Char(string='Receipt Number')
    date = fields.Date(
        string='Date',
        required=True,
        default=fields.Date.context_today,
        index=True
    )
    donation_date = fields.Date(
        string='Donation Date'
    )
    amount = fields.Monetary(
        string='Amount',
        currency_field='currency_id'
    )
    currency_id = fields.Many2one(
        'res.currency',
        string='Currency',
        required=True,
        ondelete='restrict',
        default=lambda self: self.env.user.company_id.currency_id.id
    )
    partner_id = fields.Many2one(
        'res.partner',
        string='Donor',
        required=True,
        ondelete='restrict',
        domain=[('parent_id', '=', False)],
        index=True
    )
    company_id = fields.Many2one(
        'res.company',
        string='Company',
        required=True,
        default=lambda self: self.env['res.company'].
        _company_default_get('donation.tax.receipt')
    )
    print_date = fields.Date(string='Print Date')
    type = fields.Selection([
        ('each', 'One-Time Tax Receipt'),
        ('annual', 'Annual Tax Receipt')],
        string='Type',
        required=True
    )

    @api.model
    def create(self, vals):
        date = vals.get('donation_date')
        if vals.get('name', '/') == '/':
            seq = self.env['ir.sequence']
            vals['name'] = seq.with_context(
                date=date
            ).next_by_code('donation.tax.receipt') or '/'
        return super(DonationTaxReceipt, self).create(vals)

    @api.model
    def update_tax_receipt_annual_dict(
            self, tax_receipt_annual_dict, start_date, end_date,
            precision_rounding):
        '''This method is inherited in donation and donation_sale
        It is called by the tax.receipt.annual.create wizard'''

    @api.multi
    def action_send_tax_receipt(self):
        self.ensure_one()
        if not self.partner_id.email:
            raise UserError(_(
                "Missing email on partner '%s'.")
                % self.partner_id.name_get()[0][1])
        template = self.env.ref('donation_base.tax_receipt_email_template')
        compose_form = self.env.ref('mail.email_compose_message_wizard_form')
        ctx = dict(
            default_model='donation.tax.receipt',
            default_res_id=self.id,
{'fraction_non_alphanumeric': 0.12510951463115472, 'fraction_numerical': 0.008235500262835115, 'mean_word_length': 2.4364840457555688, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5926732', 'n_tokens_mistral': 1862, 'n_tokens_neox': 1773, 'n_words': 344}