<?php require_once('Connections/lancrox.php'); ?>
<? 
$dat=date('Y-m-d');
$grandchild1 = "select office,
        SUM(case when item = 'VITAL 2MM' then inputs end) m2,
    SUM(case when item = 'VITAL 3MM' then inputs end) m3,
    SUM(case when item = 'VITAL 4MM' then inputs end) m4,
	    SUM(case when item = 'VITAL 6MM' then inputs end) m6,
    SUM(case when item = 'VITAL 9MM' then inputs end) m9,
     SUM(case when item = 'VITAL 3MM' then outputs end) out3,
    SUM(case when item = 'VITAL 2MM' then outputs end) out2,
    SUM(case when item = 'VITAL 4MM' then outputs end) out4,
		    SUM(case when item = 'VITAL 6MM' then outputs end) out6,
    SUM(case when item = 'VITAL 9MM' then outputs end) out9
from agwa
 WHERE office='Ware House' or office='Camp 74' GROUP BY office";
 
 $rst_grandchild1=mysqli_query($lancrox, $grandchild1);
$num_grandchild1=mysqli_num_rows($rst_grandchild1); 
$grandarray1=array();

while ($grandrow1 = mysqli_fetch_assoc($rst_grandchild1)){
$grandarray1[]=$grandrow1 ;
}
$aa2 =$grandarray1[0]['m2']-$grandarray1[0]['out2'];
$ba2 =$grandarray1[1]['m2']-$grandarray1[1]['out2'];
$aa3 =$grandarray1[0]['m3']-$grandarray1[0]['out3'];
$ba3 =$grandarray1[1]['m3']-$grandarray1[1]['out3'];
$aa4 =$grandarray1[0]['m4']-$grandarray1[0]['out4'];
$ba4 =$grandarray1[1]['m4']-$grandarray1[1]['out4'];
$aa6 =$grandarray1[0]['m6']-$grandarray1[0]['out6'];
$ba6 =$grandarray1[1]['m6']-$grandarray1[1]['out6'];
$aa9 =$grandarray1[0]['m9']-$grandarray1[0]['out9'];
$ba9 =$grandarray1[1]['m9']-$grandarray1[1]['out9'];
$aoffice =$grandarray1[0]['office'];
$boffice =$grandarray1[1]['office'];

 $send="INSERT INTO leftb(a2,a3,a4,a6,a9,station,items,dd) VALUES ('$aa2','$aa3','$aa4','$aa6','$aa9','$aoffice','Vitals','$dat'),('$ba2','$ba3','$ba4','$ba6','$ba9','$boffice','Vitals','$dat')";
 mysqli_query($lancrox, $send) or die (mysqli_error($GLOBALS["___mysqli_ston"]));
?>
 