Current File : /home/users/barii/public_html/finansenl.com.pl/wodki/admin/classes/zwrot_ajax.php
<?php
include('../config.php');
require('the_api.php');
header('Content-type: application/json');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$reason = isset($_POST['reason']) ? $_POST['reason'] : 'REFUND';
$dataId = isset($_POST['id']) ? $_POST['id'] : '';

for ($id_konta = 0; $id_konta <= 3; $id_konta++) {

$accessToken = file_get_contents('../../api/demo/accessToken'.$id_konta); 


date_default_timezone_set('Europe/Warsaw');

//$currentDateAtMidnight = date('Y-m-d\TH:i:s\Z', strtotime('today midnight'));
$currentDateAtMidnight = date('Y-m-d\TH:i:s\Z', strtotime('-2 months midnight'));

$zwrot_items = getCustomerReturns($accessToken, $currentDateAtMidnight);

foreach ($zwrot_items['customerReturns'] as $zwrot_item) {

$checkoutform = getAllegroCheckoutForm($accessToken, $zwrot_item['orderId']);
$checkout_item_produkty = $checkoutform['lineItems'];



$zwrot_items = $zwrot_item['items']; // Wyciągnięcie elementów zwrotu


// Tablica, do której dodamy skonfigurowane elementy refundacji
$refundItems = [
    "lineItems" => []
];

// Przechodzimy przez każdy produkt w tablicy zwrotów


if ($zwrot_item['orderId'] == $dataId) {

$orderId = $zwrot_item['orderId'];
$status = $zwrot_item['status'];
$createdAt = $zwrot_item['createdAt'];
$login = $zwrot_item['buyer']['login'];
$email = $zwrot_item['buyer']['email'];

if (isset($zwrot_item['parcels'][0]['carrierId'])) {
   $carrierId = $zwrot_item['parcels'][0]['carrierId'];
    $waybill = $zwrot_item['parcels'][0]['waybill']; 
} else {
    $carrierId = '';
    $waybill = '';
}




foreach ($zwrot_items as $item) {
    $offerIdFound = false; // Flag, aby sprawdzić, czy znaleziono dopasowanie


    // Szukamy dopasowania w tablicy produktów kupionych
    foreach ($checkout_item_produkty as $checkout_item) {

        if ($checkout_item['offer']['id'] === $item['offerId']) {

            $offerId = $item['offerId'];
            $quantity = $item['quantity'];
            $name = $item['name'];
            $price = $item['price']['amount'];
            $url = $item['url'];
            $reason = $item['reason']['type'];
 





            $offerIdFound = true;

            // Tworzymy i dodajemy element refundacji z odpowiednim ID
            $refundItems['lineItems'][] = [
                "id" => $checkout_item['id'], // Użyj identyfikatora z 'checkout_item_produkty'
                "type" => 'QUANTITY',
                "quantity" => $item['quantity']
            ];
        }
    }



}










    $payment_id  = $checkoutform['payment']['id'];

    initiateRefund($accessToken, $payment_id, $refundItems['lineItems'], $reason);
    





    foreach ($refundItems['lineItems'] as $item_zwrocony) {
        //zwracanie prowizji
       createRefundApplication($accessToken, $item_zwrocony['id'], $item_zwrocony['quantity']);
    }
    }

    $mysql->doQuery("UPDATE zwroty SET status = 'COMMISSION_REFUNDED' WHERE orderId = '".$orderId."'");
     $responseData = [
            'status' => 'success',
            'success' => 'true',
            'message' => 'Poprawnie wykonano zwrot!',
            'data' => [
                'id' => $dataId,
                'description' => 'Opis produktu dla ID: ' . $dataId
            ]
        ];

}
}
 echo json_encode($responseData);
}
/*
          if (!empty($dataId)) {
        // Załóżmy, że na podstawie ID pobieramy jakieś dane
        // Poniżej przykład statycznych danych
        $responseData = [
            'status' => 'success',
            'message' => 'Poprawnie wykonano zwrot!',
            'data' => [
                'id' => $dataId,
                'description' => 'Opis produktu dla ID: ' . $dataId
            ]
        ];
    } else {
        $responseData = [
            'status' => 'error',
            'message' => 'ID nie zostało przekazane.'
        ];
    }
 echo json_encode($responseData);
 */