<?php
include('../headertop.php');
include('../db.php');
//include('../datepicker.php');

$disp = '';
$pagetitle = 'Edit Expense Details';

if(!isset($_POST['editexpensesubmit']))
{
echo '<meta http-equiv="refresh" content="0;URL=dailyexpense.php">';
exit;
}

date_default_timezone_set('Asia/Kolkata');

$id = $_POST['id'];
$project = 1;
$name = $_POST['name'];
$work_type = $_POST['work_type'];
$amount = $_POST['amount'];
$date = $_POST['date'];
$particulars = $_POST['particulars'];
$payment_method = $_POST['payment_method'];
$payment_details = $_POST['payment_details'];



if(empty($id) or empty($name) or empty($work_type) or empty($date) or empty($amount) or empty($particulars) or empty($payment_method))
{
$sm = '<span class="error">Please fill all required details</span>';
echo '<meta http-equiv="refresh" content="0;URL=editexpense.php?sm='.$sm.'&id='.$id.'">';
exit;
}

$dateref = date("dmY", strtotime($date));

/*
$qp = "select project_name, substring(project_name,1,15) as proj from project where id='$project'";
$qw = "select work_type, substring(work_type,1,20) as wt from work_type where id='$work_type'";

$rp = mysqli_query($dbcon,$qp) or die(mysqli_error($dbcon));
$rw = mysqli_query($dbcon,$qw) or die(mysqli_error($dbcon));

$rwp = mysqli_fetch_array($rp);
$rww = mysqli_fetch_array($rw);

$reference = $rwp['proj'].$dateref.$rww['wt'];

*/


$q = "update expenses set work_type='$work_type', amount='$amount', date='$date', name='$name', particulars='$particulars', payment_method='$payment_method', payment_details='$payment_details' where id='$id'";

//mysqli_autocommit($dbcon, false);

$res = mysqli_query($dbcon,$q);

if(!$res)
{
$disp .= '<span class="error">Changes could not be saved: '.mysqli_error($dbcon).'</span>';
include('../template.php');
exit;
}
$sm = "Expense details edited";

if(!empty($_FILES['rcptphoto']['type']))
{
$photoname = explode(".",$_FILES['rcptphoto']['name']);
foreach($photoname as $v)
$photoext = $v;

$photolocation = "../files/expenses/".$id."-".$date.".".$photoext;
$photolocationx = "files/expenses/".$id."-".$date.".".$photoext;
$temp = $_FILES['rcptphoto']['tmp_name'];


if(!move_uploaded_file($temp,$photolocation))
$sm .= ' but picture could not be modified';
else
{
$sm .= ' and picture modified';
$qphoto = "update expenses set rcptphoto='$photolocationx' where id='$id'";
mysqli_query($dbcon,$qphoto);
}

}//end of if for checking if student photo exists


echo '<meta http-equiv="refresh" content="0;URL=editexpense.php?sm='.$sm.'&id='.$id.'">';