1<?php
2/**********************************************************************
3    Copyright (C) FrontAccounting, LLC.
4	Released under the terms of the GNU General Public License, GPL,
5	as published by the Free Software Foundation, either version 3
6	of the License, or (at your option) any later version.
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11***********************************************************************/
12$page_security = 'SA_SALESALLOC';
13$path_to_root = "../..";
14
15include($path_to_root . "/includes/ui/allocation_cart.inc");
16include_once($path_to_root . "/includes/session.inc");
17include_once($path_to_root . "/includes/data_checks.inc");
18include_once($path_to_root . "/sales/includes/sales_ui.inc");
19include_once($path_to_root . "/sales/includes/sales_db.inc");
20//include_once($path_to_root . "/sales/includes/ui/cust_alloc_ui.inc");
21
22$js = "";
23if ($use_popup_windows)
24	$js .= get_js_open_window(900, 500);
25
26add_js_file('allocate.js');
27
28page(_($help_context = "Allocate Customer Payment or Credit Note"), false, false, "", $js);
29
30//--------------------------------------------------------------------------------
31
32function clear_allocations()
33{
34	if (isset($_SESSION['alloc']))
35	{
36		unset($_SESSION['alloc']->allocs);
37		unset($_SESSION['alloc']);
38	}
39	//session_register('alloc');
40}
41
42//--------------------------------------------------------------------------------
43
44function edit_allocations_for_transaction($type, $trans_no)
45{
46	global $systypes_array;
47
48	$cart = $_SESSION['alloc'];
49
50    display_heading(sprintf(_("Allocation of %s # %d"), $systypes_array[$cart->type], $cart->trans_no));
51
52    display_heading($cart->person_name);
53
54    display_heading2(_("Date:") . " <b>" . $cart->date_ . "</b>");
55   	display_heading2(_("Total:"). " <b>" . price_format($cart->bank_amount).' '.$cart->currency."</b>");
56
57	if ($cart->currency != $cart->person_curr)
58	{
59	    $total = _("Total in clearing currency:") . " <b>" . price_format($cart->amount)."</b>"
60	    	. sprintf(" %s (%s %s/%s)", $cart->person_curr, exrate_format($cart->bank_amount/$cart->amount), $cart->currency, $cart->person_curr);
61    	display_heading2($total);
62	}
63
64    echo "<br>";
65
66	start_form();
67	div_start('alloc_tbl');
68    if (count($cart->allocs) > 0)
69    {
70		show_allocatable(true);
71       	submit_center_first('UpdateDisplay', _("Refresh"), _('Start again allocation of selected amount'), true);
72       	submit('Process', _("Process"), true, _('Process allocations'), 'default');
73   		submit_center_last('Cancel', _("Back to Allocations"),_('Abandon allocations and return to selection of allocatable amounts'), 'cancel');
74	}
75	else
76	{
77    	display_note(_("There are no unsettled transactions to allocate."), 0, 1);
78
79   		submit_center('Cancel', _("Back to Allocations"), true,
80			_('Abandon allocations and return to selection of allocatable amounts'), 'cancel');
81    }
82	div_end();
83  	end_form();
84}
85
86//--------------------------------------------------------------------------------
87
88if (isset($_POST['Process']))
89{
90	if (check_allocations())
91	{
92		$_SESSION['alloc']->write();
93		clear_allocations();
94		$_POST['Cancel'] = 1;
95	}
96}
97//--------------------------------------------------------------------------------
98
99if (isset($_POST['Cancel']))
100{
101	clear_allocations();
102	meta_forward($path_to_root . "/sales/allocations/customer_allocation_main.php");
103}
104
105//--------------------------------------------------------------------------------
106
107if (isset($_GET['trans_no']) && isset($_GET['trans_type']))
108{
109	clear_allocations();
110	$_SESSION['alloc'] = new allocation($_GET['trans_type'], $_GET['trans_no'], @$_GET['debtor_no'], PT_CUSTOMER);
111}
112
113if(get_post('UpdateDisplay'))
114{
115	$_SESSION['alloc']->read();
116	$Ajax->activate('alloc_tbl');
117}
118
119if (isset($_SESSION['alloc']))
120{
121	edit_allocations_for_transaction($_SESSION['alloc']->type, $_SESSION['alloc']->trans_no);
122}
123
124//--------------------------------------------------------------------------------
125
126end_page();
127
128?>