""" Ce fichier permet de gerer les factures client et les methodes de payement """ import pymongo from pymongo import MongoClient import json from bson import ObjectId import re from datetime import datetime import prj_common as mycommon import secrets import inspect import sys, os import csv import pandas as pd from pymongo import ReturnDocument from math import isnan import GlobalVariable as MYSY_GV import email_mgt as email from dateutil import tz import pytz class JSONEncoder(json.JSONEncoder): def default(self, o): if isinstance(o, ObjectId): return str(o) return json.JSONEncoder.default(self, o) def get_invoice_by_customer(diction): try: field_list = ['token',] incom_keys = diction.keys() for val in incom_keys: if val not in field_list: mycommon.myprint(str(inspect.stack()[0][ 3]) + " - Le champ '" + val + "' n'existe pas, Creation formation annulée") return False, " Impossible de recuperer les factures" ''' Une fois qu'on a controlé que toutes les clés mise dans l'API sont correcte. etape precedente, On controle que les champs obligatoires sont presents dans la liste ''' 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 liste ") return False, " Impossible de recuperer les factures" # recuperation des paramettre my_token = "" user_recid = "" if ("token" in diction.keys()): if diction['token']: my_token = diction['token'] user_recid = "None" # Verification de la validité du token/mail dans le cas des user en mode connecté if (len(str(my_token)) > 0): retval = mycommon.check_partner_token_validity("", my_token) if retval is False: mycommon.myprint(str(inspect.stack()[0][3]) + " - Le token n'est pas valide") return False, " Impossible de recuperer les factures" # Recuperation du recid de l'utilisateur user_recid = mycommon.get_parnter_recid_from_token(my_token) if user_recid is False: mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de recuperer le token de l'utilisateur") return False, " Impossible de recuperer les factures" if (len(str(my_token)) <= 0): mycommon.myprint(str(inspect.stack()[0][3]) + " - Le token est vide") return False, " Impossible de recuperer les factures" RetObject = [] coll_facture = MYSY_GV.dbname['factures'] for retVal in coll_facture.find({'client_recid':user_recid, 'valide': '1'} )\ .sort([("date_facture", pymongo.ASCENDING), ("num_facture", pymongo.DESCENDING), ]): user = retVal if ("_id" in user.keys()): user['class_id'] = user.pop('_id') RetObject.append(JSONEncoder().encode(user)) 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) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno)) return False, "Impossible de recuperer les factures" ''' Cette API enregistre la methode de payement. /!\ un client a une seule mode de payement ceci est enregistré dans la collection : payement_mode ''' def add_payement_mode(diction): try: field_list = ['token', 'pwd', 'secret', 'nom_carte', 'num_carte', 'date_exp_carte', 'cvv_carte', 'nom_compte', 'iban', 'bic','type' ] incom_keys = diction.keys() for val in incom_keys: if val not in field_list: mycommon.myprint(str(inspect.stack()[0][ 3]) + " - Creation partner account : Le champ '" + val + "' n'existe pas, Creation formation annulée") return False, "Impossible d'ajouter le mode de payement" ''' Une fois qu'on a controlé que toutes les clés mise dans l'API sont correcte. etape precedente, On controle que les champs obligatoires sont presents dans la liste ''' field_list_obligatoire = ['token', 'pwd', 'secret', 'type'] 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 liste ") return False, "Impossible d'ajouter le mode de payement" # recuperation des paramettre my_token = "" user_recid = "" my_pwd = "" my_secret = "" if ("pwd" in diction.keys()): if diction['pwd']: my_pwd = diction['pwd'] if ("secret" in diction.keys()): if diction['secret']: my_secret = diction['secret'] if ("token" in diction.keys()): if diction['token']: my_token = diction['token'] # Verification de la validité du pwd et du secret coll_tmp = MYSY_GV.dbname['partnair_account'] tmp_count = coll_tmp.count_documents({'pwd': str(my_pwd), 'active': '1', 'token':str(my_secret)}) if (tmp_count <= 0): return False, "Les identifiants sont incorrectes" user_recid = "None" # Verification de la validité du token/mail dans le cas des user en mode connecté if (len(str(my_token)) > 0): retval = mycommon.check_partner_token_validity("", my_token) if retval is False: mycommon.myprint(str(inspect.stack()[0][3]) + " - Le token n'est pas valide") return False, "Impossible d'ajouter le mode de payement" # Recuperation du recid de l'utilisateur user_recid = mycommon.get_parnter_recid_from_token(my_token) if user_recid is False: mycommon.myprint( str(inspect.stack()[0][3]) + " - Impossible de recuperer le token de l'utilisateur") return False, "Impossible d'ajouter le mode de payement" if (len(str(my_token)) <= 0): mycommon.myprint(str(inspect.stack()[0][3]) + " - Le token est vide") return False, "Impossible d'ajouter le mode de payement" coll_name = MYSY_GV.dbname['payement_mode'] new_data = {} new_data['valide'] = "1" now = datetime.now() new_data['date_update'] = str(now) # initialisation pr nettoyer l'existant (faire comme un delete) new_data['nom_carte'] = "" new_data['num_carte'] = "" new_data['date_exp_carte'] = "" new_data['cvv_carte'] = "" new_data['nom_compte'] = "" new_data['iban'] = "" new_data['bic'] = "" if ("nom_carte" in diction.keys()): if diction['nom_carte']: new_data['nom_carte'] = diction['nom_carte'] if ("num_carte" in diction.keys()): if diction['num_carte']: new_data['num_carte'] = diction['num_carte'] if ("type" in diction.keys()): if diction['type']: new_data['type'] = diction['type'] if ("date_exp_carte" in diction.keys()): if diction['date_exp_carte']: new_data['date_exp_carte'] = diction['date_exp_carte'] if ("cvv_carte" in diction.keys()): if diction['cvv_carte']: new_data['cvv_carte'] = diction['cvv_carte'] if ("nom_compte" in diction.keys()): if diction['nom_compte']: new_data['nom_compte'] = diction['nom_compte'] if ("iban" in diction.keys()): if diction['iban']: new_data['iban'] = diction['iban'] if ("nom_compte" in diction.keys()): if diction['nom_compte']: new_data['nom_compte'] = diction['nom_compte'] if ("bic" in diction.keys()): if diction['bic']: new_data['bic'] = diction['bic'] print("mode de payement data :"+str(new_data)) ret_val = coll_name.find_one_and_update( {'client_recid': user_recid, 'valide': '1'}, {"$set": new_data}, upsert=True, return_document=ReturnDocument.AFTER ) if (ret_val['_id'] is False): mycommon.myprint( str(inspect.stack()[0][3]) + " : Impossible d'ajouter le mode de payement") return False, "Impossible d'ajouter le mode de payement" ''' Une fois que le mode de payement est ajouté, alors valide le compte partenaire en mettant : ispending à 0 ''' coll_name = MYSY_GV.dbname['partnair_account'] data_update = {'date_update':str(datetime.now()), 'ispending':'0'} ret_val = coll_name.find_one_and_update( {'recid': user_recid, }, {"$set": data_update}, upsert=False, return_document=ReturnDocument.AFTER ) if (ret_val['_id'] is False): mycommon.myprint( str(inspect.stack()[0][3]) + " : Impossible de mettre à jour (ispending':'1') le compte partenaire") return False, "Impossible d'ajouter le mode de payement" return True, " Le mode de payement a bien ete ajouté" except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno)) return False, "Impossible d'ajouter le mode de payement" ''' Cette fonction recuperer le mode de payement par defaut d'un client ''' def get_payement_mode(diction): try: field_list = ['token', ] incom_keys = diction.keys() for val in incom_keys: if val not in field_list: mycommon.myprint(str(inspect.stack()[0][ 3]) + " - Creation partner account : Le champ '" + val + "' n'existe pas, Creation formation annulée") return False, " Impossible de recuperer les mode de payement" ''' Une fois qu'on a controlé que toutes les clés mise dans l'API sont correcte. etape precedente, On controle que les champs obligatoires sont presents dans la liste ''' 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 liste ") return False, " Impossible de recuperer les mode de payement" # recuperation des paramettre my_token = "" user_recid = "" if ("token" in diction.keys()): if diction['token']: my_token = diction['token'] user_recid = "None" # Verification de la validité du token/mail dans le cas des user en mode connecté if (len(str(my_token)) > 0): retval = mycommon.check_partner_token_validity("", my_token) if retval is False: mycommon.myprint(str(inspect.stack()[0][3]) + " - Le token n'est pas valide") return False, " Impossible de recuperer les mode de payement" # Recuperation du recid de l'utilisateur user_recid = mycommon.get_parnter_recid_from_token(my_token) if user_recid is False: mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de recuperer le token de l'utilisateur") return False, " Impossible de recuperer les mode de payement" if (len(str(my_token)) <= 0): mycommon.myprint(str(inspect.stack()[0][3]) + " - Le token est vide") return False, " Impossible de recuperer les mode de payement" RetObject = [] coll_facture = MYSY_GV.dbname['payement_mode'] for retVal in coll_facture.find({'client_recid': user_recid, 'valide': '1'}): user = retVal RetObject.append(JSONEncoder().encode(user)) 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) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno)) return False, " Impossible de recuperer les mode de payement" """ Cette fonction crée une facture dans la collecion : factures. """ def createOrder(diction): try: order_id = "" nb_line = 0 num_facture = "" '''field_list = ['token', 'nb_product'] incom_keys = diction.keys() for val in incom_keys: if val not in field_list: mycommon.myprint(str(inspect.stack()[0][ 3]) + " - Le champ '" + val + "' n'existe pas, Creation formation annulée") return False, " Impossible de créer la facture" ''' ''' Une fois qu'on a controlé que toutes les clés mise dans l'API sont correcte. etape precedente, On controle que les champs obligatoires sont presents dans la liste ''' field_list_obligatoire = ['token', 'nb_product', 'periodicite'] 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 liste ") return False, " Impossible de créer la facture" # recuperation des paramettre my_token = "" user_recid = "" if ("token" in diction.keys()): if diction['token']: my_token = diction['token'] user_recid = "None" # Verification de la validité du token/mail dans le cas des user en mode connecté if (len(str(my_token)) > 0): retval = mycommon.check_partner_token_validity("", my_token) if retval is False: mycommon.myprint(str(inspect.stack()[0][3]) + " - Le token n'est pas valide") return False, " Impossible de créer la facture" # Recuperation du recid de l'utilisateur user_recid = mycommon.get_parnter_recid_from_token(my_token) if user_recid is False: mycommon.myprint(str(inspect.stack()[0][3]) + " - Impossible de recuperer le token de l'utilisateur") return False," Impossible de créer la facture" if (len(str(my_token)) <= 0): mycommon.myprint(str(inspect.stack()[0][3]) + " - Le token est vide") return False, " Impossible de créer la facture" new_data = {} if ("nb_product" in diction.keys()): if diction['nb_product']: nb_line = mycommon.tryInt(diction['nb_product']) if ("periodicite" in diction.keys()): if diction['periodicite']: new_data['periodicite'] = diction['periodicite'] # Recuperation des données client new_data['client_recid'] = user_recid coll_part_account = MYSY_GV.dbname['partnair_account'] part_account = coll_part_account.find({'recid':user_recid, 'active':'1'}) if (part_account.count() <= 0): mycommon.myprint( str(inspect.stack()[0][3]) + "Aucune donnée client, Impossible de créer la commande ") return False, "Aucune donnée client, Impossible de créer la commande ", None if ("invoice_nom" in part_account[0].keys()): if part_account[0]['invoice_nom']: new_data['invoice_nom'] = part_account[0]['invoice_nom'] if ("invoice_adr_city" in part_account[0].keys()): if part_account[0]['invoice_adr_city']: new_data['invoice_adr_city'] = part_account[0]['invoice_adr_city'] if ("invoice_adr_country" in part_account[0].keys()): if part_account[0]['invoice_adr_country']: new_data['invoice_adr_country'] = part_account[0]['invoice_adr_country'] if ("invoice_adr_street" in part_account[0].keys()): if part_account[0]['invoice_adr_street']: new_data['invoice_adr_street'] = part_account[0]['invoice_adr_street'] if ("invoice_adr_zip" in part_account[0].keys()): if part_account[0]['invoice_adr_zip']: new_data['invoice_adr_zip'] = part_account[0]['invoice_adr_zip'] if ("invoice_adr_street" in part_account[0].keys()): if part_account[0]['invoice_adr_street']: new_data['invoice_adr_street'] = part_account[0]['invoice_adr_street'] if ("invoice_email" in part_account[0].keys()): if part_account[0]['invoice_email']: new_data['invoice_email'] = part_account[0]['invoice_email'] if ("invoice_vat_num" in part_account[0].keys()): if part_account[0]['invoice_vat_num']: new_data['invoice_vat_num'] = part_account[0]['invoice_vat_num'] if ("invoice_telephone" in part_account[0].keys()): if part_account[0]['invoice_telephone']: new_data['invoice_telephone'] = part_account[0]['invoice_telephone'] # Recuperation ds données de payement coll_part_payment = MYSY_GV.dbname['payement_mode'] part_account = coll_part_payment.find({'client_recid': user_recid, 'valide': '1'}) if( part_account.count() <= 0 ): mycommon.myprint( str(inspect.stack()[0][3]) + "Aucune donnée de payement, Impossible de créer la commande ") return False, "Aucune donnée de payement,, Impossible de créer la commande ", None if ("type" in part_account[0].keys()): if part_account[0]['type']: new_data['type_payment'] = part_account[0]['type'] if ("bic" in part_account[0].keys()): if part_account[0]['bic']: new_data['bic_payment'] = part_account[0]['bic'] if ("cvv_carte" in part_account[0].keys()): if part_account[0]['cvv_carte']: new_data['cvv_carte_payment'] = part_account[0]['cvv_carte'] if ("date_exp_carte" in part_account[0].keys()): if part_account[0]['date_exp_carte']: new_data['date_exp_carte_payment'] = part_account[0]['date_exp_carte'] if ("iban" in part_account[0].keys()): if part_account[0]['iban']: new_data['iban_payment'] = part_account[0]['iban'] if ("nom_carte" in part_account[0].keys()): if part_account[0]['nom_carte']: new_data['nom_carte_payment'] = part_account[0]['nom_carte'] if ("nom_compte" in part_account[0].keys()): if part_account[0]['nom_compte']: new_data['nom_compte_payment'] = part_account[0]['nom_compte'] if ("num_carte" in part_account[0].keys()): if part_account[0]['num_carte']: new_data['num_carte_payment'] = part_account[0]['num_carte'] new_data['nb_product'] = str(nb_line) i = 0 total_ht = 0 while (i < nb_line): print("PRODUIT N° " + str((i + 1)) + " : " ) line_dict = json.loads(diction[str(i)]) print(" line = "+str(line_dict)+" -- le type est : "+str(type(line_dict))) print(" code = "+str( line_dict['code'] )) print(" prix = " + str(line_dict['prix'])) print(" qty = " + str(line_dict['qty'])) montant_line = mycommon.tryFloat(str(line_dict['prix'])) * mycommon.tryInt(str(line_dict['qty'])) print(" montant_line = " + str(montant_line)) total_ht = total_ht + montant_line new_data_item = {} new_data_item['code'] = line_dict['code'] new_data_item['prix'] = line_dict['prix'] new_data_item['qty'] = line_dict['qty'] new_data_item['amount'] = str(montant_line) row = "item_"+str(i) new_data[str(row)] = new_data_item i = i + 1 new_data['total_ht'] = total_ht new_data['total_tva'] = mycommon.tryFloat( str(total_ht) ) * MYSY_GV.TVA_TAUX new_data['total_ttc'] = mycommon.tryFloat(str(total_ht))+mycommon.tryFloat(str(new_data['total_tva'])) print(" new_data['total_ht'] = "+str(new_data['total_ht']) + " -- new_data['total_tva'] " +str(new_data['total_tva'])+ " -- new_data['total_ttc'] = "+ str(new_data['total_ttc'])) new_data['valide'] = "1" now = datetime.now() new_data['date_update'] = str(now) new_data['order_date'] = str(now.strftime("%d/%m/%Y, %H:%M:%S")) new_data['order_id'] = "MySy_00"+str(mycommon.create_order_id()) print(" la line à facturer est ::::: "+str(new_data)) # Enregistrement de la commande dans la systeme coll_order = MYSY_GV.dbname['sales_order'] ret_val = coll_order.insert_one(new_data) if ret_val and ret_val.inserted_id: print( "str(new_data['invoice_email']) = "+str(new_data['invoice_email'])) # Envoie de l'email email.SalesOrderConfirmationEmail(str(new_data['invoice_email']), new_data ) # Mise à jour du partenaire (ajouter le nouveau pack) if ("pack" in diction.keys()): if diction['pack']: mypack = diction['pack'] coll_partner = MYSY_GV.dbname['partnair_account'] ret_val = coll_partner.find_one_and_update( {'recid': str(user_recid), }, {"$set": {'pack_service':str(mypack)}}, upsert=False, return_document=ReturnDocument.AFTER ) return True, "la commande été correctement créee", str(new_data['order_id']) else: mycommon.myprint( str(inspect.stack()[0][3]) + "Impossible de créer la commande ") return False, "Impossible de créer la commande ", None except Exception as e: exc_type, exc_obj, exc_tb = sys.exc_info() mycommon.myprint(str(inspect.stack()[0][3]) + " -" + str(e) + " - ERRORRRR AT Line : " + str(exc_tb.tb_lineno)) return False, " Impossible de créer la facture"