Updates for passing refund meta into templates

This commit is contained in:
David Bomba 2023-10-08 21:03:07 +11:00
parent aa3be06876
commit c44310fa09
7 changed files with 93 additions and 52 deletions

View File

@ -14,7 +14,6 @@ namespace App\Http\Controllers;
use App\Events\Payment\PaymentWasUpdated;
use App\Factory\PaymentFactory;
use App\Filters\PaymentFilters;
use App\Http\Requests\Payment\ActionPaymentRequest;
use App\Http\Requests\Payment\CreatePaymentRequest;
use App\Http\Requests\Payment\DestroyPaymentRequest;
use App\Http\Requests\Payment\EditPaymentRequest;
@ -24,14 +23,12 @@ use App\Http\Requests\Payment\StorePaymentRequest;
use App\Http\Requests\Payment\UpdatePaymentRequest;
use App\Http\Requests\Payment\UploadPaymentRequest;
use App\Models\Account;
use App\Models\Invoice;
use App\Models\Payment;
use App\Repositories\PaymentRepository;
use App\Transformers\PaymentTransformer;
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\SavesDocuments;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
/**

View File

@ -30,6 +30,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
* @property int $id
* @property int $company_id
* @property int $client_id
* @property int $category_id
* @property int|null $project_id
* @property int|null $vendor_id
* @property int|null $user_id
@ -58,6 +59,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
* @property int|null $exchange_currency_id
* @property \App\Models\Paymentable $paymentable
* @property object|null $meta
* @property object|null $refund_meta
* @property string|null $custom_value1
* @property string|null $custom_value2
* @property string|null $custom_value3
@ -151,12 +153,12 @@ class Payment extends BaseModel
'number',
'exchange_currency_id',
'exchange_rate',
// 'is_manual',
'private_notes',
'custom_value1',
'custom_value2',
'custom_value3',
'custom_value4',
'category_id',
];
protected $casts = [
@ -167,6 +169,7 @@ class Payment extends BaseModel
'deleted_at' => 'timestamp',
'is_deleted' => 'bool',
'meta' => 'object',
'refund_meta' => 'array',
];
protected $with = [
@ -436,11 +439,6 @@ class Payment extends BaseModel
public function getLink() :string
{
// if (Ninja::isHosted()) {
// $domain = isset($this->company->portal_domain) ? $this->company->portal_domain : $this->company->domain();
// } else {
// $domain = config('ninja.app_url');
// }
if (Ninja::isHosted()) {
$domain = $this->company->domain();
@ -476,4 +474,11 @@ class Payment extends BaseModel
return $use_react_url ? config('ninja.react_url')."/#/payments/{$this->hashed_id}/edit" : config('ninja.app_url');
}
public function setRefundMeta(array $data)
{
$tmp_meta = $this->refund_meta ?? [];
$tmp_meta[] = $data;
$this->refund_meta = $tmp_meta;
}
}

View File

@ -23,33 +23,15 @@ use stdClass;
class RefundPayment
{
public $payment;
public $refund_data;
private $credit_note;
private $total_refund;
private $gateway_refund_status;
private $activity_repository;
private float $total_refund = 0;
private bool $refund_failed = false;
private string $refund_failed_message = '';
public function __construct($payment, $refund_data)
public function __construct(public Payment $payment, public array $refund_data)
{
$this->payment = $payment;
$this->refund_data = $refund_data;
$this->total_refund = 0;
$this->gateway_refund_status = false;
$this->activity_repository = new ActivityRepository();
}
public function run()
@ -135,6 +117,8 @@ class RefundPayment
$this->payment->refunded += $this->total_refund;
}
$this->payment->setRefundMeta($this->refund_data);
return $this;
}

View File

@ -74,7 +74,8 @@ class TemplateAction implements ShouldQueue
*
*/
public function handle()
{ nlog("inside template action");
{
// nlog("inside template action");
MultiDB::setDb($this->db);
@ -109,7 +110,7 @@ class TemplateAction implements ShouldQueue
$ts = $template_service->build($data);
nlog($ts->getHtml());
// nlog($ts->getHtml());
if($this->send_email) {
$pdf = $ts->getPdf();

View File

@ -16,6 +16,7 @@ use App\Models\Client;
use App\Models\Credit;
use App\Models\Design;
use App\Models\Company;
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\Project;
use App\Models\Activity;
@ -529,37 +530,56 @@ class TemplateService
'refund_activity' => $this->getPaymentRefundActivity($payment),
];
nlog($this->getPaymentRefundActivity($payment));
return $data;
}
/**
* [
"id" => 12,
"date" => "2023-10-08",
"invoices" => [
[
"amount" => 1,
"invoice_id" => 23,
"id" => null,
],
],
"q" => "/api/v1/payments/refund",
"email_receipt" => "true",
"gateway_refund" => false,
"send_email" => false,
],
*
* @param Payment $payment
* @return array
*/
private function getPaymentRefundActivity(Payment $payment): array
{
return Activity::where('activity_type_id', 40)
->where('payment_id', $payment->id)
->where('company_id', $payment->company_id)
->orderBy('id', 'asc')
->cursor()
->map(function ($a) use ($payment){
$date = \Carbon\Carbon::parse($a->created_at)->addSeconds($a->payment->client->timezone_offset());
$date = $this->translateDate($date, $a->payment->client->date_format(), $a->payment->client->locale());
$notes = explode("-", $a->notes);
try {
$amount = explode(":", reset($notes));
$amount = Number::formatMoney(end($amount), $payment->client);
$notes = ctrans('texts.status_partially_refunded_amount', ['amount' => $amount]);
}
catch(\Exception $e){
}
return collect($payment->refund_meta ?? [])
->map(function ($refund) use($payment){
$date = \Carbon\Carbon::parse($refund['date'])->addSeconds($payment->client->timezone_offset());
$date = $this->translateDate($date, $payment->client->date_format(), $payment->client->locale());
$entity = ctrans('texts.invoice');
return "{$date} {$entity} #{$a->invoice->number} {$notes}\n";
$map = [];
})->toArray();
foreach($refund['invoices'] as $refunded_invoice) {
$invoice = Invoice::withTrashed()->find($refunded_invoice['invoice_id']);
$amount = Number::formatMoney($refunded_invoice['amount'], $payment->client);
$notes = ctrans('texts.status_partially_refunded_amount', ['amount' => $amount]);
array_push($map, "{$date} {$entity} #{$invoice->number} {$notes}\n");
}
return $map;
})->flatten()->toArray();
}

View File

@ -64,7 +64,15 @@ class PaymentHtmlEngine
$data['$amount'] = &$data['$payment.amount'];
$data['$payment.date'] = ['value' => $this->translateDate($this->payment->date, $this->client->date_format(), $this->client->locale()), 'label' => ctrans('texts.payment_date')];
$data['$transaction_reference'] = ['value' => $this->payment->transaction_reference, 'label' => ctrans('texts.transaction_reference')];
// $data['$public_notes'] = ['value' => $this->payment->public_notes, 'label' => ctrans('texts.notes')];
$data['$font_size'] = ['value' => $this->settings->font_size . 'px !important;', 'label' => ''];
$data['$font_name'] = ['value' => Helpers::resolveFont($this->settings->primary_font)['name'], 'label' => ''];
$data['$font_url'] = ['value' => Helpers::resolveFont($this->settings->primary_font)['url'], 'label' => ''];
$data['$secondary_font_name'] = ['value' => Helpers::resolveFont($this->settings->secondary_font)['name'], 'label' => ''];
$data['$secondary_font_url'] = ['value' => Helpers::resolveFont($this->settings->secondary_font)['url'], 'label' => ''];
$data['$invoiceninja.whitelabel'] = ['value' => 'https://invoicing.co/images/new_logo.png', 'label' => ''];
$data['$primary_color'] = ['value' => $this->settings->primary_color, 'label' => ''];
$data['$secondary_color'] = ['value' => $this->settings->secondary_color, 'label' => ''];
$data['$payment1'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'payment1', $this->payment->custom_value1, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'payment1')];
$data['$payment2'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'payment2', $this->payment->custom_value2, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'payment2')];

View File

@ -0,0 +1,26 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('payments', function (Blueprint $table) {
$table->text('refund_meta')->nullable();
$table->unsignedInteger('category_id')->nullable();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
}
};