File size: 4,818 Bytes
4fa4ebe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
import requests
import xml.etree.ElementTree as ET

def test_variant_search(product_name):
    """B2B API'den variant field'ı kontrol et"""
    try:
        warehouse_url = 'https://video.trek-turkey.com/bizimhesap-warehouse-xml.php'
        response = requests.get(warehouse_url, verify=False, timeout=15)
        
        if response.status_code != 200:
            return None
            
        root = ET.fromstring(response.content)
        
        # Turkish character normalization function
        turkish_map = {'ı': 'i', 'ğ': 'g', 'ü': 'u', 'ş': 's', 'ö': 'o', 'ç': 'c', 'İ': 'i', 'I': 'i'}
        
        def normalize_turkish(text):
            import unicodedata
            text = unicodedata.normalize('NFD', text)
            text = ''.join(char for char in text if unicodedata.category(char) != 'Mn')
            for tr_char, en_char in turkish_map.items():
                text = text.replace(tr_char, en_char)
            return text
        
        # Normalize search product name
        search_name = normalize_turkish(product_name.lower().strip())
        search_words = search_name.split()
        
        print(f"DEBUG: Aranan: '{product_name}' -> normalize: '{search_name}' -> kelimeler: {search_words}")
        
        variant_matches = []
        
        # Check variant field for M-TURUNCU like patterns
        for product in root.findall('Product'):
            product_name_elem = product.find('ProductName')
            variant_elem = product.find('Variant')
            
            if product_name_elem is not None and product_name_elem.text:
                xml_product_name = product_name_elem.text.strip()
                
                # Check variant field
                if variant_elem is not None and variant_elem.text:
                    variant_text = variant_elem.text.strip()
                    variant_normalized = normalize_turkish(variant_text.lower().replace('-', ' '))
                    
                    # Check if all search words are in variant field
                    if all(word in variant_normalized for word in search_words):
                        # Check for stock
                        warehouses = product.find('Warehouses')
                        stock_info = []
                        if warehouses is not None:
                            for warehouse in warehouses.findall('Warehouse'):
                                name_elem = warehouse.find('Name')
                                stock_elem = warehouse.find('Stock')
                                
                                if name_elem is not None and stock_elem is not None:
                                    warehouse_name = name_elem.text if name_elem.text else "Bilinmeyen"
                                    try:
                                        stock_count = int(stock_elem.text) if stock_elem.text else 0
                                        if stock_count > 0:
                                            stock_info.append(f"{warehouse_name}: {stock_count}")
                                    except (ValueError, TypeError):
                                        pass
                        
                        variant_matches.append({
                            'product_name': xml_product_name,
                            'variant': variant_text,
                            'variant_normalized': variant_normalized,
                            'stock_info': stock_info
                        })
                        
                        print(f"DEBUG: VARIANT MATCH - {xml_product_name}")
                        print(f"       Variant: {variant_text} -> {variant_normalized}")
                        if stock_info:
                            print(f"       Stok: {', '.join(stock_info)}")
                        else:
                            print(f"       Stokta yok")
        
        return variant_matches
        
    except Exception as e:
        print(f"Hata: {e}")
        return None

if __name__ == "__main__":
    # Test different cases
    test_cases = [
        "M Turuncu",
        "m turuncu", 
        "L Siyah",
        "S Beyaz"
    ]
    
    for test_case in test_cases:
        print(f"\n=== Testing: {test_case} ===")
        result = test_variant_search(test_case)
        
        if result:
            print(f"Toplam {len(result)} variant match bulundu:")
            for i, match in enumerate(result, 1):
                print(f"{i}. {match['product_name']} - {match['variant']}")
                if match['stock_info']:
                    print(f"   Stokta: {', '.join(match['stock_info'])}")
                else:
                    print(f"   Stokta değil")
        else:
            print("Variant match bulunamadı")
        print("-" * 50)