<?php

/**
 * Retrieve standard bundles from order
 *
 * @param int|WC_Order $order
 *
 * @return array
 */
function wiaas_get_order_standard_bundle_items($order) {

	if (is_numeric($order)) {

		$order = wc_get_order($order);
	}

	$items = $order->get_items();

	$standard_bundle_items = array();

	foreach ($items as $item) {

		if (isset($item['wiaas_standard_package'])) {

			$standard_bundle_items[] = $item;
		}
	}

	return $standard_bundle_items;
}

/**
 * Retrieve order summary data for procurement order for suppliers
 *
 * @param int $order_id
 *
 * @return array
 */
function wiaas_get_order_procurement_info($order_id) {

	$order = wc_get_order($order_id);

	$order_items = $order->get_items();

	$order_suppliers_info = array();
	$order_delivery_suppliers_info = $order->get_meta('_wiaas_delivery_suppliers');
	$order_installation_suppliers_info = $order->get_meta('_wiaas_installation_suppliers');

	foreach ($order_delivery_suppliers_info as $id => $order_delivery_supplier_info) {
		$order_suppliers_info[$id] = $order_delivery_supplier_info;
	}
	foreach ($order_installation_suppliers_info as $id => $order_installation_supplier_info) {
		$order_suppliers_info[$id] = $order_installation_supplier_info;
	}

	$data = array();

	foreach ($order_items as $order_item_id => $order_item) {

		$category = $order_item['wiaas_category'];
		$supplier_organization_id = $order_item['wiaas_supplier_organization_id'];
		$supplier_info = $order_suppliers_info[$supplier_organization_id];

		if (empty($category)) {

			continue;
		}

		$data[$category] ?: array();

		// get price and quantity
		$single_product_item_cost = floatval($order_item['wiaas_product_price']);

		$quantity = absint($order_item['quantity']);
		$total_price = $quantity * $single_product_item_cost;

		$data[$category][] = array(
			'Name' => $order_item->get_name(),
			'Category' => ucfirst(strtolower($category)),
			'Manufacturer Product No' => $order_item['wiaas_manufacturer_product_no'],
			'Supplier Product No' => $order_item['wiaas_supplier_product_no'],
			'Units' => $quantity,
			'Price' => $total_price,
			'Supplier Company' => $supplier_info['name'],
			'Supplier VAT' => $supplier_info['vat_code'],
			'Supplier Phone' => $supplier_info['phone'],
			'Supplier Email' => $supplier_info['email'],
		);
	}

	return $data;
}