Merged bugfixes upto [0000072] (version 2.0.3).
[fa-stable.git] / purchasing / includes / ui / invoice_ui.inc
1 <?php
2
3 //--------------------------------------------------------------------------------------------------
4
5 function copy_from_trans(&$supp_trans)
6 {
7         $_POST['Comments'] = $supp_trans->Comments;
8         $_POST['tran_date'] = $supp_trans->tran_date;
9         $_POST['due_date'] = $supp_trans->due_date;
10         $_POST['supp_reference'] = $supp_trans->supp_reference;
11         $_POST['reference'] = $supp_trans->reference;
12         $_POST['supplier_id'] = $supp_trans->supplier_id;
13 }
14
15 //--------------------------------------------------------------------------------------------------
16
17 function copy_to_trans(&$supp_trans)
18 {
19         $supp_trans->Comments = $_POST['Comments'];
20         $supp_trans->tran_date = $_POST['tran_date'];
21         $supp_trans->due_date = $_POST['due_date'];
22         $supp_trans->supp_reference = $_POST['supp_reference'];
23         $supp_trans->reference = $_POST['reference'];
24
25         $supp_trans->ov_amount = 0; /* for starters */
26         if (count($supp_trans->grn_items) > 0)
27         {
28                 foreach ( $supp_trans->grn_items as $grn)
29                 {
30                         $supp_trans->ov_amount += round(($grn->this_quantity_inv * $grn->chg_price),
31                           user_price_dec());
32                 }
33         }
34         if (count($supp_trans->gl_codes) > 0)
35         {
36                 foreach ( $supp_trans->gl_codes as $gl_line)
37                 {
38                         $supp_trans->ov_amount += $gl_line->amount;
39                 }
40         }
41 }
42
43 //--------------------------------------------------------------------------------------------------
44
45 function invoice_header(&$supp_trans)
46 {
47         global $Ajax;
48         
49         // if vars have been lost, recopy
50         if (!isset($_POST['tran_date']))
51                 copy_from_trans($supp_trans);
52
53         start_table("width=100%", 5);
54         start_row();
55         echo"<td>"; // outer
56
57         echo "<table width=100%>";
58
59     if (!isset($_POST['supplier_id']) && (get_global_supplier() != reserved_words::get_all()))
60         $_POST['supplier_id'] = get_global_supplier();
61
62         supplier_list_row(_("Supplier:"), 'supplier_id', $_POST['supplier_id'], false, true);
63
64         if ($supp_trans->supplier_id != $_POST['supplier_id'])
65         {
66                 // supplier has changed
67                 // delete all the order items - drastic but necessary because of
68                 // change of currency, etc
69                 $supp_trans->clear_items();
70                 read_supplier_details_to_trans($supp_trans, $_POST['supplier_id']);
71                 copy_from_trans($supp_trans);
72         }
73
74         if ($supp_trans->is_invoice)
75         ref_row(_("Reference:"), 'reference', '', references::get_next(20));
76     else
77         ref_row(_("Reference:"), 'reference', '', references::get_next(21));
78
79         text_row(_("Supplier's Ref.:"), 'supp_reference', $_POST['supp_reference'], 20, 20);
80
81         echo "</table>";
82
83         echo "</td><td class='tableseparator'>"; // outer
84
85         echo "<table width=100%>";
86
87         date_row(_("Date") . ":", 'tran_date', '', null, 0, 0, 0, "", true);
88         if (isset($_POST['_tran_date_changed'])) {
89                 $Ajax->activate('_ex_rate');
90                 $supp_trans->tran_date = $_POST['tran_date'];
91                 get_duedate_from_terms($supp_trans);
92                 $_POST['due_date'] = $supp_trans->due_date;
93                 $Ajax->activate('due_date');
94         }
95
96     date_row(_("Due Date") . ":", 'due_date');
97
98     label_row(_("Terms:"), $supp_trans->terms_description);
99
100         echo "</table>";
101
102         echo "</td><td class='tableseparator'>"; // outer
103
104         echo "<table width=100%>";
105
106         $supplier_currency = get_supplier_currency($supp_trans->supplier_id);
107
108         $company_currency = get_company_currency();
109
110         if ($supplier_currency != $company_currency)
111         {
112         label_row(_("Supplier's Currency:"), "<b>" . $supplier_currency . "</b>");
113                 exchange_rate_display($supplier_currency, $company_currency, $_POST['tran_date']);
114         }
115
116   label_row(_("Tax Group:"), $supp_trans->tax_description);
117         echo "</table>";
118
119         echo "</td>";
120         end_row();
121         end_table(); // outer
122 }
123
124 //--------------------------------------------------------------------------------------------------
125
126 function invoice_totals(&$supp_trans)
127 {
128         global $table_style, $table_style2;
129
130         copy_to_trans($supp_trans);
131
132     start_table("$table_style width=95%");
133         label_row(_("Sub-total:"), price_format( $supp_trans->ov_amount), "align=right", "align=right");
134
135     $taxes = $supp_trans->get_taxes($supp_trans->tax_group_id);
136     $tax_total = display_edit_tax_items($taxes, 1, 0); // tax_included==0 (we are the company)
137
138     $display_total = price_format($supp_trans->ov_amount + $tax_total);
139
140         if ($supp_trans->is_invoice)
141         label_row(_("Invoice Total:"), $display_total, "align=right", "align=right");
142     else
143                 label_row("<font color=red>" . _("Credit Note Total:") . "</font>",
144                         "<font color=red><b>$display_total</b></font>", "align=right", "nowrap align=right");
145
146     end_table();
147         br(1);
148     start_table($table_style2);
149     textarea_row(_("Memo:"), "Comments", null, 50, 3);
150     end_table();
151     br(1);
152 }
153
154 //--------------------------------------------------------------------------------------------------
155
156 // $mode = 0 display on invoice page
157 //               = 1 display on gl items edit page
158 //               = 2 display on view invoice
159 //               = 3 display on view credit
160
161 function display_gl_items(&$supp_trans, $mode=0)
162 {
163         global $table_style, $path_to_root;
164
165     if ($mode == 0 && isset($_POST["InvGL"]))
166     {
167         copy_to_trans($supp_trans);
168                 meta_forward("$path_to_root/purchasing/supplier_trans_gl.php");
169         exit;
170     }
171
172     // if displaying in form, and no items, exit
173     if (($mode == 2 || $mode == 3) && count($supp_trans->gl_codes) == 0)
174         return;
175
176         echo "<br>";
177
178         start_table("width=95%");
179         echo "<tr><td>"; // outertable
180
181         if ($supp_trans->is_invoice)
182                 $heading = _("GL Items for this Invoice");
183         else
184                 $heading = _("GL Items for this Credit Note");
185
186         echo "<table width=100%>";
187         echo "<tr><td align=center><span class='headingtext'>$heading</span></td>";
188         if ($mode == 0)
189         {
190         submit_cells('InvGL', _("Edit"), "align=right",
191                         _('Moves to additional costs posting page'), true);
192         }
193         echo "</tr></table>";
194
195         echo "</td></tr><tr><td>"; // ouer table
196
197         div_start('gl_items');
198         echo "<table width=100% $table_style>";
199
200         $dim = get_company_pref('use_dimension');
201         if ($dim == 2)
202         $th = array(_("Account"), _("Name"), _("Dimension")." 1", _("Dimension")." 2", _("Amount"), _("Memo"));
203         else if ($dim == 1)
204         $th = array(_("Account"), _("Name"), _("Dimension"), _("Amount"), _("Memo"));
205     else
206         $th = array(_("Account"), _("Name"), _("Amount"), _("Memo"));
207
208         table_header($th);
209         $total_gl_value=0;
210         $i = $k = 0;
211
212         if (count($supp_trans->gl_codes) > 0)
213         {
214
215                 foreach ($supp_trans->gl_codes as $entered_gl_code)
216                 {
217
218                         alt_table_row_color($k);
219
220                         if ($mode == 3)
221                                 $entered_gl_code->amount = -$entered_gl_code->amount;
222
223                         label_cell($entered_gl_code->gl_code);
224                         label_cell($entered_gl_code->gl_act_name);
225
226                         if ($dim >= 1)
227                                 label_cell(get_dimension_string($entered_gl_code->gl_dim, true));
228                         if ($dim > 1)
229                                 label_cell(get_dimension_string($entered_gl_code->gl_dim2, true));
230
231                         amount_cell($entered_gl_code->amount);
232                         label_cell($entered_gl_code->memo_);
233
234                         if ($mode == 1)
235                                 edit_button_cell("Delete" . $entered_gl_code->Counter, _("Delete"),
236                                           _('Remove line from document'));
237
238                         end_row();
239
240                         $total_gl_value += $entered_gl_code->amount;
241
242                         $i++;
243                         if ($i > 15)
244                         {
245                                 $i = 0;
246                                 table_header($th);
247                         }
248                 }
249
250         }
251         $colspan = ($dim == 2 ? 4 : ($dim == 1 ? 3 : 2));
252         label_row(_("Total"), price_format($total_gl_value),
253                 "colspan=".$colspan." align=right", "nowrap align=right");
254
255         echo "</table>";
256         div_end();
257
258     echo "</td></tr>";
259
260     end_table(); // outertable
261         return $total_gl_value;
262 }
263
264 //--------------------------------------------------------------------------------------------------
265
266 // $mode = 0 display on invoice page
267 //               = 1 display on grn items edit page
268 //               = 2 display on view invoice
269 //               = 3 display on view credit
270
271 function display_grn_items(&$supp_trans, $mode=0)
272 {
273         global $table_style, $path_to_root;
274
275     if ($mode == 0 && isset($_POST["InvGRN"]))
276     {
277         copy_to_trans($supp_trans);
278                 if ($supp_trans->is_invoice)
279                         meta_forward("$path_to_root/purchasing/supplier_invoice_grns.php");
280         else
281                         meta_forward("$path_to_root/purchasing/supplier_credit_grns.php");
282         exit;
283     }
284
285     // if displaying in form, and no items, exit
286     if (($mode == 2  || $mode == 3) && count($supp_trans->grn_items) == 0)
287         return;
288         br(1);
289         start_table("width=95%");
290         echo "<tr><td>"; // outertable
291
292         if ($supp_trans->is_invoice)
293                 $heading = _("Received Items Charged on this Invoice");
294         else
295                 $heading = _("Received Items Credited on this Note");
296         echo "<table width=100% >";
297         echo "<tr><td align=center><span class='headingtext'>$heading</span></td>";
298         if ($mode == 0)
299         {
300                 submit_cells('InvGRN', _("Edit"), "align=right",
301                         _('Moves to GRN selection page'), true);
302         }
303         echo "</tr></table>";
304
305         echo "</td></tr><tr><td>"; // outer table
306
307   div_start('grn_items');
308         echo "<table width=100% $table_style>";
309         $th = array(_("Delivery"), _("Item"), _("Description"),
310                 _("Quantity"), _("Price"), _("Line Value"));
311
312         table_header($th);
313     $total_grn_value = 0;
314     $i = $k = 0;
315
316         if (count($supp_trans->grn_items) > 0)
317         {
318
319         foreach ($supp_trans->grn_items as $entered_grn)
320         {
321
322                 alt_table_row_color($k);
323
324                         $grn_batch = get_grn_batch_from_item($entered_grn->id);
325                 label_cell(get_trans_view_str(25,$grn_batch));
326                         label_cell($entered_grn->item_code);
327                         label_cell($entered_grn->item_description);
328                         qty_cell(abs($entered_grn->this_quantity_inv), false, get_qty_dec($entered_grn->item_code));
329                         amount_cell($entered_grn->chg_price);
330                         amount_cell( round($entered_grn->chg_price * abs($entered_grn->this_quantity_inv)), user_price_dec());
331
332                         if ($mode == 1)
333                                 edit_button_cell("Delete" . $entered_grn->id, _("Delete"),
334                                           _('Remove item from document'));
335
336                         end_row();
337
338                 $total_grn_value += round($entered_grn->chg_price * abs($entered_grn->this_quantity_inv),
339                            user_price_dec());
340
341                 $i++;
342                 if ($i > 15)
343                 {
344                         $i = 0;
345                         table_header($th);
346                 }
347         }
348     }
349
350         label_row(_("Total"), price_format($total_grn_value),
351                 "colspan=5 align=right", "nowrap align=right");
352     echo "</table>";
353     div_end();
354     echo "</td></tr>";
355
356     end_table(); // outertable
357         return $total_grn_value;
358 }
359
360 //--------------------------------------------------------------------------------------------------
361 function get_duedate_from_terms(&$supp_trans)
362 {
363         if (!is_date($supp_trans->tran_date))
364         {
365                 $supp_trans->tran_date = Today();
366         }
367         if (substr( $supp_trans->terms, 0, 1) == "1")
368         { /*Its a day in the following month when due */
369                 $supp_trans->due_date = add_days(end_month($supp_trans->tran_date), (int) substr( $supp_trans->terms,1));
370         }
371         else
372         { /*Use the Days Before Due to add to the invoice date */
373                 $supp_trans->due_date = add_days($supp_trans->tran_date, (int) substr( $supp_trans->terms,1));
374         }
375 }
376
377 //--------------------------------------------------------------------------------------------------
378
379 ?>