<?php
    $id = intval($_POST['id']);
    $sale = $db->get_rowData('sale','id',$id);
    if(empty($sale)){$error=fl();setMessage(63,'sale');}
    else{
        $product_data = $db->selectAll('sale_products','where sale_id='.$id);
        $jArray[fl()]=$product_data;
        $general->arrayIndexChange($product_data,'product_id');
        $customer = $smt->customerInfoByID($sale['customer_id']);
        $sale['customer'] = $customer['name'].'('.$customer['code'].')';
        $sale['date'] = $general->make_date($sale['date'],'st');
        $sale['collection_date'] = $general->make_date($sale['collection_date'],'st');
        $prodcut_datails=[];
        if(!empty($product_data)){
            $prodcuts = $db->selectAllByID('products','id',array_keys($product_data));
            $unit_ids = [];
            foreach($prodcuts as $p){
                $unit_ids[$p['unit_id']] = $p['unit_id'];
            }
            $units = $db->selectAllByID('unit','id',$unit_ids);
            foreach($product_data as $product_id=> $pd){
                $p = $prodcuts[$product_id];
                $prodcut_datails[$product_id]=$pd;
                $prodcut_datails[$product_id]['product']=$p['code'].' '.$p['title'];
                $prodcut_datails[$product_id]['unit']=$units[$p['unit_id']]['title']??'';
            }
        }
        $sale_truk=$db->selectAll('sale_truk','where sale_id='.$id);
        $gAr['truk']=[];
        foreach($sale_truk as $pt){
            $truk=$smt->truk_info_by_id($pt['truk_id']);
            $gAr['truk'][]=[
                'truk'	=> $truk['title'],
                'pay_by'=> $pt['pay_by'],
                'rent'	=> $general->numberFormat($pt['rent'])
            ];
        }

        $sale_additional_cost=$db->selectAll('sale_additional_cost','where sale_id='.$id);
        $gAr['ade']=[];
        foreach($sale_additional_cost as $pt){
            $truk=$smt->truk_info_by_id($pt['truk_id']);
            $ade=$smt->additional_cost_info_by_id($pt['additional_expense_id']);
            $gAr['ade'][]=[
                'truk'	=> $truk['title'],
                'title'	=> $ade['title'],
                'pay_by'=> $pt['pay_by'],
                'amount'=> $general->numberFormat($pt['amount'])
            ];
        }
        $jArray[fl()]=$gAr;
        $gAr['sale'] = $sale;
        $gAr['sale_data']=$general->getJsonFromString($sale['data']);
        //$jArray[fl()]=$gAr
        $gAr['product_details'] = $prodcut_datails;
        $jArray['html']     = $general->fileToVariable(__DIR__.'/salse_details_view.phtml');
        $jArray['status'] = 1;
    }