$money_diff += $diff * $product->value;
    $product->count = $counts[$i];
    $product->commit();
    $contents = new DeliveryContent();
    $contents->cost = 0;
    $contents->delivery_id = $delivery->id;
    $contents->product_id = $product_id;
    $contents->count = $diff;
    var_dump($contents->count);
    $contents->commit();
}
if ($money_diff != 0) {
    $from_account = Account::from_code_name('stock_diff');
    $to_account = Account::from_code_name('stock');
    $transaction = new AccountTransaction();
    $transaction->description = "inventering: {$delivery->id}";
    $transaction->user = $_SESSION['login'];
    $from = new AccountTransactionContent();
    $from->amount = $money_diff;
    $from->account_id = $from_account->id;
    $to = new AccountTransactionContent();
    $to->amount = -$money_diff;
    $to->account_id = $to_account->id;
    $transaction->commit();
    $from->account_transaction_id = $transaction->id;
    $to->account_transaction_id = $transaction->id;
    $from->commit();
    $to->commit();
}
$db->commit();
kick('/view_delivery/' . $delivery->id);
</p>
<h2>Transaktioner</h2>
<table>
	<thead>
		<tr>
			<th>Tid</th>
			<th>Användare</th>
			<th>Summa</th>
			<th>Beskrivning</th>
		</tr>
	</thead>
	<tbody>
		<? 
			foreach(AccountTransactionContent::selection(array(
				'account_id' => $account->id,
				'@order' => 'AccountTransaction.timestamp:desc',
				'@limit' => array($page*30, $page*30+30),
			)) as $transaction_content):
		?>
			<tr>
				<td><a href="/account_transaction/<?php 
echo $transaction_content->account_transaction_id;
?>
"><?php 
echo $transaction = $transaction_content->AccountTransaction;
?>
</a></td>
				<td><?php 
echo $transaction->User;
?>
</td>
    $account = Account::from_code_name($balance_accounts[$i]);
    if ($account == null && $balance_amounts[$i] != 0) {
        $errors['konton'] = 'Du måste ange vilket konto pengarna kom ifrån';
        break;
    }
    $balance = new AccountTransactionContent();
    $balance->account_id = $account->id;
    $balance->amount = -$balance_amounts[$i];
    $balance->account_transaction_id = $transaction->id;
    $balance->commit();
}
if (abs($balance_amount - $stock_change_amount) > 0.5) {
    $errors['kassa'] = 'Lagervärde av produkterna och penningåtgång stämmer inte överens. Du måste tala om vart pengarna kommer ifrån (det är ok att avrunda till närmaste krona)';
}
$purchases_account = Account::from_code_name('purchases');
$purchases = new AccountTransactionContent();
$purchases->amount = $balance_amount;
$purchases->account_id = $purchases_account->id;
$purchases->account_transaction_id = $transaction->id;
$purchases->commit();
$stock->account_transaction_id = $transaction->id;
$stock_change->account_transaction_id = $transaction->id;
$stock->commit();
$stock_change->commit();
if (empty($errors) && $at_least_1_item) {
    $db->commit();
    kick('/view_delivery/' . $delivery->id);
} else {
    $_SESSION['_POST'] = $_POST;
    foreach ($errors as $index => $error) {
        Message::add_error("Rad {$index}: {$error}");
<?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>
?>
<p><a href="/money_transfer">Ny transaktion</a></p>
<table>
	<thead>
		<tr>
			<th>Konto</th>
			<th>Summa</th>
		</tr>
	</thead>
	<tbody>
		<? foreach($accounts as $account): ?>
			<tr>
				<td title="<?php 
echo $account->description;
?>
"><a href="/account/<?php 
echo $account->code_name;
?>
"><?php 
echo $account;
?>
</a></td>
				<td><?php 
echo AccountTransactionContent::sum('amount', array('account_id' => $account->id));
?>
</td>
			</tr>
		<? endforeach ?>
	</tbody>
</table>
$sales->account_id = Account::from_code_name('sales')->id;
$sales->amount = -$sales_amount;
$sales->commit();
$till = new AccountTransactionContent();
$till->account_transaction_id = $transaction->id;
$till->account_id = Account::from_code_name('till')->id;
$till->amount = ClientData::post('till') - $old_till;
$till->commit();
$diff = new AccountTransactionContent();
$diff->account_transaction_id = $transaction->id;
$diff->account_id = Account::from_code_name('diff')->id;
$diff->amount = -($till->amount + $sales->amount);
$diff->commit();
$stock = new AccountTransactionContent();
$stock->account_transaction_id = $transaction->id;
$stock->account_id = Account::from_code_name('stock')->id;
$stock->amount = -$stock_amount;
$stock->commit();
$stock_usage = new AccountTransactionContent();
$stock_usage->account_transaction_id = $transaction->id;
$stock_usage->account_id = Account::from_code_name('stock_change')->id;
$stock_usage->amount = $stock_amount;
$stock_usage->commit();
$daily_count = new DailyCount();
$daily_count->time = $time;
$daily_count->amount = ClientData::post('till');
$daily_count->account_transaction_id = $transaction->id;
$daily_count->user = $_SESSION['login'];
$daily_count->commit();
$db->commit();
kick("/account_transaction/{$transaction->id}");