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}
33if (! isset($_REQUEST['action'])) {
34	$_REQUEST['action'] = '';
35}
36
37$globalperms = Perms::get();
38$accountinglib = TikiLib::lib('accounting');
39
40if ($_REQUEST['book_start_Year']) {
41	$bookStartDate = new DateTime();
42	$bookStartDate->setDate(
43		$_REQUEST['book_start_Year'],
44		$_REQUEST['book_start_Month'],
45		$_REQUEST['book_start_Day']
46	);
47}
48if ($_REQUEST['book_end_Year']) {
49	$bookEndDate = new DateTime();
50	$bookEndDate->setDate(
51		$_REQUEST['book_end_Year'],
52		$_REQUEST['book_end_Month'],
53		$_REQUEST['book_end_Day']
54	);
55}
56if (! empty($_REQUEST['action'])) {
57	switch ($_REQUEST['action']) {
58		case 'create':
59			if (! $globalperms->acct_create_book) {
60				Feedback::error(tra('You do not have permission to create a book'));
61			} elseif($access->checkCsrf()) {
62				$bookId = $accountinglib->createBook(
63					$_POST['bookName'],
64					'n',
65					date_format($bookStartDate, 'Y-m-d H:i:s'),
66					date_format($bookEndDate, 'Y-m-d H:i:s'),
67					$_POST['bookCurrency'],
68					$_POST['bookCurrencyPos'],
69					$_POST['bookDecimals'],
70					$_POST['bookDecPoint'],
71					$_POST['bookThousand'],
72					$_POST['exportSeparator'],
73					$_POST['exportEOL'],
74					$_POST['exportQuote'],
75					$_POST['bookAutoTax']
76				);
77				if (! is_numeric($bookId)) {
78					$errors[] = tra($bookId);
79					Feedback::error(implode("\n", $errors));
80					$smarty->assign('bookName', $_POST['bookName']);
81					$smarty->assign('bookStartDate', $bookStartDate);
82					$smarty->assign('bookEndDate', $bookEndDate);
83					$smarty->assign('bookCurrency', $_POST['bookCurrency']);
84					$smarty->assign('bookCurrencyPos', $_POST['bookCurrencyPos']);
85					$smarty->assign('bookDecimals', $_POST['bookDecimals']);
86					$smarty->assign('bookDecPoint', $_POST['bookDecPoint']);
87					$smarty->assign('bookThousand', $_POST['bookThousand']);
88					$smarty->assign('exportSeparator', $_POST['exportSeparator']);
89					$smarty->assign('exportEOL', $_POST['exportEOL']);
90					$smarty->assign('exportQuote', $_POST['exportQuote']);
91					$smarty->assign('bookAutoTax', $_POST['bookAutoTax']);
92				} else {
93					Feedback::success(tr('Book %0 successfully created', $_POST['bookName']));
94				}
95			}
96			break;
97		case 'close':
98			if (! $globalperms->acct_create_book) {
99				Feedback::error(tra('You do not have permission to close this book'));
100			} elseif ($access->checkCsrfForm(tra('Close book (this action cannot be undone)?'))) {
101				$res = $accountinglib->closeBook($_POST['bookId']);
102				if ($res) {
103					Feedback::success(tra('Book successfully closed'));
104				} else {
105					Feedback::error(tra('The attempt to close the book was unsuccessful'));
106				}
107			}
108			break;
109		case 'view':
110			break;
111		default://list
112	}
113}
114$books = $accountinglib->listBooks();
115$filtered = Perms::filter(
116	[ 'type' => 'accounting book'],
117	'object',
118	$books,
119	[ 'object' => 'bookName' ],
120	'acct_view'
121);
122$smarty->assign('books', $books);
123$smarty->assign('canCreate', $globalperms->acct_create_book);
124$smarty->assign('mid', 'tiki-accounting_books.tpl');
125$smarty->display("tiki.tpl");
126