-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: record daily account usage for each account (#855)
- Loading branch information
Showing
19 changed files
with
642 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace App\Jobs\Invoicing; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Bus\Queueable; | ||
use App\Models\Company\Company; | ||
use App\Models\Company\CompanyInvoice; | ||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use App\Models\Company\CompanyDailyUsageHistory; | ||
|
||
class CreateMonthlyInvoiceForCompanies implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
/** | ||
* Create the monthly invoice for the company, based on the usage in the | ||
* account. | ||
*/ | ||
public function handle(): void | ||
{ | ||
if (! config('officelife.enable_paid_plan')) { | ||
return; | ||
} | ||
|
||
Company::chunk(100, function ($companies) { | ||
foreach ($companies as $company) { | ||
$usage = CompanyDailyUsageHistory::where('company_id', $company->id) | ||
->whereBetween('created_at', [Carbon::now()->startOfMonth(), Carbon::now()->endOfMonth()]) | ||
->orderBy('number_of_active_employees', 'desc') | ||
->first(); | ||
|
||
if (! $usage) { | ||
continue; | ||
} | ||
|
||
CompanyInvoice::create([ | ||
'company_id' => $company->id, | ||
'usage_history_id' => $usage->id, | ||
]); | ||
} | ||
}); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
app/Jobs/Invoicing/LogDailyMaxNumberOfActiveEmployeesInCompanies.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace App\Jobs\Invoicing; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use App\Models\Company\Company; | ||
use App\Models\Company\Employee; | ||
use Illuminate\Queue\SerializesModels; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Bus\Dispatchable; | ||
use App\Models\Company\CompanyDailyUsageHistory; | ||
use App\Models\Company\CompanyUsageHistoryDetails; | ||
|
||
class LogDailyMaxNumberOfActiveEmployeesInCompanies implements ShouldQueue | ||
{ | ||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; | ||
|
||
/** | ||
* Record the number of active employee for all the companies in the | ||
* instance. | ||
*/ | ||
public function handle(): void | ||
{ | ||
if (! config('officelife.enable_paid_plan')) { | ||
return; | ||
} | ||
|
||
Company::addSelect([ | ||
'max_employees' => Employee::selectRaw('count(*)') | ||
->whereColumn('company_id', 'companies.id') | ||
->where('locked', false), | ||
]) | ||
->chunk(100, function ($companies) { | ||
foreach ($companies as $company) { | ||
$usage = CompanyDailyUsageHistory::create([ | ||
'company_id' => $company->id, | ||
'number_of_active_employees' => $company->max_employees, | ||
]); | ||
|
||
Employee::where('company_id', $company->id) | ||
->where('locked', false) | ||
->chunk(100, function ($employees) use ($usage) { | ||
foreach ($employees as $employee) { | ||
CompanyUsageHistoryDetails::create([ | ||
'usage_history_id' => $usage->id, | ||
'employee_name' => $employee->name, | ||
'employee_email' => $employee->email, | ||
]); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace App\Models\Company; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\HasMany; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
|
||
class CompanyDailyUsageHistory extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $table = 'company_daily_usage_history'; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = [ | ||
'company_id', | ||
'number_of_active_employees', | ||
'created_at', | ||
]; | ||
|
||
/** | ||
* Get the Company record associated with the company usage history object. | ||
* | ||
* @return BelongsTo | ||
*/ | ||
public function company() | ||
{ | ||
return $this->belongsTo(Company::class); | ||
} | ||
|
||
/** | ||
* Get the company usage history details records associated with the company | ||
* usage history. | ||
* | ||
* @return HasMany | ||
*/ | ||
public function details() | ||
{ | ||
return $this->hasMany(CompanyUsageHistoryDetails::class, 'usage_history_id', 'id'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace App\Models\Company; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
|
||
class CompanyInvoice extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $table = 'company_invoices'; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = [ | ||
'company_id', | ||
'usage_history_id', | ||
'sent_to_customer', | ||
'customer_has_paid', | ||
'email_address_invoice_sent_to', | ||
]; | ||
|
||
/** | ||
* The attributes that should be cast to native types. | ||
* | ||
* @var array | ||
*/ | ||
protected $casts = [ | ||
'sent_to_customer' => 'boolean', | ||
'customer_has_paid' => 'boolean', | ||
]; | ||
|
||
/** | ||
* Get the Company record associated with the company invoice object. | ||
* | ||
* @return BelongsTo | ||
*/ | ||
public function company() | ||
{ | ||
return $this->belongsTo(Company::class); | ||
} | ||
|
||
/** | ||
* Get the Company usage history record associated with the company invoice | ||
* object. | ||
* | ||
* @return BelongsTo | ||
*/ | ||
public function companyUsageHistory() | ||
{ | ||
return $this->belongsTo(CompanyDailyUsageHistory::class, 'usage_history_id'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace App\Models\Company; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
|
||
class CompanyUsageHistoryDetails extends Model | ||
{ | ||
use HasFactory; | ||
|
||
protected $table = 'company_usage_history_details'; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = [ | ||
'usage_history_id', | ||
'employee_name', | ||
'employee_email', | ||
]; | ||
|
||
/** | ||
* Get the Company usage history record associated with the company | ||
* usage history detail. | ||
* | ||
* @return BelongsTo | ||
*/ | ||
public function companyUsageHistory() | ||
{ | ||
return $this->belongsTo(CompanyDailyUsageHistory::class, 'usage_history_id'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
database/factories/Company/CompanyDailyUsageHistoryFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Database\Factories\Company; | ||
|
||
use App\Models\Company\Company; | ||
use App\Models\Company\CompanyDailyUsageHistory; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
class CompanyDailyUsageHistoryFactory extends Factory | ||
{ | ||
/** | ||
* The name of the factory's corresponding model. | ||
* | ||
* @var string | ||
*/ | ||
protected $model = CompanyDailyUsageHistory::class; | ||
|
||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array | ||
*/ | ||
public function definition() | ||
{ | ||
return [ | ||
'company_id' => Company::factory(), | ||
'number_of_active_employees' => 3, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Database\Factories\Company; | ||
|
||
use App\Models\Company\Company; | ||
use App\Models\Company\CompanyInvoice; | ||
use App\Models\Company\CompanyDailyUsageHistory; | ||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
|
||
class CompanyInvoiceFactory extends Factory | ||
{ | ||
/** | ||
* The name of the factory's corresponding model. | ||
* | ||
* @var string | ||
*/ | ||
protected $model = CompanyInvoice::class; | ||
|
||
/** | ||
* Define the model's default state. | ||
* | ||
* @return array | ||
*/ | ||
public function definition() | ||
{ | ||
return [ | ||
'company_id' => Company::factory(), | ||
'usage_history_id' => function (array $attributes) { | ||
return CompanyDailyUsageHistory::factory()->create([ | ||
'company_id' => $attributes['company_id'], | ||
]); | ||
}, | ||
'sent_to_customer' => false, | ||
'customer_has_paid' => false, | ||
'email_address_invoice_sent_to' => $this->faker->email, | ||
]; | ||
} | ||
} |
Oops, something went wrong.