File size: 3,907 Bytes
c8f893c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Ultra simple and fast warehouse stock finder"""

def get_warehouse_stock(product_name):
    """Find warehouse stock FAST - no XML parsing, just regex"""
    try:
        import re
        import requests
        
        # Get XML
        url = 'https://video.trek-turkey.com/bizimhesap-warehouse-xml-b2b-api-v2.php'
        response = requests.get(url, verify=False, timeout=7)
        xml_text = response.text
        
        # Turkish normalize
        def normalize(text):
            tr_map = {'ı': 'i', 'ğ': 'g', 'ü': 'u', 'ş': 's', 'ö': 'o', 'ç': 'c', 'İ': 'i', 'I': 'i'}
            text = text.lower()
            for tr, en in tr_map.items():
                text = text.replace(tr, en)
            return text
        
        # Parse query
        query = normalize(product_name.strip()).replace('(2026)', '').replace('(2025)', '').strip()
        words = query.split()
        
        # Find size
        sizes = ['s', 'm', 'l', 'xl', 'xs', 'xxl', 'ml']
        size = next((w for w in words if w in sizes), None)
        product_words = [w for w in words if w not in sizes and w not in ['beden', 'size', 'boy']]
        
        # Build search pattern
        if 'madone' in product_words and 'sl' in product_words and '6' in product_words:
            pattern = 'MADONE SL 6 GEN 8'
        else:
            pattern = ' '.join(product_words).upper()
        
        print(f"DEBUG - Searching: {pattern}, Size: {size}")
        
        # Search for product + variant combo
        if size:
            # Direct search for product with specific size
            size_pattern = f'{size.upper()}-'
            
            # Find all occurrences of the product name
            import re
            product_regex = f'<Product>.*?<ProductName><!\\[CDATA\\[{re.escape(pattern)}\\]\\]></ProductName>.*?<ProductVariant><!\\[CDATA\\[{size_pattern}.*?\\]\\]></ProductVariant>.*?</Product>'
            
            match = re.search(product_regex, xml_text, re.DOTALL)
            
            if match:
                product_block = match.group(0)
                print(f"DEBUG - Found product with {size_pattern} variant")
                
                # Extract warehouses
                warehouse_info = []
                warehouse_regex = r'<Warehouse>.*?<Name><!\\[CDATA\\[(.*?)\\]\\]></Name>.*?<Stock>(.*?)</Stock>.*?</Warehouse>'
                warehouses = re.findall(warehouse_regex, product_block, re.DOTALL)
                
                for wh_name, wh_stock in warehouses:
                    try:
                        stock = int(wh_stock.strip())
                        if stock > 0:
                            # Format name
                            if "CADDEBOSTAN" in wh_name:
                                display = "Caddebostan mağazası"
                            elif "ORTAKÖY" in wh_name:
                                display = "Ortaköy mağazası"
                            elif "ALSANCAK" in wh_name:
                                display = "İzmir Alsancak mağazası"
                            elif "BAHCEKOY" in wh_name or "BAHÇEKÖY" in wh_name:
                                display = "Bahçeköy mağazası"
                            else:
                                display = wh_name
                            
                            warehouse_info.append(f"{display}: Mevcut")
                    except:
                        pass
                
                return warehouse_info if warehouse_info else ["Hiçbir mağazada mevcut değil"]
            else:
                print(f"DEBUG - No {size_pattern} variant found for {pattern}")
                return ["Hiçbir mağazada mevcut değil"]
        else:
            # No size filter - get all stock
            return ["Beden bilgisi belirtilmedi"]
            
    except Exception as e:
        print(f"Error: {e}")
        return None