tiki-edit_invoice.php 5.04 KB
Newer Older
marclaporte's avatar
marclaporte committed
1
<?php
changi67's avatar
changi67 committed
2 3 4
/**
 * @package tikiwiki
 */
5
// (c) Copyright 2002-2016 by authors of the Tiki Wiki CMS Groupware Project
6
//
changi67's avatar
changi67 committed
7 8 9 10
// 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.
// $Id$

marclaporte's avatar
marclaporte committed
11
require_once('tiki-setup.php');
12
$trklib = TikiLib::lib('trk');
marclaporte's avatar
marclaporte committed
13 14 15 16 17

$access->check_feature('feature_invoice');
$access->check_permission('tiki_p_admin');

//check if profile is created
18
if ($trklib->get_tracker_by_name("Invoice Items") < 1) {
19
	$smarty->assign('msg', tra('You need to apply the "Invoice" profile'));
marclaporte's avatar
marclaporte committed
20 21 22 23
	$smarty->display("error.tpl");
	die;
}

24
(int)$_REQUEST['InvoiceId'] = $_REQUEST['InvoiceId'];
marclaporte's avatar
marclaporte committed
25

26
//handle saving data (edit or update)
27
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
28
	//start invoice
29
	/*
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
	$_REQUEST['InvoiceId'] = $trklib->replaceItemFromRequestValues(
					$trklib->get_tracker_by_name("Invoices"),
					array(
						"Client Id",
						"Invoice Number",
						"Date Issued",
						"Payment Term",
						"Tax 1 Description",
						"Tax 1 Rate",
						"Tax 2 Description",
						"Tax 2 Rate",
						"Invoice Note",
						"Days Payment Due",
					),
					$_REQUEST, $_REQUEST['InvoiceId']
45 46
	);*/
	die;
47
	//end invoice
48

49
	//start invoice items
50
	$invoiceItems = array();
51

52 53
	$_TEMP = $_REQUEST;
	$itemsToDelete = array();
54
	foreach (explode(',', $_REQUEST['InvoiceItemIds']) as $itemId) {
55 56
		$itemsToDelete[$itemId] = $itemId;
	}
57

58
	$_TEMP['InvoiceId'] = array();
59
	for ($i = 0, $count_InvoiceItemId = count($_REQUEST['InvoiceItemId']); $i < $count_InvoiceItemId; $i++) {
60
		$_TEMP['InvoiceId'][$i] = $_REQUEST['InvoiceId'];
61

62
		$invoiceItem = $trklib->replaceItemFromRequestValues(
63 64 65 66 67 68 69 70 71 72 73
			$trklib->get_tracker_by_name("Invoice Items"),
			array(
				"Invoice Id",
				"Amount",
				"Quantity",
				"Work Description",
				"Taxable",
			),
			$_TEMP,
			$_REQUEST['InvoiceItemId'][$i],
			$i
74
		);
75

76 77 78
		if (isset($itemsToDelete[$_REQUEST['InvoiceItemId'][$i]])) {
			unset($itemsToDelete[$_REQUEST['InvoiceItemId'][$i]]);
		}
79

80
		array_push($invoiceItems, $invoiceItem);
81
	}
82
	//end invoice items
83 84

	//here I need to delete items that were deleted on the page
85
	foreach ($itemsToDelete as $itemId) {
86 87
		$trklib->remove_tracker_item($itemId);
	}
88
	//end delete
89

90
	header('Location: tiki-view_invoice.php?InvoiceId='.$_REQUEST['InvoiceId']);
91 92 93 94 95
	die;
}

$invoiceItems = array();
if (!empty($_REQUEST['InvoiceId'])) {
96
	$invoice = Tracker_Query::tracker("Invoices")
97 98 99
		->byName()
		->equals($_REQUEST['InvoiceId'])
		->getOne();
100

101 102
	$invoice['Item Ids'] = implode(',', $invoice['Item Ids']);
	$smarty->assign("invoice", $invoice);
103

104
	$invoiceItems = Tracker_Query::tracker("Invoice Items")
105 106 107 108
		->byName()
		->fields(array("Invoice Id"))
		->search(array($_REQUEST['InvoiceId']))
		->query();
109 110
} else {
	$_REQUEST['InvoiceId'] = 0;
111 112
}

113
//give the user the last invoice number
114
$LastInvoice = Tracker_Query::tracker("Invoices")
115 116 117 118 119 120 121
	->byName()
	->limit(0)
	->offset(1)
	->desc(true)
	->excludeDetails()
	->getOne();

122 123 124
$NewInvoiceNumber = (isset($LastInvoice["Invoice Number"]) ? $LastInvoice["Invoice Number"] + 1 : 1);
$smarty->assign("NewInvoiceNumber", $NewInvoiceNumber);

125
$smarty->assign("InvoiceId", $_REQUEST['InvoiceId']);
126 127
$smarty->assign("clients", Tracker_Query::tracker("Invoice Clients")->byName()->query());
$smarty->assign("setting", Tracker_Query::tracker("Invoice Settings")->byName()->getOne());
128 129 130 131 132 133 134 135 136 137 138 139

//we add an extra item to the end of invoiceItems, so we can duplicate it on the page
if (count($invoiceItems) < 1) {
	$invoiceItems[] = array(
		"Quantity" => "",
		"Work Description" => "",
		"Taxable" => "",
		"Amount" => "",
	);
}
$smarty->assign("invoiceItems", $invoiceItems);

140 141
$headerlib->add_jq_onready(
    "function setupTotal() {
142 143 144 145 146 147 148
		$('#InvoiceForm :input')
			.unbind('change')
			.change(function() {
				findTotal();
			})
			.change();
	}
149

150 151 152 153 154 155 156 157
	function findTotal() {
		var total = 0;
		$('.InvoiceItem').each(function() {
			var itemTotal = $(this).find('.InvoiceQuantity').val() * $(this).find('.InvoiceAmount').val();
			total += itemTotal;
		});
		$('#Amount').text(total);
	}
158

159
	setupTotal();
160

161 162 163
	$('#InvoiceNewItem').click(function() {
		var lastInvoiceItem = $('.InvoiceItem:last');
		var newInvoiceItem = lastInvoiceItem.clone();
164

165
		newInvoiceItem.find(':input').not(':checkbox,:button').val('');
166

167
		newInvoiceItem.insertAfter(lastInvoiceItem);
168

169 170
		setupTotal();
	});
171

172 173 174 175 176 177 178 179
	$('#InvoiceForm').click(function(e) {
		if ($(e.target).hasClass('DeleteItem')) {
			if ($('.InvoiceItem').length > 1) {
				$(e.target).parent().parent().remove();
			}
			return false;
		}
	});
180

181 182 183 184 185 186
	$('#InvoiceForm').submit(function() {
		$('.InvoiceTaxable').each(function() {
			var InvoiceTaxable = $(this);
			if (!InvoiceTaxable.is(':checked')) {
				InvoiceTaxable
					.val('n')
187
					.prop('checked', 'true');
188 189
			}
		});
190

191 192
		$('.InvoiceItemId').each(function() {
			var InvoiceItemId = $(this);
193
			InvoiceItemId.val(InvoiceItemId.val() ? InvoiceItemId.val() : '0');
194
		});
195

196 197
		var InvoiceId = $('#InvoiceId');
		InvoiceId.val(InvoiceId.val() ? InvoiceId.val() : 0);
198 199

		return false;
200 201
	});"
);
202

marclaporte's avatar
marclaporte committed
203 204
// Display the template
$smarty->assign('mid', 'tiki-edit_invoice.tpl');
changi67's avatar
changi67 committed
205
$smarty->display("tiki.tpl");