<?php require_once('Connections/lancrox.php'); ?>
<? 
$dat=date('Y-m-d');
$grandchild1 = "select office,
	    SUM(case when item = 'Skretting 0.5MM' then inputs end) m_5,
		        SUM(case when item = 'Skretting 0.7MM' then inputs end) m_7,
    SUM(case when item = 'Skretting 1MM' then inputs end) m1,
    SUM(case when item = 'Skretting 1.8MM' then inputs end) m_18,
	    SUM(case when item = 'Wean 1' then inputs end) w1,
    SUM(case when item = 'Wean 2' then inputs end) w2,
        SUM(case when item = 'Skretting 2MM' then inputs end) m2,
    SUM(case when item = 'Skretting 3MM' then inputs end) m3,
    SUM(case when item = 'Skretting 4mm ess' then inputs end) m4,
    SUM(case when item = 'Skretting 4mm sup' then inputs end) m4b,
    SUM(case when item = 'Skretting 6mm ess' then inputs end) m6,
    SUM(case when item = 'Skretting 9MM' then inputs end) m9,
     SUM(case when item = 'Skretting 3MM' then outputs end) out3,
    SUM(case when item = 'Skretting 2MM' then outputs end) out2,
	
     SUM(case when item = 'Wean 1' then outputs end) outw1,
    SUM(case when item = 'Wean 2' then outputs end) outw2,
    SUM(case when item = 'Skretting 4mm ess' then outputs end) out4,
	
    SUM(case when item = 'Skretting 4mm sup' then outputs end) out4b,
		    SUM(case when item = 'Skretting 6mm ess' then outputs end) out6,
    SUM(case when item = 'Skretting 9MM' then outputs end) out9, 
	SUM(case when item = 'Skretting 0.5MM' then outputs end) out_5,
	 SUM(case when item = 'Skretting 0.7MM' then outputs end) out_7,
    SUM(case when item = 'Skretting 1MM' then outputs end) out1,
    SUM(case when item = 'Skretting 1.8MM' then outputs end) out_18
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'];
$aa4b =$grandarray1[0]['m4b']-$grandarray1[0]['out4b'];
$ba4b =$grandarray1[1]['m4b']-$grandarray1[1]['out4b'];
$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'];
$am_5 =$grandarray1[0]['m_5']-$grandarray1[0]['out_5'];
$bm_5 =$grandarray1[1]['m_5']-$grandarray1[1]['out_5'];
$am_7 =$grandarray1[0]['m_7']-$grandarray1[0]['out_7'];
$bm_7 =$grandarray1[1]['m_7']-$grandarray1[1]['out_7'];
$aa1 =$grandarray1[0]['m1']-$grandarray1[0]['out1'];
$ba1 =$grandarray1[1]['m1']-$grandarray1[1]['out1'];
$am_18 =$grandarray1[0]['m_18']-$grandarray1[0]['out_18'];
$bm_18 =$grandarray1[1]['m_18']-$grandarray1[1]['out_18'];
$wa2 =$grandarray1[0]['w2']-$grandarray1[0]['outw2'];
$wb2 =$grandarray1[1]['w2']-$grandarray1[1]['outw2'];
$wa1 =$grandarray1[0]['w1']-$grandarray1[0]['outw1'];
$wb1 =$grandarray1[1]['w1']-$grandarray1[1]['outw1'];

 $send="INSERT INTO leftb(a_5,a_7,a1,a_18,b1,b2,a2,a3,a4,a4b,a6,a9,station,items,dd) VALUES ('$am_5','$am_7','$aa1','$am_18','$wa1','$wa2','$aa2','$aa3','$aa4','$aa4b','$aa6','$aa9','$aoffice','Skretting','$dat'),('$bm_5','$bm_7','$ba1','$bm_18','$wb1','$wb2','$ba2','$ba3','$ba4','$ba4b','$ba6','$ba9','$boffice','Skretting','$dat')";
 mysqli_query($lancrox, $send) or die (mysqli_error($GLOBALS["___mysqli_ston"]));
?>
 