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