Merged bugfixes upto [0000072] (version 2.0.3).
[fa-stable.git] / purchasing / supplier_credit_grns.php
1 <?php
2
3 $page_security = 5;
4 $path_to_root="..";
5
6 include_once($path_to_root . "/purchasing/includes/supp_trans_class.inc");
7 include_once($path_to_root . "/includes/session.inc");
8 include_once($path_to_root . "/purchasing/includes/purchasing_ui.inc");
9 include_once($path_to_root . "/purchasing/includes/purchasing_db.inc");
10
11 $js = "";
12 if ($use_date_picker)
13         $js .= get_js_date_picker();
14 if ($use_popup_windows)
15         $js .= get_js_open_window(900, 500);
16 page(_("Select Received Items to Add"), false, false, "", $js);
17
18 if (!isset($_SESSION['supp_trans']))
19 {
20         display_note(_("To enter supplier transactions the supplier must first be selected from the supplier selection screen, then the link to enter a supplier credit note must be clicked on."));
21         exit;
22         /*It all stops here if there aint no supplier selected and credit note initiated ie $_SESSION['supp_trans'] started off*/
23 }
24
25 //-----------------------------------------------------------------------------------------
26
27 display_heading($_SESSION['supp_trans']->supplier_name);
28 echo "<br>";
29
30 //-----------------------------------------------------------------------------------------
31
32 function check_data()
33 {
34         if (!check_num('This_QuantityCredited', 0))
35         {
36                 display_error(_("The quantity to credit must be numeric and greater than zero."));
37                 set_focus('This_QuantityCredited');
38                 return false;
39         }
40
41         if (!check_num('ChgPrice', 0))
42         {
43                 display_error(_("The price is either not numeric or negative."));
44                 set_focus('ChgPrice');
45                 return false;
46         }
47
48         return true;
49 }
50
51 //-----------------------------------------------------------------------------------------
52
53 if (isset($_POST['AddGRNToTrans']))
54 {
55
56         if (check_data())
57         {
58                 $complete = False;
59
60                 //$_SESSION['supp_trans']->add_grn_to_trans($_POST['GRNNumber'],
61         //      $_POST['po_detail_item'], $_POST['item_code'],
62         //      $_POST['item_description'], $_POST['qty_recd'],
63         //      $_POST['prev_quantity_inv'], $_POST['This_QuantityCredited'],
64         //      $_POST['order_price'], $_POST['ChgPrice'], $complete,
65         //      $_POST['std_cost_unit'], $_POST['gl_code']);
66                 $_SESSION['supp_trans']->add_grn_to_trans($_POST['GRNNumber'],
67                 $_POST['po_detail_item'], $_POST['item_code'],
68                 $_POST['item_description'], $_POST['qty_recd'],
69                 $_POST['prev_quantity_inv'], input_num('This_QuantityCredited'),
70                 $_POST['order_price'], input_num('ChgPrice'), $complete,
71                 $_POST['std_cost_unit'], "");
72         }
73 }
74
75 //-----------------------------------------------------------------------------------------
76 $id = find_submit('Delete');
77 if ($id != -1)
78 {
79         $_SESSION['supp_trans']->remove_grn_from_trans($id);
80         $Ajax->activate('grn_items');
81         $Ajax->activate('grn_table');
82 }
83
84 //-----------------------------------------------------------------------------------------
85 start_form(false,true);
86 display_grn_items($_SESSION['supp_trans'], 1);
87 end_form();
88 echo "<br>";
89
90 hyperlink_no_params("$path_to_root/purchasing/supplier_credit.php", _("Return to Credit Note Entry"));
91 echo "<hr>";
92
93 //-----------------------------------------------------------------------------------------
94
95 // get the supplier grns that have been invoiced
96 $result = get_grn_items(0, $_SESSION['supp_trans']->supplier_id, false, true);
97
98 if (db_num_rows($result) == 0)
99 {
100         display_note(_("There are no received items for the selected supplier that have been invoiced."));
101         display_note(_("Credits can only be applied to invoiced items."));
102
103         echo "<br>";
104         end_page();
105         exit;
106 }
107
108 /*Set up a table to show the GRN items outstanding for selection */
109 start_form(false, true);
110
111 div_start('grn_table');
112 start_table("$table_style width=95%");
113 $th = array(_("Delivery"), _("Sequence #"), _("Order"), _("Item Code"), _("Description"),
114         _("Delivered"), _("Total Qty Received"), _("Qty Already Invoiced"),
115         _("Qty Yet To Invoice"), _("Order Price"), _("Line Value"));
116 table_header($th);
117 $i = $k =0;
118 while ($myrow = db_fetch($result))
119 {
120
121         $grn_already_on_credit = False;
122
123         foreach ($_SESSION['supp_trans']->grn_items as $entered_grn)
124         {
125                 if ($entered_grn->id == $myrow["id"])
126                 {
127                         $grn_already_on_credit = True;
128                 }
129         }
130         if ($grn_already_on_credit == False)
131         {
132
133                 alt_table_row_color($k);
134
135                 label_cell(get_trans_view_str(25, $myrow["grn_batch_id"]));
136         submit_cells('grn_item_id'.$myrow["id"], $myrow["id"], '', '', true);
137                 label_cell(get_trans_view_str(systypes::po(), $myrow["purch_order_no"]));
138         label_cell($myrow["item_code"]);
139         label_cell($myrow["description"]);
140         label_cell(sql2date($myrow["delivery_date"]));
141         $dec = get_qty_dec($myrow["item_code"]);
142         qty_cell($myrow["qty_recd"], false, $dec);
143         qty_cell($myrow["quantity_inv"], false, $dec);
144         qty_cell($myrow["qty_recd"] - $myrow["quantity_inv"], false, $dec);
145                 amount_cell($myrow["unit_price"]);
146                 amount_cell(round($myrow["unit_price"] * $myrow["quantity_inv"],  user_price_dec()));
147                 end_row();
148                 $i++;
149                 if ($i > 15)
150                 {
151                         $i = 0;
152                         table_header($th);
153                 }
154         }
155 }
156
157 end_table();
158 div_end();
159
160 //-----------------------------------------------------------------------------------------
161 $id = find_submit('grn_item_id');
162 if ($id || get_post('AddGRNToTrans'))
163 {
164         $Ajax->activate('grn_selector');
165 }
166 if (get_post('AddGRNToTrans'))
167 {
168         $Ajax->activate('grn_table');
169         $Ajax->activate('grn_items');
170 }
171
172
173 div_start('grn_selector');
174 $id = find_submit('grn_item_id');
175 if ($id != -1)
176 {
177
178         $myrow = get_grn_item_detail($id);
179
180         echo "<br>";
181         display_heading2(_("Delivery Item Selected For Adding To A Supplier Credit Note"));
182         start_table("$table_style width=80%");
183         $th = array(_("Sequence #"), _("Item"), _("Qty Already Invoiced"),
184                 _("Quantity to Credit"), _("Order Price"), _("Credit Price"));
185         table_header($th);
186
187         start_row();
188         label_cell($id);
189     label_cell($myrow['item_code'] . " " . $myrow['description']);
190     $dec = get_qty_dec($myrow['item_code']);
191     qty_cell($myrow["quantity_inv"], false, $dec);
192     qty_cells(null, 'This_QuantityCredited', number_format2(max($myrow['quantity_inv'],0), $dec), null, null, $dec);
193     amount_cell($myrow['unit_price']);
194     amount_cells(null, 'ChgPrice', price_format($myrow['unit_price']));
195     end_row();
196         end_table(1);
197
198         submit_center('AddGRNToTrans', _("Add to Credit Note"), true, '', true);
199
200         hidden('GRNNumber', $id);
201         hidden('item_code', $myrow['item_code']);;
202         hidden('item_description', $myrow['description']);
203         hidden('qty_recd', $myrow['qty_recd']);
204         hidden('prev_quantity_inv', $myrow['quantity_inv']);
205         hidden('order_price', $myrow['unit_price']);
206         hidden('std_cost_unit', $myrow['std_cost_unit']);
207
208         hidden('po_detail_item', $myrow['po_detail_item']);
209 }
210 div_end();
211
212 end_form();
213 echo '<br>';
214 end_page(false, true);
215 ?>