function SalesAction($p,$v=0) {

  global $db,$ar;

  //echo <pre>; print_r($p); echo </pre>;

  $ar[act_desc][1]=Penerimaan tunai atas;

  $ar[act_bank][1]=1;

  $ar[act__cat][1]=1;



  $ar[act_desc][2]=Piutang kredit .$p[days_due_date]. atas;

  $ar[act_bank][2]=0;

  $ar[act__cat][2]=7; //piutang



  //cek dulu ref id,

  $sql1 = select id,reff_sales_id from `action` where reff_sales_id=.$p[id];

  $res1 = mysqli_query($db, $sql1);

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

    //insert

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

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

      $r4 = mysqli_fetch_assoc($res4);

      $id1=$r4[id]+1;

      $id2=$r4[id]+2;



      $idt1=array(

        id=>$id1,

        action_at=>strtotime($p[sales_at]),

        reff_sales_id=>$p[id],

        pay_from=>$p[buyer],

        invoice=>$p[invoice],

        inout=>1,

        descr=>$p[descr],

        bank=>0,

        cat=>4,

        amount=>$p[qty]*$p[hpp],

        year=>date(Y,strtotime($p[sales_at])),

        month=>date(Ym,strtotime($p[sales_at])),

        tgl=>date(Y-m-d,strtotime($p[sales_at])),

        update_at=>time(),

      );

      Insert($db,action,$idt1,$v);

      $idt2=array(

        id=>$id2,

        action_at=>strtotime($p[sales_at]),

        reff_sales_id=>$p[id],

        pay_from=>$p[buyer],

        invoice=>$p[invoice],

        inout=>1,

        descr=>$ar[act_desc][$p[cash_credit]]. .$p[descr],

        bank=>$ar[act_bank][$p[cash_credit]],

        cat=>$ar[act__cat][$p[cash_credit]],

        amount=>$p[qty]*$p[price],

        year=>date(Y,strtotime($p[sales_at])),

        month=>date(Ym,strtotime($p[sales_at])),

        tgl=>date(Y-m-d,strtotime($p[sales_at])),

        update_at=>time(),

      );

      Insert($db,action,$idt2,$v);

      $udt_41=array(

        action_id_1=>$id1,

        action_id_2=>$id2,

      );

      Update($db,sales,$udt_41,id=.$p[id],$v);

      //catat di product_trx

      $p[action_at]=strtotime($p[sales_at]);

      $p[inout]=2;

      $p[remark]=$p[descr];

      $p[action_id]=$id1;

      product_trx($p);





  } else {

    $sql2=$sql1. order by id;

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

    $dt=array();

    while($r2=mysqli_fetch_assoc($res2)) {

      $dt[id][]=$r2[id];

    }

    $udt1=array(

      action_at=>strtotime($p[sales_at]),

      reff_sales_id=>$p[id],

      pay_from=>$p[buyer],

      invoice=>$p[invoice],

      inout=>1,

      descr=>$p[descr],

      bank=>0,

      cat=>4,

      amount=>$p[qty]*$p[hpp],

      year=>date(Y,strtotime($p[sales_at])),

      month=>date(Ym,strtotime($p[sales_at])),

      tgl=>date(Y-m-d,strtotime($p[sales_at])),

      update_at=>time(),

    );

    Update($db,action,$udt1,id=.$dt[id][0],$v);

    $udt2=array(

      action_at=>strtotime($p[sales_at]),

      reff_sales_id=>$p[id],

      pay_from=>$p[buyer],

      invoice=>$p[invoice],

      inout=>1,

      descr=>$ar[act_desc][$p[cash_credit]]. .$p[descr],

      bank=>$ar[act_bank][$p[cash_credit]],

      cat=>$ar[act__cat][$p[cash_credit]],

      amount=>$p[qty]*$p[price],

      year=>date(Y,strtotime($p[sales_at])),

      month=>date(Ym,strtotime($p[sales_at])),

      tgl=>date(Y-m-d,strtotime($p[sales_at])),

      update_at=>time(),

    );

    Update($db,action,$udt2,id=.$dt[id][1],$v);

    $udt_41=array(

      action_id_1=>$dt[id][0],

      action_id_2=>$dt[id][1],

    );

    Update($db,sales,$udt_41,id=.$p[id],$v);

    //catat di product_trx

    $p[action_at]=strtotime($p[sales_at]);

    $p[inout]=2;

    $p[remark]=$p[descr];

    $p[action_id]=$dt[id][0];

    product_trx($p);

  }

}