from django.http import HttpResponse
from django.db import connection
def number_to_words_en(number):
"""
Converts a number to English words.
Handles decimals up to 3 places.
"""
if number == 0:
return "Zero"
units = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]
tens = ["", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"]
thousands = ["", "Thousand", "Million", "Billion"]
def _convert_less_than_thousand(num):
res = ""
if num >= 100:
res += units[num // 100] + " Hundred "
num %= 100
if num >= 20:
res += tens[num // 10] + " "
num %= 10
if num > 0:
res += units[num]
return res.strip()
try:
parts = str(float(number)).split('.')
integer_part = int(parts[0])
fractional_part = int(parts[1]) if len(parts) > 1 else 0
except ValueError:
return "Invalid Number"
res = ""
if integer_part == 0:
res = "Zero"
else:
idx = 0
while integer_part > 0:
if integer_part % 1000 != 0:
res = _convert_less_than_thousand(integer_part % 1000) + " " + thousands[idx] + " " + res
integer_part //= 1000
idx += 1
words = res.strip()
if fractional_part > 0:
frac_str = parts[1]
denom = 10 ** len(frac_str)
words += f" and {fractional_part}/{denom}"
return words
def number_to_words_ar(number):
return number_to_words_en(number)
def send_whatsapp_message(phone, message):
try:
import requests
from .models import SystemSetting
settings = SystemSetting.objects.first()
if not settings or not settings.wablas_enabled:
return False, "WhatsApp gateway is disabled."
if not settings.wablas_token or not settings.wablas_server_url:
return False, "Wablas configuration is incomplete."
phone = ''.join(filter(str.isdigit, str(phone)))
server_url = settings.wablas_server_url.rstrip('/')
url = f"{server_url}/api/send-message"
headers = {
"Authorization": settings.wablas_token,
"Secret": settings.wablas_secret_key
}
payload = {"phone": phone, "message": message}
response = requests.post(url, data=payload, headers=headers, timeout=10)
data = response.json()
if response.status_code == 200 and data.get('status') == True:
return True, "Message sent successfully."
else:
return False, data.get('message', 'Unknown error from Wablas.')
except Exception as e:
return False, str(e)
def send_whatsapp_document(phone, document_url, caption=""):
try:
import requests
from .models import SystemSetting
settings = SystemSetting.objects.first()
if not settings or not settings.wablas_enabled:
return False, "WhatsApp gateway is disabled."
if not settings.wablas_token or not settings.wablas_server_url:
return False, "Wablas configuration is incomplete."
phone = ''.join(filter(str.isdigit, str(phone)))
server_url = settings.wablas_server_url.rstrip('/')
url = f"{server_url}/api/send-document"
headers = {
"Authorization": settings.wablas_token,
"Secret": settings.wablas_secret_key
}
payload = {
"phone": phone,
"document": document_url,
"caption": caption
}
response = requests.post(url, data=payload, headers=headers, timeout=15)
data = response.json()
if response.status_code == 200 and data.get('status') == True:
return True, "Document sent successfully."
else:
return False, data.get('message', 'Unknown error from Wablas.')
except Exception as e:
return False, str(e)
def fix_db_view(request):
log = []
with connection.cursor() as cursor:
# 1. Check/Add is_service to core_product
try:
cursor.execute("SELECT is_service FROM core_product LIMIT 1")
log.append("SUCCESS: is_service already exists in core_product.")
except Exception:
try:
# Try MySQL syntax first
cursor.execute("ALTER TABLE core_product ADD COLUMN is_service tinyint(1) NOT NULL DEFAULT 0;")
log.append("FIXED: Added is_service column to core_product.")
except Exception as e:
log.append(f"ERROR adding is_service: {e}")
# 2. Check/Add is_active to core_paymentmethod
try:
cursor.execute("SELECT is_active FROM core_paymentmethod LIMIT 1")
log.append("SUCCESS: is_active already exists in core_paymentmethod.")
except Exception:
try:
cursor.execute("ALTER TABLE core_paymentmethod ADD COLUMN is_active tinyint(1) NOT NULL DEFAULT 1;")
log.append("FIXED: Added is_active column to core_paymentmethod.")
except Exception as e:
log.append(f"ERROR adding is_active: {e}")
return HttpResponse("
".join(log) + "
Go to Dashboard")