File size: 2,570 Bytes
1804a7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import math
import datetime
from lunardate import LunarDate

class RetailTools:
    @staticmethod
    def calculate(expression: str):
        try:
            allowed = set("0123456789.+-*/() ")
            if not all(c in allowed for c in expression): return "Error"
            return str(eval(expression, {"__builtins__": None}, {}))
        except: return "Error"

    @staticmethod
    def get_lunar_date():
        today = datetime.date.today()
        lunar = LunarDate.fromSolarDate(today.year, today.month, today.day)
        return f"{lunar.day}/{lunar.month}/{lunar.year} (Lunar)"

    @staticmethod
    def analyze_financial_health(revenue, total_assets, debt):
        """
        Implements the logic from the Research Paper.
        Calculates simple ratios to predict health.
        """
        try:
            revenue = float(revenue)
            assets = float(total_assets)
            debt = float(debt)
            
            if assets == 0: return "Error: Assets cannot be 0"
            
            # Key Metrics from Paper
            asset_turnover = revenue / assets
            leverage = debt / assets
            
            advice = []
            score = 100
            
            # 1. Asset Turnover Logic
            if asset_turnover < 0.5:
                advice.append("⚠️ Vòng quay tài sản thấp (<0.5). Bạn đang tồn đọng vốn quá nhiều.")
                score -= 20
            else:
                advice.append("✅ Vòng quay tài sản tốt.")

            # 2. Leverage Logic (Paper says high leverage correlates with profit BUT high risk)
            if leverage > 0.6:
                advice.append("⚠️ Tỷ lệ nợ cao (>60%). Rủi ro tài chính lớn nếu thị trường biến động.")
                score -= 30
            elif leverage < 0.2:
                advice.append("ℹ️ Tỷ lệ nợ thấp. Bạn có thể cân nhắc vay thêm để mở rộng (Đòn bẩy tài chính).")
            else:
                advice.append("✅ Cấu trúc vốn an toàn.")

            return {
                "score": score,
                "metrics": {"turnover": round(asset_turnover, 2), "leverage": round(leverage, 2)},
                "advice": advice
            }
        except:
            return "Invalid Data"

    @staticmethod
    def health_check(saas_api, store_id):
        alerts = []
        sales = saas_api.get_sales_report(store_id, "today")
        if sales['revenue'] == 0: alerts.append("⚠️ Chưa có doanh thu hôm nay.")
        return alerts