tiki-accounting_stack.php 5.33 KB
Newer Older
1
<?php
2
// (c) Copyright 2002-2011 by authors of the Tiki Wiki CMS Groupware Project
3 4 5
// 
// All Rights Reserved. See copyright.txt for details and a complete list of authors.
// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6
// $Id$
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

$section = 'accounting';
require_once ('tiki-setup.php');
require_once ('lib/accounting/accountinglib.php');


// Feature available?
if ($prefs['feature_accounting'] !='y') {
	$smarty->assign('msg', tra("This feature is disabled") . ": feature_accounting");
	$smarty->display("error.tpl");
	die;
}

$globalperms = Perms::get();
$objectperms = Perms::get( array( 'type' => 'accounting book', 'object' => $bookId ) );

if (!($globalperms->acct_book or $objectperms->acct_book_stack)) {
	$smarty->assign('msg', tra("You do not have the right to book into the stack"));
	$smarty->display("error.tpl");
	die;		
}

if (!isset($_REQUEST['bookId'])) {
	$smarty->assign('msg', tra("Missing book id"));
	$smarty->display("error.tpl");
	die;
}
$bookId=$_REQUEST['bookId'];
$smarty->assign('bookId',$bookId);

if (!isset($_REQUEST['stackId'])) {
	$stackId=0;
} else {
	$stackId=$_REQUEST['stackId'];
}

if (isset($_REQUEST['hideform'])) {
	$smarty->assign('hideform',$_REQUEST['hideform']);
} else {
	$smarty->assign('hideform',0);
}

$book=$accountinglib->getBook($bookId);
$smarty->assign('book',$book);

$accounts=$accountinglib->getAccounts($bookId,$all=true);
$smarty->assign('accounts',$accounts);

if (isset($_REQUEST['action'])) {
	check_ticket('accounting');
	if ($_REQUEST['action']=='book') {
		if ($stackId==0) {
			// new entry
			$result=$accountinglib->stackBook($bookId, $_REQUEST['stackDate'], $_REQUEST['stackDescription'],
					$_REQUEST['debitAccount'], $_REQUEST['creditAccount'],
					$_REQUEST['debitAmount'], $_REQUEST['creditAmount'],
					$_REQUEST['debitText'],$_REQUEST['creditText']);
		} else {
			// modified old entry
			$result=$accountinglib->stackUpdate($bookId, $stackId, $_REQUEST['stackDate'], $_REQUEST['stackDescription'],
					$_REQUEST['debitAccount'], $_REQUEST['creditAccount'],
					$_REQUEST['debitAmount'], $_REQUEST['creditAmount'],
					$_REQUEST['debitText'],$_REQUEST['creditText']);
		}
		if(is_numeric($result)) {
			if (isset($_REQUEST['statementId'])) {
				$accountinglib->updateStatementStack($bookId, $_REQUEST['statementId'],$result);		
			}
			$stackId=0; //success means we can create a new entry
		}
	} elseif ($_REQUEST['action']=='delete') {
		$result=$accountinglib->stackDelete($bookId, $stackId);
		$stackId=0;
	} elseif ($_REQUEST['action']=='confirm') {
		$result=$accountinglib->stackConfirm($bookId, $stackId);
		$stackId=0;
	} else {
		// unknown action = nothing
		$result=0;
	}
} else {
	$result=0;
}
if (is_array($result)) {
	$smarty->assign('errors',$result);
	$smarty->assign('stackId',$stackId);
    $smarty->assign('stackDate',$_REQUEST['stackDate']);
    $smarty->assign('stackDescription',$_REQUEST['stackDescription']);
    $smarty->assign('debitAccount',$_REQUEST['debitAccount']);
    $smarty->assign('creditAccount',$_REQUEST['creditAccount']);
    $smarty->assign('debitAmount',$_REQUEST['debitAmount']);
    $smarty->assign('creditAmount',$_REQUEST['creditAmount']);
    $smarty->assign('debitText',$_REQUEST['debitText']);
    $smarty->assign('creditText',$_REQUEST['creditText']);
    if (isset($_REQUEST['statementId'])) {
    	$smarty->assign('statementId',$_REQUEST['statementId']);
    }
} else {
	if ($stackId!=0) {
		$stackEntry=$accountinglib->getStackTransaction($bookId,$_REQUEST['stackId']);
		$smarty->assign('stackId',$stackId);	
	    $smarty->assign('stackDate',$stackEntry['stackDate']);
	    $smarty->assign('stackDescription',$stackEntry['stackDescription']);
	    $debitAccount=array();
	    $debitAmount=array();
	    $debitText=array();
113
	    for($i=0, $iCountStackEntryDebit = count($stackEntry['debit']); $i<$iCountStackEntryDebit; $i++) {
114 115 116 117 118 119 120
	    	$debitAccount[]=$stackEntry['debit'][$i]['stackItemAccountId'];
	    	$debitAmount[]=$stackEntry['debit'][$i]['stackItemAmount'];
	    	$debitText[]=$stackEntry['debit'][$i]['stackItemText'];
	    }
	    $creditAccount=array();
	    $creditAmount=array();
	    $creditText=array();
121
	    for($i=0, $iCountStackEntryCredit = count($stackEntry['credit']);$i<$iCountStackEntryCredit; $i++) {
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
	    	$creditAccount[]=$stackEntry['credit'][$i]['stackItemAccountId'];
	    	$creditAmount[]=$stackEntry['credit'][$i]['stackItemAmount'];
	    	$creditText[]=$stackEntry['credit'][$i]['stackItemText'];
	    }
	    $smarty->assign('debitAccount',$debitAccount);
	    $smarty->assign('creditAccount',$creditAccount);
	    $smarty->assign('debitAmount',$debitAmount);
	    $smarty->assign('creditAmount',$creditAmount);
	    $smarty->assign('debitText',$debitText);
	    $smarty->assign('creditText',$creditText);
	} else {
		$smarty->assign('stackId',$stackId);
		$smarty->assign('debitAccount',array(''));
		$smarty->assign('creditAccount',array(''));
		$smarty->assign('debitAmount',array(''));
		$smarty->assign('creditAmount',array(''));
		$smarty->assign('debitText',array(''));
		$smarty->assign('creditText',array(''));
	}
}

ask_ticket('accounting');

if ($globalperms->acct_book or $objectperms->acct_book) {
	$smarty->assign('canBook',true);
} else {
	$smarty->assign('canBook',false);
}

$stack=$accountinglib->getStack($bookId);
$smarty->assign('stack',$stack);
$smarty->assign('mid','tiki-accounting_stack.tpl');
154
$smarty->display("tiki.tpl");