<?php
    $dRange = $_POST['dRange'];
    $general->getFromToFromString($dRange,$from,$to);
    $reportInfo=['Date: '.$dRange];
    $types = $general->get_all_product_type();
    $product_id = intval($_POST['product_id']);
    $type = intval($_POST['type']);
    $zero_column = intval($_POST['zero_column'])==2?2:1;//2=with zero
    $zero_row = intval($_POST['zero_row'])==2?2:1;//2=with zero

    $q=['isActive in(1,0)']; 
    if($type>=0){
        $q[]='type='.$type;
        $reportInfo[]='Type: '.$types[$type]['title']??'';
    }
    if($product_id>0){
        $q[]='id='.$product_id;
    }
    $products = $db->selectAll('products','where '.implode(' and ',$q).' order by code','id,code,title,type,unit_id','array',$jArray);
    $rData=[];
    $sr=1;
    if(!empty($products)){
        $general->arrayIndexChange($products);
        $openings = $db->selectAll('product_stock_log','where product_id in('.implode(',',array_keys($products)).') and action_time < '.$from.' GROUP by product_id','SUM(quantity) as total_quantity, product_id','array',$jArray);
        $jArray[fl()] = $openings;
        $general->arrayIndexChange($openings,'product_id');
        $stock_data = $db->selectAll('product_stock_log','where product_id in('.implode(',',array_keys($products)).') and action_time between '.$from.' and '.$to,'','array',$jArray);
        $product_data=[];
        if(!empty($stock_data)){
            foreach($stock_data as $p){
                $product_id=$p['product_id'];
                if(!isset($product_data[$product_id])){
                    $product_data[$product_id]=[
                        'purchase'=>0,
                        'purchase_return'=>0,
                        'sale'=>0,
                        'sale_return'=>0,
                        'production'=>0,
                        'production_source'=>0,
                        'production_source_main'=>0,
                        'distribute'=>0,
                        'reject'=>0,
                    ];
                }
                if($p['change_type']==ST_CH_PURCHASE){
                    $product_data[$product_id]['purchase']+=$p['quantity'];
                }
                elseif($p['change_type']==ST_CH_PURCHASE_RETURN){
                    $product_data[$product_id]['purchase_return']+=-$p['quantity'];
                }
                elseif($p['change_type']==ST_CH_SALE ){
                    $product_data[$product_id]['sale']+=-$p['quantity'];
                }
                elseif($p['change_type']==ST_CH_SALE_RETURN){
                    $product_data[$product_id]['sale_return']+=$p['quantity'];
                }
                elseif($p['change_type']==ST_CH_PRODUCTION){
                    $product_data[$product_id]['production']+=$p['quantity'];
                }
                elseif($p['change_type']==ST_CH_PRODUCTION_SOURCE){
                    $product_data[$product_id]['production_source']+=-$p['quantity'];
                }
                elseif($p['change_type']==ST_CH_PRODUCTION_SOURCE_MAN){
                    $product_data[$product_id]['production_source_main']+=-$p['quantity'];
                }
                elseif($p['change_type']==ST_CH_DISTRIBUTE){
                    $product_data[$product_id]['distribute']+=-$p['quantity'];
                }
                elseif($p['change_type']==ST_CH_REJECT){
                    $product_data[$product_id]['reject']+=-$p['quantity'];
                }
                else{
                    $jArray[fl()][]=$p['change_type'];
                }
            }
        }
        $jArray[fl()] = $zero_row;

        //$production_costs = $db->selectAll('production_product','where product_id in('.implode(',',array_keys($products)).') and date between '.$from.' and '.$to.' group by product_id','sum(total_cost) as total_cost, product_id');
        //$general->arrayIndexChange($production_costs,'product_id');
        $units=$db->selectAll('unit','where isActive=1');
        $general->arrayIndexChange($units);
        $zero_purchase=true;
        $zero_purchase_return=true;
        $zero_sale=true;
        $zero_sale_return=true;
        $zero_production=true;
        $zero_production_source=true;
        $zero_production_source_main=true;
        $zero_distribute=true;
        $zero_reject=true;
        foreach($products as $p){
            $is_zero_row=true;
            $opening                = intval(@$openings[$p['id']]['total_quantity']);
            $st_data                = $product_data[$p['id']]??[];
            $purchase               = intval($st_data['purchase']??0);
            $purchase_return        = intval($st_data['purchase_return']??0);
            $sale                   = intval($st_data['sale']??0);
            $sale_return            = intval($st_data['sale_return']??0);
            $production             = intval($st_data['production']??0);
            $production_source      = intval($st_data['production_source']??0);
            $production_source_main = intval($st_data['production_source_main']??0);
            $distribute             = intval($st_data['distribute']??0);
            $reject             = intval($st_data['reject']??0);


            if($purchase>0){$zero_purchase=false;$is_zero_row=false;}
            if($purchase_return>0){$zero_purchase_return=false;$is_zero_row=false;}
            if($sale>0){$zero_sale=false;$is_zero_row=false;}
            if($sale_return>0){$zero_sale_return=false;$is_zero_row=false;}
            if($production>0){$zero_production=false;$is_zero_row=false;}
            if($production_source>0){$zero_production_source=false;$is_zero_row=false;}
            if($production_source_main>0){$zero_production_source_main=false;$is_zero_row=false;}
            if($distribute>0){$zero_distribute=false;$is_zero_row=false;}
            if($reject>0){$zero_reject=false;$is_zero_row=false;}
            
            if($zero_row==1&&$is_zero_row==true){//1=without zero
                continue;
            }
            $stock = $opening
            +$purchase
            -$purchase_return
            -$sale
            +$sale_return
            +$production
            -$production_source
            -$production_source_main
            -$reject
            -$distribute;
            //$pr_cost = floatval(@$production_costs[$p['id']]['total_cost']);

            $rData[]=[
                's'     => $sr++,
                't'     => $p['code'].' - '.$p['title'],
                'ty'    => $types[$p['type']]['title']??'',
                'u'     => $units[$p['unit_id']]['title']??'',
                'op'    => $opening,
                'pr'    => $purchase,
                'prr'   => $purchase_return,
                'sl'    => $sale,
                'slr'   => $sale_return,
                'p'     => $production,
                'ps'    => $production_source,
                'psm'   => $production_source_main,
                'dst'   => $distribute,
                'rj'    => $reject,
                'st'    => $stock,
                //'pc'    => $general->numberFormat($pr_cost),
            ];
        }
        $head=[
            ['title'=>"#"                  ,'key'=>'s','hw'=>5],
            ['title'=>"Product"            ,'key'=>'t'],
            ['title'=>"Type"               ,'key'=>'ty'],
            ['title'=>"Unit"               ,'key'=>'u'],
            ['title'=>"Opening"            ,'key'=>'op']
        ];
        if($zero_column==2||$zero_purchase==false){//2=with zero
            $head[]=['title'=>"Purchase"           ,'key'=>'pr'];
        }
        if($zero_column==2||$zero_purchase_return==false){
            $head[]=['title'=>"Purchase Return"    ,'key'=>'prr'];
        }
        if($zero_column==2||$zero_sale==false){
            $head[]=['title'=>"Sale"               ,'key'=>'sl'];
        }
        if($zero_column==2||$zero_sale_return==false){
            $head[]=['title'=>"Sale Return"        ,'key'=>'slr'];
        }
        if($zero_column==2||$zero_production==false){
            $head[]=['title'=>"Production"         ,'key'=>'p'];
        }
        if($zero_column==2||$zero_production_source==false){
            $head[]=['title'=>"Production Source"  ,'key'=>'ps'];
        }
        if($zero_column==2||$zero_production_source_main==false){
            $head[]=['title'=>"Production Main"    ,'key'=>'psm'];
        }
        if($zero_column==2||$zero_distribute==false){
            $head[]=['title'=>"Distribute"    ,'key'=>'dst'];
        }
        if($zero_column==2||$zero_reject==false){
            $head[]=['title'=>"Reject"    ,'key'=>'rj'];
        }
        
        
        $head[]=['title'=>"Closing"              ,'key'=>'st'];
        $fileName='product_report'.TIME.rand(0,999).'.txt';
        $report_data=array(
            'name'      => 'product_report',
            'title'     => 'Product Report',
            'info'      => $reportInfo,
            'fileName'  => $fileName,
            'head'=>$head,
            '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;
}