function account_equation($trx) {

  global $db,$ar;

  $dt=array();

  $dt[src]=$trx;

  /*  

  //cek kolom apa saja yg diupdate

  $sql = SELECT `code` from account where code=.$trx[debit].; 

  //echo $sql.<br>;

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

  $r1  = mysqli_fetch_assoc($res);



  $sql2 = SELECT lev2,min from account where code=.$trx[credit].; 

  $res2 = mysqli_query($db,$sql2);

  $r2   = mysqli_fetch_assoc($res2);

  */

  $col_debit=c.$trx[debit];

  $col_credit=c.$trx[credit];

  

  $col3_debit=substr($col_debit,0,3);

  $col3_credit=substr($col_credit,0,3);



  $val_credit=to_numeric($trx[amount]);

  $val_debit=to_numeric($trx[amount]);



  //beban dr cash

  if($col_credit==c11101) {

    //bayar beban

    if($col3_debit==c61 or $col3_debit==c62 or $col_debit==c21100 or $col_debit==c41300 or $col_debit==c31200) {

      $val_credit=-to_numeric($trx[amount]);

      $val_debit=-to_numeric($trx[amount]);

    }

    //bayar asset

    if($col_debit==c11202 or $col_debit==c11203 or $col_debit==c11204 or $col_debit==c11205 or $col_debit==c12101 or $col_debit==c12201 or $col_debit==c12301 or $col_debit==c12401) {

      $val_credit=-to_numeric($trx[amount]);

      $val_debit=to_numeric($trx[amount]);

    }



  } 



  //ada piutang yg di bayar

  if($col_credit==c11201 && $col_debit==c11101) {

    $val_credit=-to_numeric($trx[amount]);

    $val_debit=to_numeric($trx[amount]);

  } 



  //ada piutang dg barang dagangan di retur

  if($col_credit==c11201 && $col_debit==c41100) {

    $val_credit=-to_numeric($trx[amount]);

    $val_debit=-to_numeric($trx[amount]);

  } 



  //ada piutang yg di bayar

  if($col_credit==c11202 && $col_debit==c51100) {

    $val_credit=-to_numeric($trx[amount]);

    $val_debit=-to_numeric($trx[amount]);

  } 



  //ada rtur barang pembelian kredit (utang)

  if($col_credit==c11202 && $col_debit==c21100) {

    $val_credit=-to_numeric($trx[amount]);

    $val_debit=-to_numeric($trx[amount]);

  } 



  //bayar biaya dengan utang

  if($col_credit==c22100) {

    if($col3_debit==c61 or $col3_debit==c62) {

      $val_credit=to_numeric($trx[amount]);

      $val_debit=-to_numeric($trx[amount]);

    }

  } 



  //penyusutan aset

  if($col_credit==c12202 or $col_credit==c12302 or $col_credit==c12402) {

    if($col3_debit==c61 or $col3_debit==c62) {

      $val_credit=-to_numeric($trx[amount]);

      $val_debit=-to_numeric($trx[amount]);

    }

  } 





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

    }

  }

  /*

  foreach ($ar[equ] as $k => $v) {

    $udt6=array(

      $v=>0,

    );

    Update($db,account_equation,$udt6,transaction_id=.$trx[id],0);

  }*/

  $idt=array(

    id=>$id2,

    transaction_id=>$trx[id],

    action_at=>$trx[action_at],

    action_id=>$trx[action_id],

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

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

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

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

    $col_debit=>$val_debit,

    $col_credit=>$val_credit,

  );

  $udt=array(

    action_at=>$trx[action_at],

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

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

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

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

    $col_debit=>$val_debit,

    $col_credit=>$val_credit,

  );

  InsertUpdate($db,account_equation,$idt,$udt,0);

  tool_id2(account_equation);

  equ_count();

  $dt[idt]=$idt;

  $dt[udt]=$udt;

  return $dt;

}