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

$disp = '';
$pagetitle = 'Add New Expense';

date_default_timezone_set('Asia/Kolkata');

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

$project = 1;
//$floor_no = $_POST['floor_no'];
//$flat_no = $_POST['flat_no'];
$date = $_POST['date'];
$work_type = $_POST['work_type'];
$amount = $_POST['amount'];
$particulars = $_POST['particulars'];
//$comments = $_POST['comments'];
$payment_method=$_POST['payment_method'];
$payment_details = $_POST['payment_details'];
$name = $_POST['name'];


if(empty($date) or empty($name) or empty($work_type) or empty($amount) or empty($particulars) or empty($payment_method))
{
$disp .= '<span class="errordiv">Please go back and fill all required fields</span>';
include('../template.php');
exit;
}

$datedb = $date;
//$datedb = date("Y-m-d", strtotime($date));
$dateref = date("dmY", strtotime($date));

/*
$qid = "select max(id) from expenses";
$resqid = mysqli_query($dbcon,$qid) or die(mysqli_error($dbcon));
$rowqid = mysqli_fetch_array($resqid);
*/

/*
$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'];
*/
/*
if(!is_numeric($no_floors))
{
$disp .= '<span class="errordiv">Number of floors should be numeric value</span>';
include('../template.php');
exit;
}
*/
$q = "insert into expenses (name,project,date, work_type, amount, particulars, payment_method, payment_details) values('$name','$project','$datedb','$work_type','$amount','$particulars','$payment_method', '$payment_details')";
mysqli_query($dbcon,$q) or die(mysqli_error($dbcon));
$eid = mysqli_insert_id($dbcon);

$sm = "Expense information saved ";


if(!empty($_FILES['rcptphoto']['type']))
{
if($_FILES['rcptphoto']['type'] != 'image/jpeg' and $_FILES['rcptphoto']['type'] != 'image/png')
{
$disp .= '<span class="error">Please upload only image files</span>';
include('../template.php');
exit;
}


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

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


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

}//end of if to check if photo exists or not

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

$disp .= $sm."<br><br>";
$disp .= '<br><br><br><BR><a href="printexpensercpt.php?id='.$eid.'" target=_blank>Print Receipt</a><br>';
$disp .= '<br><br><br><BR><a href="emailrcpt.php?id='.$eid.'" target=_blank>Email Receipt</a><br>';
/*
$dispp = '<div class="row">
                <div class="col-md-12">
                    <!-- Advanced Tables -->
                    <div class="panel panel-default">
                        <div class="panel-heading">
                           <b> Expense Details for <b>'.$rww['work_type'].'</b> on: '.$date.' for Project '.$rwp['project_name'].':</b> <br>
                        </div>
                        <div class="panel-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                                    <tbody>
                                            <tr><th>Project</th><td>'.$rwp['project_name'].'</td></tr>
											<tr><th>Work Type</th><td>'.$rww['work_type'].'</td></tr>
											<tr><th>Amount </th><td>'.$amount.'</td></tr>
                                            <tr><th>Particulars</th><td>'.$particulars.'</td></tr>
                                            <tr><th>Reference</th><td>'.$reference.'</td></tr>
											
                                        </tr>
                                    </tbody>
                                                  </table>
                            </div>
                            
                        
                    </div>
                    <!--End Advanced Tables -->
                </div>
            </div>';
			*/
/*
$disp2 = '<form method="post" action="printexpenses.php">
		<input type="hidden" name="dispp" value="'.htmlspecialchars($dispp).'"><input type="submit" value="Print Expense">
		</form>';
		
$disp .= $disp2;
*/

include('../header.php');
include('../template.php');
?>

 <script>
            $(document).ready(function () {
                $('#dataTables-example').dataTable();
            });
    </script>