+// Exchange Variations Joe Hunt 2008-09-20 ////////////////////////////////////////
+
+function exchange_variation($pyt_type, $pyt_no, $type, $trans_no, $pyt_date, $amount, $person_type, $neg=false)
+{
+ global $systypes_array;
+
+ if ($person_type == PT_CUSTOMER)
+ {
+ $trans = get_customer_trans($trans_no, $type);
+ $pyt_trans = get_customer_trans($pyt_no, $pyt_type);
+ $cust_accs = get_branch_accounts($trans['branch_code']);
+ $ar_ap_act = $cust_accs['receivables_account'];
+ $person_id = $trans['debtor_no'];
+ $curr = $trans['curr_code'];
+ $date = sql2date($trans['tran_date']);
+ }
+ else
+ {
+ $trans = get_supp_trans($trans_no, $type);
+ $pyt_trans = get_supp_trans($pyt_no, $pyt_type);
+ $supp_accs = get_supplier_accounts($trans['supplier_id']);
+ $ar_ap_act = $supp_accs['payable_account'];
+ $person_id = $trans['supplier_id'];
+ $curr = $trans['curr_code'];
+ $date = sql2date($trans['tran_date']);
+ }
+ if (is_company_currency($curr))
+ return;
+ $inv_amt = round2($amount * $trans['rate'], user_price_dec());
+ $pay_amt = round2($amount * $pyt_trans['rate'], user_price_dec());
+ if ($inv_amt != $pay_amt)
+ {
+ $diff = $inv_amt - $pay_amt;
+ if ($person_type == PT_SUPPLIER)
+ $diff = -$diff;
+ if ($neg)
+ $diff = -$diff;
+ $exc_var_act = get_company_pref('exchange_diff_act');
+ if (date1_greater_date2($date, $pyt_date))
+ {
+ $memo = $systypes_array[$pyt_type]." ".$pyt_no;
+ add_gl_trans($type, $trans_no, $date, $ar_ap_act, 0, 0, $memo, -$diff, null, $person_type, $person_id);
+ add_gl_trans($type, $trans_no, $date, $exc_var_act, 0, 0, $memo, $diff, null, $person_type, $person_id);
+ }
+ else
+ {
+ $memo = $systypes_array[$type]." ".$trans_no;
+ add_gl_trans($pyt_type, $pyt_no, $pyt_date, $ar_ap_act, 0, 0, $memo, -$diff, null, $person_type, $person_id);
+ add_gl_trans($pyt_type, $pyt_no, $pyt_date, $exc_var_act, 0, 0, $memo, $diff, null, $person_type, $person_id);
+ }
+ }
+}