function transaction_add_v2($js){

  global $ar,$db;

  $dt=array();

  //$dt[param]=$js;



  $sql=SELECT * from `transaction` where `action_id`=.$js[action_id].;

  //$dt[sql][1]=$sql8;

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

  if(mysqli_num_rows($res)==0){

    //belum ada data, insert

    $sql6 = SELECT * from `action` where id=.$js[action_id];

    $res6 = mysqli_query($db, $sql6);

    //echo $sql;

    $r6 = mysqli_fetch_assoc($res6); 





    $sql3 = select id,id2,id-id2 as k from `transaction` 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);

      $id1=$rw3[id2];

    } else {

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

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

      $rw4 = mysqli_fetch_assoc($res4);

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

        $id1=$rw4[id]+1;

      } else {

        $id1=1;

      }

    }



    $idt=array(

      id=>$id1,

      action_at=>$r6[action_at],

      action_id=>$js[action_id],

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

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

      seq=>$r6[seq],

      cat=>$r6[cat],

      inout=>$r6[inout],

      debit=>$js[debit],

      credit=>$js[credit],

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

      st=>1,

      update_at=>time(),

    );

    Insert($db,transaction,$idt,0);

    tool_id2(transaction);



    $dt[success]=true;

    $dt[message]=Add transaction successfull; 



    $udt=array(

      st_trx=>1,

    );

    Update($db,action,$udt,id=.$js[action_id],0);  

    journal_add($id1); 



    $dt[data][transaction][byid]=transaction_byid();

    $dt[data][transaction][byseq]=transaction_byseq();

    $dt[data][action][byid]=action_byid();

    $dt[data][action][byseq]=action_byseq();



  } else {

    $r1=mysqli_fetch_assoc($res);

    //sudah ada data, update

    $sql6 = SELECT * from `action` where id=.$js[action_id];

    $res6 = mysqli_query($db, $sql6);

    //echo $sql;

    $r6 = mysqli_fetch_assoc($res6); 



    $udt=array(

      st_trx=>1,

    );

    Update($db,action,$udt,id=.$js[action_id],0);  



    $udt2=array(

      debit=>$js[debit],

      credit=>$js[credit],

      amount=>$r6[amount],

      update_at=>time(),

    );

    Update($db,transaction,$udt2,id=.$r1[id],0);

    tool_id2(journal); 

    $dt[journal]=journal_add($r1[id]);     

    

    $dt[data][transaction][byid]=transaction_byid();

    $dt[data][transaction][byseq]=transaction_byseq();

    $dt[data][action][byid]=action_byid();

    $dt[data][action][byseq]=action_byseq();

    $dt[success]=true;

    $dt[message]=This transaction update successfully;        

  }



  JournalCount_v2(date(Y));

list_equ_3();



  return $dt;

  $db->close();

}