diff --git a/app/Services/Report/ARDetailReport.php b/app/Services/Report/ARDetailReport.php index b8b4c4031b24..0492e321d0cc 100644 --- a/app/Services/Report/ARDetailReport.php +++ b/app/Services/Report/ARDetailReport.php @@ -11,14 +11,114 @@ namespace App\Services\Report; -class ARDetailReport -{ -//Date -//Invoice # -//Status -//Customer -//Age - Days -//Amount -//Balance +use Carbon\Carbon; +use App\Utils\Ninja; +use App\Utils\Number; +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 ARDetailReport extends BaseExport +{ + use MakesDates; + //Date + //Invoice # + //Status + //Customer + //Age - Days + //Amount + //Balance + + public Writer $csv; + + public string $date_key = 'created_at'; + + public array $report_keys = [ + 'date', + 'invoice_number', + 'status', + 'client_name', + 'client_number', + 'id_number', + 'age', + 'amount', + '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.aged_receivable_detailed_report')]); + $this->csv->insertOne([ctrans('texts.created_on'),' ',$this->translateDate(now()->format('Y-m-d'), $this->company->date_format(), $this->company->locale())]); + + $this->csv->insertOne($this->buildHeader()); + + $query = Invoice::query() + ->withTrashed() + ->where('company_id', $this->company->id) + ->where('is_deleted', 0) + ->where('balance', '>', 0) + ->orderBy('due_date', 'ASC') + ->whereIn('status_id', [Invoice::STATUS_SENT, Invoice::STATUS_PARTIAL]); + + $query = $this->addDateRange($query); + + $query = $this->filterByClients($query); + + $this->csv->insertOne($this->buildHeader()); + + $query->cursor() + ->each(function ($invoice) { + $this->csv->insertOne($this->buildRow($invoice)); + }); + + return $this->csv->toString(); + } + + private function buildRow(Invoice $invoice): array + { + $client = $invoice->client; + + return [ + $this->translateDate($invoice->due_date, $this->company->date_format(), $this->company->locale()), + $invoice->number, + $invoice->stringStatus($invoice->status_id), + $client->present()->name(), + $client->number, + $client->id_number, + Carbon::parse($invoice->due_date)->diffInDays(now()), + Number::formatMoney($invoice->amount, $client), + Number::formatMoney($invoice->balance, $client), + ]; + } +} \ No newline at end of file diff --git a/app/Services/Report/ClientSalesReport.php b/app/Services/Report/ClientSalesReport.php index d88fe5870688..7845545cd74a 100644 --- a/app/Services/Report/ClientSalesReport.php +++ b/app/Services/Report/ClientSalesReport.php @@ -76,6 +76,10 @@ class ClientSalesReport extends BaseExport $this->csv->insertOne([ctrans('texts.client_sales_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() @@ -93,17 +97,6 @@ class ClientSalesReport extends BaseExport } - 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) diff --git a/app/Services/Report/UserSalesReport.php b/app/Services/Report/UserSalesReport.php index a2ac90535090..71cec43081d4 100644 --- a/app/Services/Report/UserSalesReport.php +++ b/app/Services/Report/UserSalesReport.php @@ -33,6 +33,12 @@ class UserSalesReport extends BaseExport public string $date_key = 'created_at'; + public array $report_keys = [ + 'name', + 'invoices', + 'invoice_amount', + 'total_taxes', + ]; /** @param array $input [ @@ -68,6 +74,11 @@ class UserSalesReport extends BaseExport $query = $this->filterByClients($query); $this->csv->insertOne([ctrans('texts.user_sales_report_header', ['client' => $this->client_description, 'start_date' => $this->start_date, 'end_date' => $this->end_date])]); + + if (count($this->input['report_keys']) == 0) { + $this->input['report_keys'] = $this->report_keys; + } + $this->csv->insertOne($this->buildHeader()); $users = $this->company->users; @@ -95,14 +106,5 @@ class UserSalesReport extends BaseExport } - public function buildHeader(): array - { - return [ - ctrans('texts.name'), - ctrans('texts.invoices'), - ctrans('texts.invoice_amount'), - ctrans('texts.total_taxes'), - ]; - } } diff --git a/lang/en/texts.php b/lang/en/texts.php index bc89902b391a..354672f0fd25 100644 --- a/lang/en/texts.php +++ b/lang/en/texts.php @@ -5036,7 +5036,8 @@ $LANG = array( 'client_balance_report' => 'Customer balance report', 'client_sales_report' => 'Customer sales report', 'user_sales_report' => 'User sales report', - + 'aged_receivable_detailed_report' => 'Aged Receivable Detailed Report', + 'aged_receivable_summary_report' => 'Aged Receivable Summary Report', );