5acd6ac3d1bd8139ee0f53fe88ac12cda942bad5
[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'] || $order->sales_type != $_POST['sales_type_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         if ($_SESSION['Items']->trans_no==0)
48             ref_row(_("Reference").':', 'ref');
49         else
50             label_row(_("Reference").':', $_POST['ref'] );
51         
52
53
54         echo "</table>";
55
56         echo "</td><td>"; // outer table
57
58         if (!is_company_currency($order->customer_currency))
59         {
60                 echo "<table height='5'>";
61                 label_row(_("Customer Currency:"), $order->customer_currency);
62                 exchange_rate_display($order->customer_currency, get_company_currency(),
63                         $_POST['OrderDate'], true);
64                 echo "</table>";
65                 echo "</td><td>"; // outer table
66         }
67
68         echo "<table height='5'>";
69
70     if (!isset($_POST['sales_type_id']))
71         $_POST['sales_type_id'] = $order->sales_type;
72     sales_types_list_row(_("Sales Type"), 'sales_type_id', $_POST['sales_type_id'], true);
73
74         label_row(_("Customer Discount:"), ($order->default_discount * 100) . "%");
75         echo "</table>";
76
77         echo "</td><td>"; // outer table
78
79         echo "<table height='5'>";
80
81         if (!isset($_POST['OrderDate']) || $_POST['OrderDate'] == "")
82                 $_POST['OrderDate'] = $order->document_date;
83
84         date_row(_("Date:"), 'OrderDate');
85
86 //      if (!isset($_POST['tax_group_id']) || $_POST['tax_group_id'] == "")
87 //              $_POST['tax_group_id'] = $order->tax_group_id;
88 //    tax_groups_list_row(_("Tax Group:"), 'tax_group_id', null, true);
89     shippers_list_row(_("Shipping Company:"), 'ShipperID', $order->ship_via);
90
91         echo "</table>";
92
93         echo "</td></tr>";
94
95         end_table(1); // outer table
96
97         return $customer_error;
98 }
99
100 //---------------------------------------------------------------------------------
101
102 function display_credit_items($title, &$order)
103 {
104     global $table_style, $path_to_root;
105
106     display_heading($title);
107     start_table("$table_style width=90%");
108     $th = array(_("Item Code"), _("Item Description"), _("Quantity"), _("Unit"),
109         _("Price"), _("Discount %"), _("Total"),'');
110
111     if (count($order->line_items)) $th[]= '';
112
113     table_header($th);
114
115     $subtotal = 0;
116     $k = 0;  //row colour counter
117
118     $id = find_submit('Edit');
119
120     foreach ($order->line_items as $line_no=>$line)
121     {
122         $line_total =   round($line->qty_dispatched * $line->price * (1 - $line->discount_percent),
123            user_price_dec());
124
125         if ( $id != $line_no)
126         {
127             alt_table_row_color($k);
128
129             label_cell("<a target='_blank' href='$path_to_root/inventory/inquiry/stock_status.php?" . SID . "stock_id=" . $line->stock_id . "'>$line->stock_id</a>");
130             label_cell($line->item_description, "nowrap");
131             qty_cell($line->qty_dispatched);
132             label_cell($line->units);
133             amount_cell($line->price);
134
135             amount_cell($line->discount_percent * 100);
136             amount_cell($line_total);
137
138             edit_button_cell("Edit$line_no", _('Edit'));
139             edit_button_cell("Delete$line_no", _('Delete'));
140
141             end_row();
142         }
143         else
144         {
145             credit_edit_item_controls($order, $k, $line_no);
146         }
147
148         $subtotal += $line_total;
149     }
150
151     if ($id==-1)
152         credit_edit_item_controls($order, $k);
153
154     $display_sub_total = price_format($subtotal);
155     label_row(_("Sub-total"), $display_sub_total, "colspan=6 align=right", "align=right", 2);
156
157     if (!isset($_POST['ChargeFreightCost']) OR ($_POST['ChargeFreightCost'] == ""))
158         $_POST['ChargeFreightCost'] = price_format(0);
159
160     amount_cells_ex(_("Shipping"), 'ChargeFreightCost', 8, 8, $_POST['ChargeFreightCost'], "colspan=6 align=right");
161     label_cell('', 'colspan=2');
162
163     $taxes = $order->get_taxes($_POST['ChargeFreightCost']);
164
165     $tax_total = display_edit_tax_items($taxes, 6, $_SESSION['Items']->tax_included);
166
167     $display_total = price_format(($subtotal + $_POST['ChargeFreightCost'] + $tax_total));
168
169     label_row(_("Credit Note Total"), $display_total, "colspan=6 align=right","class='amount'", 2);
170
171     end_table();
172 }
173
174 //---------------------------------------------------------------------------------
175
176 function credit_edit_item_controls(&$order, $rowcounter, $line_no=-1)
177 {
178         alt_table_row_color($rowcounter);
179         $id = find_submit('Edit');
180
181         if ($line_no!=-1 && $line_no == $id)
182         {
183
184                 $_POST['stock_id'] = $order->line_items[$id]->stock_id;
185                 $_POST['qty'] = qty_format($order->line_items[$id]->qty_dispatched);
186                 $_POST['price'] = price_format($order->line_items[$id]->price);
187                 $_POST['Disc'] = percent_format(($order->line_items[$id]->discount_percent)*100);
188                 $_POST['units'] = $order->line_items[$id]->units;
189                 hidden('stock_id', $_POST['stock_id']);
190                 label_cell($_POST['stock_id']);
191                 label_cell($order->line_items[$id]->item_description, "nowrap");
192         }
193         else
194         {
195                 if (get_company_pref('no_item_list'))
196                 {
197                         echo "<td colspan=2 nowrap>\n";
198                         stock_items_list('stock_id', null, false, true);
199                         echo "</td>\n";
200                 }
201                 else
202                 {
203                 text_cells(null, "StockID2", '', 12, 10, "", "", "class='combo' rel='stock_id'"/* " onblur='submit();'"*/);
204                 stock_items_list_cells(null, 'stock_id', null, false, false, "class='combo' rel='StockID2'");
205                 }
206                 $item_info = get_item_edit_info($_POST['stock_id']);
207
208                 $_POST['units'] = $item_info["units"];
209
210                 $_POST['qty'] = qty_format(0);
211                 $_POST['price'] = price_format(get_price($_POST['stock_id'], $order->customer_currency,
212                     $order->sales_type, $order->price_factor, $order->document_date));
213
214                 // default to the customer's discount %
215                 $_POST['Disc'] = percent_format($order->default_discount * 100);
216         }
217
218
219         qty_cells(null, 'qty', $_POST['qty']);
220 //      if ($order->trans_no!=0) {
221 //              amount_cell($line_no==-1 ? 0 :$order->line_items[$line_no]->qty_done);
222 //      }
223         label_cell($_POST['units']);
224         amount_cells(null, 'price',  null);
225         small_amount_cells(null, 'Disc', percent_format(0), null, null, user_percent_dec());
226
227         amount_cell($_POST['qty'] * $_POST['price'] * (1 - $_POST['Disc']/100));
228
229         if ($id!=-1)
230         {
231                 edit_button_cell('UpdateItem', _("Update"));
232                 edit_button_cell('CancelItemChanges', _("Cancel"));
233                 hidden('line_no', $line_no);
234                 set_focus('qty');
235         }
236         else
237         {
238                 submit_cells('AddItem', _("Add Item"), "colspan=2");
239         }
240
241         end_row();
242 }
243
244
245 //---------------------------------------------------------------------------------
246
247 function credit_options_controls()
248 {
249         global $table_style2;
250         echo "<br>";
251         start_table("$table_style2");
252
253         credit_type_list_row(_("Credit Note Type"), 'CreditType', null, true);
254
255         if ($_POST['CreditType'] == "Return")
256         {
257
258                 /*if the credit note is a return of goods then need to know which location to receive them into */
259                 if (!isset($_POST['Location']))
260                         $_POST['Location'] = $_SESSION['Items']->Location;
261                 locations_list_row(_("Items Returned to Location"), 'Location', $_POST['Location']);
262
263         }
264         else
265         {
266                 /* the goods are to be written off to somewhere */
267                 gl_all_accounts_list_row(_("Write off the cost of the items to"), 'WriteOffGLCode', null);
268         }
269
270         textarea_row(_("Memo"), "CreditText", null, 51, 3);
271         echo "</table>";
272 }
273
274
275 //---------------------------------------------------------------------------------
276
277 ?>