<?php

namespace App\Controllers;
use CodeIgniter\Controller;
use App\Models\ChartIncome;
use App\Models\Client;
use CodeIgniter\Files\File;
use Aws\S3\S3Client;
use App\Models\Orders;
use App\Models\OrderItemModel;
use App\Models\Products;
use App\Models\Qcitem;
use App\Models\PaymentMainModel;
use App\Models\PaymentRecordsModel;

class Laporan extends BaseController
{
    protected $helpers = ['form'];
    protected $Orders;
    protected $orderItemModel;

    public function __construct()
    {
        
        $this->Orders = new Orders();
        $this->orderItemModel = new OrderItemModel();
        $this->Qcitem = new Qcitem();
    }

    public function invoice()
    {
        // Mengambil parameter filter dari GET request
        $client = $this->request->getGet('client');
        $product = $this->request->getGet('product');
        $start_date = $this->request->getGet('start_date');
        $end_date = $this->request->getGet('end_date');
        $paid_status = $this->request->getGet('paid_status');
        $status_produksi = $this->request->getGet('status_produksi');

        // Inisiasi query dengan model Orders
        $query = $this->Orders->select('*');

        // Filter berdasarkan client jika ada
        if ($client) {
            $query->where('client_key', $client);
        }

        // Filter berdasarkan paid_status jika ada
        if ($paid_status) {
            $query->where('paid_status', $paid_status);
        }

        // Filter berdasarkan status_produksi jika ada
        if ($status_produksi) {
            $query->where('status_produksi', $status_produksi);
        }

        // Filter berdasarkan range tanggal jika ada
        if ($start_date && $end_date) {
            $query->where('tanggal_order >=', $start_date)
                  ->where('tanggal_order <=', $end_date);
        }

        // Ambil semua hasil dari query
        $orders = $query->findAll();

      
        // Tampilkan hasil dalam format JSON atau kirimkan ke view
        return $this->response->setJSON($orders);
    }
}