Tests for emailing client statements

This commit is contained in:
David Bomba 2023-01-17 11:00:12 +11:00
parent 67dfe8a4b9
commit c51dd313b9
11 changed files with 172 additions and 91 deletions

View File

@ -12,15 +12,16 @@
namespace App\DataMapper; namespace App\DataMapper;
/** /**
* ClientSettings. * BaseSettings.
*/ */
class BaseSettings class BaseSettings
{ {
//@deprecated
public function __construct($obj) public function __construct($obj)
{ {
foreach ($obj as $key => $value) { // foreach ($obj as $key => $value) {
$obj->{$key} = $value; // $obj->{$key} = $value;
} // }
} }
public static function setCasts($obj, $casts) public static function setCasts($obj, $casts)
@ -57,7 +58,4 @@ class BaseSettings
} }
} }
public static function castSingleAttribute($key, $data)
{
}
} }

View File

@ -734,8 +734,9 @@ class CompanySettings extends BaseSettings
* and always ensure an up to date class is returned. * and always ensure an up to date class is returned.
* *
* @param $obj * @param $obj
* @deprecated
*/ */
public function __construct($obj) public function __construct()
{ {
// parent::__construct($obj); // parent::__construct($obj);
} }

View File

@ -109,16 +109,24 @@ class ClientStatementController extends BaseController
*/ */
public function statement(CreateStatementRequest $request) public function statement(CreateStatementRequest $request)
{ {
$send_email = false;
if($request->has('send_email') && $request->send_email == 'true')
$send_email = true;
$pdf = $request->client()->service()->statement( $pdf = $request->client()->service()->statement(
$request->only(['start_date', 'end_date', 'show_payments_table', 'show_aging_table', 'status']) $request->only(['start_date', 'end_date', 'show_payments_table', 'show_aging_table', 'status']), $send_email
); );
if($send_email)
return response()->json(['message' => ctrans('texts.email_queued')], 200);
if ($pdf) { if ($pdf) {
return response()->streamDownload(function () use ($pdf) { return response()->streamDownload(function () use ($pdf) {
echo $pdf; echo $pdf;
}, ctrans('texts.statement').'.pdf', ['Content-Type' => 'application/pdf']); }, ctrans('texts.statement').'.pdf', ['Content-Type' => 'application/pdf']);
} }
return response()->json(['message' => 'Something went wrong. Please check logs.']); return response()->json(['message' => ctrans('texts.error_title')], 500);
} }
} }

View File

@ -16,7 +16,6 @@ use App\Factory\CreditFactory;
use App\Factory\InvoiceFactory; use App\Factory\InvoiceFactory;
use App\Factory\QuoteFactory; use App\Factory\QuoteFactory;
use App\Factory\RecurringInvoiceFactory; use App\Factory\RecurringInvoiceFactory;
use App\Http\Requests\Invoice\StoreInvoiceRequest;
use App\Http\Requests\Preview\PreviewInvoiceRequest; use App\Http\Requests\Preview\PreviewInvoiceRequest;
use App\Jobs\Util\PreviewPdf; use App\Jobs\Util\PreviewPdf;
use App\Libraries\MultiDB; use App\Libraries\MultiDB;
@ -44,7 +43,6 @@ use App\Utils\Traits\MakesInvoiceHtml;
use App\Utils\Traits\Pdf\PageNumbering; use App\Utils\Traits\Pdf\PageNumbering;
use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Response; use Illuminate\Support\Facades\Response;
use Turbo124\Beacon\Facades\LightLogs; use Turbo124\Beacon\Facades\LightLogs;
@ -109,8 +107,6 @@ class PreviewController extends BaseController
$class = "App\Models\\$entity"; $class = "App\Models\\$entity";
$pdf_class = "App\Jobs\\$entity\\Create{$entity}Pdf";
$entity_obj = $class::whereId($this->decodePrimaryKey(request()->input('entity_id')))->company()->first(); $entity_obj = $class::whereId($this->decodePrimaryKey(request()->input('entity_id')))->company()->first();
if (! $entity_obj) { if (! $entity_obj) {

View File

@ -12,15 +12,12 @@
namespace App\Models; namespace App\Models;
use App\DataMapper\ClientSettings; use App\DataMapper\ClientSettings;
use App\DataMapper\CompanySettings;
use App\Jobs\Entity\CreateEntityPdf;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use App\Utils\Traits\UserSessionAttributes; use App\Utils\Traits\UserSessionAttributes;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException as ModelNotFoundException;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str; use Illuminate\Support\Str;
@ -115,17 +112,17 @@ class BaseModel extends Model
* reference to the parent class. * reference to the parent class.
* *
* @param $key The key of property * @param $key The key of property
* @return * @deprecated
*/ */
public function getSettingsByKey($key) // public function getSettingsByKey($key)
{ // {
/* Does Setting Exist @ client level */ // /* Does Setting Exist @ client level */
if (isset($this->getSettings()->{$key})) { // if (isset($this->getSettings()->{$key})) {
return $this->getSettings()->{$key}; // return $this->getSettings()->{$key};
} else { // } else {
return (new CompanySettings($this->company->settings))->{$key}; // return (new CompanySettings($this->company->settings))->{$key};
} // }
} // }
public function setSettingsByEntity($entity, $settings) public function setSettingsByEntity($entity, $settings)
{ {

View File

@ -15,23 +15,28 @@ use App\Models\Client;
use App\Models\Credit; use App\Models\Credit;
use App\Services\Client\Merge; use App\Services\Client\Merge;
use App\Services\Client\PaymentMethod; use App\Services\Client\PaymentMethod;
use App\Services\Email\EmailObject;
use App\Services\Email\EmailService;
use App\Utils\Number; use App\Utils\Number;
use Illuminate\Database\Eloquent\Collection; use App\Utils\Traits\MakesDates;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Support\Facades\DB;
class ClientService class ClientService
{ {
private $client; use MakesDates;
public function __construct(Client $client) private string $client_start_date;
{
$this->client = $client; private string $client_end_date;
}
public function __construct(private Client $client){}
public function updateBalance(float $amount) public function updateBalance(float $amount)
{ {
try { try {
\DB::connection(config('database.default'))->transaction(function () use($amount) { DB::connection(config('database.default'))->transaction(function () use($amount) {
$this->client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first(); $this->client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first();
$this->client->balance += $amount; $this->client->balance += $amount;
@ -51,7 +56,7 @@ class ClientService
{ {
try { try {
\DB::connection(config('database.default'))->transaction(function () use($balance, $paid_to_date) { DB::connection(config('database.default'))->transaction(function () use($balance, $paid_to_date) {
$this->client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first(); $this->client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first();
$this->client->balance += $balance; $this->client->balance += $balance;
@ -64,16 +69,14 @@ class ClientService
nlog("DB ERROR " . $throwable->getMessage()); nlog("DB ERROR " . $throwable->getMessage());
} }
return $this; return $this;
} }
public function updatePaidToDate(float $amount) public function updatePaidToDate(float $amount)
{ {
// $this->client->paid_to_date += $amount;
\DB::connection(config('database.default'))->transaction(function () use($amount) { DB::connection(config('database.default'))->transaction(function () use($amount) {
$this->client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first(); $this->client = Client::withTrashed()->where('id', $this->client->id)->lockForUpdate()->first();
$this->client->paid_to_date += $amount; $this->client->paid_to_date += $amount;
@ -82,17 +85,21 @@ class ClientService
}, 1); }, 1);
return $this; return $this;
} }
public function adjustCreditBalance(float $amount) public function adjustCreditBalance(float $amount)
{ {
$this->client->credit_balance += $amount; $this->client->credit_balance += $amount;
return $this; return $this;
} }
public function getCreditBalance() :float public function getCreditBalance() :float
{ {
$credits = Credit::withTrashed()->where('client_id', $this->client->id) $credits = Credit::withTrashed()->where('client_id', $this->client->id)
->where('is_deleted', false) ->where('is_deleted', false)
->where(function ($query) { ->where(function ($query) {
@ -102,6 +109,7 @@ class ClientService
->orderBy('created_at', 'ASC'); ->orderBy('created_at', 'ASC');
return Number::roundValue($credits->sum('balance'), $this->client->currency()->precision); return Number::roundValue($credits->sum('balance'), $this->client->currency()->precision);
} }
public function getCredits() public function getCredits()
@ -132,12 +140,71 @@ class ClientService
* Generate the client statement. * Generate the client statement.
* *
* @param array $options * @param array $options
* @param bool $send_email determines if we should send this statement direct to the client
*/ */
public function statement(array $options = []) public function statement(array $options = [], bool $send_email = false)
{ {
return (new Statement($this->client, $options))->run(); $statement = (new Statement($this->client, $options));
$pdf = $statement->run();
if($send_email)
return $this->emailStatement($pdf, $statement->options);
return $pdf;
} }
/**
* Emails the statement to the client
*
* @param mixed $pdf The pdf blob
* @param array $options The statement options array
* @return void
*/
private function emailStatement($pdf, array $options): void
{
$this->client_start_date = $this->translateDate($options['start_date'], $this->client->date_format(), $this->client->locale());
$this->client_end_date = $this->translateDate($options['end_date'], $this->client->date_format(), $this->client->locale());
$email_service = new EmailService($this->buildStatementMailableData($pdf), $this->client->company);
$email_service->send();
}
/**
* Builds and returns an EmailObject for Client Statements
*
* @param mixed $pdf The PDF to send
* @return EmailObject The EmailObject to send
*/
public function buildStatementMailableData($pdf) :EmailObject
{
$email_object = new EmailObject;
$email_object->to = [new Address($this->client->present()->email(), $this->client->present()->name())];
$email_object->attachments = [['file' => base64_encode($pdf), 'name' => ctrans('texts.statement') . ".pdf"]];
$email_object->settings = $this->client->getMergedSettings();
$email_object->company = $this->client->company;
$email_object->client = $this->client;
$email_object->email_template_subject = 'email_subject_statement';
$email_object->email_template_body = 'email_template_statement';
$email_object->variables = [
'$client' => $this->client->present()->name(),
'$start_date' => $this->client_start_date,
'$end_date' => $this->client_end_date,
];
return $email_object;
}
/**
* Saves the client instance
*
* @return Client The Client Model
*/
public function save() :Client public function save() :Client
{ {
$this->client->save(); $this->client->save();

View File

@ -32,25 +32,16 @@ class Statement
{ {
use PdfMakerTrait; use PdfMakerTrait;
protected Client $client;
/** /**
* @var Invoice|Payment|null * @var Invoice|Payment|null
*/ */
protected $entity; protected $entity;
protected array $options;
protected bool $rollback = false; protected bool $rollback = false;
public function __construct(Client $client, array $options) public function __construct(protected Client $client, public array $options){}
{
$this->client = $client;
$this->options = $options; public function run() :?string
}
public function run(): ?string
{ {
$this $this
->setupOptions() ->setupOptions()
@ -110,7 +101,7 @@ class Statement
$maker = null; $maker = null;
$state = null; $state = null;
return $pdf; return $pdf;
} }

View File

@ -11,18 +11,10 @@
namespace App\Services\Scheduler; namespace App\Services\Scheduler;
use App\DataMapper\EmailTemplateDefaults;
use App\Mail\Client\ClientStatement;
use App\Models\Client; use App\Models\Client;
use App\Models\Scheduler; use App\Models\Scheduler;
use App\Services\Email\EmailMailable;
use App\Services\Email\EmailObject;
use App\Services\Email\EmailService;
use App\Utils\Ninja;
use App\Utils\Traits\MakesDates; use App\Utils\Traits\MakesDates;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use Illuminate\Mail\Mailables\Address;
use Illuminate\Support\Str;
class SchedulerService class SchedulerService
{ {
@ -54,7 +46,6 @@ class SchedulerService
if(count($this->scheduler->parameters['clients']) >= 1) if(count($this->scheduler->parameters['clients']) >= 1)
$query->where('id', $this->transformKeys($this->scheduler->parameters['clients'])); $query->where('id', $this->transformKeys($this->scheduler->parameters['clients']));
$query->cursor() $query->cursor()
->each(function ($_client){ ->each(function ($_client){
@ -62,10 +53,7 @@ class SchedulerService
$statement_properties = $this->calculateStatementProperties(); $statement_properties = $this->calculateStatementProperties();
//work out the date range //work out the date range
$pdf = $_client->service()->statement($statement_properties); $pdf = $_client->service()->statement($statement_properties,true);
$email_service = new EmailService($this->buildMailableData($pdf), $_client->company);
$email_service->send();
//calculate next run dates; //calculate next run dates;
@ -77,9 +65,6 @@ class SchedulerService
{ {
$start_end = $this->calculateStartAndEndDates(); $start_end = $this->calculateStartAndEndDates();
$this->client_start_date = $this->translateDate($start_end[0], $this->client->date_format(), $this->client->locale());
$this->client_end_date = $this->translateDate($start_end[1], $this->client->date_format(), $this->client->locale());
return [ return [
'start_date' =>$start_end[0], 'start_date' =>$start_end[0],
'end_date' =>$start_end[1], 'end_date' =>$start_end[1],
@ -104,26 +89,7 @@ class SchedulerService
}; };
} }
private function buildMailableData($pdf)
{
$email_object = new EmailObject;
$email_object->to = [new Address($this->client->present()->email(), $this->client->present()->name())];
$email_object->attachments = [['file' => base64_encode($pdf), 'name' => ctrans('texts.statement') . ".pdf"]];
$email_object->settings = $this->client->getMergedSettings();
$email_object->company = $this->client->company;
$email_object->client = $this->client;
$email_object->email_template_subject = 'email_subject_statement';
$email_object->email_template_body = 'email_template_statement';
$email_object->variables = [
'$client' => $this->client->present()->name(),
'$start_date' => $this->client_start_date,
'$end_date' => $this->client_end_date,
];
return $email_object;
}
} }

View File

@ -20,12 +20,10 @@ use App\Models\GatewayType;
use App\Models\InvoiceInvitation; use App\Models\InvoiceInvitation;
use App\Models\QuoteInvitation; use App\Models\QuoteInvitation;
use App\Models\RecurringInvoiceInvitation; use App\Models\RecurringInvoiceInvitation;
use App\Services\PdfMaker\Designs\Utilities\DesignHelpers;
use App\Utils\Ninja; use App\Utils\Ninja;
use App\Utils\Number; use App\Utils\Number;
use App\Utils\Traits\AppSetup; use App\Utils\Traits\AppSetup;
use App\Utils\Traits\MakesDates; use App\Utils\Traits\MakesDates;
use App\Utils\transformTranslations;
use Exception; use Exception;
use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;

View File

@ -4924,7 +4924,8 @@ $LANG = array(
'action_add_to_invoice' => 'Add To Invoice', 'action_add_to_invoice' => 'Add To Invoice',
'danger_zone' => 'Danger Zone', 'danger_zone' => 'Danger Zone',
'import_completed' => 'Import completed', 'import_completed' => 'Import completed',
'client_statement_body' => 'Your statement from :start_date to :end_date is attached.' 'client_statement_body' => 'Your statement from :start_date to :end_date is attached.',
'email_queued' => 'Email queued',
); );

View File

@ -53,6 +53,64 @@ class ClientApiTest extends TestCase
Model::reguard(); Model::reguard();
} }
public function testClientStatement()
{
$response = null;
$data = [
'client_id' => $this->client->hashed_id,
'start_date' => '2000-01-01',
'end_date' => '2023-01-01',
'show_aging_table' => true,
'show_payments_table' => true,
'status' => 'paid',
];
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/client_statement', $data);
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
nlog($message);
}
$response->assertStatus(200);
}
public function testClientStatementEmail()
{
$response = null;
$data = [
'client_id' => $this->client->hashed_id,
'start_date' => '2000-01-01',
'end_date' => '2023-01-01',
'show_aging_table' => true,
'show_payments_table' => true,
'status' => 'paid',
];
try {
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/client_statement?send_email=true', $data);
} catch (ValidationException $e) {
$message = json_decode($e->validator->getMessageBag(), 1);
nlog($message);
}
$response->assertStatus(200);
}
public function testCsvImportRepositoryPersistance() public function testCsvImportRepositoryPersistance()
{ {
Client::unguard(); Client::unguard();