9625e2a222a701d4a22e87797619a7335a35274e
[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         $_POST['tax_group_id'] = $supp_trans->tax_group_id;
14 }
15
16 //--------------------------------------------------------------------------------------------------
17
18 function copy_to_trans(&$supp_trans)
19 {
20         $supp_trans->Comments = $_POST['Comments'];
21         $supp_trans->tran_date = $_POST['tran_date'];
22         $supp_trans->due_date = $_POST['due_date'];
23         $supp_trans->supp_reference = $_POST['supp_reference'];
24         $supp_trans->reference = $_POST['reference'];
25         $supp_trans->tax_group_id = $_POST['tax_group_id'];
26
27
28         $supp_trans->ov_amount = 0; /* for starters */
29         if (count($supp_trans->grn_items) > 0)
30         {
31                 foreach ( $supp_trans->grn_items as $grn)
32                 {
33                         $supp_trans->ov_amount += ($grn->this_quantity_inv * $grn->chg_price);
34                 }
35         }
36         if (count($supp_trans->gl_codes) > 0)
37         {
38                 foreach ( $supp_trans->gl_codes as $gl_line)
39                 {
40                         $supp_trans->ov_amount += $gl_line->amount;
41                 }
42         }
43 }
44
45 //--------------------------------------------------------------------------------------------------
46
47 function invoice_header(&$supp_trans)
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
89     date_row(_("Due Date") . ":", 'due_date', null);
90
91     label_row(_("Terms:"), $supp_trans->terms_description);
92
93         echo "</table>";
94
95         echo "</td><td class='tableseparator'>"; // outer
96
97         echo "<table width=100%>";
98
99         $supplier_currency = get_supplier_currency($supp_trans->supplier_id);
100
101         $company_currency = get_company_currency();
102
103         if ($supplier_currency != $company_currency)
104         {
105         label_row(_("Supplier's Currency:"), "<b>" . $supplier_currency . "</b>");
106                 exchange_rate_display($supplier_currency, $company_currency, $_POST['tran_date']);
107         }
108
109
110         if (!isset($_POST['tax_group_id']))
111         $_POST['tax_group_id'] = $supp_trans->tax_group_id;
112     tax_groups_list_row(_("Tax Group:"), 'tax_group_id', $_POST['tax_group_id'], true);
113
114         echo "</table>";
115
116         echo "</td>";
117         end_row();
118         end_table(); // outer
119 }
120
121 //--------------------------------------------------------------------------------------------------
122
123 function invoice_totals(&$supp_trans)
124 {
125         global $table_style, $table_style2;
126
127         copy_to_trans($supp_trans);
128
129     start_table("$table_style width=95%");
130         label_row(_("Sub-total:"), number_format2( $supp_trans->ov_amount,user_price_dec()), "align=right", "align=right");
131
132     $taxes = $supp_trans->get_taxes($_POST['tax_group_id']);
133     $tax_total = display_edit_tax_items($taxes, 1);
134
135     $display_total = number_format2(( $supp_trans->ov_amount + $tax_total), user_price_dec());
136
137         if ($supp_trans->is_invoice)
138         label_row(_("Invoice Total:"), $display_total, "align=right", "align=right");
139     else
140                 label_row("<font color=red>" . _("Credit Note Total:") . "</font>", 
141                         "<font color=red><b>$display_total</b></font>", "align=right", "nowrap align=right");
142
143     end_table();
144         br(1);
145     start_table($table_style2);
146     textarea_row(_("Memo:"), "Comments", null, 50, 3);
147     end_table();
148     br(1);
149 }
150
151 //--------------------------------------------------------------------------------------------------
152
153 // $mode = 0 display on invoice page
154 //               = 1 display on gl items edit page
155 //               = 2 display on view invoice
156 //               = 3 display on view credit
157
158 function display_gl_items(&$supp_trans, $mode=0)
159 {
160         global $table_style, $path_to_root;
161
162     if ($mode == 0 && isset($_POST["InvGL"]))
163     {
164         copy_to_trans($supp_trans);
165                 meta_forward("$path_to_root/purchasing/supplier_trans_gl.php");
166         exit;
167     }
168
169     // if displaying in form, and no items, exit
170     if (($mode == 2 || $mode == 3) && count($supp_trans->gl_codes) == 0)
171         return;
172
173         echo "<br>";
174
175         start_table("width=95%");
176         echo "<tr><td>"; // outertable
177
178         if ($supp_trans->is_invoice)
179                 $heading = _("GL Items for this Invoice");
180         else
181                 $heading = _("GL Items for this Credit Note");
182
183         echo "<table width=100%>";
184         echo "<tr><td align=center><span class='headingtext'>$heading</span></td>";
185         if ($mode == 0) 
186         {
187         submit_cells('InvGL', _("Edit"), "align=right");
188         }
189         echo "</tr></table>";
190
191         echo "</td></tr><tr><td>"; // ouer table
192
193         echo "<table width=100% $table_style>";
194         
195         $dim = get_company_pref('use_dimension');
196         if ($dim == 2)
197         $th = array(_("Account"), _("Name"), _("Dimension")." 1", _("Dimension")." 2", _("Amount"), _("Memo"));
198         else if ($dim == 1)
199         $th = array(_("Account"), _("Name"), _("Dimension"), _("Amount"), _("Memo"));
200     else
201         $th = array(_("Account"), _("Name"), _("Amount"), _("Memo"));
202
203         table_header($th);
204         $total_gl_value=0;
205         $i = $k = 0;
206
207         if (count($supp_trans->gl_codes) > 0)
208         {
209
210                 foreach ($supp_trans->gl_codes as $entered_gl_code)
211                 {
212
213                         alt_table_row_color($k);
214
215                         if ($mode == 3)
216                                 $entered_gl_code->amount = -$entered_gl_code->amount;
217
218                         label_cell($entered_gl_code->gl_code);
219                         label_cell($entered_gl_code->gl_act_name);
220                 
221                         if ($dim >= 1)
222                                 label_cell(get_dimension_string($entered_gl_code->gl_dim, true));
223                         if ($dim > 1)
224                                 label_cell(get_dimension_string($entered_gl_code->gl_dim2, true));
225
226                         amount_cell($entered_gl_code->amount);
227                         label_cell($entered_gl_code->memo_);
228
229                         if ($mode == 1)
230                                 delete_link_cell(SID . "Delete=" . $entered_gl_code->Counter);
231
232                         end_row();
233
234                         $total_gl_value += $entered_gl_code->amount;
235
236                         $i++;
237                         if ($i > 15)
238                         {
239                                 $i = 0; 
240                                 table_header($th); 
241                         }
242                 }
243
244         }
245         $colspan = ($dim == 2 ? 4 : ($dim == 1 ? 3 : 2));
246         label_row(_("Total"), number_format2($total_gl_value,user_price_dec()), 
247                 "colspan=".$colspan." align=right", "nowrap align=right");
248
249         echo "</table>";
250
251     echo "</td></tr>";
252     
253     end_table(); // outertable
254
255         return $total_gl_value;
256 }
257
258 //--------------------------------------------------------------------------------------------------
259
260 // $mode = 0 display on invoice page
261 //               = 1 display on grn items edit page
262 //               = 2 display on view invoice
263 //               = 3 display on view credit
264
265 function display_grn_items(&$supp_trans, $mode=0)
266 {
267         global $table_style, $path_to_root;
268
269     if ($mode == 0 && isset($_POST["InvGRN"]))
270     {
271         copy_to_trans($supp_trans);
272                 if ($supp_trans->is_invoice)
273                         meta_forward("$path_to_root/purchasing/supplier_invoice_grns.php");
274         else
275                         meta_forward("$path_to_root/purchasing/supplier_credit_grns.php");
276         exit;
277     }
278
279     // if displaying in form, and no items, exit
280     if (($mode == 2  || $mode == 3) && count($supp_trans->grn_items) == 0)
281         return;
282         br(1);
283         start_table("width=95%");
284         echo "<tr><td>"; // outertable
285
286         if ($supp_trans->is_invoice)
287                 $heading = _("Received Items Charged on this Invoice");
288         else
289                 $heading = _("Received Items Credited on this Note");
290
291         echo "<table width=100% >";
292         echo "<tr><td align=center><span class='headingtext'>$heading</span></td>";
293         if ($mode == 0) 
294         {
295                 submit_cells('InvGRN', _("Edit"), "align=right");
296         }
297         echo "</tr></table>";
298
299         echo "</td></tr><tr><td>"; // outer table
300
301         echo "<table width=100% $table_style>";
302         $th = array(_("Delivery Sequence #"), _("Item"), _("Description"),
303                 _("Quantity"), _("Price"), _("Line Value"));
304
305         table_header($th);
306     $total_grn_value = 0;
307     $i = $k = 0;
308
309         if (count($supp_trans->grn_items) > 0)
310         {
311
312         foreach ($supp_trans->grn_items as $entered_grn)
313         {
314
315                 alt_table_row_color($k);
316
317                 label_cell(get_trans_view_str(25,$entered_grn->id));
318                         label_cell($entered_grn->item_code);
319                         label_cell($entered_grn->item_description);
320                         qty_cell(abs($entered_grn->this_quantity_inv));
321                         amount_cell($entered_grn->chg_price);
322                         amount_cell($entered_grn->chg_price * abs($entered_grn->this_quantity_inv));
323
324                         if ($mode == 1)
325                                 delete_link_cell(SID . "Delete=" . $entered_grn->id);
326
327                         end_row();
328
329                 $total_grn_value += ($entered_grn->chg_price * abs($entered_grn->this_quantity_inv));
330
331                 $i++;
332                 if ($i > 15)
333                 {
334                         $i = 0;
335                         table_header($th);
336                 }
337         }
338     }
339
340         label_row(_("Total"), number_format2($total_grn_value,user_price_dec()),
341                 "colspan=5 align=right", "nowrap align=right");
342     echo "</table>";
343
344     echo "</td></tr>";
345     
346     end_table(); // outertable
347
348         return $total_grn_value;
349 }
350
351 //--------------------------------------------------------------------------------------------------
352 function get_duedate_from_terms(&$supp_trans)
353 {
354         if (!is_date($supp_trans->tran_date)) 
355         {
356                 $supp_trans->tran_date = Today();
357         }
358         if (substr( $supp_trans->terms, 0, 1) == "1") 
359         { /*Its a day in the following month when due */
360                 $supp_trans->due_date = add_days(end_month($supp_trans->tran_date), (int) substr( $supp_trans->terms,1));
361         } 
362         else 
363         { /*Use the Days Before Due to add to the invoice date */
364                 $supp_trans->due_date = add_days($supp_trans->tran_date, (int) substr( $supp_trans->terms,1));
365         }
366 }
367
368 //--------------------------------------------------------------------------------------------------
369
370 ?>