<?php

error_reporting(E_ALL & ~E_NOTICE);

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_COSTS_GROUPS_UPDATE_COSTS_FUNC() {

	require_once APP_PATH_ROOT . DS . 'se-lib' . DS . 'Lib.php';
	Lib::loadClass('V');
	Lib::loadClass('User');
	Lib::loadClass('Config');
	Lib::loadClass('DB');

	$db = DB::getDB();
	$sql = "update `USERS2_OFFERS_COSTS_GROUPS` ug
		set
			`P_OFFER_COSTS`=(select sum(us.`COST_POWER`)
				from `USERS2_OFFERS_COSTS` as us
				where
					us.`ID_COSTS_GROUPS`=ug.`ID`
					and us.`A_STATUS` in('WAITING','NORMAL')
					and us.`COST_ACCOUNTED_ON`='MONTHLY'
			)
			, `P_OFFER_COSTS_START`=(select sum(us.`COST_POWER`)
				from `USERS2_OFFERS_COSTS` as us
				where
					us.`ID_COSTS_GROUPS`=ug.`ID`
					and us.`A_STATUS` in('WAITING','NORMAL')
					and us.`COST_ACCOUNTED_ON`='START'
			)
	";
	$db->query($sql);
	$affected = $db->affected_rows();
	echo '<p>' . "Zaktualizowano {$affected} rekordów." . '</p>';

	Lib::loadClass('ProcesHelper');
	$groupCostsID = ProcesHelper::getZasobTableID('USERS2_OFFERS_COSTS_GROUPS');

	echo '<p>' . "Wróć do ";
		echo '<a href="' . "index.php?MENU_INIT=VIEWTABLE_AJAX&ZASOB_ID={$groupCostsID}" . '">' . "USERS2_OFFERS_COSTS_GROUPS" . '</a>';
	echo '</p>';
}
