Commit c651b8d1 by Javier

Nueva Versión

parent d64a0615
from decimal import Decimal from decimal import Decimal
from typing import Tuple from typing import Tuple
import typer import typer
from mbilling.functions import MagnusCommandError, SipAccount, active_users, fully_ported_users, get_sip_accounts, get_cdrs, add_balance, get_user, get_plan_consumption from mbilling.functions import MagnusCommandError, SipAccount, active_users, fully_ported_users
from mbilling.functions import get_sip_accounts, get_cdrs, add_balance, get_user, get_plan_consumption
from mbilling.functions import get_accounts
import time import time
import pyodbc import pyodbc
import os import os
...@@ -43,34 +45,31 @@ def update_sip_accounts() -> 'Tuple[int,int]': ...@@ -43,34 +45,31 @@ def update_sip_accounts() -> 'Tuple[int,int]':
with pyodbc.connect(SQL_SERVER_CNX_STRING) as cnx: with pyodbc.connect(SQL_SERVER_CNX_STRING) as cnx:
with cnx.cursor() as cursor: with cnx.cursor() as cursor:
with typer.progressbar(get_sip_accounts(), label="Sincronizando Cuentas") as sips: with typer.progressbar(get_accounts(only_fullyported=True), label="Sincronizando Cuentas") as accounts:
for sip in sips: for account in accounts:
# Llamadas Salientes por Nueva Plataforma
if sip.credit == 0.0:
cursor.execute(
"SELECT NuevaPlataforma FROM [bdd_wifi].[dbo].[TB_ServicioFijo]\
WHERE DDI IN (?,?) AND NuevaPlataforma=1",
(sip.callerid, sip.account)
)
nueva_plataforma = 1 if cursor.fetchone() else 0
else:
nueva_plataforma = 1
cursor.execute( cursor.execute(
"UPDATE [bdd_wifi].[dbo].[TB_ServicioFijo]\ "UPDATE [bdd_wifi].[dbo].[TB_ServicioFijo]\
SET SaldoActual=?, Activo=?, TarifaPlana=?, Pospago=?, IdTarifa=?, UsuarioEXT=?,\ SET SaldoActual=?, Activo=?, TarifaPlana=?, Pospago=?, IdTarifa=?,\
ClaveEXT=?, NuevaPlataforma=? WHERE DDI=? AND FechaBaja is NULL", UsuarioEXT=?, ClaveEXT=?, NuevaPlataforma=1 WHERE DDI=? AND FechaBaja is NULL",
(sip.credit, sip.active, sip.offer != 0, sip.postpaid, sip.plan, (account.credit, account.active, account.plan == 4, account.postpaid,
sip.callerid if sip.account == sip.callerid else None, account.plan, account.account, account.secret, account.account)
sip.secret if sip.account == sip.callerid else None, nueva_plataforma,
sip.callerid)
) )
if sip.account == sip.callerid: sip_accounts_updated += 1
sip_accounts_updated = sip_accounts_updated + 1
else: for did in account.dids:
did_accounts_updated = did_accounts_updated + 1 if did != account.account:
cursor.execute(
'UPDATE [bdd_wifi].[dbo].[TB_ServicioFijo] SET CuentaA2Billing=NULL,\
UsuarioEXT=NULL, ClaveEXT=NULL, Activo=?, TarifaPlana=0, Pospago=0,\
IdTarifa=0, SaldoActual=0.0 WHERE DDI=?',
(account.active, did)
)
did_accounts_updated += 1
cnx.commit()
return sip_accounts_updated, did_accounts_updated return sip_accounts_updated, did_accounts_updated
...@@ -82,9 +81,11 @@ def activate_new_plataform(): ...@@ -82,9 +81,11 @@ def activate_new_plataform():
with pyodbc.connect(SQL_SERVER_CNX_STRING) as cnx: with pyodbc.connect(SQL_SERVER_CNX_STRING) as cnx:
with cnx.cursor() as cursor: with cnx.cursor() as cursor:
for sip, account in fully_ported_users(): for sip, account in fully_ported_users():
user = get_user(sip)
account = account if sip == account else None account = account if sip == account else None
cursor.execute( cursor.execute(
'UPDATE [bdd_wifi].[dbo].[TB_ServicioFijo] SET NuevaPlataforma=1 WHERE DDI=?', 'UPDATE [bdd_wifi].[dbo].[TB_ServicioFijo] SET NuevaPlataforma=1 WHERE DDI=?\
AND FechaBaja is not NULL',
(sip,) (sip,)
) )
...@@ -105,6 +106,7 @@ def perform_migration(): ...@@ -105,6 +106,7 @@ def perform_migration():
for ddi in migration_targets.fetchall(): for ddi in migration_targets.fetchall():
ddi = ddi[0] ddi = ddi[0]
# Obtención Cuenta MagnusBilling # Obtención Cuenta MagnusBilling
user = get_user(ddi) user = get_user(ddi)
a2billing_user = a2billing.account_data(ddi) a2billing_user = a2billing.account_data(ddi)
...@@ -121,11 +123,9 @@ def perform_migration(): ...@@ -121,11 +123,9 @@ def perform_migration():
cursor.execute( cursor.execute(
'UPDATE [bdd_wifi].[dbo].[TB_Recargas] SET Cuenta=? WHERE Cuenta=?', 'UPDATE [bdd_wifi].[dbo].[TB_Recargas] SET Cuenta=? WHERE Cuenta=?',
(user['username'], a2billing_user['account']) (user['username'], a2billing_user['account'] if a2billing_user else None)
) )
print(user['username'])
else: else:
cursor.execute( cursor.execute(
'UPDATE [bdd_wifi].[dbo].[TB_ServicioFijo] SET CuentaA2Billing=NULL,\ 'UPDATE [bdd_wifi].[dbo].[TB_ServicioFijo] SET CuentaA2Billing=NULL,\
...@@ -258,6 +258,7 @@ def update_plan_consumption(): ...@@ -258,6 +258,7 @@ def update_plan_consumption():
with pyodbc.connect(SQL_SERVER_CNX_STRING) as cnx: with pyodbc.connect(SQL_SERVER_CNX_STRING) as cnx:
with cnx.cursor() as cursor: with cnx.cursor() as cursor:
for account, consumption in get_plan_consumption().items(): for account, consumption in get_plan_consumption().items():
cursor.execute( cursor.execute(
'UPDATE [bdd_wifi].[dbo].[TB_ServicioFijo] SET ConsumoTarifa=?, LimiteTarifa=? WHERE\ 'UPDATE [bdd_wifi].[dbo].[TB_ServicioFijo] SET ConsumoTarifa=?, LimiteTarifa=? WHERE\
UsuarioEXT=?', UsuarioEXT=?',
...@@ -318,7 +319,72 @@ def process_refills() -> Tuple[int, float]: ...@@ -318,7 +319,72 @@ def process_refills() -> Tuple[int, float]:
cnx.commit() cnx.commit()
return total_refills, amount return total_refills, amount
# Definición de Comandos
@app.command()
def restore():
# Restaura las Cuentas Originales
with pyodbc.connect(SQL_SERVER_CNX_STRING) as cnx:
with cnx.cursor() as cursor:
cursor.execute(
'SELECT [UsuarioEXT] FROM [bdd_wifi].[dbo].[TB_ServicioFijo]\
WHERE NuevaPlataforma=1')
for username in cursor.fetchall():
cli = username[0]
user = get_user(cli)
a2billing_account = a2billing.account_data(cli)
if user and a2billing_account:
cursor.execute(
'UPDATE [bdd_wifi].[dbo].[TB_Recargas] SET Cuenta=? WHERE Cuenta=?',
(a2billing_account['account'], user['username'])
)
cursor.execute(
'UPDATE [bdd_wifi].[dbo].[TB_ServicioFijo] SET\
UsuarioExt=?, ClaveEXT=?, NuevaPlataforma=?, SaldoActual=?\
WHERE DDI=?',
(a2billing_account['account'], a2billing_account['secret'],
0, a2billing_account['credit'], cli)
)
cursor.commit()
# Realiza las Migraciones a la Nueva Plataforma
activate_new_plataform()
perform_migration()
# Restaura los Valores de Cuentas que ya no están en Nueva Plataforma
with pyodbc.connect(SQL_SERVER_CNX_STRING) as cnx:
with cnx.cursor() as cursor:
cursor.execute(
'SELECT DDI FROM[bdd_wifi].[dbo].[TB_ServicioFijo]\
WHERE NuevaPlataforma=0 AND UsuarioEXT != CuentaA2Billing')
for username in cursor.fetchall():
cli = username[0]
user = get_user(cli)
a2billing_account = a2billing.account_data(cli)
if user and a2billing_account:
cursor.execute(
'UPDATE [bdd_wifi].[dbo].[TB_Recargas] SET Cuenta=? WHERE Cuenta=?',
(a2billing_account['account'], user['username'])
)
cursor.execute(
'UPDATE [bdd_wifi].[dbo].[TB_ServicioFijo] SET\
UsuarioExt=?, ClaveEXT=?, NuevaPlataforma=?, SaldoActual=?\
WHERE DDI=?',
(a2billing_account['account'], a2billing_account['secret'],
0, a2billing_account['credit'], cli)
)
cursor.commit()
@app.command() @app.command()
...@@ -384,7 +450,7 @@ def synchronize(): ...@@ -384,7 +450,7 @@ def synchronize():
# Actualización de Estado # Actualización de Estado
accounts, balance = update_balance() accounts, balance = update_balance()
update_plan_consumption() update_plan_consumption()
msg += f"\n\t- Se ha actualizado el saldo de {accounts} cuentas, con un saldo total de {typer.style(f'{round(balance,2)}€', fg=typer.colors.GREEN, bold=True)}€." msg += f"\n\t- Se ha actualizado el saldo de {accounts} cuentas, con un saldo total de {typer.style(f'{round(balance,2)}€', fg=typer.colors.GREEN, bold=True)}€."
# Actualización de CDRs # Actualización de CDRs
...@@ -396,8 +462,7 @@ def synchronize(): ...@@ -396,8 +462,7 @@ def synchronize():
@app.command() @app.command()
def test(): def test():
print(a2billing.account_data('956373434')) update_sip_accounts()
if __name__ == "__main__": if __name__ == "__main__":
app() app()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment