import requests from create_order_in_crm import BASE_URL, CRM_API_KEY from get_order_number_from_crm_by_id import get_order_number from get_orders import get_auth_token, LOGIN, PASSWORD from models import EpicenterOrder, CancelReason, db def get_cancel_reason_name(reason_ukr: str): try: # Query the CancelReason table cancel_reason = ( CancelReason.select() .where(CancelReason.ukr_description.contains(reason_ukr)) .first() ) if cancel_reason: return cancel_reason.name else: return None except Exception as e: print(f"Error: {e}") return None def get_cancel_reason_from_crm(order_id: int): order_url = BASE_URL + f"order/{order_id}" headers = { "accept": "application/json", "Authorization": f"Bearer {CRM_API_KEY}", } order_info_response = requests.get( order_url, headers=headers, params={"include": "custom_fields"} ) # Check if request was successful if order_info_response.status_code == 200: order_info = order_info_response.json() # Convert response to JSON custom_fields = order_info.get("custom_fields", []) # Get custom_fields target_uuid = "OR_1002" # Используем list comprehension для поиска нужного словаря matching_dict = next( (item for item in custom_fields if item.get("uuid") == target_uuid), None ) # Если словарь найден, выводим значение 'value' if matching_dict: methodpay = matching_dict.get("value") return methodpay[0] else: print(f"Не найден словарь с uuid") else: print( f"Failed to retrieve order information. Status code: {order_info_response.status_code}" ) def cancel_order(order_number: int, reason: str): order = EpicenterOrder.get(EpicenterOrder.order_number == order_number) order_id = order.order_id auth_token = get_auth_token(LOGIN, PASSWORD) reason_to_cancel = get_cancel_reason_name(reason) first_headers = { "authority": "core-api.epicentrm.cloud", "accept": "*/*", "accept-language": "en-US,en;q=0.9,ru;q=0.8,ru-RU;q=0.7", "access-control-request-headers": "authorization,content-type", "access-control-request-method": "POST", "origin": "https://admin.epicentrm.com.ua", "referer": "https://admin.epicentrm.com.ua/", "sec-fetch-dest": "empty", "sec-fetch-mode": "cors", "sec-fetch-site": "cross-site", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36", } first_response = requests.options( f"https://core-api.epicentrm.cloud/v2/oms/orders/{order_id}/change-status/to/canceled", headers=first_headers, ) second_headers = { "authority": "core-api.epicentrm.cloud", "accept": "*/*", "accept-language": "en-US,en;q=0.9,ru;q=0.8,ru-RU;q=0.7", "authorization": f"Bearer {auth_token}", "content-type": "application/json", "origin": "https://admin.epicentrm.com.ua", "referer": "https://admin.epicentrm.com.ua/", "sec-ch-ua": '"Not A(Brand";v="99", "Google Chrome";v="121", "Chromium";v="121"', "sec-ch-ua-mobile": "?0", "sec-ch-ua-platform": '"Windows"', "sec-fetch-dest": "empty", "sec-fetch-mode": "cors", "sec-fetch-site": "cross-site", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36", } json_data = { "reason_code": reason_to_cancel, "comment": None, "translationKey": f"order.{reason_to_cancel}", "number": "", "provider": "", } second_response = requests.post( f"https://core-api.epicentrm.cloud/v2/oms/orders/{order_id}/change-status/to/canceled", headers=second_headers, json=json_data, ) def change_status_in_db(order_number: int): with db.atomic(): order = EpicenterOrder.get(EpicenterOrder.order_number == order_number) order.order_status = "canceled" order.save() print(f"Status for order {order_number} changed to canceled.") def cancel_order_and_change_status(order_id: int): order_number = get_order_number(order_id) reason = get_cancel_reason_from_crm(order_id) print(order_number) print(reason) cancel_order(order_number, reason=reason) change_status_in_db(order_number) # cancel_order_and_change_status(171)