function product(){

  global $ar,$db;  

  $dt=array();

  $dt2=array();

  //id,name,brand,qty,item_code,price_per_unit,update_at

  $sql2=SELECT `brand` from `product` where `del`=0 group by `brand` order by `brand`;

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

  //$dt[count]=$r2;

  $count=0;

  if(mysqli_num_rows($res2)>0) {

    $c=0;

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

      $dt[data][by_brand][$c][brand]=$r2[brand];

      $sql3=SELECT id,name,qty,item_code,price_per_unit,img,update_at from `product` where del=0 and brand=.$r2[brand]. order by `name`;

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

      $d=0;

      while($r3=mysqli_fetch_assoc($res3)) {

        $i_img_med=str_replace(small,medium,$r3[img]);

        $i_img_big=str_replace(small,big,$r3[img]);

        $dt[data][by_brand][$c][items][$d]=$r3;

        $dt[data][by_brand][$c][items][$d][url_img]=$ar[img_url]..$r3[img];

        $dt[data][by_brand][$c][items][$d][url_img_medium]=$ar[img_url]..$i_img_med;

        $dt[data][by_brand][$c][items][$d][url_img_big]=$ar[img_url]..$i_img_big;

        $dt[data][by_brand][$c][items][$d][hpp]=ang2($r3[price_per_unit]);

        $dt[data][by_brand][$c][items][$d][amount]=ang2($r3[price_per_unit]*$r3[qty]);

        $dt[data][by_brand][$c][items][$d][update]=date(Y-m-d H:i:s,$r3[update_at]);



        $sql4=SELECT a.id,a.action_id,a.transaction_id,a.item_in,a.item_out,a.`balance`,a.invoice,a.tgl,a.`inout`,b.descr from `product_trx` a JOIN `action` b ON a.action_id=b.id where product_id=.$r3[id]. order by a.action_at,a.action_id ;

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

        $e=0;

        while($r4=mysqli_fetch_assoc($res4)) {

          $dt[data][by_brand][$c][items][$d][trx][$e]=$r3;

          $dt[data][by_brand][$c][items][$d][trx][$e][amount]=ang2($r3[balance]*$r1[price_per_unit]);

          $e++;

        }        

        $d++;

        $count++;

      }

      $c++;

    }



    

    $sql5=SELECT id,name,img,brand,qty,item_code,price_per_unit,update_at from `product` where del=0 order by id;

    //echo $sql.;<br>;

    $res5=mysqli_query($db,$sql5);

    while($r5=mysqli_fetch_assoc($res5)) {

      $c=$r5[id];



      $i_img_med=str_replace(small,medium,$r5[img]);

      $i_img_big=str_replace(small,big,$r5[img]);



      $dt[data][by_id][$c]=$r5;

      $dt[data][by_id][$c][url_img]=$ar[img_url]..$r5[img];

      $dt[data][by_id][$c][url_img_medium]=$ar[img_url]..$i_img_med;

      $dt[data][by_id][$c][url_img_big]=$ar[img_url]..$i_img_big;

      $dt[data][by_id][$c][hpp]=ang2($r5[price_per_unit]);

      $dt[data][by_id][$c][amount]=ang2($r5[price_per_unit]*$r5[qty]);

      $dt[data][by_id][$c][update]=date(Y-m-d H:i:s,$r5[update_at]);

      $sql6=SELECT a.id,a.action_id,a.transaction_id,a.item_in,a.item_out,a.`balance`,a.invoice,a.tgl,a.`inout`,b.descr from `product_trx` a JOIN `action` b ON a.action_id=b.id where product_id=.$r5[id]. order by a.action_at,a.action_id ;

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

      $d=0;

      while($r6=mysqli_fetch_assoc($res6)) {

        $dt[data][by_id][$c][trx][$d]=$r6;

        $dt[data][by_id][$c][trx][$d][amount]=ang2($r6[balance]*$r5[price_per_unit]);

        $d++;

      }

    }



    $dt[count]=$count;

    $dt[success]=true;

  } else {

    $dt[count]=0;

  }  

  return $dt;

  $db->close();

}