<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class MtransaksiMitra extends CI_Model{

  /**

  * @author Fendrik Nurul Jadid <fendrik1311@gmail.com>

  * @since v.1.0

  **/


  public function __construct()
  {
    parent::__construct();
    //Codeigniter : Write Less Do More
  }

  public function getMitra($idmitra)
  {
    $this->db->where("idmitra", $idmitra);
    return $this->db->get("mitra");
  }

  public function get_kurir($id_kurir){
    return $this->db
    ->where("idkurir",$id_kurir)
    ->get("kurir")->row();
  }

  public function get_pemesanan($idpemesanan){
    return $this->db
    ->where("idpemesanan",$idpemesanan)
    ->get("pemesanan")->result();
  }
  public function getdata_pemesanan($idpemesanan){
    $this->db->join("kurir", "kurir.idkurir = pemesanan.kurir_idkurir", "left");
    return $this->db
    ->where("idpemesanan",$idpemesanan)
    ->get("pemesanan");
  }

  public function get_riwayat($id_user){
    return $this->db
    ->join("kurir","kurir.idkurir=pemesanan.kurir_idkurir")
    ->where("data_user_iddata_user",$id_user)
    ->order_by("pemesanan.tanggal", "DESC")
    ->get("pemesanan")->result();
  }

  public function get_riwayat_mitra($id, $tgl_awal = null, $tgl_akhir = null){
    if ($tgl_awal!= null || $tgl_akhir != null) {
      $this->db->where('date(tanggal) BETWEEN "'. date('Y-m-d', strtotime($tgl_mulai)). '" and "'. date('Y-m-d', strtotime($tgl_akhir)).'"');
    };
    $this->db->join("kurir","kurir.idkurir=pemesanan.kurir_idkurir");
    $this->db->where("mitra_idmitra",$id);
    $this->db->order_by("pemesanan.tanggal", "DESC");
    return $this->db->get("pemesanan")->result();
  }

  public function get_belum_bayar($id_user){
    return $this->db
    ->join("kurir","kurir.idkurir=pemesanan.kurir_idkurir","left")
    ->where("pemesanan.mitra_idmitra",$id_user)
    ->where("status_pemesanan",1)
    ->order_by("pemesanan.tanggal", "DESC")
    ->get("pemesanan")->result();
  }

  public function get_sudah_bayar($id_user){
    return $this->db
    ->join("kurir","kurir.idkurir=pemesanan.kurir_idkurir","left")
    ->where("pemesanan.mitra_idmitra",$id_user)
    ->where("status_pemesanan",2)
    ->order_by("pemesanan.tanggal", "DESC")
    ->get("pemesanan")->result();
  }

  public function get_proses_kirim($id_user){
    return $this->db
    ->join("kurir","kurir.idkurir=pemesanan.kurir_idkurir")
    ->where("pemesanan.mitra_idmitra",$id_user)
    ->where("status_pemesanan",3)
    ->order_by("pemesanan.tanggal", "DESC")
    ->get("pemesanan")->result();
  }
  public function get_diterima($id_user){
    return $this->db
    ->join("kurir","kurir.idkurir=pemesanan.kurir_idkurir")
    ->where("pemesanan.mitra_idmitra",$id_user)
    ->where("status_pemesanan",4)
    ->order_by("pemesanan.tanggal", "DESC")
    ->get("pemesanan")->result();
  }

  public function get_detail_riwayat($id_pemesanan){
    return $this->db
    // ->select("detail_pemesanan.*,barang.")
    ->join("barang","detail_pemesanan.barang_idbarang=barang.idbarang")
    // ->join("mitra","mitra.idmitra=barang.mitra_idmitra")
    ->where("pemesanan_idpemesanan",$id_pemesanan)
    ->get("detail_pemesanan")->result();
  }
  public function get_detail_riwayat2($id_pemesanan){
    return $this->db
    ->select("pemesanan.*,mitra.*")
    ->join("detail_pemesanan","pemesanan.idpemesanan = detail_pemesanan.pemesanan_idpemesanan")
    ->join("barang","detail_pemesanan.barang_idbarang=barang.idbarang")
    ->join("mitra","mitra.idmitra=barang.mitra_idmitra")
    ->where("idpemesanan",$id_pemesanan)
    ->get("pemesanan")->row();
  }

  public function get_detail_pemesanan($id_pemesanan){
  return $this->db
  ->join("barang","detail_pemesanan.barang_idbarang=barang.idbarang")
  ->join("mitra","mitra.idmitra=barang.mitra_idmitra")
  ->join("ulasan","ulasan.detail_pemesanan_iddetail_pemesanan=detail_pemesanan.iddetail_pemesanan","left")
  ->where("pemesanan_idpemesanan",$id_pemesanan)
  ->get("detail_pemesanan");
}

}
