<?php

session_start();

////////////////////////////////////////////// Database Connector /////////////////////////////////////////////////////////////
require_once ("connection_sql.php");

////////////////////////////////////////////// Write XML ////////////////////////////////////////////////////////////////////
header('Content-Type: text/xml');

date_default_timezone_set('Asia/Colombo');

if($_GET["Command"]=="process")
{



	$ResponseXML = "";
	$ResponseXML .= "<salesdetails>";





	$ResponseXML .= "<sales_table><![CDATA[<table class=\"table table-bordered\">
	<tr>
	
	<th style=\"width: 20px;text-align:center\">Id</th>   
	<th style=\"width: 80px;text-align:center\">Invoice No</th>   
	<th style=\"width: 80px;text-align:center\">Invoice Date</th>     
	<th style=\"width: 90px;text-align:center;\" >Item Code</th>
	<th style=\"width: 250px;text-align:center\">Description</th>
	<th style=\"width: 80px;text-align:center\">Cost</th>   

	</tr>";

	$i = 1;
	$sql = "Select * from s_invo where SDATE >='" . $_GET['dtfrom'] . "' and SDATE <='" . $_GET['dtfrom'] . "' and DEPARTMENT='" . $_GET["company"] . "' ";
	if($_GET['brand']  !="All"){
	    $sql .=" and Brand='".$_GET['brand']."'";
	}  
	 $sql .=" order by REF_NO";
	
 	foreach ($conn->query($sql) as $row) {
		
        $id = "id" . $i; 
        $invno = "invno" . $i; 
        $date = "date" . $i; 
        $stk_no = "stk_no" .$i;
        $des = "des" . $i;
        $cost = "cost" . $i;
		
		$ResponseXML .= "<tr>                              
		<td><input class='input-sm form-control' disabled type='text' id='" . $id . "' value='" . $row['id'] . "'></td>	
		<td><input class='input-sm form-control' disabled type='text' id='" . $invno . "' value='" . $row['REF_NO'] . "'></td>
		<td><input class='input-sm form-control' disabled type='text' id='" . $date . "' value='" . $row['SDATE'] . "'></td>
		<td><input class='input-sm form-control' disabled type='text' id='" . $stk_no . "' value='" . $row['STK_NO'] . "'></td>
		<td><input class='input-sm form-control' disabled type='text' id='" . $des . "' value='" . $row['DESCRIPT'] . "'></td>
		<td><input class='input-sm form-control' style='text-align:right'  type='number' id='" . $cost . "' value='" . $row['COST'] . "'></td> 
		</tr>";
		$i = $i + 1;
		
	}
	

	$ResponseXML .= "   </table>]]></sales_table>";



	$ResponseXML .= "<itm_count><![CDATA[" . $i . "]]></itm_count>";

	$ResponseXML .= " </salesdetails>";
	echo $ResponseXML;

}

 
	if ($_POST["Command"] == "save_inv") {





		try {
			$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$conn->beginTransaction();


			$i = 1;

			$count = $_POST['count'];
			while ($_POST["count"] > $i) {	
			 $id = "id" . $i; 
        $invno = "invno" . $i; 
        $date = "date" . $i; 
        $stk_no = "stk_no" .$i;
        $des = "des" . $i;
        $cost = "cost" . $i;

				$sql = "update s_invo set COST ='" . $_POST[$cost] . "' where  id ='" . $_POST[$id] . "'";  	
				$conn->query($sql);	
				$i = $i+1;	
			}
			
			 $sql2 = "insert into entry_log(refno, username, docname, trnType, stime, sdate) values 
			 ('Cost Update', '" . $_SESSION["CURRENT_USER"] . "', 'Cost Update', 'Update', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "')";
            $result2 = $conn->query($sql2);

        	echo "Saved";

			$conn->commit();
		
		} catch (Exception $e) {
			$conn->rollBack();
			echo $e;
		}
	}
 

?>