<commit_before><?php
class DeliveryC extends Controller {
	protected $_default_site = 'index';

	public function __construct($site, $data = array()) {
		parent::__construct($site, $data);
		verify_login(kickback_url());
	}
	public function make($params) {
		$this->_access_type('script');
		global $db;
		$user = new User($_SESSION['login']);

		$multiplyer = ClientData::post('multiplyer');
		$single = ClientData::post('price_per');
		if($single != 'product_type' && $single != 'each_product') {
			$errors['Pris'] = 'Inte valt pris per enskild vara/varotyp';
		}

		$eans = ClientData::post('ean');
		$counts = ClientData::post('count');
		$sales_prices = ClientData::post('sales_price');
		$names = ClientData::post('name');
		$categories = ClientData::post('category');
		$purchase_prices = ClientData::post('purchase_price');
		for($i = 0; $i < count($purchase_prices); $i++) {
			$purchase_prices[$i] *= $multiplyer;
			if($single == 'each_product') {
				$purchase_prices[$i] *= $counts[$i];
			}
		}

		$at_least_1_item = false;
		$db->autoCommit(false);
		$delivery = new Delivery();
		$delivery->description = ClientData::post('description');
		$delivery->user_id = $_SESSION['login'];
		$delivery->commit();
		$stock_change_amount = 0;
		for($i=0; $i < count($eans); $i++) {
			if(empty($eans[$i])) {
				continue;
			}
			$at_least_1_item = true;
			try{
				$product = Product::from_ean($eans[$i]);
				if($product == null) {
					$product = new Product();
					$product->ean = $eans[$i];
				}

				$contents = new DeliveryContent();
				$product->value = ($product->value * $product->count + $purchase_prices[$i]) / ($product->count + $counts[$i]);
				$contents->cost = $purchase_prices[$i] / $counts[$i];
				$product->count += $counts[$i];
				$product->name = $names[$i];
				$product->price = $sales_prices[$i];
				$product->category_id = $categories[$i];
				$product->commit();
				$contents->delivery_id = $delivery->id;
				$contents->product_id = $product->id;
				$contents->count = $counts[$i];
				$contents->commit();
				$stock_change_amount += $purchase_prices[$i];
			} catch(Exception $e) {
				$errors[$i] = $e->getMessage();;
			}
		}
		$transaction = new AccountTransaction();
		$transaction->description = "Inköp id: {$delivery->id}";
		$transaction->user_id = $_SESSION['login'];
		$transaction->commit();
		$transactions = array(
			'stock'        => $stock_change_amount,
			'stock_change' => -$stock_change_amount,
		);

		$balance_amount = 0;
		$balance_amounts = ClientData::post('amount');
		$balance_accounts = ClientData::post('from_account');
		for($i = 0; $i < count($balance_amounts); $i++) {
			$balance_amount += $balance_amounts[$i];
			$transactions[$balance_accounts[$i]] = -$balance_amounts[$i];
		}
		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)';
		}
		$transactions['purchases'] = $balance_amount;

		$transaction->add_contents($transactions);

		if(empty($errors) && $at_least_1_item) {
			$db->commit();
			kick('/Delivery/view/'.$delivery->id);
		} else {
			$_SESSION['_POST'] = $_POST;
			foreach($errors as $index => $error) {
				Message::add_error("Rad $index: $error");
			}
			kick('Delivery/create');
		}
	}

	public function index($params) {
		$this->_access_type('html');
		$this->page = array_shift($params);
		$this->deliveries = Delivery::selection(array(
			'@order' => 'timestamp:desc',
			'@limit' => array($this->page * 50, 50),
		));
		$this->last_page = ceil(Delivery::count()/50)-1;<commit_msg>Fail gracefully if no account chosen in delivery<commit_after><?php
class DeliveryC extends Controller {
	protected $_default_site = 'index';

	public function __construct($site, $data = array()) {
		parent::__construct($site, $data);
		verify_login(kickback_url());
	}
	public function make($params) {
		$this->_access_type('script');
		global $db;
		$user = new User($_SESSION['login']);

		$multiplyer = ClientData::post('multiplyer');
		$single = ClientData::post('price_per');
		if($single != 'product_type' && $single != 'each_product') {
			$errors['Pris'] = 'Inte valt pris per enskild vara/varotyp';
		}

		$eans = ClientData::post('ean');
		$counts = ClientData::post('count');
		$sales_prices = ClientData::post('sales_price');
		$names = ClientData::post('name');
		$categories = ClientData::post('category');
		$purchase_prices = ClientData::post('purchase_price');
		for($i = 0; $i < count($purchase_prices); $i++) {
			$purchase_prices[$i] *= $multiplyer;
			if($single == 'each_product') {
				$purchase_prices[$i] *= $counts[$i];
			}
		}

		$at_least_1_item = false;
		$db->autoCommit(false);
		$delivery = new Delivery();
		$delivery->description = ClientData::post('description');
		$delivery->user_id = $_SESSION['login'];
		$delivery->commit();
		$stock_change_amount = 0;
		for($i=0; $i < count($eans); $i++) {
			if(empty($eans[$i])) {
				continue;
			}
			$at_least_1_item = true;
			try{
				$product = Product::from_ean($eans[$i]);
				if($product == null) {
					$product = new Product();
					$product->ean = $eans[$i];
				}

				$contents = new DeliveryContent();
				$product->value = ($product->value * $product->count + $purchase_prices[$i]) / ($product->count + $counts[$i]);
				$contents->cost = $purchase_prices[$i] / $counts[$i];
				$product->count += $counts[$i];
				$product->name = $names[$i];
				$product->price = $sales_prices[$i];
				$product->category_id = $categories[$i];
				$product->commit();
				$contents->delivery_id = $delivery->id;
				$contents->product_id = $product->id;
				$contents->count = $counts[$i];
				$contents->commit();
				$stock_change_amount += $purchase_prices[$i];
			} catch(Exception $e) {
				$errors[$i] = $e->getMessage();;
			}
		}
		$transaction = new AccountTransaction();
		$transaction->description = "Inköp id: {$delivery->id}";
		$transaction->user_id = $_SESSION['login'];
		$transaction->commit();
		$transactions = array(
			'stock'        => $stock_change_amount,
			'stock_change' => -$stock_change_amount,
		);

		$balance_amount = 0;
		$balance_amounts = ClientData::post('amount');
		$balance_accounts = ClientData::post('from_account');
		for($i = 0; $i < count($balance_amounts); $i++) {
			$balance_amount += $balance_amounts[$i];
			$transactions[$balance_accounts[$i]] = -$balance_amounts[$i];
		}
		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)';
		}
		$transactions['purchases'] = $balance_amount;

		try{
			$transaction->add_contents($transactions);
		} catch(Exception $e) {
			$errors['Konton'] = $e->getMessage();
		}

		if(empty($errors) && $at_least_1_item) {
			$db->commit();
			kick('/Delivery/view/'.$delivery->id);
		} else {
			$_SESSION['_POST'] = $_POST;
			foreach($errors as $index => $error) {
				Message::add_error("Rad $index: $error");
			}
			kick('Delivery/create');
		}
	}

	public function index($params) {
		$this->_access_type('html');
		$this->page = array_shift($params);
		$this->deliveries = Delivery::selection(array(
			'@order' => 'timestamp:desc',
			'@limit' => array($this->page * 50, 50),
		));
		$this->last_page = ceil(Delivery::count()/50)-1;
{'fraction_non_alphanumeric': 0.20002686727565824, 'fraction_numerical': 0.0045674368619022035, 'mean_word_length': 4.5559701492537314, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 86, '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': '3273393', 'n_tokens_mistral': 3015, 'n_tokens_neox': 2610, 'n_words': 453}