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

class Mtransaksi 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 PemesananHari($tgl=null)
  {
    $this->db->select("COUNT(idpemesanan) as jumlah_trans, SUM(total_pembayaran) as total");
    if ($tgl!=null) {
      $this->db->where("tanggal",$tgl);
    }
    $this->db->where("status_pemesanan",3);
    return $this->db->get("pemesanan");
  }

  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_kurir($id_kurir){
    return $this->db
    ->where("idkurir",$id_kurir)
    ->get("kurir")->row();
  }

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

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

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

  public function get_riwayat($id_user, $tgl_awal = null, $tgl_akhir = null, $status_pemesanan = null){
    if ($tgl_awal!= null || $tgl_akhir != null) {
      $this->db->where('date(tanggal) BETWEEN "'. date('Y-m-d', strtotime($tgl_awal)). '" and "'. date('Y-m-d', strtotime($tgl_akhir)).'"');
    };
    if ($status_pemesanan != null) {
      $this->db->where("status_pemesanan", $status_pemesanan);
    }
    return $this->db
    ->join("kurir","kurir.idkurir=pemesanan.kurir_idkurir","LEFT")
    ->where("data_user_iddata_user",$id_user)
    ->get("pemesanan")->result();
  }
  public function get_belum_bayar($id_user=null){
    if ($id_user!=null || $id_user != "") {
      $this->db->where("data_user_iddata_user",$id_user);
    }
    return $this->db
    ->join("kurir","kurir.idkurir=pemesanan.kurir_idkurir","left")
    ->where("status_pemesanan",0)
    ->get("pemesanan")->result();
  }

  public function get_sudah_bayar($id_user=null){
    if ($id_user!=null || $id_user != "") {
      $this->db->where("data_user_iddata_user",$id_user);
    }
    return $this->db
    ->join("kurir","kurir.idkurir=pemesanan.kurir_idkurir")
    ->where("status_pemesanan",1)
    ->get("pemesanan")->result();
  }

  public function get_dikemas($id_user=null){
    if ($id_user!=null || $id_user != "") {
      $this->db->where("data_user_iddata_user",$id_user);
    }
    return $this->db
    ->join("kurir","kurir.idkurir=pemesanan.kurir_idkurir")
    ->where("status_pemesanan",2)
    ->get("pemesanan")->result();
  }
  public function get_proses_kirim($id_user=null){
    if ($id_user!=null || $id_user != "") {
      $this->db->where("data_user_iddata_user",$id_user);
    }
    return $this->db
    ->join("kurir","kurir.idkurir=pemesanan.kurir_idkurir")
    ->where("status_pemesanan",3)
    ->get("pemesanan")->result();
  }
  public function get_diterima($id_user=null){
    if ($id_user!=null || $id_user != "") {
      $this->db->where("data_user_iddata_user",$id_user);
    }
    return $this->db
    ->join("kurir","kurir.idkurir=pemesanan.kurir_idkurir")
    ->where("status_pemesanan",4)
    ->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("kategori","kategori.idkategori = barang.kategori_idkategori")
    ->join("mitra","mitra.idmitra=barang.mitra_idmitra")
    ->where("pemesanan_idpemesanan",$id_pemesanan)
    ->get("detail_pemesanan");
  }

  public function get_laba($id, $tgl_awal = null, $tgl_akhir = null)
  {
    $this->db->select("pemesanan.*, kurir.*, SUM(detail_pemesanan.hpp_pemesanan) as total_hpp");
    $this->db->join("detail_pemesanan","detail_pemesanan.pemesanan_idpemesanan = pemesanan.idpemesanan");
    $this->db->join("kurir","kurir.idkurir=pemesanan.kurir_idkurir");
    $this->db->where("pemesanan.mitra_idmitra",$id);
    if ($tgl_awal!= null || $tgl_akhir != null) {
      $this->db->where('date(tanggal) BETWEEN "'. date('Y-m-d', strtotime($tgl_awal)). '" and "'. date('Y-m-d', strtotime($tgl_akhir)).'"');
    };
    $this->db->order_by("pemesanan.tanggal", "DESC");
    $this->db->group_by("pemesanan.idpemesanan");
    return $this->db->get("pemesanan");
  }

  public function get_laba_kurir($id, $tgl_awal = null, $tgl_akhir = null)
  {
    $this->db->select("pemesanan.*, kurir.*, SUM(pemesanan.biaya_kurir) as total_kurir, SUM(pemesanan.admin_ongkir) as admin_ongkir");
    $this->db->join("kurir","kurir.idkurir=pemesanan.kurir_idkurir");
    $this->db->join("user","user.idkurir = kurir.idkurir");
    $this->db->where("pemesanan.mitra_idmitra",$id);
    if ($tgl_awal!= null || $tgl_akhir != null) {
      $this->db->where('date(tanggal) BETWEEN "'. date('Y-m-d', strtotime($tgl_awal)). '" and "'. date('Y-m-d', strtotime($tgl_akhir)).'"');
    };
    $this->db->order_by("pemesanan.tanggal", "DESC");
    // $this->db->group_by("kurir.idkurir");
    return $this->db->get("pemesanan");
  }

}
