<?php

class Wiaas_Pricing_Test extends Wiaas_Unit_Test_Case {

	private function _create_package_to_sell() {
		$product1 = $this->create_new_product(20);
		$this->add_product_category($product1, 'hardware');

		$product2 = $this->create_new_product(20, true, 2);
		$this->add_product_category($product2, 'installation');

		$package = $this->create_new_package();

		$this->add_products_to_package($package, array( $product1, $product2));

		$pricing_rules = array(
			'purchase' => array(
				'minimal_fixed_price' => 100,
				'principal_amount' => 0,
				'minimal_services_price' => 0
			),
			'purchase_24' => array(
				'minimal_fixed_price' => 100,
				'principal_amount' => 0,
				'minimal_services_price' => 100
			),
			'managed_36' => array(
				'minimal_fixed_price' => 100,
				'principal_amount' => 100,
				'minimal_services_price' => 100
			)
		);
		$commision = 50;
		$cost_margin = 0;
		Wiaas_Package_Pricing::set_package_prices($package, $pricing_rules, $commision, $cost_margin);

		$customer_id = wp_create_term(
			'Customer',
			Wiaas_User_Organization::TAXONOMY_NAME
		)['term_id'];

		$commercial_lead_id = wp_create_term(
			'Commercial Lead',
			Wiaas_User_Organization::TAXONOMY_NAME
		)['term_id'];


		self::_set_package_default_extras($commercial_lead_id, $package->get_id());

		$expected_prices = array(
			'purchase' => array(
				'fixed_extra' => 100,
				'recurrent_extra' => 0,
				'services_extra' => 0
			),
			'purchase_24' => array(
				'fixed_extra' => 100,
				'recurrent_extra' => 0,
				'services_extra' => 100
			),
			'managed_36' => array(
				'fixed_extra' => 100,
				'recurrent_extra' => 3,
				'services_extra' => 100
			)
		);

		return array( $package,  $expected_prices, $customer_id, $commercial_lead_id );
	}

	private function _set_package_default_extras($commercial_lead_id, $package_id) {
		$cl_extras = array(
			'purchase_default' => array(
				'fixed' => 0,
				'recurrent' => 0,
				'services' => 0,
				'visible' => true
			),
			'purchase_24_default' => array(
				'fixed' => 0,
				'recurrent' => 0,
				'services' => 0,
				'visible' => true
			),
			'managed_36_default' => array(
				'fixed' => 0,
				'recurrent' => 0,
				'services' => 0,
				'visible' => true
			)
		);

		Wiaas_Package_CL_Pricing::set_extras($commercial_lead_id, $package_id, $cl_extras);
	}

	/**
	 * @covers Wiaas_Pricing::get_product_total_cost()
	 */
	function test_get_fixed_product_total_cost() {
		$product  = $this->create_new_product();

		$total_cost = Wiaas_Pricing::get_product_total_cost($product);

		$this->assertEquals($total_cost, $product->get_price());
	}

	/**
	 * @covers Wiaas_Pricing::get_product_total_cost()
	 */
	function test_get_recurring_product_total_cost() {
		$product  = $this->create_new_product(10, true, 2);

		$total_cost = Wiaas_Pricing::get_product_total_cost($product);

		$this->assertEquals($total_cost, 20);
	}

	/**
	 * @covers Wiaas_Pricing::get_package_total_cost()
	 */
	function test_get_package_with_fixed_products_total_cost() {
		$product1 = $this->create_new_product(20);
		$this->add_product_category($product1, 'hardware');

		$product2 = $this->create_new_product(20);
		$this->add_product_category($product2, 'software');

		$package = $this->create_new_package();

		$this->add_products_to_package($package, array( $product1, $product2));

		$expected_total_price = 40;

		$total_price = Wiaas_Pricing::get_package_total_cost($package);

		$this->assertEquals($expected_total_price, $total_price);
	}

	/**
	 * @covers Wiaas_Pricing::get_package_total_cost()
	 */
	function test_get_package_with_recurring_products_total_cost() {
		$product1 = $this->create_new_product(20);
		$this->add_product_category($product1, 'hardware');

		$product2 = $this->create_new_product(20, true, 2);
		$this->add_product_category($product2, 'software');

		$package = $this->create_new_package();

		$this->add_products_to_package($package, array( $product1, $product2));

		$expected_total_price = 60;

		$total_price = Wiaas_Pricing::get_package_total_cost($package);

		$this->assertEquals($expected_total_price, $total_price);
	}

	/**
	 * @covers Wiaas_Pricing::get_package_total_cost()
	 */
	function test_package_with_single_installation_product_total_cost() {
		$product1 = $this->create_new_product(20);
		$this->add_product_category($product1, 'hardware');

		$product2 = $this->create_new_product(20, true, 2);
		$this->add_product_category($product2, 'installation');

		$package = $this->create_new_package();

		$this->add_products_to_package($package, array( $product1, $product2));

		$expected_total_price = 60;

		$total_price = Wiaas_Pricing::get_package_total_cost($package);

		$this->assertEquals($expected_total_price, $total_price);
	}

	/**
	 * @covers Wiaas_Pricing::get_package_total_cost()
	 */
	function test_package_with_multiple_installation_products_total_cost() {
		$product1 = $this->create_new_product(20);
		$this->add_product_category($product1, 'hardware');

		$product2 = $this->create_new_product(10, true, 2);
		$this->add_product_category($product2, 'installation');

		$this->assertTrue(Wiaas_Product_Category::is_installation($product2));

		$product3 = $this->create_new_product(20, true, 2);
		$this->add_product_category($product3, 'installation');

		$package = $this->create_new_package();

		$this->add_products_to_package($package, array( $product1, $product2, $product3));

		// price will be 20 + 20*2 , more expensive installation will be applied
		$expected_total_price = 60;

		$total_price = Wiaas_Pricing::get_package_total_cost($package);

		$this->assertEquals($expected_total_price, $total_price);
	}

	/**
	 * @covers Wiaas_Pricing::get_standard_package_customer_prices()
	 */
	function test_get_standard_package_customer_price() {

		list( $package,  $expected_prices, $customer_id, $commercial_lead_id ) = $this->_create_package_to_sell();

		$customer_prices = Wiaas_Pricing::get_standard_package_customer_prices(
			$package,
			$customer_id,
			$commercial_lead_id);

		$this->assertCount(3, $customer_prices);

		foreach ($customer_prices as $customer_price) {

			$this->assertArrayHasKey('id', $customer_price);
			$this->assertArrayHasKey('payment_type', $customer_price);
			$this->assertArrayHasKey('max_contract_period', $customer_price);
			$this->assertArrayHasKey('package_pay_period', $customer_price);
			$this->assertArrayHasKey('period_unit', $customer_price);
			$this->assertArrayHasKey('services_contract_period', $customer_price);
			$this->assertArrayHasKey('fixed_extra', $customer_price);
			$this->assertArrayHasKey('recurrent_extra', $customer_price);
			$this->assertArrayHasKey('services_extra', $customer_price);

			$id = $customer_price['id'];

			$this->assertEquals($customer_price['fixed_extra'], $expected_prices[$id]['fixed_extra']);
			$this->assertEquals($customer_price['recurrent_extra'], $expected_prices[$id]['recurrent_extra']);
			$this->assertEquals($customer_price['services_extra'], $expected_prices[$id]['services_extra']);
		}

	}

	/**
	 * @covers Wiaas_Pricing::get_addon_package_customer_price()
	 */
	function test_get_addon_package_customer_price() {
		list( $package, $expected_prices, $customer_id, $commercial_lead_id ) = $this->_create_package_to_sell();

		$addon_product = $this->create_new_product(20);
		$this->add_product_category($addon_product, 'hardware');

		$addon_package = $this->create_new_package();

		$this->add_products_to_package($addon_package, array($addon_product));

		$pricing_rules = array(
			'purchase' => array(
				'minimal_fixed_price' => 500,
				'principal_amount' => 0,
				'minimal_services_price' => 0
			),
			'purchase_24' => array(
				'minimal_fixed_price' => 500,
				'principal_amount' => 0,
				'minimal_services_price' => 500
			),
			'managed_36' => array(
				'minimal_fixed_price' => 500,
				'principal_amount' => 500,
				'minimal_services_price' => 500
			)
		);
		$commision = 50;
		$cost_margin = 0;
		Wiaas_Package_Pricing::set_package_prices($addon_package, $pricing_rules, $commision, $cost_margin);

		self::_set_package_default_extras($commercial_lead_id, $addon_package->get_id());

		Wiaas_Package_Addon::set_package_addons($package, array($addon_package->get_id()));

		$expected_prices = array(
			'purchase' => array(
				'fixed_extra' => 500,
				'recurrent_extra' => 0,
				'services_extra' => 0
			),
			'purchase_24' => array(
				'fixed_extra' => 500,
				'recurrent_extra' => 0,
				'services_extra' => 500
			),
			'managed_36' => array(
				'fixed_extra' => 500,
				'recurrent_extra' => 14,
				'services_extra' => 500
			)
		);

		$customer_prices = Wiaas_Pricing::get_addon_package_customer_price(
			$addon_package,
			$package,
			$customer_id,
			$commercial_lead_id);

		$this->assertCount(3, $customer_prices);

		foreach ($customer_prices as $customer_price) {

			$this->assertArrayHasKey('id', $customer_price);
			$this->assertArrayHasKey('payment_type', $customer_price);
			$this->assertArrayHasKey('max_contract_period', $customer_price);
			$this->assertArrayHasKey('package_pay_period', $customer_price);
			$this->assertArrayHasKey('period_unit', $customer_price);
			$this->assertArrayHasKey('services_contract_period', $customer_price);
			$this->assertArrayHasKey('fixed_extra', $customer_price);
			$this->assertArrayHasKey('recurrent_extra', $customer_price);
			$this->assertArrayHasKey('services_extra', $customer_price);

			$id = $customer_price['id'];

			$this->assertEquals($customer_price['fixed_extra'], $expected_prices[$id]['fixed_extra']);
			$this->assertEquals($customer_price['recurrent_extra'], $expected_prices[$id]['recurrent_extra']);
			$this->assertEquals($customer_price['services_extra'], $expected_prices[$id]['services_extra']);
		}
	}

	/**
	 * @covers Wiaas_Pricing::get_option_package_customer_price()
	 */
	function test_get_option_package_customer_price() {
		list( $package,  $customer_id, $commercial_lead_id ) = $this->_create_package_to_sell();

		$option_product = $this->create_new_product(20);
		$this->add_product_category($option_product, 'hardware');

		$option_package = $this->create_new_package();

		$this->add_products_to_package($option_package, array($option_product));

		$pricing_rules = array(
			'purchase' => array(
				'minimal_fixed_price' => 500,
				'principal_amount' => 0,
				'minimal_services_price' => 0
			),
			'purchase_24' => array(
				'minimal_fixed_price' => 500,
				'principal_amount' => 0,
				'minimal_services_price' => 500
			),
			'managed_36' => array(
				'minimal_fixed_price' => 500,
				'principal_amount' => 500,
				'minimal_services_price' => 500
			)
		);
		$commision = 50;
		$cost_margin = 0;
		Wiaas_Package_Pricing::set_package_prices($option_package, $pricing_rules, $commision, $cost_margin);

		self::_set_package_default_extras($commercial_lead_id, $option_package->get_id());

		Wiaas_Package_Option_Groups::set_package_option_groups($package, array(
			'id' => 'option',
			'name' => 'Option',
			'default' => $option_package->get_id(),
			'options' => array( $option_package->get_id() )
		));

		$expected_prices = array(
			'purchase' => array(
				'fixed_extra' => 500,
				'recurrent_extra' => 0,
				'services_extra' => 0
			),
			'purchase_24' => array(
				'fixed_extra' => 500,
				'recurrent_extra' => 0,
				'services_extra' => 500
			),
			'managed_36' => array(
				'fixed_extra' => 500,
				'recurrent_extra' => 14,
				'services_extra' => 500
			)
		);

		$customer_prices = Wiaas_Pricing::get_option_package_customer_price(
			$option_package,
			$package,
			$customer_id,
			$commercial_lead_id);

		$this->assertCount(3, $customer_prices);

		foreach ($customer_prices as $customer_price) {

			$this->assertArrayHasKey('id', $customer_price);
			$this->assertArrayHasKey('payment_type', $customer_price);
			$this->assertArrayHasKey('max_contract_period', $customer_price);
			$this->assertArrayHasKey('package_pay_period', $customer_price);
			$this->assertArrayHasKey('period_unit', $customer_price);
			$this->assertArrayHasKey('services_contract_period', $customer_price);
			$this->assertArrayHasKey('fixed_extra', $customer_price);
			$this->assertArrayHasKey('recurrent_extra', $customer_price);
			$this->assertArrayHasKey('services_extra', $customer_price);

			$id = $customer_price['id'];

			$this->assertEquals($customer_price['fixed_extra'], $expected_prices[$id]['fixed_extra']);
			$this->assertEquals($customer_price['recurrent_extra'], $expected_prices[$id]['recurrent_extra']);
			$this->assertEquals($customer_price['services_extra'], $expected_prices[$id]['services_extra']);
		}
	}
}