<td><?php 
echo DeliveryContent::sum('count', array('product_id' => $product->id));
?>
</td>
	</tr>
	<tr>
		<th>Totalt sålt</th>
		<td><?php 
echo TransactionContent::sum('count', array('product_id' => $product->id));
?>
</td>
	</tr>
	<tr>
		<th>Totalt sålt för</th>
		<td><?php 
echo TransactionContent::sum('amount', array('product_id' => $product->id));
?>
</td>
	</tr>
</table>
<div>
	<h2>EAN</h2>
	<img src="/gfx/barcode.php?barcode=<?php 
echo $product->ean;
?>
&amp;width=300" alt="<?php 
echo $product->ean;
?>
" />
</div>
<? if($packages): ?>
echo $category->id;
?>
"><img src="/gfx/edit.png" alt="edit" /></a></td>
				<td class="name_column"><a href="/category/<?php 
echo $category->id;
?>
"><?php 
echo $category->name;
?>
</a></td>
				<td class="numeric total_value_column"><?php 
echo number(Product::sum(array('value', '*', 'count'), array('category_id' => $category->id)));
?>
 kr</td>
				<td class="numeric sales_value"><?php 
echo number($sale = TransactionContent::sum('amount', array('Product.category_id' => $category->id, 'Transaction.timestamp:>' => date('Y-m-d', time() - 60 * 60 * 24 * 30))));
?>
 kr</td>
				<td class="numeric sales_revenue"><?php 
echo number($revenue = $category->revenue(date('Y-m-d', time() - 60 * 60 * 24 * 30)));
?>
 kr</td>
				<td class="numeric revenue_p"><?php 
echo $sale != 0 ? number($revenue / $sale * 100) : '';
?>
 %</td>
			</tr>
		<? endforeach ?>
	</tbody>
</table>
<script type="text/javascript">
<?php

if (empty($_SESSION['login'])) {
    kick();
}
$daily_count = DailyCount::last();
$old_till = TransactionContent::sum('amount', array('Transaction.timestamp:>' => $daily_count->time, 'Transaction.timestamp:<=' => date('Y-m-d H:i:s')));
$old_till += $daily_count->amount;
$account_change = AccountTransactionContent::sum('amount', array('Account.code_name' => 'till', 'AccountTransaction.timestamp:>' => $daily_count->time, 'AccountTransaction.timestamp:<=' => date('Y-m-d H:i:s')));
$old_till += $account_change;
$sales = Transaction::sum('amount', array('timestamp:>' => $daily_count->time));
$stock_amount = TransactionContent::sum('stock_usage', array('Transaction.timestamp:>' => $daily_count->time, 'Transaction.timestamp:<=' => date('Y-m-d H:i:s')));
?>
<script type="text/javascript">
<!--
	function update_diff(value) {
		var calc = document.getElementById('calculated').innerHTML;
		var diff = document.getElementById('diff');
		var html = ''
		if(value != '') {
			html = value - calc;
		}
		diff.innerHTML = html;

	}
-->
</script>
<h1>Dagsavslut</h1>
<table>
	<tr>
		<th>Senast dagsavslut:</th>
    kick("/account_transaction/{$daily_count->transaction_id}");
}
if (!is_numeric(ClientData::post('till'))) {
    die('Vänligen kontrollera värdet i kassan, det var inte numeriskt');
}
$_SESSION['last_request'] = ClientData::post('random');
if (strtotime($daily_count->time) + 120 > time()) {
    die('Det måste gå minst 2 minuter mellan två kassaslut.');
}
$sales_amount = Transaction::sum('amount', array('timestamp:>' => $daily_count->time, 'timestamp:<=' => $time));
if ($sales_amount == null) {
    $sales_amount = 0;
}
$old_till = AccountTransactionContent::sum('amount', array('Account.code_name' => 'till', 'AccountTransaction.timestamp:>' => $daily_count->time, 'AccountTransaction.timestamp:<=' => $time));
$old_till += $daily_count->amount;
$stock_amount = TransactionContent::sum('stock_usage', array('Transaction.timestamp:>' => $daily_count->time, 'Transaction.timestamp:<=' => $time));
if ($stock_amount == null) {
    $stock_amount = 0;
}
$db->autocommit(false);
$transaction = new AccountTransaction();
$transaction->description = 'Dagsavslut';
$transaction->user_id = $_SESSION['login'];
$transaction->timestamp = $time;
$transaction->commit();
$sales = new AccountTransactionContent();
$sales->account_transaction_id = $transaction->id;
$sales->account_id = Account::from_code_name('sales')->id;
$sales->amount = -$sales_amount;
$sales->commit();
$till = new AccountTransactionContent();
echo $product->inventory_threshold ? $product->inventory_threshold . ' st' : '';
?>
</td>
				<td class="numeric threshold_diff<?php 
echo mark(($t_diff = $product->count - $product->inventory_threshold) <= 0 && $product->inventory_threshold != null);
?>
" ><?php 
echo $product->inventory_threshold > 0 ? $t_diff . ' st' : '';
?>
</td>
				<td class="numeric sales"><?php 
echo $count = TransactionContent::sum('count', array('product_id' => $product->id, 'Transaction.timestamp:>' => date('Y-m-d', time() - 60 * 60 * 24 * 30)));
?>
 st</td>
				<td class="numeric sales_value"><?php 
echo number($sold = TransactionContent::sum('amount', array('product_id' => $product->id, 'Transaction.timestamp:>' => date('Y-m-d', time() - 60 * 60 * 24 * 30))));
?>
 kr</td>
				<td class="numeric sales_value"><?php 
echo number($sold - $count * $product->value);
?>
 kr</td>
			</tr>
		<? endforeach ?>
	</tbody>
</table>


<script type="text/javascript">
var sort;
function startSort() {
}
$products = Product::selection(array('@custom_order' => '`products`.`count` > 0 DESC', '@order' => array('Category.name', 'name'), 'category_id:!=' => 0));
$i = 0;
$revenue = array();
$db->prepare_fetch("\n\tSELECT\n\t\tSUM(`transaction_contents`.`amount` -\n\t\t\t`transaction_contents`.`count` * `products`.`value`\n\t\t) as revenue\n\tFROM\n\t\t`transaction_contents` JOIN\n\t\t`products` ON (`products`.`product_id` = `transaction_contents`.`product_id`) JOIN\n\t\t`transactions` ON (`transaction_contents`.`transaction_id` = `transactions`.`transaction_id`)\n\tWHERE\n\t\t`transactions`.`timestamp` > ?", $revenue, 's', date('Y-m-d', time() - 60 * 60 * 24 * 30));
?>
<table>
	<tr>
		<th>Lagrets värde</th>
		<td class="numeric"><?php 
echo number(Product::sum(array('value', '*', 'count')));
?>
 kr</td>
	</tr>
	<tr>
		<th>Försäljning senaste 30 dagarna</th>
		<td class="numeric"><?php 
echo number(TransactionContent::sum('amount', array('Transaction.timestamp:>' => date('Y-m-d', time() - 60 * 60 * 24 * 30))));
?>
 kr</td>
	</tr>
	<tr>
		<th>Vinst senaste 30 dagarna</th>
		<td class="numeric"><?php 
echo number($revenue['revenue']);
?>
 kr</td>
	</tr>
</table>
<? require "../content/list_products.php" ?>