function sales_add($js){

  global $ar,$db;

  $dt=array();

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

    }

  }



  if($js[cat]==7 && $js[due_days]>0) {

    $due_days=$js[due_days]*24*60*60;

    $due_date=date(Y-m-d,strtotime($js[action_at])+$due_days);

    $cash_credit=2;

  } else {

    $due_date=$js[action_at];

    $cash_credit=1;

  }



  $idt=array(

    id=>$id1,

    product_id=>$js[product_id],

    hpp=>$js[hpp],

    cash_credit=>$cash_credit,

    buyer=>$js[buyer],

    sales=>$js[sales],

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

    qty=>$js[qty],

    price=>$js[price_per_unit],

    amount=>$js[qty]*$js[price_per_unit],

    due_date=>$due_date,

    invoice=>$js[invoice],

    descr=>$js[descr],

    tgl=>$js[action_at],

    days_due_date=>$js[due_days],

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

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

    update_at=>time(),

  );

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

  tool_id2(sales); 

  $dt[message]=Add sales Successfully;

  $dt[sales_id]=$id1;

  $dt[success]=true; 

  return $dt;

}