1<?php
2/**
3 * @package tikiwiki
4 */
5// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
6//
7// All Rights Reserved. See copyright.txt for details and a complete list of authors.
8// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
9// $Id$
10
11/***
12 *
13 * @var \TikiAccessLib  $access
14 *
15 * @var \AccountingLib  $accountinglib
16 *
17 *
18 * @var \Smarty_Tiki    $smarty
19 *
20 * Define the current section
21 * @var string $section
22 */
23
24$section = 'accounting';
25require_once('tiki-setup.php');
26
27// Feature available?
28if ($prefs['feature_accounting'] != 'y') {
29	$smarty->assign('msg', tra('This feature is disabled') . ': feature_accounting');
30	$smarty->display('error.tpl');
31	die;
32}
33
34$globalperms = Perms::get();
35$objectperms = Perms::get([ 'type' => 'accounting book', 'object' => $bookId ]);
36
37if (! ($globalperms->acct_book or $objectperms->acct_book)) {
38	$smarty->assign('msg', tra('You do not have the right to book'));
39	$smarty->display('error.tpl');
40	die;
41}
42
43if (! isset($_REQUEST['bookId'])) {
44	$smarty->assign('msg', tra('Missing book id'));
45	$smarty->display('error.tpl');
46	die;
47}
48$bookId = $_REQUEST['bookId'];
49$smarty->assign('bookId', $bookId);
50
51$accountinglib = TikiLib::lib('accounting');
52$book = $accountinglib->getBook($bookId);
53$smarty->assign('book', $book);
54
55$accounts = $accountinglib->getAccounts($bookId, $all = true);
56$smarty->assign('accounts', $accounts);
57
58if ($_POST['journal_Year']) {
59	$journalDate = new DateTime();
60	$journalDate->setDate(
61		$_POST['journal_Year'],
62		$_POST['journal_Month'],
63		$_POST['journal_Day']
64	);
65}
66
67if (isset($_POST['book']) && $access->checkCsrfForm(tr('Record entry in book %0?', $book['bookName']))) {
68	$result = $accountinglib->book(
69		$bookId,
70		$journalDate,
71		$_POST['journalDescription'],
72		$_POST['debitAccount'],
73		$_POST['creditAccount'],
74		$_POST['debitAmount'],
75		$_POST['creditAmount'],
76		$_POST['debitText'],
77		$_POST['creditText']
78	);
79	if (is_numeric($result)) {
80		if (isset($_POST['statementId'])) {
81			$accountinglib->updateStatement($bookId, $_POST['statementId'], $result);
82		}
83	}
84} else {
85	$result = 0;
86}
87
88if (is_array($result)) {
89	Feedback::error(['mes' => $result]);
90	$smarty->assign('journalDate', $journalDate);
91	$smarty->assign('journalDescription', $_POST['journalDescription']);
92	$smarty->assign('debitAccount', $_POST['debitAccount']);
93	$smarty->assign('creditAccount', $_POST['creditAccount']);
94	$smarty->assign('debitAmount', $_POST['debitAmount']);
95	$smarty->assign('creditAmount', $_POST['creditAmount']);
96	$smarty->assign('debitText', $_POST['debitText']);
97	$smarty->assign('creditText', $_POST['creditText']);
98	if (isset($_POST['statementId'])) {
99		$smarty->assign('statementId', $_POST['statementId']);
100	}
101} else {
102	if (is_numeric($result) && $result > 0) {
103		Feedback::success(tr('Journal %0 successfully recorded in book %1', $result, $book['bookName']));
104	}
105	$smarty->assign('debitAccount', ['']);
106	$smarty->assign('creditAccount', ['']);
107	$smarty->assign('debitAmount', ['']);
108	$smarty->assign('creditAmount', ['']);
109	$smarty->assign('debitText', ['']);
110	$smarty->assign('creditText', ['']);
111}
112
113$journal = $accountinglib->getJournal($bookId, '%', '`journalId` DESC', 5);
114$smarty->assign('journal', $journal);
115
116$smarty->assign('req_url', $_SERVER['REQUEST_URI']);
117$smarty->assign('mid', 'tiki-accounting_entry.tpl');
118$smarty->display('tiki.tpl');
119