diff --git a/app/Services/Report/ClientBalanceReport.php b/app/Services/Report/ClientBalanceReport.php index 62cfd4c1bb43..993faf89df5b 100644 --- a/app/Services/Report/ClientBalanceReport.php +++ b/app/Services/Report/ClientBalanceReport.php @@ -36,10 +36,8 @@ class ClientBalanceReport extends BaseExport 'client_name', 'client_number', 'id_number', - 'invoices', - 'amount', - 'balance', - 'total_taxes', + 'invoice_balance', + 'credit_balance', ]; /** @@ -87,7 +85,7 @@ class ClientBalanceReport extends BaseExport }); return $this->csv->toString(); - + } public function buildHeader(): array @@ -112,9 +110,8 @@ class ClientBalanceReport extends BaseExport $client->number, $client->id_number, $query->count(), - $query->sum('amount'), $query->sum('balance'), - $query->sum('total_taxes'), + $client->credit_balance, ]; } } \ No newline at end of file diff --git a/app/Services/Report/ClientSalesReport.php b/app/Services/Report/ClientSalesReport.php index 1d32b9d92994..d88fe5870688 100644 --- a/app/Services/Report/ClientSalesReport.php +++ b/app/Services/Report/ClientSalesReport.php @@ -11,11 +11,120 @@ namespace App\Services\Report; -class ClientSalesReport +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 ClientSalesReport 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', + 'amount', + 'balance', + 'total_taxes', + 'amount_paid', + ]; + + /** + @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_sales_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()); + + 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, Invoice::STATUS_PAID]); + + $query = $this->addDateRange($query); + + $amount = $query->sum('amount'); + $balance = $query->sum('balance'); + $paid = $amount-$balance; + + return [ + $client->present()->name(), + $client->number, + $client->id_number, + $query->count(), + Number::formatMoney($amount, $client), + Number::formatMoney($balance, $client), + Number::formatMoney($query->sum('total_taxes'), $client), + Number::formatMoney($amount-$balance, $client), + + ]; + } } diff --git a/app/Services/Report/UserSalesReport.php b/app/Services/Report/UserSalesReport.php index ff1182219cd5..a2ac90535090 100644 --- a/app/Services/Report/UserSalesReport.php +++ b/app/Services/Report/UserSalesReport.php @@ -67,7 +67,7 @@ class UserSalesReport extends BaseExport $query = $this->filterByClients($query); - $this->csv->insertOne([ctrans('texts.sales_report_header', ['client' => $this->client_description, 'start_date' => $this->start_date, 'end_date' => $this->end_date])]); + $this->csv->insertOne([ctrans('texts.user_sales_report_header', ['client' => $this->client_description, 'start_date' => $this->start_date, 'end_date' => $this->end_date])]); $this->csv->insertOne($this->buildHeader()); $users = $this->company->users; diff --git a/lang/en/texts.php b/lang/en/texts.php index daa40b7100ef..bc89902b391a 100644 --- a/lang/en/texts.php +++ b/lang/en/texts.php @@ -5032,8 +5032,9 @@ $LANG = array( 'quote_product_columns' => 'Quote Product Columns', 'vendors' => 'Vendors', 'product_sales' => 'Product Sales', - 'sales_report_header' => 'User sales report for client/s :client from :start_date to :end_date', - 'customer_balance_report' => 'Customer balance report', + 'user_sales_report_header' => 'User sales report for client/s :client from :start_date to :end_date', + 'client_balance_report' => 'Customer balance report', + 'client_sales_report' => 'Customer sales report', 'user_sales_report' => 'User sales report', );