<commit_before><?php
require_once('../config.php');
require_once('../db.php');
require_once('../comp.php');

session_start();

//prevent unauthorized access
if (!isset($_SESSION['user_priv']) || 
    !in_array(
        $_SESSION['user_priv'],
        array('Administrator', 'Employee')
    )) {
	http_response_code(302);
	header('Location: ' . BASE_URL . 'login.php');
    echo 'Not Authorized';
    exit();
}

//prevent csrf
$csrf_passed = (
    isset($_SESSION['csrf_token']) &&
    isset($_POST['csrf_token']) &&
    $_SESSION['csrf_token'] == $_POST['csrf_token']
);

if (!isset($_POST['customer_id']) || !isset($_POST['invoice_month'])) {
    http_response_code(500);
    echo "Missing required params";
    exit();
}
//($_REQUEST); echo '<hr/>';

$customer_contact = '';

$min_time = (new DateTime($_POST['invoice_month'] . ' UTC'));
$max_time = (clone $min_time);
$max_time->modify('last day of this month');
$max_time->setTime(23,59,59);
//($min_time); echo '<hr/>';
//($max_time); echo '<hr/>';
$billable_times = get_all_documents(
	'timer',
	array(
		'customer_id'=>$_POST['customer_id'],
		'start_time' => array(
			'$gte' => prepare_datetime($min_time),
			'$lte' => prepare_datetime($max_time)
		)
	)
);

//($billable_times); echo '<hr/>';

function new_line_item($project_id) {
	$project = get_one_document(
		'projects',
		array(
			'_id' => (new MongoId($project_id))
		)
	);
	return array(
		'project_name' => $project['project_name'],
		'notes' => $project['project_notes'],
		'price' => $project['project_price'],
		'quantity' => 0,
		'unit' => 'hour',
	);
}

function seconds_to_hours_rounded($seconds) {
	/*
	 * $quarter_hours = round($seconds / (60*15));
	 * $hours = $quarter_hours/4;
	 */
	return round($seconds/(60*15))/4;
}

$line_items = array();

foreach ($billable_times as $time) {
	$interval = ($time['stop_time']->sec) - ($time['start_time']->sec);
	if (!isset($line_items[$time['project_id']])) {
		$line_items[$time['project_id']] = new_line_item($time['project_id']);
	}
	$line_items[$time['project_id']]['quantity'] += $interval;
}
$rows = "<tr><td>Project</td><td>Note</td><td>Quantity</td><td>Price</td><td>Total</td></tr>";
foreach ($line_items as $item) {
	$item['quantity'] = seconds_to_hours_rounded((float)$item['quantity']);
//	echo $item['project_name'] . " => " . $item['quantity'] . '<br/>';


	$rows .= "<tr><td>${item['project_name']}</td><td>${item['notes']}</td>" .
		"<td>${item['quantity']} ${item['unit']}(s)</td>" .
		"<td>$${item['price']}/${item['unit']}</td>" .
		"<td>$" . (float)$item['price'] * (float)$item['quantity'] . "</td></tr>";
}

echo $customer_contact;

echo "<table style=\"width:80%;text-align:center\">$rows</table>";

?><commit_msg>added total line to invoice.<commit_after><?php
require_once('../config.php');
require_once('../db.php');
require_once('../comp.php');

session_start();

//prevent unauthorized access
if (!isset($_SESSION['user_priv']) || 
    !in_array(
        $_SESSION['user_priv'],
        array('Administrator', 'Employee')
    )) {
	http_response_code(302);
	header('Location: ' . BASE_URL . 'login.php');
    echo 'Not Authorized';
    exit();
}

//prevent csrf
$csrf_passed = (
    isset($_SESSION['csrf_token']) &&
    isset($_POST['csrf_token']) &&
    $_SESSION['csrf_token'] == $_POST['csrf_token']
);

if (!isset($_POST['customer_id']) || !isset($_POST['invoice_month'])) {
    http_response_code(500);
    echo "Missing required params";
    exit();
}
//($_REQUEST); echo '<hr/>';

$customer_contact = '';

$min_time = (new DateTime($_POST['invoice_month'] . ' UTC'));
$max_time = (clone $min_time);
$max_time->modify('last day of this month');
$max_time->setTime(23,59,59);
//($min_time); echo '<hr/>';
//($max_time); echo '<hr/>';
$billable_times = get_all_documents(
	'timer',
	array(
		'customer_id'=>$_POST['customer_id'],
		'start_time' => array(
			'$gte' => prepare_datetime($min_time),
			'$lte' => prepare_datetime($max_time)
		)
	)
);

//($billable_times); echo '<hr/>';

function new_line_item($project_id) {
	$project = get_one_document(
		'projects',
		array(
			'_id' => (new MongoId($project_id))
		)
	);
	return array(
		'project_name' => $project['project_name'],
		'notes' => $project['project_notes'],
		'price' => $project['project_price'],
		'quantity' => 0,
		'unit' => 'hour',
	);
}

function seconds_to_hours_rounded($seconds) {
	/*
	 * $quarter_hours = round($seconds / (60*15));
	 * $hours = $quarter_hours/4;
	 */
	return round($seconds/(60*15))/4;
}

$line_items = array();

foreach ($billable_times as $time) {
	$interval = ($time['stop_time']->sec) - ($time['start_time']->sec);
	if (!isset($line_items[$time['project_id']])) {
		$line_items[$time['project_id']] = new_line_item($time['project_id']);
	}
	$line_items[$time['project_id']]['quantity'] += $interval;
}
$total = 0;
$rows = "<tr><td>Project</td><td>Note</td><td>Quantity</td><td>Price</td><td>Total</td></tr>";
foreach ($line_items as $item) {
	$item['quantity'] = seconds_to_hours_rounded((float)$item['quantity']);
//	echo $item['project_name'] . " => " . $item['quantity'] . '<br/>';

	$sub_total =  (float)$item['price'] * (float)$item['quantity'];
	$total += $sub_total;
	$rows .= "<tr><td>${item['project_name']}</td><td>${item['notes']}</td>" .
		"<td>${item['quantity']} ${item['unit']}(s)</td>" .
		"<td>$${item['price']}/${item['unit']}</td>" .
		"<td>$" . $sub_total . "</td></tr>";
}

echo $customer_contact;

echo "<table style=\"width:80%;text-align:center\">$rows</table>";

echo "<h4>Total: $$total</h4>"

?>
{'fraction_non_alphanumeric': 0.2537124230351322, 'fraction_numerical': 0.009597971749366172, 'mean_word_length': 5.261904761904762, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 99, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5274178', 'n_tokens_mistral': 2159, 'n_tokens_neox': 2031, 'n_words': 305}