This repository has been archived by the owner on Feb 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 129
feat: add billing #855
Merged
Merged
feat: add billing #855
Changes from 18 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
c0a1b73
feat: add billing
djaiss bf299b2
wip
djaiss 02d0d4b
Merge branch 'main' into 2021-05-08-account
djaiss 4a52928
wip
djaiss 129d0e6
Update 2021_05_07_191513_create_company_usage_history_table.php
djaiss 628aac6
wip
djaiss d9a8e04
Update LogDailyMaxNumberOfActiveEmployeesInCompaniesTest.php
djaiss 21adb26
wip
djaiss 1706a93
Update LogDailyMaxNumberOfActiveEmployeesInCompanies.php
djaiss cef0ac5
Merge branch 'main' into 2021-05-08-account
djaiss 38fb744
wip
djaiss ae0e200
wip
djaiss e135380
fixes
djaiss 29fe142
fixes
djaiss 734075c
fixes
djaiss a3b9058
fixes
djaiss cbdca23
fixes
djaiss 4db17fb
fix
djaiss c5790da
Update app/Jobs/Invoicing/CreateMonthlyInvoiceForCompanies.php
djaiss b8fa002
Update app/Jobs/Invoicing/LogDailyMaxNumberOfActiveEmployeesInCompani…
djaiss 3bdf53f
Update database/factories/Company/CompanyInvoiceFactory.php
djaiss 9b186d3
test
djaiss 86c74a5
Update CreateMonthlyInvoiceForCompaniesTest.php
djaiss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,54 @@ | ||
<?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 a new job instance. | ||
*/ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
djaiss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* 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) | ||
djaiss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
->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, | ||
]); | ||
} | ||
}); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
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,62 @@ | ||
<?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; | ||
|
||
/** | ||
* Create a new job instance. | ||
*/ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
djaiss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* 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) | ||
djaiss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
->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, | ||
]; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the same as the commented line below