 
<?php

session_start();


require_once ("connection_sql.php");
header('Content-Type: text/xml');
date_default_timezone_set('Asia/Colombo');
 

if ($_POST["Command"] == "sendinvoice") {
    try {
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $conn->beginTransaction();
        
        if($_POST['cost']==""){
            exit('Enter Cost');
        }
        if($_POST['selling']==""){
            exit('Enter Selling');
        }
        if($_POST['newjobno']==""){
            exit('Enter New Job No');
        }
         if($_POST['remark']==""){
            exit('Enter Remark');
        }
        if($_POST['warrenty']==""){
            exit('Enter Warrenty');
        }
        if($_POST['belt']==""){
            exit('Enter Belt');
        }
        
        $sql = "Select * from dag_item  where id='" . $_POST['id'] . "'";
        $result = $conn->query($sql); 
        if ($rowM = $result->fetch()) {
              $remark=$_POST['remark'];
              
            $sql_invpara = "SELECT * FROM invpara where flag='".$rowM['department']."'";
            $result_invpara = $conn->query($sql_invpara); 
            $row_invpara = $result_invpara->fetch();
           
            $vatrate = $row_invpara["vatrate"];
           
            
            $sql = "UPDATE dag_item set  warrenty='" . $_POST['warrenty'] . "',belt='" . $_POST['belt'] . "',remark='".$rowM['remark']." -".$remark."',flag='5',jobno='" . $_POST['newjobno'] . "',total=total+'" . $_POST['selling'] . "',cost=cost+'" . $_POST['cost'] . "',com_date='" . date("Y-m-d") . "'  where id='" . $_POST['id'] . "'";
            $result = $conn->query($sql); 
            
              $sql1 = "insert into c_bal(REFNO, SDATE, CUSCODE, AMOUNT, BALANCE, Cancell, trn_type, vatrate, totpay,tmp_no,company) values 
            ('" . $rowM['jobno'] . "', '" . date("Y-m-d") . "', '" . $rowM["cuscode"] . "', '" . $_POST['cost']. "', '" . $_POST['cost'] . "',  '0', 'ARN', '" . $vatrate . "', 0, '','" . $rowM['department'] .  "')"; 
          $conn->exec($sql1);
            
            $sqllog = "insert into entry_log(refno, username, docname, trnType, stime, sdate) values
            ('" . $rowM['jobno'] . "', '" . $_SESSION["CURRENT_USER"] . "', 'SEND OTHER TO INVOICE', 'Save', '" . date("Y-m-d H:i:s") . "', '" . date("Y-m-d") . "')";
            $resultlog = $conn->query($sqllog);
            
            
            echo "SEND INVOICE";
        }
        
        $conn->commit();
    } catch (Exception $e) {
        $conn->rollBack();
        echo $e;
    }
}



?>