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

class ATK_Pengambilan_model extends CI_Model
{
    private $_table = "atk_pengambilan";


    public function getAll() {
        $this->db->select('atk_pengambilan.*, atk_product.atk_product_nama, divisi_categories.divisi_categories_nama');
        $this->db->from('atk_pengambilan');
        $this->db->join('atk_product', 'atk_pengambilan.atk_pengambilan_product = atk_product.atk_product_id', 'left');
        $this->db->join('divisi_categories', 'atk_pengambilan.atk_pengambilan_divisi = divisi_categories.divisi_categories_id', 'left');
        // $this->db->where('atk_product.atk_product_inventory_types', 9); // Kondisi 1
        $this->db->where('atk_pengambilan.atk_pengambilan_status', 1); // Kondisi 2
        $query = $this->db->get();
        return $query->result();
    }
    
   

    public function jumlah_inventory()
    {
        $query =  $this->db->query(" SELECT COUNT(inventory_id) AS JumlahInventory FROM inventory where inventory_status = '1'");
        return $query->result();
      
    }
   
    public function save()
{
    date_default_timezone_set("Asia/Jakarta");
    $tanggal = date("Y-m-d H:i:s");

    $post = $this->input->post();
    $this->atk_pengambilan_peminjam = $post["atk_pengambilan_peminjam"];
    $this->atk_pengambilan_divisi = $post["atk_pengambilan_divisi"];
    $this->atk_pengambilan_product = $post["atk_pengambilan_product"];
    $this->atk_pengambilan_jumlah = $post["atk_pengambilan_jumlah"];
    $this->atk_pengambilan_status = "1";
    $this->created_at =  $tanggal;

    // Kurangi stok di tabel atk_product
    $product_id = $post["atk_pengambilan_product"];
    $jumlah_pengambilan = $post["atk_pengambilan_jumlah"];

    // Ambil stok saat ini
    $this->db->select('atk_product_stok');
    $this->db->from('atk_product');
    $this->db->where('atk_product_id', $product_id);
    $current_stok = $this->db->get()->row()->atk_product_stok;

    // Hitung stok baru
    $new_stok = $current_stok - $jumlah_pengambilan;

    // Update stok di tabel atk_product
    $this->db->set('atk_product_stok', $new_stok);
    $this->db->where('atk_product_id', $product_id);
    $this->db->update('atk_product');

    // Simpan data pengambilan
    return $this->db->insert($this->_table, $this);
}

  
    // public function save()
    // {
    //     date_default_timezone_set("Asia/Jakarta");
    //     $tanggal = date("Y-m-d H:i:s");

    //     $post = $this->input->post();
    //     // $this->crew_id = $post["crew_id"];
    //     $this->atk_pengambilan_peminjam = $post["atk_pengambilan_peminjam"];
    //     $this->atk_pengambilan_divisi = $post["atk_pengambilan_divisi"];
    //     $this->atk_pengambilan_product = $post["atk_pengambilan_product"];
    //     $this->atk_pengambilan_jumlah = $post["atk_pengambilan_jumlah"];
    //     $this->atk_pengambilan_status = "1";
    //     $this->created_at =  $tanggal;
    //     return $this->db->insert($this->_table, $this);
    // }
    public function view_update($where, $table){
        return $this->db->get_where($table, $where);
    }

    public function update($where, $data, $table){
        $this->db->where($where);
        $this->db->update($table, $data);
    }

    public function delete($where, $table){
        $this->db->where($where);
        $this->db->delete($table);
    }
}
