from odoo import api, fields, models
from datetime import datetime,timedelta,date
import locale
import threading
from num2words import num2words
from contextlib import contextmanager
class DonationTaxReceipt(models.Model):
_inherit = "donation.tax.receipt"
template_rf_id=fields.Many2one('opendons.template_rf', 'RF template')
html_content_print=fields.Html('html content print')
css_print=fields.Text('css')
pdf_file=fields.Binary("PDF")
@api.model
def create(self, values):
res = super(DonationTaxReceipt, self).create(values)
#generate the taxreceipt in HTMl format for PDF print
template_rf=self.env['opendons.template_rf'].search([('type_rf','=','generic'),('active','=',True)],limit=1)
res.css_print=template_rf.css
html_content_print=template_rf.html_content
html_content_print=html_content_print.replace('{{partner_id.name}}',str(res.partner_id.name))
html_content_print=html_content_print.replace('{{partner_id.firstname}}',str(res.partner_id.firstname))
html_content_print=html_content_print.replace('{{adresse}}',str(res.update_adresse()))
html_content_print=html_content_print.replace('{{number}}',str(res.number))
locale.setlocale(locale.LC_ALL, 'fr_FR.UTF-8')
today=datetime.today()
html_content_print=html_content_print.replace('{{date}}',today.strftime("%d %B %Y"))
html_content_print=html_content_print.replace('{{donor_id}}',str(self.partner_id.donor_id))
html_content_print=html_content_print.replace('{{fiscalyear}}',str(int(today.strftime("%Y"))-1))
html_content_print=html_content_print.replace('{{amount}}',str(round(res.amount,0)))
html_content_print=html_content_print.replace('{{amountstr}}',num2words(round(float(res.amount),0),lang="fr",to="currency"))
res.html_content_print=html_content_print
res.template_rf_id=template_rf.id
return res
def get_portal_url(self):
return "/my/taxreceipt/print?id="+str(self.id)
def action_print_rf(self):
self.ensure_one()
self.print_date=datetime.now()
return self.env.ref("opendons.report_donation_tax_receipt").report_action(self)
def _html_content_rf(self):
self.html_content=self.template_rf_id.html_content
def update_adresse(self):
p=self.env['res.partner'].search([('id','=',int(self.partner_id))])
result=''
#prise en compte de l'adresse fiscale ou non
if p.tax_street and p.tax_zip and p.city:
title=p.title.name if p.title.name else ''
name=p.name if p.name else ''
firstname=p.firstname if p.firstname else ''
street=p.tax_street if p.tax_street else ''
street2='
'+p.tax_street2 if p.tax_street2 else ''
locality='
'+p.tax_locality if p.tax_locality else ''
zip=p.tax_zip if p.tax_zip else ''
city=p.tax_city if p.tax_city else ''
result='
'+title+' '+name+' '+firstname+'
'+street+street2+locality+'
'+zip+' '+city+'
'+title+' '+name+' '+firstname+'
'+street+street2+locality+'
'+zip+' '+city+'
Monsieur Dupont Marcel
7 avenue de la Marne
75016 Paris
" return result class DonationTaxReceiptPrint(models.TransientModel): _inherit = "donation.tax.receipt.print" def print_receipts(self): self.ensure_one() if not self.receipt_ids: raise UserError(_("There are no tax receipts to print.")) today = fields.Date.context_today(self) return self.env.ref("opendons.report_donation_tax_receipt").report_action(self.receipt_ids)