Customer Payment, Supplier Payment: early discount support.
[fa-stable.git] / sales / allocations / customer_allocate.php
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
15 include($path_to_root . "/includes/ui/allocation_cart.inc");
16 include_once($path_to_root . "/includes/session.inc");
17 include_once($path_to_root . "/includes/data_checks.inc");
18 include_once($path_to_root . "/sales/includes/sales_ui.inc");
19 include_once($path_to_root . "/sales/includes/sales_db.inc");
20
21 $js = "";
22 if ($SysPrefs->use_popup_windows)
23         $js .= get_js_open_window(900, 500);
24
25 add_js_file('allocate.js');
26
27 page(_($help_context = "Allocate Customer Payment or Credit Note"), false, false, "", $js);
28
29 //--------------------------------------------------------------------------------
30
31 function clear_allocations()
32 {
33         if (isset($_SESSION['alloc']))
34         {
35                 unset($_SESSION['alloc']->allocs);
36                 unset($_SESSION['alloc']);
37         }
38         //session_register('alloc');
39 }
40
41 //--------------------------------------------------------------------------------
42
43 function edit_allocations_for_transaction($type, $trans_no)
44 {
45         global $systypes_array;
46
47         $cart = $_SESSION['alloc'];
48         
49         if ($cart->type == ST_JOURNAL && $cart->bank_amount < 0)
50         {
51                 $cart->bank_amount = -$cart->bank_amount;
52                 $cart->amount = -$cart->amount;
53         }       
54     display_heading(sprintf(_("Allocation of %s # %d"), $systypes_array[$cart->type], $cart->trans_no));
55
56     display_heading($cart->person_name);
57
58     display_heading2(_("Date:") . " <b>" . $cart->date_ . "</b>");
59
60         if (floatcmp($cart->bank_amount, $cart->amount))
61                 display_heading2(_("Total:"). " <b>" . price_format($cart->bank_amount).' '.$cart->currency."</b>");
62
63         $total = _("Amount to be settled:") . " <b>" . '<span id="total">'.price_format($cart->amount).'</span> '.$cart->person_curr."</b>";
64         if ($cart->currency != $cart->person_curr)
65                 $total .= sprintf(" (%s %s/%s)",  exrate_format($cart->bank_amount/$cart->amount), $cart->currency, $cart->person_curr);
66         display_heading2($total);
67
68     echo "<br>";
69
70         start_form();
71         div_start('alloc_tbl');
72     if (count($cart->allocs) > 0)
73     {
74                 show_allocatable(true);
75         submit_center_first('UpdateDisplay', _("Refresh"), _('Start again allocation of selected amount'), true);
76         submit('Process', _("Process"), true, _('Process allocations'), 'default');
77                 submit_center_last('Cancel', _("Back to Allocations"),_('Abandon allocations and return to selection of allocatable amounts'), 'cancel');
78         }
79         else
80         {
81         display_note(_("There are no unsettled transactions to allocate."), 0, 1);
82
83                 submit_center('Cancel', _("Back to Allocations"), true,
84                         _('Abandon allocations and return to selection of allocatable amounts'), 'cancel');
85     }
86         div_end();
87         end_form();
88 }
89
90 //--------------------------------------------------------------------------------
91
92 if (isset($_POST['Process']))
93 {
94         if (check_allocations())
95         {
96                 write_cust_allocation($_SESSION['alloc']);
97                 clear_allocations();
98                 $_POST['Cancel'] = 1;
99         }
100 }
101 //--------------------------------------------------------------------------------
102
103 if (isset($_POST['Cancel']))
104 {
105         clear_allocations();
106         meta_forward($path_to_root . "/sales/allocations/customer_allocation_main.php");
107 }
108
109 //--------------------------------------------------------------------------------
110
111 if (isset($_GET['trans_no']) && isset($_GET['trans_type']))
112 {
113         clear_allocations();
114         $_SESSION['alloc'] = new allocation($_GET['trans_type'], $_GET['trans_no'], @$_GET['debtor_no'], PT_CUSTOMER);
115 }
116
117 if(get_post('UpdateDisplay'))
118 {
119         $_SESSION['alloc']->read();
120         $Ajax->activate('alloc_tbl');
121 }
122
123 if (isset($_SESSION['alloc']))
124 {
125         edit_allocations_for_transaction($_SESSION['alloc']->type, $_SESSION['alloc']->trans_no);
126 }
127
128 //--------------------------------------------------------------------------------
129
130 end_page();
131