4ba875bd9524161fa9ab081e0223281a28cbe007
[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         set_global_customer($_POST['customer_id']);
42
43         if (isset($_POST['customer_id']) && ($_POST['customer_id'] == reserved_words::get_all()))
44         {
45                 unset($_POST['customer_id']);
46         }
47
48         /*if (isset($_POST['customer_id'])) {
49                 $custCurr = get_customer_currency($_POST['customer_id']);
50                 if (!is_company_currency($custCurr))
51                         echo _("Customer Currency:") . $custCurr;
52         }*/
53
54         $settled = false;
55         if (check_value('ShowSettled'))
56                 $settled = true;
57
58         $customer_id = null;
59         if (isset($_POST['customer_id']))
60                 $customer_id = $_POST['customer_id'];
61
62 //--------------------------------------------------------------------------------
63 function systype_name($dummy, $type)
64 {
65         return systypes::name($type);
66 }
67
68 function trans_view($trans)
69 {
70         return get_trans_view_str($trans["type"], $trans["trans_no"]);
71 }
72
73 function alloc_link($row)
74 {
75         return pager_link(_("Allocate"),
76                 "/sales/allocations/customer_allocate.php?trans_no="
77                         .$row["trans_no"] . "&trans_type=" . $row["type"]               
78         );
79 }
80
81 function amount_left($row)
82 {
83         return $row["Total"]-$row["alloc"];
84 }
85
86 function check_settled($row)
87 {
88         return $row['settled'] == 1;
89 }
90
91
92 $sql = get_allocatable_from_cust_sql($customer_id, $settled);
93
94 $cols = array(
95         _("Transaction Type") => array('fun'=>'systype_name'),
96         _("#") => array('fun'=>'trans_view'),
97         _("Reference"), 
98         _("Date") => array('type'=>'date', 'ord'=>'asc'),
99         _("Customer") => array('ord'=>''),
100         _("Currency") => array('align'=>'center'),
101         _("Total") => 'amount', 
102         _("Left to Allocate") => array('align'=>'right','insert'=>true, 'fun'=>'amount_left'), 
103         array('insert'=>true, 'fun'=>'alloc_link')
104         );
105
106 if (isset($_POST['customer_id'])) {
107         $cols[_("Customer")] = 'skip';
108         $cols[_("Currency")] = 'skip';
109 }
110
111 $table =& new_db_pager('alloc_tbl', $sql, $cols);
112 $table->set_marker('check_settled', _("Marked items are settled."), 'settledbg', 'settledfg');
113
114 if (get_post('_ShowSettled_update') || get_post('_customer_id_update')) {
115         $table->set_sql($sql);
116         $table->set_columns($cols);
117 }
118
119 display_db_pager($table);
120 end_form();
121
122 end_page();
123 ?>