<?php

###################################################################################################
###################################################################################################
## CASHBOOK VIEW - SET FIRST TOTALS ###############################################################
###################################################################################################
###################################################################################################

if ($R['submitFirstTimeCashBookForm']) {

	///////////////////////////////////////////////////////////////////////////////////////////////
	// DATA VALIDATION ////////////////////////////////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////////////////

	$OK=1;

	// check date (mandatory info)
	if (!$R['firstTimeCashBook']['date']) {
		$showMessage['warning'] = $_LANG['cashBookView']['saveFirstTotals']['messages']['missingDate'];
		$OK=0;
	}
	else {

		// check that start date is valid
		$firstTimeCashBookDate=standard2mysqlDateConversion($R['firstTimeCashBook']['date']);
		if (!$firstTimeCashBookDate) {
			$showMessage['warning'] = $_LANG['cashBookView']['saveFirstTotals']['messages']['invalidDate'];
			$OK=0;
		}

		// check that start date is not in the future
		else {
			if ($firstTimeCashBookDate>TODAY_ISO) {
				$showMessage['warning'] = $_LANG['cashBookView']['saveFirstTotals']['messages']['dateInFuture'];
				$OK=0;
			}
		}

	}

	// check initial value for each currency
	if (!$R['firstTimeCashBook']) {
		$showMessage['error'] = $_LANG['cashBookView']['saveFirstTotals']['messages']['missingValuesFromRequest'];
		$OK=0;
	}
	else {
		foreach ($currencyArray as $thisCurrency) {

			// check that all values are valid  (for office total)
			if (!is_numeric($R['firstTimeCashBook']['initialValue'][$thisCurrency]) or $R['firstTimeCashBook']['initialValue'][$thisCurrency]<0) {
				$showMessage['warning'] = sprintf($_LANG['cashBookView']['saveFirstTotals']['messages']['invalidInitialValue'], $thisCurrency);
				$OK=0;
			}

		}

	}

	// check if first totals don't already exist
	$lastClosedDate=Cashbook::getLastClosedBalanceDate($R['idOffice']);
	if ($lastClosedDate) {
		$showMessage['warning'] = $_LANG['cashBookView']['saveFirstTotals']['messages']['firstTotalsExist'];
		$OK=0;
	}


	///////////////////////////////////////////////////////////////////////////////////////////////
	// CASHBOOK DATA IS VALID -> SET FIRST TOTALS /////////////////////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////////////////

	// SAVE OFFICE TOTALS /////////////////////////////////////////////////////////////////////////
	if ($OK) {

		// form "values" part of the sql
		$sqlValues=array();
		foreach ($currencyArray as $thisCurrency) {
			$sqlValues[]="(".
				"'".$R['idOffice']."', '".$firstTimeCashBookDate."', ".
				"'".$thisCurrency."', '".round($R['firstTimeCashBook']['initialValue'][$thisCurrency], 2)."', ".
				"'".$S['auth']['idUser']."', '".TIMESTAMP."'
			)";
		}

		// form and execute main sql
		if ($sqlValues) {
			$updateCashBookSQL="
				INSERT INTO `$DBTCashBook` (`idOffice`, `closedDate`, `currency`, `totalValue`, `idUpdateUser`, `lastUpdateTime`)
				VALUES ".implode(", ", $sqlValues)
			;
			$updateCashBook=$db->Execute($updateCashBookSQL);

			// check for errors
			if (!$updateCashBook) {
				$showMessage['error'] = $_LANG['cashBookView']['saveFirstTotals']['messages']['saveFirstTotalsError'];
				$OK=0;
				writeLog("Could not update initial cashBook (offices totals). SQL: ".$updateCashBookSQL, 'events', 10);
			}
		}
	}

	// SAVE LAST CLOSED DATE //////////////////////////////////////////////////////////////////////
	if ($OK) {
		$showMessage['success'] = $_LANG['cashBookView']['saveFirstTotals']['messages']['saveFirstTotalsSuccess'];
		// trace visits
		$userTrailItem->updateActionOld('cashBook_saveFirstTotalsOk');
		$userTrailItem->updateExtraDataOld(array('date'=>$firstTimeCashBookDate));
	}

}

