<?php
header('Access-Control-Allow-Origin:*');
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

// Получаем данные
$data = json_decode(file_get_contents("php://input"));

include_once "Class/function.php";

$arr= json_decode($data -> data,true);

$kag=$arr['kag'];
$param_api=$arr['kag']['param_api'];



$url='https://marketplace.book-online.ru/api/shipment_barcodes/';

//переворачиваем дату для яндекса
$date_query_strtotime=strtotime($arr['date_query']);
$date_query_dmy=date('d-m-Y',$date_query_strtotime);

if($kag['firma']==0){
    $post_data=json_encode(['date'=> $date_query_dmy,'kag'=>$kag['kag']]);

}else{
    $post_data=json_encode(['date'=> $arr['date_query'],'kag'=>$kag['kag']]);
}



$headers = Array("Authorization: fi9tzpIHgp84E1Pf45rMzR/hy8lA2zE5", 'Content-Type: "application/json"');

$data_json=getSSLPage($url,$post_data,$headers);

if(is_numeric($data_json) and $data_json>=400){
    http_code($data_json);
}


include_once "Class/Database.php";
//include_once "Class/DatabasePGSQL.php";
include_once  "Class/MainClass.php";

$database = new Database();
//$database = new Database();//PGSQL
$db = $database->getConnection();

$user=0;

$param=[
    'date_query' => $arr['date_query'],
    'data_json' => $data_json,
    'user' => $user,
    'kag'=> $arr['kag']
 ];

$data=json_decode($data_json,true);

$main_class = new MainClass($db);
//$main_class->WrFile($param);

if($data['error'] and strlen($data['error'])){
    $result=null;

}else{
    $result=$main_class->wrDatabase($param);

    //передается несколько актов
    for($i=0;$i<count(data);$i++){
        $param1=[
            'idoper'=>$data[$i]['id'],
            'kag'=> $arr['kag'],
        ];

        //очень важно------------------>
        $data[$i]['idoper']=$data[$i]['id'];
        $data[$i]['id']=$result['result'][$data[$i]['id']];//id (в нем содержится idoper) меняется на id строки
        $data[$i]['data_collect']=$main_class->DataCollect($param1); //результаты сканирования актов
        $data[$i]['date_query']=$arr['date_query'];
        $data[$i]['date_q']=$date_query_dmy;
        $data[$i]['data_json']=['barcodes'=>$data[$i]['barcodes'],'id'=>$data[$i]['idoper']];
        //unset($data[$i]['barcodes']);
        //<------------------очень важно
    }


}





if(!$result['result']){
    $data_=json_encode(['data_json'=>$data,'server_error'=>$data['error'],'post_data'=>$post_data,'result'=>0,'error'=>$result['error'], 'id'=>0,'date_query'=>$arr['date_query'],'data_collect'=>[],'method'=>$url,'param'=>$param]);
}else{
    $data_=json_encode(['data_json'=>$data,'server_error'=>$data['error'],'post_data'=>$post_data,'result'=>1,'error'=>$result['error'],'id'=>$result['ids'],'idoper'=>$result['idoper'], 'date_query'=>$arr['date_query'],'data_collect'=>$data_collect,'method'=>$url,'param'=>$param]);
}




echo $data_;

