Unstable release 2.
[fa-stable.git] / sales / includes / ui / sales_credit_ui.inc
1 <?php
2
3 include_once($path_to_root . "/includes/ui.inc");
4
5 // ------------------------------------------------------------------------------
6
7 function display_credit_header(&$order)
8 {
9         global $table_style;
10         start_table("width=80% $table_style");
11         echo "<tr><td valign=top>"; // outer table
12         echo "<table>";
13
14         $customer_error = "";
15
16     if (!isset($_POST['customer_id']) && (get_global_customer() != reserved_words::get_all()))
17         $_POST['customer_id'] = get_global_customer();
18
19         customer_list_row(_("Customer:"), 'customer_id', null, false, true);
20
21         if ($order->customer_id != $_POST['customer_id'])
22         {
23                 // customer has changed
24
25                 // delete all the order items - drastic but necessary because of
26                 // change of currency, sales type, etc
27                 $order->clear_items();
28
29                 // clear the branch selection
30                 unset($_POST['branch_id']);
31         }
32
33         customer_branches_list_row(_("Branch:"), $_POST['customer_id'], 'branch_id', null, false, true, true);
34
35         //if (($_SESSION['credit_items']->order_no == 0) ||
36         //      ($order->customer_id != $_POST['customer_id']) ||
37         //      ($order->Branch != $_POST['branch_id']))
38         //      $customer_error = get_customer_details_to_order($order, $_POST['customer_id'], $_POST['branch_id']);
39         if (($order->customer_id != $_POST['customer_id']) ||
40                 ($order->Branch != $_POST['branch_id']))
41                 $customer_error = get_customer_details_to_order($order, $_POST['customer_id'], $_POST['branch_id']);
42
43         set_global_customer($_POST['customer_id']);
44
45         if (!isset($_POST['ref']))
46                 $_POST['ref'] = references::get_next(11);
47
48         ref_row(_("Reference:"), 'ref');
49
50         echo "</table>";
51
52         echo "</td><td>"; // outer table
53
54         if (!is_company_currency($order->customer_currency))
55         {
56                 echo "<table height='5'>";
57                 label_row(_("Customer Currency:"), $order->customer_currency);
58                 exchange_rate_display($order->customer_currency, get_company_currency(),
59                         $_POST['OrderDate'], true);
60                 echo "</table>";
61                 echo "</td><td>"; // outer table
62         }
63
64         echo "<table height='5'>";
65
66     if (!isset($_POST['sales_type_id']))
67         $_POST['sales_type_id'] = $order->default_sales_type;
68     sales_types_list_row(_("Sales Type"), 'sales_type_id', $_POST['sales_type_id']);
69
70         label_row(_("Customer Discount:"), ($order->default_discount * 100) . "%");
71         echo "</table>";
72
73         echo "</td><td>"; // outer table
74
75         echo "<table height='5'>";
76
77         if (!isset($_POST['OrderDate']) || $_POST['OrderDate'] == "")
78                 $_POST['OrderDate'] = $order->orig_order_date;
79
80         date_row(_("Date:"), 'OrderDate');
81
82         if (!isset($_POST['tax_group_id']) || $_POST['tax_group_id'] == "")
83                 $_POST['tax_group_id'] = $order->tax_group_id;
84     tax_groups_list_row(_("Tax Group:"), 'tax_group_id', null, true);
85
86         echo "</table>";
87
88         echo "</td></tr>";
89
90         end_table(1); // outer table
91
92         return $customer_error;
93 }
94
95 //---------------------------------------------------------------------------------
96
97 function display_credit_items($title, &$order)
98 {
99         global $table_style, $path_to_root;
100
101         display_heading($title);
102         start_table("$table_style width=90%");
103         $th = array(_("Item Code"), _("Item Description"), _("Quantity"), _("Unit"),
104                 _("Price"), _("Discount %"), _("Total"));
105         table_header($th);
106
107         $subtotal = 0;
108         $k = 0;  //row colour counter
109
110         foreach ($order->line_items as $line)
111         {
112
113                 $line_total =   $line->quantity * $line->price * (1 - $line->discount_percent);
114
115                 if (!isset($_GET['Edit']) || $_GET['Edit'] != $line->line_no)
116                 {
117                 alt_table_row_color($k);
118
119                 label_cell("<a target='_blank' href='$path_to_root/inventory/inquiry/stock_status.php?" . SID . "stock_id=" . $line->stock_id . "'>$line->stock_id</a>");
120                 label_cell($line->item_description);
121                 qty_cell($line->quantity);
122                 label_cell($line->units);
123                 amount_cell($line->price);
124
125                         amount_cell($line->discount_percent * 100);
126                 amount_cell($line_total);
127
128                 edit_link_cell(SID . "Edit=$line->line_no");
129                 delete_link_cell(SID . "Delete=$line->line_no");
130
131                         //labelt_cell(get_tax_free_price_for_item($line->stock_id, $line_total, $_POST['tax_group_id']));
132
133                 end_row();
134                 }
135                 else
136                 {
137                         credit_edit_item_controls($order, $line->stock_id);
138                 }
139
140                 $subtotal += $line_total;
141         }
142
143         if (!isset($_GET['Edit']))
144                 credit_edit_item_controls($order);
145
146         $display_sub_total = number_format2($subtotal,user_price_dec());
147         label_row(_("Sub-total"), $display_sub_total, "colspan=6 align=right", "align=right");
148
149         if (!isset($_POST['ChargeFreightCost']) OR ($_POST['ChargeFreightCost'] == ""))
150                 $_POST['ChargeFreightCost'] = 0;
151
152         text_row(_("Shipping"), 'ChargeFreightCost', $_POST['ChargeFreightCost'], 8, 8, "colspan=6 align=right");
153
154     $taxes = $order->get_taxes($_POST['tax_group_id'], $_POST['ChargeFreightCost']);
155
156         $tax_total = display_edit_tax_items($taxes, 6);
157
158     $display_total = number_format2(($subtotal + $_POST['ChargeFreightCost'] + $tax_total), user_price_dec());
159     label_row(_("Credit Note Total"), $display_total, "colspan=6 align=right","align=right");
160
161     end_table();
162 }
163
164 //---------------------------------------------------------------------------------
165
166 function credit_edit_item_controls(&$order, $stock_id=null)
167 {
168         start_row();
169
170         if (isset($_GET['Edit']) and $stock_id!=null)
171         {
172                 $line_no = $_GET['Edit'];
173                 hidden('line_no', $line_no);
174
175                 if (!isset($_POST['stock_id']))
176                         $_POST['stock_id'] = $order->line_items[$line_no]->stock_id;
177                 if (!isset($_POST['qty']) OR ($_POST['qty']==""))
178                         $_POST['qty'] = $order->line_items[$line_no]->quantity;
179                 if (!isset($_POST['price']) OR ($_POST['price']==""))
180                         $_POST['price'] = $order->line_items[$line_no]->price;
181                 if (!isset($_POST['Disc']) OR ($_POST['Disc']==""))
182                         $_POST['Disc'] = ($order->line_items[$line_no]->discount_percent)*100;
183
184                 $_POST['units'] = $order->line_items[$line_no]->units;
185
186                 hidden('stock_id', $_POST['stock_id']);
187                 label_cell($_POST['stock_id']);
188                 label_cell($order->line_items[$line_no]->item_description);
189         }
190         else
191         {
192                 hidden('line_no', ($_SESSION['credit_items']->lines_on_order + 1));
193         echo "<td colspan=2>";
194         stock_items_list('stock_id', null, false, true);
195         echo "</td>";
196
197         $item_info = get_item_edit_info($_POST['stock_id']);
198
199                 $_POST['units'] = $item_info["units"];
200
201                 $_POST['qty'] = 0;
202                 $_POST['price'] = get_price($_POST['stock_id'], $order->customer_id);
203                 // default to the customer's discount %
204                 $_POST['Disc'] = $order->default_discount * 100;
205         }
206
207
208         text_cells(null, 'qty', $_POST['qty'], 13, 15);
209         label_cell($_POST['units']);
210         text_cells(null, 'price',  null, 13, 15);
211         text_cells(null, 'Disc', $_POST['Disc'], 7, 5);
212         amount_cell($_POST['qty'] * $_POST['price'] * (1 - $_POST['Disc']/100));
213
214         if (isset($_GET['Edit']))
215         {
216         submit_cells('UpdateItem', _("Update"));
217         submit_cells('CancelItemChanges', _("Cancel"));
218         }
219         else
220         {
221                 submit_cells('AddItem', _("Add Item"), "colspan=2");
222         }
223
224         end_row();
225 }
226
227
228 //---------------------------------------------------------------------------------
229
230 function credit_options_controls()
231 {
232         global $table_style2;
233         echo "<br>";
234         start_table("$table_style2");
235
236         credit_type_list_row(_("Credit Note Type"), 'CreditType', null, true);
237
238         if ($_POST['CreditType'] == "Return")
239         {
240
241                 /*if the credit note is a return of goods then need to know which location to receive them into */
242                 if (!isset($_POST['Location']))
243                         $_POST['Location'] = $_SESSION['credit_items']->Location;
244                 locations_list_row(_("Items Returned to Location"), 'Location', $_POST['Location']);
245
246         }
247         else
248         {
249                 /* the goods are to be written off to somewhere */
250                 gl_all_accounts_list_row(_("Write off the cost of the items to"), 'WriteOffGLCode', null);
251         }
252
253         textarea_row(_("Memo"), "CreditText", null, 51, 3);
254         echo "</table>";
255 }
256
257
258 //---------------------------------------------------------------------------------
259
260 ?>