<?php

session_start(); 
    $u_uname = '';
    if (isset($_COOKIE['auth_token']))
    {
        $u_uname = $_COOKIE['auth_token'];
    }else{
        header("location:../login.php");
    }
    
    include('../include/connection.php');

    $projId = $_GET['id'];
    $tAmount = $_POST['tAmount'];

    $sql = "INSERT INTO `transaction`(`projectId`, `tAmount`)VALUES('$projId','$tAmount')";

    $result = mysqli_query($link,$sql);
    
    $sql = "SELECT * FROM `project` WHERE srno='$projId'";

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

    if(mysqli_num_rows($result) > 0)
    {
        $project = mysqli_fetch_array($result);
    }

    $prevBalance = $project['balance'];
    $prevPay = $project['recievedPay'];

    $sql = "UPDATE `project` SET `recievedPay`=$prevPay+$tAmount,`balance`=$prevBalance-$tAmount WHERE srno='$projId'";

    $result = mysqli_query($link,$sql);

    $sql = "INSERT INTO `add_expenses`(`amount`, `remark`, `status`, `project`)VALUES('$tAmount', 'Added Income', 'credit', '$projId')";

    $result = mysqli_query($link,$sql);

    if($result){
        header('Location:../allproject.php?status=4');
    }
    
?>