KUJUNTI.ID MINISH3LL
Path : /var/www/html/jewelry-pos/app/Exports/
(S)h3ll Cr3at0r :
F!le Upl0ad :

B-Con CMD Config cPanel C-Rdp D-Log Info Jump Mass Ransom Symlink vHost Zone-H

Current File : /var/www/html/jewelry-pos/app/Exports/ProductTransactionReportExport.php


<?php

namespace App\Exports;

use App\Models\QtyHistory;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;

class ProductTransactionReportExport implements FromQuery , WithMapping , WithHeadings
{
    /**
    * @return \Illuminate\Support\Collection
    */
//    public function collection()
//    {
//        //
//    }
    private $no = 0;
    private $q;
    private $in_out;
    private $paginate;
    private $start_date;
    private $end_date;
    private $date;
    private $for_sale;

    public function __construct($for_sale,$start_date , $end_date , $paginate , $in_out , $q , $date)
    {
        $this->q = $q;
        $this->paginate = $paginate??25;
        $this->in_out = $in_out;
        $this->start_date = $start_date;
        $this->date = $date;
        $this->for_sale = $for_sale;
    }

    /**
     * @return \Illuminate\Support\Collection
     */

    public function headings(): array
    {
        if($this->for_sale == 0){
            return [
                'no',
                __('message.part number'),
                __('message.product category'),
                __('message.name'),
                __('message.weight'),
                __('message.weight before transaction'),
                __('message.weight after transaction'),
                __('message.note'),
                __('message.user'),
                __('message.updated at'),
            ];
        }else{
            return [
                'no',
                __('message.part number'),
                __('message.product category'),
                __('message.name'),
                __('message.qty'),
                __('message.qty before transaction'),
                __('message.qty after transaction'),
                __('message.note'),
                __('message.user'),
                __('message.updated at'),
            ];
        }
        return [];
    }
    public function query()
    {
        $paginate = $this->paginate??25;
        $for_sale = $this->for_sale;
        $q = $this->q??null;
        $start_date = $this->start_date ?? date('Y-m-d');
        $end_date = $this->end_date ?? date('Y-m-d');
        $dates = explode(' - ', $this->date);
        if ($this->date) {
            $start_date = $dates[0];
            $end_date = $dates[1];
        }
        $productQtyHistories = QtyHistory::whereBetween('created_at' , [$start_date . " 00:00:00" , $end_date . " 23:59:59"])
            ->whereHas('product', function ($query) use ($for_sale) {  // Filter based on related product's 'for_sale' column
                $query->where('for_sale', $for_sale);
            })
            ->orderBy('id' , 'desc');
        if($q){
            $productQtyHistories = $productQtyHistories->where('grn' , $q);
        }
        if($this->in_out > 0){
            $productQtyHistories = $productQtyHistories->where('qty' , '>' , 0);
        }else if($this->in_out < 0){
            $productQtyHistories = $productQtyHistories->where('qty' , '<' , 0);
        }else{
            $productQtyHistories = $productQtyHistories;
        }
        return $productQtyHistories;
    }

    public function map($productQtyHistories): array
    {
        $this->no++;
        if($this->for_sale == 0){
            return [
                $this->no,
                $productQtyHistories->product->part_number,
                $productQtyHistories->product->productCategory->name??'',
                $productQtyHistories->product->name??'',
                $productQtyHistories->weight,
                $productQtyHistories->weight_before_transaction,
                $productQtyHistories->weight_after_transaction,
                $productQtyHistories->note,
                $productQtyHistories->user->name??'',
                $productQtyHistories->created_at->format('d/m/y H:i'),
            ];
        }else{
            return [
                $this->no,
                $productQtyHistories->product->part_number,
                $productQtyHistories->product->productCategory->name??'',
                $productQtyHistories->product->name??'',
                $productQtyHistories->qty,
                $productQtyHistories->qty_before_transaction,
                $productQtyHistories->qty_after_transaction,
                $productQtyHistories->note,
                $productQtyHistories->user->name??'',
                $productQtyHistories->created_at->format('d/m/y H:i'),
            ];
        }
    }

}

© KUJUNTI.ID