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