b0c3c6dedfa08bd8d0146dc2ee048df8ca8db1b3
[fa-stable.git] / sales / allocations / customer_allocation_main.php
1 <?php
2
3 $path_to_root="../..";
4 $page_security = 3;
5 include($path_to_root . "/includes/db_pager.inc");
6 include_once($path_to_root . "/includes/session.inc");
7
8 include_once($path_to_root . "/sales/includes/sales_ui.inc");
9 include_once($path_to_root . "/sales/includes/sales_db.inc");
10 $js = "";
11 if ($use_popup_windows)
12         $js .= get_js_open_window(900, 500);
13 page(_("Customer Allocations"), false, false, "", $js);
14
15 //--------------------------------------------------------------------------------
16 if ($ret = context_restore()) {
17         if(isset($ret['customer_id']))
18                 $_POST['customer_id'] = $ret['customer_id'];
19 }
20 if (isset($_POST['_customer_id_editor'])) {
21         context_call($path_to_root.'/sales/manage/customers.php?debtor_no='.$_POST['customer_id'] );
22 }
23
24 start_form();
25         /* show all outstanding receipts and credits to be allocated */
26         /*Clear any previous allocation records */
27         if (isset($_SESSION['alloc']))
28         {
29                 unset($_SESSION['alloc']->allocs);
30                 unset($_SESSION['alloc']);
31         }
32     if (!isset($_POST['customer_id']))
33         $_POST['customer_id'] = get_global_customer();
34
35     echo "<center>" . _("Select a customer: ") . "&nbsp;&nbsp;";
36         customer_list('customer_id', $_POST['customer_id'], true, true);
37     echo "<br>";
38     check(_("Show Settled Items:"), 'ShowSettled', null, true);
39         echo "</center><br><br>";
40
41 end_form();
42
43         set_global_customer($_POST['customer_id']);
44
45         if (isset($_POST['customer_id']) && ($_POST['customer_id'] == reserved_words::get_all()))
46         {
47                 unset($_POST['customer_id']);
48         }
49
50         /*if (isset($_POST['customer_id'])) {
51                 $custCurr = get_customer_currency($_POST['customer_id']);
52                 if (!is_company_currency($custCurr))
53                         echo _("Customer Currency:") . $custCurr;
54         }*/
55
56         $settled = false;
57         if (check_value('ShowSettled'))
58                 $settled = true;
59
60         $customer_id = null;
61         if (isset($_POST['customer_id']))
62                 $customer_id = $_POST['customer_id'];
63
64 //--------------------------------------------------------------------------------
65 function systype_name($dummy, $type)
66 {
67         return systypes::name($type);
68 }
69
70 function trans_view($trans)
71 {
72         return get_trans_view_str($trans["type"], $trans["trans_no"]);
73 }
74
75 function alloc_link($row)
76 {
77         return pager_link(_("Allocate"),
78                 "/sales/allocations/customer_allocate.php?trans_no="
79                         .$row["trans_no"] . "&trans_type=" . $row["type"]               
80         );
81 }
82
83 function amount_left($row)
84 {
85         return $row["Total"]-$row["alloc"];
86 }
87
88 function check_settled($row)
89 {
90         return $row['settled'] == 1;
91 }
92
93
94 $sql = get_allocatable_from_cust_sql($customer_id, $settled);
95
96 $cols = array(
97         _("Transaction Type") => array('fun'=>'systype_name'),
98         _("#") => array('fun'=>'trans_view'),
99         _("Reference"), 
100         _("Date") => array('type'=>'date', 'ord'=>'asc'),
101         _("Customer") => array('ord'=>''),
102         _("Currency") => array('align'=>'center'),
103         _("Total") => 'amount', 
104         _("Left to Allocate") => array('align'=>'right','insert'=>true, 'fun'=>'amount_left'), 
105         array('insert'=>true, 'fun'=>'alloc_link')
106         );
107
108 if (isset($_POST['customer_id'])) {
109         $cols[_("Customer")] = 'skip';
110         $cols[_("Currency")] = 'skip';
111 }
112
113 $table =& new_db_pager('alloc_tbl', $sql, $cols);
114 $table->set_marker('check_settled', _("Marked items are settled."), 'settledbg', 'settledfg');
115
116 if (get_post('_ShowSettled_update') || get_post('_customer_id_update')) {
117         $table->set_sql($sql);
118         $table->set_columns($cols);
119         $Ajax->activate('alloc_tbl');
120 }
121
122         start_form();
123         display_db_pager($table);
124         end_form();
125 end_page();
126
127 ?>