ReportManageController

 avatar
FIKRI
php_laravel_blade
2 years ago
116 kB
2
Indexable
<?php

namespace App\Http\Controllers;

use PDF;
use Auth;
use Carbon\Carbon;
use App\User;
use App\Acces;
use App\Market;
use App\Supply;
use App\Transaction;
use App\Customer;
use App\Exports\ExportLaporanPembelian;
use App\Exports\ExportLaporanPenjualan;
use App\Exports\ExportLaporanReturPembelian;
use App\Exports\ExportLaporanReturPenjualan;
use App\Exports\ExportTransaction;
use App\Product;
use App\ReturSupplier;
use App\Supplier;
use App\TransactionNonPPN;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class ReportManageController extends Controller
{
    // Show View Report Transaction
    public function reportTransaction()
    {
        $id_account = Auth::id();
        $check_access = Acces::where('user', $id_account)
        ->first();
        if($check_access->kelola_laporan == 1){
        	$transactions = Transaction::whereDate('created_at', Carbon::today())->get();
            $array = array();
            foreach ($transactions as $no => $transaction) {
                array_push($array, $transactions[$no]->created_at->toDateString());
            }
            $dates = array_unique($array);
            rsort($dates);

            $arr_ammount = count($dates);
            $incomes_data = array();
            if($arr_ammount > 7){
            	for ($i = 0; $i < 7; $i++) { 
            		array_push($incomes_data, $dates[$i]);	
            	}
            }elseif($arr_ammount > 0){
            	for ($i = 0; $i < $arr_ammount; $i++) { 
            		array_push($incomes_data, $dates[$i]);
            	}
            }
            $incomes = array_reverse($incomes_data);

        	return view('report.report_transaction', compact('dates', 'incomes'));
        }
        else{
            return back();
        }
    }

    // Show View Report Worker
    public function reportWorker()
    {
        $id_account = Auth::id();
        $check_access = Acces::where('user', $id_account)
        ->first();
        if($check_access->kelola_laporan == 1){
            $users = User::all();

            return view('report.report_worker', compact('users'));
        }else{
            return back();
        }
    }

    // Filter Report Transaction
    public function filterTransaction(Request $req)
    {
        $id_account = Auth::id();
        $check_access = Acces::where('user', $id_account)
        ->first();
        if($check_access->kelola_laporan == 1){
            // date
            if($req->tgl_awal == null){
                $start_date = date("d-m-Y");
            }
            else{
                $start_date = $req->tgl_awal;
            }
            if($req->tgl_akhir == null){
                $end_date = date('d-m-Y');
            }
            else{
                $end_date = $req->tgl_akhir;
            }
        	$start_date2 = $start_date[6].$start_date[7].$start_date[8].$start_date[9].'-'.$start_date[3].$start_date[4].'-'.$start_date[0].$start_date[1].' 00:00:00';
        	$end_date2 = $end_date[6].$end_date[7].$end_date[8].$end_date[9].'-'.$end_date[3].$end_date[4].'-'.$end_date[0].$end_date[1].' 23:59:59';
        	$supplies = Transaction::select()
        	->whereBetween('created_at', array($start_date2, $end_date2))
        	->get();
            $array = array();
            foreach ($supplies as $no => $supply) {
                array_push($array, $supplies[$no]->created_at->toDateString());
            }
            $dates = array_unique($array);
            rsort($dates);

            // id transaksi
            $kode_transaksi = $req->search;

            // nama customer
            $customer = Customer::select('id')
            ->where('nama', $req->customer)
            ->first();

            // ppn non-ppn
            $status = $req->status;

        	return view('report.report_transaction_filter', compact('dates', 'kode_transaksi', 'customer', 'status'));
        }else{
            return back();
        }
    }

    // Filter Report Worker
    public function filterWorker($id)
    {
        $id_account = Auth::id();
        $check_access = Acces::where('user', $id_account)
        ->first();
        if($check_access->kelola_laporan == 1){
            $users = User::orderBy($id, 'asc')
            ->get();
            
            return view('report.filter_table.filter_table_worker', compact('users'));
        }else{
            return back();
        }
    }

    // Filter Chart Transaction
    public function chartTransaction($id)
    {
        $id_account = Auth::id();
        $check_access = Acces::where('user', $id_account)
        ->first();
        if($check_access->kelola_laporan == 1){
        	$supplies = Transaction::all();
            $array = array();
            foreach ($supplies as $no => $supply) {
                array_push($array, $supplies[$no]->created_at->toDateString());
            }
            $dates = array_unique($array);
            rsort($dates);
            $arr_ammount = count($dates);
            $incomes_data = array();

        	if($id == 'minggu'){
        		if($arr_ammount > 7){
    	        	for ($i = 0; $i < 7; $i++) { 
    	        		array_push($incomes_data, $dates[$i]);	
    	        	}
    	        }elseif($arr_ammount > 0){
    	        	for ($i = 0; $i < $arr_ammount; $i++) { 
    	        		array_push($incomes_data, $dates[$i]);
    	        	}
    	        }
    	        $incomes = array_reverse($incomes_data);
    	        $total = array();
    	        foreach ($incomes as $no => $income) {
    	        	array_push($total, Transaction::whereDate('created_at', $income)->sum('total'));
    	        }

    	        return response()->json([
    	        	'incomes' => $incomes, 
    	        	'total' => $total
    	        ]);
        	}elseif($id == 'bulan'){
        		if($arr_ammount > 30){
    	        	for ($i = 0; $i < 30; $i++) { 
    	        		array_push($incomes_data, $dates[$i]);	
    	        	}
    	        }elseif($arr_ammount > 0){
    	        	for ($i = 0; $i < $arr_ammount; $i++) { 
    	        		array_push($incomes_data, $dates[$i]);
    	        	}
    	        }
    	        $incomes = array_reverse($incomes_data);
    	        $total = array();
    	        foreach ($incomes as $no => $income) {
    	        	array_push($total, Transaction::whereDate('created_at', $income)->sum('total'));
    	        }

    	        return response()->json([
    	        	'incomes' => $incomes, 
    	        	'total' => $total
    	        ]);
        	}elseif($id == 'tahun'){
        		if($arr_ammount > 365){
    	        	for ($i = 0; $i < 365; $i++) { 
    	        		array_push($incomes_data, $dates[$i]);	
    	        	}
    	        }elseif($arr_ammount > 0){
    	        	for ($i = 0; $i < $arr_ammount; $i++) { 
    	        		array_push($incomes_data, $dates[$i]);
    	        	}
    	        }
    	        $incomes = array_reverse($incomes_data);
    	        $total = array();
    	        foreach ($incomes as $no => $income) {
    	        	array_push($total, Transaction::whereDate('created_at', $income)->sum('total'));
    	        }

    	        return response()->json([
    	        	'incomes' => $incomes, 
    	        	'total' => $total
    	        ]);
        	}
        }else{
            return back();
        }
    }

    // Detail Report Worker
    public function detailWorker($id)
    {
        $id_account = Auth::id();
        $check_access = Acces::where('user', $id_account)
        ->first();
        if($check_access->kelola_laporan == 1){
            $worker = User::find($id);
            $supplies = Supply::where('id_worker', $id)
            ->whereMonth('created_at', now()->month)
            ->whereYear('created_at', now()->year)
            ->get();
            $array_1 = array();
            foreach ($supplies as $no => $supply) {
                array_push($array_1, $supplies[$no]->created_at->toDateString());
            }
            $dates_1 = array_unique($array_1);
            rsort($dates_1);

            $transactions = Transaction::where('id_kasir', $id)
            ->whereMonth('created_at', now()->month)
            ->whereYear('created_at', now()->year)
            ->get();
            $array_2 = array();
            foreach ($transactions as $no => $transaction) {
                array_push($array_2, $transactions[$no]->created_at->toDateString());
            }
            $dates_2 = array_unique($array_2);
            rsort($dates_2);

            return view('report.detail_report_worker', compact('worker', 'dates_1', 'dates_2'));
        }else{
            return back();
        }
    }

    // Export Transaction Report
    public function exportTransaction(Request $req)
    {
        $id_account = Auth::id();
        $check_access = Acces::where('user', $id_account)
        ->first();
        if($check_access->kelola_laporan == 1){
            $jenis_laporan = $req->jns_laporan;
            $current_time = Carbon::now()->isoFormat('Y-MM-DD') . ' 23:59:59';
            if($jenis_laporan == 'period'){
                if($req->period == 'minggu'){
                    $last_time = Carbon::now()->subWeeks($req->time)->isoFormat('Y-MM-DD') . ' 00:00:00';
                    $transactions = Transaction::select('transactions.*')
                    ->whereBetween('created_at', array($last_time, $current_time))
                    ->get();
                    $array = array();
                    foreach ($transactions as $no => $transaction) {
                        array_push($array, $transactions[$no]->created_at->toDateString());
                    }
                    $dates = array_unique($array);
                    rsort($dates);
                    $tgl_awal = $last_time;
                    $tgl_akhir = $current_time;
                }elseif($req->period == 'bulan'){
                    $last_time = Carbon::now()->subMonths($req->time)->isoFormat('Y-MM-DD') . ' 00:00:00';
                    $transactions = Transaction::select('transactions.*')
                    ->whereBetween('created_at', array($last_time, $current_time))
                    ->get();
                    $array = array();
                    foreach ($transactions as $no => $transaction) {
                        array_push($array, $transactions[$no]->created_at->toDateString());
                    }
                    $dates = array_unique($array);
                    rsort($dates);
                    $tgl_awal = $last_time;
                    $tgl_akhir = $current_time;
                }elseif($req->period == 'tahun'){
                    $last_time = Carbon::now()->subYears($req->time)->isoFormat('Y-MM-DD') . ' 00:00:00';
                    $transactions = Transaction::select('transactions.*')
                    ->whereBetween('created_at', array($last_time, $current_time))
                    ->get();
                    $array = array();
                    foreach ($transactions as $no => $transaction) {
                        array_push($array, $transactions[$no]->created_at->toDateString());
                    }
                    $dates = array_unique($array);
                    rsort($dates);
                    $tgl_awal = $last_time;
                    $tgl_akhir = $current_time;
                }
            }else{
                $start_date = $req->tgl_awal_export;
                $end_date = $req->tgl_akhir_export;
                $start_date2 = $start_date[6].$start_date[7].$start_date[8].$start_date[9].'-'.$start_date[3].$start_date[4].'-'.$start_date[0].$start_date[1].' 00:00:00';
                $end_date2 = $end_date[6].$end_date[7].$end_date[8].$end_date[9].'-'.$end_date[3].$end_date[4].'-'.$end_date[0].$end_date[1].' 23:59:59';
                $transactions = Transaction::select('transactions.*')
                ->whereBetween('created_at', array($start_date2, $end_date2))
                ->get();
                $array = array();
                foreach ($transactions as $no => $transaction) {
                    array_push($array, $transactions[$no]->created_at->toDateString());
                }
                $dates = array_unique($array);
                rsort($dates);
                $tgl_awal = $start_date2;
                $tgl_akhir = $end_date2;
            }
            $market = Market::first();

            $pdf = PDF::loadview('report.export_report_transaction', compact('dates', 'tgl_awal', 'tgl_akhir', 'market'));
            return $pdf->stream();
        }else{
            return back();
        }
    }

    public function exportExcelTransaction(Request $req)
    {
        $id_account = Auth::id();
        $check_access = Acces::where('user', $id_account)
        ->first();
        if($check_access->kelola_laporan == 1){
            $fromInt = (int) date('Ymd', strtotime($req->from));
            $toInt = (int) date('Ymd', strtotime($req->to));
            
            if($fromInt > $toInt){
                return back()->with('error', 'Tanggal awal tidak boleh lebih besar dari tanggal akhir');
            }else{
                return Excel::download(new ExportTransaction($req->from,$req->to), 'Transaction-'.now().'.xlsx');
            }
            
        }else{
            return back();
        }
    }

    // Export Worker Report
    public function exportWorker(Request $req, $id)
    {
        $id_account = Auth::id();
        $check_access = Acces::where('user', $id_account)
        ->first();
        if($check_access->kelola_laporan == 1){
            $jml_laporan = count($req->laporan);

            $jenis_laporan = $req->jns_laporan;
            $current_time = Carbon::now()->isoFormat('Y-MM-DD') . ' 23:59:59';
            if($jenis_laporan == 'period'){
                if($req->period == 'minggu'){
                    $last_time = Carbon::now()->subWeeks($req->time)->isoFormat('Y-MM-DD') . ' 00:00:00';
                    if(count($req->laporan) == 2){
                        $transactions = Transaction::select('transactions.*')
                        ->where('id_kasir', $id)
                        ->whereBetween('created_at', array($last_time, $current_time))
                        ->get();
                        $array = array();
                        foreach ($transactions as $no => $transaction) {
                            array_push($array, $transactions[$no]->created_at->toDateString());
                        }
                        $transaksi = array_unique($array);
                        rsort($transaksi);
                        $supplies = Supply::select('supplies.*')
                        ->where('id_worker', $id)
                        ->whereBetween('created_at', array($last_time, $current_time))
                        ->get();
                        $array = array();
                        foreach ($supplies as $no => $supply) {
                            array_push($array, $supplies[$no]->created_at->toDateString());
                        }
                        $pasok = array_unique($array);
                        rsort($pasok);
                    }elseif($req->laporan[0] == 'pasok'){
                        $transaksi = '';
                        $supplies = Supply::select('supplies.*')
                        ->where('id_worker', $id)
                        ->whereBetween('created_at', array($last_time, $current_time))
                        ->get();
                        $array = array();
                        foreach ($supplies as $no => $supply) {
                            array_push($array, $supplies[$no]->created_at->toDateString());
                        }
                        $pasok = array_unique($array);
                        rsort($pasok);
                    }elseif($req->laporan[0] == 'transaksi'){
                        $transactions = Transaction::select('transactions.*')
                        ->where('id_kasir', $id)
                        ->whereBetween('created_at', array($last_time, $current_time))
                        ->get();
                        $array = array();
                        foreach ($transactions as $no => $transaction) {
                            array_push($array, $transactions[$no]->created_at->toDateString());
                        }
                        $transaksi = array_unique($array);
                        rsort($transaksi);
                        $pasok = '';
                    }
                    $tgl_awal = $last_time;
                    $tgl_akhir = $current_time;
                }elseif($req->period == 'bulan'){
                    $last_time = Carbon::now()->subMonths($req->time)->isoFormat('Y-MM-DD') . ' 00:00:00';
                    if(count($req->laporan) == 2){
                        $transactions = Transaction::select('transactions.*')
                        ->where('id_kasir', $id)
                        ->whereBetween('created_at', array($last_time, $current_time))
                        ->get();
                        $array = array();
                        foreach ($transactions as $no => $transaction) {
                            array_push($array, $transactions[$no]->created_at->toDateString());
                        }
                        $transaksi = array_unique($array);
                        rsort($transaksi);
                        $supplies = Supply::select('supplies.*')
                        ->where('id_worker', $id)
                        ->whereBetween('created_at', array($last_time, $current_time))
                        ->get();
                        $array = array();
                        foreach ($supplies as $no => $supply) {
                            array_push($array, $supplies[$no]->created_at->toDateString());
                        }
                        $pasok = array_unique($array);
                        rsort($pasok);
                    }elseif($req->laporan[0] == 'pasok'){
                        $transaksi = '';
                        $supplies = Supply::select('supplies.*')
                        ->where('id_worker', $id)
                        ->whereBetween('created_at', array($last_time, $current_time))
                        ->get();
                        $array = array();
                        foreach ($supplies as $no => $supply) {
                            array_push($array, $supplies[$no]->created_at->toDateString());
                        }
                        $pasok = array_unique($array);
                        rsort($pasok);
                    }elseif($req->laporan[0] == 'transaksi'){
                        $transactions = Transaction::select('transactions.*')
                        ->where('id_kasir', $id)
                        ->whereBetween('created_at', array($last_time, $current_time))
                        ->get();
                        $array = array();
                        foreach ($transactions as $no => $transaction) {
                            array_push($array, $transactions[$no]->created_at->toDateString());
                        }
                        $transaksi = array_unique($array);
                        rsort($transaksi);
                        $pasok = '';
                    }
                    $tgl_awal = $last_time;
                    $tgl_akhir = $current_time;
                }elseif($req->period == 'tahun'){
                    $last_time = Carbon::now()->subYears($req->time)->isoFormat('Y-MM-DD') . ' 00:00:00';
                    if(count($req->laporan) == 2){
                        $transactions = Transaction::select('transactions.*')
                        ->where('id_kasir', $id)
                        ->whereBetween('created_at', array($last_time, $current_time))
                        ->get();
                        $array = array();
                        foreach ($transactions as $no => $transaction) {
                            array_push($array, $transactions[$no]->created_at->toDateString());
                        }
                        $transaksi = array_unique($array);
                        rsort($transaksi);
                        $supplies = Supply::select('supplies.*')
                        ->where('id_worker', $id)
                        ->whereBetween('created_at', array($last_time, $current_time))
                        ->get();
                        $array = array();
                        foreach ($supplies as $no => $supply) {
                            array_push($array, $supplies[$no]->created_at->toDateString());
                        }
                        $pasok = array_unique($array);
                        rsort($pasok);
                    }elseif($req->laporan[0] == 'pasok'){
                        $transaksi = '';
                        $supplies = Supply::select('supplies.*')
                        ->where('id_worker', $id)
                        ->whereBetween('created_at', array($last_time, $current_time))
                        ->get();
                        $array = array();
                        foreach ($supplies as $no => $supply) {
                            array_push($array, $supplies[$no]->created_at->toDateString());
                        }
                        $pasok = array_unique($array);
                        rsort($pasok);
                    }elseif($req->laporan[0] == 'transaksi'){
                        $transactions = Transaction::select('transactions.*')
                        ->where('id_kasir', $id)
                        ->whereBetween('created_at', array($last_time, $current_time))
                        ->get();
                        $array = array();
                        foreach ($transactions as $no => $transaction) {
                            array_push($array, $transactions[$no]->created_at->toDateString());
                        }
                        $transaksi = array_unique($array);
                        rsort($transaksi);
                        $pasok = '';
                    }
                    $tgl_awal = $last_time;
                    $tgl_akhir = $current_time;
                }
            }else{
                $start_date = $req->tgl_awal_export;
                $end_date = $req->tgl_akhir_export;
                $start_date2 = $start_date[6].$start_date[7].$start_date[8].$start_date[9].'-'.$start_date[3].$start_date[4].'-'.$start_date[0].$start_date[1].' 00:00:00';
                $end_date2 = $end_date[6].$end_date[7].$end_date[8].$end_date[9].'-'.$end_date[3].$end_date[4].'-'.$end_date[0].$end_date[1].' 23:59:59';
                if(count($req->laporan) == 2){
                    $transactions = Transaction::select('transactions.*')
                    ->where('id_kasir', $id)
                    ->whereBetween('created_at', array($start_date2, $end_date2))
                    ->get();
                    $array = array();
                    foreach ($transactions as $no => $transaction) {
                        array_push($array, $transactions[$no]->created_at->toDateString());
                    }
                    $transaksi = array_unique($array);
                    rsort($transaksi);
                    $supplies = Supply::select('supplies.*')
                    ->where('id_worker', $id)
                    ->whereBetween('created_at', array($start_date2, $end_date2))
                    ->get();
                    $array = array();
                    foreach ($supplies as $no => $supply) {
                        array_push($array, $supplies[$no]->created_at->toDateString());
                    }
                    $pasok = array_unique($array);
                    rsort($pasok);
                }elseif($req->laporan[0] == 'pasok'){
                    $transaksi = '';
                    $supplies = Supply::select('supplies.*')
                    ->where('id_worker', $id)
                    ->whereBetween('created_at', array($start_date2, $end_date2))
                    ->get();
                    $array = array();
                    foreach ($supplies as $no => $supply) {
                        array_push($array, $supplies[$no]->created_at->toDateString());
                    }
                    $pasok = array_unique($array);
                    rsort($pasok);
                }elseif($req->laporan[0] == 'transaksi'){
                    $transactions = Transaction::select('transactions.*')
                    ->where('id_kasir', $id)
                    ->whereBetween('created_at', array($start_date2, $end_date2))
                    ->get();
                    $array = array();
                    foreach ($transactions as $no => $transaction) {
                        array_push($array, $transactions[$no]->created_at->toDateString());
                    }
                    $transaksi = array_unique($array);
                    rsort($transaksi);
                    $pasok = '';
                }
                $tgl_awal = $start_date2;
                $tgl_akhir = $end_date2;
            }
            $jml_act_pasok = Supply::where('id_worker', $id)
            ->count();
            $jml_act_trans = Transaction::where('id_kasir', $id)
            ->count();
            $market = Market::first();

            $pdf = PDF::loadview('report.export_report_worker', compact('transaksi', 'pasok', 'tgl_awal', 'tgl_akhir', 'id', 'jml_act_pasok', 'jml_act_trans', 'market'));
            return $pdf->stream();
        }else{
            return back();
        }
    }

    public function reportPenjualan()
    {
        return view('report.report_penjualan');
    }

    public function getPenjualan()
    {
        $transaction = Transaction::whereDate('created_at', Carbon::today())
        ->select('transactions.kode_transaksi')
        ->where('status', 'lunas')
        ->orderBy('created_at', 'desc')
        ->get();
        $transactionNonPPN = TransactionNonPPN::whereDate('created_at',Carbon::today())
        ->select('transaction_non_ppn.kode_transaksi')
        ->where('status', 'lunas')
        ->orderBy('created_at', 'desc')
        ->get();
        $transaction = $transaction->concat($transactionNonPPN);
        $jumlah = count($transaction);
        // ===============================================================================
        $array = [];
        foreach ($transaction as $no => $trans) {
            array_push($array, $trans->kode_transaksi);
        }
        $uniq = array_unique($array);
        $penjualan = array_values($uniq);
        // ===============================================================================

        $data_tr = Transaction::whereDate('created_at', Carbon::today())
        ->where('status', 'lunas')
        ->orderBy('created_at', 'desc')
        ->get();
        $data_trr = TransactionNonPPN::whereDate('created_at',Carbon::today())
        ->where('status', 'lunas')
        ->orderBy('created_at', 'desc')
        ->get();
        $transaksi = $data_tr->merge($data_trr);
        $transaksi = $transaksi->sortBy('created_at');
        $transaksi = $transaksi->values()->all();

        // ===================================== Jumlah Stok ==========================================

        $dos = 0;
        $pack = 0;
        foreach ($transaksi as $key => $value) {
            $pp = Product::where('kode_barang', $value->kode_barang)->first();
            if(strtolower($value->jenis_kemasan) == 'pack'){
                $pack += $value->jumlah;
            }else{
                $dos +=  $value->jumlah / $pp->pack;
            }
        }
        $jumlah_dos = (int) $dos ;
        $jumlah_pack = (int) $pack ;

        // ==================================== ganti data transaksi ===========================================

        array_walk($transaksi, function(&$item, $key) {
            $item->kode_transaksi = $item->kode_transaksi.''.$item->luar_pulau;
            $product = Product::where('kode_barang', $item->kode_barang)->first();
            if(strtolower($item->jenis_kemasan) == 'pack'){
                $item->jumlah = $item->jumlah .' '. $item->jenis_kemasan;
            }else{
                $item->jumlah = (int) $item->jumlah / $product->pack .' '. $item->jenis_kemasan;
            }
            $item->total_barang = number_format($item->total_barang, 0, ',', '.');
            $item->id_customer = (Customer::find($item->id_customer) != null) ? Customer::find($item->id_customer)->nama : 'Customer dihapus';
            $item->created_at = $item->created_at->format('d-m-Y H:i:s');
        });
        // ===============================================================================

        $pelanggan = [];
        $jumlah_pelanggan = 0;
        foreach ($transaksi as $key => $value) {
            if(!in_array($value->id_customer, $pelanggan)){
                array_push($pelanggan, $value->id_customer);
                $jumlah_pelanggan++;
            } 
        }

        // ===============================================================================

        //========================== jumlah total harga =============================//

        $hppn = Transaction::whereDate('created_at', Carbon::today())
        ->where('status', 'lunas')
        ->orderBy('created_at', 'desc')
        ->sum('total_barang');
        $hnonppn = TransactionNonPPN::whereDate('created_at',Carbon::today())
        ->where('status', 'lunas')
        ->orderBy('created_at', 'desc')
        ->sum('total_barang');

        $total_harga = $hppn + $hnonppn;
        //=====================================================================//
            
        $tgl = Carbon::parse(now())->isoFormat('D MMMM Y');

        return request()->json(200, [
            'tgl' => $tgl,
            'transaksi' => $transaksi,
            'jumlah_dos' => $jumlah_dos,
            'jumlah_pack' => $jumlah_pack,
            'jumlah' => $jumlah,
            'penjualan' => $penjualan,
            'pelanggan' => $pelanggan,
            'jumlah_pelanggan' => $jumlah_pelanggan,
            'jumlah_harga_total' => number_format($total_harga,2,',','.'),
        ]);
    }

    public function getPenjualanFilter(Request $req)
    {
        // $start_date = $req->tgl_awal;
        // $end_date = $req->tgl_akhir;
        // $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
        // $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));

        $search_penjualan = $req->cari_penjualan;

        if($req->tgl_awal != null || $req->tgl_akhir != null){
            $start_date = $req->tgl_awal;
            $end_date = $req->tgl_akhir;
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }else{
            $get_start1 = Transaction::first();
            $get_start2 = TransactionNonPPN::first();
            if ($get_start1->created_at < $get_start2->created_at) {
                $get_start = $get_start1->created_at;
            }else{
                $get_start = $get_start2->created_at;
            }

            $get_end1 = Transaction::latest()->first();
            $get_end2 = TransactionNonPPN::latest()->first();
            if ($get_end1->created_at > $get_end2->created_at) {
                $get_end = $get_end1->created_at;
            }else{
                $get_end = $get_end2->created_at;
            }

            $start_date = $get_start->format('Y-m-d');
            $end_date = $get_end->format('Y-m-d');
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }

        $transaction = Transaction::whereBetween('created_at', [$start_date2, $end_date2])
        ->select('transactions.kode_transaksi')
        ->where('status', 'lunas')
        ->where(function ($query) use($search_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_penjualan){
                    $query->where('nama', 'like', '%'.$search_penjualan.'%');
                });
        })
        ->orderBy('created_at', 'desc')
        ->get();
        $transactionNonPPN = TransactionNonPPN::whereBetween('created_at', [$start_date2, $end_date2])
        ->select('transaction_non_ppn.kode_transaksi')
        ->where('status', 'lunas')
        ->where(function ($query) use($search_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_penjualan){
                    $query->where('nama', 'like', '%'.$search_penjualan.'%');
                });
        })
        ->orderBy('created_at', 'desc')
        ->get();
        $transaction = $transaction->concat($transactionNonPPN);
        $jumlah = count($transaction);
        // ===============================================================================
        $array = [];
        foreach ($transaction as $no => $trans) {
            array_push($array, $trans->kode_transaksi);
        }
        $uniq = array_unique($array);
        $penjualan = array_values($uniq);
        // ===============================================================================

        $data_tr = Transaction::whereBetween('created_at', [$start_date2, $end_date2])
        ->where('status', 'lunas')
        ->orderBy('created_at', 'desc')
        ->where(function ($query) use($search_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_penjualan){
                    $query->where('nama', 'like', '%'.$search_penjualan.'%');
                });
        })
        ->get();
        $data_trr = TransactionNonPPN::whereBetween('created_at',[$start_date2, $end_date2])
        ->where('status', 'lunas')
        ->orderBy('created_at', 'desc')
        ->where(function ($query) use($search_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_penjualan){
                    $query->where('nama', 'like', '%'.$search_penjualan.'%');
                });
        })
        ->get();
        $transaksi = $data_tr->merge($data_trr);
        $transaksi = $transaksi->sortBy('created_at');
        $transaksi = $transaksi->values()->all();

        // ===================================== Jumlah Stok ==========================================

        $dos = 0;
        $pack = 0;
        $total_harga = 0;
        foreach ($transaksi as $key => $value) {
            $pp = Product::where('kode_barang', $value->kode_barang)->first();
            if(strtolower($value->jenis_kemasan) == 'pack'){
                $pack += $value->jumlah;
            }else{
                $dos +=  $value->jumlah / $pp->pack;
            }

            $total_harga += $value->total_barang;
        }
        $jumlah_dos = (int) $dos ;
        $jumlah_pack = (int) $pack ;

        // ==================================== ganti data transaksi ===========================================

        array_walk($transaksi, function(&$item, $key) {
            $item->kode_transaksi = $item->kode_transaksi.''.$item->luar_pulau;
            $product = Product::where('kode_barang', $item->kode_barang)->first();
            if(strtolower($item->jenis_kemasan) == 'pack'){
                $item->jumlah = $item->jumlah .' '. $item->jenis_kemasan;
            }else{
                $item->jumlah = (int) $item->jumlah / $product->pack .' '. $item->jenis_kemasan;
            }
            $item->total_barang = number_format($item->total_barang, 0, ',', '.');
            $item->id_customer = (Customer::find($item->id_customer) != null) ? Customer::find($item->id_customer)->nama : 'Customer dihapus';
            $item->created_at = $item->created_at->format('d-m-Y H:i:s');
        });
        // ===============================================================================

        $pelanggan = [];
        $jumlah_pelanggan = 0;
        foreach ($transaksi as $key => $value) {
            if(!in_array($value->id_customer, $pelanggan)){
                array_push($pelanggan, $value->id_customer);
                $jumlah_pelanggan++;
            } 
        }

        // ===============================================================================

        $tgl1 = Carbon::parse($start_date)->isoFormat('D MMMM Y');
        $tgl2 = Carbon::parse($end_date)->isoFormat('D MMMM Y');
        $tgl = $tgl1.' s/d '.$tgl2;

        return request()->json(200, [
            'tgl' => $tgl,
            'transaksi' => $transaksi,
            'jumlah_dos' => $jumlah_dos,
            'jumlah_pack' => $jumlah_pack,
            'jumlah' => $jumlah,
            'penjualan' => $penjualan,
            'pelanggan' => $pelanggan,
            'jumlah_pelanggan' => $jumlah_pelanggan,
            'jumlah_harga_total' => number_format($total_harga,2,',','.'),
        ]);
    }

    public function exportPenjualan(Request $req)
    {
        // $start_date = $req->tgl_awal;
        // $end_date = $req->tgl_akhir;
        // $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
        // $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));

        $search_penjualan = $req->cari_penjualan;

        if($req->tgl_awal != null || $req->tgl_akhir != null){
            $start_date = $req->tgl_awal;
            $end_date = $req->tgl_akhir;
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }else{
            $get_start1 = Transaction::first();
            $get_start2 = TransactionNonPPN::first();
            if ($get_start1->created_at < $get_start2->created_at) {
                $get_start = $get_start1->created_at;
            }else{
                $get_start = $get_start2->created_at;
            }

            $get_end1 = Transaction::latest()->first();
            $get_end2 = TransactionNonPPN::latest()->first();
            if ($get_end1->created_at > $get_end2->created_at) {
                $get_end = $get_end1->created_at;
            }else{
                $get_end = $get_end2->created_at;
            }

            $start_date = $get_start->format('Y-m-d');
            $end_date = $get_end->format('Y-m-d');
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }

        $transaction = Transaction::whereBetween('created_at', [$start_date2, $end_date2])
        ->select('transactions.kode_transaksi')
        ->where('status', 'lunas')
        ->where(function ($query) use($search_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_penjualan){
                    $query->where('nama', 'like', '%'.$search_penjualan.'%');
                });
        })
        ->orderBy('created_at', 'desc')
        ->get();
        $transactionNonPPN = TransactionNonPPN::whereBetween('created_at', [$start_date2, $end_date2])
        ->select('transaction_non_ppn.kode_transaksi')
        ->where('status', 'lunas')
        ->where(function ($query) use($search_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_penjualan){
                    $query->where('nama', 'like', '%'.$search_penjualan.'%');
                });
        })
        ->orderBy('created_at', 'desc')
        ->get();
        $transaction = $transaction->concat($transactionNonPPN);
        $jumlah = count($transaction);
        // ===============================================================================
        $array = [];
        foreach ($transaction as $no => $trans) {
            array_push($array, $trans->kode_transaksi);
        }
        $uniq = array_unique($array);
        $penjualan = array_values($uniq);
        // ===============================================================================

        $data_tr = Transaction::whereBetween('created_at', [$start_date2, $end_date2])
        ->where('status', 'lunas')
        ->orderBy('created_at', 'desc')
        ->where(function ($query) use($search_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_penjualan){
                    $query->where('nama', 'like', '%'.$search_penjualan.'%');
                });
        })
        ->get();
        $data_trr = TransactionNonPPN::whereBetween('created_at',[$start_date2, $end_date2])
        ->where('status', 'lunas')
        ->orderBy('created_at', 'desc')
        ->where(function ($query) use($search_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_penjualan){
                    $query->where('nama', 'like', '%'.$search_penjualan.'%');
                });
        })
        ->get();
        $transaksi = $data_tr->merge($data_trr);
        $transaksi = $transaksi->sortBy('created_at');
        $transaksi = $transaksi->values()->all();

        // ===================================== Jumlah Stok ==========================================

        $dos = 0;
        $pack = 0;
        $total_harga = 0;
        foreach ($transaksi as $key => $value) {
            $pp = Product::where('kode_barang', $value->kode_barang)->first();
            if(strtolower($value->jenis_kemasan) == 'pack'){
                $pack += $value->jumlah;
            }else{
                $dos +=  $value->jumlah / $pp->pack;
            }

            $total_harga += $value->total_barang;
        }
        $jumlah_dos = (int) $dos ;
        $jumlah_pack = (int) $pack ;

        // ==================================== ganti data transaksi ===========================================

        array_walk($transaksi, function(&$item, $key) {
            $item->kode_transaksi = $item->kode_transaksi.''.$item->luar_pulau;
            $product = Product::where('kode_barang', $item->kode_barang)->first();
            if(strtolower($item->jenis_kemasan) == 'pack'){
                $item->jumlah = $item->jumlah;
            }else{
                $item->jumlah = (int) $item->jumlah / $product->pack;
            }
            $item->id_kasir = User::find($item->id_kasir)->nama;
            $item->id_customer = (Customer::find($item->id_customer) != null) ? Customer::find($item->id_customer)->nama : 'Customer dihapus';
            $item->created_at = $item->created_at->format('d-m-Y H:i:s');
        });
        // ===============================================================================

        $pelanggan = [];
        $jumlah_pelanggan = 0;
        foreach ($transaksi as $key => $value) {
            if(!in_array($value->id_customer, $pelanggan)){
                array_push($pelanggan, $value->id_customer);
                $jumlah_pelanggan++;
            } 
        }

        // ===============================================================================
        
        $tgl1 = Carbon::parse($start_date)->isoFormat('D MMMM Y');
        $tgl2 = Carbon::parse($end_date)->isoFormat('D MMMM Y');
        $tgl = $tgl1.' s/d '.$tgl2;

        return Excel::download(new ExportLaporanPenjualan($transaksi, $penjualan, $jumlah_dos, $jumlah_pack, $tgl, $jumlah, $jumlah_pelanggan, $total_harga), 'penjualan-'.now().'.xlsx');
    }
    public function printPenjualan(Request $req)
    {
        // $start_date = $req->tgl_awal;
        // $end_date = $req->tgl_akhir;
        // $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
        // $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
            
        $search_penjualan = $req->cari_penjualan;

        if($req->tgl_awal != null || $req->tgl_akhir != null){
            $start_date = $req->tgl_awal;
            $end_date = $req->tgl_akhir;
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }else{
            $get_start1 = Transaction::first();
            $get_start2 = TransactionNonPPN::first();
            if ($get_start1->created_at < $get_start2->created_at) {
                $get_start = $get_start1->created_at;
            }else{
                $get_start = $get_start2->created_at;
            }

            $get_end1 = Transaction::latest()->first();
            $get_end2 = TransactionNonPPN::latest()->first();
            if ($get_end1->created_at > $get_end2->created_at) {
                $get_end = $get_end1->created_at;
            }else{
                $get_end = $get_end2->created_at;
            }

            $start_date = $get_start->format('Y-m-d');
            $end_date = $get_end->format('Y-m-d');
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }

        $transaction = Transaction::whereBetween('created_at', [$start_date2, $end_date2])
        ->select('transactions.kode_transaksi')
        ->where('status', 'lunas')
        ->where(function ($query) use($search_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_penjualan){
                    $query->where('nama', 'like', '%'.$search_penjualan.'%');
                });
        })
        ->orderBy('created_at', 'desc')
        ->get();
        $transactionNonPPN = TransactionNonPPN::whereBetween('created_at', [$start_date2, $end_date2])
        ->select('transaction_non_ppn.kode_transaksi')
        ->where('status', 'lunas')
        ->where(function ($query) use($search_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_penjualan){
                    $query->where('nama', 'like', '%'.$search_penjualan.'%');
                });
        })
        ->orderBy('created_at', 'desc')
        ->get();
        $transaction = $transaction->concat($transactionNonPPN);
        $jumlah = count($transaction);
        // ===============================================================================
        $array = [];
        foreach ($transaction as $no => $trans) {
            array_push($array, $trans->kode_transaksi);
        }
        $uniq = array_unique($array);
        $penjualan = array_values($uniq);
        // ===============================================================================

        $data_tr = Transaction::whereBetween('created_at', [$start_date2, $end_date2])
        ->where('status', 'lunas')
        ->orderBy('created_at', 'desc')
        ->where(function ($query) use($search_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_penjualan){
                    $query->where('nama', 'like', '%'.$search_penjualan.'%');
                });
        })
        ->get();
        $data_trr = TransactionNonPPN::whereBetween('created_at',[$start_date2, $end_date2])
        ->where('status', 'lunas')
        ->orderBy('created_at', 'desc')
        ->where(function ($query) use($search_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_penjualan){
                    $query->where('nama', 'like', '%'.$search_penjualan.'%');
                });
        })
        ->get();
        $transaksi = $data_tr->merge($data_trr);
        $transaksi = $transaksi->sortBy('created_at');
        $transaksi = $transaksi->values()->all();

        // ===================================== Jumlah Stok ==========================================

        $dos = 0;
        $pack = 0;
        $total_harga = 0;
        foreach ($transaksi as $key => $value) {
            $pp = Product::where('kode_barang', $value->kode_barang)->first();
            if(strtolower($value->jenis_kemasan) == 'pack'){
                $pack += $value->jumlah;
            }else{
                $dos +=  $value->jumlah / $pp->pack;
            }
            $total_harga += $value->total_barang;
        }
        $dos = (int) $dos ;
        $pack = (int) $pack ;

        // ==================================== ganti data transaksi ===========================================

        array_walk($transaksi, function(&$item, $key) {
            $item->kode_transaksi = $item->kode_transaksi.''.$item->luar_pulau;
            $product = Product::where('kode_barang', $item->kode_barang)->first();
            if(strtolower($item->jenis_kemasan) == 'pack'){
                $item->jumlah = $item->jumlah;
            }else{
                $item->jumlah = (int) $item->jumlah / $product->pack;
            }
            $item->id_kasir = User::find($item->id_kasir)->nama;
            $item->id_customer = (Customer::find($item->id_customer) != null) ? Customer::find($item->id_customer)->nama : 'Customer dihapus';
            $item->created_at = $item->created_at->format('d-m-Y H:i:s');
        });
        // ===============================================================================

        $pelanggan = [];
        $jumlah_pelanggan = 0;
        foreach ($transaksi as $key => $value) {
            if(!in_array($value->id_customer, $pelanggan)){
                array_push($pelanggan, $value->id_customer);
                $jumlah_pelanggan++;
            } 
        }

        // ===============================================================================

        $sum_transaction = Transaction::whereBetween('created_at', [$start_date2, $end_date2])
        ->where('status', 'lunas')
        ->where(function ($query) use($search_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_penjualan){
                    $query->where('nama', 'like', '%'.$search_penjualan.'%');
                });
        })
        ->sum('total_barang');
        $sum_transactionNonPPN = TransactionNonPPN::whereBetween('created_at', [$start_date2, $end_date2])
        ->where('status', 'lunas')
        ->where(function ($query) use($search_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_penjualan){
                    $query->where('nama', 'like', '%'.$search_penjualan.'%');
                });
        })
        ->sum('total_barang');
        $total = $sum_transaction + $sum_transactionNonPPN;
        $total = number_format($total, 0, ',', '.');
        // ===============================================================================
        
        $tgl1 = Carbon::parse($start_date)->isoFormat('D MMMM Y');
        $tgl2 = Carbon::parse($end_date)->isoFormat('D MMMM Y');
        $tgl = $tgl1.' s/d '.$tgl2;

        return view('report.print_laporan_penjualan', compact('transaksi', 'penjualan', 'dos', 'pack', 'total', 'tgl', 'jumlah', 'jumlah_pelanggan', 'total_harga'));
    }
    public function reportReturPenjualan()
    {
        return view('report.report_retur_penjualan');
    }

    public function getReturPenjualan()
    {
        $transaction = Transaction::whereDate('created_at', Carbon::today())
        ->select('transactions.kode_transaksi')
        ->where('status', 'retur')
        ->orderBy('created_at', 'desc')
        ->get();
        $transactionNonPPN = TransactionNonPPN::whereDate('created_at',Carbon::today())
        ->select('transaction_non_ppn.kode_transaksi')
        ->where('status', 'retur')
        ->orderBy('created_at', 'desc')
        ->get();
        $transaction = $transaction->concat($transactionNonPPN);
        $jumlah = count($transaction);
        // ===============================================================================
        $array = [];
        foreach ($transaction as $no => $trans) {
            array_push($array, $trans->kode_transaksi);
        }
        $uniq = array_unique($array);
        $penjualan = array_values($uniq);
        // ===============================================================================

        $data_tr = Transaction::whereDate('created_at', Carbon::today())
        ->where('status', 'retur')
        ->orderBy('created_at', 'desc')
        ->get();
        $data_trr = TransactionNonPPN::whereDate('created_at',Carbon::today())
        ->where('status', 'retur')
        ->orderBy('created_at', 'desc')
        ->get();
        $transaksi = $data_tr->merge($data_trr);
        $transaksi = $transaksi->sortBy('created_at');
        $transaksi = $transaksi->values()->all();

        // ===================================== Jumlah Stok ==========================================

        $dos = 0;
        $pack = 0;
        $total_harga = 0;
        foreach ($transaksi as $key => $value) {
            $pp = Product::where('kode_barang', $value->kode_barang)->first();
            if(strtolower($value->jenis_kemasan) == 'pack'){
                $pack += $value->jumlah;
            }else{
                $dos +=  $value->jumlah / $pp->pack;
            }

            $total_harga += $value->total_barang;
        }
        $jumlah_dos = (int) $dos ;
        $jumlah_pack = (int) $pack ;

        // ==================================== ganti data transaksi ===========================================

        array_walk($transaksi, function(&$item, $key) {
            $item->kode_transaksi = $item->kode_transaksi.''.$item->luar_pulau;
            $product = Product::where('kode_barang', $item->kode_barang)->first();
            if(strtolower($item->jenis_kemasan) == 'pack'){
                $item->jumlah = $item->jumlah .' '. $item->jenis_kemasan;
            }else{
                $item->jumlah = (int) $item->jumlah / $product->pack .' '. $item->jenis_kemasan;
            }
            $item->total_barang = number_format($item->total_barang, 0, ',', '.');
            $item->id_customer = Customer::find($item->id_customer)->nama;
            $item->created_at = $item->created_at->format('d-m-Y H:i:s');
        });
        // ===============================================================================

        $pelanggan = [];
        $jumlah_pelanggan = 0;
        foreach ($transaksi as $key => $value) {
            if(!in_array($value->id_customer, $pelanggan)){
                array_push($pelanggan, $value->id_customer);
                $jumlah_pelanggan++;
            } 
        }

        // ===============================================================================
            
        $tgl = Carbon::parse(now())->isoFormat('D MMMM Y');

        return request()->json(200, [
            'tgl' => $tgl,
            'transaksi' => $transaksi,
            'jumlah_dos' => $jumlah_dos,
            'jumlah_pack' => $jumlah_pack,
            'jumlah' => $jumlah,
            'penjualan' => $penjualan,
            'pelanggan' => $pelanggan,
            'jumlah_pelanggan' => $jumlah_pelanggan,
            'jumlah_total_harga' => number_format($total_harga,2,',','.')
        ]);
    }

    public function getReturPenjualanFilter(Request $req)
    {

        // $start_date = $req->tgl_awal;
        // $end_date = $req->tgl_akhir;
        // $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
        // $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));

        $search_retur_penjualan = $req->cari_retur_penjualan;

        if($req->tgl_awal != null || $req->tgl_akhir != null){
            $start_date = $req->tgl_awal;
            $end_date = $req->tgl_akhir;
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }else{
            $get_start1 = Transaction::first();
            $get_start2 = TransactionNonPPN::first();
            if ($get_start1->created_at < $get_start2->created_at) {
                $get_start = $get_start1->created_at;
            }else{
                $get_start = $get_start2->created_at;
            }

            $get_end1 = Transaction::latest()->first();
            $get_end2 = TransactionNonPPN::latest()->first();
            if ($get_end1->created_at > $get_end2->created_at) {
                $get_end = $get_end1->created_at;
            }else{
                $get_end = $get_end2->created_at;
            }

            $start_date = $get_start->format('Y-m-d');
            $end_date = $get_end->format('Y-m-d');
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }

        $transaction = Transaction::whereBetween('created_at', [$start_date2, $end_date2])
        ->select('transactions.kode_transaksi')
        ->where('status', 'retur')
        ->where(function ($query) use($search_retur_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_retur_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_retur_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_retur_penjualan){
                    $query->where('nama', 'like', '%'.$search_retur_penjualan.'%');
                });
        })
        ->orderBy('created_at', 'desc')
        ->get();
        $transactionNonPPN = TransactionNonPPN::whereBetween('created_at', [$start_date2, $end_date2])
        ->select('transaction_non_ppn.kode_transaksi')
        ->where('status', 'retur')
        ->where(function ($query) use($search_retur_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_retur_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_retur_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_retur_penjualan){
                    $query->where('nama', 'like', '%'.$search_retur_penjualan.'%');
                });
        })
        ->orderBy('created_at', 'desc')
        ->get();
        $transaction = $transaction->concat($transactionNonPPN);
        $jumlah = count($transaction);
        // ===============================================================================
        $array = [];
        foreach ($transaction as $no => $trans) {
            array_push($array, $trans->kode_transaksi);
        }
        $uniq = array_unique($array);
        $penjualan = array_values($uniq);
        // ===============================================================================

        $data_tr = Transaction::whereBetween('created_at', [$start_date2, $end_date2])
        ->where('status', 'retur')
        ->orderBy('created_at', 'desc')
        ->where(function ($query) use($search_retur_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_retur_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_retur_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_retur_penjualan){
                    $query->where('nama', 'like', '%'.$search_retur_penjualan.'%');
                });
        })
        ->get();
        $data_trr = TransactionNonPPN::whereBetween('created_at',[$start_date2, $end_date2])
        ->where('status', 'retur')
        ->orderBy('created_at', 'desc')
        ->where(function ($query) use($search_retur_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_retur_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_retur_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_retur_penjualan){
                    $query->where('nama', 'like', '%'.$search_retur_penjualan.'%');
                });
        })
        ->get();
        $transaksi = $data_tr->merge($data_trr);
        $transaksi = $transaksi->sortBy('created_at');
        $transaksi = $transaksi->values()->all();

        // ===================================== Jumlah Stok ==========================================

        $dos = 0;
        $pack = 0;
        $total_harga = 0;
        foreach ($transaksi as $key => $value) {
            $pp = Product::where('kode_barang', $value->kode_barang)->first();
            if(strtolower($value->jenis_kemasan) == 'pack'){
                $pack += $value->jumlah;
            }else{
                $dos +=  $value->jumlah / $pp->pack;
            }
            $total_harga += $value->total_barang;
        }
        $jumlah_dos = (int) $dos ;
        $jumlah_pack = (int) $pack ;

        // ==================================== ganti data transaksi ===========================================

        array_walk($transaksi, function(&$item, $key) {
            $item->kode_transaksi = $item->kode_transaksi.''.$item->luar_pulau;
            $product = Product::where('kode_barang', $item->kode_barang)->first();
            if(strtolower($item->jenis_kemasan) == 'pack'){
                $item->jumlah = $item->jumlah .' '. $item->jenis_kemasan;
            }else{
                $item->jumlah = (int) $item->jumlah / $product->pack .' '. $item->jenis_kemasan;
            }
            $item->total_barang = number_format($item->total_barang, 0, ',', '.');
            $item->id_customer = (Customer::find($item->id_customer) != null) ? Customer::find($item->id_customer)->nama : 'Customer dihapus';
            $item->created_at = $item->created_at->format('d-m-Y H:i:s');
        });
        // ===============================================================================

        $pelanggan = [];
        $jumlah_pelanggan = 0;
        foreach ($transaksi as $key => $value) {
            if(!in_array($value->id_customer, $pelanggan)){
                array_push($pelanggan, $value->id_customer);
                $jumlah_pelanggan++;
            } 
        }

        // ===============================================================================

        $tgl1 = Carbon::parse($start_date)->isoFormat('D MMMM Y');
        $tgl2 = Carbon::parse($end_date)->isoFormat('D MMMM Y');
        $tgl = $tgl1.' s/d '.$tgl2;

        return request()->json(200, [
            'tgl' => $tgl,
            'transaksi' => $transaksi,
            'jumlah_dos' => $jumlah_dos,
            'jumlah_pack' => $jumlah_pack,
            'jumlah' => $jumlah,
            'penjualan' => $penjualan,
            'pelanggan' => $pelanggan,
            'jumlah_pelanggan' => $jumlah_pelanggan,
            'jumlah_total_harga' => number_format($total_harga,2,',','.'),
        ]);
    }

    public function exportReturPenjualan(Request $req)
    {
        // $start_date = $req->tgl_awal;
        // $end_date = $req->tgl_akhir;
        // $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
        // $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));

        $search_retur_penjualan = $req->cari_retur_penjualan;

        if($req->tgl_awal != null || $req->tgl_akhir != null){
            $start_date = $req->tgl_awal;
            $end_date = $req->tgl_akhir;
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }else{
            $get_start1 = Transaction::first();
            $get_start2 = TransactionNonPPN::first();
            if ($get_start1->created_at < $get_start2->created_at) {
                $get_start = $get_start1->created_at;
            }else{
                $get_start = $get_start2->created_at;
            }

            $get_end1 = Transaction::latest()->first();
            $get_end2 = TransactionNonPPN::latest()->first();
            if ($get_end1->created_at > $get_end2->created_at) {
                $get_end = $get_end1->created_at;
            }else{
                $get_end = $get_end2->created_at;
            }

            $start_date = $get_start->format('Y-m-d');
            $end_date = $get_end->format('Y-m-d');
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }

        $transaction = Transaction::whereBetween('created_at', [$start_date2, $end_date2])
        ->select('transactions.kode_transaksi')
        ->where('status', 'retur')
        ->where(function ($query) use($search_retur_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_retur_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_retur_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_retur_penjualan){
                    $query->where('nama', 'like', '%'.$search_retur_penjualan.'%');
                });
        })
        ->orderBy('created_at', 'desc')
        ->get();
        $transactionNonPPN = TransactionNonPPN::whereBetween('created_at', [$start_date2, $end_date2])
        ->select('transaction_non_ppn.kode_transaksi')
        ->where('status', 'retur')
        ->where(function ($query) use($search_retur_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_retur_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_retur_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_retur_penjualan){
                    $query->where('nama', 'like', '%'.$search_retur_penjualan.'%');
                });
        })
        ->orderBy('created_at', 'desc')
        ->get();
        $transaction = $transaction->concat($transactionNonPPN);
        $jumlah = count($transaction);
        // ===============================================================================
        $array = [];
        foreach ($transaction as $no => $trans) {
            array_push($array, $trans->kode_transaksi);
        }
        $uniq = array_unique($array);
        $penjualan = array_values($uniq);
        // ===============================================================================

        $data_tr = Transaction::whereBetween('created_at', [$start_date2, $end_date2])
        ->where('status', 'retur')
        ->orderBy('created_at', 'desc')
        ->where(function ($query) use($search_retur_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_retur_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_retur_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_retur_penjualan){
                    $query->where('nama', 'like', '%'.$search_retur_penjualan.'%');
                });
        })
        ->get();
        $data_trr = TransactionNonPPN::whereBetween('created_at',[$start_date2, $end_date2])
        ->where('status', 'retur')
        ->orderBy('created_at', 'desc')
        ->where(function ($query) use($search_retur_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_retur_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_retur_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_retur_penjualan){
                    $query->where('nama', 'like', '%'.$search_retur_penjualan.'%');
                });
        })
        ->get();
        $transaksi = $data_tr->merge($data_trr);
        $transaksi = $transaksi->sortBy('created_at');
        $transaksi = $transaksi->values()->all();

        // ===================================== Jumlah Stok ==========================================

        $dos = 0;
        $pack = 0;
        $total_harga= 0;
        foreach ($transaksi as $key => $value) {
            $pp = Product::where('kode_barang', $value->kode_barang)->first();
            if(strtolower($value->jenis_kemasan) == 'pack'){
                $pack += $value->jumlah;
            }else{
                $dos +=  $value->jumlah / $pp->pack;
            }

            $total_harga += $value->total_barang;
        }
        $jumlah_dos = (int) $dos ;
        $jumlah_pack = (int) $pack ;

        // ==================================== ganti data transaksi ===========================================

        array_walk($transaksi, function(&$item, $key) {
            $item->kode_transaksi = $item->kode_transaksi.''.$item->luar_pulau;
            $product = Product::where('kode_barang', $item->kode_barang)->first();
            if(strtolower($item->jenis_kemasan) == 'pack'){
                $item->jumlah = $item->jumlah;
            }else{
                $item->jumlah = (int) $item->jumlah / $product->pack;
            }
            $item->id_kasir = User::find($item->id_kasir)->nama;
            $item->id_customer = (Customer::find($item->id_customer) != null) ? Customer::find($item->id_customer)->nama : 'Customer dihapus';
            $item->created_at = $item->created_at->format('d-m-Y H:i:s');
        });
        // ===============================================================================

        $pelanggan = [];
        $jumlah_pelanggan = 0;
        foreach ($transaksi as $key => $value) {
            if(!in_array($value->id_customer, $pelanggan)){
                array_push($pelanggan, $value->id_customer);
                $jumlah_pelanggan++;
            } 
        }

        // ===============================================================================
        
        $tgl1 = Carbon::parse($start_date)->isoFormat('D MMMM Y');
        $tgl2 = Carbon::parse($end_date)->isoFormat('D MMMM Y');
        $tgl = $tgl1.' s/d '.$tgl2;
        
        return Excel::download(new ExportLaporanReturPenjualan($transaksi, $penjualan, $jumlah_dos, $jumlah_pack, $tgl, $jumlah, $jumlah_pelanggan, $total_harga), 'retur penjualan-'.now().'.xlsx');
    }
    public function printReturPenjualan(Request $req)
    {
        // $start_date = $req->tgl_awal;
        // $end_date = $req->tgl_akhir;
        // $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
        // $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
            
        $search_retur_penjualan = $req->cari_retur_penjualan;

        if($req->tgl_awal != null || $req->tgl_akhir != null){
            $start_date = $req->tgl_awal;
            $end_date = $req->tgl_akhir;
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }else{
            $get_start1 = Transaction::first();
            $get_start2 = TransactionNonPPN::first();
            if ($get_start1->created_at < $get_start2->created_at) {
                $get_start = $get_start1->created_at;
            }else{
                $get_start = $get_start2->created_at;
            }

            $get_end1 = Transaction::latest()->first();
            $get_end2 = TransactionNonPPN::latest()->first();
            if ($get_end1->created_at > $get_end2->created_at) {
                $get_end = $get_end1->created_at;
            }else{
                $get_end = $get_end2->created_at;
            }

            $start_date = $get_start->format('Y-m-d');
            $end_date = $get_end->format('Y-m-d');
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }

        $transaction = Transaction::whereBetween('created_at', [$start_date2, $end_date2])
        ->select('transactions.kode_transaksi')
        ->where('status', 'retur')
        ->where(function ($query) use($search_retur_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_retur_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_retur_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_retur_penjualan){
                    $query->where('nama', 'like', '%'.$search_retur_penjualan.'%');
                });
        })
        ->orderBy('created_at', 'desc')
        ->get();
        $transactionNonPPN = TransactionNonPPN::whereBetween('created_at', [$start_date2, $end_date2])
        ->select('transaction_non_ppn.kode_transaksi')
        ->where('status', 'retur')
        ->where(function ($query) use($search_retur_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_retur_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_retur_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_retur_penjualan){
                    $query->where('nama', 'like', '%'.$search_retur_penjualan.'%');
                });
        })
        ->orderBy('created_at', 'desc')
        ->get();
        $transaction = $transaction->concat($transactionNonPPN);
        $jumlah = count($transaction);
        // ===============================================================================
        $array = [];
        foreach ($transaction as $no => $trans) {
            array_push($array, $trans->kode_transaksi);
        }
        $uniq = array_unique($array);
        $penjualan = array_values($uniq);
        // ===============================================================================

        $data_tr = Transaction::whereBetween('created_at', [$start_date2, $end_date2])
        ->where('status', 'retur')
        ->orderBy('created_at', 'desc')
        ->where(function ($query) use($search_retur_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_retur_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_retur_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_retur_penjualan){
                    $query->where('nama', 'like', '%'.$search_retur_penjualan.'%');
                });
        })
        ->get();
        $data_trr = TransactionNonPPN::whereBetween('created_at',[$start_date2, $end_date2])
        ->where('status', 'retur')
        ->orderBy('created_at', 'desc')
        ->where(function ($query) use($search_retur_penjualan){
            $query->where('kode_transaksi', 'like', '%' . $search_retur_penjualan . '%')
                ->orWhere('nama_barang', 'like', '%' . $search_retur_penjualan . '%')
                ->orWhereHas('customer', function($query) use ($search_retur_penjualan){
                    $query->where('nama', 'like', '%'.$search_retur_penjualan.'%');
                });
        })
        ->get();
        $transaksi = $data_tr->merge($data_trr);
        $transaksi = $transaksi->sortBy('created_at');
        $transaksi = $transaksi->values()->all();

        // ===================================== Jumlah Stok ==========================================

        $dos = 0;
        $pack = 0;
        $total_harga = 0;
        foreach ($transaksi as $key => $value) {
            $pp = Product::where('kode_barang', $value->kode_barang)->first();
            if(strtolower($value->jenis_kemasan) == 'pack'){
                $pack += $value->jumlah;
            }else{
                $dos +=  $value->jumlah / $pp->pack;
            }

            $total_harga += $value->total_barang;
        }
        $dos = (int) $dos ;
        $pack = (int) $pack ;

        // ==================================== ganti data transaksi ===========================================

        array_walk($transaksi, function(&$item, $key) {
            $item->kode_transaksi = $item->kode_transaksi.''.$item->luar_pulau;
            $product = Product::where('kode_barang', $item->kode_barang)->first();
            if(strtolower($item->jenis_kemasan) == 'pack'){
                $item->jumlah = $item->jumlah;
            }else{
                $item->jumlah = (int) $item->jumlah / $product->pack;
            }
            $item->id_kasir = User::find($item->id_kasir)->nama;
            $item->id_customer = (Customer::find($item->id_customer) != null) ? Customer::find($item->id_customer)->nama : 'Customer dihapus';
            $item->created_at = $item->created_at->format('d-m-Y H:i:s');
        });
        // ===============================================================================

        $pelanggan = [];
        $jumlah_pelanggan = 0;
        foreach ($transaksi as $key => $value) {
            if(!in_array($value->id_customer, $pelanggan)){
                array_push($pelanggan, $value->id_customer);
                $jumlah_pelanggan++;
            } 
        }

        // ===============================================================================
        
        $tgl1 = Carbon::parse($start_date)->isoFormat('D MMMM Y');
        $tgl2 = Carbon::parse($end_date)->isoFormat('D MMMM Y');
        $tgl = $tgl1.' s/d '.$tgl2;

        return view('report.print_laporan_retur_penjualan', compact('transaksi', 'penjualan', 'dos', 'pack', 'tgl', 'jumlah', 'jumlah_pelanggan', 'total_harga'));
    }
    public function reportPembelian()
    {
        return view('report.report_pembelian');
    }

    public function getPembelian()
    {
        $supply = Supply::whereDate('created_at', Carbon::today())
        ->select('supplies.kode_pasok')
        ->where('status', 'lunas')
        ->orderBy('created_at', 'desc')
        ->get();
        $jumlah = count($supply);
        // ===============================================================================
        $array = [];
        foreach ($supply as $no => $supp) {
            array_push($array, $supp->kode_pasok);
        }
        $uniq = array_unique($array);
        $pembelian = array_values($uniq);
        // ===============================================================================

        $pasok = Supply::whereDate('created_at', Carbon::today())
        ->where('status', 'lunas')
        ->orderBy('created_at', 'desc')
        ->get();
        $pasok = $pasok->sortBy('created_at');
        $pasok = $pasok->values()->all();

        // ===================================== Jumlah Stok ==========================================

        $dos = 0;
        $pack = 0;
        $harga_total = 0;
        foreach ($pasok as $key => $value) {
            $pp = Product::where('kode_barang', $value->kode_barang)->first();
            if(strtolower($value->jenis_kemasan) == 'pack'){
                $pack += $value->jumlah;
            }else{
                $dos +=  $value->jumlah / $pp->pack;
            }
            $harga_total += $value->subtotal;
        }
        $jumlah_dos = (int) $dos ;
        $jumlah_pack = (int) $pack ;

        // ==================================== ganti data transaksi ===========================================

        array_walk($pasok, function(&$item, $key) {
            $product = Product::where('kode_barang', $item->kode_barang)->first();
            if(strtolower($item->jenis_kemasan) == 'pack'){
                $item->jumlah = $item->jumlah .' '. $item->jenis_kemasan;
            }else{
                $item->jumlah = (int) $item->jumlah / $product->pack .' '. $item->jenis_kemasan;
            }
            $item->subtotal = number_format($item->subtotal, 0, ',', '.');
            $item->created_at = $item->created_at->format('d-m-Y H:i:s');
        });
        // ===============================================================================

        $pemasok = [];
        $jumlah_pemasok = 0;
        foreach ($pasok as $key => $value) {
            if(!in_array($value->pemasok, $pemasok)){
                array_push($pemasok, $value->pemasok);
                $jumlah_pemasok++;
            } 
        }

        // ===============================================================================
            
        $tgl = Carbon::parse(now())->isoFormat('D MMMM Y');

        return request()->json(200, [
            'tgl' => $tgl,
            'pasok' => $pasok,
            'jumlah_dos' => $jumlah_dos,
            'jumlah_pack' => $jumlah_pack,
            'jumlah' => $jumlah,
            'pembelian' => $pembelian,
            'pemasok' => $pemasok,
            'jumlah_pemasok' => $jumlah_pemasok,
            'jumlah_harga_total' => number_format($harga_total,2,',','.')
        ]);
    }

    public function getPembelianFilter(Request $req)
    {
        // $start_date = $req->tgl_awal;
        // $end_date = $req->tgl_akhir;
        // $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
        // $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));

        $search_pembelian = $req->cari_pembelian;

        if($req->tgl_awal != null || $req->tgl_akhir != null){
            $start_date = $req->tgl_awal;
            $end_date = $req->tgl_akhir;
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }else{
            $get_start = Supply::first();
            $get_end = Supply::latest()->first();

            $start_date = $get_start->created_at->format('Y-m-d');
            $end_date = $get_end->created_at->format('Y-m-d');
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }

        $supply = Supply::whereBetween('created_at', [$start_date2, $end_date2])
        ->select('supplies.kode_pasok')
        ->where('status', 'lunas')
        ->where(function ($query) use($search_pembelian){
            $query->where('kode_pasok', 'like', '%'.$search_pembelian.'%')
                ->orWhere('surat_jalan', 'like', '%'.$search_pembelian.'%')
                ->orWhere('no_faktur', 'like', '%'.$search_pembelian.'%')
                ->orWhere('nama_barang', 'like', '%'.$search_pembelian.'%')
                ->orWhereHas('supplier', function($query) use ($search_pembelian){
                        $query->where('nama', 'like', '%'.$search_pembelian.'%');
                });
        })
        ->orderBy('created_at', 'desc')
        ->get();
        
        $jumlah = count($supply);
        // ===============================================================================
        $array = [];
        foreach ($supply as $no => $trans) {
            array_push($array, $trans->kode_pasok);
        }
        $uniq = array_unique($array);
        $pembelian = array_values($uniq);
        // ===============================================================================

        $data_sp = supply::whereBetween('created_at', [$start_date2, $end_date2])
        ->where('status', 'lunas')
        ->orderBy('created_at', 'desc')
        ->where(function ($query) use($search_pembelian){
            $query->where('kode_pasok', 'like', '%'.$search_pembelian.'%')
                ->orWhere('surat_jalan', 'like', '%'.$search_pembelian.'%')
                ->orWhere('no_faktur', 'like', '%'.$search_pembelian.'%')
                ->orWhere('nama_barang', 'like', '%'.$search_pembelian.'%')
                ->orWhereHas('supplier', function($query) use ($search_pembelian){
                        $query->where('nama', 'like', '%'.$search_pembelian.'%');
                });
        })
        ->get();
        $pasok = $data_sp;
        $pasok = $pasok->sortBy('created_at');
        $pasok = $pasok->values()->all();

        // ===================================== Jumlah Stok ==========================================

        $dos = 0;
        $pack = 0;
        $harga_total = 0;
        foreach ($pasok as $key => $value) {
            $pp = Product::where('kode_barang', $value->kode_barang)->first();
            if(strtolower($value->jenis_kemasan) == 'pack'){
                $pack += $value->jumlah;
            }else{
                $dos +=  $value->jumlah / $pp->pack;
            }
            $harga_total += $value->subtotal;
        }
        $jumlah_dos = (int) $dos ;
        $jumlah_pack = (int) $pack ;

        // ==================================== ganti data pasok ===========================================

        array_walk($pasok, function(&$item, $key) {
            $item->kode_pasok = $item->kode_pasok.''.$item->luar_pulau;
            $product = Product::where('kode_barang', $item->kode_barang)->first();
            if(strtolower($item->jenis_kemasan) == 'pack'){
                $item->jumlah = $item->jumlah .' '. $item->jenis_kemasan;
            }else{
                $item->jumlah = (int) $item->jumlah / $product->pack .' '. $item->jenis_kemasan;
            }
            $item->subtotal = number_format($item->subtotal, 0, ',', '.');
            $item->created_at = $item->created_at->format('d-m-Y H:i:s');
        });
        // ===============================================================================
        
        $pemasok = [];
        $jumlah_pemasok = 0;
        foreach ($pasok as $key => $value) {
            if(!in_array($value->pemasok, $pemasok)){
                array_push($pemasok, $value->pemasok);
                $jumlah_pemasok++;
            } 
        }

        $tgl1 = Carbon::parse($start_date)->isoFormat('D MMMM Y');
        $tgl2 = Carbon::parse($end_date)->isoFormat('D MMMM Y');
        $tgl = $tgl1.' s/d '.$tgl2;

        return request()->json(200, [
            'tgl' => $tgl,
            'pasok' => $pasok,
            'jumlah_dos' => $jumlah_dos,
            'jumlah_pack' => $jumlah_pack,
            'jumlah' => $jumlah,
            'pembelian' => $pembelian,
            'jumlah_pemasok' => $jumlah_pemasok,
            'jumlah_harga_total' => number_format($harga_total,2,',','.')
        ]);
    }

    public function exportPembelian(Request $req)
    {
        // $start_date = $req->tgl_awal;
        // $end_date = $req->tgl_akhir;
        // $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
        // $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));

        $search_pembelian = $req->cari_pembelian;

        if($req->tgl_awal != null || $req->tgl_akhir != null){
            $start_date = $req->tgl_awal;
            $end_date = $req->tgl_akhir;
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }else{
            $get_start = Supply::first();
            $get_end = Supply::latest()->first();

            $start_date = $get_start->created_at->format('Y-m-d');
            $end_date = $get_end->created_at->format('Y-m-d');
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }

        $supply = Supply::whereBetween('created_at', [$start_date2, $end_date2])
        ->select('supplies.kode_pasok')
        ->where('status', 'lunas')
        ->where(function ($query) use($search_pembelian){
            $query->where('kode_pasok', 'like', '%'.$search_pembelian.'%')
                ->orWhere('surat_jalan', 'like', '%'.$search_pembelian.'%')
                ->orWhere('no_faktur', 'like', '%'.$search_pembelian.'%')
                ->orWhere('nama_barang', 'like', '%'.$search_pembelian.'%')
                ->orWhereHas('supplier', function($query) use ($search_pembelian){
                        $query->where('nama', 'like', '%'.$search_pembelian.'%');
                });
        })
        ->orderBy('created_at', 'desc')
        ->get();

        $jumlah = count($supply);
        // ===============================================================================
        $array = [];
        foreach ($supply as $no => $trans) {
            array_push($array, $trans->kode_pasok);
        }
        $uniq = array_unique($array);
        $pembelian = array_values($uniq);
        // ===============================================================================

        $pasok = Supply::whereBetween('created_at', [$start_date2, $end_date2])
        ->where('status', 'lunas')
        ->orderBy('created_at', 'desc')
        ->where(function ($query) use($search_pembelian){
            $query->where('kode_pasok', 'like', '%'.$search_pembelian.'%')
                ->orWhere('surat_jalan', 'like', '%'.$search_pembelian.'%')
                ->orWhere('no_faktur', 'like', '%'.$search_pembelian.'%')
                ->orWhere('nama_barang', 'like', '%'.$search_pembelian.'%')
                ->orWhereHas('supplier', function($query) use ($search_pembelian){
                        $query->where('nama', 'like', '%'.$search_pembelian.'%');
                });
        })
        ->get();
        $pasok = $pasok->sortBy('created_at');
        $pasok = $pasok->values()->all();
        
        // ===================================== Jumlah Stok ==========================================

        $dos = 0;
        $pack = 0;
        $harga_total = 0;
        foreach ($pasok as $key => $value) {
            $pp = Product::where('kode_barang', $value->kode_barang)->first();
            if(strtolower($value->jenis_kemasan) == 'pack'){
                $pack += $value->jumlah;
            }else{
                $dos +=  $value->jumlah / $pp->pack;
            }
            $harga_total += $value->subtotal;
        }
        $jumlah_dos = (int) $dos ;
        $jumlah_pack = (int) $pack ;

        // ==================================== ganti data pasok ===========================================

        array_walk($pasok, function(&$item, $key) {
            $product = Product::where('kode_barang', $item->kode_barang)->first();
            if(strtolower($item->jenis_kemasan) == 'pack'){
                $item->jumlah = $item->jumlah;
            }else{
                $item->jumlah = (int) $item->jumlah / $product->pack;
            }
            $item->id_worker = User::find($item->id_worker)->nama;
            $item->id_pemasok = Supplier::find($item->id_pemasok)->nama;
            $item->created_at = $item->created_at->format('d-m-Y H:i:s');
        });
        // ===============================================================================

        $pemasok = [];
        $jumlah_pemasok = 0;
        foreach ($pasok as $key => $value) {
            if(!in_array($value->id_pemasok, $pemasok)){
                array_push($pemasok, $value->id_pemasok);
                $jumlah_pemasok++;
            } 
        }

        // ===============================================================================
        
        $tgl1 = Carbon::parse($start_date)->isoFormat('D MMMM Y');
        $tgl2 = Carbon::parse($end_date)->isoFormat('D MMMM Y');
        $tgl = $tgl1.' s/d '.$tgl2;
 
        return Excel::download(new ExportLaporanPembelian($pasok, $pembelian, $jumlah_dos, $jumlah_pack, $tgl, $jumlah, $jumlah_pemasok, $harga_total), 'pembelian-'.now().'.xlsx');
    }
    public function printPembelian(Request $req)
    {
        // $start_date = $req->tgl_awal;
        // $end_date = $req->tgl_akhir;
        // $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
        // $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
            
        $search_pembelian = $req->cari_pembelian;

        if($req->tgl_awal != null || $req->tgl_akhir != null){
            $start_date = $req->tgl_awal;
            $end_date = $req->tgl_akhir;
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }else{
            $get_start = Supply::first();
            $get_end = Supply::latest()->first();

            $start_date = $get_start->created_at->format('Y-m-d');
            $end_date = $get_end->created_at->format('Y-m-d');
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }
        
        $supply = Supply::whereBetween('created_at', [$start_date2, $end_date2])
        ->select('supplies.kode_pasok')
        ->where('status', 'lunas')
        ->where(function ($query) use($search_pembelian){
            $query->where('kode_pasok', 'like', '%'.$search_pembelian.'%')
                ->orWhere('surat_jalan', 'like', '%'.$search_pembelian.'%')
                ->orWhere('no_faktur', 'like', '%'.$search_pembelian.'%')
                ->orWhere('nama_barang', 'like', '%'.$search_pembelian.'%')
                ->orWhereHas('supplier', function($query) use ($search_pembelian){
                        $query->where('nama', 'like', '%'.$search_pembelian.'%');
                });
        })
        ->orderBy('created_at', 'desc')
        ->get();
        
        $jumlah = count($supply);
        // ===============================================================================
        $array = [];
        foreach ($supply as $no => $trans) {
            array_push($array, $trans->kode_pasok);
        }
        $uniq = array_unique($array);
        $pembelian = array_values($uniq);
        // ===============================================================================

        $pasok = Supply::whereBetween('created_at', [$start_date2, $end_date2])
        ->where('status', 'lunas')
        ->orderBy('created_at', 'desc')
        ->where(function ($query) use($search_pembelian){
            $query->where('kode_pasok', 'like', '%'.$search_pembelian.'%')
                ->orWhere('surat_jalan', 'like', '%'.$search_pembelian.'%')
                ->orWhere('no_faktur', 'like', '%'.$search_pembelian.'%')
                ->orWhere('nama_barang', 'like', '%'.$search_pembelian.'%')
                ->orWhereHas('supplier', function($query) use ($search_pembelian){
                        $query->where('nama', 'like', '%'.$search_pembelian.'%');
                });
        })
        ->get();
        $pasok = $pasok->sortBy('created_at');
        $pasok = $pasok->values()->all();
        
        // ===================================== Jumlah Stok ==========================================

        $dos = 0;
        $pack = 0;
        $harga_total = 0;
        foreach ($pasok as $key => $value) {
            $pp = Product::where('kode_barang', $value->kode_barang)->first();
            if(strtolower($value->jenis_kemasan) == 'pack'){
                $pack += $value->jumlah;
            }else{
                $dos +=  $value->jumlah / $pp->pack;
            }

            $harga_total += $value->subtotal;
        }
        $dos = (int) $dos ;
        $pack = (int) $pack ;

        // ==================================== ganti data pasok ===========================================

        array_walk($pasok, function(&$item, $key) {
            $product = Product::where('kode_barang', $item->kode_barang)->first();
            if(strtolower($item->jenis_kemasan) == 'pack'){
                $item->jumlah = $item->jumlah;
            }else{
                $item->jumlah = (int) $item->jumlah / $product->pack;
            }
            $item->id_worker = User::find($item->id_worker)->nama;
            $item->id_pemasok = Supplier::find($item->id_pemasok)->nama;
            $item->created_at = $item->created_at->format('d-m-Y H:i:s');
        });
        // ===============================================================================

        $pemasok = [];
        $jumlah_pemasok = 0;
        foreach ($pasok as $key => $value) {
            if(!in_array($value->id_pemasok, $pemasok)){
                array_push($pemasok, $value->id_pemasok);
                $jumlah_pemasok++;
            } 
        }

        // ===============================================================================
        
        $tgl1 = Carbon::parse($start_date)->isoFormat('D MMMM Y');
        $tgl2 = Carbon::parse($end_date)->isoFormat('D MMMM Y');
        $tgl = $tgl1.' s/d '.$tgl2;
        return view('report.print_laporan_pembelian', compact('pasok', 'pembelian', 'dos', 'pack', 'tgl', 'jumlah', 'jumlah_pemasok', 'harga_total'));
    }
    public function reportReturPembelian()
    {
        return view('report.report_retur_pembelian');
    }

    public function getReturPembelian()
    {
        $retur_supply = ReturSupplier::whereDate('created_at', Carbon::today())
        ->select('retur_supplier.no_surat_keluar')
        ->orderBy('created_at', 'desc')
        ->get();
        $jumlah = count($retur_supply);
        // ===============================================================================
        $array = [];
        foreach ($retur_supply as $no => $retur_supp) {
            array_push($array, $retur_supp->no_surat_keluar);
        }
        $uniq = array_unique($array);
        $retur_pembelian = array_values($uniq);
        // ===============================================================================

        $retur_pasok = ReturSupplier::whereDate('created_at', Carbon::today())
        ->orderBy('created_at', 'desc')
        ->get();
        $retur_pasok = $retur_pasok->sortBy('created_at');
        $retur_pasok = $retur_pasok->values()->all();

        // ===================================== Jumlah Stok ==========================================

        $dos = 0;
        $pack = 0;
        $harga_total= 0;
        foreach ($retur_pasok as $key => $value) {
            $pp = Product::where('id', $value->id_product)->first();
            if(strtolower($value->jenis_kemasan) == 'pack'){
                $pack += $value->jumlah;
            }else{
                $dos +=  $value->jumlah;
            }

            $harga_total += $value->jumlah * $value->harga_modal;
        }
        $jumlah_dos = (int) $dos ;
        $jumlah_pack = (int) $pack ;

        // ==================================== ganti data transaksi ===========================================

        array_walk($retur_pasok, function(&$item, $key) {
            $product = Product::where('id', $item->id_product)->first();
            $item->created_at = $item->created_at->format('d-m-Y H:i:s');
            $item->id_product = $product->nama_barang;
        });
        // ===============================================================================

        $pemasok = [];
        $jumlah_pemasok = 0;
        foreach ($retur_pasok as $key => $value) {
            if(!in_array($value->nama_supplier, $pemasok)){
                array_push($pemasok, $value->nama_supplier);
                $jumlah_pemasok++;
            } 
        }

        // ===============================================================================
            
        $tgl = Carbon::parse(now())->isoFormat('D MMMM Y');

        return request()->json(200, [
            'tgl' => $tgl,
            'retur_pasok' => $retur_pasok,
            'jumlah_dos' => $jumlah_dos,
            'jumlah_pack' => $jumlah_pack,
            'jumlah' => $jumlah,
            'retur_pembelian' => $retur_pembelian,
            'pemasok' => $pemasok,
            'jumlah_pemasok' => $jumlah_pemasok,
            'jumlah_harga_total' => $harga_total,
        ]);
    }

    public function getReturPembelianFilter(Request $req)
    {

        $search_retur_pembelian = $req->cari_retur_pembelian;

        if($req->tgl_awal != null || $req->tgl_akhir != null){
            $start_date = $req->tgl_awal;
            $end_date = $req->tgl_akhir;
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }else{
            $get_start = ReturSupplier::first();
            $get_end = ReturSupplier::latest()->first();

            $start_date = $get_start->created_at->format('Y-m-d');
            $end_date = $get_end->created_at->format('Y-m-d');
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }

        $retur_supply = ReturSupplier::whereBetween('created_at', [$start_date2, $end_date2])
        ->select('retur_supplier.no_surat_keluar')
        ->where(function ($query) use($search_retur_pembelian){
            $query->where('no_surat_keluar', 'like', '%'.$search_retur_pembelian.'%')
                ->orWhere('nama_supplier', 'like', '%'.$search_retur_pembelian.'%')
                ->orWhereHas('product', function($query) use ($search_retur_pembelian){
                        $query->where('nama_barang', 'like', '%'.$search_retur_pembelian.'%');
                });
        })
        ->orderBy('created_at', 'desc')
        ->get();

        
        $jumlah = count($retur_supply);
        // ===============================================================================
        $array = [];
        foreach ($retur_supply as $no => $trans) {
            array_push($array, $trans->no_surat_keluar);
        }
        $uniq = array_unique($array);
        $retur_pembelian = array_values($uniq);
        // ===============================================================================

        $retur_pasok = ReturSupplier::whereBetween('created_at', [$start_date2, $end_date2])
        ->orderBy('created_at', 'desc')
        ->where(function ($query) use($search_retur_pembelian){
            $query->where('no_surat_keluar', 'like', '%'.$search_retur_pembelian.'%')
                ->orWhere('nama_supplier', 'like', '%'.$search_retur_pembelian.'%')
                ->orWhereHas('product', function($query) use ($search_retur_pembelian){
                        $query->where('nama_barang', 'like', '%'.$search_retur_pembelian.'%');
                });
        })
        ->get();
        
        $retur_pasok = $retur_pasok->sortBy('created_at');
        $retur_pasok = $retur_pasok->values()->all();

        // ===================================== Jumlah Stok ==========================================

        $dos = 0;
        $pack = 0;
        $harga_total = 0;
        foreach ($retur_pasok as $key => $value) {
            $pp = Product::where('id', $value->id_product)->first();
            if(strtolower($value->jenis_kemasan) == 'pack'){
                $pack += $value->jumlah;
            }else{
                $dos +=  $value->jumlah;
            }

            $harga_total += $value->jumlah * $value->harga_modal;
        }
        $jumlah_dos = (int) $dos ;
        $jumlah_pack = (int) $pack ;

        // ==================================== ganti data pasok ===========================================

        array_walk($retur_pasok, function(&$item, $key) {
            $product = Product::where('id', $item->id_product)->first();
            $item->created_at = $item->created_at->format('d-m-Y H:i:s');
            $item->id_product = $product->nama_barang;
        });
        // ===============================================================================
        
        $pemasok = [];
        $jumlah_pemasok = 0;
        foreach ($retur_pasok as $key => $value) {
            if(!in_array($value->nama_supplier, $pemasok)){
                array_push($pemasok, $value->nama_supplier);
                $jumlah_pemasok++;
            } 
        }

        // ===============================================================================

        $tgl1 = Carbon::parse($start_date)->isoFormat('D MMMM Y');
        $tgl2 = Carbon::parse($end_date)->isoFormat('D MMMM Y');
        $tgl = $tgl1.' s/d '.$tgl2;
        
        return request()->json(200, [
            'tgl' => $tgl,
            'retur_pasok' => $retur_pasok,
            'jumlah_dos' => $jumlah_dos,
            'jumlah_pack' => $jumlah_pack,
            'jumlah' => $jumlah,
            'retur_pembelian' => $retur_pembelian,
            'pemasok' => $pemasok,
            'jumlah_pemasok' => $jumlah_pemasok,
            'jumlah_harga_total' => $harga_total,
        ]);
    }

    public function exportReturPembelian(Request $req)
    {

        $search_retur_pembelian = $req->cari_retur_pembelian;

        if($req->tgl_awal != null || $req->tgl_akhir != null){
            $start_date = $req->tgl_awal;
            $end_date = $req->tgl_akhir;
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }else{
            $get_start = ReturSupplier::first();
            $get_end = ReturSupplier::latest()->first();

            $start_date = $get_start->created_at->format('Y-m-d');
            $end_date = $get_end->created_at->format('Y-m-d');
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }

        $retur_supply = ReturSupplier::whereBetween('created_at', [$start_date2, $end_date2])
        ->select('retur_supplier.no_surat_keluar')
        ->where(function ($query) use($search_retur_pembelian){
            $query->where('no_surat_keluar', 'like', '%'.$search_retur_pembelian.'%')
                ->orWhere('nama_supplier', 'like', '%'.$search_retur_pembelian.'%')
                ->orWhereHas('product', function($query) use ($search_retur_pembelian){
                        $query->where('nama_barang', 'like', '%'.$search_retur_pembelian.'%');
                });
        })
        ->orderBy('created_at', 'desc')
        ->get();

        $total_jumlah = count($retur_supply);
  
        // ===============================================================================
        $array = [];
        foreach ($retur_supply as $no => $trans) {
            array_push($array, $trans->no_surat_keluar);
        }
        $uniq = array_unique($array);
        $retur_pembelian = array_values($uniq);
        // ===============================================================================

        $retur_pasok = ReturSupplier::whereBetween('created_at', [$start_date2, $end_date2])
        ->orderBy('created_at', 'desc')
        ->where(function ($query) use($search_retur_pembelian){
            $query->where('no_surat_keluar', 'like', '%'.$search_retur_pembelian.'%')
                ->orWhere('nama_supplier', 'like', '%'.$search_retur_pembelian.'%')
                ->orWhereHas('product', function($query) use ($search_retur_pembelian){
                        $query->where('nama_barang', 'like', '%'.$search_retur_pembelian.'%');
                });
        })
        ->get();
        
        $retur_pasok = $retur_pasok->sortBy('created_at');
        $retur_pasok = $retur_pasok->values()->all();
        
        // ===================================== Jumlah Stok ==========================================

        $dos = 0;
        $pack = 0;
        $harga_total = 0;
        foreach ($retur_pasok as $key => $value) {
            $pp = Product::where('id', $value->id_product)->first();
            if(strtolower($value->jenis_kemasan) == 'pack'){
                $pack += $value->jumlah;
            }else{
                $dos +=  $value->jumlah;
            }
            $harga_total += $value->jumlah * $value->harga_modal;
        }
        $jumlah_dos = (int) $dos ;
        $jumlah_pack = (int) $pack ;

        // ==================================== ganti data pasok ===========================================

        array_walk($retur_pasok, function(&$item, $key) {
            $item->created_at = $item->created_at->format('d-m-Y H:i:s');
        });
        // ===============================================================================

        $pemasok = [];
        $jumlah_pemasok = 0;
        foreach ($retur_pasok as $key => $value) {
            if(!in_array($value->nama_supplier, $pemasok)){
                array_push($pemasok, $value->nama_supplier);
                $jumlah_pemasok++;
            } 
        }

        $tgl1 = Carbon::parse($start_date)->isoFormat('D MMMM Y');
        $tgl2 = Carbon::parse($end_date)->isoFormat('D MMMM Y');
        $tgl = $tgl1.' s/d '.$tgl2;
        return Excel::download(new ExportLaporanReturPembelian($retur_pasok, $retur_pembelian, $jumlah_dos, $jumlah_pack, $tgl, $total_jumlah, $jumlah_pemasok, $harga_total), 'retur pembelian-'.now().'.xlsx');
    }

    public function printReturPembelian(Request $req)
    {

        $search_retur_pembelian = $req->cari_retur_pembelian;

        if($req->tgl_awal != null || $req->tgl_akhir != null){
            $start_date = $req->tgl_awal;
            $end_date = $req->tgl_akhir;
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }else{
            $get_start = ReturSupplier::first();
            $get_end = ReturSupplier::latest()->first();

            $start_date = $get_start->created_at->format('Y-m-d');
            $end_date = $get_end->created_at->format('Y-m-d');
            $start_date2 = Carbon::parse($start_date)->format('Y-m-d');
            $end_date2 = date('Y-m-d', strtotime($end_date.'+1 day'));
        }

        $retur_supply = ReturSupplier::whereBetween('created_at', [$start_date2, $end_date2])
        ->select('retur_supplier.no_surat_keluar')
        ->where(function ($query) use($search_retur_pembelian){
            $query->where('no_surat_keluar', 'like', '%'.$search_retur_pembelian.'%')
                ->orWhere('nama_supplier', 'like', '%'.$search_retur_pembelian.'%')
                ->orWhereHas('product', function($query) use ($search_retur_pembelian){
                        $query->where('nama_barang', 'like', '%'.$search_retur_pembelian.'%');
                });
        })
        ->orderBy('created_at', 'desc')
        ->get();

        $jumlah = count($retur_supply);
  
        // ===============================================================================
        $array = [];
        foreach ($retur_supply as $no => $trans) {
            array_push($array, $trans->no_surat_keluar);
        }
        $uniq = array_unique($array);
        $retur_pembelian = array_values($uniq);
        // ===============================================================================

        $retur_pasok = ReturSupplier::whereBetween('created_at', [$start_date2, $end_date2])
        ->orderBy('created_at', 'desc')
        ->where(function ($query) use($search_retur_pembelian){
            $query->where('no_surat_keluar', 'like', '%'.$search_retur_pembelian.'%')
                ->orWhere('nama_supplier', 'like', '%'.$search_retur_pembelian.'%')
                ->orWhereHas('product', function($query) use ($search_retur_pembelian){
                        $query->where('nama_barang', 'like', '%'.$search_retur_pembelian.'%');
                });
        })
        ->get();
        
        $retur_pasok = $retur_pasok->sortBy('created_at');
        $retur_pasok = $retur_pasok->values()->all();
        
        // ===================================== Jumlah Stok ==========================================

        $dos = 0;
        $pack = 0;
        $harga_total = 0;
        foreach ($retur_pasok as $key => $value) {
            $pp = Product::where('id', $value->id_product)->first();
            if(strtolower($value->jenis_kemasan) == 'pack'){
                $pack += $value->jumlah;
            }else{
                $dos +=  $value->jumlah;
            }

            $harga_total += $value->jumlah * $value->harga_modal;
        }
        $dos = (int) $dos ;
        $pack = (int) $pack ;

        // ==================================== ganti data pasok ===========================================

        array_walk($retur_pasok, function(&$item, $key) {
            $item->created_at = $item->created_at->format('d-m-Y H:i:s');
        });
        // ===============================================================================

        $pemasok = [];
        $jumlah_pemasok = 0;
        foreach ($retur_pasok as $key => $value) {
            if(!in_array($value->nama_supplier, $pemasok)){
                array_push($pemasok, $value->nama_supplier);
                $jumlah_pemasok++;
            } 
        }

        $tgl1 = Carbon::parse($start_date)->isoFormat('D MMMM Y');
        $tgl2 = Carbon::parse($end_date)->isoFormat('D MMMM Y');
        $tgl = $tgl1.' s/d '.$tgl2;
        return view('report.print_laporan_retur_pembelian', compact('retur_pasok', 'retur_pembelian', 'dos', 'pack', 'tgl', 'jumlah', 'jumlah_pemasok', 'harga_total'));
    }
}