File size: 11,395 Bytes
edf6940
 
 
 
 
 
 
75f5dad
 
edf6940
 
 
7480421
75f5dad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a05cc02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
edf6940
 
 
 
 
 
 
8ba1576
edf6940
 
 
 
75f5dad
 
 
 
 
 
 
 
 
 
 
 
edf6940
 
 
75f5dad
 
edf6940
 
 
75f5dad
 
 
 
 
8ba1576
edf6940
 
8ba1576
bdfbcee
8ba1576
75f5dad
8ba1576
bdfbcee
 
 
 
 
75f5dad
b60320f
edf6940
8ba1576
 
 
 
 
 
bdfbcee
 
 
 
edf6940
 
 
 
 
 
 
 
 
8ba1576
edf6940
 
 
8ba1576
edf6940
 
 
 
 
 
 
 
 
 
 
 
8ba1576
 
 
 
edf6940
 
8ba1576
 
 
7480421
 
 
8ba1576
 
 
 
 
 
 
 
 
 
75f5dad
 
 
7480421
 
 
 
 
 
 
75f5dad
 
 
 
 
 
 
 
 
7480421
 
 
 
75f5dad
7480421
 
 
 
75f5dad
 
7480421
 
 
edf6940
8ba1576
75f5dad
 
 
7480421
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75f5dad
7480421
75f5dad
7480421
 
 
75f5dad
7480421
 
 
 
75f5dad
 
7480421
 
b430085
8ba1576
 
75f5dad
 
edf6940
8ba1576
 
edf6940
 
 
 
 
 
 
 
 
8ba1576
 
 
 
 
 
 
 
 
 
edf6940
8ba1576
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
"""Smart warehouse stock finder using GPT-5's intelligence"""

import requests
import re
import os
import json

def get_warehouse_stock_smart(user_message, previous_result=None):
    """Let GPT-5 intelligently find products or filter by warehouse"""
    
    OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
    
    # Check if user is asking about specific warehouse
    warehouse_keywords = {
        'caddebostan': 'Caddebostan',
        'ortaköy': 'Ortaköy', 
        'ortakoy': 'Ortaköy',
        'alsancak': 'Alsancak',
        'izmir': 'Alsancak',
        'bahçeköy': 'Bahçeköy',
        'bahcekoy': 'Bahçeköy'
    }
    
    user_lower = user_message.lower()
    asked_warehouse = None
    for keyword, warehouse in warehouse_keywords.items():
        if keyword in user_lower:
            asked_warehouse = warehouse
            break
    
    # Get XML data with retry
    xml_text = None
    for attempt in range(3):  # Try 3 times
        try:
            url = 'https://video.trek-turkey.com/bizimhesap-warehouse-xml-b2b-api-v2.php'
            timeout_val = 10 + (attempt * 5)  # Increase timeout on each retry: 10, 15, 20
            response = requests.get(url, verify=False, timeout=timeout_val)
            xml_text = response.text
            print(f"DEBUG - XML fetched: {len(xml_text)} characters (attempt {attempt+1})")
            break
        except requests.exceptions.Timeout:
            print(f"XML fetch timeout (attempt {attempt+1}/3, timeout={timeout_val}s)")
            if attempt == 2:
                print("All attempts failed - timeout")
                return None
        except Exception as e:
            print(f"XML fetch error: {e}")
            return None
    
    # Extract just product blocks to reduce token usage
    product_pattern = r'<Product>(.*?)</Product>'
    all_products = re.findall(product_pattern, xml_text, re.DOTALL)
    
    # Create a simplified product list for GPT
    products_summary = []
    for i, product_block in enumerate(all_products):
        name_match = re.search(r'<ProductName><!\[CDATA\[(.*?)\]\]></ProductName>', product_block)
        variant_match = re.search(r'<ProductVariant><!\[CDATA\[(.*?)\]\]></ProductVariant>', product_block)
        
        if name_match:
            # Check warehouse stock for this product
            warehouses_with_stock = []
            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:
                    if int(wh_stock.strip()) > 0:
                        warehouses_with_stock.append(wh_name)
                except:
                    pass
            
            product_info = {
                "index": i,
                "name": name_match.group(1),
                "variant": variant_match.group(1) if variant_match else "",
                "warehouses": warehouses_with_stock
            }
            products_summary.append(product_info)
    
    # If user is asking about specific warehouse, include that in prompt
    warehouse_filter = ""
    if asked_warehouse:
        warehouse_filter = f"\nIMPORTANT: User is asking specifically about {asked_warehouse} warehouse. Only return products available in that warehouse."
    
    # Let GPT-5 find ALL matching products
    smart_prompt = f"""User is asking: "{user_message}"

Find ALL products that match this query from the list below.
If user asks about specific size (S, M, L, XL, XXL, SMALL, MEDIUM, LARGE, X-LARGE), return only that size.
If user asks generally (without size), return ALL variants of the product.
{warehouse_filter}

IMPORTANT BRAND RULES:
- GOBIK: Spanish textile brand we import. When user asks about "gobik", return ALL products with "GOBIK" in the name.
- Product names may contain brand + model + color + type (e.g., "GOBIK SELKIE UNISEX OATMEAL YAĞMURLUK")
- Match partial names: "gobik yağmurluk" should find all GOBIK raincoats, "gobik selkie" should find all SELKIE models

Products list (with warehouse availability):
{json.dumps(products_summary, ensure_ascii=False, indent=2)}

Return index numbers of ALL matching products as comma-separated list (e.g., "5,8,12,15").
If no products found, return: -1

Examples:
- "madone sl 6 var mı" -> Return ALL Madone SL 6 variants (all sizes)
- "madone sl 6 S beden" -> Return only S size variant
- "alsancak şubede hangi boy var" -> Return only products available in ALSANCAK warehouse
- "gobik ürünleri" -> Return ALL products with GOBIK in name
- "gobik yağmurluk" -> Return all GOBIK products that are raincoats (YAĞMURLUK)
- "gobik large" -> Return all GOBIK products in LARGE/L/X-LARGE size"""

    headers = {
        "Content-Type": "application/json",
        "Authorization": f"Bearer {OPENAI_API_KEY}"
    }
    
    payload = {
        "model": "gpt-5-chat-latest",
        "messages": [
            {"role": "system", "content": "You are a product matcher. Find ALL matching products. Return only index numbers."},
            {"role": "user", "content": smart_prompt}
        ],
        "temperature": 0,
        "max_tokens": 100
    }
    
    try:
        response = requests.post(
            "https://api.openai.com/v1/chat/completions",
            headers=headers,
            json=payload,
            timeout=10
        )
        
        if response.status_code == 200:
            result = response.json()
            indices_str = result['choices'][0]['message']['content'].strip()
            
            if indices_str == "-1":
                return ["Ürün bulunamadı"]
            
            try:
                # Parse multiple indices
                indices = [int(idx.strip()) for idx in indices_str.split(',')]
                
                # Collect all matching products
                all_variants = []
                warehouse_stock = {}
                
                for idx in indices:
                    if 0 <= idx < len(all_products):
                        product_block = all_products[idx]
                        
                        # Get product name and variant
                        name_match = re.search(r'<ProductName><!\[CDATA\[(.*?)\]\]></ProductName>', product_block)
                        variant_match = re.search(r'<ProductVariant><!\[CDATA\[(.*?)\]\]></ProductVariant>', product_block)
                        
                        if name_match:
                            product_name = name_match.group(1)
                            variant = variant_match.group(1) if variant_match else ""
                            
                            # Track this variant
                            variant_info = {
                                'name': product_name,
                                'variant': variant,
                                'warehouses': []
                            }
                            
                            # Get warehouse stock
                            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:
                                        display_name = format_warehouse_name(wh_name)
                                        variant_info['warehouses'].append({
                                            'name': display_name,
                                            'stock': stock
                                        })
                                        
                                        # Track total stock per warehouse
                                        if display_name not in warehouse_stock:
                                            warehouse_stock[display_name] = 0
                                        warehouse_stock[display_name] += stock
                                except:
                                    pass
                            
                            if variant_info['warehouses']:  # Only add if has stock
                                all_variants.append(variant_info)
                
                # Format result
                result = []
                
                if asked_warehouse:
                    # Filter for specific warehouse
                    warehouse_variants = []
                    for variant in all_variants:
                        for wh in variant['warehouses']:
                            if asked_warehouse in wh['name']:
                                warehouse_variants.append({
                                    'name': variant['name'],
                                    'variant': variant['variant'],
                                    'stock': wh['stock']
                                })
                    
                    if warehouse_variants:
                        result.append(f"{format_warehouse_name(asked_warehouse)} mağazasında mevcut:")
                        for v in warehouse_variants:
                            result.append(f"• {v['name']} ({v['variant']}): {v['stock']} adet")
                    else:
                        result.append(f"{format_warehouse_name(asked_warehouse)} mağazasında bu ürün mevcut değil")
                else:
                    # Show all variants and warehouses
                    if all_variants:
                        result.append(f"Bulunan {len(all_variants)} varyant:")
                        
                        # Show ALL variants (not just first 5)
                        for variant in all_variants:
                            variant_text = f" ({variant['variant']})" if variant['variant'] else ""
                            result.append(f"• {variant['name']}{variant_text}")
                        
                        result.append("")
                        result.append("Mağaza stok durumu:")
                        for warehouse, total_stock in sorted(warehouse_stock.items()):
                            result.append(f"• {warehouse}: {total_stock} adet")
                    else:
                        result.append("Hiçbir mağazada stok yok")
                
                return result
                
            except (ValueError, IndexError) as e:
                print(f"DEBUG - Error parsing indices: {e}")
                return None
        else:
            print(f"GPT API error: {response.status_code}")
            return None
            
    except Exception as e:
        print(f"Error calling GPT: {e}")
        return None

def format_warehouse_name(wh_name):
    """Format warehouse name nicely"""
    if "CADDEBOSTAN" in wh_name:
        return "Caddebostan mağazası"
    elif "ORTAKÖY" in wh_name:
        return "Ortaköy mağazası"
    elif "ALSANCAK" in wh_name:
        return "İzmir Alsancak mağazası"
    elif "BAHCEKOY" in wh_name or "BAHÇEKÖY" in wh_name:
        return "Bahçeköy mağazası"
    else:
        return wh_name.replace("MAGAZA DEPO", "").strip()