<?php

if (!defined('DS')) define('DS', DIRECTORY_SEPARATOR);
if (!defined('APP_PATH_ROOT')) define('APP_PATH_ROOT', dirname(__FILE__));
if (!defined('APP_PATH_WWW')) define('APP_PATH_WWW', dirname(__FILE__));
if (!defined('APP_PATH_CONFIG')) define('APP_PATH_CONFIG', APP_PATH_ROOT . DS . 'config');

function USERS2_OFFERS() {
	require_once dirname(__FILE__) . '/' . 'se-lib' . '/' . 'Lib.php';
	Lib::loadClass('V');
	Lib::loadClass('DB');

	$params = array();
	$params['toolbar'] = array();
	$params['toolbar'] []= '<a href="' . "?MENU_INIT=USERS2_OFFERS" . "&task=FIX_NETTO" . '" title="' . "Popraw kwoty netto" . '">' . "FIX NETTO" . '</a>';

	$task = V::get('task', '', $_GET);
	if ($task == 'FIX_NETTO') {
		$db = DB::getDB();
		$sql_update_array = array();
		$sql_update_array []= "update `USERS2_OFFERS` set
			P_CONTRACT_PRICE_NETTO	 = round(P_CONTRACT_PRICE / (1 + STAWKA_VAT/100), 2)
			, P_PROMO_MONTH_PRICE_NETTO	 = round(P_PROMO_MONTH_PRICE / (1 + STAWKA_VAT/100), 2)
			, P_NORMAL_PRICE_NETTO	 = round(P_NORMAL_PRICE / (1 + STAWKA_VAT/100), 2);";
		$sql_update_array []= "update `USERS2_OFFERS` set P_CONTRACT_PRICE_NETTO    =(P_CONTRACT_PRICE_NETTO - 0.01)    where round(P_CONTRACT_PRICE_NETTO    * (1 + STAWKA_VAT/100), 2) > P_CONTRACT_PRICE;";
		$sql_update_array []= "update `USERS2_OFFERS` set P_PROMO_MONTH_PRICE_NETTO =(P_PROMO_MONTH_PRICE_NETTO - 0.01) where round(P_PROMO_MONTH_PRICE_NETTO * (1 + STAWKA_VAT/100), 2) > P_PROMO_MONTH_PRICE;";
		$sql_update_array []= "update `USERS2_OFFERS` set P_NORMAL_PRICE_NETTO      =(P_NORMAL_PRICE_NETTO - 0.01)      where round(P_NORMAL_PRICE_NETTO      * (1 + STAWKA_VAT/100), 2) > P_NORMAL_PRICE;";

		foreach ($sql_update_array as $v_sql) {
			$res = $db->query($v_sql);
			if ($db->has_errors()) {
				echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">DB Errors: ';print_r($db->get_errors());echo'</pre>';
			} else {
				echo '<br />' . "Poprawiono " . $db->affected_rows() . " rekordów.";
			}
		}
	}

	WIEVTABLE( $params );
}

function USERS2_OFFERS_EDIT() {
	EDIT_TABLE_RECORD();
}

function USERS2_OFFERS_HIST() {
	WIEVTABLE_HIST();
}


/**
 * triggers:

--
-- 
--
DROP TRIGGER IF EXISTS `PRICE_NETTO_before_update_USERS2_OFFERS`;
DELIMITER $$
CREATE TRIGGER `PRICE_NETTO_before_update_USERS2_OFFERS` BEFORE UPDATE ON `USERS2_OFFERS`
	FOR EACH ROW BEGIN
		IF NEW.`P_CONTRACT_PRICE` != OLD.`P_CONTRACT_PRICE`
				or NEW.`P_PROMO_MONTH_PRICE` != OLD.`P_PROMO_MONTH_PRICE`
				or NEW.`P_NORMAL_PRICE` != OLD.`P_NORMAL_PRICE`
				or NEW.`STAWKA_VAT` != OLD.`STAWKA_VAT`
		THEN

			SET NEW.`P_CONTRACT_PRICE_NETTO`    = round(NEW.`P_CONTRACT_PRICE`    / (1 + NEW.`STAWKA_VAT`/100), 2);
			SET NEW.`P_PROMO_MONTH_PRICE_NETTO` = round(NEW.`P_PROMO_MONTH_PRICE` / (1 + NEW.`STAWKA_VAT`/100), 2);
			SET NEW.`P_NORMAL_PRICE_NETTO`      = round(NEW.`P_NORMAL_PRICE`      / (1 + NEW.`STAWKA_VAT`/100), 2);

			IF round(NEW.`P_CONTRACT_PRICE_NETTO` * (1 + NEW.`STAWKA_VAT`/100), 2) > NEW.`P_CONTRACT_PRICE` THEN
				SET NEW.`P_CONTRACT_PRICE_NETTO`=(NEW.`P_CONTRACT_PRICE_NETTO` - 0.01);
			END IF;

			IF round(NEW.`P_PROMO_MONTH_PRICE_NETTO` * (1 + NEW.`STAWKA_VAT`/100), 2) > NEW.`P_PROMO_MONTH_PRICE` THEN
				SET NEW.`P_PROMO_MONTH_PRICE_NETTO`=(NEW.`P_PROMO_MONTH_PRICE_NETTO` - 0.01);
			END IF;

			IF round(NEW.`P_NORMAL_PRICE_NETTO` * (1 + NEW.`STAWKA_VAT`/100), 2) > NEW.`P_NORMAL_PRICE` THEN
				SET NEW.`P_NORMAL_PRICE_NETTO`=(NEW.`P_NORMAL_PRICE_NETTO` - 0.01);
			END IF;

		END IF;
	END
$$

--
-- 
--
DROP TRIGGER IF EXISTS `PRICE_NETTO_before_insert_USERS2_OFFERS`;
DELIMITER $$
CREATE TRIGGER `PRICE_NETTO_before_insert_USERS2_OFFERS` BEFORE INSERT ON `USERS2_OFFERS`
	FOR EACH ROW BEGIN
		SET NEW.`P_CONTRACT_PRICE_NETTO`    = round(NEW.`P_CONTRACT_PRICE`    / (1 + NEW.`STAWKA_VAT`/100), 2);
		SET NEW.`P_PROMO_MONTH_PRICE_NETTO` = round(NEW.`P_PROMO_MONTH_PRICE` / (1 + NEW.`STAWKA_VAT`/100), 2);
		SET NEW.`P_NORMAL_PRICE_NETTO`      = round(NEW.`P_NORMAL_PRICE`      / (1 + NEW.`STAWKA_VAT`/100), 2);

		IF round(NEW.`P_CONTRACT_PRICE_NETTO` * (1 + NEW.`STAWKA_VAT`/100), 2) > NEW.`P_CONTRACT_PRICE` THEN
			SET NEW.`P_CONTRACT_PRICE_NETTO`=(NEW.`P_CONTRACT_PRICE_NETTO` - 0.01);
		END IF;

		IF round(NEW.`P_PROMO_MONTH_PRICE_NETTO` * (1 + NEW.`STAWKA_VAT`/100), 2) > NEW.`P_PROMO_MONTH_PRICE` THEN
			SET NEW.`P_PROMO_MONTH_PRICE_NETTO`=(NEW.`P_PROMO_MONTH_PRICE_NETTO` - 0.01);
		END IF;

		IF round(NEW.`P_NORMAL_PRICE_NETTO` * (1 + NEW.`STAWKA_VAT`/100), 2) > NEW.`P_NORMAL_PRICE` THEN
			SET NEW.`P_NORMAL_PRICE_NETTO`=(NEW.`P_NORMAL_PRICE_NETTO` - 0.01);
		END IF;
	END
$$

 */
