function olah_csv_mecmed($p) {
  global $db,$ar;    
  //no,code,BUS,IN_TIME,majmuah,med_hotel_no,pax,BUS_NO,contract_no,kitchen_code,qty
  //3,SUB57,BARAKAH,1-10,2026-06-07 16,SADU,512,380,66362,SAT,378
  $data=explode(',',strtoupper(trim($p)));
  $dt['no']=$data[0];
  $dt['code']=$data[1];
  $dt['bus']=$data[2];
  $dt['bus_no']=$data[3];
  if(strlen(trim($data[4]))<9) {
    $dt['in_time']='2026-'.$data[4].':00:00';
  } else {
    $dt['in_time']=$data[4].':00:00';
  }
  //$dt['in_time']=$data[4];
  $dt['majmuah']=$data[5];
  $dt['med_hotel_no']=$data[6];
  $dt['pax']=$data[7];
  $dt['contract_no']=$data[8];
  $dt['kitchen']=$data[9];
  $dt['qty']=$data[10];
  echo "<hr>".count($data)."<br>";
  echo "<pre>"; print_r($dt); echo "</pre>";
  if(count($data)!=11) {
    echo "jumalh data sebaris salah<br>";
  } else {  
    
    $sql1 = "select * from `berangkat` where code='".$dt['code']."'";
    $res1 = mysqli_query($db, $sql1);
    //echo $sql;
    $r1 = mysqli_fetch_assoc($res1);
    //echo "<pre>"; print_r($r1); echo "</pre>";

    $sql2 = "SELECT * from `kloter_jadwal` where code='".$dt['code']."'";
    $res2 = mysqli_query($db, $sql2);
    //echo $sql;
    $r2 = mysqli_fetch_assoc($res2);
    //echo "<pre>"; print_r($r2); echo "</pre>";


    $udt3=array(
      'in_hotel_date'=>substr($dt['in_time'],0,10),
      'in_hotel_time'=>strtotime($dt['in_time']),
      'out_hotel_date'=>date("Y-m-d",$r2['rpulang_jam']-21600),
      'out_hotel_time'=>$r2['rpulang_jam']-21600,
      'update_at'=>time(),
    );
    Update($db,"penempatan",$udt3,"`daker`=2 and gel=2 and `emb`='".substr($dt['code'],0,3)."' and `klt`='".substr($dt['code'],3)."' and no_rumah='".$dt['med_hotel_no']."'",1);

    $sql0="SELECT * from mykloter where code='".$dt['code']."'";
    $res0=mysqli_query($db,$sql0);
    if(mysqli_num_rows($res0)==0) {
      $sql4 = "select id from `mykloter` order by id desc limit 1";
      $res4 = mysqli_query($db, $sql4);
      $rw4 = mysqli_fetch_assoc($res4);
      $id2=$rw4['id']+1;
      $sql5 = "select id from `myhotel` order by id desc limit 1";
      $res5 = mysqli_query($db, $sql5);
      $r5 = mysqli_fetch_assoc($res5);
      $id5=$r5['id']+1;

      $udt=array(
        'id'=>$id2,
        'code'=>strtoupper($dt['code']),
        'no'=>$dt['no'],
        'gel'=>2,
        'sa_arrival_port'=>'JED',
        'flight_no'=>$r1['flight'],
        'id_takeoff_date'=>date("Y-m-d",$r1['rpergi_jam']),
        'id_takeoff_time'=>$r1['rpergi_jam'],
        'sa_arrival_date'=>date("Y-m-d",$r1['rtiba_jam']),
        'sa_arrival_time'=>$r1['rtiba_jam'],
        'sa_departure_date'=>date("Y-m-d",$r2['rpulang_jam']),
        'sa_departure_time'=>$r2['rpulang_jam'],
        'sa_departure_flight'=>$r2['rpulang_flight'],
        'sa_departure_port'=>$r2['rpulang_port'],
        'in_hotel_date'=>substr($dt['in_time'],0,10),
        'in_hotel_time'=>strtotime($dt['in_time']),
        'out_hotel_date'=>date("Y-m-d",$r2['rpulang_jam']-21600),
        'out_hotel_time'=>$r2['rpulang_jam']-21600,
        'jhi'=>$r1['fjhi'],
        'tphi'=>$r1['ftphi'],
        'tpihi'=>$r1['ftpihi'],
        'tkhi'=>$r1['ftkhi'],
        'ptg'=>$r1['ftphi']+$r1['ftpihi']+$r1['ftkhi'],
        'total'=>$r1['ftotal'],
        'code_qty'=>$dt['qty'],
        'syarikah'=>$ar['emb2syarikah'][substr($dt['code'],0,3)],
        'emb'=>substr(strtoupper($dt['code']),0,3),
        'klt'=>substr($dt['code'],3),
        'daker'=>$_SESSION['user']['daker'],
        'create_by'=>$_SESSION['user']['petugas'],
        'update_by'=>$_SESSION['user']['petugas'],
        'create_at'=>time(),
        'update_at'=>time(),
      );
      Insert($db,"mykloter",$udt,1);
      $idt=array(
        'id'=>$id5,
        'code'=>strtoupper($dt['code']),
        'in_no'=>$dt['no'],
        'gel'=>2,
        'emb'=>substr(strtoupper($dt['code']),0,3),
        'klt'=>substr($dt['code'],3),
        'in_hotel_date'=>substr($dt['in_time'],0,10),
        'in_hotel_time'=>strtotime($dt['in_time']),
        'out_hotel_date'=>date("Y-m-d",$r2['rpulang_jam']-21600),
        'out_hotel_time'=>$r2['rpulang_jam']-21600,
        'jhi'=>$r1['fjhi'],
        'tphi'=>$r1['ftphi'],
        'tpihi'=>$r1['ftpihi'],
        'tkhi'=>$r1['ftkhi'],
        'ptg'=>$r1['ftphi']+$r1['ftpihi']+$r1['ftkhi'],
        'total'=>$r1['ftotal'],
        'code_qty'=>$dt['qty'],
        'majmuah'=>$dt['majmuah'],
        'sektor'=>substr($dt['med_hotel_no'],0,1),
        'hotel_no'=>$dt['med_hotel_no'],
        'contract_no'=>$dt['contract_no'],
        'pax'=>$dt['pax'],
        'kitchen_code'=>$dt['kitchen'],
        'in_bus'=>$dt['bus'],
        'in_bus_no'=>$dt['bus_no'],
        'out_bus'=>$dt['bus'],
        'out_bus_no'=>$dt['bus_no'],
        'daker'=>$_SESSION['user']['daker'],
        'create_by'=>$_SESSION['user']['petugas'],
        'update_by'=>$_SESSION['user']['petugas'],
        'create_at'=>time(),
        'update_at'=>time(),
      );
      Insert($db,"myhotel",$idt,1);
    } else {

      $udt3=array(
        'sa_departure_date'=>date("Y-m-d",$r2['rpulang_jam']),
        'sa_departure_time'=>$r2['rpulang_jam'],
        'sa_departure_flight'=>$r2['rpulang_flight'],
        'sa_departure_port'=>$r2['rpulang_port'],
        'in_hotel_date'=>substr($dt['in_time'],0,10),
        'in_hotel_time'=>strtotime($dt['in_time']),
        'out_hotel_date'=>date("Y-m-d",($r2['rpulang_jam']-21600)),
        'update_by'=>$_SESSION['user']['petugas'],
        'out_hotel_time'=>$r2['rpulang_jam']-21600,
      );
      Update($db,"mykloter",$udt3,"`code`='".$dt['code']."'",1);

      $sql6="SELECT * from myhotel where code='".$dt['code']."' and hotel_no=".$dt['med_hotel_no'];
      $res6=mysqli_query($db,$sql6);
      if(mysqli_num_rows($res6)==0) {

        $sql5 = "select id from `myhotel` order by id desc limit 1";
        $res5 = mysqli_query($db, $sql5);
        $r5 = mysqli_fetch_assoc($res5);
        $id5=$r5['id']+1;
        $idt=array(
          'id'=>$id5,
          'code'=>strtoupper($dt['code']),
          'in_no'=>$dt['no'],
          'gel'=>2,
          'emb'=>substr(strtoupper($dt['code']),0,3),
          'klt'=>substr($dt['code'],3),
          'in_hotel_date'=>substr($dt['in_time'],0,10),
          'in_hotel_time'=>strtotime($dt['in_time']),
          'out_hotel_date'=>date("Y-m-d",$r2['rpulang_jam']-21600),
          'out_hotel_time'=>$r2['rpulang_jam']-21600,
          'jhi'=>$r1['fjhi'],
          'tphi'=>$r1['ftphi'],
          'tpihi'=>$r1['ftpihi'],
          'tkhi'=>$r1['ftkhi'],
          'ptg'=>$r1['ftphi']+$r1['ftpihi']+$r1['ftkhi'],
          'total'=>$r1['ftotal'],
          'code_qty'=>$dt['qty'],
          'majmuah'=>$dt['majmuah'],
          'sektor'=>substr($dt['med_hotel_no'],0,1),
          'hotel_no'=>trim($dt['med_hotel_no']),
          'contract_no'=>trim($dt['contract_no']),
          'pax'=>$dt['pax'],
          'kitchen_code'=>trim($dt['kitchen']),
          'in_bus'=>$dt['bus'],
          'in_bus_no'=>$dt['bus_no'],
          'out_bus'=>$dt['bus'],
          'out_bus_no'=>$dt['bus_no'],
          'daker'=>$_SESSION['user']['daker'],
          'create_by'=>$_SESSION['user']['petugas'],
          'update_by'=>$_SESSION['user']['petugas'],
          'create_at'=>time(),
          'update_at'=>time(),
        );
        Insert($db,"myhotel",$idt,1);
      } else {
        $udt=array(
          'in_no'=>$dt['no'],
          'gel'=>2,
          'emb'=>substr(strtoupper($dt['code']),0,3),
          'klt'=>substr($dt['code'],3),
          'in_hotel_date'=>substr($dt['in_time'],0,10),
          'in_hotel_time'=>strtotime($dt['in_time']),
          'out_hotel_date'=>date("Y-m-d",$r2['rpulang_jam']-21600),
          'out_hotel_time'=>$r2['rpulang_jam']-21600,
          'jhi'=>$r1['fjhi'],
          'tphi'=>$r1['ftphi'],
          'tpihi'=>$r1['ftpihi'],
          'tkhi'=>$r1['ftkhi'],
          'ptg'=>$r1['ftphi']+$r1['ftpihi']+$r1['ftkhi'],
          'total'=>$r1['ftotal'],
          'code_qty'=>$dt['qty'],
          'majmuah'=>$dt['majmuah'],
          'sektor'=>substr($dt['med_hotel_no'],0,1),
          'contract_no'=>trim($dt['contract_no']),
          'pax'=>$dt['pax'],
          'kitchen_code'=>trim($dt['kitchen']),
          'in_bus'=>$dt['bus'],
          'in_bus_no'=>$dt['bus_no'],
          'out_bus'=>$dt['bus'],
          'out_bus_no'=>$dt['bus_no'],
          'update_by'=>$_SESSION['user']['petugas'],
          'update_at'=>time(),
        );
        Update($db,"myhotel",$udt,"`code`='".$dt['code']."' and hotel_no='".$dt['med_hotel_no']."'",1);
      }
    }
  }  
  /* */
}