Current File : /home/users/barii/public_html/finansenl.com.pl/wodki/admin/server_processing_zwroty.php
<?php

include ('config.php');
$mysql = new MySQL;
/*
 * DataTables example server-side processing script.
 *
 * Please note that this script is intentionally extremely simple to show how
 * server-side processing can be implemented, and probably shouldn't be used as
 * the basis for a large complex system. It is suitable for simple use cases as
 * for learning.
 *
 * See https://datatables.net/usage/server-side for full details on the server-
 * side processing requirements of DataTables.
 *
 * @license MIT - https://datatables.net/license_mit
 */

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Easy set variables
 */
$startDate = isset($_GET['startDate']) ? $_GET['startDate'] : null;
$endDate = isset($_GET['endDate']) ? $_GET['endDate'] : null;


// DB table to use
$table = 'zwroty';

// Table's primary key
$primaryKey = 'id';

function convertDate($data)
{



	 $czas = strtotime($data);
    $teraz = time();
    $roznica = $teraz - $czas;
    $dzien = 60 * 60 * 24;



    	 $miesiace = [
        1 => 'stycznia', 2 => 'lutego', 3 => 'marca',
        4 => 'kwietnia', 5 => 'maja', 6 => 'czerwca',
        7 => 'lipca', 8 => 'sierpnia', 9 => 'września',
        10 => 'października', 11 => 'listopada', 12 => 'grudnia'
    ];

        $dzien = date('j', $czas);
        $miesiac = $miesiace[intval(date('n', $czas))];
        $rok = date('Y', $czas);
        $data_nowa = '<span class="">'.$dzien.' '.$miesiac.' '.$rok.'</span>';
        $godzina = '<span class="">'.date('H:i:s', $czas).'</span>';

        return "{$data_nowa}<br />{$godzina}";
  

}

function getDostawa($data) {
	global $mysql;

	$row = $mysql->sqlRow("dostawa", "all_sprzedaze", "WHERE email = '".$data."'");
	$row2 = $mysql->sqlRow("numer", "all_wysylki", "WHERE email = '".$data."'");	 

	if (isset($row['dostawa'])) {
		$dostawa = $row['dostawa'];
	} else {
		$dostawa = '?';
	}
	if (isset($row2['numer'])) {
		$numer_wysylki = $row2['numer'];
	} else {
		$numer_wysylki = '?';
	}
	return $dostawa.' '.$numer_wysylki;
}



function convertKonto($data) {



	if ($data == 0) {
		$status = '<span class="badge badge-primary">manyak2</span>';
	} elseif ($data == 1) {
		$status = '<span class="badge badge-success">fotomagnesy_pl</span>';
	} elseif ($data == 2) {
		$status = '<span class="badge badge-secondary">prolabel_pl</span>';
	} elseif ($data == 3) {
		$status = '<span class="badge badge-dark tag-pills-sm-m">fotomagnes_pl</span>';
	} else {
		$status = '<span class="badge badge-light badge-sklep-label text-dark">sklep</span>';
	}
	return $status;
}

function convertStatus($row) {



	if ($row['status'] == 'CREATED') {
		$status = '<span class="badge label-pending">oczekiwanie</span>';
	} elseif ($row['status'] == 'IN_TRANSIT') {
		$status = '<span class="badge label-intransit">w drodze</span>';
	} elseif ($row['status'] == 'DELIVERED') {
		$status = '<span class="badge label-releasedfordelivery">dostarczono</span>';
	} elseif ($row['status'] == 'COMMISSION_REFUNDED') {
		$status = '<span class="badge label-availableforpickup">zwrócono środki</span>';
	} elseif ($row['status'] == 'FINISHED') {
		$status = '<span class="badge label-delivered">zakończono</span>';
	} elseif ($row['status'] == 'COMMISSION_REFUND_CLAIMED') {
		$status = '<span class="badge label-issue">zwrócono prowizję</span>';
	} else {
		$status = '<span class="badge badge-light text-dark">oczekiwanie</span>';
	}




	return $status;
}


function convertKwota($row) {
	if ($row['czyfv'] > 0) {
		$fv = '<span class="badge badge-info">FV</span>';
	} else {
		$fv = '';
	}

	return '<p class="m-0 font-success f-w-700">'.$row['kwota'].'zł '.$fv.' '.$message_icon .'</p>';
}


// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
		array( 'db' => 'id',     'dt' => 0 ),
		array(
		'db'        => 'createdAt',
		'dt'        => 1,
		'formatter' => function( $d, $row ) {
			return convertDate($d);
		}
	),
			array(
		'db'        => 'status',
		'dt'        => 2,
		'formatter' => function( $d, $row ) {
			return convertStatus($row);
		}
	),  

	array( 'db' => 'email',     'dt' => 3 ),
	array( 'db' => 'login', 
	    'dt' => 3,
        'formatter' => function($d, $row) {


        	return $row['email'].'<br />'.$row['login'];
          

            // Łączy kolumny 'login' i 'email' z bazy danych
        } ),
	array( 'db' => 'login', 
	    'dt' => 4,
        'formatter' => function($d, $row) {
$mysql = new MySQL;
$produkty = '';

        	$rows2 = $mysql->sqlQuery("SELECT * FROM zwroty_produkty WHERE zwrot_id = '".$row['id']."'");
        	foreach ($rows2 as $item) {
        		$produkty = $produkty. '<br />'.$item['quantity'].'x '.$item['name'].' ('.$item['price'].'zł)';        
        			}
            

        	return $produkty;



            // Łączy kolumny 'login' i 'email' z bazy danych
        } ),
	array(
		'db'        => 'id_konta',
		'dt'        => 5,
		'formatter' => function( $d, $row ) {
			return convertKonto($d);
		}
	),  
		array(
		'db'        => 'orderId',
		'dt'        => 6,
		'formatter' => function( $d, $row ) {

			
			return '<button type="button" data-id="'.$row['orderId'].'" class="wykonaj_zwrot btn btn-primary" >Wykonaj zwrot</button>';

			return convertKonto($d);
		}
	),  
		array( 'db' => 'waybill',     'dt' => 7 ),
	    array(
        'db' => 'carrierId',
        'dt' => 7,
        'formatter' => function($d, $row) {

if($row['carrierId'] == "INPOST" && $row['waybill'] > 0) {
	$link_przesylki = 'https://twoj.inpost.pl/pl/znajdz-przesylke?parcel='.$row['waybill'];
return '<span class="dostawa"><a target="_blank" href="'.$link_przesylki.'">'.$row['carrierId'].'<br /><span class="badge badge-primary numerwysylki noclick">' . $row['waybill'] . '</span></a></span>';
        
} else {
	return '(brak)';
}

          

            // Łączy kolumny 'login' i 'email' z bazy danych
        }
    )





	    
);


$sql_details = array(
    'type' => 'Mysql',
    'user' => 'admin67973_etykiety',
    'pass' => '5BmF1MXif0',
    'db'   => 'baza67973_etykiety',
    'host' => '67973.m.tld.pl'
);

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * If you just want to use the basic configuration for DataTables with PHP
 * server-side, there is no need to edit below this line.
 */

require( 'ssp.class.php' );




$filterValue = isset($_GET['search']['value']) ? $_GET['search']['value'] : '';


$whereConditions = [];

if (isset($_GET['dateRange']) && !empty($_GET['dateRange'])) {
    $dates = explode(' - ', $_GET['dateRange']);
    if (count($dates) === 2) {
        $startDate = DateTime::createFromFormat('d/m/Y', trim($dates[0]))->format('Y-m-d');
        $endDate = DateTime::createFromFormat('d/m/Y', trim($dates[1]))->format('Y-m-d');
        
        if ($startDate === $endDate) {
            // If the start date and end date are the same, adjust the query to cover the whole day
            $whereConditions[] = "(createdAt >= '$startDate 00:00:00' AND createdAt <= '$endDate 23:59:59')";
        } else {
            $whereConditions[] = "createdAt BETWEEN '$startDate' AND '$endDate'";
        }
    }
}

if (!empty($radioFilter)) {
    $whereConditions[] = "id_konta = '$radioFilter'";
}



$whereResult = !empty($whereConditions) ? implode(' AND ', $whereConditions) : null;






echo json_encode(
	SSP::complex($_GET, $sql_details, $table, $primaryKey, $columns, null, $whereResult)
);