File size: 10,847 Bytes
41aafc4
 
 
 
 
 
 
 
 
 
 
 
 
 
337cd5d
 
 
 
4d31bc7
41aafc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
337cd5d
41aafc4
 
337cd5d
41aafc4
337cd5d
4d31bc7
41aafc4
 
 
4d31bc7
 
41aafc4
 
 
4d31bc7
41aafc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d31bc7
41aafc4
4d31bc7
 
41aafc4
 
 
 
4d31bc7
41aafc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d31bc7
41aafc4
4d31bc7
 
17b18e9
 
41aafc4
4d31bc7
 
41aafc4
 
17b18e9
41aafc4
 
 
 
 
 
 
 
 
 
 
 
 
17b18e9
41aafc4
 
 
 
 
 
 
 
 
17b18e9
41aafc4
17b18e9
 
41aafc4
 
 
 
 
337cd5d
4d31bc7
 
17b18e9
41aafc4
 
 
17b18e9
 
41aafc4
 
 
 
 
 
 
 
4d31bc7
41aafc4
4d31bc7
41aafc4
4d31bc7
41aafc4
337cd5d
41aafc4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
"""
Quick start example for merchant module usage.
Run this after starting the server to create sample merchants.
"""
import asyncio
import httpx

BASE_URL = "http://localhost:8000"


async def create_sample_merchants():
    """Create a complete merchant hierarchy for testing"""
    
    async with httpx.AsyncClient() as client:
        # 1. Create ncnf (Root)
        print("Creating ncnf...")
        ncnf = {
            "merchant_type": "ncnf",
            "merchant_code": "ncnf-INDIA-001",
            "merchant_name": "National Distribution Center India",
            "contact": {
                "phone": "+919876543210",
                "email": "national@distribution.com",
                "address_line1": "Plot 123, Industrial Area",
                "address_line2": "Phase 1",
                "city": "Mumbai",
                "state": "Maharashtra",
                "pincode": "400001",
                "country": "India"
            },
            "kyc": {
                "gst_number": "27AAPFU0939F1ZV",
                "pan_number": "AAPFU0939F",
                "bank_account_number": "1234567890123",
                "bank_ifsc": "HDFC0001234",
                "business_certificate_url": "https://storage.example.com/ncnf_cert.pdf",
                "cancelled_cheque_url": "https://storage.example.com/ncnf_cheque.jpg"
            },
            "settings": {
                "pricing_inherited": False,
                "inventory_inherited": False,
                "auto_allocate_stock": True,
                "allowed_payment_modes": ["PREPAID", "COD"],
                "credit_limit": 10000000
            },
            "created_by": "system_admin"
        }
        
        response = await client.post(f"{BASE_URL}/merchants", json=ncnf)
        if response.status_code == 201:
            ncnf_data = response.json()
            print(f"βœ“ Created ncnf: {ncnf_data['merchant_id']}")
            
            # 2. Create cnf under ncnf
            print("\nCreating cnf...")
            cnf = {
                "merchant_type": "cnf",
                "parent_merchant_id": ncnf_data["merchant_id"],
                "merchant_code": "cnf-MH-001",
                "merchant_name": "Maharashtra cnf",
                "contact": {
                    "phone": "+919876543211",
                    "email": "maharashtra@cnf.com",
                    "address_line1": "456 cnf Street",
                    "city": "Mumbai",
                    "state": "Maharashtra",
                    "pincode": "400002",
                    "country": "India"
                },
                "kyc": {
                    "gst_number": "27BBCDE1234F1Z5",
                    "pan_number": "BBCDE1234F",
                    "bank_account_number": "9876543210123",
                    "bank_ifsc": "ICIC0001234",
                    "business_certificate_url": "https://storage.example.com/cnf_cert.pdf",
                    "cancelled_cheque_url": "https://storage.example.com/cnf_cheque.jpg"
                },
                "settings": {
                    "pricing_inherited": True,
                    "allowed_payment_modes": ["PREPAID", "COD"],
                    "credit_limit": 5000000
                },
                "created_by": "ncnf_admin"
            }
            
            response = await client.post(f"{BASE_URL}/merchants", json=cnf)
            if response.status_code == 201:
                cnf_data = response.json()
                print(f"βœ“ Created cnf: {cnf_data['merchant_id']}")
                
                # 3. Create distributor under cnf
                print("\nCreating distributor...")
                distributor = {
                    "merchant_type": "distributor",
                    "parent_merchant_id": cnf_data["merchant_id"],
                    "merchant_code": "DIST-MUM-001",
                    "merchant_name": "Mumbai Central distributor",
                    "contact": {
                        "phone": "+919876543212",
                        "email": "mumbai@distributor.com",
                        "address_line1": "789 Distribution Hub",
                        "city": "Mumbai",
                        "state": "Maharashtra",
                        "pincode": "400003",
                        "country": "India"
                    },
                    "kyc": {
                        "gst_number": "27CDEFG5678H1Z2",
                        "pan_number": "CDEFG5678H",
                        "bank_account_number": "5555666677778888",
                        "bank_ifsc": "SBIN0001234",
                        "business_certificate_url": "https://storage.example.com/dist_cert.pdf",
                        "cancelled_cheque_url": "https://storage.example.com/dist_cheque.jpg"
                    },
                    "settings": {
                        "pricing_inherited": True,
                        "allowed_payment_modes": ["PREPAID", "COD", "PARTIAL"],
                        "credit_limit": 2000000
                    },
                    "created_by": "cnf_admin"
                }
                
                response = await client.post(f"{BASE_URL}/merchants", json=distributor)
                if response.status_code == 201:
                    dist_data = response.json()
                    print(f"βœ“ Created distributor: {dist_data['merchant_id']}")
                    
                    # 4. Create retail under distributor
                    print("\nCreating retail...")
                    retail = {
                        "merchant_type": "retail",
                        "parent_merchant_id": dist_data["merchant_id"],
                        "merchant_code": "retail-MUM-001",
                        "merchant_name": "Glamour retail Mumbai",
                        "contact": {
                            "phone": "+919876543213",
                            "email": "contact@glamourretail.com",
                            "address_line1": "101 Fashion Street",
                            "address_line2": "Near Mall",
                            "city": "Mumbai",
                            "state": "Maharashtra",
                            "pincode": "400004",
                            "country": "India"
                        },
                        "geo_location": {
                            "lat": 19.0760,
                            "lng": 72.8777
                        },
                        "kyc": {
                            "pan_number": "DEFGH6789I",
                            "business_certificate_url": "https://storage.example.com/retail_cert.pdf"
                        },
                        "settings": {
                            "pricing_inherited": True,
                            "allowed_payment_modes": ["PREPAID", "COD"],
                            "credit_limit": 100000
                        },
                        "created_by": "dist_admin"
                    }
                    
                    response = await client.post(f"{BASE_URL}/merchants", json=retail)
                    if response.status_code == 201:
                        retail_data = response.json()
                        print(f"βœ“ Created retail: {retail_data['merchant_id']}")
                        
                        # Show hierarchy
                        print("\n" + "="*60)
                        print("MERCHANT HIERARCHY CREATED SUCCESSFULLY")
                        print("="*60)
                        print(f"ncnf: {ncnf_data['merchant_id']}")
                        print(f"  └── cnf: {cnf_data['merchant_id']}")
                        print(f"      └── distributor: {dist_data['merchant_id']}")
                        print(f"          └── retail: {retail_data['merchant_id']}")
                        print("="*60)
                        
                        # Test hierarchy endpoint
                        print("\nFetching hierarchy for retail...")
                        response = await client.get(f"{BASE_URL}/merchants/{retail_data['merchant_id']}/hierarchy")
                        if response.status_code == 200:
                            hierarchy = response.json()
                            print(f"Hierarchy depth: {hierarchy['depth']}")
                            for i, merchant in enumerate(hierarchy['hierarchy'], 1):
                                print(f"  {i}. {merchant['merchant_type']}: {merchant['merchant_name']}")
                        
                        return True
                    else:
                        print(f"βœ— Failed to create retail: {response.text}")
                else:
                    print(f"βœ— Failed to create distributor: {response.text}")
            else:
                print(f"βœ— Failed to create cnf: {response.text}")
        else:
            print(f"βœ— Failed to create ncnf: {response.text}")
            
    return False


async def list_all_merchants():
    """List all merchants"""
    async with httpx.AsyncClient() as client:
        print("\n" + "="*60)
        print("ALL MERCHANTS")
        print("="*60)
        
        response = await client.get(f"{BASE_URL}/merchants?limit=100")
        if response.status_code == 200:
            merchants = response.json()
            for merchant in merchants:
                print(f"{merchant['merchant_type']:15} | {merchant['merchant_code']:15} | {merchant['merchant_name']}")
        else:
            print(f"Error listing merchants: {response.text}")


async def main():
    """Main execution"""
    print("SCM Merchant Module - Quick Start")
    print("="*60)
    print("Ensure the server is running at http://localhost:8000")
    print("="*60)
    
    # Test health check
    async with httpx.AsyncClient() as client:
        try:
            response = await client.get(f"{BASE_URL}/health")
            if response.status_code == 200:
                print("βœ“ Server is running\n")
            else:
                print("βœ— Server health check failed")
                return
        except Exception as e:
            print(f"βœ— Cannot connect to server: {e}")
            print("Please start the server with: python main.py")
            return
    
    # Create sample data
    success = await create_sample_merchants()
    
    if success:
        # List all merchants
        await list_all_merchants()
        
        print("\n" + "="*60)
        print("Quick Start Complete!")
        print("="*60)
        print("Next steps:")
        print("  - Visit http://localhost:8000/docs for API documentation")
        print("  - Try updating a merchant status")
        print("  - Create more merchants in the hierarchy")
        print("  - Test validation with invalid data")


if __name__ == "__main__":
    asyncio.run(main())