Location: PHPKode > projects > PhpRechnung > phpRechnung/cashbook/newf.php
<?php
/*
	newf.php

	phpRechnung - is easy-to-use Web-based multilingual accounting software.
	Copyright (C) 2001 - 2008 Edy Corak < phprechnung at ecorak dot net >

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

require_once("../include/phprechnung.inc.php");
require_once("../include/smarty.inc.php");
CheckUser();
CheckAdminGroup1();
CheckSession();

if(isset($_POST['cashbookID']))
{
	$cashbookID = $_POST['cashbookID'];
}
if(isset($_POST['page']))
{
	$page = $_POST['page'];
}
if(isset($_POST['infoID']))
{
	$infoID = $_POST['infoID'];
}
if(isset($_POST['CashbookDate']))
{
	$CashbookDate = $_POST['CashbookDate'];
}
if(isset($_POST['StartingWith']))
{
	$StartingWith = ereg_replace(",", ".", $_POST['StartingWith']);
}
if(isset($_POST['Takings']))
{
	$Takings = ereg_replace(",", ".", $_POST['Takings']);
}
if(isset($_POST['Expenditures']))
{
	$Expenditures = ereg_replace(",", ".", $_POST['Expenditures']);
}
if(isset($_POST['Description']))
{
	$Description = $_POST['Description'];
}
if(isset($_POST['Order']))
{
	$Order = $_POST['Order'];
}
if(isset($_POST['Sort']))
{
	$Sort = $_POST['Sort'];
}
if(isset($_REQUEST['Canceled']))
{
	$Canceled = $_REQUEST['Canceled'];
	$smarty->assign("Canceled","$Canceled");
}

// Database connection
//
DBConnect();

// Calculate cash in hand
//
$query = $db->GetAll("SELECT TAKINGS, EXPENDITURES, CASH_IN_HAND_STARTING_WITH FROM {$TBLName}cashbook WHERE CANCELED=2");

// If an error has occurred, display the error message
//
if (!$query)
	print($db->ErrorMsg());
else
	foreach($query as $result)
	{
		$TotalTakings += $result['TAKINGS'];
		$TotalExpenditures += $result['EXPENDITURES'];
		$Cash_In_Hand_Starting_With += $result['CASH_IN_HAND_STARTING_WITH'];
	}

if(isset($StartingWith))
{
	$Cash_In_Hand = $StartingWith + $Cash_In_Hand_Starting_With + ( $TotalTakings - $TotalExpenditures );
}
else
{
	$Cash_In_Hand = $Cash_In_Hand_Starting_With + ( $TotalTakings - $TotalExpenditures );
}

// Get min date from cashbook
//
$query2 = $db->GetRow("SELECT MIN(CASHBOOK_DATE) AS MIN_CASHBOOK_DATE FROM {$TBLName}cashbook WHERE CANCELED=2");
if (!$query2)
	print($db->ErrorMsg());
else
	$Min_Cashbook_Date = $query2['MIN_CASHBOOK_DATE'];

// Calculate total takings / expenditures depend on $Min_Cashbook_Date and $CashbookDate
//
$CashbookDate_Till = German_Mysql_Date($CashbookDate);

$query3 = $db->GetAll("SELECT CASH_IN_HAND_STARTING_WITH, TAKINGS, EXPENDITURES, CASHBOOK_DATE FROM {$TBLName}cashbook WHERE CANCELED=2 AND TO_DAYS(CASHBOOK_DATE) BETWEEN TO_DAYS('$Min_Cashbook_Date') AND TO_DAYS('$CashbookDate_Till')");

// If an error has occurred, display the error message
//
if (!$query3)
	print($db->ErrorMsg());
else
	foreach($query3 as $result3)
	{
		$Cash_In_Hand_Starting_With_Till += $result3['CASH_IN_HAND_STARTING_WITH'];
		$TotalExpenditures_Till += $result3['EXPENDITURES'];
		$TotalTakings_Till += $result3['TAKINGS'];
	}

	$Cash_In_Hand_Till = $Cash_In_Hand_Starting_With_Till + ( $TotalTakings_Till-$TotalExpenditures_Till );

function UserInput($mark)
{
	global $smarty, $cashbookID, $page, $infoID, $CashbookDate, $StartingWith,
	$Takings, $Expenditures, $Description, $Order, $Sort, $Canceled;
	$smarty->assign("cashbookID","$cashbookID");
	$smarty->assign("page","$page");
	$smarty->assign("infoID","$infoID");
	$smarty->assign("CashbookDate","$CashbookDate");
	$smarty->assign("StartingWith","$StartingWith");
	$smarty->assign("Takings","$Takings");
	$smarty->assign("Expenditures","$Expenditures");
	$smarty->assign("Description","$Description");
	$smarty->assign("Order","$Order");
	$smarty->assign("Sort","$Sort");
	$smarty->assign("Mark",$mark);
	$smarty->assign("Canceled",$Canceled);
}

list($day, $month, $year) = explode(".", $CashbookDate);

if (empty($CashbookDate))
{
	$smarty->assign("FieldError","$a[date_text] - $a[field_error]");
	UserInput("CashbookDate");
	$smarty->display('cashbook/newf.tpl');
}
else if (!checkdate($month, $day, $year))
{
	$smarty->assign("FieldError","$a[date_text] - $a[field_error]");
	UserInput("CashbookDate");
	$smarty->display('cashbook/newf.tpl');
}
else if (isset($StartingWith) && $StartingWith <= 0)
{
	$smarty->assign("FieldError","$a[starting_with] - $a[field_error]");
	UserInput("StartingWith");
	$smarty->display('cashbook/newf.tpl');
}
else if (isset($Takings) && $Takings == "" && (isset($Expenditures) && $Expenditures == ""))
{
	$smarty->assign("FieldError","$a[takings] / $a[expenditures] - $a[field_error]");
	UserInput("Takings");
	$smarty->display('cashbook/newf.tpl');
}
else if (isset($Takings) && $Takings > 0 && (isset($Expenditures) && $Expenditures > 0))
{
	$smarty->assign("FieldError","$a[takings_expenditures_error] - $a[field_error]");
	UserInput("Takings");
	$smarty->display('cashbook/newf.tpl');
}
else if (isset($Takings) && $Takings < 0)
{
	$smarty->assign("FieldError","$a[takings] - $a[field_error]");
	UserInput("Takings");
	$smarty->display('cashbook/newf.tpl');
}
else if (isset($Expenditures) && $Expenditures < 0)
{
	$smarty->assign("FieldError","$a[expenditures] - $a[field_error]");
	UserInput("Expenditures");
	$smarty->display('cashbook/newf.tpl');
}
else if (empty($Description))
{
	$smarty->assign("FieldError","$a[cashbook_description] - $a[field_error]");
	UserInput("Description");
	$smarty->display('cashbook/newf.tpl');
}
else if (($StartingWith + $Cash_In_Hand - $Expenditures) < 0)
{
	$smarty->assign("FieldError","$a[cashbook_expenditures]");
	UserInput("Expenditures");
	$smarty->display('cashbook/newf.tpl');
}
// This is to prevent to spend more money you have if you enter date in the past
//
else if (($Cash_In_Hand_Till - $Expenditures) < 0)
{
	$smarty->assign("FieldError","$a[cashbook_expenditures]");
	UserInput("Expenditures");
	$smarty->display('cashbook/newf.tpl');
}
else
{
	$Cash_In_Hand_Day = $Cash_In_Hand + ( $Takings - $Expenditures );
	$Cash_In_Hand_Day = ereg_replace(",", ".", $Cash_In_Hand_Day);

	$CashbookDate = German_Mysql_Date($CashbookDate);

	$query4 = "INSERT INTO {$TBLName}cashbook (CASHBOOKID, MYID, INVOICEID, PAYMENTID, DESCRIPTION, CASHBOOK_DATE, TAKINGS, EXPENDITURES, CASH_IN_HAND, CASH_IN_HAND_STARTING_WITH, CANCELED, CREATEDBY, MODIFIEDBY, USERGROUP1, USERGROUP2, CREATED, MODIFIED)";
	$query4 .= "VALUES(NULL, '', '', '', '$Description', '$CashbookDate', '$Takings', '$Expenditures', '$Cash_In_Hand_Day', '$StartingWith', '2', '$_SESSION[Username]', '$_SESSION[Username]', '$_SESSION[Usergroup1]', '$_SESSION[Usergroup2]', '$CurrentDateTime','$CurrentDateTime')";

	if ($db->Execute($query4) === false)
	{
		die($db->ErrorMsg());
	}

	$_SESSION['NewID'] = "1";

	Header("Location: $web/cashbook/new.php?page=$page&infoID=$infoID&Order=$Order&Sort=$Sort&Canceled=$Canceled&$sessname=$sessid");
}

?>
Return current item: PhpRechnung