<?php require_once('../../includes/connection.php');
require_once(__DIR__ . '/reportListHelpers.php');
$clientnumber='';
$chains='';
$items='';
$itemlist='';
$link='';
$arrays=array();
$arrays['catchall']=array();
$clientnumber=$_REQUEST['clientnumber'];
$chains=json_decode($_REQUEST['chainlist']);
$items=$_REQUEST['items'];
$itemlist = delivery_report_list_tokens($items);
$outputmode=$_REQUEST['mode'];
foreach($chains as $chain){
$link=$chain;
$arrays[$link]=array();
foreach($itemlist as $item){
$itemBreakdown=explode("_",$item);
$itemData=$itemBreakdown[0];
$itemLink=$itemBreakdown[1];
if($itemLink==$chain){
    if(!in_array($itemData,$arrays[$link])){$arrays[$link][]=$itemData;}
}
if($itemLink=='undefined'){
    if(!in_array($itemData,$arrays['catchall'])){$arrays['catchall'][]=$itemData;}
}
}
}

$output = array();
$outputZip = array();

// REPORT COVER GENERATION

$_REQUEST['mode']="split";
$split='../';
$_REQUEST['src']='ext4'; require_once('../global.php'); require_once('../../includes/master/form_functions.php');
$pmode=7;
if($pmode==6){include($split.'../packages/mpd/mpdf.php');}
if($pmode==7){require_once $split.'../packages/mpd/vendor/autoload.php';}

function getPssrNo($itemnumber){
$stmt = $GLOBALS['con']->prepare("SELECT `wsnumber` FROM `wsitems` WHERE `itemnumber`=?")or die($error=mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$itemnumber);$stmt->execute();$res=$stmt->get_result();$stmt->close();
$d=mysqli_fetch_array($res);
if(empty($d['wsnumber'])){return 'None found';}
else{return $d['wsnumber'];}
}

$coverStamp = time().'_'.random_int(1000, 9999);
foreach($arrays as $name=>$array){
    $coverName = preg_replace('/[^A-Za-z0-9_-]/', '_', (string)$name);
    if($coverName === ''){$coverName = 'cover';}
    $coverFile = 'report_cover_'.preg_replace('/[^0-9]/', '', (string)$_REQUEST['clientnumber']).'_'.$coverStamp.'_'.$coverName.'.pdf';
$_REQUEST['items']='';
$_REQUEST['items']=$array;
$_POST['cover_file']=$coverFile;
$_REQUEST['cover_file']=$coverFile;
$_POST['cover_title']=($name === 'catchall') ? 'Unchained' : (string)$name;
$_REQUEST['cover_title']=$_POST['cover_title'];
include('../genCover.php');
$coverData =  array (
'source' => 'output/reports/'.$_REQUEST['clientnumber'],
'file' => $coverFile,
'type' => 'rc'
);
array_push($output,$coverData );


// SCHEDULE CHECK AND ATTACH

if($_REQUEST['schMode']=="attach"){
    include('../genShed.php');
    $schedData =  array (
    'source' => 'output/reports/'.$_REQUEST['clientnumber'],
    'file' => $_REQUEST['clientnumber'].'_schedule.pdf',
    'type' => 'sch'
    );
    if(!in_array($schedData,$output)){array_push($output,$schedData );}
    if(!in_array($schedData,$outputZip)){array_push($outputZip,$schedData );}

}

// WS CHECK AND ATTACH

if($_REQUEST['wsMode']=="attach"){
    foreach($array as $value){
    $item= explode("-",$value); 
    $pssr=getPssrNo($item[1]);
    if($pssr=="None found"){}
    else{
    if(file_exists('../../output/wsfiles/'.$_REQUEST['clientnumber'].'/WS'.$pssr.'.pdf')){  
    $pssrData =  array (
    'source' => 'output/wsfiles/'.$_REQUEST['clientnumber'],
    'file' => 'WS'.$pssr.'.pdf',
    'type' => 'ws'
    );
    if(!in_array($pssrData,$output)){array_push($output,$pssrData );}
  
    else{}
    }
    }
    }
    }

//FILE ATTACHMENT

foreach($array as $value){
$v=explode("-",$value);
$file=$v[0]."_".$v[1].".pdf"; 
if(file_exists('../../output/reports/'.$_REQUEST['clientnumber'].'/'.$file)){
$itemData =  array (
'source' => 'output/reports/'.$_REQUEST['clientnumber'],
'file' => $file,
'type' => 'pdf'
);
array_push($output,$itemData );
}
}



if($outputmode=="pdf"){array_push($output,$itemData );}

if($outputmode=="zip"){
$path='../../output/reports/'.$_REQUEST['clientnumber'].'/';
$wspath='../../output/wsfiles/'.$_REQUEST['clientnumber'].'/';

$theZip=$_REQUEST['clientnumber'].'_'.$name.'.zip';
$zip = new ZipArchive;
if ($zip->open($path.$theZip, ZipArchive::CREATE) === TRUE)
{
if(file_exists($path.$coverFile)){$zip->addFile($path.$coverFile,$coverFile);}

if($_REQUEST['schMode']=="attach"){
    $scheduleFile = $_REQUEST['clientnumber'].'_schedule.pdf';
    if(file_exists($path.$scheduleFile)){
        $zip->addFile($path.$scheduleFile,$scheduleFile);
    }
}

if($_REQUEST['wsMode']=="attach"){
foreach($array as $value){
$item= explode("-",$value); 
$pssr=getPssrNo($item[1]);
if($pssr=="None found"){}
else{
if(file_exists('../../output/wsfiles/'.$_REQUEST['clientnumber'].'/WS'.$pssr.'.pdf')){  
$zip->addFile($wspath.'WS'.$pssr.'.pdf','WS'.$pssr.'.pdf');
}
else{}
}
}
}


foreach($array as $value){
    $v=explode("-",$value);
    $file=$v[0]."_".$v[1].".pdf"; 
    if(file_exists('../../output/reports/'.$_REQUEST['clientnumber'].'/'.$file)){
$zip->addFile($path.$file,$file);

}
}
$zip->close();
$itemData =  array (
    'source' => 'output/reports/'.$_REQUEST['clientnumber'],
    'file' => $theZip,
    'type' => 'zip'
    );
    array_push($outputZip,$itemData );

}

}
}

if($outputmode=="pdf"){echo json_encode($output);}

if($outputmode=="zip"){echo json_encode($outputZip);}

?>
