diff --git a/ela_factures_mgt.py b/ela_factures_mgt.py index 3cd406e..0456a20 100644 --- a/ela_factures_mgt.py +++ b/ela_factures_mgt.py @@ -674,13 +674,35 @@ def CreateInvoice(diction): """ Mise à jour de la commande avec le prochaine date de facturation """ next_invoice_date = "" - if( str(diction['periodicite']).lower() == "mensuel" ): + if ("next_invoice_date" in diction.keys()): + if diction['next_invoice_date']: + local_status, tmp_date = mycommon.TryToDateYYYMMDD(diction['next_invoice_date']) + + print(" apres conversion tmp_date = "+str(tmp_date)) + + if( local_status and str(diction['periodicite']).lower() == "mensuel" ): + next_invoice_date = (tmp_date + relativedelta(months=+1)).date() + print(" ### next_invoice_date = "+str(next_invoice_date)) + elif (local_status is False): + print(" ### IMPOSSIBLE DE FACTURER la COMMANDE") + return False + + if (local_status and str(diction['periodicite']).lower() == "annuel"): + next_invoice_date = (tmp_date + relativedelta(years=+1)).date() + print(" ### next_invoice_date = " + str(next_invoice_date)) + elif (local_status is False): + print(" ### IMPOSSIBLE DE FACTURER la COMMANDE") + return False + + + + elif( str(diction['periodicite']).lower() == "mensuel" ): next_invoice_date = datetime.today().date() + relativedelta(months=+1) - if (str(diction['periodicite']).lower() == "annuel"): + elif (str(diction['periodicite']).lower() == "annuel"): next_invoice_date = datetime.today().date() + relativedelta(years=+1) - #print(" ####### prochaine facturation de la commande : "+str(diction['order_id'])+" LE : "+str(next_invoice_date)) + print(" ####### prochaine facturation de la commande : "+str(diction['order_id'])+" LE : "+str(next_invoice_date)) coll_orders = MYSY_GV.dbname['sales_order'] ret_val_order = coll_orders.find_one_and_update( @@ -904,3 +926,40 @@ def GetCustomerInvoice(diction): 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, "KO" + + +""" +Cette fonction va créer toutes les facture pour les commandes +dont la date facturation est arrivée à échéance (colonne : next_invoice_date) + +IMPORTANT : Meme la facturation n'est pas faire le meme jours, on qu'on a un retard de X jours, ceci +n'est pas grave tant que X < 1 mois. + +Car à la facturation, la date prochaine facturation se mettra à date de dernière facture + 1 mois ou un 1 an. + +""" +""" +Cette fonction créer une facture +""" +def AutoamticCreateInvoice(): + try: + + + field_list_obligatoire = ['client_recid', 'invoice_nom', 'order_id', 'order_date', 'total_ttc','total_tva','total_ht','item_0'] + coll_order = MYSY_GV.dbname['sales_order'] + + today = datetime.today().date() + i = 0 + for diction in coll_order.find({"next_invoice_date" : { '$lte' : str(today) }},{'_id':0}): + i = i +1 + print(' Facturation de la commande '+str(diction['order_id'])+" -- "+str(diction['next_invoice_date'])) + + CreateInvoice(diction) + + return True, str(i)+" Factures ont été créées" + + 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 les factures automatiques" + diff --git a/main.py b/main.py index 2da3610..031cdc6 100644 --- a/main.py +++ b/main.py @@ -1354,11 +1354,23 @@ def GetCustomerInvoice(invoiceid, token): payload = {} payload['token'] = str(token) payload['invoiceid'] = str(invoiceid) - print(" ### payload facture = ", str(invoiceid), " token = ",str(token)) - return factures.GetCustomerInvoice(payload) +""" +API de facturation automatique des commandes +""" +@app.route('/myclass/api/AutoamticCreateInvoice/', methods=['GET','POST']) +@crossdomain(origin='*') +def AutoamticCreateInvoice(): + # On recupere le corps (payload) de la requete + payload = request.form.to_dict() + print(" ### payload = ", str(payload)) + status, message = factures.AutoamticCreateInvoice() + return jsonify(status=status, message=message) + + + if __name__ == '__main__': print(" debut api") diff --git a/prj_common.py b/prj_common.py index 82b9cf6..48a3b0c 100644 --- a/prj_common.py +++ b/prj_common.py @@ -1696,3 +1696,14 @@ def PutClassNote(): 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, "KO" + +""" +Cette fonction essaye de convertir une chaine en date (yyyy-mm-dd) +""" +def TryToDateYYYMMDD(mydate): + try: + datetime.strptime(mydate, '%Y-%m-%d') + return True, datetime.strptime(mydate, '%Y-%m-%d') + + except ValueError: + return False, False \ No newline at end of file