3 include_once($path_to_root . "/purchasing/includes/db/invoice_items_db.inc");
5 //--------------------------------------------------------------------------------------------------
7 function read_supplier_details_to_trans(&$supp_trans, $supplier_id)
9 $sql = "SELECT ".TB_PREF."suppliers.supp_name, ".TB_PREF."payment_terms.terms, ".TB_PREF."payment_terms.days_before_due,
10 ".TB_PREF."payment_terms.day_in_following_month,
11 ".TB_PREF."suppliers.tax_group_id, ".TB_PREF."tax_groups.name As tax_group_name
12 From ".TB_PREF."suppliers, ".TB_PREF."payment_terms, ".TB_PREF."tax_groups
13 WHERE ".TB_PREF."suppliers.tax_group_id = ".TB_PREF."tax_groups.id
14 AND ".TB_PREF."suppliers.payment_terms=".TB_PREF."payment_terms.terms_indicator
15 AND ".TB_PREF."suppliers.supplier_id = '" . $supplier_id . "'";
17 $result = db_query($sql, "The supplier record selected: " . $supplier_id . " cannot be retrieved");
19 $myrow = db_fetch($result);
21 $supp_trans->supplier_id = $supplier_id;
22 $supp_trans->supplier_name = $myrow['supp_name'];
23 $supp_trans->terms_description = $myrow['terms'];
25 if ($myrow['days_before_due'] == 0)
27 $supp_trans->terms = "1" . $myrow['day_in_following_month'];
31 $supp_trans->terms = "0" . $myrow['days_before_due'];
33 $supp_trans->tax_description = $myrow['tax_group_name'];
34 $supp_trans->tax_group_id = $myrow['tax_group_id'];
36 if ($supp_trans->tran_date == "")
38 $supp_trans->tran_date = Today();
39 if (!is_date_in_fiscalyear($supp_trans->tran_date))
40 $supp_trans->tran_date = end_fiscalyear();
42 //if ($supp_trans->due_date=="") {
43 // get_duedate_from_terms($supp_trans);
45 get_duedate_from_terms($supp_trans);
48 //--------------------------------------------------------------------------------------------------
50 function update_supp_received_items_for_invoice($id, $po_detail_item, $qty_invoiced, $chg_price=null)
52 if ($chg_price != null)
54 $sql = "SELECT act_price, unit_price FROM ".TB_PREF."purch_order_details WHERE
55 po_detail_item = $po_detail_item";
56 $result = db_query($sql, "The old actual price of the purchase order line could not be retrieved");
57 $row = db_fetch_row($result);
60 $unit_price = $row[1]; //Added by Rasmus
62 $sql = "SELECT delivery_date FROM ".TB_PREF."grn_batch,".TB_PREF."grn_items WHERE
63 ".TB_PREF."grn_batch.id = ".TB_PREF."grn_items.grn_batch_id AND ".TB_PREF."grn_items.id=$id";
64 $result = db_query($sql, "The old delivery date from the received record cout not be retrieved");
65 $row = db_fetch_row($result);
72 $unit_price = 0; // Added by Rasmus
74 $sql = "UPDATE ".TB_PREF."purch_order_details
75 SET qty_invoiced = qty_invoiced + $qty_invoiced ";
77 if ($chg_price != null)
78 $sql .= " , act_price = $chg_price ";
80 $sql .= " WHERE po_detail_item = $po_detail_item";
81 db_query($sql, "The quantity invoiced of the purchase order line could not be updated");
83 $sql = "UPDATE ".TB_PREF."grn_items
84 SET quantity_inv = quantity_inv + $qty_invoiced
86 db_query($sql, "The quantity invoiced off the items received record could not be updated");
87 return array($ret, $date, $unit_price);
90 function get_deliveries_between($stock_id, $from, $to)
92 $from = date2sql($from);
94 $sql = "SELECT SUM(-qty), SUM(-qty*standard_cost) FROM ".TB_PREF."stock_moves
95 WHERE type=13 AND stock_id='$stock_id' AND
96 tran_date>='$from' AND tran_date<='$to' GROUP BY stock_id";
97 $result = db_query($sql, "The deliveries could not be updated");
98 return db_fetch_row($result);
101 function get_diff_in_home_currency($supplier, $old_date, $date, $amount1, $amount2)
103 if ($supplier != null)
104 $currency = get_supplier_currency($supplier);
107 if ($currency != null)
109 $amount1 = to_home_currency($amount1, $currency, $old_date);
110 $amount2 = to_home_currency($amount2, $currency, $date);
112 return $amount2 - $amount1;
114 //----------------------------------------------------------------------------------------
116 function add_supp_invoice($supp_trans) // do not receive as ref because we change locally
118 //$company_currency = get_company_currency();
119 /*Start an sql transaction */
123 $taxes = $supp_trans->get_taxes($supp_trans->tax_group_id);
125 foreach ($taxes as $taxitem)
127 $tax_total += $taxitem['Value'];
130 $invoice_items_total = $supp_trans->get_total_charged($supp_trans->tax_group_id);
132 if ($supp_trans->is_invoice)
137 // let's negate everything because it's a credit note
138 $invoice_items_total = -$invoice_items_total;
139 $tax_total = -$tax_total;
140 $supp_trans->ov_discount = -$supp_trans->ov_discount; // this isn't used at all...
143 $date_ = $supp_trans->tran_date;
145 /*First insert the invoice into the supp_trans table*/
146 $invoice_id = add_supp_trans($trans_type, $supp_trans->supplier_id, $date_, $supp_trans->due_date,
147 $supp_trans->reference, $supp_trans->supp_reference,
148 $invoice_items_total, $tax_total, $supp_trans->ov_discount);
150 /* Now the control account */
151 $supplier_accounts = get_supplier_accounts($supp_trans->supplier_id);
152 add_gl_trans_supplier($trans_type, $invoice_id, $date_, $supplier_accounts["payable_account"], 0, 0,
153 -($invoice_items_total + $tax_total + $supp_trans->ov_discount),
154 $supp_trans->supplier_id,
155 "The general ledger transaction for the control total could not be added");
157 /*Loop through the GL Entries and create a debit posting for each of the accounts entered */
159 /*the postings here are a little tricky, the logic goes like this:
160 if its a general ledger amount it goes straight to the account specified
162 if its a GRN amount invoiced then :
164 The cost as originally credited to GRN suspense on arrival of items is debited to GRN suspense. Any difference
165 between the std cost and the currency cost charged as converted at the ex rate of of the invoice is written off
166 to the purchase price variance account applicable to the item being invoiced.
169 foreach ($supp_trans->gl_codes as $entered_gl_code)
172 /*GL Items are straight forward - just do the debit postings to the GL accounts specified -
173 the credit is to creditors control act done later for the total invoice value + tax*/
175 if (!$supp_trans->is_invoice)
176 $entered_gl_code->amount = -$entered_gl_code->amount;
178 $memo_ = $entered_gl_code->memo_;
179 add_gl_trans_supplier($trans_type, $invoice_id, $date_, $entered_gl_code->gl_code,
180 $entered_gl_code->gl_dim, $entered_gl_code->gl_dim2, $entered_gl_code->amount, $supp_trans->supplier_id);
182 add_supp_invoice_gl_item($trans_type, $invoice_id, $entered_gl_code->gl_code,
183 $entered_gl_code->amount, $memo_);
185 foreach ($supp_trans->grn_items as $entered_grn)
188 if (!$supp_trans->is_invoice)
190 $entered_grn->this_quantity_inv = -$entered_grn->this_quantity_inv;
191 set_grn_item_credited($entered_grn, $supp_trans->supplier_id, $invoice_id, $date_);
194 $line_taxfree = $entered_grn->taxfree_charge_price($supp_trans->tax_group_id);
195 $line_tax = $entered_grn->full_charge_price($supp_trans->tax_group_id) - $line_taxfree;
196 $stock_gl_code = get_stock_gl_code($entered_grn->item_code);
198 add_gl_trans_supplier($trans_type, $invoice_id, $date_, $stock_gl_code["inventory_account"],
199 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'],
200 $entered_grn->this_quantity_inv * $line_taxfree, $supp_trans->supplier_id);
201 // -------------- if price changed since po received. 16 Aug 2008 Joe Hunt
202 if($supp_trans->is_invoice)
204 $old = update_supp_received_items_for_invoice($entered_grn->id, $entered_grn->po_detail_item,
205 $entered_grn->this_quantity_inv, $entered_grn->chg_price);
206 // Since the standard cost is always calculated on basis of the po unit_price,
207 // this is also the price that should be the base of calculating the price diff.
208 // In cases where there is two different po invoices on the same delivery with different unit prices this will not work either
210 //$old_price = $old[0];
212 $old_price = $old[2];
217 If statement is removed. Should always check for deliveries nomatter if there has been a price change.
219 //if ($old_price != $entered_grn->chg_price) // price-change, so update
221 //$diff = $entered_grn->chg_price - $old_price;
222 $old_date = sql2date($old[1]);
223 $diff = get_diff_in_home_currency($supp_trans->supplier_id, $old_date, $date_, $old_price,
224 $entered_grn->chg_price);
225 // always return due to change in currency.
226 $mat_cost = update_average_material_cost(null, $entered_grn->item_code,
227 $diff, $entered_grn->this_quantity_inv, $old_date, true);
228 $deliveries = get_deliveries_between($entered_grn->item_code, $old_date, $date_);
229 if ($deliveries[0] != 0) // have deliveries been done during the period?
231 $deliveries[1] /= $deliveries[0];
232 $amt = ($mat_cost - $deliveries[1]) * $deliveries[0]; // $amt in home currency
235 add_gl_trans($trans_type, $invoice_id, $date_, $stock_gl_code["cogs_account"],
236 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], _("Cost diff."),
237 $amt, null, null, null,
238 "The general ledger transaction could not be added for the price variance of the inventory item");
239 add_gl_trans($trans_type, $invoice_id, $date_, $stock_gl_code["inventory_account"],
240 0, 0, _("Cost diff."), -$amt, null, null, null,
241 "The general ledger transaction could not be added for the price variance of the inventory item");
243 update_stock_move_pid(13, $entered_grn->item_code, $old_date, $date_, 0, $mat_cost);
245 update_stock_move_pid(25, $entered_grn->item_code, $old_date, $old_date, $supp_trans->supplier_id, $mat_cost);
248 // ----------------------------------------------------------------------
250 add_supp_invoice_item($trans_type, $invoice_id, $entered_grn->item_code,
251 $entered_grn->item_description, 0, $line_taxfree, $line_tax,
252 $entered_grn->this_quantity_inv, $entered_grn->id, $entered_grn->po_detail_item, "");
253 } /* end of GRN postings */
254 /* Now the TAX account */
255 foreach ($taxes as $taxitem)
257 if ($taxitem['Value'] != 0)
260 if (!$supp_trans->is_invoice)
261 $taxitem['Value'] = -$taxitem['Value'];
262 // here we suppose that tax is never included in price (we are company customer).
263 add_supp_invoice_tax_item($trans_type, $invoice_id, $taxitem['tax_type_id'],
264 $taxitem['rate'], 0, $taxitem['Value']);
266 add_gl_trans_supplier($trans_type, $invoice_id, $date_,
267 $taxitem['purchasing_gl_code'], 0, 0, $taxitem['Value'],
268 $supp_trans->supplier_id,
269 "A general ledger transaction for the tax amount could not be added");
272 add_comments($trans_type, $invoice_id, $date_, $supp_trans->Comments);
274 references::save_last($supp_trans->reference, $trans_type);
276 commit_transaction();
281 //----------------------------------------------------------------------------------------
283 // get all the invoices/credits for a given PO - quite long route to get there !
285 function get_po_invoices_credits($po_number)
287 $sql = "SELECT DISTINCT ".TB_PREF."supp_trans.trans_no, ".TB_PREF."supp_trans.type,
288 ov_amount+ov_discount+ov_gst AS Total,
289 ".TB_PREF."supp_trans.tran_date
290 FROM ".TB_PREF."supp_trans, ".TB_PREF."supp_invoice_items, ".TB_PREF."purch_order_details
291 WHERE ".TB_PREF."supp_invoice_items.supp_trans_no = ".TB_PREF."supp_trans.trans_no
292 AND ".TB_PREF."supp_invoice_items.po_detail_item_id = ".TB_PREF."purch_order_details.po_detail_item
293 AND ".TB_PREF."purch_order_details.order_no = $po_number";
295 return db_query($sql, "The invoices/credits for the po $po_number could not be retreived");
298 //----------------------------------------------------------------------------------------
300 function read_supp_invoice($trans_no, $trans_type, &$supp_trans)
302 $sql = "SELECT ".TB_PREF."supp_trans.*, supp_name FROM ".TB_PREF."supp_trans,".TB_PREF."suppliers
303 WHERE trans_no = $trans_no AND type = $trans_type
304 AND ".TB_PREF."suppliers.supplier_id=".TB_PREF."supp_trans.supplier_id";
305 $result = db_query($sql, "Cannot retreive a supplier transaction");
307 if (db_num_rows($result) == 1)
309 $trans_row = db_fetch($result);
311 $supp_trans->supplier_id = $trans_row["supplier_id"];
312 $supp_trans->supplier_name = $trans_row["supp_name"];
313 $supp_trans->tran_date = sql2date($trans_row["tran_date"]);
314 $supp_trans->due_date = sql2date($trans_row["due_date"]);
315 //$supp_trans->Comments = $trans_row["TransText"];
316 $supp_trans->Comments = "";
317 $supp_trans->reference = $trans_row["reference"];
318 $supp_trans->supp_reference = $trans_row["supp_reference"];
319 $supp_trans->ov_amount = $trans_row["ov_amount"];
320 $supp_trans->ov_discount = $trans_row["ov_discount"];
321 $supp_trans->ov_gst = $trans_row["ov_gst"];
323 $id = $trans_row["trans_no"];
325 $result = get_supp_invoice_items($trans_type, $id);
327 if (db_num_rows($result) > 0)
330 while ($details_row = db_fetch($result))
333 if ($details_row["gl_code"] == 0)
335 $supp_trans->add_grn_to_trans($details_row["grn_item_id"], $details_row["po_detail_item_id"], $details_row["stock_id"],
336 $details_row["description"], 0, 0, $details_row["quantity"], 0, $details_row["FullUnitPrice"],
341 $supp_trans->add_gl_codes_to_trans($details_row["gl_code"], get_gl_account_name($details_row["gl_code"]), 0, 0,
342 $details_row["FullUnitPrice"], $details_row["memo_"]);
348 return display_db_error("Invalid supptrans details for supptrans number : $trans_no and type : $trans_type", $sql, true);
354 return display_db_error("Invalid supptrans number : $trans_no and type : $trans_type", $sql, true);
358 //----------------------------------------------------------------------------------------
360 function void_supp_invoice($type, $type_no)
364 void_bank_trans($type, $type_no, true);
366 void_gl_trans($type, $type_no, true);
368 void_supp_allocations($type, $type_no);
370 void_supp_trans($type, $type_no);
372 $result = get_supp_invoice_items($type, $type_no);
374 // now remove this invoice/credit from any GRNs/POs that it's related to
375 if (db_num_rows($result) > 0)
378 while ($details_row = db_fetch($result))
380 if ((int)$details_row["grn_item_id"] > 0) // it can be empty for GL items
382 // Changed 2008-10-17 by Joe Hunt to get the avg. material cost updated
383 $old = update_supp_received_items_for_invoice($details_row["grn_item_id"],
384 $details_row["po_detail_item_id"], -$details_row["quantity"], $details_row["FullUnitPrice"]);
386 //$diff = $details_row["FullUnitPrice"] - $old[2];
387 $old_date = sql2date($old[1]);
389 $batch = get_grn_batch_from_item($details_row["grn_item_id"]);
390 $grn = get_grn_batch($batch);
392 $diff = get_diff_in_home_currency($grn["supplier_id"], $old_date, $date_, $old[2],
393 $details_row["FullUnitPrice"]);
394 // Only adjust the avg for the diff
395 $mat_cost = update_average_material_cost(null, $details_row["stock_id"],
396 -$diff, $details_row["quantity"], $old_date, true);
398 $deliveries = get_deliveries_between($details_row["stock_id"], $old_date, $date_);
399 if ($deliveries[0] != 0) // have deliveries been done during the period?
401 $deliveries[1] /= $deliveries[0];
402 $amt = ($mat_cost - $deliveries[1]) * $deliveries[0]; // $amt in home currency
405 $stock_gl_code = get_stock_gl_code($details_row["stock_id"]);
406 add_gl_trans($type, $type_no, $date_, $stock_gl_code["cogs_account"],
407 $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], _("Cost diff."),
408 -$amt, null, null, null,
409 "The general ledger transaction could not be added for the price variance of the inventory item");
410 add_gl_trans($type, $type_no, $date_, $stock_gl_code["inventory_account"],
411 0, 0, _("Cost diff."), $amt, null, null, null,
412 "The general ledger transaction could not be added for the price variance of the inventory item");
414 update_stock_move_pid(13, $details_row["stock_id"], $old_date, $date_, 0, $mat_cost);
416 update_stock_move_pid(25, $details_row["stock_id"], $old_date, $old_date, $grn['supplier_id'], $mat_cost);
422 void_supp_invoice_items($type, $type_no);
423 void_supp_invoice_tax_items($type, $type_no);
425 commit_transaction();
428 //----------------------------------------------------------------------------------------