Skip to content

Commit

Permalink
fix export format and values, and fix money format utility
Browse files Browse the repository at this point in the history
  • Loading branch information
roncodes committed May 17, 2024
1 parent 4e40f0e commit 12deee3
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 40 deletions.
25 changes: 18 additions & 7 deletions src/Exports/ApiCredentialExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,31 @@

use Fleetbase\Models\ApiCredential;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

class ApiCredentialExport implements FromCollection, WithHeadings, WithMapping, WithColumnFormatting
class ApiCredentialExport implements FromCollection, WithHeadings, WithMapping, WithColumnFormatting, ShouldAutoSize
{
protected array $selections = [];

public function __construct(array $selections = [])
{
$this->selections = $selections;
}

public function map($apiCredential): array
{
return [
$apiCredential->name,
$apiCredential->key,
$apiCredential->secret,
$apiCredential->test_mode ? 'Test' : 'Live',
$apiCredential->expires_at ? Date::dateTimeToExcel($apiCredential->expires_at) : 'Never',
$apiCredential->last_used_at ? Date::dateTimeToExcel($apiCredential->last_used_at) : 'Never',
Date::dateTimeToExcel($apiCredential->created_at),
$apiCredential->expires_at ? $apiCredential->expires_at : 'Never',
$apiCredential->last_used_at ? $apiCredential->last_used_at : 'Never',
$apiCredential->created_at,
];
}

Expand All @@ -32,9 +39,9 @@ public function headings(): array
'Public Key',
'Secret Key',
'Environment',
'Expiry',
'Expiry Date',
'Last Used',
'Created',
'Date Created',
];
}

Expand All @@ -52,6 +59,10 @@ public function columnFormats(): array
*/
public function collection()
{
if ($this->selections) {
return ApiCredential::whereIn('uuid', $this->selections)->get();
}

return ApiCredential::where('company_uuid', session('company'))->get();
}
}
17 changes: 8 additions & 9 deletions src/Exports/CompanyExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

use Fleetbase\Models\Company;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

class CompanyExport implements FromCollection, WithHeadings, WithMapping, WithColumnFormatting
class CompanyExport implements FromCollection, WithHeadings, WithMapping, WithColumnFormatting, ShouldAutoSize
{
protected array $selections = [];

Expand All @@ -26,8 +26,8 @@ public function map($company): array
data_get($company, 'owner.name'),
data_get($company, 'owner.email'),
data_get($company, 'owner.phone'),
data_get($company, 'owner.user'),
$company->created_at ? Date::dateTimeToExcel($company->created_at) : 'N/A',
$company->companyUsers ? $company->companyUsers->count() : $company->companyUsers()->count(),
$company->created_at,
];
}

Expand All @@ -38,17 +38,16 @@ public function headings(): array
'Owner',
'Email',
'Phone',
'User',
'Users Count',
'Created',
];
}

public function columnFormats(): array
{
return [
'E' => NumberFormat::FORMAT_DATE_DDMMYYYY,
'D' => '+#',
'F' => NumberFormat::FORMAT_DATE_DDMMYYYY,
'G' => NumberFormat::FORMAT_DATE_DDMMYYYY,
];
}

Expand All @@ -58,9 +57,9 @@ public function columnFormats(): array
public function collection()
{
if ($this->selections) {
return Company::where('owner_uuid', session('user'))->whereIn('uuid', $this->selections)->get();
return Company::whereIn('uuid', $this->selections)->with(['companyUsers'])->get();
}

return Company::where('owner_uuid', session('user'))->get();
return Company::with(['companyUsers'])->get();
}
}
29 changes: 16 additions & 13 deletions src/Exports/GroupExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,41 @@

use Fleetbase\Models\Group;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

class GroupExport implements FromCollection, WithHeadings, WithMapping, WithColumnFormatting
class GroupExport implements FromCollection, WithHeadings, WithMapping, WithColumnFormatting, ShouldAutoSize
{
protected array $selections = [];

public function __construct(array $selections = [])
{
$this->selections = $selections;
}

public function map($group): array
{
return [
$group->name,
$group->company_name,
$group->last_login,
$group->email_verified_at ? Date::dateTimeToExcel($group->email_verified_at) : 'Never',
Date::dateTimeToExcel($group->created_at),
$group->created_at,
];
}

public function headings(): array
{
return [
'Name',
'Company',
'Last Login',
'Email Verified At',
'Created',
'Date Created',
];
}

public function columnFormats(): array
{
return [
'E' => NumberFormat::FORMAT_DATE_DDMMYYYY,
'F' => NumberFormat::FORMAT_DATE_DDMMYYYY,
'G' => NumberFormat::FORMAT_DATE_DDMMYYYY,
'B' => NumberFormat::FORMAT_DATE_DDMMYYYY,
];
}

Expand All @@ -48,6 +47,10 @@ public function columnFormats(): array
*/
public function collection()
{
if ($this->selections) {
return Group::whereIn('uuid', $this->selections)->get();
}

return Group::where('company_uuid', session('company'))->get();
}
}
27 changes: 19 additions & 8 deletions src/Exports/UserExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

use Fleetbase\Models\User;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;

class UserExport implements FromCollection, WithHeadings, WithMapping, WithColumnFormatting
class UserExport implements FromCollection, WithHeadings, WithMapping, WithColumnFormatting, ShouldAutoSize
{
protected array $selections = [];

Expand All @@ -24,9 +24,14 @@ public function map($user): array
return [
$user->name,
$user->company_name,
$user->email,
$user->phone,
$user->country,
$user->timezone,
$user->ip_address,
$user->last_login,
$user->email_verified_at ? Date::dateTimeToExcel($user->email_verified_at) : 'Never',
Date::dateTimeToExcel($user->created_at),
$user->email_verified_at ? $user->email_verified_at : 'Never',
$user->created_at,
];
}

Expand All @@ -35,18 +40,24 @@ public function headings(): array
return [
'Name',
'Company',
'Email',
'Phone',
'Country',
'Timezone',
'IP Address',
'Last Login',
'Email Verified At',
'Created',
'Date Created',
];
}

public function columnFormats(): array
{
return [
'E' => NumberFormat::FORMAT_DATE_DDMMYYYY,
'F' => NumberFormat::FORMAT_DATE_DDMMYYYY,
'G' => NumberFormat::FORMAT_DATE_DDMMYYYY,
'D' => '+#',
'H' => NumberFormat::FORMAT_DATE_DDMMYYYY,
'I' => NumberFormat::FORMAT_DATE_DDMMYYYY,
'J' => NumberFormat::FORMAT_DATE_DDMMYYYY,
];
}

Expand Down
1 change: 1 addition & 0 deletions src/Http/Resources/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function toArray($request)
'is_admin' => $this->when(Http::isInternalRequest(), $this->is_admin),
'is_online' => $this->is_online,
'last_seen_at' => $this->last_seen_at,
'last_login' => $this->last_login,
'updated_at' => $this->updated_at,
'created_at' => $this->created_at,
];
Expand Down
5 changes: 2 additions & 3 deletions src/Support/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -703,13 +703,12 @@ public static function removeSpecialCharacters($string, $except = [])
*
* @param float $amount amount to format
* @param string $currency the currency to format into
* @param bool $cents whether if amount is in cents, this will auto divide by 100
*
* @return string
*/
public static function moneyFormat($amount, $currency = 'USD', $cents = true)
public static function moneyFormat($amount, $currency = 'USD')
{
$amount = $cents === true ? static::numbersOnly($amount) / 100 : $amount;
$amount = static::numbersOnly($amount);
$money = new \Cknow\Money\Money($amount, $currency);

return $money->format();
Expand Down

0 comments on commit 12deee3

Please sign in to comment.