#!/usr/bin/env python3 """ Script de création de factures pour Odoo 17 Génère automatiquement les factures pour les commandes confirmées """ import xmlrpc.client # Configuration url = 'http://localhost:8069' db = 'covoiturage_db' username = 'admin' password = 'admin' # Connexion à Odoo print("🔌 Connexion à Odoo...") common = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(url)) uid = common.authenticate(db, username, password, {}) if not uid: print("❌ Erreur: Impossible de se connecter à Odoo") exit(1) print(f"✅ Connecté en tant qu'utilisateur ID: {uid}\n") models = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url)) print("="*60) print("GÉNÉRATION DES FACTURES - ODOO 17") print("="*60) # Récupérer toutes les commandes confirmées sans facture print("\n🔍 Recherche des commandes à facturer...") orders = models.execute_kw(db, uid, password, 'sale.order', 'search_read', [[('state', 'in', ['sale', 'done']), ('invoice_status', 'in', ['to invoice', 'no'])]], {'fields': ['name', 'partner_id', 'amount_total', 'invoice_status']}) if not orders: print(" ℹ️ Aucune commande à facturer") else: print(f" ✓ {len(orders)} commande(s) à facturer\n") for order in orders: print(f"📝 Traitement de la commande {order['name']}...") print(f" Client: {order['partner_id'][1]}") print(f" Montant: {order['amount_total']}€") try: # Méthode Odoo 17: Créer une facture via _create_invoices() # Cette méthode retourne les factures créées invoice_ids = models.execute_kw( db, uid, password, 'sale.order', '_create_invoices', [[order['id']]] ) if invoice_ids: # Récupérer les informations de la facture invoices = models.execute_kw( db, uid, password, 'account.move', 'read', [invoice_ids], {'fields': ['name', 'state', 'amount_total', 'invoice_date']} ) for invoice in invoices: print(f" ✅ Facture créée: {invoice['name']}") print(f" État: {invoice['state']}") print(f" Montant: {invoice['amount_total']}€") # Valider la facture (action_post) if invoice['state'] == 'draft': try: models.execute_kw( db, uid, password, 'account.move', 'action_post', [[invoice['id']]] ) print(f" ✅ Facture validée et comptabilisée") except Exception as e: print(f" ⚠️ Impossible de valider: {str(e)[:60]}") else: print(f" ⚠️ Impossible de créer la facture") except Exception as e: error_msg = str(e) if "already invoiced" in error_msg.lower(): print(f" ℹ️ Commande déjà facturée") else: print(f" ❌ Erreur: {error_msg[:100]}") print() # Statistiques finales print("="*60) print("📊 STATISTIQUES FINALES") print("="*60) # Compter les factures nb_invoices = models.execute_kw(db, uid, password, 'account.move', 'search_count', [[('move_type', '=', 'out_invoice')]]) nb_posted = models.execute_kw(db, uid, password, 'account.move', 'search_count', [[('move_type', '=', 'out_invoice'), ('state', '=', 'posted')]]) nb_draft = models.execute_kw(db, uid, password, 'account.move', 'search_count', [[('move_type', '=', 'out_invoice'), ('state', '=', 'draft')]]) print(f"📄 Factures totales: {nb_invoices}") print(f" - Validées: {nb_posted}") print(f" - Brouillon: {nb_draft}") # Calculer le CA facturé invoices = models.execute_kw(db, uid, password, 'account.move', 'search_read', [[('move_type', '=', 'out_invoice'), ('state', '=', 'posted')]], {'fields': ['amount_total']}) ca_facture = sum([inv['amount_total'] for inv in invoices]) print(f"💰 CA facturé: {ca_facture:.2f}€") print("\n" + "="*60) print("✅ FACTURATION TERMINÉE!") print("="*60) print("\n📌 Prochaines étapes:") print(" 1. Vérifier les factures: Facturation > Clients > Factures") print(" 2. Enregistrer les paiements: Facturation > Clients > Paiements") print(" 3. Consulter le tableau de bord: Facturation > Reporting")