<?php

/**
 * Copy of Excel's PMT function.
 * Credit: http://thoughts-of-laszlo.blogspot.nl/2012/08/complete-formula-behind-excels-pmt.html
 *
 * @param double $interest        The interest rate for the loan.
 * @param int    $num_of_payments The total number of payments for the loan in months.
 * @param double $PV              The present value, or the total amount that a series of future payments is worth now;
 *                                Also known as the principal.
 * @param double $FV              The future value, or a cash balance you want to attain after the last payment is made.
 *                                If fv is omitted, it is assumed to be 0 (zero), that is, the future value of a loan is 0.
 * @param int    $Type            Optional, defaults to 0. The number 0 (zero) or 1 and indicates when payments are due.
 *                                0 = At the end of period
 *                                1 = At the beginning of the period
 *
 * @return float
 */
function wiaas_PMT($interest, $num_of_payments, $PV, $FV = 0.00, $Type = 0){
	/*$interest = $interest / 12;
	$xp=pow((1+$interest),$num_of_payments);
	return
		($PV* $interest*$xp/($xp-1)+$interest/($xp-1)*$FV)*
		($Type==0 ? 1 : 1/($interest+1));*/
	$rates = [
		24 => 4.282,
		30 => 3.451,
		36 => 2.896,
		42 => 2.500,
		48 => 2.223,
		54 => 2.025,
		60 => 1.834
	];

	$interest = isset($rates[$num_of_payments]) ? $rates[$num_of_payments] : 10;

	return round($PV * ($interest / 100));
}

function wiaas_get_recurrent_price_mortage($principal_amount, $pay_period, $margin, $interest_rate) {
	$new_principal_amount = $principal_amount - $margin;
	$interest_rate = $interest_rate / 100;
	$fixed_mortage = wiaas_PMT($interest_rate, $pay_period, $new_principal_amount);

	return round($fixed_mortage, 2);
}

function wiaas_get_price_margin($fixed_price, $principal_amount, $total_cost) {
	$total_gain = $fixed_price + $principal_amount;
	return $total_gain - $total_cost;
}

function wiaas_get_package_hardware_procurement_cost($package) {
	$bundled_items = $package->get_bundled_items();
	$total_cost = 0;

	foreach ($bundled_items as $bundled_item) {
		$product = $bundled_item->product;

		if (Wiaas_Product_Category::get_category($product) === 'hardware') {
			$total_cost += Wiaas_Pricing::get_product_total_cost($product) * $bundled_item->get_quantity();
		}
	}

	return $total_cost;
}

function wiaas_get_package_software_procurement_cost($package) {
	$bundled_items = $package->get_bundled_items();
	$total_cost = 0;

	foreach ($bundled_items as $bundled_item) {
		if (Wiaas_Product_Category::is_hardware($bundled_item->product)) {
			$total_cost += Wiaas_Pricing::get_product_total_cost($bundled_item->product) * $bundled_item->get_quantity();
		}
	}

	return $total_cost;
}

function wiaas_get_package_installation_procurement_cost($package) {
	$bundled_items = $package->get_bundled_items();
	$total_cost = 0;

	foreach ($bundled_items as $bundled_item) {
		if (Wiaas_Product_Category::is_installation($bundled_item->product)) {
			$installation_cost = Wiaas_Pricing::get_product_total_cost($bundled_item->product);
			$total_cost = $total_cost > $installation_cost ? $total_cost : $installation_cost;
		}
	}

	return $total_cost;
}

function wiaas_get_package_one_time_services_procurement_cost($package) {
	$bundled_items = $package->get_bundled_items();
	$total_cost = 0;

	foreach ($bundled_items as $bundled_item) {
		if (Wiaas_Product_Category::is_service($bundled_item->product)) {

			$price = Wiaas_Product_Pricing::get_product_price($bundled_item->product);
			if (! $price['is_recurring']) {
				$total_cost += Wiaas_Pricing::get_product_total_cost($bundled_item->product) * $bundled_item->get_quantity();
			}
		}
	}

	return $total_cost;
}

function wiaas_get_package_recurring_services_procurement_cost($package) {
	$bundled_items = $package->get_bundled_items();
	$total_cost = 0;

	foreach ($bundled_items as $bundled_item) {
		if (Wiaas_Product_Category::is_service($bundled_item->product)) {

			$price = Wiaas_Product_Pricing::get_product_price($bundled_item->product);
			if ( $price['is_recurring']) {
				$total_cost += $price['price'] * $bundled_item->get_quantity();
			}
		}
	}

	return $total_cost;
}