Current File : /home/users/barii/public_html/finansenl.com.pl/wodki/admin/classes/thee2bk.php
<meta charset="UTF-8">

<?php

include('../config.php');
                        

require_once 'the_api.php'; 

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

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


echo '<pre>';

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('2024-05-15 midnight'));

$zwrot_items = getCustomerReturns($id_konta, $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



$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 = '';
}

$first_query = false;
 if (!$mysql->sqlCheck('zwroty', "orderId = '" . $orderId . "'")) {
    $first_query = true;
    $zwrot_id = $mysql->sqlInsertArray('zwroty', [
        'orderId' => $orderId,
        'status' => $status,
        'createdAt' => $createdAt,
        'login' => $login,
        'email' => $email,
        'carrierId' => $carrierId,
        'waybill' => $waybill,
        'id_konta' => $id_konta
    ], true);
} else {
    $mysql->doQuery("UPDATE zwroty SET status='".$status."', carrierId = '".$carrierId."', waybill='".$waybill."' WHERE order_id = '".$orderId."'");

}


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'];
 

 if ($first_query) {
            $mysql->sqlInsertArray('zwroty_produkty', [
                'zwrot_id' => $zwrot_id,
                'offerId' => $offerId,
                'quantity' => $quantity,
                'name' => $name,
                'price' => $price,
                'url' => $url,
                'reason' => $reason
            ]);
        }



            $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']
            ];
        }
    }



}







if ($offerIdFound && $zwrot_item['status'] == 'DELIVERED') {


    $payment_id  = $checkoutform['payment']['id'];
    //echo $payment_id;
    print_r($refundItems);


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


}


    foreach ($refundItems['lineItems'] as $item_zwrocony) {
        //echo 'wow'.$item_zwrocony['id'];
       print_r(createRefundApplication($accessToken, $item_zwrocony['id'], $item_zwrocony['quantity']));
    }







//$payment_id  = $checkout_item['payment']['id'];

//print_r(initiateRefund($accessToken, $payment_id, $refundItems));

}
//print_r(getCustomerReturnById(0, '97511c0c-31a5-46a2-bdb7-75db47f0514d'));
//$payment_id  = $checkout_item['payment']['id'];
//echo $payment_id;
}

?>