<?php

require "../../includes.php";
if (empty($_SESSION['login'])) {
    kick('login?kickback=' . htmlspecialchars(kickback_url()));
}
$db->autoCommit(false);
$products = ClientData::post('product_id');
$counts = ClientData::post('product_count');
$money_diff = 0;
$delivery = new Delivery();
$delivery->description = "Inventering";
$delivery->user = $_SESSION['login'];
$delivery->commit();
foreach ($products as $i => $product_id) {
    // Create purchase
    $product = Product::from_id($product_id);
    $diff = $counts[$i] - $product->count;
    $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');