File size: 10,477 Bytes
cd357c6
 
 
 
 
b3b8847
 
 
 
cd357c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3b8847
cd357c6
 
 
 
b3b8847
cd357c6
 
 
 
b3b8847
cd357c6
 
 
 
b3b8847
cd357c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env python3
"""
Migration script to generate pricing_levels for catalogues that don't have them.

This script generates pricing_levels based on MRP with the following margins:
- ncnf: MRP - 12%
- cnf: MRP - 6% 
- distributor: MRP - 10%
- retail: MRP - 20%

Usage:
    python migration_generate_missing_pricing_levels.py
"""

import asyncio
import json
from datetime import datetime
from decimal import Decimal, ROUND_HALF_UP
from motor.motor_asyncio import AsyncIOMotorClient
from insightfy_utils.logging import get_logger
from app.core.config import settings
from app.catalogues.constants import SCM_CATALOGUE_COLLECTION

logger = get_logger(__name__)

# Pricing level margins (discount from MRP)
PRICING_MARGINS = {
    "ncnf": {
        "discount_pct": 12,
        "trade_margin": 15,
        "max_discount_pct": 5
    },
    "cnf": {
        "discount_pct": 6,
        "trade_margin": 18,
        "max_discount_pct": 8
    },
    "distributor": {
        "discount_pct": 10,
        "trade_margin": 20,
        "max_discount_pct": 15
    },
    "retail": {
        "discount_pct": 20,
        "trade_margin": 25,
        "max_discount_pct": 20
    }
}


def calculate_pricing_level(mrp: float, level_config: dict) -> dict:
    """
    Calculate pricing for a specific level based on MRP and configuration.
    
    Args:
        mrp: Maximum Retail Price
        level_config: Configuration with discount_pct and margins
        
    Returns:
        Dictionary with cost_price, trade_margin, and max_discount_pct
    """
    mrp_decimal = Decimal(str(mrp))
    discount_pct = Decimal(str(level_config["discount_pct"]))
    
    # Calculate cost price (MRP - discount%)
    cost_price = mrp_decimal * (Decimal('100') - discount_pct) / Decimal('100')
    cost_price = cost_price.quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)
    
    return {
        "cost_price": float(cost_price),
        "trade_margin": level_config["trade_margin"],
        "max_discount_pct": level_config["max_discount_pct"]
    }


def generate_pricing_levels(mrp: float, currency: str = "INR") -> dict:
    """
    Generate complete pricing_levels structure based on MRP.
    
    Args:
        mrp: Maximum Retail Price
        currency: Currency code (default: INR)
        
    Returns:
        Complete pricing_levels dictionary
    """
    if not mrp or mrp <= 0:
        return None
    
    pricing_levels = {
        "currency": currency,
        "mrp": mrp,
        "levels": {}
    }
    
    # Generate pricing for each level
    for level_name, level_config in PRICING_MARGINS.items():
        pricing_levels["levels"][level_name] = calculate_pricing_level(mrp, level_config)
    
    return pricing_levels


async def find_catalogues_without_pricing_levels():
    """Find catalogues that don't have pricing_levels but have MRP."""
    
    # Connect to MongoDB
    mongo_client = AsyncIOMotorClient(settings.MONGODB_URI)
    mongo_db = mongo_client[settings.MONGODB_DB_NAME]
    collection = mongo_db[SCM_CATALOGUE_COLLECTION]
    
    try:
        # Find catalogues without pricing_levels but with MRP
        query = {
            "$and": [
                {"pricing_levels": {"$exists": False}},  # No pricing_levels
                {
                    "$or": [
                        {"pricing.mrp": {"$exists": True, "$ne": None, "$gt": 0}},  # Has MRP in pricing
                        {"mrp": {"$exists": True, "$ne": None, "$gt": 0}}  # Has top-level MRP
                    ]
                },
                {"meta.status": {"$ne": "Deleted"}}  # Not deleted
            ]
        }
        
        projection = {
            "catalogue_id": 1,
            "catalogue_name": 1,
            "pricing": 1,
            "mrp": 1,
            "pricing_levels": 1
        }
        
        cursor = collection.find(query, projection)
        catalogues = await cursor.to_list(length=None)
        
        logger.info(f"Found {len(catalogues)} catalogues without pricing_levels")
        return catalogues
        
    except Exception as e:
        logger.error(f"Error finding catalogues: {e}")
        raise
    finally:
        mongo_client.close()


async def update_catalogue_pricing_levels(catalogue_id: str, pricing_levels: dict):
    """Update a single catalogue with generated pricing_levels."""
    
    # Connect to MongoDB
    mongo_client = AsyncIOMotorClient(settings.MONGODB_URI)
    mongo_db = mongo_client[settings.MONGODB_DB_NAME]
    collection = mongo_db[SCM_CATALOGUE_COLLECTION]
    
    try:
        # Update the catalogue
        update_result = await collection.update_one(
            {"catalogue_id": catalogue_id},
            {
                "$set": {
                    "pricing_levels": pricing_levels,
                    "meta.updated_at": datetime.utcnow(),
                    "meta.updated_by": "migration_script"
                }
            }
        )
        
        if update_result.modified_count > 0:
            logger.debug(f"Updated pricing_levels for catalogue: {catalogue_id}")
            return True
        else:
            logger.warning(f"No update performed for catalogue: {catalogue_id}")
            return False
            
    except Exception as e:
        logger.error(f"Error updating catalogue {catalogue_id}: {e}")
        raise
    finally:
        mongo_client.close()


async def migrate_pricing_levels():
    """Main migration function to generate pricing_levels for catalogues."""
    logger.info("Starting pricing_levels migration")
    
    try:
        # Find catalogues without pricing_levels
        catalogues = await find_catalogues_without_pricing_levels()
        
        if not catalogues:
            logger.info("No catalogues found that need pricing_levels migration")
            return
        
        updated_count = 0
        skipped_count = 0
        error_count = 0
        
        for catalogue in catalogues:
            try:
                catalogue_id = catalogue["catalogue_id"]
                catalogue_name = catalogue.get("catalogue_name", "Unknown")
                
                # Extract MRP from pricing or top-level
                mrp = None
                if "pricing" in catalogue and catalogue["pricing"]:
                    mrp = catalogue["pricing"].get("mrp")
                if not mrp and "mrp" in catalogue:
                    mrp = catalogue["mrp"]
                
                if not mrp or mrp <= 0:
                    logger.warning(f"Skipping catalogue {catalogue_id} - no valid MRP found")
                    skipped_count += 1
                    continue
                
                # Generate pricing_levels
                pricing_levels = generate_pricing_levels(mrp)
                
                if not pricing_levels:
                    logger.warning(f"Skipping catalogue {catalogue_id} - could not generate pricing_levels")
                    skipped_count += 1
                    continue
                
                # Update catalogue
                success = await update_catalogue_pricing_levels(catalogue_id, pricing_levels)
                
                if success:
                    updated_count += 1
                    logger.info(f"✅ Updated {catalogue_name} (ID: {catalogue_id}) - MRP: ₹{mrp}")
                else:
                    skipped_count += 1
                    
            except Exception as e:
                error_count += 1
                logger.error(f"Error processing catalogue {catalogue.get('catalogue_id', 'unknown')}: {e}")
        
        # Summary
        logger.info("=" * 60)
        logger.info("MIGRATION SUMMARY")
        logger.info("=" * 60)
        logger.info(f"Total catalogues found: {len(catalogues)}")
        logger.info(f"Successfully updated: {updated_count}")
        logger.info(f"Skipped: {skipped_count}")
        logger.info(f"Errors: {error_count}")
        logger.info("=" * 60)
        
        if updated_count > 0:
            logger.info("✅ Migration completed successfully!")
        else:
            logger.warning("⚠️ No catalogues were updated")
            
    except Exception as e:
        logger.error(f"Migration failed: {e}")
        raise


async def preview_migration():
    """Preview what the migration would do without making changes."""
    logger.info("PREVIEW MODE - No changes will be made")
    logger.info("=" * 60)
    
    try:
        catalogues = await find_catalogues_without_pricing_levels()
        
        if not catalogues:
            logger.info("No catalogues found that need pricing_levels migration")
            return
        
        logger.info(f"Found {len(catalogues)} catalogues that would be updated:")
        logger.info("")
        
        for i, catalogue in enumerate(catalogues[:10], 1):  # Show first 10
            catalogue_id = catalogue["catalogue_id"]
            catalogue_name = catalogue.get("catalogue_name", "Unknown")
            
            # Extract MRP
            mrp = None
            if "pricing" in catalogue and catalogue["pricing"]:
                mrp = catalogue["pricing"].get("mrp")
            if not mrp and "mrp" in catalogue:
                mrp = catalogue["mrp"]
            
            logger.info(f"{i}. {catalogue_name}")
            logger.info(f"   ID: {catalogue_id}")
            logger.info(f"   MRP: ₹{mrp if mrp else 'Not found'}")
            
            if mrp and mrp > 0:
                # Show what pricing_levels would be generated
                pricing_levels = generate_pricing_levels(mrp)
                if pricing_levels:
                    logger.info("   Generated pricing levels:")
                    for level, data in pricing_levels["levels"].items():
                        logger.info(f"     {level}: ₹{data['cost_price']} (MRP-{PRICING_MARGINS[level]['discount_pct']}%)")
            logger.info("")
        
        if len(catalogues) > 10:
            logger.info(f"... and {len(catalogues) - 10} more catalogues")
        
        logger.info("=" * 60)
        logger.info("Run without --preview flag to execute the migration")
        
    except Exception as e:
        logger.error(f"Preview failed: {e}")
        raise


async def main():
    """Main function with preview option."""
    import sys
    
    if "--preview" in sys.argv:
        await preview_migration()
    else:
        await migrate_pricing_levels()


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