<?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"] == "save_dag") {


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

//
//        $sql = "Insert into batch(coursecode,coursecode2,Year,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday,Registration_Fee,Description,Batch_Code,Registration_No)values 
//        ('" . $_GET['coursecode'] . "','" . $_GET['code2'] . "','" . $_GET['year'] . "','" . $_GET['mon'] . "','" . $_GET['tue'] . "','" . $_GET['wed'] . "','" . $_GET['thu'] . "','" . $_GET['fri'] . "','" . $_GET['sat'] . "','" . $_GET['sun'] . "','" . $_GET['Registration_fee'] . "','" . $_GET['Description'] . "','" . $_GET['Batch_Code'] . "','" . $_GET['Registration_No'] . "') ";
      
        
          $sql = "Insert into dag_item_master(dag_no,description,price,size,remark)values 
                                       ('" . $_GET['dagno'] . "','" . $_GET['Description'] . "','" . $_GET['Price'] . "','" . $_GET['Size'] . "','" . $_GET['Remark'] . "')";
    

        
      
        $result = $conn->query($sql);

        $sql = "SELECT dagcode FROM invpara";
        $resul = $conn->query($sql);
        $row = $resul->fetch();
        $no = $row['dagcode'];
        $no2 = $no + 1;
        $sql = "update invpara set dagcode = $no2 where dagcode = $no";
        $result = $conn->query($sql);
        
        
        $conn->commit();
        echo "Saved";
    } catch (Exception $e) {
        $conn->rollBack();
        echo $e;
    }
}

if ($_GET["Command"] == "getdt") {
    header('Content-Type: text/xml');
    echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";

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

    $sql = "SELECT dagcode FROM invpara";
    $result = $conn->query($sql);
    $row = $result->fetch();
    $no = $row['dagcode'];
    $ResponseXML .= "<id><![CDATA[$no]]></id>";
    $ResponseXML .= "</new>";

    echo $ResponseXML;
}