function journal_add($transaction_id) {

  global $ar,$db;

  $dt=array();

  $sql = select * from transaction where id=.$transaction_id;

  $res = mysqli_query($db, $sql);

  //echo $sql;

  $r1 = mysqli_fetch_assoc($res);    



    $sql3 = select id,id2,id-id2 as k from journal where id-id2>0 order by id2 limit 1;

    $res3 = mysqli_query($db, $sql3);

    if(mysqli_num_rows($res3)>0) {

      $rw3 = mysqli_fetch_assoc($res3);

      $id2=$rw3[id2];

    } else {

      $sql4 = select * from journal order by id desc limit 1;

      $res4 = mysqli_query($db, $sql4);

      $rw4 = mysqli_fetch_assoc($res4);

      if(isset($rw4[id]) && $rw4[id]>0) {

        $id2=$rw4[id]+1;

      } else {

        $id2=1;

      }

    }



    $idt1=array(

      id=>$id2,

      transaction_id=>$r1[id],

      action_at=>$r1[action_at],

      amount=>to_numeric($r1[amount]),

      debit=>to_numeric($r1[amount]),

      inout=>1,

      account_code=>$r1[debit],

      action_id=>$r1[action_id],

      seq=>$r1[seq],

      year=>date(Y,$r1[action_at]),

      week=>date(W,$r1[action_at]),

      month=>date(Ym,$r1[action_at]),

    ); 

    $udt1=array(

      transaction_id=>$r1[id],

      action_at=>$r1[action_at],

      amount=>to_numeric($r1[amount]),

      debit=>to_numeric($r1[amount]),

      inout=>1,

      account_code=>$r1[debit],

      action_id=>$r1[action_id],

      seq=>$r1[seq],

      year=>date(Y,$r1[action_at]),

      week=>date(W,$r1[action_at]),

      month=>date(Ym,$r1[action_at]),

      update_at=>time(),

    );

    InsertUpdate($db,journal,$idt1,$udt1,0); 

    tool_id2(journal); 



    $sql3 = select id,id2,id-id2 as k from journal where id-id2>0 order by id2 limit 1;

    $res3 = mysqli_query($db, $sql3);

    if(mysqli_num_rows($res3)>0) {

      $rw3 = mysqli_fetch_assoc($res3);

      $id2=$rw3[id2];

    } else {

      $sql4 = select * from journal order by id desc limit 1;

      $res4 = mysqli_query($db, $sql4);

      $rw4 = mysqli_fetch_assoc($res4);

      if(isset($rw4[id]) && $rw4[id]>0) {

        $id2=$rw4[id]+1;

      } else {

        $id2=1;

      }

    }



    $idt2=array(

      id=>$id2,

      action_at=>$r1[action_at],

      seq=>$r1[seq],

      transaction_id=>$r1[id],

      action_id=>$r1[action_id],

      amount=>to_numeric($r1[amount]),

      credit=>to_numeric($r1[amount]),

      inout=>2,

      account_code=>$r1[credit],

      year=>date(Y,$r1[action_at]),

      week=>date(W,$r1[action_at]),

      month=>date(Ym,$r1[action_at]),

    ); 

    $udt2=array(

      transaction_id=>$r1[id],

      action_id=>$r1[action_id],

      action_at=>$r1[action_at],

      seq=>$r1[seq],

      amount=>to_numeric($r1[amount]),

      credit=>to_numeric($r1[amount]),

      inout=>2,

      account_code=>$r1[credit],

      year=>date(Y,$r1[action_at]),

      week=>date(W,$r1[action_at]),

      month=>date(Ym,$r1[action_at]),

      update_at=>time(),

    );

    InsertUpdate($db,journal,$idt2,$udt2,0); 

    tool_id2(journal);  

    account_equation($r1); 

    $dt[message]=journal & account_equation added;

    //$dt[account_equation]=account_equation($r1);    

    //JournalCount(date(Y,$r1[action_at]));

    return $dt;

}