<?php $PageSecurity = 4;
global  $db;
include('includes/session.inc');
include('includes/function_lib.php');
include('includes/stockcalculation.php');

error_reporting(0);


$outputdata=array();

switch ($_REQUEST['form_type']) {
    case 'getRequest':
        if(isset($_REQUEST['to_store']) && !empty($_REQUEST['to_store'])){
            $result =DB_query("SELECT d.id,mat_slip_no 
FROM `material_slip_against_job_card` AS d
LEFT JOIN `transfer_issue` AS t ON(d.id=request_id)
WHERE t.id IS NULL AND from_store='".$_REQUEST['to_store']."'",$db);
            $data=array();
            while($row=DB_fetch_array($result)){
                $data['request_no'][]=array('id'=>$row['id'],'name'=>$row['mat_slip_no']);
            }
            $outputdata['msg']=DB_num_rows($result).' data get';
            $outputdata['code']=DB_num_rows($result);
            $outputdata['data']=$data;
        } else {
            $outputdata['msg']='0 data get';
            $outputdata['code']='0';
            $outputdata['data']=array();
        }

        break;
    case 'getRequestData':
        $function_filter[0]=$_REQUEST['sitecode'];
        $function_filter[1]=$_REQUEST['store'];
        $function_filter[2]=date('Y-m-d');
            $result =DB_query("SELECT mst_itemid,i.StockID,i.item_name,unit.unitname,unit1.unitname AS unitname1,i.mst_unitid,i.conv_unit,i.conv_factor,m.primary_qty
FROM `material_job_item` AS m
INNER JOIN `item_master` AS i ON(m.mst_itemid=i.id)
LEFT JOIN unitsofmeasure AS unit ON(i.mst_unitid=unit.unitid)
LEFT JOIN unitsofmeasure AS unit1 ON(i.conv_unit=unit1.unitid)
WHERE material_slip_id='".$_REQUEST['request_id']."'",$db);
            $data=array();
            while($row=DB_fetch_array($result)){
                $op=openingBalance($function_filter,$row['mst_itemid']);
                $in_1=receivedMRN($function_filter,$row['mst_itemid']);
                $out_1=issueSlip($function_filter,$row['mst_itemid']);
                $out_2=issueSiteToSite($function_filter,$row['mst_itemid']);
                $in_2=receivedSiteToSite($function_filter,$row['mst_itemid']);
                $out_3=issueMatarialReturn($function_filter,$row['mst_itemid']);
                $in_3=receivedReturn($function_filter,$row['mst_itemid']);
                //$out_4=Rgpchallan($function_filter,$myrow['id']);
                //$out_5=Nrgp($function_filter,$myrow['id']);
                //$in_4=Rgpreturn($function_filter,$myrow['id']);
                //$in_5=FinishedMR($function_filter,$myrow['id']);
                $in_6=MaterialConReceived($function_filter,$row['mst_itemid']);
                $out_6=MaterialConIssue($function_filter,$row['mst_itemid']);
                $in_7=PhysicalStockPlus($function_filter,$row['mst_itemid']);
                $out_7=PhysicalStockMinus($function_filter,$row['mst_itemid']);
                $out_8=dispatchIssue($function_filter,$row['mst_itemid']);
                //$out_9=jobWorkChallan57Issue($function_filter,$myrow['id']);
                //$in_8=jobWorkChallan57Received($function_filter,$myrow['id']);
                $in_9=directPurchase($function_filter,$row['mst_itemid']);
                $in_10=saleReturn($function_filter,$row['mst_itemid']);
                $out_10=invoice($function_filter,$row['mst_itemid']);
                $in_11=storereceipt($function_filter,$row['mst_itemid']);
                $out_11=drugissue($function_filter,$row['mst_itemid']);

                $invalue=($op+$in_1+$in_2+$in_3+$in_6 + $in_7 +$in_9 + $in_10+$in_11);
                $outvalue=($out_1+$out_2+$out_3+ $out_6 + $out_7 + $out_8 +$out_10+$out_11);
                $tot=$invalue-$outvalue;
                $stockallitems=$tot;
                $total_current=$tot;

                $data['item_data'][]=array('item_id'=>$row['mst_itemid'],'StockID'=>$row['StockID'],'item_name'=>$row['item_name'],'unitname'=>$row['unitname'],'unitname1'=>$row['unitname1'],'mst_unitid'=>$row['mst_unitid'],'conv_unit'=>$row['conv_unit'],'conv_factor'=>$row['conv_factor'],'total_current'=>$total_current,'primary_qty'=>$row['primary_qty']);
            }
            $outputdata['msg']=DB_num_rows($result).' data get';
            $outputdata['code']=DB_num_rows($result);
            $outputdata['data']=$data;
        break;

    default:

        break;
}
if(!empty($outputdata)){
    echo json_encode($outputdata);
}