+ if ($new)
+ {
+ global $Refs;
+ $trans_type = ST_JOURNAL;
+ $date_ = sql2date($to);
+ $reference = $Refs->get_next($trans_type, null, $date_);
+ $Refs->save($trans_type, $trans_no, $reference);
+ add_journal($trans_type, $trans_no, $total, $date_, get_company_currency(), $reference);
+ $Refs->save($trans_type, $trans_no, $reference);
+ add_audit_trail($trans_type, $trans_no, $date_);
+ }
+ $sql = "SELECT bank_act, SUM(amount) AS amount FROM ".TB_PREF."bank_trans WHERE trans_date <= '$to' GROUP BY bank_act";
+ $result = db_query($sql, "Could not retrieve bank trans");
+ while ($row = db_fetch($result))
+ {
+ $sql = "DELETE FROM ".TB_PREF."bank_trans WHERE trans_date <= '$to' AND bank_act = '{$row['bank_act']}'";
+ db_query($sql, "Could not delete bank trans");
+ $sql = "INSERT INTO ".TB_PREF."bank_trans (type, trans_no, trans_date, bank_act, ref, amount) VALUES
+ (0, 0, '$to', '{$row['bank_act']}', '$ref', {$row['amount']})";
+ db_query($sql, "Could not insert bank trans");
+ }
+
+ $sql = "DELETE FROM ".TB_PREF."audit_trail WHERE gl_date <= '$to'";
+ db_query($sql, "Could not delete audit trail");
+
+ $sql = "SELECT type, id FROM ".TB_PREF."comments WHERE type != ".ST_SALESQUOTE." AND type != ".ST_SALESORDER." AND type != ".ST_PURCHORDER;
+ $result = db_query($sql, "Could not retrieve comments");
+ while ($row = db_fetch($result))
+ {
+ $sql = "SELECT count(*) FROM ".TB_PREF."gl_trans WHERE type = {$row['type']} AND type_no = {$row['id']}";
+ $res = db_query($sql, "Could not retrieve gl_trans");
+ $row2 = db_fetch_row($res);
+ if ($row2[0] == 0) // if no link, then delete comments
+ {
+ $sql = "DELETE FROM ".TB_PREF."comments WHERE type = {$row['type']} AND id = {$row['id']}";
+ db_query($sql, "Could not delete comments");
+ }
+ }
+ $sql = "SELECT type, id FROM ".TB_PREF."refs WHERE type != ".ST_SALESQUOTE." AND type != ".ST_SALESORDER." AND type != ".ST_PURCHORDER;
+ $result = db_query($sql, "Could not retrieve refs");
+ while ($row = db_fetch($result))
+ {
+ $sql = "SELECT count(*) FROM ".TB_PREF."gl_trans WHERE type = {$row['type']} AND type_no = {$row['id']}";
+ $res = db_query($sql, "Could not retrieve gl_trans");
+ $row2 = db_fetch_row($res);
+ if ($row2[0] == 0) // if no link, then delete refs
+ {
+ $sql = "DELETE FROM ".TB_PREF."refs WHERE type = {$row['type']} AND id = {$row['id']}";
+ db_query($sql, "Could not delete refs");
+ }
+ }
+