Merge Invoice and Bill into Document
This commit is contained in:
		
							
								
								
									
										75
									
								
								app/Exports/Document/Sheets/DocumentTransactions.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										75
									
								
								app/Exports/Document/Sheets/DocumentTransactions.php
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,75 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
namespace App\Exports\Document\Sheets;
 | 
			
		||||
 | 
			
		||||
use App\Abstracts\Export;
 | 
			
		||||
use App\Models\Banking\Transaction as Model;
 | 
			
		||||
use App\Models\Document\Document;
 | 
			
		||||
use Illuminate\Support\Str;
 | 
			
		||||
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
 | 
			
		||||
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
 | 
			
		||||
 | 
			
		||||
class DocumentTransactions extends Export implements WithColumnFormatting
 | 
			
		||||
{
 | 
			
		||||
    public function collection()
 | 
			
		||||
    {
 | 
			
		||||
        $model = Model::with('account', 'category', 'contact', 'document')->income()->isDocument()->usingSearchString(request('search'));
 | 
			
		||||
 | 
			
		||||
        if (!empty($this->ids)) {
 | 
			
		||||
            $model->whereIn('document_id', (array) $this->ids);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $model->cursor();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function map($model): array
 | 
			
		||||
    {
 | 
			
		||||
        $document = $model->document;
 | 
			
		||||
 | 
			
		||||
        if (empty($document)) {
 | 
			
		||||
            return [];
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if ($this->type === Document::INVOICE_TYPE) {
 | 
			
		||||
            $model->invoice_number = $document->document_number;
 | 
			
		||||
        } else {
 | 
			
		||||
            $model->bill_number = $document->document_number;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        $model->account_name = $model->account->name;
 | 
			
		||||
        $model->category_name = $model->category->name;
 | 
			
		||||
        $model->contact_email = $model->contact->email;
 | 
			
		||||
 | 
			
		||||
        return parent::map($model);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function fields(): array
 | 
			
		||||
    {
 | 
			
		||||
        return [
 | 
			
		||||
            $this->type === Document::INVOICE_TYPE ? 'invoice_number' : 'bill_number',
 | 
			
		||||
            'paid_at',
 | 
			
		||||
            'amount',
 | 
			
		||||
            'currency_code',
 | 
			
		||||
            'currency_rate',
 | 
			
		||||
            'account_name',
 | 
			
		||||
            'contact_email',
 | 
			
		||||
            'category_name',
 | 
			
		||||
            'description',
 | 
			
		||||
            'payment_method',
 | 
			
		||||
            'reference',
 | 
			
		||||
            'reconciled',
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function columnFormats(): array
 | 
			
		||||
    {
 | 
			
		||||
        return [
 | 
			
		||||
            'B' => NumberFormat::FORMAT_DATE_YYYYMMDD,
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function title(): string
 | 
			
		||||
    {
 | 
			
		||||
        return Str::replaceFirst('document', $this->type, parent::title());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user