<?php

class Model_transaksi extends CI_Model
{
   public function proses_pesan($id_user)
   {



      $tglin='';
      $tglout='';

      $pesanan = [
         'id_user' => $id_user,
         'tgl_in' => $tglin,
         'tgl_out' => $tglout,
      ];
      $this->db->insert('tb_pesanan', $pesanan);
      $id_pesanan = $this->db->insert_id();

      foreach ($this->cart->contents() as $item) {
         $tglin=$item['tglin'];
         $tglout=$item['tglout'];
         $data['pesanan'] = [
            'id_pesanan' => $id_pesanan,
            'id_barang' => $item['id'],
            'qty' => $item['qty'],
            'total_bayar' =>  $item['price'],
            'tgl_in' => $item['tglin'],
            'tgl_out' => $item['tglout'],
         ];
         $this->db->insert('tb_pesanan_detail', $data['pesanan']);
         $this->db->insert_id();
      }

       $data1 = array(
            'tgl_in' => $tglin,
            'tgl_out' => $tglout,
         );
      $this->db->update('tb_pesanan', $data1, "id_pesanan = '$id_pesanan'");

      return $data['pesanan'];
   }

   public function get_pesanan_id($last_id)
   {
      $query = "SELECT * FROM `tb_pesanan` JOIN `user` ON `tb_pesanan`.`id_user`=`user`.`id_user` 
                        WHERE `tb_pesanan`.`id_pesanan`=$last_id";

      return $this->db->query($query)->row_array();
   }

   public function get_user_id($last_id)
   {
      $query = "SELECT * FROM user WHERE `id_user`=$last_id";

      return $this->db->query($query)->row_array();
   }

   public function get_barang_detail($last_id)
   {
      $query = "SELECT `tb_pesanan_detail`.*, `area_camping`.* FROM `tb_pesanan_detail` JOIN `area_camping` ON `tb_pesanan_detail`.`id_barang`=`area_camping`.`id_area` WHERE `tb_pesanan_detail`.`id_pesanan`=$last_id";

      return $this->db->query($query)->result_array();
   }

   public function tampil()
   {
      return $this->db->get('pelanggan')->row_array();
   }

   public function invoice()
   {
      return $this->db->get('invoice')->result_array();
   }

   public function barang_invoice($id_invoice)
   {
      // ambil data barang JOIN
      $query = "SELECT `tb_pesanan_detail`.*, `tb_jasa`.`nama_barang` FROM `tb_pesanan_detail` JOIN `tb_jasa` ON `tb_pesanan_detail`.`id_barang`=`tb_jasa`.`id_barang` JOIN `invoice` ON `invoice`.`id_invoice`=`tb_pesanan_detail`.`id_invoice` WHERE `tb_pesanan_detail`.`id_invoice`=$id_invoice";

      return $this->db->query($query)->result_array();
   }

   public function get_detail_pesanan($id_invoice)
   {
      // ambil data pelanggan join
      $query = "SELECT `tb_pesanan`.`nama_penerima`, `tb_pesanan`.`telepon`, `tb_pesanan`.`alamat`, `tb_pesanan`.`provinsi`, 
                        `tb_pesanan`.`distrik`, `tb_pesanan`.`type`, `tb_pesanan`.`kodepos`, `tb_pesanan`.`catatan`, `invoice`.`tanggal_pesan`  
                        FROM `tb_pesanan` JOIN `invoice` ON `invoice`.`id_invoice`=`tb_pesanan`.`id_invoice` 
                        WHERE `invoice`.`id_invoice`=$id_invoice";

      return $this->db->query($query)->row_array();
   }

   public function pendapatan()
   {
      $query = "SELECT SUM(IF(`transaction_status` like 'settlement', gross_amount, 0)) as 'pendapatan'
                  FROM tb_transaksi";
      return $this->db->query($query)->result_array();
   }
}
