See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
***********************************************************************/
//------------------- update average material cost ------------------------------------------ Joe Hunt Mar-03-2008
-
function update_average_material_cost($supplier, $stock_id, $price, $qty, $date, $adj_only=false)
{
+ // probably this function should be optimized
+ // passing transaction cart as argument. This would
+ // save a couple of db calls like get_supplier()
+
+ $supp = get_supplier($supplier);
if ($supplier != null)
- $currency = get_supplier_currency($supplier);
+ $currency = $supp['curr_code'];
else
$currency = null;
+
+ if ($supp['tax_included'])
+ $price = get_tax_free_price_for_item($stock_id, $price, $supp['tax_group_id'],
+ $supp['tax_included']);
+
$dec = user_price_dec();
price_decimal_format($price, $dec);
$price = round2($price, $dec);
function read_supplier_details_to_trans(&$supp_trans, $supplier_id)
{
- $sql = "SELECT ".TB_PREF."suppliers.supp_name, ".TB_PREF."payment_terms.terms, "
- .TB_PREF."payment_terms.days_before_due,
- ".TB_PREF."payment_terms.day_in_following_month,
- ".TB_PREF."suppliers.tax_group_id, ".TB_PREF."tax_groups.name As tax_group_name
- From ".TB_PREF."suppliers, ".TB_PREF."payment_terms, ".TB_PREF."tax_groups
- WHERE ".TB_PREF."suppliers.tax_group_id = ".TB_PREF."tax_groups.id
- AND ".TB_PREF."suppliers.payment_terms=".TB_PREF."payment_terms.terms_indicator
- AND ".TB_PREF."suppliers.supplier_id = ".db_escape($supplier_id);
+ $sql = "SELECT supp.supp_name, terms.terms, terms.days_before_due,
+ terms.day_in_following_month, supp.tax_included,
+ supp.tax_group_id, tax_group.name AS tax_group_name
+ FROM ".TB_PREF."suppliers supp, ".TB_PREF."payment_terms terms, ".TB_PREF."tax_groups tax_group
+ WHERE supp.tax_group_id = tax_group.id
+ AND supp.payment_terms=terms.terms_indicator
+ AND supp.supplier_id = ".db_escape($supplier_id);
$result = db_query($sql, "The supplier record selected: " . $supplier_id . " cannot be retrieved");
$myrow = db_fetch($result);
$supp_trans->supplier_id = $supplier_id;
+ $supp_trans->tax_included = $myrow['tax_included'];
$supp_trans->supplier_name = $myrow['supp_name'];
$supp_trans->terms_description = $myrow['terms'];
$tax_total = 0;
$taxes = $supp_trans->get_taxes($supp_trans->tax_group_id);
- foreach ($taxes as $taxitem)
+ foreach ($taxes as $n => $taxitem)
{
- $taxitem['Value'] = round2($taxitem['Value'], user_price_dec());
- $tax_total += $taxitem['Value'];
+ $taxes[$n]['Value'] = round2($taxitem['Value'], user_price_dec());
+ $tax_total += $taxes[$n]['Value'];
}
- $invoice_items_total = $supp_trans->get_total_charged($supp_trans->tax_group_id);
+ $invoice_items_total = $supp_trans->get_items_total();
+// $invoice_items_total = $supp_trans->get_total_taxfree($supp_trans->tax_group_id);
+
+ if($supp_trans->tax_included==0) {
+ $item_added_tax = $tax_total;
+ } else {
+ $item_added_tax = 0;
+
+ }
$trans_type = $supp_trans->trans_type;
$invoice_items_total = -$invoice_items_total;
$tax_total = -$tax_total;
$supp_trans->ov_discount = -$supp_trans->ov_discount; // this isn't used at all...
+ $item_added_tax = -$item_added_tax;
}
$date_ = $supp_trans->tran_date;
/*First insert the invoice into the supp_trans table*/
$invoice_id = add_supp_trans($trans_type, $supp_trans->supplier_id, $date_, $supp_trans->due_date,
$supp_trans->reference, $supp_trans->supp_reference,
- $invoice_items_total, $tax_total, $supp_trans->ov_discount);
+ $invoice_items_total, $item_added_tax, $supp_trans->ov_discount);
$total = 0;
/* Now the control account */
$supplier_accounts = get_supplier_accounts($supp_trans->supplier_id);
$total += add_gl_trans_supplier($trans_type, $invoice_id, $date_, $supplier_accounts["payable_account"], 0, 0,
- -($invoice_items_total + $tax_total + $supp_trans->ov_discount),
+ -($invoice_items_total + $item_added_tax + $supp_trans->ov_discount),
$supp_trans->supplier_id,
"The general ledger transaction for the control total could not be added");
- /*Loop through the GL Entries and create a debit posting for each of the accounts entered */
-
- /*the postings here are a little tricky, the logic goes like this:
- if its a general ledger amount it goes straight to the account specified
-
- if its a GRN amount invoiced then :
-
- The cost as originally credited to GRN suspense on arrival of items is debited to GRN suspense. Any difference
- between the std cost and the currency cost charged as converted at the ex rate of of the invoice is written off
- to the purchase price variance account applicable to the item being invoiced.
- */
foreach ($supp_trans->gl_codes as $entered_gl_code)
{
$trans_type, $invoice_id, $entered_gl_code->amount,
$ex_rate, $date_, $supp_trans->supp_reference);
}
- foreach ($supp_trans->grn_items as $entered_grn)
+ foreach ($supp_trans->grn_items as $line_no => $entered_grn)
{
if ($trans_type == ST_SUPPCREDIT)
$line_tax = $entered_grn->full_charge_price($supp_trans->tax_group_id) - $line_taxfree;
$stock_gl_code = get_stock_gl_code($entered_grn->item_code);
$iv_act = (is_inventory_item($entered_grn->item_code) ? $stock_gl_code["inventory_account"] : $stock_gl_code["cogs_account"]);
+
$total += add_gl_trans_supplier($trans_type, $invoice_id, $date_, $supplier_accounts["purchase_account"],
$stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'],
$entered_grn->this_quantity_inv * $line_taxfree, $supp_trans->supplier_id);
// ----------------------------------------------------------------------
add_supp_invoice_item($trans_type, $invoice_id, $entered_grn->item_code,
- $entered_grn->item_description, 0, $line_taxfree, $line_tax,
+ $entered_grn->item_description, 0, $entered_grn->chg_price, $line_tax,
$entered_grn->this_quantity_inv, $entered_grn->id, $entered_grn->po_detail_item, "");
} /* end of GRN postings */
/* Now the TAX account */
$taxitem['Net'] = -$taxitem['Net'];
$taxitem['Value'] = -$taxitem['Value'];
}
- // here we suppose that tax is never included in price (we are company customer).
add_trans_tax_details($trans_type, $invoice_id,
- $taxitem['tax_type_id'], $taxitem['rate'], 0, $taxitem['Value'],
+ $taxitem['tax_type_id'], $taxitem['rate'], $supp_trans->tax_included, $taxitem['Value'],
$taxitem['Net'], $ex_rate, $date_, $supp_trans->supp_reference);
if ($trans_type == ST_SUPPCREDIT)
$supp_trans->ov_amount = $trans_row["ov_amount"];
$supp_trans->ov_discount = $trans_row["ov_discount"];
$supp_trans->ov_gst = $trans_row["ov_gst"];
+ $supp_trans->tax_included = $trans_row["tax_included"];
$id = $trans_row["trans_no"];
begin_transaction();
/*Insert to purchase order header record */
- $sql = "INSERT INTO ".TB_PREF."purch_orders (supplier_id, Comments, ord_date, reference, requisition_no, into_stock_location, delivery_address, total) VALUES(";
+ $sql = "INSERT INTO ".TB_PREF."purch_orders (supplier_id, Comments, ord_date, reference,
+ requisition_no, into_stock_location, delivery_address, total, tax_included) VALUES(";
$sql .= db_escape($po_obj->supplier_id) . "," .
db_escape($po_obj->Comments) . ",'" .
date2sql($po_obj->orig_order_date) . "', " .
db_escape($po_obj->supp_ref) . ", " .
db_escape($po_obj->Location) . ", " .
db_escape($po_obj->delivery_address) . ", " .
- db_escape($po_obj->get_trans_total()) . ")";
+ db_escape($po_obj->get_trans_total()). ", " .
+ db_escape($po_obj->tax_included) . ")";
db_query($sql, "The purchase order header record could not be inserted");
into_stock_location=" . db_escape($po_obj->Location). ",
ord_date='" . date2sql($po_obj->orig_order_date) . "',
delivery_address=" . db_escape($po_obj->delivery_address).",
- total=". db_escape($po_obj->get_trans_total());
+ total=". db_escape($po_obj->get_trans_total()).",
+ tax_included=". db_escape($po_obj->tax_included);
$sql .= " WHERE order_no = " . $po_obj->order_no;
db_query($sql, "The purchase order could not be updated");
$myrow = db_fetch($result);
$order->order_no = $order_no;
- $order->supplier_id = $myrow["supplier_id"];
- $order->supplier_name = $myrow["supp_name"];
- $order->curr_code = $myrow["curr_code"];
- $order->tax_group_id = $myrow['tax_group_id'];
+
+ $order->set_supplier($myrow["supplier_id"], $myrow["supp_name"], $myrow["curr_code"],
+ $myrow['tax_group_id'], $myrow["tax_included"]);
+
$order->credit = get_current_supp_credit($order->supplier_id);
$order->orig_order_date = sql2date($myrow["ord_date"]);
AND trans.tran_date >= '$date_after'
AND trans.tran_date <= '$date_to'
AND trans.ov_amount != 0"; // exclude voided transactions
- if ($_POST['supplier_id'] != ALL_TEXT)
+
+ $sql2 = "SELECT ".ST_SUPPRECEIVE." as type,
+ trans.id as trans_no,
+ trans.reference,
+ supplier.supp_name,
+ '' as supp_reference,
+ delivery_date as tran_date,
+ '' as due_date,
+ supplier.curr_code,
+ '' AS TotalAmount,
+ '' AS Allocated,
+ 0 as OverDue,
+ 1 as Settled
+ FROM ".TB_PREF."grn_batch as trans, ".TB_PREF."suppliers as supplier
+ WHERE supplier.supplier_id = trans.supplier_id
+ AND trans.delivery_date >= '$date_after'
+ AND trans.delivery_date <= '$date_to'";
+
+ if ($_POST['supplier_id'] != ALL_TEXT) {
$sql .= " AND trans.supplier_id = ".db_escape($_POST['supplier_id']);
+ $sql2 .= " AND trans.supplier_id = ".db_escape($_POST['supplier_id']);
+ }
+ if (($_POST['filterType'] == '6'))
+ {
+ $sql = $sql2;
+ }
+ elseif (!isset($_POST['filterType']) || $_POST['filterType'] == ALL_TEXT || $_POST['filterType'] == '6') {
+ $sql = "SELECT * FROM (($sql) UNION ($sql2)) as tr";
+ }
+
if (isset($_POST['filterType']) && $_POST['filterType'] != ALL_TEXT)
{
if (($_POST['filterType'] == '1'))
var $lines_on_order = 0;
var $credit;
var $tax_group_id;
+ var $tax_group_array = null; // saves db queries
+ var $tax_included; // type of prices
var $reference;
$this->line_items = array();
$this->lines_on_order = $this->order_no = $this->supplier_id = 0;
}
-
+
+ function set_supplier($supplier_id, $supplier_name, $curr_code, $tax_group_id, $tax_included)
+ {
+ $this->supplier_id = $supplier_id;
+ $this->supplier_name = $supplier_name;
+ $this->curr_code = $curr_code;
+ $this->tax_group_id = $tax_group_id;
+ $this->tax_included = $tax_included;
+ $this->tax_group_array = get_tax_group_items_as_array($tax_group_id);
+ }
+
function add_to_order($line_no, $stock_id, $qty, $item_descr, $price, $uom, $req_del_date, $qty_inv, $qty_recd)
{
if ($qty != 0 && isset($qty))
}
return 0;
}
+
+ function get_taxes($shipping_cost=null)
+ {
+ $items = array();
+ $prices = array();
+ if($shipping_cost==null)
+ $shipping_cost = 0;//$this->freight_cost;
+
+ foreach ($this->line_items as $ln_itm) {
+ $items[] = $ln_itm->stock_id;
+ $prices[] = round($ln_itm->quantity * $ln_itm->price, user_price_dec());
+ }
+ $taxes = get_tax_for_items($items, $prices, $shipping_cost,
+ $this->tax_group_id, $this->tax_included, $this->tax_group_array);
+
+ // Adjustment for swiss franken, we always have 5 rappen = 1/20 franken
+ if ($this->curr_code == 'CHF') {
+ $val = $taxes['1']['Value'];
+ $val1 = (floatval((intval(round(($val*20),0)))/20));
+ $taxes['1']['Value'] = $val1;
+ }
+ return $taxes;
+ }
+
/*
Returns order value including all taxes
*/
$total += $value;
}
- $taxes = get_tax_for_items($items, $prices, 0, $this->tax_group_id);
-
- foreach($taxes as $tax)
- $total += round($tax['Value'], $dec);
+ if (!$this->tax_included ) {
+ $taxes = get_tax_for_items($items, $prices, 0, $this->tax_group_id,
+ $this->tax_included, $this->tax_group_array);
+ foreach($taxes as $tax)
+ $total += round($tax['Value'], $dec);
+ }
return $total;
}
var $price;
var $units;
var $req_del_date;
+ var $tax_type;
+ var $tax_type_name;
var $quantity; // current/entry quantity of PO line
var $qty_inv; // quantity already invoiced against this line
$this->req_del_date = $req_del_date;
$this->price = $prc;
// $this->units = $uom;
+ $this->tax_type = $item_row["tax_type_id"];
+ $this->tax_type_name = $item_row["tax_type_name"];
$this->units = $item_row["units"];
$this->qty_received = $qty_recd;
$this->qty_inv = $qty_inv;
var $tax_description;
var $tax_group_id;
+ var $tax_included;
var $trans_type; // invoice or credit
{
$this->grn_items[$grn_item_id] = new grn_item($grn_item_id, $po_detail_item,
$item_code, $item_description, $qty_recd, $prev_quantity_inv, $this_quantity_inv,
- $order_price, $chg_price, $std_cost_unit, $gl_code);
+ $order_price, $chg_price, $std_cost_unit, $gl_code, $this->tax_included);
return 1;
}
foreach ($this->grn_items as $ln_itm)
{
$items[] = $ln_itm->item_code;
- $prices[] =round( ($ln_itm->this_quantity_inv * $ln_itm->taxfree_charge_price($tax_group_id, $tax_group)),
+// $prices[] =round( ($ln_itm->this_quantity_inv * $ln_itm->taxfree_charge_price($tax_group_id, $tax_group)),
+ $prices[] =round( ($ln_itm->this_quantity_inv * $ln_itm->chg_price),
user_price_dec());
}
if ($tax_group_id == null)
- $tax_group_id = $this->tax_group_id;
-
- $taxes = get_tax_for_items($items, $prices, $shipping_cost, $tax_group_id);
+ $tax_group_id = $this->tax_group_id;
+ $taxes = get_tax_for_items($items, $prices, $shipping_cost, $tax_group_id,
+ $this->tax_included);
///////////////// Joe Hunt 2009.08.18
////////////////
return $taxes;
}
-
- function get_total_charged($tax_group_id=null)
+ //
+ // Returns total invoice amount without taxes.
+ //
+ function get_total_taxfree($tax_group_id=null)
{
$total = 0;
return $total;
}
+ function get_items_total()
+ {
+ $total = 0;
+
+ foreach ($this->grn_items as $ln_itm)
+ $total += round($ln_itm->this_quantity_inv * $ln_itm->chg_price, user_price_dec());
+
+ return $total;
+
+ foreach ($this->gl_codes as $gl_line)
+ {
+ $total += $gl_line->amount;
+ }
+ }
} /* end of class defintion */
class grn_item
var $chg_price;
var $std_cost_unit;
var $gl_code;
+ var $tax_included;
function grn_item ($id, $po_detail_item, $item_code, $item_description, $qty_recd,
$prev_quantity_inv, $this_quantity_inv, $order_price, $chg_price,
- $std_cost_unit, $gl_code)
+ $std_cost_unit, $gl_code, $tax_included)
{
$this->id = $id;
$this->chg_price = $chg_price;
$this->std_cost_unit = $std_cost_unit;
$this->gl_code = $gl_code;
+ $this->tax_included = $tax_included;
}
function full_charge_price($tax_group_id, $tax_group=null)
{
return get_full_price_for_item($this->item_code,
- $this->chg_price, $tax_group_id, 0, $tax_group);
+ $this->chg_price, $tax_group_id, $this->tax_included, $tax_group);
}
function taxfree_charge_price($tax_group_id, $tax_group=null)
// if ($tax_group_id==null)
// return $this->chg_price;
return get_tax_free_price_for_item($this->item_code, $this->chg_price,
- $tax_group_id, 0, $tax_group);
+ $tax_group_id, $this->tax_included, $tax_group);
}
}
label_row(_("Sub-total:"), price_format( $supp_trans->ov_amount), "colspan=$colspan align=right", "align=right");
$taxes = $supp_trans->get_taxes($supp_trans->tax_group_id);
- $tax_total = display_edit_tax_items($taxes, $colspan, 0); // tax_included==0 (we are the company)
+ $tax_total = display_edit_tax_items($taxes, $colspan, $supp_trans->tax_included);
$display_total = price_format($supp_trans->ov_amount + $tax_total);
{
$th = array(_("Delivery"), _("Sequence #"), _("P.O."), _("Item"), _("Description"),
_("Received On"), _("Quantity Received"), _("Quantity Invoiced"),
- _("Qty Yet To Invoice"), _("Order Price"), _("Total"), "");
+ _("Qty Yet To Invoice"), $supp_trans->tax_included ? _("Price after Tax") : _("Price before Tax"),
+ _("Total"), "");
if (($supp_trans->trans_type == ST_SUPPINVOICE) && $_SESSION["wa_current_user"]->can_access('SA_GRNDELETE')) // Added 2008-10-18 by Joe Hunt. Only admins can remove GRNs
$th[] = "";
if ($supp_trans->trans_type == ST_SUPPCREDIT)
function get_supplier_details_to_order(&$order, $supplier_id)
{
- $sql = "SELECT curr_code, supp_name, tax_group_id,
+ $sql = "SELECT curr_code, supp_name, tax_group_id, supp.tax_included,
supp.credit_limit - Sum(IFNULL(IF(trans.type=".ST_SUPPCREDIT.", -1, 1)
* (ov_amount + ov_gst + ov_discount),0)) as cur_credit
FROM ".TB_PREF."suppliers supp
$myrow = db_fetch($result);
$order->credit = $myrow["cur_credit"];
- $order->tax_group_id = $myrow["tax_group_id"];
- $order->curr_code = $_POST['curr_code'] = $myrow["curr_code"];
- $order->supplier_name = $_POST['supplier_name'] = $myrow["supp_name"];
- $order->supplier_id = $_POST['supplier_id'] = $supplier_id;
+ $_POST['supplier_id'] = $supplier_id;
+ $_POST['supplier_name'] = $myrow["supp_name"];
+ $_POST['curr_code'] = $myrow["curr_code"];
+
+ $order->set_supplier($supplier_id, $myrow["supp_name"], $myrow["curr_code"],
+ $myrow["tax_group_id"], $myrow["tax_included"]);
}
//---------------------------------------------------------------------------------------------------
$th = array(_("Item Code"), _("Item Description"), _("Quantity"),
_("Received"), _("Unit"),
- _("Required Delivery Date"), _("Price"), _("Line Total"), "");
+ _("Required Delivery Date"), $order->tax_included ? _("Price after Tax") : _("Price before Tax"), _("Line Total"), "");
if ($order->trans_type != ST_PURCHORDER)
array_remove($th, 5);
if ($id==-1 && $editable)
po_item_controls($order, $k);
- $display_total = price_format($total);
- label_row(_("Total Excluding Shipping/Tax"), $display_total, "colspan="
- .(count($order->line_items) ? 7:6)." align=right",
- "nowrap align=right", 2);
+ $colspan = count($th)-2;
+ if (count($order->line_items))
+ $colspan--;
+
+ $display_sub_total = price_format($total);
+
+ label_row(_("Sub-total"), $display_sub_total, "colspan=$colspan align=right","align=right", 2);
+
+ $taxes = $order->get_taxes(input_num('freight_cost'));
+
+ $tax_total = display_edit_tax_items($taxes, $colspan, $order->tax_included, 2);
+
+ $display_total = price_format(($total + input_num('freight_cost') + $tax_total));
+
+ start_row();
+ label_cells(_("Amount Total"), $display_total, "colspan=$colspan align='right'","align='right'");
+ $order->order_no ? submit_cells('update', _("Update"), "colspan=2 align='center'", _("Refresh"), true)
+ : label_cell('', "colspan=2");
+ end_row();
end_table(1);
div_end();
}
else
{
- submit_cells('EnterLine', _("Add Item"), "colspan=2",
+ submit_cells('EnterLine', _("Add Item"), "colspan=2 align='center'",
_('Add new item to document'), true);
}
set_focus('delivery_address');
return false;
}
-
if (get_post('StkLocation') == '')
{
display_error(_("There is no location specified to move any items into."));
$inv->tran_date = $cart->orig_order_date;
$inv->reference = $ref;
$inv->supp_reference = $cart->supp_ref;
+ $inv->tax_included = $cart->tax_included;
$supp = get_supplier($cart->supplier_id);
$inv->tax_group_id = $supp['tax_group_id'];
// $inv->ov_discount 'this isn't used at all'
}
}
- $display_total = number_format2($total,user_price_dec());
- label_row(_("Total value of items received"), $display_total, "colspan=8 align=right",
- "nowrap align=right");
+ $colspan = count($th)-1;
+
+ $display_sub_total = price_format($total/* + input_num('freight_cost')*/);
+
+ label_row(_("Sub-total"), $display_sub_total, "colspan=$colspan align=right","align=right");
+ $taxes = $_SESSION['PO']->get_taxes(input_num('freight_cost'));
+
+ $tax_total = display_edit_tax_items($taxes, $colspan, $_SESSION['PO']->tax_included);
+
+ $display_total = price_format(($total + input_num('freight_cost') + $tax_total));
+
+ start_row();
+ label_cells(_("Amount Total"), $display_total, "colspan=$colspan align='right'","align='right'");
+ end_row();
end_table();
div_end();
}
end_form();
//--------------------------------------------------------------------------------------------------
-
end_page();
?>