3923 lines
190 KiB
Python
3923 lines
190 KiB
Python
"""
|
|
Ce fichier permet de gerer la facturation des commandes des partenaires vers leur client.
|
|
|
|
Il se base sur les commandes faite à l'aide du fichier "partner_order.py"
|
|
|
|
"""
|
|
import ast
|
|
from calendar import monthrange
|
|
|
|
import bson
|
|
import pymongo
|
|
from pymongo import MongoClient
|
|
import json
|
|
from bson import ObjectId
|
|
import re
|
|
from datetime import datetime, timedelta, date
|
|
|
|
import Session_Formation
|
|
import invoice_paiement_mgt
|
|
import partners
|
|
import prj_common as mycommon
|
|
import secrets
|
|
import inspect
|
|
import sys, os
|
|
import csv
|
|
import pandas as pd
|
|
from pymongo import ReturnDocument
|
|
import GlobalVariable as MYSY_GV
|
|
from math import isnan
|
|
import GlobalVariable as MYSY_GV
|
|
import ela_index_bdd_classes as eibdd
|
|
import email_mgt as email
|
|
import jinja2
|
|
from flask import send_file
|
|
from xhtml2pdf import pisa
|
|
from email.message import EmailMessage
|
|
from email.mime.text import MIMEText
|
|
from email import encoders
|
|
import smtplib
|
|
from email.mime.multipart import MIMEMultipart
|
|
from email.mime.text import MIMEText
|
|
from email.mime.base import MIMEBase
|
|
from email import encoders
|
|
import partner_order as partner_order
|
|
import base64
|
|
|
|
"""
|
|
Creation de la facture d'un commande
|
|
"""
|
|
def Invoice_Partner_Order(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', 'order_id', 'order_ref_interne']
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list and val.startswith('my_') is False:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'est pas autorisé")
|
|
return False, " Les informations fournies sont incorrectes", False
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token', 'order_id', 'order_ref_interne']
|
|
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans la liste des arguments ")
|
|
return False, " Les informations fournies sont incorrectes",False
|
|
|
|
"""
|
|
Verification de l'identité et autorisation de l'entité qui
|
|
appelle cette API
|
|
"""
|
|
token = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
token = diction['token']
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner, False
|
|
|
|
|
|
# Verification de la validité de la commande à facturer
|
|
order_to_invoice_data_count = MYSY_GV.dbname['partner_order_header'].count_documents(
|
|
{'_id': ObjectId(str(diction['order_id'])), 'order_header_ref_interne': str(diction['order_ref_interne']),
|
|
'partner_owner_recid': my_partner['recid'], 'order_header_type':'commande', 'valide': '1',
|
|
'locked': '0'})
|
|
|
|
if( order_to_invoice_data_count < 0 ):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - Les references de la commande sont invalides ")
|
|
return False, " Les references de la commande sont invalides", False
|
|
|
|
if (order_to_invoice_data_count > 1):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - Les references correspondent à plusieurs commandes. Facturation annulée. ")
|
|
return False, " Les references correspondent à plusieurs commandes. Facturation annulée.",False
|
|
|
|
|
|
|
|
|
|
order_to_invoice_data = MYSY_GV.dbname['partner_order_header'].find_one({'_id':ObjectId(str(diction['order_id'])), 'order_header_ref_interne':str(diction['order_ref_interne']),
|
|
'partner_owner_recid':my_partner['recid'], 'valide':'1',
|
|
'locked':'0', 'order_header_type':'commande'})
|
|
|
|
print(" #### order_to_invoice_data = ", order_to_invoice_data);
|
|
if( order_to_invoice_data is None or str(order_to_invoice_data['order_header_status']) != "2" ):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " - La commande n'est pas au statut 'Traité'. Facturation annulée. ")
|
|
return False, " La commande n'est pas au statut 'Traité'. Facturation annulée.",False
|
|
|
|
|
|
# Verifier que toutes lignes sont au statut 'traité'
|
|
nb_line_a_facturer = 0
|
|
for order_lines_to_invoice_data in MYSY_GV.dbname['partner_order_line'].find(
|
|
{'order_header_id': str(diction['order_id']), 'order_header_ref_interne': str(diction['order_ref_interne']),
|
|
'partner_owner_recid': my_partner['recid']}):
|
|
nb_line_a_facturer = nb_line_a_facturer +1
|
|
if( str(order_lines_to_invoice_data['order_line_type']) != "commande" or str(order_lines_to_invoice_data['order_line_status']) != "2"
|
|
or str(order_lines_to_invoice_data['valide']) != "1" or str(order_lines_to_invoice_data['locked']) != "0"):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " - La ligne de commande "+str(order_lines_to_invoice_data['order_line_formation'])+" avec la Quantite "+ str(order_lines_to_invoice_data['order_line_qty'])+" n'est pas cohérente. Facturation annulée. ")
|
|
return False, " La ligne de commande "+str(order_lines_to_invoice_data['order_line_formation'])+" avec la Quantite "+ str(order_lines_to_invoice_data['order_line_qty'])+" n'est pas cohérente. Facturation annulée.",False
|
|
|
|
if( nb_line_a_facturer == 0):
|
|
# Alors il n'y a aucune ligne à facturer
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " - Il n'y a aucune ligne à facturer. Facturation annulée. ")
|
|
return False, "Il n'y a aucune ligne à facturer. Facturation annulée.",False
|
|
|
|
# Verification de la validité du client
|
|
is_client_valide = MYSY_GV.dbname['partner_client'].count_documents({'_id':ObjectId(str(order_to_invoice_data['order_header_client_id'])),
|
|
'valide':'1', 'locked':'0', 'partner_recid':str(my_partner['recid'])})
|
|
|
|
if( is_client_valide != 1):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " - Le client à facturer est invalide. Facturation annulée. ")
|
|
return False, "Le client à facturer est invalide. Facturation annulée.",False
|
|
|
|
"""
|
|
A présent la commande est valide, on va
|
|
0 - Mettre à jour la commande en mettant le statut "facturé' ==> comme ca on bloque d'evenuelles modification de la commande
|
|
1 - Relancer un compute de la commande
|
|
2 - copier les données dans la collection 'facture'
|
|
3 - on met à jour les lignes de la commande, pr la mettre à facturé
|
|
"""
|
|
|
|
# 0 - Mettre à jour la commande en mettant le statut "facturé' ==> comme ca on bloque d'evenuelles modification de la commande
|
|
order_updated = MYSY_GV.dbname['partner_order_header'].update_one({'_id':ObjectId(str(diction['order_id'])), 'order_header_ref_interne':str(diction['order_ref_interne']),
|
|
'partner_owner_recid':my_partner['recid'], 'valide':'1',
|
|
'locked':'0', 'order_header_type':'commande'},
|
|
{'$set':{'order_header_status':'3'}
|
|
}
|
|
)
|
|
|
|
if(order_updated.modified_count != 1 ):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " - Impossible de verrouiller la commande à facturer. Facturation annulée. ")
|
|
return False, "Impossible de verrouiller la commande à facturer. Facturation annulée.",False
|
|
|
|
|
|
# 1 - Relancer un compute de la commande
|
|
comput_diction = {}
|
|
comput_diction['token'] = diction['token']
|
|
comput_diction['_id'] = diction['order_id']
|
|
|
|
local_retval, local_message = partner_order.Compute_Order_Header(comput_diction)
|
|
if (local_retval is False):
|
|
order_updated = MYSY_GV.dbname['partner_order_header'].update_one({{'_id': ObjectId(
|
|
str(diction['order_id'])), 'order_header_ref_interne': str(diction['order_ref_interne']),
|
|
'partner_owner_recid': my_partner[
|
|
'recid'], 'valide': '1',
|
|
'locked': '0',
|
|
'order_header_type': 'commande'},
|
|
{'$set': {'order_header_status': '2'}}
|
|
})
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " - Impossible de mettre à jour les prix de la commande à facturer. Facturation annulée. ")
|
|
return False, "Impossible de mettre à jour les prix de la commande à facturer. Facturation annulée.",False
|
|
|
|
|
|
|
|
#2 - copier les données dans la collection 'facture'
|
|
"""
|
|
/!\ On va recopier les header et line, telqel.
|
|
On fera les changements plus tard si besoin
|
|
"""
|
|
|
|
|
|
# Récuperation de la sequence de l'objet "partner_invoice_header" dans la collection : "mysy_sequence"
|
|
retval_sequence_invoice = MYSY_GV.dbname['mysy_sequence'].find_one({'partner_invoice_header': 'partner_order_header',
|
|
'valide': '1', 'partner_owner_recid': str(
|
|
my_partner['recid'])})
|
|
|
|
if (retval_sequence_invoice is None):
|
|
# Il n'y pas de sequence pour le partenaire, on va aller chercher la sequence par defaut
|
|
retval_sequence_invoice = MYSY_GV.dbname['mysy_sequence'].find_one(
|
|
{'related_mysy_object': 'partner_invoice_header',
|
|
'valide': '1', 'partner_owner_recid': 'default'})
|
|
|
|
if (retval_sequence_invoice is None or "current_val" not in retval_sequence_invoice.keys()):
|
|
# Il n'y aucune sequence meme par defaut.
|
|
order_updated = MYSY_GV.dbname['partner_order_header'].update_one({{'_id': ObjectId(
|
|
str(diction['order_id'])), 'order_header_ref_interne': str(diction['order_ref_interne']),
|
|
'partner_owner_recid': my_partner[
|
|
'recid'], 'valide': '1',
|
|
'locked': '0',
|
|
'order_header_type': 'commande'},
|
|
{'$set': {'order_header_status': '2'}}
|
|
})
|
|
|
|
mycommon.myprint(" Impossible de récupérer la sequence 'retval_sequence_invoice' ")
|
|
return False, "Impossible de récupérer la sequence 'retval_sequence_invoice'", False
|
|
|
|
current_seq_value = str(retval_sequence_invoice['current_val'])
|
|
new_sequence_value = int(mycommon.tryInt(current_seq_value)) + 1
|
|
new_sequance_data_to_update = {'current_val': new_sequence_value}
|
|
ret_val2 = MYSY_GV.dbname['mysy_sequence'].find_one_and_update(
|
|
{'_id': ObjectId(str(retval_sequence_invoice['_id'])), 'valide': '1'},
|
|
{"$set": new_sequance_data_to_update},
|
|
return_document=ReturnDocument.AFTER,
|
|
upsert=False,
|
|
)
|
|
invoice_date_time = str(datetime.now().strftime("%d/%m/%Y"))
|
|
new_invoice_data_header = MYSY_GV.dbname['partner_order_header'].find_one({'_id':ObjectId(str(diction['order_id'])), 'order_header_ref_interne':str(diction['order_ref_interne']),
|
|
'partner_owner_recid':my_partner['recid'], 'valide':'1',
|
|
'locked':'0', 'order_header_type':'commande'}, {'_id':0, 'order_header_type':0, 'order_header_status':0})
|
|
|
|
print(" #### A COPIER new_invoice_data_header = ", str(new_invoice_data_header))
|
|
|
|
invoice_ref_interne = retval_sequence_invoice['prefixe']+str(current_seq_value)
|
|
new_invoice_data_header['invoice_header_ref_interne'] = invoice_ref_interne
|
|
new_invoice_data_header['invoice_header_type'] = "facture"
|
|
new_invoice_data_header['invoice_date'] = invoice_date_time
|
|
new_invoice_data_header['update_by'] = str(my_partner['_id'])
|
|
|
|
|
|
"""
|
|
Calcul de la date d'échance de la facture :
|
|
Si pas de conidtion de paiement, alors la date à la date du jour
|
|
"""
|
|
if( "order_header_condition_paiement_id" in new_invoice_data_header.keys() and
|
|
new_invoice_data_header['order_header_condition_paiement_id'] ) :
|
|
paiement_condition_data = MYSY_GV.dbname['base_partner_paiement_condition'].find_one({'partner_owner_recid':str(my_partner['recid']),
|
|
'_id':ObjectId(str(new_invoice_data_header['order_header_condition_paiement_id'])),
|
|
'valide':'1',
|
|
'locked':'0'})
|
|
|
|
if(paiement_condition_data is None or 'depart' not in paiement_condition_data.keys() or
|
|
'nb_jour' not in paiement_condition_data.keys() ):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " WARNING - DATE ECHEANCE : Condition de paiement invalide pour calculer la date d'échéance ")
|
|
|
|
|
|
if( paiement_condition_data['depart'] not in MYSY_GV.PAIEMENT_CONDITION_DEPART ):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " WARNING - DATE ECHEANCE : Condition de départ n'est pas dans la liste "+str(MYSY_GV.PAIEMENT_CONDITION_DEPART))
|
|
|
|
|
|
nb_jour_int = mycommon.tryInt(str(paiement_condition_data['nb_jour']))
|
|
today = datetime.today()
|
|
date_echance = datetime.today()
|
|
|
|
if (str(paiement_condition_data['depart']) == "mois"):
|
|
days_in_month = lambda dt: monthrange(dt.year, dt.month)[1]
|
|
first_day_next_month = today.replace(day=1) + timedelta(days_in_month(today))
|
|
date_echance = first_day_next_month + timedelta(days=nb_jour_int)
|
|
|
|
if( str(paiement_condition_data['depart']) == "facture") :
|
|
date_echance = today + timedelta(days=nb_jour_int)
|
|
|
|
date_echance = date_echance.strftime("%d/%m/%Y")
|
|
new_invoice_data_header['invoice_date_echeance'] = str(date_echance)
|
|
new_invoice_data_header['order_header_condition_paiement_code'] = str(paiement_condition_data['code'])
|
|
new_invoice_data_header['order_header_condition_paiement_description'] = str(paiement_condition_data['description'])
|
|
|
|
else:
|
|
today = datetime.today()
|
|
date_echance = datetime.today().strftime("%d/%m/%Y")
|
|
new_invoice_data_header['invoice_date_echeance'] = str(date_echance)
|
|
new_invoice_data_header['order_header_condition_paiement_code'] = ""
|
|
new_invoice_data_header['order_header_condition_paiement_description'] = ""
|
|
|
|
|
|
inserted_invoice_id = MYSY_GV.dbname['partner_invoice_header'].insert_one(new_invoice_data_header).inserted_id
|
|
if (not inserted_invoice_id):
|
|
order_updated = MYSY_GV.dbname['partner_order_header'].update_one({{'_id': ObjectId(
|
|
str(diction['order_id'])), 'order_header_ref_interne': str(diction['order_ref_interne']),
|
|
'partner_owner_recid': my_partner[
|
|
'recid'], 'valide': '1',
|
|
'locked': '0',
|
|
'order_header_type': 'commande'},
|
|
{'$set': {'order_header_status': '2'}}
|
|
})
|
|
mycommon.myprint(
|
|
" Impossible de créer l'entete de la facture ")
|
|
return False, "Impossible de créer l'entete de la facture ", False
|
|
|
|
|
|
for new_invoice_data_line in MYSY_GV.dbname['partner_order_line'].find( {'order_header_id': str(diction['order_id']), 'order_header_ref_interne': str(diction['order_ref_interne']),
|
|
'partner_owner_recid': my_partner['recid']}, {'_id':0, 'order_line_type':0, 'order_line_status':0,
|
|
}):
|
|
new_invoice_data_line['invoice_header_ref_interne'] = retval_sequence_invoice['prefixe']+str(current_seq_value)
|
|
new_invoice_data_line['invoice_line_type'] = "facture"
|
|
new_invoice_data_line['invoice_date'] = invoice_date_time
|
|
new_invoice_data_line['invoice_header_id'] = str(inserted_invoice_id)
|
|
new_invoice_data_line['update_by'] = str(my_partner['_id'])
|
|
|
|
#print(" ### on Va inserer la ligne de factue new_invoice_data_line = ", new_invoice_data_line)
|
|
|
|
inserted_line = MYSY_GV.dbname['partner_invoice_line'].insert_one(new_invoice_data_line)
|
|
|
|
#print(" ### inserted_line de la ligne inserée = ", inserted_line)
|
|
|
|
inserted_line_id = inserted_line.inserted_id
|
|
|
|
#print(" ### inserted_line_id de la ligne inserée = ", inserted_line_id)
|
|
|
|
if (not inserted_line_id):
|
|
|
|
# Vu quil y a un souci avec l'une des lignes, on fait un roll back complet de la facturation
|
|
order_updated = MYSY_GV.dbname['partner_order_header'].update_one({{'_id': ObjectId(
|
|
str(diction['order_id'])), 'order_header_ref_interne': str(diction['order_ref_interne']),
|
|
'partner_owner_recid': my_partner[
|
|
'recid'], 'valide': '1',
|
|
'locked': '0',
|
|
'order_header_type': 'commande'},
|
|
{'$set': {'order_header_status': '2'}}
|
|
})
|
|
|
|
MYSY_GV.dbname['partner_invoice_header'].delete_one({'_id':ObjectId(str(str(inserted_invoice_id)))})
|
|
MYSY_GV.dbname['partner_invoice_line'].delete_many({'invoice_header_id': ObjectId(str(str(inserted_invoice_id)))})
|
|
|
|
|
|
mycommon.myprint(
|
|
" Impossible de finaliser la facturation des lignes de la ligne la commande ")
|
|
return False, " Impossible de finaliser la facturation des lignes de la ligne la commande. ", False
|
|
|
|
"""
|
|
04/10/2024 - update pour faire le BPF
|
|
|
|
on va créer une table de detail qui reprend le detail des inscription
|
|
"""
|
|
partner_invoice_line_data_detail = {}
|
|
partner_invoice_line_data_detail['order_line_inscription_id'] = ""
|
|
partner_invoice_line_data_detail['order_line_inscription_type_apprenant'] = str(new_invoice_data_line['order_line_type_apprenant'])
|
|
partner_invoice_line_data_detail['order_line_inscription_modefinancement'] = ""
|
|
partner_invoice_line_data_detail['order_line_formation'] = str(new_invoice_data_line['order_line_formation'])
|
|
partner_invoice_line_data_detail['order_line_prix_unitaire'] = str(new_invoice_data_line['order_line_prix_unitaire'])
|
|
partner_invoice_line_data_detail['order_line_montant_hors_taxes'] = str(new_invoice_data_line['order_line_montant_hors_taxes'])
|
|
partner_invoice_line_data_detail['order_line_invoiced_amount'] = str(new_invoice_data_line['order_line_montant_hors_taxes'])
|
|
partner_invoice_line_data_detail['order_line_qty'] = str( new_invoice_data_line['order_line_qty'])
|
|
|
|
|
|
partner_invoice_line_data_detail['order_line_comment'] = ""
|
|
partner_invoice_line_data_detail['invoice_header_id'] = str(inserted_invoice_id)
|
|
partner_invoice_line_data_detail['invoice_line_type'] = "facture"
|
|
partner_invoice_line_data_detail['invoice_header_ref_interne'] = str(invoice_ref_interne)
|
|
|
|
order_line_is_include_bpf = ""
|
|
if ("order_header_inclus_bpf" in order_to_invoice_data.keys()):
|
|
order_line_is_include_bpf = order_to_invoice_data['order_header_inclus_bpf']
|
|
partner_invoice_line_data_detail['order_line_is_include_bpf'] = order_line_is_include_bpf
|
|
|
|
partner_invoice_line_data_detail['update_by'] = str(my_partner['_id'])
|
|
partner_invoice_line_data_detail['valide'] = "1"
|
|
partner_invoice_line_data_detail['locked'] = "0"
|
|
partner_invoice_line_data_detail['partner_owner_recid'] = str(my_partner['recid'])
|
|
|
|
print(" #### partner_invoice_line_data = ", partner_invoice_line_data_detail)
|
|
inserted_detail_invoice_line_id = MYSY_GV.dbname['partner_invoice_line_detail'].insert_one(
|
|
partner_invoice_line_data_detail).inserted_id
|
|
|
|
|
|
# 3 - on met à jour les lignes de la commande, pr la mettre à facturé
|
|
qry = {'order_header_id': str(diction['order_id']), 'order_header_ref_interne': str(diction['order_ref_interne']),
|
|
'partner_owner_recid': my_partner['recid'], 'valide': '1',
|
|
'locked': '0', 'order_header_type': 'commande'}
|
|
|
|
#print(" #### qry = ", qry)
|
|
|
|
order_updated = MYSY_GV.dbname['partner_order_line'].update_many(
|
|
{'order_header_id': str(diction['order_id']), 'order_header_ref_interne': str(diction['order_ref_interne']),
|
|
'partner_owner_recid': my_partner['recid'], 'valide': '1',
|
|
'locked': '0', 'order_line_type': 'commande'},
|
|
{'$set': {'order_line_status': '3'}}
|
|
)
|
|
|
|
if (order_updated.modified_count <= 0):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " WARNING - Impossible de mettre les lignes à facturer pour order_header_id = "+str(str(diction['order_id'])))
|
|
|
|
"""
|
|
05/06/2024 Gestion E-Facture
|
|
Apres la creation de la facture, on va aller créer le document securisé
|
|
"""
|
|
e_Invoice_Diction = {}
|
|
e_Invoice_Diction['token'] = diction['token']
|
|
e_Invoice_Diction['invoice_id'] = str(inserted_invoice_id)
|
|
|
|
local_E_Invoice_status, local_E_Invoice_retval = Session_Formation.Invoice_Create_Secure_E_Document(e_Invoice_Diction)
|
|
if (local_E_Invoice_status is False):
|
|
return True, "WARNING : La facture a été créée avec la réf. "+str(new_invoice_data_header['invoice_header_ref_interne'])+"; mais impossible de créer la e-Facture Sécurisée (1).", str(new_invoice_data_header['invoice_header_ref_interne'])
|
|
|
|
|
|
"""
|
|
Ajout l'action dans l'historique
|
|
"""
|
|
## Add to log history pour la facture
|
|
now = str(datetime.now().strftime("%d/%m/%Y %H:%M:%S"))
|
|
history_event_dict = {}
|
|
history_event_dict['token'] = diction['token']
|
|
history_event_dict['related_collection'] = "partner_invoice_header"
|
|
history_event_dict['related_collection_recid'] = str(inserted_invoice_id)
|
|
history_event_dict['action_date'] = str(now)
|
|
history_event_dict['action_description'] = "Creation facture"
|
|
local_status, local_retval = mycommon.Collection_Historique.Add_Historique_Event(history_event_dict)
|
|
if (local_status is False):
|
|
mycommon.myprint(
|
|
" WARNING : Impossible de logguer l'historique pour l'évènement : " + str(history_event_dict))
|
|
|
|
## Add to log history pour la commande
|
|
now = str(datetime.now().strftime("%d/%m/%Y %H:%M:%S"))
|
|
history_event_dict = {}
|
|
history_event_dict['token'] = diction['token']
|
|
history_event_dict['related_collection'] = "partner_order_header"
|
|
history_event_dict['related_collection_recid'] = str(diction['order_id'])
|
|
history_event_dict['action_date'] = str(now)
|
|
history_event_dict['action_description'] = "Facturé. Ref. Facture :"+str(new_invoice_data_header['invoice_header_ref_interne'])
|
|
local_status, local_retval = mycommon.Collection_Historique.Add_Historique_Event(history_event_dict)
|
|
if (local_status is False):
|
|
mycommon.myprint(
|
|
" WARNING : Impossible de logguer l'historique pour l'évènement : " + str(history_event_dict))
|
|
|
|
|
|
return True, " La commande a été correctement facturée", str(new_invoice_data_header['invoice_header_ref_interne'])
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de facturer la commande ", False
|
|
|
|
|
|
"""
|
|
Recuperation d'une facture donnée
|
|
"""
|
|
def Get_Given_Partner_Invoice(diction):
|
|
try:
|
|
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', '_id']
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list and val.startswith('my_') is False:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'existe pas")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token', '_id']
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans la liste des arguments ")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification de l'identité et autorisation de l'entité qui
|
|
appelle cette API
|
|
"""
|
|
token = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
token = diction['token']
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner
|
|
|
|
"""
|
|
Clés de mise à jour
|
|
"""
|
|
data_cle = {}
|
|
data_cle['partner_owner_recid'] = str(my_partner['recid'])
|
|
data_cle['_id'] = ObjectId(str(diction['_id']))
|
|
|
|
data_cle['valide'] = "1"
|
|
data_cle['locked'] = "0"
|
|
|
|
RetObject = []
|
|
val_tmp = 1
|
|
|
|
#print(" ### data_cle = ", data_cle)
|
|
for retval in MYSY_GV.dbname['partner_invoice_header'].find(data_cle):
|
|
user = retval
|
|
user['id'] = str(val_tmp)
|
|
val_tmp = val_tmp + 1
|
|
|
|
# Si le champ 'order_header_condition_paiement_id' alors on va chercher le code de la condition de paiement
|
|
paiement_ction_code = ""
|
|
if ('order_header_condition_paiement_id' in retval.keys() and retval[
|
|
'order_header_condition_paiement_id']):
|
|
paiement_ction_data = MYSY_GV.dbname['base_partner_paiement_condition'].find_one(
|
|
{'_id': ObjectId(str(retval['order_header_condition_paiement_id'])), 'valide': '1',
|
|
'locked': '0',
|
|
'partner_owner_recid': str(my_partner['recid'])})
|
|
|
|
if (paiement_ction_data and 'code' in paiement_ction_data.keys()):
|
|
paiement_ction_code = str(paiement_ction_data['code'])
|
|
user['order_header_paiement_condition_code'] = paiement_ction_code
|
|
|
|
# Si le champ 'order_header_client_id' alors on va chercher le nom du client
|
|
if ('order_header_client_id' in retval.keys()):
|
|
Client_data = MYSY_GV.dbname['partner_client'].find_one(
|
|
{'_id': ObjectId(str(retval['order_header_client_id'])), 'valide': '1', 'locked': '0',
|
|
'partner_recid': str(my_partner['recid'])})
|
|
|
|
if (Client_data and 'nom' in Client_data.keys()):
|
|
user['order_header_client_nom'] = str(Client_data['nom'])
|
|
|
|
# Si le champ 'order_header_vendeur_id' alors on va chercher le nom et prenom du vendeur (employe)
|
|
if ('order_header_vendeur_id' in retval.keys() and retval['order_header_vendeur_id']):
|
|
Employee_data = MYSY_GV.dbname['ressource_humaine'].find_one(
|
|
{'_id': ObjectId(str(retval['order_header_vendeur_id'])), 'valide': '1', 'locked': '0',
|
|
'partner_recid': str(my_partner['recid'])})
|
|
|
|
order_header_vendeur_nom_prenom = ""
|
|
if (Employee_data and 'nom' in Employee_data.keys()):
|
|
order_header_vendeur_nom_prenom = str(Employee_data['nom'])
|
|
|
|
if (Employee_data and 'prenom' in Employee_data.keys()):
|
|
order_header_vendeur_nom_prenom = str(order_header_vendeur_nom_prenom) + " " + str(
|
|
Employee_data['prenom'])
|
|
|
|
user['order_header_vendeur_nom_prenom'] = str(order_header_vendeur_nom_prenom)
|
|
|
|
# Recuperation des ligne associées
|
|
retval_line_data = []
|
|
for retval_line in MYSY_GV.dbname['partner_invoice_line'].find({'invoice_header_id':str(retval['_id']), 'partner_owner_recid':str(my_partner['recid']),
|
|
'valide':'1', 'locked':'0'}):
|
|
|
|
retval_line_data.append(retval_line)
|
|
|
|
|
|
RetObject.append(mycommon.JSONEncoder().encode(user))
|
|
|
|
#print(" ### RetObject = ", RetObject)
|
|
return True, RetObject
|
|
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de récupérer les données de la commande "
|
|
|
|
"""
|
|
Recuperation des lignes d'une facture à partir de l'invoice_header_id, sans entete
|
|
"""
|
|
def Get_Given_Partner_Invoice_Lines(diction):
|
|
try:
|
|
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', 'invoice_header_id']
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list and val.startswith('my_') is False:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'existe pas")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token', 'invoice_header_id']
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans la liste des arguments ")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification de l'identité et autorisation de l'entité qui
|
|
appelle cette API
|
|
"""
|
|
token = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
token = diction['token']
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner
|
|
|
|
"""
|
|
Clés de mise à jour
|
|
"""
|
|
data_cle = {}
|
|
data_cle['partner_owner_recid'] = str(my_partner['recid'])
|
|
data_cle['invoice_header_id'] = str(diction['invoice_header_id'])
|
|
|
|
data_cle['valide'] = "1"
|
|
data_cle['locked'] = "0"
|
|
|
|
RetObject = []
|
|
val_tmp = 1
|
|
|
|
filt_class_partner_recid = {'partner_owner_recid': str(my_partner['recid'])}
|
|
filt_invoice_header_id = {'invoice_header_id': str(diction['invoice_header_id'])}
|
|
|
|
query = [{'$match': {'$and': [ filt_invoice_header_id,
|
|
{'partner_owner_recid': str(my_partner['recid'])}]}},
|
|
{'$lookup':
|
|
{
|
|
'from': 'myclass',
|
|
'localField': 'order_line_formation',
|
|
'foreignField': 'internal_url',
|
|
'pipeline': [{'$match': {'$and': [ filt_class_partner_recid]}},
|
|
{'$project': {'title': 1, 'domaine': 1,
|
|
'duration': 1,
|
|
'duration_unit': 1,'external_code':1}}],
|
|
'as': 'myclass_collection'
|
|
}
|
|
}
|
|
]
|
|
print("#### Get_Given_Partner_Invoice_Lines : query pip= ", query)
|
|
|
|
for retval in MYSY_GV.dbname['partner_invoice_line'].aggregate(query):
|
|
if ('myclass_collection' in retval.keys() and len(retval['myclass_collection']) > 0):
|
|
user = {}
|
|
user['id'] = str(val_tmp)
|
|
val_tmp = val_tmp + 1
|
|
user['_id'] = retval['_id']
|
|
user['order_line_formation'] = retval['order_line_formation']
|
|
user['order_line_qty'] = retval['order_line_qty']
|
|
user['order_line_prix_unitaire'] = retval['order_line_prix_unitaire']
|
|
user['invoice_header_id'] = retval['invoice_header_id']
|
|
user['invoice_header_ref_interne'] = retval['invoice_header_ref_interne']
|
|
user['invoice_date'] = retval['invoice_date']
|
|
user['valide'] = retval['valide']
|
|
user['locked'] = retval['locked']
|
|
|
|
if ("order_line_montant_reduction" in retval.keys()):
|
|
user['order_line_montant_reduction'] = retval['order_line_montant_reduction']
|
|
else:
|
|
user['order_line_montant_reduction'] = "0"
|
|
|
|
if ("order_line_type_article" in retval.keys()):
|
|
user['order_line_type_article'] = retval['order_line_type_article']
|
|
else:
|
|
user['order_line_type_article'] = ""
|
|
|
|
if ("order_line_type_apprenant" in retval.keys()):
|
|
user['order_line_type_apprenant'] = retval['order_line_type_apprenant']
|
|
else:
|
|
user['order_line_type_apprenant'] = ""
|
|
|
|
|
|
if ("order_line_montant_toutes_taxes" in retval.keys()):
|
|
user['order_line_montant_toutes_taxes'] = retval['order_line_montant_toutes_taxes']
|
|
else:
|
|
user['order_line_montant_toutes_taxes'] = "0"
|
|
|
|
if ("order_line_tax" in retval.keys()):
|
|
user['order_line_tax'] = retval['order_line_tax']
|
|
else:
|
|
user['order_line_tax'] = ""
|
|
|
|
if ("order_line_tax_amount" in retval.keys()):
|
|
user['order_line_tax_amount'] = retval['order_line_tax_amount']
|
|
else:
|
|
user['order_line_tax_amount'] = "0"
|
|
|
|
if ("order_line_type_reduction" in retval.keys()):
|
|
user['order_line_type_reduction'] = retval['order_line_type_reduction']
|
|
else:
|
|
user['order_line_montant_reduction'] = ""
|
|
|
|
if ("order_line_type_valeur" in retval.keys()):
|
|
user['order_line_type_valeur'] = retval['order_line_type_valeur']
|
|
else:
|
|
user['order_line_type_valeur'] = "0"
|
|
|
|
|
|
if ("order_line_montant_hors_taxes" in retval.keys()):
|
|
user['order_line_montant_hors_taxes'] = retval['order_line_montant_hors_taxes']
|
|
else:
|
|
user['order_line_montant_hors_taxes'] = "0"
|
|
|
|
|
|
user['date_update'] = retval['date_update']
|
|
user['update_by'] = str(my_partner['_id'])
|
|
user['partner_owner_recid'] = retval['partner_owner_recid']
|
|
user['invoice_line_type'] = retval['invoice_line_type']
|
|
|
|
|
|
if( "order_line_comment" in retval.keys()):
|
|
user['order_line_comment'] = retval['order_line_comment']
|
|
else:
|
|
user['order_line_comment'] = ""
|
|
|
|
user['title'] = retval['myclass_collection'][0]['title']
|
|
user['order_line_formation_external_code'] = retval['myclass_collection'][0]['external_code']
|
|
|
|
if( "domaine" in retval['myclass_collection'][0].keys() ):
|
|
user['domaine'] = retval['myclass_collection'][0]['domaine']
|
|
else:
|
|
user['domaine'] = ""
|
|
|
|
user['duration'] = retval['myclass_collection'][0]['duration']
|
|
user['duration_unit'] = retval['myclass_collection'][0]['duration_unit']
|
|
|
|
if (str(retval['myclass_collection'][0]['duration_unit']) == "heure"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " h"
|
|
|
|
elif (str(retval['myclass_collection'][0]['duration_unit']) == "jour"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " j"
|
|
|
|
elif (str(retval['myclass_collection'][0]['duration_unit']) == "semaine"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " s"
|
|
|
|
elif (str(retval['myclass_collection'][0]['duration_unit']) == "mois"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " m"
|
|
|
|
elif (str(retval['myclass_collection'][0]['duration_unit']) == "annee"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " a"
|
|
|
|
elif (str(retval['myclass_collection'][0]['duration_unit']) == "user_rythme"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " u"
|
|
|
|
else:
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " ?"
|
|
|
|
"""
|
|
Il s'agit d'un formation, vu qu'on a un lien avec la collection "myclass", on force alors le 'order_line_type_article'
|
|
a 'formation'
|
|
"""
|
|
user['order_line_type_article'] = "formation"
|
|
|
|
|
|
RetObject.append(mycommon.JSONEncoder().encode(user))
|
|
|
|
"""
|
|
Recuperation des produits et services
|
|
"""
|
|
query = [{'$match': {'$and': [ filt_invoice_header_id,
|
|
{'partner_owner_recid': str(my_partner['recid'])}]}},
|
|
{'$lookup': {
|
|
'from': 'partner_produit_service',
|
|
"let": {'order_line_formation': "$order_line_formation",
|
|
"partner_produit_service_partner_owner_recid": "$partner_owner_recid"
|
|
},
|
|
'pipeline': [
|
|
{'$match':
|
|
{'$expr':
|
|
{'$and':
|
|
[
|
|
{'$eq': ["$valide", "1"]},
|
|
{'$eq': ["$_id", {'$convert': {
|
|
'input': "$$order_line_formation",
|
|
'to': "objectId",
|
|
'onError': {'error': 'true'},
|
|
'onNull': {'isnull': 'true'}
|
|
}}]},
|
|
{'$eq': ["$partner_owner_recid",
|
|
'$$partner_produit_service_partner_owner_recid']},
|
|
|
|
]
|
|
}
|
|
}
|
|
},
|
|
|
|
],
|
|
'as': 'collection_partner_produit_service'
|
|
}
|
|
},
|
|
]
|
|
print("#### Get_Given_Partner_Invoice_Lines for PRODUCT & SERVICES : query pip= ", query)
|
|
|
|
for retval in MYSY_GV.dbname['partner_invoice_line'].aggregate(query):
|
|
if ('collection_partner_produit_service' in retval.keys() and len(retval['collection_partner_produit_service']) > 0):
|
|
user = {}
|
|
user['id'] = str(val_tmp)
|
|
val_tmp = val_tmp + 1
|
|
user['_id'] = retval['_id']
|
|
user['order_line_formation'] = retval['order_line_formation']
|
|
user['order_line_qty'] = retval['order_line_qty']
|
|
user['order_line_prix_unitaire'] = retval['order_line_prix_unitaire']
|
|
user['invoice_header_id'] = retval['invoice_header_id']
|
|
user['invoice_header_ref_interne'] = retval['invoice_header_ref_interne']
|
|
user['invoice_date'] = retval['invoice_date']
|
|
user['valide'] = retval['valide']
|
|
user['locked'] = retval['locked']
|
|
|
|
if ("order_line_montant_reduction" in retval.keys()):
|
|
user['order_line_montant_reduction'] = retval['order_line_montant_reduction']
|
|
else:
|
|
user['order_line_montant_reduction'] = "0"
|
|
|
|
if ("order_line_type_apprenant" in retval.keys()):
|
|
user['order_line_type_apprenant'] = retval['order_line_type_apprenant']
|
|
else:
|
|
user['order_line_type_apprenant'] = ""
|
|
|
|
|
|
if ("order_line_montant_toutes_taxes" in retval.keys()):
|
|
user['order_line_montant_toutes_taxes'] = retval['order_line_montant_toutes_taxes']
|
|
else:
|
|
user['order_line_montant_toutes_taxes'] = "0"
|
|
|
|
if ("order_line_tax" in retval.keys()):
|
|
user['order_line_tax'] = retval['order_line_tax']
|
|
else:
|
|
user['order_line_tax'] = ""
|
|
|
|
if ("order_line_tax_amount" in retval.keys()):
|
|
user['order_line_tax_amount'] = retval['order_line_tax_amount']
|
|
else:
|
|
user['order_line_tax_amount'] = "0"
|
|
|
|
if ("order_line_type_reduction" in retval.keys()):
|
|
user['order_line_type_reduction'] = retval['order_line_type_reduction']
|
|
else:
|
|
user['order_line_montant_reduction'] = ""
|
|
|
|
if ("order_line_type_valeur" in retval.keys()):
|
|
user['order_line_type_valeur'] = retval['order_line_type_valeur']
|
|
else:
|
|
user['order_line_type_valeur'] = "0"
|
|
|
|
if ("order_line_montant_hors_taxes" in retval.keys()):
|
|
user['order_line_montant_hors_taxes'] = retval['order_line_montant_hors_taxes']
|
|
else:
|
|
user['order_line_montant_hors_taxes'] = "0"
|
|
|
|
user['date_update'] = retval['date_update']
|
|
user['update_by'] = str(my_partner['_id'])
|
|
user['partner_owner_recid'] = retval['partner_owner_recid']
|
|
user['invoice_line_type'] = retval['invoice_line_type']
|
|
|
|
if ("order_line_comment" in retval.keys()):
|
|
user['order_line_comment'] = retval['order_line_comment']
|
|
else:
|
|
user['order_line_comment'] = ""
|
|
|
|
user['title'] = retval['collection_partner_produit_service'][0]['nom']
|
|
user['order_line_formation_external_code'] = retval['collection_partner_produit_service'][0]['code']
|
|
|
|
user['domaine'] = ""
|
|
|
|
user['duration'] = ""
|
|
user['duration_unit'] = ""
|
|
user['duration_concat'] = ""
|
|
|
|
"""
|
|
Il s'agit d'un produit, vu qu'on a un lien avec la collection "partner_produit_service", on force alors le 'order_line_type_article'
|
|
a 'produit'
|
|
"""
|
|
user['order_line_type_article'] = "produit"
|
|
|
|
RetObject.append(mycommon.JSONEncoder().encode(user))
|
|
|
|
print(" ### Get_Given_Partner_Invoice_Lines for PRODUCT & SERVICES : RetObject = ", RetObject)
|
|
return True, RetObject
|
|
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de récupérer les lignes de la facture "
|
|
|
|
|
|
|
|
"""
|
|
Recuperation des lignes d'une commande à partir de l' invoice_header_ref_interne, sans entete
|
|
"""
|
|
def Get_Given_Partner_Invoice_Lines_From_Invoice_ref_interne(diction):
|
|
try:
|
|
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', 'invoice_header_ref_interne']
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list and val.startswith('my_') is False:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'existe pas")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token', 'invoice_header_ref_interne']
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans la liste des arguments ")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification de l'identité et autorisation de l'entité qui
|
|
appelle cette API
|
|
"""
|
|
token = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
token = diction['token']
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner
|
|
|
|
"""
|
|
Clés de mise à jour
|
|
"""
|
|
data_cle = {}
|
|
data_cle['partner_owner_recid'] = str(my_partner['recid'])
|
|
data_cle['invoice_header_ref_interne'] = str(diction['invoice_header_ref_interne'])
|
|
|
|
data_cle['valide'] = "1"
|
|
data_cle['locked'] = "0"
|
|
|
|
RetObject = []
|
|
val_tmp = 1
|
|
|
|
filt_class_partner_recid = {'partner_owner_recid': str(my_partner['recid'])}
|
|
filt_invoice_header_id = {'invoice_header_id': str(diction['order_header_id'])}
|
|
|
|
query = [{'$match': {'$and': [filt_invoice_header_id,
|
|
{'partner_owner_recid': str(my_partner['recid'])}]}},
|
|
{'$lookup':
|
|
{
|
|
'from': 'myclass',
|
|
'localField': 'order_line_formation',
|
|
'foreignField': 'internal_url',
|
|
'pipeline': [{'$match': {'$and': [filt_class_partner_recid]}},
|
|
{'$project': {'title': 1, 'domaine': 1,
|
|
'duration': 1,
|
|
'duration_unit': 1, 'external_code': 1}}],
|
|
'as': 'myclass_collection'
|
|
}
|
|
}
|
|
]
|
|
print("#### Get_Given_Partner_Invoice_Lines : query pip= ", query)
|
|
|
|
for retval in MYSY_GV.dbname['partner_invoice_line'].aggregate(query):
|
|
if ('myclass_collection' in retval.keys() and len(retval['myclass_collection']) > 0):
|
|
user = {}
|
|
user['id'] = str(val_tmp)
|
|
val_tmp = val_tmp + 1
|
|
user['_id'] = retval['_id']
|
|
user['order_line_formation'] = retval['order_line_formation']
|
|
user['order_line_qty'] = retval['order_line_qty']
|
|
user['order_line_prix_unitaire'] = retval['order_line_prix_unitaire']
|
|
user['invoice_header_id'] = retval['invoice_header_id']
|
|
user['invoice_header_ref_interne'] = retval['invoice_header_ref_interne']
|
|
user['invoice_date'] = retval['invoice_date']
|
|
user['valide'] = retval['valide']
|
|
user['locked'] = retval['locked']
|
|
|
|
if ("order_line_montant_reduction" in retval.keys()):
|
|
user['order_line_montant_reduction'] = retval['order_line_montant_reduction']
|
|
else:
|
|
user['order_line_montant_reduction'] = "0"
|
|
|
|
if ("order_line_type_article" in retval.keys()):
|
|
user['order_line_type_article'] = retval['order_line_type_article']
|
|
else:
|
|
user['order_line_type_article'] = ""
|
|
|
|
if ("order_line_type_apprenant" in retval.keys()):
|
|
user['order_line_type_apprenant'] = retval['order_line_type_apprenant']
|
|
else:
|
|
user['order_line_type_apprenant'] = ""
|
|
|
|
if ("order_line_montant_toutes_taxes" in retval.keys()):
|
|
user['order_line_montant_toutes_taxes'] = retval['order_line_montant_toutes_taxes']
|
|
else:
|
|
user['order_line_montant_toutes_taxes'] = "0"
|
|
|
|
if ("order_line_tax" in retval.keys()):
|
|
user['order_line_tax'] = retval['order_line_tax']
|
|
else:
|
|
user['order_line_tax'] = ""
|
|
|
|
if ("order_line_tax_amount" in retval.keys()):
|
|
user['order_line_tax_amount'] = retval['order_line_tax_amount']
|
|
else:
|
|
user['order_line_tax_amount'] = "0"
|
|
|
|
if ("order_line_type_reduction" in retval.keys()):
|
|
user['order_line_type_reduction'] = retval['order_line_type_reduction']
|
|
else:
|
|
user['order_line_montant_reduction'] = ""
|
|
|
|
if ("order_line_type_valeur" in retval.keys()):
|
|
user['order_line_type_valeur'] = retval['order_line_type_valeur']
|
|
else:
|
|
user['order_line_type_valeur'] = "0"
|
|
|
|
if ("order_line_montant_hors_taxes" in retval.keys()):
|
|
user['order_line_montant_hors_taxes'] = retval['order_line_montant_hors_taxes']
|
|
else:
|
|
user['order_line_montant_hors_taxes'] = "0"
|
|
|
|
user['date_update'] = retval['date_update']
|
|
user['update_by'] = str(my_partner['_id'])
|
|
user['partner_owner_recid'] = retval['partner_owner_recid']
|
|
user['invoice_line_type'] = retval['invoice_line_type']
|
|
|
|
if ("order_line_comment" in retval.keys()):
|
|
user['order_line_comment'] = retval['order_line_comment']
|
|
else:
|
|
user['order_line_comment'] = ""
|
|
|
|
user['title'] = retval['myclass_collection'][0]['title']
|
|
user['order_line_formation_external_code'] = retval['myclass_collection'][0]['external_code']
|
|
|
|
if ("domaine" in retval['myclass_collection'][0].keys()):
|
|
user['domaine'] = retval['myclass_collection'][0]['domaine']
|
|
else:
|
|
user['domaine'] = ""
|
|
|
|
user['duration'] = retval['myclass_collection'][0]['duration']
|
|
user['duration_unit'] = retval['myclass_collection'][0]['duration_unit']
|
|
|
|
if (str(retval['myclass_collection'][0]['duration_unit']) == "heure"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " h"
|
|
|
|
elif (str(retval['myclass_collection'][0]['duration_unit']) == "jour"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " j"
|
|
|
|
elif (str(retval['myclass_collection'][0]['duration_unit']) == "semaine"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " s"
|
|
|
|
elif (str(retval['myclass_collection'][0]['duration_unit']) == "mois"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " m"
|
|
|
|
elif (str(retval['myclass_collection'][0]['duration_unit']) == "annee"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " a"
|
|
|
|
elif (str(retval['myclass_collection'][0]['duration_unit']) == "user_rythme"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " u"
|
|
|
|
else:
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " ?"
|
|
|
|
"""
|
|
Il s'agit d'un formation, vu qu'on a un lien avec la collection "myclass", on force alors le 'order_line_type_article'
|
|
a 'formation'
|
|
"""
|
|
user['order_line_type_article'] = "formation"
|
|
|
|
RetObject.append(mycommon.JSONEncoder().encode(user))
|
|
|
|
"""
|
|
Recuperation des produits et services
|
|
"""
|
|
query = [{'$match': {'$and': [filt_invoice_header_id,
|
|
{'partner_owner_recid': str(my_partner['recid'])}]}},
|
|
{'$lookup': {
|
|
'from': 'partner_produit_service',
|
|
"let": {'order_line_formation': "$order_line_formation",
|
|
"partner_produit_service_partner_owner_recid": "$partner_owner_recid"
|
|
},
|
|
'pipeline': [
|
|
{'$match':
|
|
{'$expr':
|
|
{'$and':
|
|
[
|
|
{'$eq': ["$valide", "1"]},
|
|
{'$eq': ["$_id", {'$convert': {
|
|
'input': "$$order_line_formation",
|
|
'to': "objectId",
|
|
'onError': {'error': 'true'},
|
|
'onNull': {'isnull': 'true'}
|
|
}}]},
|
|
{'$eq': ["$partner_owner_recid",
|
|
'$$partner_produit_service_partner_owner_recid']},
|
|
|
|
]
|
|
}
|
|
}
|
|
},
|
|
|
|
],
|
|
'as': 'collection_partner_produit_service'
|
|
}
|
|
},
|
|
]
|
|
print("#### Get_Given_Partner_Invoice_Lines for PRODUCT & SERVICES : query pip= ", query)
|
|
|
|
for retval in MYSY_GV.dbname['partner_invoice_line'].aggregate(query):
|
|
if ('collection_partner_produit_service' in retval.keys() and len(retval['collection_partner_produit_service']) > 0):
|
|
user = {}
|
|
user['id'] = str(val_tmp)
|
|
val_tmp = val_tmp + 1
|
|
user['_id'] = retval['_id']
|
|
user['order_line_formation'] = retval['order_line_formation']
|
|
user['order_line_qty'] = retval['order_line_qty']
|
|
user['order_line_prix_unitaire'] = retval['order_line_prix_unitaire']
|
|
user['invoice_header_id'] = retval['invoice_header_id']
|
|
user['invoice_header_ref_interne'] = retval['invoice_header_ref_interne']
|
|
user['invoice_date'] = retval['invoice_date']
|
|
user['valide'] = retval['valide']
|
|
user['locked'] = retval['locked']
|
|
|
|
if ("order_line_montant_reduction" in retval.keys()):
|
|
user['order_line_montant_reduction'] = retval['order_line_montant_reduction']
|
|
else:
|
|
user['order_line_montant_reduction'] = "0"
|
|
|
|
if ("order_line_type_apprenant" in retval.keys()):
|
|
user['order_line_type_apprenant'] = retval['order_line_type_apprenant']
|
|
else:
|
|
user['order_line_type_apprenant'] = ""
|
|
|
|
if ("order_line_montant_toutes_taxes" in retval.keys()):
|
|
user['order_line_montant_toutes_taxes'] = retval['order_line_montant_toutes_taxes']
|
|
else:
|
|
user['order_line_montant_toutes_taxes'] = "0"
|
|
|
|
if ("order_line_tax" in retval.keys()):
|
|
user['order_line_tax'] = retval['order_line_tax']
|
|
else:
|
|
user['order_line_tax'] = ""
|
|
|
|
if ("order_line_tax_amount" in retval.keys()):
|
|
user['order_line_tax_amount'] = retval['order_line_tax_amount']
|
|
else:
|
|
user['order_line_tax_amount'] = "0"
|
|
|
|
if ("order_line_type_reduction" in retval.keys()):
|
|
user['order_line_type_reduction'] = retval['order_line_type_reduction']
|
|
else:
|
|
user['order_line_montant_reduction'] = ""
|
|
|
|
if ("order_line_type_valeur" in retval.keys()):
|
|
user['order_line_type_valeur'] = retval['order_line_type_valeur']
|
|
else:
|
|
user['order_line_type_valeur'] = "0"
|
|
|
|
if ("order_line_montant_hors_taxes" in retval.keys()):
|
|
user['order_line_montant_hors_taxes'] = retval['order_line_montant_hors_taxes']
|
|
else:
|
|
user['order_line_montant_hors_taxes'] = "0"
|
|
|
|
user['date_update'] = retval['date_update']
|
|
user['update_by'] = str(my_partner['_id'])
|
|
user['partner_owner_recid'] = retval['partner_owner_recid']
|
|
user['invoice_line_type'] = retval['invoice_line_type']
|
|
|
|
if ("order_line_comment" in retval.keys()):
|
|
user['order_line_comment'] = retval['order_line_comment']
|
|
else:
|
|
user['order_line_comment'] = ""
|
|
|
|
user['title'] = retval['collection_partner_produit_service'][0]['nom']
|
|
user['order_line_formation_external_code'] = retval['collection_partner_produit_service'][0]['code']
|
|
|
|
user['domaine'] = ""
|
|
|
|
user['duration'] = ""
|
|
user['duration_unit'] = ""
|
|
user['duration_concat'] = ""
|
|
|
|
"""
|
|
Il s'agit d'un produit, vu qu'on a un lien avec la collection "partner_produit_service", on force alors le 'order_line_type_article'
|
|
a 'produit'
|
|
"""
|
|
user['order_line_type_article'] = "produit"
|
|
|
|
RetObject.append(mycommon.JSONEncoder().encode(user))
|
|
|
|
#print(" ### RetObject = ", RetObject)
|
|
return True, RetObject
|
|
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de récupérer les lignes de commande "
|
|
|
|
|
|
|
|
|
|
"""
|
|
Recuperation de la liste des factures d'un partner
|
|
"""
|
|
def Get_List_Partner_Invoice_no_filter(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', ]
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list and val.startswith('my_') is False:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'existe pas")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token', ]
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans la liste des arguments ")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification de l'identité et autorisation de l'entité qui
|
|
appelle cette API
|
|
"""
|
|
token = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
token = diction['token']
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner
|
|
|
|
|
|
|
|
find_qry = {'$and': [{'partner_owner_recid': str(my_partner['recid']), 'valide': '1', 'locked': '0'}, {}, ]}
|
|
|
|
new_myquery_find_invoice = [{'$match': find_qry},
|
|
{ '$sort': {'_id': -1}},
|
|
{"$addFields": {"partner_invoice_header_Id": {"$toString": "$_id"}}},
|
|
{'$lookup':
|
|
{
|
|
'from': 'partner_order_line',
|
|
'localField': "partner_invoice_header_Id",
|
|
'foreignField': 'invoice_header_id',
|
|
'pipeline': [{'$match': {'$and': [{}, {
|
|
'partner_owner_recid': str(my_partner['recid'])}, {'valide': '1'}]}}, ],
|
|
'as': 'partner_invoice_line_collection'
|
|
}
|
|
}
|
|
]
|
|
|
|
|
|
#print(" ### orders new_myquery_find_invoice = ", new_myquery_find_invoice)
|
|
RetObject = []
|
|
val_tmp = 0
|
|
|
|
somme_header_ca_ht = 0
|
|
nb_header_invoice = 0
|
|
|
|
for New_retVal in MYSY_GV.dbname['partner_invoice_header'].aggregate(new_myquery_find_invoice):
|
|
user = New_retVal
|
|
user['id'] = str(val_tmp)
|
|
val_tmp = val_tmp + 1
|
|
|
|
"""
|
|
recuperer le CA globale et le nombre de factures
|
|
"""
|
|
if ("total_header_hors_taxe_before_header_reduction" in New_retVal.keys()):
|
|
somme_header_ca_ht = somme_header_ca_ht + mycommon.tryFloat( str(New_retVal['total_header_hors_taxe_before_header_reduction']))
|
|
nb_header_invoice = nb_header_invoice + 1
|
|
|
|
# Convertir la date facture en jj/mm/aaaa
|
|
if( 'invoice_date' in New_retVal.keys()):
|
|
date_jjmmaaa = str(New_retVal['invoice_date'])[0:10]
|
|
#print(" ### date_jjmmaaa = ", date_jjmmaaa)
|
|
#date_jjmmaaa = datetime.strptime(date_jjmmaaa, '%d/%m/%Y')
|
|
#New_retVal['invoice_date'] = str(date_jjmmaaa)
|
|
|
|
# Si le champ 'order_header_condition_paiement_id' alors on va chercher le code de la condition de paiement
|
|
paiement_ction_code = ""
|
|
if ('order_header_condition_paiement_id' in New_retVal.keys() and New_retVal[
|
|
'order_header_condition_paiement_id']):
|
|
paiement_ction_data = MYSY_GV.dbname['base_partner_paiement_condition'].find_one(
|
|
{'_id': ObjectId(str(New_retVal['order_header_condition_paiement_id'])), 'valide': '1',
|
|
'locked': '0',
|
|
'partner_owner_recid': str(my_partner['recid'])})
|
|
|
|
if (paiement_ction_data and 'code' in paiement_ction_data.keys()):
|
|
paiement_ction_code = str(paiement_ction_data['code'])
|
|
user['order_header_paiement_condition_code'] = paiement_ction_code
|
|
|
|
|
|
# Si le champ 'order_header_client_id' alors on va chercher le nom du client
|
|
if( 'order_header_client_id' in New_retVal.keys()):
|
|
Client_data = MYSY_GV.dbname['partner_client'].find_one({'_id':ObjectId(str(New_retVal['order_header_client_id'])), 'valide':'1', 'locked':'0',
|
|
'partner_recid':str(my_partner['recid'])})
|
|
|
|
if( Client_data and 'nom' in Client_data.keys() ):
|
|
user['order_header_client_nom'] = str(Client_data['nom'])
|
|
|
|
# Si le champ 'order_header_vendeur_id' alors on va chercher le nom et prenom du vendeur (employe)
|
|
if ('order_header_vendeur_id' in New_retVal.keys() and New_retVal['order_header_vendeur_id']):
|
|
Employee_data = MYSY_GV.dbname['ressource_humaine'].find_one(
|
|
{'_id': ObjectId(str(New_retVal['order_header_vendeur_id'])), 'valide': '1', 'locked': '0',
|
|
'partner_recid': str(my_partner['recid'])})
|
|
|
|
order_header_vendeur_nom_prenom = ""
|
|
if (Employee_data and 'nom' in Employee_data.keys()):
|
|
order_header_vendeur_nom_prenom = str(Employee_data['nom'])
|
|
|
|
if (Employee_data and 'prenom' in Employee_data.keys()):
|
|
order_header_vendeur_nom_prenom = str(order_header_vendeur_nom_prenom)+" "+str(Employee_data['prenom'])
|
|
|
|
user['order_header_vendeur_nom_prenom'] = str(order_header_vendeur_nom_prenom)
|
|
|
|
"""
|
|
Recuperer le solde (montant restant à payer)
|
|
"""
|
|
local_diction = {'token':str(diction['token']), 'invoice_id':str(New_retVal['_id'])}
|
|
|
|
reste_to_paye = "0"
|
|
local_solde_status, local_solde_retval = invoice_paiement_mgt.Get_Invoice_Total_Amount_Payed_And_Remaining_Amount(local_diction)
|
|
if( local_solde_status ):
|
|
sold_data = ast.literal_eval(local_solde_retval[0])
|
|
reste_to_paye = sold_data['remaining_amount']
|
|
|
|
user['reste_to_paye'] = reste_to_paye
|
|
|
|
if( "credit_note_ref" not in New_retVal.keys() ):
|
|
user['credit_note_ref'] = ""
|
|
|
|
RetObject.append(mycommon.JSONEncoder().encode(user))
|
|
|
|
|
|
|
|
total_data = []
|
|
node = {}
|
|
node['somme_header_ca_ht'] = str(somme_header_ca_ht)
|
|
node['nb_header_invoice'] = str(nb_header_invoice)
|
|
|
|
total_data.append(node)
|
|
|
|
total_data = mycommon.JSONEncoder().encode(total_data)
|
|
|
|
#print(" ### RetObject = ", RetObject)
|
|
|
|
return True, RetObject, total_data
|
|
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de récupérer la liste des factures "
|
|
|
|
"""
|
|
Recuperation de la liste des factures avec des filtres.
|
|
les filtres acceptés sont :
|
|
- ref_interne_cmd (commande)
|
|
- ref_interne_invoice
|
|
- ref_externe
|
|
- invoice_date entre date_debut et date_fin
|
|
- nom_client
|
|
"""
|
|
def Get_List_Partner_Invoice_with_filter(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés. les filtres accepté sont :
|
|
- ref_interne,
|
|
"""
|
|
field_list = ['token', 'date_facture_debut', 'date_facture_fin', 'client_nom', 'ref_interne_cmd',
|
|
'ref_interne_invoice', 'ref_externe', 'formation', 'order_header_client_id']
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list and val.startswith('my_') is False:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'existe pas")
|
|
return False, " Les informations fournies sont incorrectes", False
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token', ]
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans la liste des arguments ")
|
|
return False, " Les informations fournies sont incorrectes", False
|
|
|
|
"""
|
|
Verification de l'identité et autorisation de l'entité qui
|
|
appelle cette API
|
|
"""
|
|
token = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
token = diction['token']
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner, False
|
|
|
|
filt_client_nom = {}
|
|
sub_filt_client_nom = {}
|
|
Lists_partner_client_id = []
|
|
if ("client_nom" in diction.keys()):
|
|
sub_filt_client_nom = {'nom': {'$regex': str(diction['client_nom']), "$options": "i"}, 'partner_recid':str(my_partner['recid']), 'valide':'1', 'locked':'0'}
|
|
# Recuperation des '_id' des clients dont le nom match en regexp
|
|
#print(" ### sub_filt_client_nom = ", sub_filt_client_nom)
|
|
for List_Client_Data in MYSY_GV.dbname['partner_client'].find(sub_filt_client_nom, {'_id':1}):
|
|
Lists_partner_client_id.append(str(List_Client_Data['_id']))
|
|
|
|
filt_client_nom = {'order_header_client_id': {'$in': Lists_partner_client_id, }}
|
|
#print(' ### filt_client_nom = ', filt_client_nom)
|
|
|
|
|
|
filt_formation_external_code = {}
|
|
sub_filt_formation_external_code = {}
|
|
Lists_partner_formation_internal_url = []
|
|
if ("formation" in diction.keys()):
|
|
sub_filt_formation_external_code = {'external_code': {'$regex': str(diction['formation']), "$options": "i"},
|
|
'partner_owner_recid': str(my_partner['recid']), 'valide': '1', 'locked': '0'}
|
|
|
|
# Recuperation des '_id' des formation dont le nom match en regexp
|
|
print(" ### sub_filt_formation_external_code = ", sub_filt_formation_external_code)
|
|
for Lists_partner_formation_Data in MYSY_GV.dbname['myclass'].find(sub_filt_formation_external_code, {'internal_url': 1}):
|
|
Lists_partner_formation_internal_url.append(str(Lists_partner_formation_Data['internal_url']))
|
|
|
|
filt_formation_external_code = {'order_line_formation': {'$in': Lists_partner_formation_internal_url }}
|
|
|
|
|
|
filt_client_id = {}
|
|
if ("order_header_client_id" in diction.keys() and diction['order_header_client_id']):
|
|
filt_client_id = {'order_header_client_id': str(diction['order_header_client_id'])}
|
|
|
|
|
|
filt_ref_interne_cmd = {}
|
|
if ("ref_interne_cmd" in diction.keys()):
|
|
filt_ref_interne_cmd = {
|
|
'order_header_ref_interne': {'$regex': str(diction['ref_interne_cmd']), "$options": "i"}}
|
|
|
|
filt_ref_interne_invoice = {}
|
|
if ("ref_interne_invoice" in diction.keys()):
|
|
filt_ref_interne_cmd = {
|
|
'invoice_header_ref_interne': {'$regex': str(diction['ref_interne_invoice']), "$options": "i"}}
|
|
|
|
|
|
filt_ref_externe = {}
|
|
if ("ref_externe" in diction.keys()):
|
|
filt_ref_externe = {
|
|
'order_header_ref_client': {'$regex': str(diction['ref_externe']), "$options": "i"}}
|
|
|
|
|
|
find_qry = {'$and': [{'partner_owner_recid': str(my_partner['recid']), 'valide': '1', 'locked': '0'}, filt_client_nom, filt_ref_interne_cmd, filt_ref_interne_invoice, filt_ref_externe, filt_client_id]}
|
|
|
|
new_myquery_find_order = [{'$match': find_qry},
|
|
{"$addFields": {"partner_invoice_header_Id": {"$toString": "$_id"}}},
|
|
{'$lookup':
|
|
{
|
|
'from': 'partner_invoice_line',
|
|
'localField': "partner_invoice_header_Id",
|
|
'foreignField': 'invoice_header_id',
|
|
'pipeline': [{'$match': {'$and': [filt_formation_external_code, {
|
|
'partner_owner_recid': str(my_partner['recid'])}, {'valide': '1'}]}}, ],
|
|
'as': 'partner_invoice_line_collection'
|
|
}
|
|
},
|
|
{
|
|
'$sort': {'_id':-1, }
|
|
},
|
|
]
|
|
|
|
|
|
#print(" ### Get_List_Partner_Order_with_filter orders new_myquery_find_order = ", new_myquery_find_order)
|
|
RetObject = []
|
|
val_tmp = 0
|
|
|
|
filter_date_debut = ""
|
|
if ("date_facture_debut" in diction.keys()):
|
|
if diction['date_facture_debut']:
|
|
filter_date_debut = str(diction['date_facture_debut'])[0:10]
|
|
local_status = mycommon.CheckisDate(filter_date_debut)
|
|
if (local_status is False):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " La date de debut (filtre) n'est pas au format 'jj/mm/aaaa' ")
|
|
return False, " La date de debut (filtre) n'est pas au format 'jj/mm/aaaa'", False
|
|
|
|
filter_date_fin = ""
|
|
if ("date_facture_fin" in diction.keys()):
|
|
if diction['date_facture_fin']:
|
|
filter_date_fin = str(diction['date_facture_fin'])[0:10]
|
|
local_status = mycommon.CheckisDate(filter_date_fin)
|
|
if (local_status is False):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + " La date de fin (filtre) n'est pas au format 'jj/mm/aaaa' ")
|
|
return False, " La date de fin (filtre) n'est pas au format 'jj/mm/aaaa'", False
|
|
|
|
somme_header_ca_ht = 0
|
|
nb_header_invoice = 0
|
|
for New_retVal in MYSY_GV.dbname['partner_invoice_header'].aggregate(new_myquery_find_order):
|
|
|
|
"""
|
|
recuperer le CA globale et le nombre de factures
|
|
"""
|
|
if("total_header_hors_taxe_before_header_reduction" in New_retVal.keys() ):
|
|
somme_header_ca_ht = somme_header_ca_ht + mycommon.tryFloat(str(New_retVal['total_header_hors_taxe_before_header_reduction']))
|
|
nb_header_invoice = nb_header_invoice + 1
|
|
|
|
|
|
|
|
if ('partner_invoice_line_collection' in New_retVal.keys() and len( New_retVal['partner_invoice_line_collection']) > 0):
|
|
user = New_retVal
|
|
|
|
# Si le champ 'order_header_condition_paiement_id' alors on va chercher le code de la condition de paiement
|
|
paiement_ction_code = ""
|
|
if ('order_header_condition_paiement_id' in New_retVal.keys() and New_retVal[
|
|
'order_header_condition_paiement_id']):
|
|
paiement_ction_data = MYSY_GV.dbname['base_partner_paiement_condition'].find_one(
|
|
{'_id': ObjectId(str(New_retVal['order_header_condition_paiement_id'])), 'valide': '1',
|
|
'locked': '0',
|
|
'partner_owner_recid': str(my_partner['recid'])})
|
|
|
|
if (paiement_ction_data and 'code' in paiement_ction_data.keys()):
|
|
paiement_ction_code = str(paiement_ction_data['code'])
|
|
user['order_header_paiement_condition_code'] = paiement_ction_code
|
|
|
|
# Si le champ 'order_header_client_id' alors on va chercher le nom du client
|
|
if ('order_header_client_id' in New_retVal.keys()):
|
|
Client_data = MYSY_GV.dbname['partner_client'].find_one(
|
|
{'_id': ObjectId(str(New_retVal['order_header_client_id'])), 'valide': '1', 'locked': '0',
|
|
'partner_recid': str(my_partner['recid'])})
|
|
|
|
if (Client_data and 'nom' in Client_data.keys()):
|
|
user['order_header_client_nom'] = str(Client_data['nom'])
|
|
|
|
# Si le champ 'order_header_vendeur_id' alors on va chercher le nom et prenom du vendeur (employe)
|
|
if ('order_header_vendeur_id' in New_retVal.keys() and New_retVal['order_header_vendeur_id'] ):
|
|
Employee_data = MYSY_GV.dbname['ressource_humaine'].find_one(
|
|
{'_id': ObjectId(str(New_retVal['order_header_vendeur_id'])), 'valide': '1', 'locked': '0',
|
|
'partner_recid': str(my_partner['recid'])})
|
|
|
|
order_header_vendeur_nom_prenom = ""
|
|
if (Employee_data and 'nom' in Employee_data.keys()):
|
|
order_header_vendeur_nom_prenom = str(Employee_data['nom'])
|
|
|
|
if (Employee_data and 'prenom' in Employee_data.keys()):
|
|
order_header_vendeur_nom_prenom = str(order_header_vendeur_nom_prenom) + " " + str(
|
|
Employee_data['prenom'])
|
|
|
|
user['order_header_vendeur_nom_prenom'] = str(order_header_vendeur_nom_prenom)
|
|
|
|
"""
|
|
Recuperer le solde (montant restant à payer)
|
|
"""
|
|
local_diction = {'token': str(diction['token']), 'invoice_id': str(New_retVal['_id'])}
|
|
|
|
reste_to_paye = "0"
|
|
local_solde_status, local_solde_retval = invoice_paiement_mgt.Get_Invoice_Total_Amount_Payed_And_Remaining_Amount(
|
|
local_diction)
|
|
if (local_solde_status):
|
|
sold_data = ast.literal_eval(local_solde_retval[0])
|
|
reste_to_paye = sold_data['remaining_amount']
|
|
|
|
user['reste_to_paye'] = reste_to_paye
|
|
|
|
if ("credit_note_ref" not in New_retVal.keys()):
|
|
user['credit_note_ref'] = ""
|
|
|
|
|
|
if( filter_date_debut and filter_date_fin ):
|
|
if ( datetime.strptime(str(New_retVal['invoice_date'])[0:10], '%d/%m/%Y') >= datetime.strptime(str(filter_date_debut)[0:10], '%d/%m/%Y') and
|
|
datetime.strptime(str(New_retVal['invoice_date'])[0:10], '%d/%m/%Y') <= datetime.strptime(str(filter_date_fin)[0:10], '%d/%m/%Y') ):
|
|
user['id'] = str(val_tmp)
|
|
val_tmp = val_tmp + 1
|
|
|
|
RetObject.append(mycommon.JSONEncoder().encode(user))
|
|
|
|
elif ( filter_date_debut ):
|
|
if ( datetime.strptime(str(New_retVal['invoice_date'])[0:10], '%d/%m/%Y') >= datetime.strptime(str(filter_date_debut)[0:10], '%d/%m/%Y') ):
|
|
user['id'] = str(val_tmp)
|
|
val_tmp = val_tmp + 1
|
|
RetObject.append(mycommon.JSONEncoder().encode(user))
|
|
|
|
elif ( filter_date_fin ):
|
|
if ( datetime.strptime(str(New_retVal['invoice_date'])[0:10], '%d/%m/%Y') <= datetime.strptime(str(filter_date_fin)[0:10], '%d/%m/%Y') ):
|
|
user['id'] = str(val_tmp)
|
|
val_tmp = val_tmp + 1
|
|
RetObject.append(mycommon.JSONEncoder().encode(user))
|
|
|
|
else:
|
|
user['id'] = str(val_tmp)
|
|
val_tmp = val_tmp + 1
|
|
RetObject.append(mycommon.JSONEncoder().encode(user))
|
|
|
|
|
|
total_data = []
|
|
node = {}
|
|
node['somme_header_ca_ht'] = str(somme_header_ca_ht)
|
|
node['nb_header_invoice'] = str(nb_header_invoice)
|
|
total_data.append(node)
|
|
|
|
total_data = mycommon.JSONEncoder().encode(total_data)
|
|
#print(" #### nb_result = ", val_tmp)
|
|
return True, RetObject, total_data
|
|
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de récupérer la liste des factures ", False
|
|
|
|
|
|
|
|
"""
|
|
Cette fonction permet de récupérer une ligne de detail d'une facture donnée
|
|
c'est a dire, une ligne de la collection 'partner_invoice_line'
|
|
"""
|
|
def Get_Given_Line_Of_Partner_Invoice_Lines(diction):
|
|
try:
|
|
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', 'invoice_line_id', 'invoice_header_ref_interne']
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list and val.startswith('my_') is False:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'existe pas")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token', 'invoice_line_id', 'invoice_header_ref_interne']
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans la liste des arguments ")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification de l'identité et autorisation de l'entité qui
|
|
appelle cette API
|
|
"""
|
|
token = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
token = diction['token']
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner
|
|
|
|
"""
|
|
Clés de mise à jour
|
|
"""
|
|
data_cle = {}
|
|
data_cle['partner_owner_recid'] = str(my_partner['recid'])
|
|
data_cle['invoice_line_id'] = str(diction['invoice_line_id'])
|
|
data_cle['invoice_header_ref_interne'] = str(diction['invoice_header_ref_interne'])
|
|
|
|
data_cle['valide'] = "1"
|
|
data_cle['locked'] = "0"
|
|
|
|
RetObject = []
|
|
val_tmp = 1
|
|
|
|
filt_class_partner_recid = {'partner_owner_recid': str(my_partner['recid'])}
|
|
filt_invoice_line_id = {'_id': ObjectId(str(diction['invoice_line_id']))}
|
|
filt_invoice_line_header_ref_interne = {'invoice_header_ref_interne': str(diction['invoice_header_ref_interne'])}
|
|
|
|
query = [{'$match': {'$and': [ filt_invoice_line_id, filt_invoice_line_header_ref_interne,
|
|
{'partner_owner_recid': str(my_partner['recid'])}]}},
|
|
{'$lookup':
|
|
{
|
|
'from': 'myclass',
|
|
'localField': 'order_line_formation',
|
|
'foreignField': 'internal_url',
|
|
'pipeline': [{'$match': {'$and': [ filt_class_partner_recid]}},
|
|
{'$project': {'title': 1, 'domaine': 1,
|
|
'duration': 1,
|
|
'duration_unit': 1,
|
|
'external_code':1}}],
|
|
'as': 'myclass_collection'
|
|
}
|
|
}
|
|
]
|
|
print("#### Get_Given_Partner_Invoice_Lines : query pip= ", query)
|
|
|
|
for retval in MYSY_GV.dbname['partner_invoice_line'].aggregate(query):
|
|
if ('myclass_collection' in retval.keys() and len(retval['myclass_collection']) > 0):
|
|
user = {}
|
|
user['id'] = str(val_tmp)
|
|
val_tmp = val_tmp + 1
|
|
user['_id'] = retval['_id']
|
|
|
|
user['order_line_formation'] = retval['order_line_formation']
|
|
user['order_line_qty'] = retval['order_line_qty']
|
|
user['order_line_prix_unitaire'] = retval['order_line_prix_unitaire']
|
|
user['order_header_id'] = retval['order_header_id']
|
|
user['order_header_ref_interne'] = retval['order_header_ref_interne']
|
|
|
|
user['invoice_header_ref_interne'] = retval['invoice_header_ref_interne']
|
|
user['invoice_line_type'] = retval['invoice_line_type']
|
|
user['invoice_date'] = retval['invoice_date']
|
|
user['invoice_header_id'] = retval['invoice_header_id']
|
|
|
|
|
|
if ("order_line_montant_reduction" in retval.keys()):
|
|
user['order_line_montant_reduction'] = retval['order_line_montant_reduction']
|
|
else:
|
|
user['order_line_montant_reduction'] = "0"
|
|
|
|
if ("order_line_type_article" in retval.keys()):
|
|
user['order_line_type_article'] = retval['order_line_type_article']
|
|
else:
|
|
user['order_line_type_article'] = ""
|
|
|
|
if ("order_line_type_apprenant" in retval.keys()):
|
|
user['order_line_type_apprenant'] = retval['order_line_type_apprenant']
|
|
else:
|
|
user['order_line_type_apprenant'] = ""
|
|
|
|
if ("order_line_montant_toutes_taxes" in retval.keys()):
|
|
user['order_line_montant_toutes_taxes'] = retval['order_line_montant_toutes_taxes']
|
|
else:
|
|
user['order_line_montant_toutes_taxes'] = "0"
|
|
|
|
if ("order_line_tax" in retval.keys()):
|
|
user['order_line_tax'] = retval['order_line_tax']
|
|
else:
|
|
user['order_line_tax'] = ""
|
|
|
|
if ("order_line_tax_amount" in retval.keys()):
|
|
user['order_line_tax_amount'] = retval['order_line_tax_amount']
|
|
else:
|
|
user['order_line_tax_amount'] = "0"
|
|
|
|
if ("order_line_type_reduction" in retval.keys()):
|
|
user['order_line_type_reduction'] = retval['order_line_type_reduction']
|
|
else:
|
|
user['order_line_montant_reduction'] = ""
|
|
|
|
if ("order_line_type_valeur" in retval.keys()):
|
|
user['order_line_type_valeur'] = retval['order_line_type_valeur']
|
|
else:
|
|
user['order_line_type_valeur'] = "0"
|
|
|
|
if ("order_line_montant_hors_taxes" in retval.keys()):
|
|
user['order_line_montant_hors_taxes'] = retval['order_line_montant_hors_taxes']
|
|
else:
|
|
user['order_line_montant_hors_taxes'] = "0"
|
|
|
|
|
|
user['valide'] = retval['valide']
|
|
user['locked'] = retval['locked']
|
|
user['date_update'] = retval['date_update']
|
|
user['update_by'] = str(my_partner['_id'])
|
|
user['partner_owner_recid'] = retval['partner_owner_recid']
|
|
|
|
|
|
if( "order_line_comment" in retval.keys()):
|
|
user['order_line_comment'] = retval['order_line_comment']
|
|
else:
|
|
user['order_line_comment'] = ""
|
|
|
|
user['title'] = retval['myclass_collection'][0]['title']
|
|
user['order_line_formation_external_code'] = retval['myclass_collection'][0]['external_code']
|
|
|
|
if ("domaine" in retval['myclass_collection'][0].keys()):
|
|
user['domaine'] = retval['myclass_collection'][0]['domaine']
|
|
else:
|
|
user['domaine'] = ""
|
|
|
|
user['duration'] = retval['myclass_collection'][0]['duration']
|
|
user['duration_unit'] = retval['myclass_collection'][0]['duration_unit']
|
|
|
|
if (str(retval['myclass_collection'][0]['duration_unit']) == "heure"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " h"
|
|
|
|
elif (str(retval['myclass_collection'][0]['duration_unit']) == "jour"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " j"
|
|
|
|
elif (str(retval['myclass_collection'][0]['duration_unit']) == "semaine"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " s"
|
|
|
|
elif (str(retval['myclass_collection'][0]['duration_unit']) == "mois"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " m"
|
|
|
|
elif (str(retval['myclass_collection'][0]['duration_unit']) == "annee"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " a"
|
|
|
|
elif (str(retval['myclass_collection'][0]['duration_unit']) == "user_rythme"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " u"
|
|
|
|
else:
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " ?"
|
|
|
|
RetObject.append(mycommon.JSONEncoder().encode(user))
|
|
|
|
|
|
|
|
"""
|
|
Recuperation des produits et services
|
|
"""
|
|
query = [{'$match': {'$and': [ filt_invoice_line_id, filt_invoice_line_header_ref_interne,
|
|
{'partner_owner_recid': str(my_partner['recid'])}]}},
|
|
{'$lookup': {
|
|
'from': 'partner_produit_service',
|
|
"let": {'order_line_formation': "$order_line_formation",
|
|
"partner_produit_service_partner_owner_recid": "$partner_owner_recid"
|
|
},
|
|
'pipeline': [
|
|
{'$match':
|
|
{'$expr':
|
|
{'$and':
|
|
[
|
|
{'$eq': ["$valide", "1"]},
|
|
{'$eq': ["$_id", {'$convert': {
|
|
'input': "$$order_line_formation",
|
|
'to': "objectId",
|
|
'onError': {'error': 'true'},
|
|
'onNull': {'isnull': 'true'}
|
|
}}]},
|
|
{'$eq': ["$partner_owner_recid",
|
|
'$$partner_produit_service_partner_owner_recid']},
|
|
|
|
]
|
|
}
|
|
}
|
|
},
|
|
|
|
],
|
|
'as': 'collection_partner_produit_service'
|
|
}
|
|
},
|
|
]
|
|
print("#### Get_Given_Partner_Invoice_Lines for PRODUCT & SERVICES : query pip= ", query)
|
|
|
|
for retval in MYSY_GV.dbname['partner_invoice_line'].aggregate(query):
|
|
if ('collection_partner_produit_service' in retval.keys() and len(retval['collection_partner_produit_service']) > 0):
|
|
user = {}
|
|
user['id'] = str(val_tmp)
|
|
val_tmp = val_tmp + 1
|
|
user['_id'] = retval['_id']
|
|
user['order_line_formation'] = retval['order_line_formation']
|
|
user['order_line_qty'] = retval['order_line_qty']
|
|
user['order_line_prix_unitaire'] = retval['order_line_prix_unitaire']
|
|
user['invoice_header_id'] = retval['invoice_header_id']
|
|
user['invoice_header_ref_interne'] = retval['invoice_header_ref_interne']
|
|
user['invoice_date'] = retval['invoice_date']
|
|
user['valide'] = retval['valide']
|
|
user['locked'] = retval['locked']
|
|
|
|
if ("order_line_montant_reduction" in retval.keys()):
|
|
user['order_line_montant_reduction'] = retval['order_line_montant_reduction']
|
|
else:
|
|
user['order_line_montant_reduction'] = "0"
|
|
|
|
if ("order_line_type_apprenant" in retval.keys()):
|
|
user['order_line_type_apprenant'] = retval['order_line_type_apprenant']
|
|
else:
|
|
user['order_line_type_apprenant'] = ""
|
|
|
|
|
|
if ("order_line_montant_toutes_taxes" in retval.keys()):
|
|
user['order_line_montant_toutes_taxes'] = retval['order_line_montant_toutes_taxes']
|
|
else:
|
|
user['order_line_montant_toutes_taxes'] = "0"
|
|
|
|
if ("order_line_tax" in retval.keys()):
|
|
user['order_line_tax'] = retval['order_line_tax']
|
|
else:
|
|
user['order_line_tax'] = ""
|
|
|
|
if ("order_line_tax_amount" in retval.keys()):
|
|
user['order_line_tax_amount'] = retval['order_line_tax_amount']
|
|
else:
|
|
user['order_line_tax_amount'] = "0"
|
|
|
|
if ("order_line_type_reduction" in retval.keys()):
|
|
user['order_line_type_reduction'] = retval['order_line_type_reduction']
|
|
else:
|
|
user['order_line_montant_reduction'] = ""
|
|
|
|
if ("order_line_type_valeur" in retval.keys()):
|
|
user['order_line_type_valeur'] = retval['order_line_type_valeur']
|
|
else:
|
|
user['order_line_type_valeur'] = "0"
|
|
|
|
if ("order_line_montant_hors_taxes" in retval.keys()):
|
|
user['order_line_montant_hors_taxes'] = retval['order_line_montant_hors_taxes']
|
|
else:
|
|
user['order_line_montant_hors_taxes'] = "0"
|
|
|
|
user['date_update'] = retval['date_update']
|
|
user['update_by'] = str(my_partner['_id'])
|
|
user['partner_owner_recid'] = retval['partner_owner_recid']
|
|
user['invoice_line_type'] = retval['invoice_line_type']
|
|
|
|
if ("order_line_comment" in retval.keys()):
|
|
user['order_line_comment'] = retval['order_line_comment']
|
|
else:
|
|
user['order_line_comment'] = ""
|
|
|
|
user['title'] = retval['collection_partner_produit_service'][0]['nom']
|
|
user['order_line_formation_external_code'] = retval['collection_partner_produit_service'][0]['code']
|
|
|
|
user['domaine'] = ""
|
|
|
|
user['duration'] = ""
|
|
user['duration_unit'] = ""
|
|
user['duration_concat'] = ""
|
|
|
|
"""
|
|
Il s'agit d'un produit, vu qu'on a un lien avec la collection "partner_produit_service", on force alors le 'order_line_type_article'
|
|
a 'produit'
|
|
"""
|
|
user['order_line_type_article'] = "produit"
|
|
|
|
RetObject.append(mycommon.JSONEncoder().encode(user))
|
|
|
|
|
|
#print(" ### RetObject = ", RetObject)
|
|
return True, RetObject
|
|
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de récupérer les lignes de facture "
|
|
|
|
|
|
"""
|
|
Impression PDF d'une facture
|
|
/!\ : update du 05/06/2024 :
|
|
Si la facture a un document sécurisée (e_Invoice) associé dans la colonne :
|
|
'e_document_signe_id', alors on va aller recuperer le document securisé associé
|
|
|
|
"""
|
|
|
|
def GerneratePDF_Partner_Invoice(diction):
|
|
try:
|
|
field_list = ['invoice_id', 'token', ]
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list and val.startswith('my_') is False:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans la liste des arguments ")
|
|
return False, " Les informations fournies sont incorrectes"
|
|
|
|
"""
|
|
Verification de la liste des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['invoice_id', 'token', ]
|
|
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - : La valeur '" + val + "' n'est pas presente dans la liste des arguments ")
|
|
return False, "Les informations fournies sont incorrectes"
|
|
|
|
query_get_data = {}
|
|
mytoken = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
mytoken = diction['token']
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner
|
|
|
|
# Verification de la validité de la facture
|
|
qry = {'_id': ObjectId(str(diction['invoice_id'])), 'valide': '1', 'locked': '0',
|
|
'partner_owner_recid': str(my_partner['recid'])}
|
|
#print(" ### qry = ", qry)
|
|
|
|
is_invoice_Existe_Count = MYSY_GV.dbname['partner_invoice_header'].count_documents(
|
|
{'_id': ObjectId(str(diction['invoice_id'])),
|
|
'valide': '1', 'locked': '0',
|
|
'partner_owner_recid': str(my_partner['recid'])})
|
|
|
|
if (is_invoice_Existe_Count != 1):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La facture n'est pas valide ")
|
|
return False, " La facture n'est pas valide",
|
|
|
|
Order_header_data = MYSY_GV.dbname['partner_invoice_header'].find_one({'_id': ObjectId(str(diction['invoice_id'])),
|
|
'valide': '1', 'locked': '0',
|
|
'partner_owner_recid': str(
|
|
my_partner['recid'])})
|
|
|
|
|
|
|
|
|
|
if( "e_document_signe_id" in Order_header_data.keys() and Order_header_data['e_document_signe_id'] ):
|
|
# On retourne la e_Invoice securisée
|
|
|
|
print(" #### RECUPERATION DU FICHIER SECURISE ")
|
|
qry = {'_id':ObjectId(str(Order_header_data['e_document_signe_id'])),
|
|
'valide':'1',
|
|
'locked':'0',
|
|
'statut':'1',
|
|
'related_collection':'partner_invoice_header',
|
|
'related_collection_id':str(Order_header_data['_id']),
|
|
'partner_owner_recid':str(my_partner['recid'])}
|
|
|
|
|
|
e_Invoice_Secure_Data = MYSY_GV.dbname['e_document_signe'].find_one({'_id':ObjectId(str(Order_header_data['e_document_signe_id'])),
|
|
'valide':'1',
|
|
'locked':'0',
|
|
'statut':'1',
|
|
'related_collection':'partner_invoice_header',
|
|
'related_collection_id':str(Order_header_data['_id']),
|
|
'partner_owner_recid':str(my_partner['recid'])})
|
|
|
|
if(e_Invoice_Secure_Data is None ):
|
|
mycommon.myprint(str(inspect.stack()[0][
|
|
3]) + " Le document sécurisé associé à la facture est invalide 'e_document_signe_id' = "+str(Order_header_data['e_document_signe_id']))
|
|
|
|
return False, " Le document sécurisé associé à la facture est invalide "
|
|
|
|
orig_file_name = "Partner_Invoice_" + str(Order_header_data['invoice_header_ref_interne']) + ".pdf"
|
|
outputFilename = str(MYSY_GV.TEMPORARY_DIRECTORY) + "/" + str(orig_file_name)
|
|
|
|
|
|
# open output file for writing (truncated binary)
|
|
with open(outputFilename, 'w+b') as resultFile:
|
|
encoded = base64.b64encode(e_Invoice_Secure_Data['document_data_signed'])
|
|
decode_data = e_Invoice_Secure_Data['document_data_signed'].decode()
|
|
bytes = base64.b64decode(decode_data, validate=True)
|
|
resultFile.write(bytes)
|
|
|
|
|
|
# close output file
|
|
resultFile.close()
|
|
|
|
# print(" ### outputFilename = "+str(outputFilename))
|
|
if os.path.exists(outputFilename):
|
|
# print(" ### ok os.path.exists(outputFilename) "+str(outputFilename))
|
|
return True, send_file(outputFilename, as_attachment=True)
|
|
|
|
|
|
return True, " le fichier securisé recuperé "
|
|
|
|
|
|
else:
|
|
partner_document_INVOICE_data_qry = {'partner_owner_recid': str(my_partner['recid']),
|
|
'valide': '1', 'locked': '0', 'ref_interne': 'PART_INVOICE', 'type_doc':'pdf'}
|
|
|
|
print(" ### partner_document_INVOICE_data_qry = ", partner_document_INVOICE_data_qry)
|
|
partner_document_INVOICE_data = MYSY_GV.dbname['courrier_template'].find_one(
|
|
{'partner_owner_recid': str(my_partner['recid']),
|
|
'valide': '1', 'locked': '0', 'ref_interne': 'PART_INVOICE', 'type_doc':'pdf'})
|
|
|
|
if (partner_document_INVOICE_data is None):
|
|
# Il n'existe pas de personnalisation de la preinscription pour ce partenaire, on va aller récupérer la presinscription pa defaut
|
|
partner_document_INVOICE_data = MYSY_GV.dbname['courrier_template'].find_one(
|
|
{'partner_owner_recid': 'default',
|
|
'valide': '1', 'locked': '0', 'ref_interne': 'PART_INVOICE', 'type_doc':'pdf'})
|
|
|
|
if (partner_document_INVOICE_data is None):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + "Aucun document parametré ")
|
|
return False, "Aucun document parametré "
|
|
|
|
if ("contenu_doc" not in partner_document_INVOICE_data or len(
|
|
str(partner_document_INVOICE_data['contenu_doc'])) <= 0):
|
|
mycommon.myprint(str(inspect.stack()[0][
|
|
3]) + " - Le parametrage du document est invalide")
|
|
return False, " Le parametrage du document est invalide "
|
|
|
|
# Recuperation des données du client
|
|
if ("order_header_client_id" in Order_header_data.keys()):
|
|
Order_header_client_data = MYSY_GV.dbname['partner_client'].find_one(
|
|
{'_id': ObjectId(str(Order_header_data['order_header_client_id'])),
|
|
'partner_recid': str(my_partner['recid']), 'valide': '1', 'locked': '0'})
|
|
|
|
if (Order_header_client_data is None):
|
|
mycommon.myprint(str(inspect.stack()[0][
|
|
3]) + " - Le client est invalide")
|
|
return False, " Le client est invalide"
|
|
|
|
### Ajout des données du client sur l'entete de la commande, exemple : le nom, email, etc
|
|
if("raison_sociale" in Order_header_client_data.keys() ):
|
|
Order_header_data['client_raison_sociale'] = Order_header_client_data['raison_sociale']
|
|
|
|
if ("nom" in Order_header_client_data.keys()):
|
|
Order_header_data['client_nom'] = Order_header_client_data['nom']
|
|
|
|
if ("email" in Order_header_client_data.keys()):
|
|
Order_header_data['client_email'] = Order_header_client_data['email']
|
|
|
|
# Ajout d'un parametre pour le data time du jour de l'edition (c'est une data static qui peut servir pour l'horodatage
|
|
Order_header_data['current_date_time'] = str(datetime.now().strftime("%d/%m/%Y %H:%M:%S") )
|
|
|
|
|
|
# Recuperation des details de lignes de : partner_invoice_line
|
|
filt_class_partner_recid = {'partner_owner_recid': str(my_partner['recid'])}
|
|
filt_order_header_order_id = {'invoice_header_id': str(diction['invoice_id'])}
|
|
|
|
query = [{'$match': {'$and': [filt_order_header_order_id,
|
|
{'partner_owner_recid': str(my_partner['recid'])}]}},
|
|
{'$lookup':
|
|
{
|
|
'from': 'myclass',
|
|
'localField': 'order_line_formation',
|
|
'foreignField': 'internal_url',
|
|
'pipeline': [{'$match': {'$and': [filt_class_partner_recid]}},
|
|
{'$project': {'title': 1, 'domaine': 1,
|
|
'duration': 1,
|
|
'duration_unit': 1,
|
|
'external_code':1}}],
|
|
'as': 'myclass_collection'
|
|
}
|
|
}
|
|
]
|
|
#print("#### Get_Given_Partner_Order_Lines_From_order_ref_interne : query pip= ", query)
|
|
val_tmp = 0
|
|
Order_header_lines_data = []
|
|
for retval in MYSY_GV.dbname['partner_invoice_line'].aggregate(query):
|
|
if ('myclass_collection' in retval.keys() and len(retval['myclass_collection']) > 0):
|
|
user = {}
|
|
user['id'] = str(val_tmp)
|
|
val_tmp = val_tmp + 1
|
|
user['_id'] = retval['_id']
|
|
user['order_line_formation'] = retval['order_line_formation']
|
|
user['order_line_qty'] = retval['order_line_qty']
|
|
user['order_line_prix_unitaire'] = retval['order_line_prix_unitaire']
|
|
user['order_header_id'] = retval['order_header_id']
|
|
user['order_header_ref_interne'] = retval['order_header_ref_interne']
|
|
user['invoice_header_ref_interne'] = retval['invoice_header_ref_interne']
|
|
user['invoice_line_type'] = retval['invoice_line_type']
|
|
user['invoice_date'] = retval['invoice_date']
|
|
|
|
user['valide'] = retval['valide']
|
|
user['locked'] = retval['locked']
|
|
|
|
if ("order_line_montant_reduction" in retval.keys()):
|
|
user['order_line_montant_reduction'] = retval['order_line_montant_reduction']
|
|
else:
|
|
user['order_line_montant_reduction'] = "0"
|
|
|
|
if ("order_line_type_article" in retval.keys()):
|
|
user['order_line_type_article'] = retval['order_line_type_article']
|
|
else:
|
|
user['order_line_type_article'] = ""
|
|
|
|
|
|
if ("order_line_type_apprenant" in retval.keys()):
|
|
user['order_line_type_apprenant'] = retval['order_line_type_apprenant']
|
|
else:
|
|
user['order_line_type_apprenant'] = ""
|
|
|
|
if ("order_line_montant_toutes_taxes" in retval.keys()):
|
|
user['order_line_montant_toutes_taxes'] = retval['order_line_montant_toutes_taxes']
|
|
else:
|
|
user['order_line_montant_toutes_taxes'] = "0"
|
|
|
|
if ("order_line_tax" in retval.keys()):
|
|
user['order_line_tax'] = retval['order_line_tax']
|
|
else:
|
|
user['order_line_tax'] = ""
|
|
|
|
if ("order_line_tax_amount" in retval.keys()):
|
|
user['order_line_tax_amount'] = retval['order_line_tax_amount']
|
|
else:
|
|
user['order_line_tax_amount'] = "0"
|
|
|
|
if ("order_line_type_reduction" in retval.keys()):
|
|
user['order_line_type_reduction'] = retval['order_line_type_reduction']
|
|
else:
|
|
user['order_line_montant_reduction'] = ""
|
|
|
|
if ("order_line_type_valeur" in retval.keys()):
|
|
user['order_line_type_valeur'] = retval['order_line_type_valeur']
|
|
else:
|
|
user['order_line_type_valeur'] = "0"
|
|
|
|
if ("order_line_montant_hors_taxes" in retval.keys()):
|
|
user['order_line_montant_hors_taxes'] = retval['order_line_montant_hors_taxes']
|
|
else:
|
|
user['order_line_montant_hors_taxes'] = "0"
|
|
|
|
user['date_update'] = retval['date_update']
|
|
user['update_by'] = str(my_partner['_id'])
|
|
user['partner_owner_recid'] = retval['partner_owner_recid']
|
|
|
|
if ("order_line_comment" in retval.keys()):
|
|
user['order_line_comment'] = retval['order_line_comment']
|
|
else:
|
|
user['order_line_comment'] = ""
|
|
|
|
user['title'] = retval['myclass_collection'][0]['title']
|
|
user['order_line_formation_external_code'] = retval['myclass_collection'][0]['external_code']
|
|
|
|
if ("domaine" in retval['myclass_collection'][0].keys()):
|
|
user['domaine'] = retval['myclass_collection'][0]['domaine']
|
|
else:
|
|
user['domaine'] = ""
|
|
|
|
user['duration'] = retval['myclass_collection'][0]['duration']
|
|
user['duration_unit'] = retval['myclass_collection'][0]['duration_unit']
|
|
|
|
if (str(retval['myclass_collection'][0]['duration_unit']) == "heure"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " h"
|
|
|
|
elif (str(retval['myclass_collection'][0]['duration_unit']) == "jour"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " j"
|
|
|
|
elif (str(retval['myclass_collection'][0]['duration_unit']) == "semaine"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " s"
|
|
|
|
elif (str(retval['myclass_collection'][0]['duration_unit']) == "mois"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " m"
|
|
|
|
elif (str(retval['myclass_collection'][0]['duration_unit']) == "annee"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " a"
|
|
|
|
elif (str(retval['myclass_collection'][0]['duration_unit']) == "user_rythme"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " u"
|
|
|
|
else:
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " ?"
|
|
|
|
"""
|
|
Il s'agit d'un formation, vu qu'on a un lien avec la collection "myclass", on force alors le 'order_line_type_article'
|
|
a 'formation'
|
|
"""
|
|
user['order_line_type_article'] = "formation"
|
|
|
|
|
|
Order_header_lines_data.append(user)
|
|
|
|
"""
|
|
Recuperation des produits et services
|
|
"""
|
|
query = query = [{'$match': {'$and': [filt_order_header_order_id,
|
|
{'partner_owner_recid': str(my_partner['recid'])}]}},
|
|
{'$lookup': {
|
|
'from': 'partner_produit_service',
|
|
"let": {'order_line_formation': "$order_line_formation",
|
|
"partner_produit_service_partner_owner_recid": "$partner_owner_recid"
|
|
},
|
|
'pipeline': [
|
|
{'$match':
|
|
{'$expr':
|
|
{'$and':
|
|
[
|
|
{'$eq': ["$valide", "1"]},
|
|
{'$eq': ["$_id", {'$convert': {
|
|
'input': "$$order_line_formation",
|
|
'to': "objectId",
|
|
'onError': {'error': 'true'},
|
|
'onNull': {'isnull': 'true'}
|
|
}}]},
|
|
{'$eq': ["$partner_owner_recid",
|
|
'$$partner_produit_service_partner_owner_recid']},
|
|
|
|
]
|
|
}
|
|
}
|
|
},
|
|
|
|
],
|
|
'as': 'collection_partner_produit_service'
|
|
}
|
|
},
|
|
]
|
|
print("#### Get_Given_Partner_Invoice_Lines for PRODUCT & SERVICES : query pip= ", query)
|
|
|
|
for retval in MYSY_GV.dbname['partner_invoice_line'].aggregate(query):
|
|
if ('collection_partner_produit_service' in retval.keys() and len(retval['collection_partner_produit_service']) > 0):
|
|
user = {}
|
|
user['id'] = str(val_tmp)
|
|
val_tmp = val_tmp + 1
|
|
user['_id'] = retval['_id']
|
|
user['order_line_formation'] = retval['order_line_formation']
|
|
user['order_line_qty'] = retval['order_line_qty']
|
|
user['order_line_prix_unitaire'] = retval['order_line_prix_unitaire']
|
|
user['invoice_header_id'] = retval['invoice_header_id']
|
|
user['invoice_header_ref_interne'] = retval['invoice_header_ref_interne']
|
|
user['invoice_date'] = retval['invoice_date']
|
|
user['valide'] = retval['valide']
|
|
user['locked'] = retval['locked']
|
|
|
|
if ("order_line_montant_reduction" in retval.keys()):
|
|
user['order_line_montant_reduction'] = retval['order_line_montant_reduction']
|
|
else:
|
|
user['order_line_montant_reduction'] = "0"
|
|
|
|
if ("order_line_type_apprenant" in retval.keys()):
|
|
user['order_line_type_apprenant'] = retval['order_line_type_apprenant']
|
|
else:
|
|
user['order_line_type_apprenant'] = ""
|
|
|
|
|
|
if ("order_line_montant_toutes_taxes" in retval.keys()):
|
|
user['order_line_montant_toutes_taxes'] = retval['order_line_montant_toutes_taxes']
|
|
else:
|
|
user['order_line_montant_toutes_taxes'] = "0"
|
|
|
|
if ("order_line_tax" in retval.keys()):
|
|
user['order_line_tax'] = retval['order_line_tax']
|
|
else:
|
|
user['order_line_tax'] = ""
|
|
|
|
if ("order_line_tax_amount" in retval.keys()):
|
|
user['order_line_tax_amount'] = retval['order_line_tax_amount']
|
|
else:
|
|
user['order_line_tax_amount'] = "0"
|
|
|
|
if ("order_line_type_reduction" in retval.keys()):
|
|
user['order_line_type_reduction'] = retval['order_line_type_reduction']
|
|
else:
|
|
user['order_line_montant_reduction'] = ""
|
|
|
|
if ("order_line_type_valeur" in retval.keys()):
|
|
user['order_line_type_valeur'] = retval['order_line_type_valeur']
|
|
else:
|
|
user['order_line_type_valeur'] = "0"
|
|
|
|
if ("order_line_montant_hors_taxes" in retval.keys()):
|
|
user['order_line_montant_hors_taxes'] = retval['order_line_montant_hors_taxes']
|
|
else:
|
|
user['order_line_montant_hors_taxes'] = "0"
|
|
|
|
user['date_update'] = retval['date_update']
|
|
user['update_by'] = str(my_partner['_id'])
|
|
user['partner_owner_recid'] = retval['partner_owner_recid']
|
|
user['invoice_line_type'] = retval['invoice_line_type']
|
|
|
|
if ("order_line_comment" in retval.keys()):
|
|
user['order_line_comment'] = retval['order_line_comment']
|
|
else:
|
|
user['order_line_comment'] = ""
|
|
|
|
user['title'] = retval['collection_partner_produit_service'][0]['nom']
|
|
user['order_line_formation_external_code'] = retval['collection_partner_produit_service'][0]['code']
|
|
|
|
user['domaine'] = ""
|
|
|
|
user['duration'] = ""
|
|
user['duration_unit'] = ""
|
|
user['duration_concat'] = ""
|
|
|
|
"""
|
|
Il s'agit d'un produit, vu qu'on a un lien avec la collection "partner_produit_service", on force alors le 'order_line_type_article'
|
|
a 'produit'
|
|
"""
|
|
user['order_line_type_article'] = "produit"
|
|
|
|
Order_header_lines_data.append(user)
|
|
|
|
if (len(Order_header_lines_data) <= 0):
|
|
mycommon.myprint(str(inspect.stack()[0][
|
|
3]) + " - Aucune ligne de détail pour cette facture ")
|
|
return False, " Aucune ligne de détail pour cette facture "
|
|
|
|
#print(" ### Order_header_lines_data = ", Order_header_lines_data)
|
|
|
|
|
|
contenu_doc_Template = jinja2.Template(str(partner_document_INVOICE_data['contenu_doc']))
|
|
|
|
#print(" #### partner_document_INVOICE_data = ", str(partner_document_INVOICE_data['contenu_doc']))
|
|
#sourceHtml = contenu_doc_Template.render(params=Order_header_data)
|
|
|
|
#print(" ### Order_header_data = ", Order_header_data)
|
|
#print(" ### Order_header_lines_data = ", Order_header_lines_data)
|
|
|
|
|
|
"""
|
|
Recuperation du dictionnaire des info
|
|
"""
|
|
tab_client = []
|
|
tab_client.append(ObjectId(str(Order_header_client_data['_id'])))
|
|
|
|
new_diction = {}
|
|
new_diction['token'] = diction['token']
|
|
new_diction['list_stagiaire_id'] = []
|
|
new_diction['list_session_id'] = []
|
|
new_diction['list_class_id'] = []
|
|
new_diction['list_client_id'] = tab_client
|
|
new_diction['list_apprenant_id'] = []
|
|
|
|
local_status, local_retval = mycommon.Get_Dictionnary_data_For_Template(new_diction)
|
|
|
|
if (local_status is False):
|
|
return local_status, local_retval
|
|
|
|
convention_dictionnary_data = local_retval
|
|
company_data = {
|
|
"params": convention_dictionnary_data,
|
|
}
|
|
|
|
convention_dictionnary_data['order_header'] = Order_header_data
|
|
convention_dictionnary_data['order_lines'] = Order_header_lines_data
|
|
|
|
#sourceHtml = contenu_doc_Template.render(params=Order_header_data, param_order_lines=Order_header_lines_data, company_data=company_data)
|
|
|
|
#sourceHtml = contenu_doc_Template.render(params_order_header=Order_header_data, params_order_lines=Order_header_lines_data, params=company_data['params'])
|
|
|
|
sourceHtml = contenu_doc_Template.render(params=company_data['params'])
|
|
|
|
orig_file_name = "Partner_Invoice_"+str(Order_header_data['invoice_header_ref_interne'])+".pdf"
|
|
outputFilename = str(MYSY_GV.TEMPORARY_DIRECTORY) +"/"+ str(orig_file_name)
|
|
|
|
# open output file for writing (truncated binary)
|
|
resultFile = open(outputFilename, "w+b")
|
|
|
|
# convert HTML to PDF
|
|
pisaStatus = pisa.CreatePDF(
|
|
src=sourceHtml, # the HTML to convert
|
|
dest=resultFile) # file handle to receive result
|
|
|
|
# close output file
|
|
resultFile.close()
|
|
|
|
# print(" ### outputFilename = "+str(outputFilename))
|
|
if os.path.exists(outputFilename):
|
|
# print(" ### ok os.path.exists(outputFilename) "+str(outputFilename))
|
|
return True, send_file(outputFilename, as_attachment=True)
|
|
|
|
# return True on success and False on errors
|
|
print(pisaStatus.err, type(pisaStatus.err))
|
|
|
|
return True, " le fichier generé "
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
print(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno))
|
|
return False, False
|
|
|
|
|
|
"""
|
|
Envoie de la facture par email
|
|
"""
|
|
def Send_Partner_Invoice_By_Email(tab_files, Folder, diction):
|
|
try:
|
|
field_list = ['invoice_id', 'token', ]
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list and val.startswith('my_') is False:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans la liste des arguments ")
|
|
return False, "Les informations fournies sont incorrectes"
|
|
|
|
"""
|
|
Verification de la liste des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['invoice_id', 'token', ]
|
|
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - : La valeur '" + val + "' n'est pas presente dans la liste des arguments ")
|
|
return False, " Les informations fournies sont incorrectes "
|
|
|
|
query_get_data = {}
|
|
mytoken = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
mytoken = diction['token']
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner
|
|
|
|
# Verification de la validité de la facture
|
|
qry = {'_id': ObjectId(str(diction['invoice_id'])), 'valide': '1', 'locked': '0',
|
|
'partner_owner_recid': str(my_partner['recid'])}
|
|
#print(" ### qry = ", qry)
|
|
|
|
is_Invoice_Existe_Count = MYSY_GV.dbname['partner_invoice_header'].count_documents(
|
|
{'_id': ObjectId(str(diction['invoice_id'])),
|
|
'valide': '1', 'locked': '0',
|
|
'partner_owner_recid': str(my_partner['recid'])})
|
|
|
|
if (is_Invoice_Existe_Count != 1):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La facture n'est pas valide is_Invoice_Existe_Count = "+str(is_Invoice_Existe_Count))
|
|
return False, " La facture n'est pas valide",
|
|
|
|
Order_header_data = MYSY_GV.dbname['partner_invoice_header'].find_one({'_id': ObjectId(str(diction['invoice_id'])),
|
|
'valide': '1', 'locked': '0',
|
|
'partner_owner_recid': str(
|
|
my_partner['recid'])})
|
|
|
|
partner_document_INVOICE_data_qry = {'partner_owner_recid': str(my_partner['recid']),
|
|
'valide': '1', 'locked': '0', 'ref_interne': 'PART_INVOICE', 'type_doc':'email'}
|
|
|
|
print(" ### partner_document_CONF_ORDER_data_qry = ", partner_document_INVOICE_data_qry)
|
|
partner_document_INVOICE_data = MYSY_GV.dbname['courrier_template'].find_one(
|
|
{'partner_owner_recid': str(my_partner['recid']),
|
|
'valide': '1', 'locked': '0', 'ref_interne': 'PART_INVOICE', 'type_doc':'email'})
|
|
|
|
if (partner_document_INVOICE_data is None):
|
|
# Il n'existe pas de personnalisation de la preinscription pour ce partenaire, on va aller récupérer la presinscription pa defaut
|
|
partner_document_INVOICE_data = MYSY_GV.dbname['courrier_template'].find_one(
|
|
{'partner_owner_recid': 'default',
|
|
'valide': '1', 'locked': '0', 'ref_interne': 'PART_INVOICE', 'type_doc':'email'})
|
|
|
|
if (partner_document_INVOICE_data is None):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][
|
|
3]) + "Aucun document parametré ")
|
|
return False, "Aucun document parametré "
|
|
|
|
if ("contenu_doc" not in partner_document_INVOICE_data or len(
|
|
str(partner_document_INVOICE_data['contenu_doc'])) <= 0):
|
|
mycommon.myprint(str(inspect.stack()[0][
|
|
3]) + " - Le parametrage du document est invalide")
|
|
return False, " Le parametrage du document est invalide "
|
|
|
|
|
|
# Recuperation des données du client
|
|
if( "order_header_client_id" in Order_header_data.keys() ):
|
|
Order_header_client_data = MYSY_GV.dbname['partner_client'].find_one({'_id':ObjectId(str(Order_header_data['order_header_client_id'])),
|
|
'partner_recid':str(my_partner['recid']), 'valide':'1', 'locked':'0'})
|
|
|
|
if( Order_header_client_data is None):
|
|
mycommon.myprint(str(inspect.stack()[0][
|
|
3]) + " - Le client est invalide")
|
|
return False, " Le client est invalide"
|
|
|
|
### Ajout des données du client sur l'entete de la commande, exemple : le nom, email, etc
|
|
if ("raison_sociale" in Order_header_client_data.keys()):
|
|
Order_header_data['client_raison_sociale'] = Order_header_client_data['raison_sociale']
|
|
|
|
if ("nom" in Order_header_client_data.keys()):
|
|
Order_header_data['client_nom'] = Order_header_client_data['nom']
|
|
|
|
if ("email" in Order_header_client_data.keys()):
|
|
Order_header_data['client_email'] = Order_header_client_data['email']
|
|
|
|
# Ajout d'un parametre pour le data time du jour de l'edition (c'est une data static qui peut servir pour l'horodatage
|
|
Order_header_data['current_date_time'] = str(datetime.now().strftime("%d/%m/%Y %H:%M:%S"))
|
|
|
|
# Recuperation des details de lignes de : partner_order_line
|
|
filt_class_partner_recid = {'partner_owner_recid': str(my_partner['recid'])}
|
|
filt_order_header_order_id= {'invoice_header_id': str(diction['invoice_id'])}
|
|
|
|
query = [{'$match': {'$and': [filt_order_header_order_id,
|
|
{'partner_owner_recid': str(my_partner['recid'])}]}},
|
|
{'$lookup':
|
|
{
|
|
'from': 'myclass',
|
|
'localField': 'order_line_formation',
|
|
'foreignField': 'internal_url',
|
|
'pipeline': [{'$match': {'$and': [filt_class_partner_recid]}},
|
|
{'$project': {'title': 1, 'domaine': 1,
|
|
'duration': 1,
|
|
'duration_unit': 1,
|
|
'external_code':1}}],
|
|
'as': 'myclass_collection'
|
|
}
|
|
}
|
|
]
|
|
#print("#### Get_Given_Partner_Order_Lines_From_order_ref_interne : query pip= ", query)
|
|
val_tmp = 0
|
|
Order_header_lines_data = []
|
|
for retval in MYSY_GV.dbname['partner_invoice_line'].aggregate(query):
|
|
if ('myclass_collection' in retval.keys() and len(retval['myclass_collection']) > 0):
|
|
user = {}
|
|
user['id'] = str(val_tmp)
|
|
val_tmp = val_tmp + 1
|
|
user['_id'] = retval['_id']
|
|
user['order_line_formation'] = retval['order_line_formation']
|
|
user['order_line_qty'] = retval['order_line_qty']
|
|
user['order_line_prix_unitaire'] = retval['order_line_prix_unitaire']
|
|
user['order_header_id'] = retval['order_header_id']
|
|
user['order_header_ref_interne'] = retval['order_header_ref_interne']
|
|
|
|
user['invoice_header_ref_interne'] = retval['invoice_header_ref_interne']
|
|
user['invoice_line_type'] = retval['invoice_line_type']
|
|
user['invoice_date'] = retval['invoice_date']
|
|
user['invoice_header_id'] = retval['invoice_header_id']
|
|
|
|
user['valide'] = retval['valide']
|
|
user['locked'] = retval['locked']
|
|
|
|
if ("order_line_montant_reduction" in retval.keys()):
|
|
user['order_line_montant_reduction'] = retval['order_line_montant_reduction']
|
|
else:
|
|
user['order_line_montant_reduction'] = "0"
|
|
|
|
if ("order_line_type_article" in retval.keys()):
|
|
user['order_line_type_article'] = retval['order_line_type_article']
|
|
else:
|
|
user['order_line_type_article'] = ""
|
|
|
|
|
|
if ("order_line_type_apprenant" in retval.keys()):
|
|
user['order_line_type_apprenant'] = retval['order_line_type_apprenant']
|
|
else:
|
|
user['order_line_type_apprenant'] = ""
|
|
|
|
|
|
if ("order_line_montant_toutes_taxes" in retval.keys()):
|
|
user['order_line_montant_toutes_taxes'] = retval['order_line_montant_toutes_taxes']
|
|
else:
|
|
user['order_line_montant_toutes_taxes'] = "0"
|
|
|
|
if ("order_line_tax" in retval.keys()):
|
|
user['order_line_tax'] = retval['order_line_tax']
|
|
else:
|
|
user['order_line_tax'] = ""
|
|
|
|
if ("order_line_tax_amount" in retval.keys()):
|
|
user['order_line_tax_amount'] = retval['order_line_tax_amount']
|
|
else:
|
|
user['order_line_tax_amount'] = "0"
|
|
|
|
if ("order_line_type_reduction" in retval.keys()):
|
|
user['order_line_type_reduction'] = retval['order_line_type_reduction']
|
|
else:
|
|
user['order_line_montant_reduction'] = ""
|
|
|
|
if ("order_line_type_valeur" in retval.keys()):
|
|
user['order_line_type_valeur'] = retval['order_line_type_valeur']
|
|
else:
|
|
user['order_line_type_valeur'] = "0"
|
|
|
|
if ("order_line_montant_hors_taxes" in retval.keys()):
|
|
user['order_line_montant_hors_taxes'] = retval['order_line_montant_hors_taxes']
|
|
else:
|
|
user['order_line_montant_hors_taxes'] = "0"
|
|
|
|
user['date_update'] = retval['date_update']
|
|
user['update_by'] = str(my_partner['_id'])
|
|
user['partner_owner_recid'] = retval['partner_owner_recid']
|
|
|
|
if ("order_line_comment" in retval.keys()):
|
|
user['order_line_comment'] = retval['order_line_comment']
|
|
else:
|
|
user['order_line_comment'] = ""
|
|
|
|
user['title'] = retval['myclass_collection'][0]['title']
|
|
user['order_line_formation_external_code'] = retval['myclass_collection'][0]['external_code']
|
|
|
|
if ("domaine" in retval['myclass_collection'][0].keys()):
|
|
user['domaine'] = retval['myclass_collection'][0]['domaine']
|
|
else:
|
|
user['domaine'] = ""
|
|
|
|
user['duration'] = retval['myclass_collection'][0]['duration']
|
|
user['duration_unit'] = retval['myclass_collection'][0]['duration_unit']
|
|
|
|
if (str(retval['myclass_collection'][0]['duration_unit']) == "heure"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " h"
|
|
|
|
elif (str(retval['myclass_collection'][0]['duration_unit']) == "jour"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " j"
|
|
|
|
elif (str(retval['myclass_collection'][0]['duration_unit']) == "semaine"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " s"
|
|
|
|
elif (str(retval['myclass_collection'][0]['duration_unit']) == "mois"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " m"
|
|
|
|
elif (str(retval['myclass_collection'][0]['duration_unit']) == "annee"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " a"
|
|
|
|
elif (str(retval['myclass_collection'][0]['duration_unit']) == "user_rythme"):
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " u"
|
|
|
|
else:
|
|
user['duration_concat'] = str(retval['myclass_collection'][0]['duration']) + " ?"
|
|
|
|
"""
|
|
Il s'agit d'un formation, vu qu'on a un lien avec la collection "myclass", on force alors le 'order_line_type_article'
|
|
a 'formation'
|
|
"""
|
|
user['order_line_type_article'] = "formation"
|
|
|
|
Order_header_lines_data.append(user)
|
|
|
|
"""
|
|
Recuperation des produits et services
|
|
"""
|
|
query = [{'$match': {'$and': [filt_order_header_order_id,
|
|
{'partner_owner_recid': str(my_partner['recid'])}]}},
|
|
|
|
{'$lookup': {
|
|
'from': 'partner_produit_service',
|
|
"let": {'order_line_formation': "$order_line_formation",
|
|
"partner_produit_service_partner_owner_recid": "$partner_owner_recid"
|
|
},
|
|
'pipeline': [
|
|
{'$match':
|
|
{'$expr':
|
|
{'$and':
|
|
[
|
|
{'$eq': ["$valide", "1"]},
|
|
{'$eq': ["$_id", {'$convert': {
|
|
'input': "$$order_line_formation",
|
|
'to': "objectId",
|
|
'onError': {'error': 'true'},
|
|
'onNull': {'isnull': 'true'}
|
|
}}]},
|
|
{'$eq': ["$partner_owner_recid",
|
|
'$$partner_produit_service_partner_owner_recid']},
|
|
|
|
]
|
|
}
|
|
}
|
|
},
|
|
|
|
],
|
|
'as': 'collection_partner_produit_service'
|
|
}
|
|
},
|
|
]
|
|
print("#### Get_Given_Partner_Invoice_Lines for PRODUCT & SERVICES : query pip= ", query)
|
|
|
|
for retval in MYSY_GV.dbname['partner_invoice_line'].aggregate(query):
|
|
if ('collection_partner_produit_service' in retval.keys() and len(retval['collection_partner_produit_service']) > 0):
|
|
user = {}
|
|
user['id'] = str(val_tmp)
|
|
val_tmp = val_tmp + 1
|
|
user['_id'] = retval['_id']
|
|
user['order_line_formation'] = retval['order_line_formation']
|
|
user['order_line_qty'] = retval['order_line_qty']
|
|
user['order_line_prix_unitaire'] = retval['order_line_prix_unitaire']
|
|
user['invoice_header_id'] = retval['invoice_header_id']
|
|
user['invoice_header_ref_interne'] = retval['invoice_header_ref_interne']
|
|
user['invoice_date'] = retval['invoice_date']
|
|
user['valide'] = retval['valide']
|
|
user['locked'] = retval['locked']
|
|
|
|
if ("order_line_montant_reduction" in retval.keys()):
|
|
user['order_line_montant_reduction'] = retval['order_line_montant_reduction']
|
|
else:
|
|
user['order_line_montant_reduction'] = "0"
|
|
|
|
if ("order_line_type_apprenant" in retval.keys()):
|
|
user['order_line_type_apprenant'] = retval['order_line_type_apprenant']
|
|
else:
|
|
user['order_line_type_apprenant'] = ""
|
|
|
|
|
|
if ("order_line_montant_toutes_taxes" in retval.keys()):
|
|
user['order_line_montant_toutes_taxes'] = retval['order_line_montant_toutes_taxes']
|
|
else:
|
|
user['order_line_montant_toutes_taxes'] = "0"
|
|
|
|
if ("order_line_tax" in retval.keys()):
|
|
user['order_line_tax'] = retval['order_line_tax']
|
|
else:
|
|
user['order_line_tax'] = ""
|
|
|
|
if ("order_line_tax_amount" in retval.keys()):
|
|
user['order_line_tax_amount'] = retval['order_line_tax_amount']
|
|
else:
|
|
user['order_line_tax_amount'] = "0"
|
|
|
|
if ("order_line_type_reduction" in retval.keys()):
|
|
user['order_line_type_reduction'] = retval['order_line_type_reduction']
|
|
else:
|
|
user['order_line_montant_reduction'] = ""
|
|
|
|
if ("order_line_type_valeur" in retval.keys()):
|
|
user['order_line_type_valeur'] = retval['order_line_type_valeur']
|
|
else:
|
|
user['order_line_type_valeur'] = "0"
|
|
|
|
if ("order_line_montant_hors_taxes" in retval.keys()):
|
|
user['order_line_montant_hors_taxes'] = retval['order_line_montant_hors_taxes']
|
|
else:
|
|
user['order_line_montant_hors_taxes'] = "0"
|
|
|
|
user['date_update'] = retval['date_update']
|
|
user['update_by'] = str(my_partner['_id'])
|
|
user['partner_owner_recid'] = retval['partner_owner_recid']
|
|
user['invoice_line_type'] = retval['invoice_line_type']
|
|
|
|
if ("order_line_comment" in retval.keys()):
|
|
user['order_line_comment'] = retval['order_line_comment']
|
|
else:
|
|
user['order_line_comment'] = ""
|
|
|
|
user['title'] = retval['collection_partner_produit_service'][0]['nom']
|
|
user['order_line_formation_external_code'] = retval['collection_partner_produit_service'][0]['code']
|
|
|
|
user['domaine'] = ""
|
|
|
|
user['duration'] = ""
|
|
user['duration_unit'] = ""
|
|
user['duration_concat'] = ""
|
|
|
|
"""
|
|
Il s'agit d'un produit, vu qu'on a un lien avec la collection "partner_produit_service", on force alors le 'order_line_type_article'
|
|
a 'produit'
|
|
"""
|
|
user['order_line_type_article'] = "produit"
|
|
|
|
Order_header_lines_data.append(user)
|
|
|
|
if(len(Order_header_lines_data) <= 0 ):
|
|
mycommon.myprint(str(inspect.stack()[0][
|
|
3]) + " - Aucune ligne de détail pour cette facture ")
|
|
return False, " Aucune ligne de détail pour cette facture "
|
|
|
|
# Sauvegarde des fichiers joints depuis le front
|
|
tab_saved_file_full_path = []
|
|
for file in tab_files:
|
|
status, saved_file_full_path = mycommon.Upload_Save_PDF_IMG_File(file, Folder)
|
|
if (status is False):
|
|
return status, saved_file_full_path
|
|
|
|
tab_saved_file_full_path.append(saved_file_full_path)
|
|
|
|
|
|
# Traitement de l'eventuel fichier joint
|
|
tab_files_to_attache_to_mail = []
|
|
for saved_file in tab_saved_file_full_path:
|
|
file_to_attache_to_mail = MIMEBase('application', "octet-stream")
|
|
file_to_attache_to_mail.set_payload(open(saved_file, "rb").read())
|
|
|
|
encoders.encode_base64(file_to_attache_to_mail)
|
|
file_to_attache_to_mail.add_header('Content-Disposition',
|
|
'attachment; filename="{0}"'.format(os.path.basename(saved_file)))
|
|
|
|
new_node = {"attached_file": file_to_attache_to_mail}
|
|
tab_files_to_attache_to_mail.append(new_node)
|
|
|
|
print(" ## tab_files_to_attache_to_mail 01 = ", tab_files_to_attache_to_mail)
|
|
|
|
|
|
|
|
# Recuperation des données du partenaire associé à l'utilisateur connecté
|
|
local_status, local_retval = mycommon.Get_Connected_User_Partner_Data_From_RecID(my_partner['recid'])
|
|
if (local_status is False):
|
|
return local_status, local_retval
|
|
|
|
|
|
local_company_data = local_retval
|
|
|
|
|
|
|
|
"""
|
|
Recuperation du dictionnaire des info
|
|
"""
|
|
tab_client = []
|
|
tab_client.append(ObjectId(str(Order_header_client_data['_id'])))
|
|
|
|
new_diction = {}
|
|
new_diction['token'] = diction['token']
|
|
new_diction['list_stagiaire_id'] = []
|
|
new_diction['list_session_id'] = []
|
|
new_diction['list_class_id'] = []
|
|
new_diction['list_client_id'] = tab_client
|
|
new_diction['list_apprenant_id'] = []
|
|
|
|
local_status, local_retval = mycommon.Get_Dictionnary_data_For_Template(new_diction)
|
|
|
|
if (local_status is False):
|
|
return local_status, local_retval
|
|
|
|
convention_dictionnary_data = local_retval
|
|
company_data = {
|
|
"params": convention_dictionnary_data,
|
|
}
|
|
|
|
body = {
|
|
"params": convention_dictionnary_data,
|
|
}
|
|
|
|
|
|
convention_dictionnary_data['order_header'] = Order_header_data
|
|
convention_dictionnary_data['order_lines'] = Order_header_lines_data
|
|
|
|
#print(" ############ convention_dictionnary_data = ", convention_dictionnary_data)
|
|
|
|
# Recuperation de la version PDF de la facture déjà enregistrée
|
|
Invoice_header_data = MYSY_GV.dbname['partner_invoice_header'].find_one(
|
|
{'_id': ObjectId(str(diction['invoice_id'])),
|
|
'valide': '1', 'locked': '0',
|
|
'partner_owner_recid': str(
|
|
my_partner['recid'])})
|
|
|
|
# Verifier s'il s'agit d'un document à envoyer avec une version de pièce jointe.
|
|
if ("joint_pdf" in partner_document_INVOICE_data.keys() and str(
|
|
partner_document_INVOICE_data['joint_pdf']) == "1"):
|
|
|
|
# Si on a un document sécurisé associé, alors on va le chercher, si non on le crée à la volée
|
|
if ("e_document_signe_id" in Invoice_header_data.keys() and Invoice_header_data['e_document_signe_id']):
|
|
# On retourne la e_Invoice securisée
|
|
|
|
print(" #### RECUPERATION DU FICHIER SECURISE ")
|
|
qry = {'_id': ObjectId(str(Invoice_header_data['e_document_signe_id'])),
|
|
'valide': '1',
|
|
'locked': '0',
|
|
'statut': '1',
|
|
'related_collection': 'partner_invoice_header',
|
|
'related_collection_id': str(Invoice_header_data['_id']),
|
|
'partner_owner_recid': str(my_partner['recid'])}
|
|
|
|
|
|
e_Invoice_Secure_Data = MYSY_GV.dbname['e_document_signe'].find_one(
|
|
{'_id': ObjectId(str(Invoice_header_data['e_document_signe_id'])),
|
|
'valide': '1',
|
|
'locked': '0',
|
|
'statut': '1',
|
|
'related_collection': 'partner_invoice_header',
|
|
'related_collection_id': str(Order_header_data['_id']),
|
|
'partner_owner_recid': str(my_partner['recid'])})
|
|
|
|
if (e_Invoice_Secure_Data is None):
|
|
mycommon.myprint(str(inspect.stack()[0][
|
|
3]) + " Le document sécurisé associé à la facture est invalide 'e_document_signe_id' = " + str(
|
|
Order_header_data['e_document_signe_id']))
|
|
|
|
return False, " Le document sécurisé associé à la facture est invalide "
|
|
|
|
orig_file_name = "Partner_Invoice_" + str(Invoice_header_data['invoice_header_ref_interne']) + ".pdf"
|
|
outputFilename = str(MYSY_GV.TEMPORARY_DIRECTORY) + "/" + str(orig_file_name)
|
|
|
|
# open output file for writing (truncated binary)
|
|
with open(outputFilename, 'w+b') as resultFile:
|
|
encoded = base64.b64encode(e_Invoice_Secure_Data['document_data_signed'])
|
|
decode_data = e_Invoice_Secure_Data['document_data_signed'].decode()
|
|
bytes = base64.b64decode(decode_data, validate=True)
|
|
resultFile.write(bytes)
|
|
|
|
# close output file
|
|
resultFile.close()
|
|
|
|
# Attachement du fichier joint
|
|
file_to_attache_to_mail = MIMEBase('application', "octet-stream")
|
|
file_to_attache_to_mail.set_payload(open(outputFilename, "rb").read())
|
|
|
|
encoders.encode_base64(file_to_attache_to_mail)
|
|
file_to_attache_to_mail.add_header('Content-Disposition',
|
|
'attachment; filename="{0}"'.format(
|
|
os.path.basename(outputFilename)))
|
|
|
|
new_node = {"attached_file": file_to_attache_to_mail}
|
|
tab_files_to_attache_to_mail.append(new_node)
|
|
|
|
|
|
|
|
|
|
else:
|
|
# Il n'y pas de fichier joint sécurisé, il faut donc en créer un à la volée
|
|
|
|
"""
|
|
1 - Creation du PDF
|
|
"""
|
|
contenu_doc_Template = jinja2.Template(str(partner_document_INVOICE_data['contenu_doc']))
|
|
|
|
contenuHtml = contenu_doc_Template.render(params=body["params"])
|
|
|
|
todays_date = str(date.today().strftime("%d/%m/%Y"))
|
|
ts = datetime.now().timestamp()
|
|
ts = str(ts).replace(".", "").replace(",", "")[-5:]
|
|
|
|
orig_file_name = str(Invoice_header_data['invoice_header_type']) + "_" + str(my_partner['recid'])[
|
|
0:5] + "_" + str(ts) + ".pdf"
|
|
outputFilename = str(MYSY_GV.TEMPORARY_DIRECTORY) + "/" + str(orig_file_name)
|
|
|
|
# open output file for writing (truncated binary)
|
|
resultFile = open(outputFilename, "w+b")
|
|
|
|
# convert HTML to PDF
|
|
pisaStatus = pisa.CreatePDF(
|
|
src=contenuHtml, # the HTML to convert
|
|
dest=resultFile) # file handle to receive result
|
|
|
|
# close output file
|
|
resultFile.close()
|
|
|
|
# Attachement du fichier joint
|
|
file_to_attache_to_mail = MIMEBase('application', "octet-stream")
|
|
file_to_attache_to_mail.set_payload(open(outputFilename, "rb").read())
|
|
|
|
encoders.encode_base64(file_to_attache_to_mail)
|
|
file_to_attache_to_mail.add_header('Content-Disposition',
|
|
'attachment; filename="{0}"'.format(os.path.basename(outputFilename)))
|
|
|
|
new_node = {"attached_file": file_to_attache_to_mail}
|
|
tab_files_to_attache_to_mail.append(new_node)
|
|
|
|
|
|
|
|
corps_mail_doc_Template = jinja2.Template(str(partner_document_INVOICE_data['corps_mail']))
|
|
sujet_doc_Template_subject = jinja2.Template(str(partner_document_INVOICE_data['sujet']))
|
|
|
|
|
|
sourceHtml = corps_mail_doc_Template.render(params=body['params'], )
|
|
sujetHtml = sujet_doc_Template_subject.render(params=body['params'], )
|
|
|
|
|
|
html_mime = MIMEText(sourceHtml, 'html')
|
|
|
|
else:
|
|
mycommon.myprint(str(inspect.stack()[0][
|
|
3]) + " Le paramétrage du modèle de courrier 'PART_ORDER' ne comporte pas de 'joint_pdf' ")
|
|
return False, " Le paramétrage du modèle de courrier 'PART_ORDER' ne comporte pas de 'joint_pdf'. Merci d'activier les pièces jointes dans le paramétrage "
|
|
|
|
|
|
""" ORIG "" "
|
|
#print(" #### Order_header_data = ", Order_header_data)
|
|
#sourceHtml = contenu_doc_Template.render(params=Order_header_data, param_order_lines=Order_header_lines_data, company_data=company_data)
|
|
|
|
contenu_doc_Template = jinja2.Template(str(partner_document_INVOICE_data['contenu_doc']))
|
|
#contenuHtml = contenu_doc_Template.render(params_order_header=Order_header_data,params_order_lines=Order_header_lines_data, params=company_data['params'])
|
|
contenuHtml = contenu_doc_Template.render(params=company_data['params'])
|
|
|
|
|
|
contenu_doc_Template_subject = jinja2.Template(str(partner_document_INVOICE_data['sujet']))
|
|
#sujetHtml = contenu_doc_Template_subject.render(params_order_header=Order_header_data)
|
|
sujetHtml = contenu_doc_Template_subject.render(params=company_data['params'])
|
|
|
|
#print(" #### sourceHtml = ", sourceHtml)
|
|
|
|
"" " FIN ORIG """
|
|
|
|
"""
|
|
Recuperation des parametre SMTP du partner si le client a decidé d'utiliser son propre smpt
|
|
"""
|
|
partner_own_smtp_value = "0"
|
|
partner_own_smtp = MYSY_GV.dbname['base_partner_setup'].find_one(
|
|
{'partner_owner_recid': str(my_partner['recid']),
|
|
'config_name': 'partner_smtp',
|
|
'valide': '1',
|
|
'locked': '0'})
|
|
|
|
if( partner_own_smtp and "config_value" in partner_own_smtp.keys()):
|
|
partner_own_smtp_value = partner_own_smtp['config_value']
|
|
|
|
if( str(partner_own_smtp_value) == "1"):
|
|
partner_SMTP_COUNT_password = str(MYSY_GV.dbname['base_partner_setup'].find_one(
|
|
{'partner_owner_recid': str(my_partner['recid']),
|
|
'config_name': 'smtp_user_pwd',
|
|
'valide': '1',
|
|
'locked': '0'}, {'config_value':1} )['config_value'])
|
|
|
|
|
|
|
|
partner_SMTP_COUNT_smtpsrv = str(MYSY_GV.dbname['base_partner_setup'].find_one(
|
|
{'partner_owner_recid': str(my_partner['recid']),
|
|
'config_name': 'smtp_server',
|
|
'valide': '1',
|
|
'locked': '0'}, {'config_value':1})['config_value'])
|
|
|
|
partner_SMTP_COUNT_user = str(MYSY_GV.dbname['base_partner_setup'].find_one(
|
|
{'partner_owner_recid': str(my_partner['recid']),
|
|
'config_name': 'smtp_user',
|
|
'valide': '1',
|
|
'locked': '0'}, {'config_value':1})['config_value'])
|
|
|
|
partner_SMTP_COUNT_From_User = str(MYSY_GV.dbname['base_partner_setup'].find_one(
|
|
{'partner_owner_recid': str(my_partner['recid']),
|
|
'config_name': 'smtp_count_from_name',
|
|
'valide': '1',
|
|
'locked': '0'}, {'config_value':1})['config_value'])
|
|
|
|
partner_SMTP_COUNT_port = str(MYSY_GV.dbname['base_partner_setup'].find_one(
|
|
{'partner_owner_recid': str(my_partner['recid']),
|
|
'config_name': 'smtp_count_port',
|
|
'valide': '1',
|
|
'locked': '0'}, {'config_value':1})['config_value'])
|
|
|
|
|
|
if (str(partner_own_smtp_value) == "1"):
|
|
print("debut envoi mail de test ")
|
|
# Creation de l'email à enoyer
|
|
msg = MIMEMultipart("alternative")
|
|
msg.attach(html_mime)
|
|
|
|
smtpserver = smtplib.SMTP(partner_SMTP_COUNT_smtpsrv, partner_SMTP_COUNT_port)
|
|
|
|
client_main_mail_tmp = ""
|
|
|
|
if ("order_header_email_client" in Order_header_data.keys() and Order_header_data[
|
|
'order_header_email_client']):
|
|
client_main_mail_tmp = str(Order_header_data['order_header_email_client'])
|
|
|
|
if (mycommon.isEmailValide(client_main_mail_tmp) is True):
|
|
msg['To'] = client_main_mail_tmp
|
|
|
|
else:
|
|
mycommon.myprint(str(inspect.stack()[0][
|
|
3]) + " - L'adresse email sur la commande n'est pas valide ")
|
|
return False, " L'adresse email sur la commande n'est pas valide "
|
|
|
|
else:
|
|
mycommon.myprint(str(inspect.stack()[0][
|
|
3]) + " - Aucune adresse email sur la commande ")
|
|
return False, " Aucune adresse email sur la commande "
|
|
|
|
msg['From'] = partner_SMTP_COUNT_From_User
|
|
msg['Bcc'] = 'contact@mysy-training.com'
|
|
msg['Subject'] = sujetHtml
|
|
|
|
# Attacher l'eventuelle pièces jointes
|
|
for myfile in tab_files_to_attache_to_mail:
|
|
msg.attach(myfile['attached_file'])
|
|
|
|
smtpserver.ehlo()
|
|
smtpserver.starttls()
|
|
smtpserver.login(partner_SMTP_COUNT_user, partner_SMTP_COUNT_password)
|
|
val = smtpserver.send_message(msg)
|
|
smtpserver.close()
|
|
print(" Email envoyé " + str(val))
|
|
|
|
|
|
else:
|
|
print("debut envoi mail de test ")
|
|
# Creation de l'email à enoyer
|
|
msg = MIMEMultipart("alternative")
|
|
|
|
msg.attach(html_mime)
|
|
smtpserver = smtplib.SMTP(MYSY_GV.O365_SMTP_COUNT_smtpsrv, MYSY_GV.O365_SMTP_COUNT_port)
|
|
|
|
client_main_mail_tmp = ""
|
|
|
|
if ("order_header_email_client" in Order_header_data.keys() and Order_header_data[
|
|
'order_header_email_client']):
|
|
client_main_mail_tmp = str(Order_header_data['order_header_email_client'])
|
|
|
|
if (mycommon.isEmailValide(client_main_mail_tmp) is True):
|
|
msg['To'] = client_main_mail_tmp
|
|
|
|
else:
|
|
mycommon.myprint(str(inspect.stack()[0][
|
|
3]) + " - L'adresse email sur la commande n'est pas valide ")
|
|
return False, " L'adresse email sur la commande n'est pas valide "
|
|
|
|
else:
|
|
mycommon.myprint(str(inspect.stack()[0][
|
|
3]) + " - Aucune adresse email sur la commande ")
|
|
return False, " Aucune adresse email sur la commande "
|
|
|
|
|
|
msg['From'] = MYSY_GV.O365_SMTP_COUNT_From_User
|
|
msg['Bcc'] = 'contact@mysy-training.com'
|
|
msg['Subject'] = sujetHtml
|
|
|
|
# Attacher l'eventuelle pièces jointes
|
|
for myfile in tab_files_to_attache_to_mail:
|
|
msg.attach(myfile['attached_file'])
|
|
|
|
smtpserver.ehlo()
|
|
smtpserver.starttls()
|
|
smtpserver.login(MYSY_GV.O365_SMTP_COUNT_user, MYSY_GV.O365_SMTP_COUNT_password)
|
|
val = smtpserver.send_message(msg)
|
|
smtpserver.close()
|
|
print(" Email envoyé " + str(val))
|
|
|
|
|
|
"""
|
|
Ajout l'action dans l'historique
|
|
"""
|
|
## Add to log history
|
|
now = str(datetime.now().strftime("%d/%m/%Y %H:%M:%S"))
|
|
history_event_dict = {}
|
|
history_event_dict['token'] = diction['token']
|
|
history_event_dict['related_collection'] = "partner_invoice_header"
|
|
history_event_dict['related_collection_recid'] = str(diction['invoice_id'])
|
|
history_event_dict['action_date'] = str(now)
|
|
history_event_dict['action_description'] = "Envoyé par email à : '"+str(client_main_mail_tmp)+"' "
|
|
local_status, local_retval = mycommon.Collection_Historique.Add_Historique_Event(history_event_dict)
|
|
if (local_status is False):
|
|
mycommon.myprint(
|
|
" WARNING : Impossible de logguer l'historique pour l'évènement : " + str(history_event_dict))
|
|
|
|
return True, "Facture a été envoyée par email à : '"+str(client_main_mail_tmp)+"' "
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
print(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno))
|
|
return False, "Impossible d'envoyer la facture par email"
|
|
|
|
|
|
|
|
"""
|
|
/!\ 26/04/2024
|
|
La fonction de creation d'une facture va computer la commande associée, puis copier les ligne
|
|
|
|
or quand on fait une facturation a partir d'une session, il n'y pas de 'commande', donc pas de computation.
|
|
|
|
Il faut donc créer une fonction de computation de la factures apres sa creation
|
|
|
|
"""
|
|
def Compute_Invoice_Order_Data(diction):
|
|
try:
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', '_id']
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list and val.startswith('my_') is False:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'existe pas")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token', '_id']
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans la liste des arguments ")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
|
|
|
|
"""
|
|
Verification de l'identité et autorisation de l'entité qui
|
|
appelle cette API
|
|
"""
|
|
token = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
token = diction['token']
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner
|
|
|
|
# Recuperation de la valeur de la TVA (taux tva)
|
|
partner_taux_tva = 20
|
|
|
|
if ("invoice_taux_vat" in my_partner.keys()):
|
|
IsInt_status, IsInt_retval = mycommon.IsInt(str(my_partner['invoice_taux_vat']))
|
|
if (IsInt_status is False):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + str(
|
|
my_partner['invoice_taux_vat']) + "' n'est pas un taux de TVA correcte ")
|
|
return False, " La valeur '" + str(
|
|
my_partner['invoice_taux_vat']) + "' n'est pas un taux de TVA correcte ",
|
|
|
|
partner_taux_tva = IsInt_retval
|
|
|
|
print(" ### COMPUTE : le taux de TVA = ", str(partner_taux_tva))
|
|
|
|
# Verification de la validité de la facture
|
|
qry = {'_id': ObjectId(str(diction['_id'])), 'valide': '1', 'locked': '0',
|
|
'partner_owner_recid': str(my_partner['recid'])}
|
|
print(" ### qry = ", qry)
|
|
|
|
is_Invoice_Existe_Count = MYSY_GV.dbname['partner_invoice_header'].count_documents(
|
|
{'_id': ObjectId(str(diction['_id'])),
|
|
'valide': '1', 'locked': '0',
|
|
'partner_owner_recid': str(my_partner['recid'])})
|
|
|
|
if (is_Invoice_Existe_Count != 1):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " L'identifiant de la facture n'est pas valide ")
|
|
return False, " L'identifiant de la facture n'est pas valide ",
|
|
|
|
Invoice_Data = MYSY_GV.dbname['partner_invoice_header'].find_one({'_id': ObjectId(str(diction['_id'])),
|
|
'valide': '1', 'locked': '0',
|
|
'partner_owner_recid':str(my_partner['recid'])})
|
|
|
|
|
|
"""
|
|
Verifier que la facture a des lignes valides, si non refuser
|
|
"""
|
|
is_Invoice_Line_Existe_Count = MYSY_GV.dbname['partner_invoice_line'].count_documents(
|
|
{'invoice_header_id': (str(diction['_id'])),
|
|
'valide': '1', 'locked': '0',
|
|
'partner_owner_recid': str(my_partner['recid'])})
|
|
|
|
if (is_Invoice_Line_Existe_Count <= 0):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " Il n'existe aucune ligne de facture pour cette facture_id : "+str(diction['_id']))
|
|
return False, " Il n'existe aucune ligne de facture pour cette facture_id : "+str(diction['_id'])
|
|
|
|
|
|
"""
|
|
Algo :
|
|
1 - récupérer toutes les lignes valides, créer des sous totaux
|
|
2 - Appliquer les eventuels reductions d'entete
|
|
"""
|
|
nb_line = 0
|
|
line_sum_invoice_line_tax_amount = 0
|
|
line_sum_invoice_line_montant_reduction = 0
|
|
line_sum_invoice_line_montant_hors_taxes_before_reduction = 0
|
|
line_sum_invoice_line_montant_hors_taxes_after_reduction = 0
|
|
line_sum_invoice_line_montant_toutes_taxes = 0
|
|
|
|
for local_retval in MYSY_GV.dbname['partner_invoice_line'].find( {'invoice_header_id': (str(diction['_id'])), 'valide': '1', 'locked': '0','partner_owner_recid': str(my_partner['recid'])}):
|
|
print(" ------------------- Pour la ligne numero : ", nb_line)
|
|
ligne_montant_reduction = 0
|
|
if ("order_line_montant_reduction" in local_retval.keys()):
|
|
line_sum_invoice_line_montant_reduction = line_sum_invoice_line_montant_reduction + mycommon.tryFloat(
|
|
local_retval['order_line_montant_reduction'])
|
|
ligne_montant_reduction = mycommon.tryFloat(local_retval['order_line_montant_reduction'])
|
|
|
|
"""
|
|
print(" #### invoice_line_montant_reduction = ",
|
|
str(mycommon.tryFloat(local_retval['order_line_montant_reduction'])))
|
|
"""
|
|
|
|
if ("order_line_tax_amount" in local_retval.keys()):
|
|
line_sum_invoice_line_tax_amount = line_sum_invoice_line_tax_amount + mycommon.tryFloat(
|
|
local_retval['order_line_tax_amount'])
|
|
#print(" #### invoice_line_tax_amount = ", str(mycommon.tryFloat(local_retval['order_line_tax_amount'])))
|
|
|
|
if ("order_line_montant_hors_taxes" in local_retval.keys()):
|
|
line_sum_invoice_line_montant_hors_taxes_before_reduction = line_sum_invoice_line_montant_hors_taxes_before_reduction + mycommon.tryFloat(
|
|
local_retval['order_line_montant_hors_taxes'])
|
|
|
|
"""
|
|
print(" #### invoice_line_montant_hors_taxes = ",
|
|
str(mycommon.tryFloat(local_retval['order_line_montant_hors_taxes'])))
|
|
"""
|
|
|
|
invoice_line_montant_hors_taxes_APRES_REDUCTION = mycommon.tryFloat(
|
|
local_retval['order_line_montant_hors_taxes']) - ligne_montant_reduction
|
|
#print(" #### invoice_line_montant_hors_taxes_APRES_REDUCTION = ", str(invoice_line_montant_hors_taxes_APRES_REDUCTION))
|
|
|
|
if ("order_line_montant_toutes_taxes" in local_retval.keys()):
|
|
line_sum_invoice_line_montant_toutes_taxes = line_sum_invoice_line_montant_toutes_taxes + mycommon.tryFloat(local_retval['order_line_montant_toutes_taxes'])
|
|
#print(" #### invoice_line_montant_toutes_taxes = ", str(mycommon.tryFloat(local_retval['order_line_montant_toutes_taxes'])))
|
|
|
|
print(" ----------- FIN DES LIGNES ")
|
|
|
|
nb_line = nb_line + 1
|
|
|
|
line_sum_invoice_line_montant_hors_taxes_after_reduction = line_sum_invoice_line_montant_hors_taxes_before_reduction - line_sum_invoice_line_montant_reduction
|
|
"""print(" ###### Apres compute des lignes : NB_LINE = ", nb_line)
|
|
print(" ###### line_sum_order_line_montant_reduction = ", line_sum_order_line_montant_reduction)
|
|
print(" ###### line_sum_order_line_tax_amount = ", line_sum_order_line_tax_amount)
|
|
print(" ###### line_sum_order_line_montant_hors_taxes_before_reduction = ", line_sum_order_line_montant_hors_taxes_before_reduction)
|
|
print(" ###### line_sum_order_line_montant_hors_taxes_after_reduction = ", line_sum_order_line_montant_hors_taxes_after_reduction)
|
|
print(" ###### line_sum_order_line_montant_toutes_taxes = ", line_sum_order_line_montant_toutes_taxes)
|
|
"""
|
|
|
|
header_reduction_type = ""
|
|
header_reduction_type_value = ""
|
|
if ("order_header_type_reduction" in Invoice_Data.keys()):
|
|
header_reduction_type = Invoice_Data['order_header_type_reduction']
|
|
|
|
if ("order_header_type_reduction_valeur" in Invoice_Data.keys()):
|
|
header_reduction_type_value = Invoice_Data['order_header_type_reduction_valeur']
|
|
|
|
"""print(" ### les reduction d'entete ")
|
|
print(" ###### header_reduction_type = ", header_reduction_type)
|
|
print(" ###### header_reduction_type_value = ", header_reduction_type_value)
|
|
"""
|
|
|
|
global_invoice_taxe_amount = 0
|
|
global_invoice_amount_ht_before_header_reduction = line_sum_invoice_line_montant_hors_taxes_after_reduction
|
|
global_invoice_amount_ht_after_header_reduction = 0
|
|
header_reduction_type_value_total_amount = 0
|
|
|
|
if (str(header_reduction_type) == "fixe"):
|
|
header_reduction_type_value_total_amount = mycommon.tryFloat(header_reduction_type_value)
|
|
global_order_amount_ht_after_header_reduction = line_sum_invoice_line_montant_hors_taxes_after_reduction - mycommon.tryFloat(
|
|
header_reduction_type_value)
|
|
|
|
"""print(" GRRRR 022 header_reduction_type_value_total_amount = ",
|
|
|
|
header_reduction_type_value_total_amount)
|
|
|
|
print(" GRRRR 022 global_order_amount_ht_after_header_reduction = ",
|
|
global_order_amount_ht_after_header_reduction)
|
|
"""
|
|
|
|
elif (str(header_reduction_type) == "percent"):
|
|
|
|
"""
|
|
print(" GRRRR line_sum_order_line_montant_hors_taxes_after_reduction = ",
|
|
line_sum_invoice_line_montant_hors_taxes_after_reduction)
|
|
print(" GRRRR mycommon.tryFloat(header_reduction_type_value)/100 = ",
|
|
line_sum_invoice_line_montant_hors_taxes_after_reduction * mycommon.tryFloat(
|
|
header_reduction_type_value) / 100)
|
|
print(" GRRRR mycommon.tryFloat(header_reduction_type_value)/100 = ",
|
|
(line_sum_invoice_line_montant_hors_taxes_after_reduction - (
|
|
line_sum_invoice_line_montant_hors_taxes_after_reduction * mycommon.tryFloat(
|
|
header_reduction_type_value) / 100)))
|
|
"""
|
|
header_reduction_type_value_total_amount = line_sum_invoice_line_montant_hors_taxes_after_reduction * mycommon.tryFloat(
|
|
header_reduction_type_value) / 100
|
|
global_order_amount_ht_after_header_reduction = line_sum_invoice_line_montant_hors_taxes_after_reduction - ((
|
|
line_sum_invoice_line_montant_hors_taxes_after_reduction * mycommon.tryFloat(
|
|
header_reduction_type_value) / 100))
|
|
|
|
"""
|
|
print(" GRRRR global_order_amount_ht_after_header_reduction = ",
|
|
global_order_amount_ht_after_header_reduction)
|
|
"""
|
|
else:
|
|
header_reduction_type_value_total_amount = 0
|
|
global_order_amount_ht_before_header_reduction = line_sum_invoice_line_montant_hors_taxes_after_reduction
|
|
global_order_amount_ht_after_header_reduction = line_sum_invoice_line_montant_hors_taxes_after_reduction
|
|
|
|
global_order_amount_ttc = global_order_amount_ht_after_header_reduction + line_sum_invoice_line_tax_amount
|
|
|
|
"""print(" ###### header_reduction_type_value_total_amount = ", header_reduction_type_value_total_amount)
|
|
print(" ###### global_order_amount_ht_before_header_reduction = ", line_sum_order_line_montant_hors_taxes_after_reduction)
|
|
print(" ###### global_order_amount_ht_after_header_reduction = ", global_order_amount_ht_after_header_reduction)
|
|
|
|
print(" ###### global_order_amount_ttc = ", global_order_amount_ttc)
|
|
"""
|
|
"""
|
|
Tous les calculs, ok, on met à l'entete de de l'order
|
|
"""
|
|
header_computed_data = {}
|
|
header_computed_data['total_lines_montant_reduction'] = str(round(line_sum_invoice_line_montant_reduction, 3))
|
|
header_computed_data['total_lines_hors_taxe_before_lines_reduction'] = str(
|
|
round(line_sum_invoice_line_montant_hors_taxes_before_reduction, 3))
|
|
header_computed_data['total_lines_hors_taxe_after_lines_reduction'] = str(
|
|
round(line_sum_invoice_line_montant_hors_taxes_after_reduction, 3))
|
|
header_computed_data['order_header_montant_reduction'] = str(round(header_reduction_type_value_total_amount, 3))
|
|
header_computed_data['total_header_hors_taxe_before_header_reduction'] = str(
|
|
round(line_sum_invoice_line_montant_hors_taxes_after_reduction, 3))
|
|
header_computed_data['total_header_hors_taxe_after_header_reduction'] = str(
|
|
round(global_order_amount_ht_after_header_reduction, 3))
|
|
header_computed_data['order_header_tax'] = "TVA " + str(partner_taux_tva) + "%"
|
|
header_computed_data['order_header_tax_amount'] = str(round(line_sum_invoice_line_tax_amount, 3))
|
|
header_computed_data['total_header_toutes_taxes'] = str(round(global_order_amount_ttc, 3))
|
|
|
|
header_computed_data['date_update'] = str(datetime.now())
|
|
header_computed_data['update_by'] = str(my_partner['recid'])
|
|
|
|
|
|
print(" ### header_computed_data = ", header_computed_data)
|
|
|
|
local_retval = MYSY_GV.dbname['partner_invoice_header'].find_one_and_update({'_id': ObjectId(str(diction['_id'])),
|
|
'valide': '1', 'locked': '0',
|
|
'partner_owner_recid': str(
|
|
my_partner['recid'])},
|
|
{"$set": header_computed_data
|
|
},
|
|
upsert=False,
|
|
return_document=ReturnDocument.AFTER
|
|
)
|
|
|
|
if (local_retval is None):
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " Impossible de finaliser la mise à jour")
|
|
return False, " Impossible de finaliser la mise à jour "
|
|
|
|
|
|
return True, 'La facture a été correctement recalculée.'
|
|
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de recalculer les totaux de la facture "
|
|
|
|
|
|
"""
|
|
Cette fonction permets ajouter / mettre à jour une annotation
|
|
sur une facture
|
|
"""
|
|
def Add_Update_Invoice_Annotation(diction):
|
|
try:
|
|
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', '_id', 'annotation']
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list and val.startswith('my_') is False:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'existe pas")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token', '_id', 'annotation']
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans la liste des arguments ")
|
|
return False, " Les informations fournies sont incorrectes",
|
|
|
|
"""
|
|
Verification de l'identité et autorisation de l'entité qui
|
|
appelle cette API
|
|
"""
|
|
token = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
token = diction['token']
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner
|
|
|
|
|
|
"""
|
|
Verifier que la factre est valide
|
|
"""
|
|
is_valide_invoice_cout = MYSY_GV.dbname['partner_invoice_header'].count_documents({'_id':ObjectId(str(diction['_id'])),
|
|
'partner_owner_recid':my_partner['recid'],
|
|
'valide':'1',
|
|
'locked':'0'})
|
|
|
|
if( is_valide_invoice_cout <= 0 ):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " L'identifiant de la facture est invalide ")
|
|
return False, " L'identifiant de la facture est invalide ",
|
|
|
|
update_data = {}
|
|
update_data['date_update'] = str(datetime.now())
|
|
update_data['update_by'] = str(my_partner['_id'])
|
|
update_data['partner_owner_recid'] = str(my_partner['recid'])
|
|
|
|
result = MYSY_GV.dbname['partner_invoice_header'].find_one_and_update(
|
|
{'_id': ObjectId(str(diction['_id'])),
|
|
'valide': '1',
|
|
'locked': '0',
|
|
'partner_owner_recid': str(my_partner['recid'])},
|
|
{"$set": {"annotation":str(diction['annotation'])}},
|
|
upsert=False,
|
|
return_document=ReturnDocument.AFTER
|
|
)
|
|
|
|
"""
|
|
Ajout l'action dans l'historique
|
|
"""
|
|
## Add to log history
|
|
now = str(datetime.now().strftime("%d/%m/%Y %H:%M:%S"))
|
|
history_event_dict = {}
|
|
history_event_dict['token'] = diction['token']
|
|
history_event_dict['related_collection'] = "partner_invoice_header"
|
|
history_event_dict['related_collection_recid'] = str(diction['_id'])
|
|
history_event_dict['action_date'] = str(now)
|
|
history_event_dict['action_description'] = "Annotation Mise à jour "
|
|
local_status, local_retval = mycommon.Collection_Historique.Add_Historique_Event(history_event_dict)
|
|
if (local_status is False):
|
|
mycommon.myprint(
|
|
" WARNING : Impossible de logguer l'historique pour l'évènement : " + str(history_event_dict))
|
|
|
|
|
|
return True, 'La note a été mise à jour'
|
|
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de mettre à jour la note"
|
|
|
|
|
|
"""
|
|
Cette fontion permet de créer un avoir sur la facture.
|
|
On par d'annulation totale, donc pas partiel
|
|
|
|
/!\ : On créer une souche spéciale pour les avoirs
|
|
|
|
apres la creation de l'avoir, on va ajouter sur la facture
|
|
la reference de l'avoir associé.
|
|
|
|
A l'annulation de la facture, si la facture est liée à un inscrit
|
|
sur une session, on aller remettre la session à 'non facture'
|
|
|
|
"""
|
|
def Create_Invoice_Avoir_Total(diction):
|
|
try:
|
|
|
|
diction = mycommon.strip_dictionary(diction)
|
|
|
|
"""
|
|
Verification des input acceptés
|
|
"""
|
|
field_list = ['token', '_id',]
|
|
|
|
incom_keys = diction.keys()
|
|
for val in incom_keys:
|
|
if val not in field_list and val.startswith('my_') is False:
|
|
mycommon.myprint(str(
|
|
inspect.stack()[0][3]) + " Le champ '" + val + "' n'existe pas")
|
|
return False, " Les informations fournies sont incorrectes", False
|
|
|
|
"""
|
|
Verification des champs obligatoires
|
|
"""
|
|
field_list_obligatoire = ['token', '_id']
|
|
for val in field_list_obligatoire:
|
|
if val not in diction:
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " - La valeur '" + val + "' n'est pas presente dans la liste des arguments ")
|
|
return False, " Les informations fournies sont incorrectes", False
|
|
|
|
"""
|
|
Verification de l'identité et autorisation de l'entité qui
|
|
appelle cette API
|
|
"""
|
|
token = ""
|
|
if ("token" in diction.keys()):
|
|
if diction['token']:
|
|
token = diction['token']
|
|
|
|
local_status, my_partner = mycommon.Check_Connexion_And_Return_Partner_Data(diction)
|
|
if (local_status is not True):
|
|
return local_status, my_partner, False
|
|
|
|
|
|
"""
|
|
Verifier que la facture est valide et s'assurer qu'il n'y pas un avoir (on est en mode avoir TOTAL).
|
|
Colonne : 'credit_note_ref'
|
|
"""
|
|
is_valide_invoice_cout = MYSY_GV.dbname['partner_invoice_header'].count_documents({'_id':ObjectId(str(diction['_id'])),
|
|
'partner_owner_recid':my_partner['recid'],
|
|
'valide':'1',
|
|
'locked':'0',
|
|
'credit_note_ref': {'$exists': False},
|
|
})
|
|
|
|
if( is_valide_invoice_cout <= 0 ):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " L'identifiant de la facture est invalide ")
|
|
return False, " L'identifiant de la facture est invalide Ou le document a déjà une avoir ", False
|
|
|
|
|
|
|
|
# Récuperation de la sequence de l'objet "partner_credit_note" dans la collection : "mysy_sequence"
|
|
retval_sequence_credit_note = MYSY_GV.dbname['mysy_sequence'].find_one(
|
|
{'related_mysy_object': 'partner_credit_note',
|
|
'valide': '1', 'partner_owner_recid': str(
|
|
my_partner['recid'])})
|
|
|
|
if (retval_sequence_credit_note is None):
|
|
# Il n'y pas de sequence pour le partenaire, on va aller chercher la sequence par defaut
|
|
retval_sequence_credit_note = MYSY_GV.dbname['mysy_sequence'].find_one(
|
|
{'related_mysy_object': 'partner_credit_note',
|
|
'valide': '1', 'partner_owner_recid': 'default'})
|
|
|
|
if (retval_sequence_credit_note is None or "current_val" not in retval_sequence_credit_note.keys()):
|
|
# Il n'y aucune sequence meme par defaut.
|
|
|
|
|
|
mycommon.myprint(" Aucune sequence de type 'partner_credit_note' n'est configurée dans le système ")
|
|
return False, " Aucune sequence de type 'partner_credit_note' n'est configurée dans le système ", False
|
|
|
|
current_seq_value = str(retval_sequence_credit_note['current_val'])
|
|
new_sequence_value = int(mycommon.tryInt(current_seq_value)) + 1
|
|
new_sequance_data_to_update = {'current_val': new_sequence_value}
|
|
|
|
ret_val2 = MYSY_GV.dbname['mysy_sequence'].find_one_and_update(
|
|
{'_id': ObjectId(str(retval_sequence_credit_note['_id'])), 'valide': '1'},
|
|
{"$set": new_sequance_data_to_update},
|
|
return_document=ReturnDocument.AFTER,
|
|
upsert=False,
|
|
)
|
|
|
|
credit_notes_reference = retval_sequence_credit_note['prefixe'] + str(current_seq_value)
|
|
invoice_date_time = str(datetime.now().strftime("%d/%m/%Y"))
|
|
|
|
local_credit_note = None
|
|
liste_credit_node = []
|
|
|
|
for local_invoice in MYSY_GV.dbname['partner_invoice_header'].find({'_id':ObjectId(str(diction['_id'])),
|
|
'partner_owner_recid':my_partner['recid'],
|
|
'valide':'1',
|
|
'locked':'0'}):
|
|
|
|
local_credit_note = local_invoice
|
|
|
|
list_champs_a_inverser = ['total_header_hors_taxe_before_header_reduction', 'total_header_toutes_taxes',
|
|
'total_lines_hors_taxe_after_lines_reduction', 'total_lines_hors_taxe_before_lines_reduction', 'total_lines_montant_reduction']
|
|
|
|
for tmp in list_champs_a_inverser :
|
|
if( tmp in local_credit_note.keys() and local_credit_note[tmp] ):
|
|
inversed_tmp = mycommon.tryFloat(str(local_credit_note[tmp]).strip()) * (-1)
|
|
local_credit_note[tmp] = inversed_tmp
|
|
|
|
local_credit_note['invoice_header_origin'] = local_invoice['invoice_header_ref_interne']
|
|
local_credit_note['invoice_header_ref_interne'] = credit_notes_reference
|
|
local_credit_note['invoice_header_type'] = "avoir"
|
|
local_credit_note['invoice_date'] = invoice_date_time
|
|
local_credit_note['date_update'] = str(datetime.now())
|
|
local_credit_note['update_by'] = str(my_partner['_id'])
|
|
local_credit_note['partner_owner_recid'] = str(my_partner['recid'])
|
|
|
|
if( "_id" in local_credit_note.keys() ):
|
|
del local_credit_note['_id']
|
|
|
|
if( "e_document_signe_id" in local_credit_note.keys()):
|
|
del local_credit_note['e_document_signe_id']
|
|
|
|
"""
|
|
Insertion de l'avoir
|
|
"""
|
|
#print(" ### creadit note = ", local_credit_note)
|
|
|
|
"""
|
|
Verifier qu'il n'y pas dans la base un document avec le meme refrence
|
|
pour ce partenaire
|
|
"""
|
|
is_document_exist_count = MYSY_GV.dbname['partner_invoice_header'].count_documents({"invoice_header_ref_interne":credit_notes_reference,
|
|
"partner_owner_recid":my_partner['recid']})
|
|
if( is_document_exist_count > 0 ):
|
|
mycommon.myprint(
|
|
str(inspect.stack()[0][3]) + " Il existe déjà un document avec la même reference : "+str(credit_notes_reference))
|
|
return False, " Il existe déjà un document avec la même reference : "+str(credit_notes_reference), False
|
|
|
|
inserted_id = MYSY_GV.dbname['partner_invoice_header'].insert_one(local_credit_note).inserted_id
|
|
if (not inserted_id):
|
|
mycommon.myprint(
|
|
" Impossible de créer l'avoir (2) ")
|
|
return False, "Impossible de créer l'avoir (2) "
|
|
|
|
liste_credit_node.append(credit_notes_reference)
|
|
|
|
invoice_header_inserted_id = inserted_id
|
|
|
|
"""
|
|
Mettre à jour la facture avec la reference de l'avoir
|
|
"""
|
|
result = MYSY_GV.dbname['partner_invoice_header'].find_one_and_update(
|
|
{'_id': ObjectId(str(diction['_id'])),
|
|
'partner_owner_recid': my_partner['recid'],
|
|
'valide': '1',
|
|
'locked': '0'},
|
|
{"$set": {"credit_note_ref":credit_notes_reference}},
|
|
upsert=False,
|
|
return_document=ReturnDocument.AFTER
|
|
)
|
|
|
|
|
|
|
|
"""
|
|
On a finit de créer l'entete, on va aller créer les lignes, partner_invoice_line
|
|
"""
|
|
for local_invoice_line in MYSY_GV.dbname['partner_invoice_line'].find({'invoice_header_id': str(diction['_id']),
|
|
'partner_owner_recid': my_partner['recid'],
|
|
'valide': '1',
|
|
'locked': '0'}):
|
|
|
|
local_credit_note_line = local_invoice_line
|
|
|
|
list_champs_a_inverser = ['order_line_qty', 'order_line_tax_amount',
|
|
'order_line_montant_toutes_taxes',
|
|
'order_line_montant_hors_taxes',
|
|
]
|
|
|
|
for tmp in list_champs_a_inverser:
|
|
if (tmp in local_credit_note_line.keys() and local_credit_note_line[tmp]):
|
|
inversed_tmp = mycommon.tryFloat(str(local_credit_note_line[tmp]).strip()) * (-1)
|
|
local_credit_note_line[tmp] = str(inversed_tmp)
|
|
|
|
local_credit_note_line['invoice_header_ref_interne_origin'] = local_invoice['invoice_header_ref_interne']
|
|
local_credit_note_line['invoice_header_ref_interne'] = credit_notes_reference
|
|
|
|
local_credit_note_line['invoice_header_id'] = str(invoice_header_inserted_id)
|
|
local_credit_note_line['invoice_line_type'] = "avoir"
|
|
|
|
local_credit_note_line['invoice_date'] = invoice_date_time
|
|
local_credit_note_line['date_update'] = str(datetime.now())
|
|
local_credit_note_line['update_by'] = str(my_partner['_id'])
|
|
local_credit_note_line['partner_owner_recid'] = str(my_partner['recid'])
|
|
|
|
if ("_id" in local_credit_note_line.keys()):
|
|
del local_credit_note_line['_id']
|
|
|
|
inserted_id = MYSY_GV.dbname['partner_invoice_line'].insert_one(local_credit_note_line).inserted_id
|
|
if (not inserted_id):
|
|
mycommon.myprint( " WARNING : Impossible d'inserer la ligne d'avoir pour l'avoir "+str(credit_notes_reference))
|
|
|
|
"""
|
|
On a finit de créer l'entete, on va aller créer les lignes: partner_invoice_line_detail
|
|
"""
|
|
for local_invoice_line_detail in MYSY_GV.dbname['partner_invoice_line_detail'].find({'invoice_header_id': str(diction['_id']),
|
|
'partner_owner_recid': my_partner[
|
|
'recid'],
|
|
'valide': '1',
|
|
'locked': '0'}):
|
|
|
|
local_credit_note_line_detail = local_invoice_line_detail
|
|
|
|
list_champs_a_inverser = ['order_line_qty', 'order_line_montant_hors_taxes',
|
|
'order_line_invoiced_amount',
|
|
]
|
|
|
|
for tmp in list_champs_a_inverser:
|
|
if (tmp in local_credit_note_line_detail.keys() and local_credit_note_line_detail[tmp]):
|
|
inversed_tmp = mycommon.tryFloat(str(local_credit_note_line_detail[tmp]).strip()) * (-1)
|
|
local_credit_note_line_detail[tmp] = str(inversed_tmp)
|
|
|
|
local_credit_note_line_detail['invoice_header_ref_interne_origin'] = local_invoice['invoice_header_ref_interne']
|
|
local_credit_note_line_detail['invoice_header_ref_interne'] = credit_notes_reference
|
|
|
|
local_credit_note_line_detail['invoice_header_id'] = str(invoice_header_inserted_id)
|
|
local_credit_note_line_detail['invoice_line_type'] = "avoir"
|
|
|
|
local_credit_note_line_detail['invoice_date'] = invoice_date_time
|
|
local_credit_note_line_detail['date_update'] = str(datetime.now())
|
|
local_credit_note_line_detail['update_by'] = str(my_partner['_id'])
|
|
local_credit_note_line_detail['partner_owner_recid'] = str(my_partner['recid'])
|
|
|
|
if ("_id" in local_credit_note_line_detail.keys()):
|
|
del local_credit_note_line_detail['_id']
|
|
|
|
inserted_id = MYSY_GV.dbname['partner_invoice_line_detail'].insert_one(local_credit_note_line_detail).inserted_id
|
|
if (not inserted_id):
|
|
mycommon.myprint(
|
|
" WARNING : Impossible d'inserer la ligne d'avoir pour l'avoir " + str(credit_notes_reference))
|
|
|
|
|
|
"""
|
|
Apres avoir créer l'avoir, il faut aller voir s'il la facture est liée à une inscription
|
|
si c'est le cas, on remettre le statut de ligne d'inscription à non-facture (invoiced).
|
|
mais on laisse volontaire la ref de facture, on l'efface pas la ref de la facture.
|
|
on va juste ajouter l'avoir
|
|
"""
|
|
is_valide_invoice_data = MYSY_GV.dbname['partner_invoice_header'].find_one({'_id':ObjectId(str(diction['_id'])),
|
|
'partner_owner_recid':my_partner['recid'],
|
|
'valide':'1',
|
|
'locked':'0', }, {'invoice_header_ref_interne':1})
|
|
|
|
|
|
|
|
inscription_data = MYSY_GV.dbname['inscription'].find_one({'invoiced_ref': {'$regex': str(is_valide_invoice_data['invoice_header_ref_interne'])},
|
|
'valide':'1',
|
|
'partner_owner_recid':str(my_partner['recid'])})
|
|
|
|
if(inscription_data ):
|
|
current_invoiced_ref = ""
|
|
local_invoiced = ""
|
|
if( "invoiced_ref" in inscription_data.keys() ):
|
|
current_invoiced_ref = inscription_data['invoiced_ref']
|
|
|
|
if ("invoiced" in inscription_data.keys()):
|
|
local_invoiced = inscription_data['invoiced']
|
|
|
|
new_data = {}
|
|
new_data['invoiced_ref'] = current_invoiced_ref+", "+str(credit_notes_reference)
|
|
new_data['invoiced'] = "0"
|
|
|
|
print( {'_id': ObjectId(str(inscription_data['_id'])), 'valide': '1',
|
|
'partner_owner_recid': str(my_partner['recid'])
|
|
} )
|
|
|
|
ret_val2 = MYSY_GV.dbname['inscription'].find_one_and_update(
|
|
{'_id': ObjectId(str(inscription_data['_id'])), 'valide': '1',
|
|
'partner_owner_recid': str(my_partner['recid'])
|
|
},
|
|
{"$set": new_data},
|
|
return_document=ReturnDocument.AFTER,
|
|
upsert=False,
|
|
)
|
|
|
|
"""
|
|
Creation des log historique pour :
|
|
- la facture et
|
|
- l'avoir
|
|
"""
|
|
## Add to log history pour l'annulation de l'avoir
|
|
now = str(datetime.now().strftime("%d/%m/%Y %H:%M:%S"))
|
|
history_event_dict = {}
|
|
history_event_dict['token'] = str(token)
|
|
history_event_dict['related_collection'] = "partner_invoice_header"
|
|
history_event_dict['related_collection_recid'] = str(diction['_id'])
|
|
history_event_dict['action_date'] = str(now)
|
|
history_event_dict['action_description'] = "Avoir : annulation facture. Ref Avoir : '"+str(credit_notes_reference)+"' "
|
|
local_status, local_retval = mycommon.Collection_Historique.Add_Historique_Event(history_event_dict)
|
|
if (local_status is False):
|
|
mycommon.myprint(
|
|
" WARNING : Impossible de logguer l'historique pour l'évènement : " + str(history_event_dict))
|
|
|
|
## Add to log history pour l'avoir (nouveau document)
|
|
now = str(datetime.now().strftime("%d/%m/%Y %H:%M:%S"))
|
|
history_event_dict = {}
|
|
history_event_dict['token'] = str(token)
|
|
history_event_dict['related_collection'] = "partner_invoice_header"
|
|
history_event_dict['related_collection_recid'] = str(invoice_header_inserted_id)
|
|
history_event_dict['action_date'] = str(now)
|
|
history_event_dict['action_description'] = "Creation Avoir "
|
|
local_status, local_retval = mycommon.Collection_Historique.Add_Historique_Event(history_event_dict)
|
|
if (local_status is False):
|
|
mycommon.myprint(
|
|
" WARNING : Impossible de logguer l'historique pour l'évènement : " + str(history_event_dict))
|
|
|
|
|
|
|
|
|
|
return True, "L'avoir a été créé avec la référence "+str(liste_credit_node), liste_credit_node
|
|
|
|
|
|
except Exception as e:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - Line : " + str(exc_tb.tb_lineno))
|
|
return False, " Impossible de créer l'avoir ", False
|