<?php
$tPTbl  =104;
$q=['isActive in(1,0)'];
$reportInfo=['Date :'.date('d-M-Y')];
$type = intval($_POST['type']);
if($type>=0){
    $q[]='type='.$type;
}
$products=$db->selectAll('products','where '.implode(' and ',$q).' order by title asc');
$units=$db->selectAll('unit');
$general->arrayIndexChange($units,'id');
$types=$general->get_all_product_type();
$tStockAmount=0;
if(!empty($products)){
    $serial=1;
    foreach($products as $p){
        $stockAmount    = 0;
        if($p['stock']>0){
            $stockAmount=round($p['stock']*$p['unit_cost'],2);
        }   
        $tStockAmount   += $stockAmount;
        
        
        $rData[]=[
            's'     => $serial++,
            'c'     => $p['code'],
            't'     => $p['title'],
            'ty'     => $types[$p['type']]['title'],
            'un'    => $units[$p['unit_id']]['title'],
            'bp'    => $p['unit_cost'],
            'sp'    => $p['sale_price'],
            'st'    => $general->numberFormat($p['stock'],0),
            'sa'    => $general->numberFormat($stockAmount),
        ];
    }
}
$rData[]=[
    's'=>'',
    'c'=>['t'=>'Total','b'=>1,'col'=>7],
    't'=>['t'=>false],
    'ty'=>['t'=>false],
    'un'=>['t'=>false],
    'bp'=>['t'=>false],
    'sp'=>['t'=>false], 
    'tc'=>['t'=>false], 
    'st'=>['t'=>false],
    'stD'=>['t'=>false],
    'sa'=>['t'=>$general->numberFormat($tStockAmount),'b'=>1],
];  


$fileName='prdliRep_'.TIME.rand(0,999).'.txt';
$report_data=array(
    'name'      => 'purRep',
    'title'     => 'Products List Report',
    'info'      => $reportInfo,
    'fileName'  => $fileName,
    'head'=>array(
        array('title'=>"SN"             ,'key'=>'s'),
        array('title'=>"Code"          ,'key'=>'c'),
        array('title'=>"Title"          ,'key'=>'t'),
        array('title'=>"Type"          ,'key'=>'ty'),
        array('title'=>"Unit"           ,'key'=>'un'    ,'hw'=>7,'al'=>'r'),
        array('title'=>"Unit cost"      ,'key'=>'bp'    ,'hw'=>10,'al'=>'r'),
        array('title'=>"Sale Price"     ,'key'=>'sp'    ,'hw'=>10,'al'=>'r'),
        array('title'=>"Stock"          ,'key'=>'st'    ,'hw'=>9,'al'=>'r'),
        array('title'=>"Stock Amount"   ,'key'=>'sa'    ,'al'=>'r'),

    ),
    'data'=>$rData
);

$gAr['report_data']= $report_data;
textFileWrite(json_encode($report_data),ROOT_DIR.'/print_file/'.$fileName);
$jArray['html']     = $general->fileToVariable(ROOT_DIR.'/ajax/report.phtml');
$jArray['status']=1;             
?>