<?php

// include_once("../public/con_reedbean.php");
include("../public/impOpreation.php");
require_once('../models/dao/YoutubeLinkDAO.class.php');
require_once('../models/dto/YoutubeLink.class.php');
require_once('../models/mysql/YoutubeLinkMySqlDAO.class.php');
require_once('../models/mysql/ext/YoutubeLinkMySqlExtDAO.class.php');

$youtubeLink = new YoutubeLink();
$youtubeLinkDAO = new YoutubeLinkMySqlDAO();
$youtubeLinkEX = new YoutubeLinkMySqlExtDAO();
$do = $_GET['do'];
$smarty->display("header.html");

if (empty($do)) {
     $projectId = filter_input(INPUT_POST, 'projectId');
    if($projectId > 0){
       // $projectData = R::load('project', $projectId);
       // $smarty->assign("projectData", $projectData); 

       $allproductids = R::getAll("SELECT productid FROM ( 
        SELECT productid FROM projectexchmaterial left join projectexchmaterialdetail on 
        projectexchmaterial.id = projectexchmaterialdetail.projectexchid WHERE projectexchmaterial.projectid = $projectId
        UNION ALL
        SELECT productid FROM projectexchmaterialreturn left join projectexchmaterialdetailreturn on 
        projectexchmaterialreturn.id = projectexchmaterialdetailreturn.projectexchid WHERE projectexchmaterialreturn.projectid = $projectId 
    ) s GROUP BY productid");
    $alldata = array();
    $alltotal = 0;
    $alltotaln = 0;
      foreach($allproductids as $product){
        $projectexchmaterialnum = R::getrow("SELECT sum(pronumber) as pronumbers ,  sum(probuyprice) as probuyprices FROM projectexchmaterial left join projectexchmaterialdetail on 
        projectexchmaterial.id = projectexchmaterialdetail.projectexchid WHERE projectexchmaterial.projectid = $projectId and productid = " . $product['productid']);
        $productdata = R::getrow("SELECT productName FROM product WHERE productid = " . $product['productid']);
        $projectexchmaterialnnum = R::getrow("SELECT sum(pronumber) as pronumbers ,  sum(probuyprice) as probuyprices FROM projectexchmaterialreturn left join projectexchmaterialdetailreturn on 
        projectexchmaterialreturn.id = projectexchmaterialdetailreturn.projectexchid WHERE projectexchmaterialreturn.projectid = $projectId and productid = " . $product['productid']);
        $alltotal += intval($projectexchmaterialnum['pronumbers']) * intval($projectexchmaterialnum['probuyprices']);
        $alltotaln += intval($projectexchmaterialnnum['pronumbers']) * intval($projectexchmaterialnnum['probuyprices']);
        $inside_item = array(
                          "productid" => $product['productid'],
                          "productName" => $productdata['productName'],
                          "pronumber" => intval($projectexchmaterialnum['pronumbers']),
                          "probuyprice" => intval($projectexchmaterialnum['probuyprices']),
                          "pronumbern" => intval($projectexchmaterialnnum['pronumbers']),
                          "probuypricen" => intval($projectexchmaterialnnum['probuyprices'])
                      );


        array_push($alldata, $inside_item);
        
      }


       $smarty->assign("alltotal", $alltotal);
       $smarty->assign("alltotaln", $alltotaln);
       $smarty->assign("alltotals", $alltotal - $alltotaln);
        $smarty->assign("alldata", $alldata);

    }


    $youtubes = $youtubeLinkDAO->queryAll();
    $smarty->assign("youtubes", $youtubes);





    $Programsettingdata = R::getRow('SELECT * FROM `programsettings` WHERE programsettingsid = 1');
    $smarty->assign("Programsettingdata", $Programsettingdata); 
    $smarty->assign("projectValidation", 1); 
    $smarty->display("projectview/reportdetail2.html");
}
$smarty->display("footer.html");



?>