+
+ //
+ // Returns taxes for PO/GRN.
+ // $receival=true in purchase receive context.
+ //
+ function get_taxes($shipping_cost=null, $receival=false)
+ {
+ $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->price * $ln_itm->quantity, user_price_dec());
+ }
+ $taxes = get_tax_for_items($this->trans_type, $items, $prices, $shipping_cost,
+ $this->tax_group_id, $this->tax_included);
+
+ // 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;
+ }
+ foreach($this->tax_overrides as $id => $value) // add values entered manually
+ {
+ $taxes[$id]['Override'] = $value;
+ }
+ return $taxes;
+ }
+
+ /*
+ Returns order value including all taxes
+ */
+ function get_trans_total() {
+
+ $total = 0;
+ $dec = user_price_dec();
+
+ foreach ($this->line_items as $ln_itm) {
+ $items[] = $ln_itm->stock_id;
+ $value = round($ln_itm->quantity * $ln_itm->price, $dec);
+ $prices[] =$value;
+ $total += $value;
+ }
+
+ if (!$this->tax_included ) {
+ $taxes = get_tax_for_items($this->trans_type, $items, $prices, 0, $this->tax_group_id,
+ $this->tax_included);
+
+ foreach($taxes as $tax)
+ $total += round($tax['Value'], $dec);
+ }
+ return $total;
+ }
+
+ function split_line_values()
+ {
+ // split nominal line values
+ foreach($this->line_items as $line)
+ $line->split_item_value($this);
+
+ // Exact tax values are currently entered as tax totals, so we need to move the differences back on line level.
+ // currently first item with given tax type will be fixed with the calculated difference
+ // FIXME: change UI moving tax edit to line level in line edit mode, then this workaround will be obsolete.
+ foreach($this->get_taxes() as $tax_id => $tax)
+ {
+ if ($tax['Value'] != 0 && ($tax['Value'] != $tax['Override']))
+ {
+ foreach($this->line_items as $id => $line)
+ if ($line->gl_amounts[0]['tax_type_id'] == $tax_id) // assumed single tax rate on item, so always gl_mount[0] is valid
+ {
+ $diff = $tax['Override'] - $tax['Value'];
+ $this->line_items[$id]->gl_amounts[0]['Value'] += $diff;
+ if ($this->vat_category() != VC_NONDEDUCT)
+ $this->line_items[$id]->gl_amounts[0]['Deductible'] += $diff;
+ else
+ $this->line_items[$id]->gl_amounts['Cost'] += $diff;
+ // when supplier uses net prices the price is exact, so don't fix net, still record exact VAT.
+ if ($this->tax_included)
+ {
+ $this->line_items[$id]->gl_amounts['Net'] -= $diff;
+ $this->line_items[$id]->gl_amounts['Cost'] -= $diff;
+ }
+ break;
+ }
+ }
+ }
+ }
+