mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-30 01:43:59 +00:00
146 lines
4.7 KiB
PHP
146 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Classes\Modules\Exports\Services;
|
|
|
|
use App\Classes\ValueObjects\Constants\PaymentMethodType;
|
|
use App\Classes\ValueObjects\Constants\TransactionType;
|
|
use App\Classes\ValueObjects\Constants\ApprovalStatus;
|
|
use App\Models\Transaction;
|
|
use Maatwebsite\Excel\Concerns\Exportable;
|
|
use Maatwebsite\Excel\Concerns\FromQuery;
|
|
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
|
|
use Maatwebsite\Excel\Concerns\WithHeadingRow;
|
|
use Maatwebsite\Excel\Concerns\WithHeadings;
|
|
use Maatwebsite\Excel\Concerns\WithMapping;
|
|
use Illuminate\Http\Request;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class ExportsSalesInvoiceReport implements FromQuery, WithHeadings, WithHeadingRow, WithMapping, ShouldAutoSize
|
|
{
|
|
use Exportable;
|
|
|
|
private $request;
|
|
|
|
public function __construct(Request $request)
|
|
{
|
|
$this->request = $request;
|
|
}
|
|
|
|
public function headings(): array
|
|
{
|
|
return [
|
|
'DocNo',
|
|
'DocDate',
|
|
'DebtorCode',
|
|
'Ref',
|
|
'ShipInfo',
|
|
'AccNo',
|
|
'DetailDescription',
|
|
'FurtherDescription',
|
|
'Classification',
|
|
'DeptNo',
|
|
'Qty',
|
|
'UnitPrice',
|
|
'submiteinvoice',
|
|
'ConsolidatedEinvoice',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return \Illuminate\Support\Collection|mixed
|
|
*/
|
|
public function query()
|
|
{
|
|
$start_date = $this->request->input('startDate', null);
|
|
if ($start_date) {
|
|
$start_date = Carbon::parse($this->request->input('startDate'))->format('Y-m-d');
|
|
}
|
|
|
|
$end_date = $this->request->input('endDate', null);
|
|
if ($end_date) {
|
|
$end_date = Carbon::parse($this->request->input('endDate'))->format('Y-m-d');
|
|
}
|
|
|
|
if (!$start_date) {
|
|
$start_date = Carbon::now()->subMonths(1)->startOfDay();
|
|
}
|
|
if (!$end_date) {
|
|
$end_date = Carbon::now()->endOfDay();
|
|
}
|
|
|
|
$query = Transaction::query();
|
|
|
|
$query->where('type', TransactionType::PAYMENT)->where('payment_method', '!=', PaymentMethodType::WALLET);
|
|
// $query->where('type', TransactionType::PAYMENT);
|
|
$query->whereIn('status', [ApprovalStatus::APPROVED, ApprovalStatus::COMPLETED]);
|
|
$query->whereHas('booking.transactions', function ($query) {
|
|
$query->where('type', TransactionType::PURCHASE_ORDER)->complete();
|
|
});
|
|
|
|
if($start_date && $end_date) {
|
|
$query->whereBetween('created_at', [
|
|
Carbon::parse($start_date)->format('Y-m-d 0:00:00'),
|
|
Carbon::parse($end_date)->format('Y-m-d 23:59:59')
|
|
]);
|
|
}
|
|
elseif($start_date && !$end_date) {
|
|
$query->where('created_at', '>=', Carbon::parse($start_date)->format('Y-m-d 0:00:00'));
|
|
}
|
|
elseif(!$start_date && $end_date) {
|
|
$query->where('created_at', '<=', Carbon::parse($end_date)->format('Y-m-d 23:59:59'));
|
|
}
|
|
|
|
return $query;
|
|
}
|
|
|
|
/**
|
|
* @param Transaction $transaction
|
|
* @return array
|
|
*/
|
|
public function map($transaction): array
|
|
{
|
|
$booking = $transaction->owner()->first();
|
|
$purchaseOrder = $booking->transactions()->where('type', TransactionType::PURCHASE_ORDER)->first();
|
|
//$purchaseOrder = $booking->transactions()->payments()->complete()->first();
|
|
$company = $booking->company()->first();
|
|
|
|
$lastPaymentTransaction = $booking->transactions()->where('type', TransactionType::PAYMENT)->whereIn('status', [ApprovalStatus::COMPLETED, ApprovalStatus::APPROVED])->latest()->first();
|
|
$documentDate = $lastPaymentTransaction->created_at;
|
|
if(Carbon::parse($booking->updated_at)->isAfter($lastPaymentTransaction->created_at)){
|
|
$documentDate = $booking->updated_at;
|
|
}
|
|
$formattedDocumentDate = Carbon::parse($documentDate)->format('m/d/Y');
|
|
|
|
$firstItem = true;
|
|
$records = [];
|
|
|
|
$transactionDetails = $purchaseOrder->transactionDetails;
|
|
|
|
|
|
foreach ($transactionDetails as $detail) {
|
|
$records[] = [
|
|
$firstItem ? '<<New>>' : '',
|
|
$formattedDocumentDate,
|
|
$company->debtor,
|
|
$booking->marking,
|
|
$booking->marking,
|
|
'500-0000',
|
|
'PRODUCT NAME :',
|
|
$detail->product_name,
|
|
'022',
|
|
'C',
|
|
$detail->quantity,
|
|
number_format($detail->price, 2),
|
|
$firstItem ? 'T' : '',
|
|
$company->e_invoice ? 'F' : 'T'
|
|
];
|
|
|
|
if($firstItem) {
|
|
$firstItem = false;
|
|
}
|
|
}
|
|
return $records;
|
|
}
|
|
}
|