2 /**********************************************************************
3 Copyright (C) FrontAccounting, LLC.
4 Released under the terms of the GNU General Public License, GPL,
5 as published by the Free Software Foundation, either version 3
6 of the License, or (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 include_once($path_to_root . "/includes/prefs/sysprefs.inc");
13 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
39 var $tax_info; // tax info for the GL transaction
41 function items_cart($type, $trans_no=0)
43 $this->trans_type = $type;
44 $this->order_id = $trans_no;
46 if (in_array($type, array(ST_LOCTRANSFER, ST_INVADJUST, ST_COSTUPDATE, ST_MANUISSUE, ST_MANURECEIVE, ST_JOURNAL)))
47 $this->currency = get_company_pref('curr_default');
51 // --------------- line item functions
53 function add_to_cart($line_no, $stock_id, $qty, $standard_cost, $description=null)
56 if (isset($stock_id) && $stock_id != "" && isset($qty))
58 $this->line_items[$line_no] = new line_item($stock_id, $qty,
59 $standard_cost, $description);
64 // shouldn't come here under normal circumstances
65 display_error("unexpected - adding an invalid item or null quantity", "", true);
71 function find_cart_item($stock_id)
73 foreach($this->line_items as $line_no=>$line) {
74 if ($line->stock_id == $stock_id)
75 return $this->line_items[$line_no];
80 function update_cart_item($line_no, $qty, $standard_cost)
82 $this->line_items[$line_no]->quantity = $qty;
83 $this->line_items[$line_no]->standard_cost = $standard_cost;
86 function remove_from_cart($line_no)
88 array_splice($this->line_items, $line_no, 1);
91 function count_items()
93 return count($this->line_items);
96 function check_qoh($location, $date_, $reverse=false)
100 $low_stock = array();
102 if (!$SysPrefs->allow_negative_stock())
104 foreach ($this->line_items as $line_no => $line_item)
105 if (has_stock_holding($line_item->mb_flag))
107 $quantity = $line_item->quantity;
109 $quantity = -$line_item->quantity;
114 if (check_negative_stock($line_item->stock_id, $quantity, $location, $date_))
115 $low_stock[] = $line_item->stock_id;
121 // ----------- GL item functions
123 function add_gl_item($code_id, $dimension_id, $dimension2_id, $amount, $memo='', $act_descr=null, $person_id=null)
125 if (isset($code_id) && $code_id != "" && isset($amount) && isset($dimension_id) &&
126 isset($dimension2_id))
128 $this->gl_items[] = new gl_item($code_id, $dimension_id, $dimension2_id, $amount, $memo, $act_descr, $person_id);
133 // shouldn't come here under normal circumstances
134 display_error("unexpected - invalid parameters in add_gl_item($code_id, $dimension_id, $dimension2_id, $amount,...)", "", true);
140 function update_gl_item($index, $code_id, $dimension_id, $dimension2_id, $amount, $memo='', $act_descr=null, $person_id=null)
142 $this->gl_items[$index]->code_id = $code_id;
143 $this->gl_items[$index]->person_id = $person_id;
145 $gl_type = is_subledger_account($code_id, $person_id);
148 $this->gl_items[$index]->person_type_id = $gl_type > 0 ? PT_CUSTOMER : PT_SUPPLIER;
149 $data = get_subaccount_data($code_id, $person_id);
150 $this->gl_items[$index]->person_name = $data['name'];
151 $this->gl_items[$index]->branch_id = $data['id'];
154 $this->gl_items[$index]->person_type_id = $this->gl_items[$index]->person_name = '';
156 $this->gl_items[$index]->dimension_id = $dimension_id;
157 $this->gl_items[$index]->dimension2_id = $dimension2_id;
158 $this->gl_items[$index]->amount = $amount;
159 $this->gl_items[$index]->reference = $memo;
160 if ($act_descr == null)
161 $this->gl_items[$index]->description = get_gl_account_name($code_id);
163 $this->gl_items[$index]->description = $act_descr;
167 function remove_gl_item($index)
169 array_splice($this->gl_items, $index, 1);
172 function count_gl_items()
174 return count($this->gl_items);
177 function gl_items_total()
180 foreach ($this->gl_items as $gl_item)
181 $total += $gl_item->amount;
185 function gl_items_total_debit()
188 foreach ($this->gl_items as $gl_item)
190 if ($gl_item->amount > 0)
191 $total += $gl_item->amount;
196 function gl_items_total_credit()
199 foreach ($this->gl_items as $gl_item)
201 if ($gl_item->amount < 0)
202 $total += $gl_item->amount;
207 // ------------ common functions
209 function clear_items()
211 unset($this->line_items);
212 $this->line_items = array();
214 unset($this->gl_items);
215 $this->gl_items = array();
219 // Check if cart contains virtual subaccount (AP/AR) postings
221 function has_sub_accounts()
223 foreach ($this->gl_items as $gl_item)
225 if ($gl_item->person_id)
232 // Check if cart contains postings to tax accounts
236 foreach ($this->gl_items as $gl_item)
238 if (is_tax_account($gl_item->code_id))
245 Collect tax info from the GL transaction lines and return as array of values:
246 'tax_date' - tax date
247 'tax_group' - related counterparty tax group
248 'tax_category' - tax category (not set for now)
249 'net_amount' - tax amounts array indexed by tax type id
250 'tax_in', 'tax_out' - tax amounts array indexed by tax type id
251 'tax_reg' - tax register used
253 function collect_tax_info()
256 $subledger_sum = $net_sum = 0;
258 $tax_info['tax_date'] = $this->tran_date;
259 $vat_percent = get_company_pref('partial_vat_percent');
260 $factor = $vat_percent && ($this->vat_category == VC_PARTIAL) ? $vat_percent/100: 1;
262 foreach($this->gl_items as $gl)
264 if ($person_type = is_subledger_account($gl->code_id, $gl->person_id))
266 $tax_info['person_type'] = $person_type < 0 ? PT_SUPPLIER : PT_CUSTOMER;
267 $tax_info['person_id'] = $gl->person_id;
269 if ($tax_info['person_type'] == PT_CUSTOMER)
271 $branch = get_default_branch($gl->person_id);
272 $tax_info['tax_group'] = $branch['tax_group_id'];
274 $supplier = get_supplier($gl->person_id);
275 $tax_info['tax_group'] = $supplier['tax_group_id'];
277 $subledger_sum += $gl->amount;
278 } elseif ($tax_id = is_tax_account($gl->code_id))
280 $tax_type = get_tax_type($tax_id);
281 if ($gl->code_id == $tax_type['purchasing_gl_code']) {
282 if (!isset($tax_info['tax_in'][$tax_id]))
283 $tax_info['tax_in'][$tax_id] = 0;
284 $tax_info['tax_in'][$tax_id] += $gl->amount;
285 $tax_info['tax_reg'] = TR_INPUT;
287 if (!isset($tax_info['tax_out'][$tax_id]))
288 $tax_info['tax_out'][$tax_id] = 0;
289 $tax_info['tax_out'][$tax_id] -= $gl->amount;
290 if (!isset($tax_info['tax_reg'])) // TR_INPUT has priority (EU are posted on both accounts)
291 $tax_info['tax_reg'] = TR_OUTPUT;
293 if ($tax_type['rate'])
295 // assume transaction adjustment for negative tax in/out
296 $sign = (@$tax_info['tax_in'][$tax_id] < 0 || @$tax_info['tax_out'][$tax_id] < 0) ? -1 : 1;
297 // we can have both input and output tax postings in some cases like intra-EU trade.
298 // so just calculate net_amount from the higher in/out tax
299 $tax_info['net_amount'][$tax_id]
300 = $sign*round2(max(abs(@$tax_info['tax_in'][$tax_id]), abs(@$tax_info['tax_out'][$tax_id]))/$tax_type['rate']*100, 2)/$factor;
304 $net_sum += $gl->amount;
306 // if no tax amount posted guess register type from person_type used (e.g. export invoice)
307 if (!isset($tax_info['tax_reg']) && isset($tax_info['person_type']))
308 $tax_info['tax_reg'] = $tax_info['person_type']==PT_CUSTOMER ? TR_OUTPUT : TR_INPUT;
310 if (count(@$tax_info['net_amount'])) // guess exempt sales/purchase if any tax has been found
312 $ex_net = abs($net_sum) - @array_sum($tax_info['net_amount']);
314 $tax_info['net_amount_ex'] = $ex_net;
320 function set_currency($curr, $rate=0)
322 $this->currency = $curr;
327 Reduce number of necessary gl posting lines.
331 /* reduce additional postings */
333 foreach($this->gl_items as $n => $gl)
335 $prev = @$codes[$gl->code_id][$gl->person_id][$gl->dimension_id][$gl->dimension2_id][$gl->reference];
336 if (isset($prev)) { // add amount to previous line for the same gl_code dims and memo
337 $this->gl_items[$prev]->amount += $gl->amount;
338 if ($this->gl_items[$prev]->amount == 0) // discard if overall amount==0
340 unset($this->gl_items[$prev], $codes[$gl->code_id][$gl->person_id][$gl->dimension_id][$gl->dimension2_id][$gl->reference]);
342 unset($this->gl_items[$n]);
344 $codes[$gl->code_id][$gl->person_id][$gl->dimension_id][$gl->dimension2_id][$gl->reference] = $n;
348 Write transaction GL postings, creating tax records and updating AP/AR/bank ledger if needed.
350 function write_gl($check_balance = true)
352 $delta = $this->gl_items_total();
353 if ($check_balance && floatcmp($delta, 0) !=0)
355 $this->add_gl_item(get_company_pref($delta>0 ? 'rounding_db_act' : 'rounding_cr_act'),
357 error_log(sprintf( _("Rounding error %s encountered for trans_type:%s,trans_no:%s"), $delta, $this->trans_type, $this->order_id));
360 $bank_trans = $supp_trans = $cust_trans = array();
362 foreach($this->gl_items as $gl)
364 $total_gl += add_gl_trans($this->trans_type, $this->order_id, $this->tran_date, $gl->code_id, $gl->dimension_id, $gl->dimension2_id,
365 $gl->reference, $gl->amount, $this->currency, $gl->person_type_id, $gl->person_id, "", $this->rate);
367 // post to first found bank account using given gl acount code.
368 $is_bank_to = is_bank_account($gl->code_id);
369 if ($is_bank_to && (get_bank_account_currency($is_bank_to) == $this->currency)) // do not register exchange variations in bank trans
371 if (!isset($bank_trans[$is_bank_to]))
372 $bank_trans[$is_bank_to] = 0;
374 $bank_trans[$is_bank_to] += $gl->amount;
375 } elseif ($gl->person_id)
377 $home_currency = get_company_currency();
378 // collect per counterparty amounts (in case more than one posting was done to the account),
379 // do not post exchange variations to AR/AP (journal in not customer/supplier currency)
380 if ($gl->person_type_id==PT_SUPPLIER && (get_supplier_currency($gl->person_id) == $this->currency || $this->currency != $home_currency))
381 $supp_trans[$gl->person_id] = @$supp_trans[$gl->person_id] + $gl->amount;
383 if ($gl->person_type_id==PT_CUSTOMER && (get_customer_currency(null, $gl->branch_id) == $this->currency || $this->currency != $home_currency))
384 $cust_trans[$gl->branch_id] = @$cust_trans[$gl->branch_id] + $gl->amount;
388 // post currency roundings if any
389 if ($check_balance && floatcmp($total_gl, 0))
390 add_gl_trans($this->trans_type, $this->order_id, $this->tran_date,
391 get_company_pref($total_gl>0 ? 'rounding_db_act' : 'rounding_cr_act'), 0, 0, _('Exchange rate roundings'), -$total_gl);
393 // update bank ledger if used
394 foreach($bank_trans as $bank_id => $amount)
395 add_bank_trans($this->trans_type, $this->order_id, $bank_id, $this->reference,
396 $this->tran_date, $amount, 0, "", $this->currency,
397 "Cannot insert a destination bank transaction");
399 // add AP/AR for journal transaction
400 if ($this->trans_type == ST_JOURNAL)
403 foreach($cust_trans as $branch_id => $amount)
404 if (floatcmp($amount, 0))
405 write_cust_journal($this->trans_type, $this->order_id, $branch_id, $this->tran_date,
406 $this->reference, -$amount, $this->rate);
408 foreach($supp_trans as $supp_id => $amount)
409 if (floatcmp($amount, 0))
410 write_supp_journal($this->trans_type, $this->order_id, $supp_id, $this->tran_date,
411 $this->reference, -$amount, $this->rate, $this->source_ref);
414 // generate tax records for journal transaction
415 if ($this->trans_type == ST_JOURNAL && is_array($this->tax_info))
417 foreach($this->tax_info['net_amount'] as $tax_id => $net)
422 // in EU VAT system intra-community goods aquisition is posted to both purchasing and sales tax accounts,
423 // but included only in purchase register. To avoid double registering ELSE is used below!
424 if (isset($this->tax_info['tax_in'][$tax_id]))
426 $tax = $this->tax_info['tax_in'][$tax_id];
429 elseif (isset($this->tax_info['tax_out'][$tax_id]))
431 $tax = $this->tax_info['tax_out'][$tax_id];
434 elseif (isset($this->tax_info['tax_reg'])) // e.g. export
437 $reg = $this->tax_info['tax_reg'];
441 $tax_nominal = $this->tax_info['rate'][$tax_id]/100*$net;
442 add_trans_tax_details($this->trans_type, $this->order_id,
443 $tax_id, $this->tax_info['rate'][$tax_id], 0, $tax_nominal, $net, $this->rate,
445 $this->source_ref, $reg);
451 //--------------------------------------------------------------------------------------------
456 var $item_description;
464 function line_item ($stock_id, $qty, $standard_cost=null, $description=null)
466 $item_row = get_item($stock_id);
468 if ($item_row == null)
469 display_error("invalid item added to order : $stock_id", "");
471 $this->mb_flag = $item_row["mb_flag"];
472 $this->units = $item_row["units"];
474 if ($description == null)
475 $this->item_description = $item_row["description"];
477 $this->item_description = $description;
479 if ($standard_cost == null)
480 $this->standard_cost = $item_row["actual_cost"];
482 $this->standard_cost = $standard_cost;
484 $this->stock_id = $stock_id;
485 $this->quantity = $qty;
486 //$this->price = $price;
491 //---------------------------------------------------------------------------------------
507 function gl_item($code_id=null, $dimension_id=0, $dimension2_id=0, $amount=0, $memo='',
508 $act_descr=null, $person_id=null)
510 //echo "adding $index, $code_id, $dimension_id, $amount, $reference<br>";
512 if ($act_descr == null && $code_id)
513 $this->description = get_gl_account_name($code_id);
515 $this->description = $act_descr;
517 $this->code_id = $code_id;
518 $this->person_id = $person_id;
519 $gl_type = is_subledger_account($code_id, $person_id);
522 $this->person_type_id = $gl_type > 0 ? PT_CUSTOMER : PT_SUPPLIER;
523 $data = get_subaccount_data($code_id, $person_id);
524 $this->person_name = $data['name'];
525 $this->branch_id = $data['id'];
527 $this->dimension_id = $dimension_id;
528 $this->dimension2_id = $dimension2_id;
529 $this->amount = round($amount, 2);
530 $this->reference = $memo;