mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-03 19:30:07 -05:00 
			
		
		
		
	
		
			
				
	
	
		
			122 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			122 lines
		
	
	
		
			3.1 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
/**
 | 
						|
 * Invoice Ninja (https://invoiceninja.com).
 | 
						|
 *
 | 
						|
 * @link https://github.com/invoiceninja/invoiceninja source repository
 | 
						|
 *
 | 
						|
 * @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
 | 
						|
 *
 | 
						|
 * @license https://www.elastic.co/licensing/elastic-license
 | 
						|
 */
 | 
						|
 | 
						|
namespace App\Services\Report;
 | 
						|
 | 
						|
use App\Utils\Ninja;
 | 
						|
use App\Models\Client;
 | 
						|
use League\Csv\Writer;
 | 
						|
use App\Models\Company;
 | 
						|
use App\Models\Invoice;
 | 
						|
use App\Libraries\MultiDB;
 | 
						|
use App\Export\CSV\BaseExport;
 | 
						|
use App\Utils\Traits\MakesDates;
 | 
						|
use Illuminate\Support\Facades\App;
 | 
						|
 | 
						|
class ClientBalanceReport extends BaseExport
 | 
						|
{
 | 
						|
    use MakesDates;
 | 
						|
    //Name
 | 
						|
    //Invoice count
 | 
						|
    //Amount
 | 
						|
    //Amount with Tax
 | 
						|
    public Writer $csv;
 | 
						|
    
 | 
						|
    public string $date_key = 'created_at';
 | 
						|
 | 
						|
    public array $report_keys = [
 | 
						|
        'client_name',
 | 
						|
        'client_number',
 | 
						|
        'id_number',
 | 
						|
        'invoices',
 | 
						|
        'invoice_balance',
 | 
						|
        'credit_balance',
 | 
						|
    ];
 | 
						|
 | 
						|
    /**
 | 
						|
        @param array $input
 | 
						|
        [
 | 
						|
            'date_range',
 | 
						|
            'start_date',
 | 
						|
            'end_date',
 | 
						|
            'clients',
 | 
						|
            'client_id',
 | 
						|
        ]
 | 
						|
    */
 | 
						|
    public function __construct(public Company $company, public array $input)
 | 
						|
    {
 | 
						|
    }
 | 
						|
 | 
						|
    public function run()
 | 
						|
    {
 | 
						|
        MultiDB::setDb($this->company->db);
 | 
						|
        App::forgetInstance('translator');
 | 
						|
        App::setLocale($this->company->locale());
 | 
						|
        $t = app('translator');
 | 
						|
        $t->replace(Ninja::transformTranslations($this->company->settings));
 | 
						|
 | 
						|
        $this->csv = Writer::createFromString();
 | 
						|
        
 | 
						|
        $this->csv->insertOne([]);
 | 
						|
        $this->csv->insertOne([]);
 | 
						|
        $this->csv->insertOne([]);
 | 
						|
        $this->csv->insertOne([]);
 | 
						|
        $this->csv->insertOne([ctrans('texts.client_balance_report')]);
 | 
						|
        $this->csv->insertOne([ctrans('texts.created_on'),' ',$this->translateDate(now()->format('Y-m-d'), $this->company->date_format(), $this->company->locale())]);
 | 
						|
 | 
						|
        if (count($this->input['report_keys']) == 0) {
 | 
						|
            $this->input['report_keys'] = $this->report_keys;
 | 
						|
        }
 | 
						|
 | 
						|
        $this->csv->insertOne($this->buildHeader());
 | 
						|
 | 
						|
        Client::query()
 | 
						|
            ->where('company_id', $this->company->id)
 | 
						|
            ->where('is_deleted', 0)
 | 
						|
            ->orderBy('balance', 'desc')
 | 
						|
            ->cursor()
 | 
						|
            ->each(function ($client){
 | 
						|
                
 | 
						|
                $this->csv->insertOne($this->buildRow($client));
 | 
						|
                
 | 
						|
            });
 | 
						|
 | 
						|
        return $this->csv->toString();
 | 
						|
 | 
						|
    }
 | 
						|
 | 
						|
    public function buildHeader(): array
 | 
						|
    {
 | 
						|
        $headers = [];
 | 
						|
 | 
						|
        foreach($this->report_keys as $key)
 | 
						|
            $headers[] = ctrans("texts.{$key}");
 | 
						|
 | 
						|
        return $headers;
 | 
						|
 | 
						|
    }
 | 
						|
    private function buildRow(Client $client): array
 | 
						|
    {
 | 
						|
        $query = Invoice::where('client_id', $client->id)
 | 
						|
                                ->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]);
 | 
						|
    
 | 
						|
        $query = $this->addDateRange($query);
 | 
						|
 | 
						|
        return [
 | 
						|
            $client->present()->name(),
 | 
						|
            $client->number,
 | 
						|
            $client->id_number,
 | 
						|
            $query->count(),
 | 
						|
            $query->sum('balance'),
 | 
						|
            $client->credit_balance,
 | 
						|
        ];
 | 
						|
    }
 | 
						|
} |