from odoo import models, fields, api from odoo.exceptions import UserError, ValidationError, Warning from psycopg2 import sql, DatabaseError from dateutil.relativedelta import relativedelta from datetime import datetime from werkzeug import utils import json class EventRegistration(models.Model): _inherit = 'event.registration' online=fields.Boolean(string='Online participation') onthespot_payment=fields.Boolean(string='On the spot payment') free_participation=fields.Boolean(related='event_id.free_participation') with_membership=fields.Boolean('with membership') def action_generate_participation_invoice(self,id_registration=None,payment_mode=None,membership=False): if not id_registration:id_registration=int(self.id) reg=self.env['event.registration'].search([('id','=',id_registration)]) event=self.env['event.event'].search([('id','=',int(reg.event_id))]) if membership: membership_product=self.env['product.product'].search([('membership_product','=',True)],limit=1) if not membership_product: raise UserError(_('No membership product, please add one')) #Prix à appliquer au produit en fonction du statut de l'inscrit status=reg.partner_id.member_status if status=='not member':product_price=event.participation_standard_price if status=='member':product_price=event.participation_member_price if status=='super member':product_price=event.participation_super_member_price product_id=event.participation_product_id #création de la facture vals={} vals['partner_id']=int(reg.partner_id) vals['invoice_date']=datetime.now() #mode de paiement CB if payment_mode=='CB': electronic_method=self.env['account.payment.method'].search([('code','=','electronic')],limit=1) if electronic_method: cb_mode=self.env['account.payment.mode'].search([('payment_method_id','=',int(electronic_method.id))],limit=1) if cb_mode: vals['payment_mode_id']=cb_mode.id else: raise Warning('please configure credit card mode') vals['move_type']='out_invoice' vals['state']='draft' invoice=self.env['account.move'].create(vals) #invoice.state='posted' invoice.name='REC'+str(invoice.id) invoice.payment_reference=invoice.name #creation des écritures comptable name=event.participation_product_id.name if membership and status=='not member': product_price=product_price+membership_product.list_price name=event.participation_product_id.name+"+"+membership_product.name vals={} account_credit=self.env['account.account'].search([('code','=','707100')]) account_debit=self.env['account.account'].search([('code','=','411100')]) vals['move_id']=invoice.id vals['product_id']=int(event.participation_product_id) vals['quantity']=1 vals['price_unit']=product_price vals['name']=name vals['account_id']=int(account_credit.id) invoice_line=self.env['account.move.line'].with_context(check_move_validity=False).create(vals) # #debit_line vals_d={} vals_d['move_id']=invoice.id vals_d['debit']=product_price vals_d['credit']=0 vals_d['date']=datetime.now() vals_d['partner_id']=int(reg.partner_id) vals_d['product_id']=int(event.participation_product_id) vals_d['name']=name vals_d['account_id']=int(account_debit.id) vals_d['quantity']=1 vals_d['price_unit']=product_price vals_d['exclude_from_invoice_tab']=True invoice_line=self.env['account.move.line'].with_context(check_move_validity=False).create(vals_d) l=self.env['account.move.line'].search([('move_id','=',invoice.id),('balance','<',0)]) l.partner_id=int(reg.partner_id) reg.invoice_id=invoice.id return invoice.id