+ check_is_closed($type, $trans_no);
+ $cart = new items_cart($type);
+ $cart->order_id = $trans_no;
+
+ if ($trans_no) {
+ $header = get_journal($type, $trans_no);
+ $cart->event_date = sql2date($header['event_date']);
+ $cart->doc_date = sql2date($header['doc_date']);
+ $cart->tran_date = sql2date($header['tran_date']);
+ $cart->currency = $header['currency'];
+ $cart->rate = $header['rate'];
+ $cart->source_ref = $header['source_ref'];
+
+ $result = get_gl_trans($type, $trans_no);
+
+ if ($result) {
+ while ($row = db_fetch($result)) {
+ $curr_amount = $cart->rate ? round($row['amount']/$cart->rate, $_SESSION["wa_current_user"]->prefs->price_dec()) : $row['amount'];
+ if ($curr_amount)
+ $cart->add_gl_item($row['account'], $row['dimension_id'], $row['dimension2_id'],
+ $curr_amount, $row['memo_'], '', $row['person_id']);
+ }
+ }
+ $cart->memo_ = get_comments_string($type, $trans_no);
+ $cart->reference = $header['reference'];
+
+ // update net_amounts from tax register
+
+ // retrieve tax details
+ $tax_info = $cart->collect_tax_info(); // tax amounts in reg are always consistent with GL, so we can read them from GL lines
+
+ $taxes = get_trans_tax_details($type, $trans_no);
+ while ($detail = db_fetch($taxes))
+ {
+ $tax_id = $detail['tax_type_id'];
+ $tax_info['net_amount'][$tax_id] = $detail['net_amount']; // we can two records for the same tax_id, but in this case net_amount is the same
+ $tax_info['tax_date'] = sql2date($detail['tran_date']);
+ //$tax_info['tax_group'] = $detail['tax_group_id'];
+
+ }
+ if (isset($tax_info['net_amount'])) // guess exempt sales/purchase if any tax has been found
+ {
+ $net_sum = 0;
+ foreach($cart->gl_items as $gl)
+ if (!is_tax_account($gl->code_id) && !is_subledger_account($gl->code_id, $gl->person_id))
+ $net_sum += $gl->amount;
+
+ $ex_net = abs($net_sum) - array_sum($tax_info['net_amount']);
+ if ($ex_net > 0)
+ $tax_info['net_amount_ex'] = $ex_net;
+ }
+ $cart->tax_info = $tax_info;
+
+ } else {
+ $cart->tran_date = $cart->doc_date = $cart->event_date = new_doc_date();
+ if (!is_date_in_fiscalyear($cart->tran_date))
+ $cart->tran_date = end_fiscalyear();
+ $cart->reference = $Refs->get_next(ST_JOURNAL, null, $cart->tran_date);
+ }