Working on export refinements

This commit is contained in:
David Bomba 2023-11-25 22:58:27 +11:00
parent 69a5712670
commit 4183426f39
16 changed files with 329 additions and 57 deletions

View File

@ -11,16 +11,17 @@
namespace App\Export\CSV;
use App\Libraries\MultiDB;
use App\Models\Client;
use App\Models\Company;
use App\Transformers\ClientContactTransformer;
use App\Transformers\ClientTransformer;
use App\Utils\Ninja;
use App\Utils\Number;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\App;
use App\Models\Client;
use League\Csv\Writer;
use App\Models\Company;
use App\Libraries\MultiDB;
use Illuminate\Support\Facades\App;
use App\Export\Decorators\Decorator;
use App\Transformers\ClientTransformer;
use Illuminate\Database\Eloquent\Builder;
use App\Transformers\ClientContactTransformer;
class ClientExport extends BaseExport
{
@ -32,6 +33,8 @@ class ClientExport extends BaseExport
public string $date_key = 'created_at';
private Decorator $decorator;
public array $entity_keys = [
'address1' => 'client.address1',
'address2' => 'client.address2',
@ -84,6 +87,8 @@ class ClientExport extends BaseExport
$this->input = $input;
$this->client_transformer = new ClientTransformer();
$this->contact_transformer = new ClientContactTransformer();
$this->decorator = new Decorator();
}
public function returnJson()

View File

@ -11,10 +11,165 @@
namespace App\Export\Decorators;
class ClientDecorator implements DecoratorInterface
use App\Models\Client;
class ClientDecorator extends Decorator implements DecoratorInterface
{
public function transform(): string
private $entity_key = 'client';
public function transform(string $key, mixed $entity): mixed
{
return 'Payment Decorator';
$client = false;
if($entity instanceof Client){
$client = $entity;
}
elseif($entity->client) {
$client = $entity->client;
}
if($client && method_exists($this, $key)) {
return $this->{$key}($client);
}
return '';
}
public function name(Client $client) {
return $client->present()->name();
}
public function number(Client $client) {
return $client->number ?? '';
}
public function user(Client $client) {
return $client->user->present()->name();
}
public function assigned_user(Client $client) {
return $client->assigned_user ? $client->user->present()->name() : '';
}
public function balance(Client $client) {
return $client->balance ?? 0;
}
public function paid_to_date(Client $client) {
return $client->paid_to_date ?? 0;
}
public function currency_id(Client $client) {
return $client->currency() ? $client->currency()->code : $client->company->currency()->code;
}
public function website(Client $client) {
return $client->website ?? '';
}
public function private_notes(Client $client) {
return $client->private_notes ?? '';
}
public function industry_id(Client $client) {
return $client->industry ? ctrans("texts.industry_{$client->industry->name}") : '';
}
public function size_id(Client $client) {
return $client->size ? ctrans("texts.size_{$client->size->name}") : '';
}
public function phone(Client $client) {
return $client->phone ?? '';
}
public function address1(Client $client) {
return $client->address1 ?? '';
}
public function address2(Client $client) {
return $client->address2 ?? '';
}
public function city(Client $client) {
return $client->city ?? '';
}
public function state(Client $client) {
return $client->state ?? '';
}
public function postal_code(Client $client) {
return $client->postal_code ?? '';
}
public function country_id(Client $client) {
return $client->country ? ctrans("texts.country_{$client->country->name}") : '';
}
public function shipping_address1(Client $client) {
return $client->shipping_address1 ?? '';
}
public function shipping_address2(Client $client) {
return $client->shipping_address2 ?? '';
}
public function shipping_city(Client $client) {
return $client->shipping_city ?? '';
}
public function shipping_state(Client $client) {
return $client->shipping_state ?? '';
}
public function shipping_postal_code(Client $client) {
return $client->shipping_postal_code ?? '';
}
public function shipping_country_id(Client $client) {
return $client->shipping_country ? ctrans("texts.country_{$client->shipping_country->name}") : '';
}
public function payment_terms(Client $client) {
return $client?->settings?->payment_terms ?? $client->company->settings->payment_terms;
}
public function vat_number(Client $client) {
return $client->vat_number ?? '';
}
public function id_number(Client $client) {
return $client->id_number ?? '';
}
public function public_notes(Client $client) {
return $client->public_notes ?? '';
}
public function custom_value1(Client $client) {
return $client->custom_value1 ?? '';
}
public function custom_value2(Client $client) {
return $client->custom_value2 ?? '';
}
public function custom_value3(Client $client) {
return $client->custom_value3 ?? '';
}
public function custom_value4(Client $client) {
return $client->custom_value4 ?? '';
}
public function payment_balance(Client $client) {
return $client->payment_balance ?? 0;
}
public function credit_balance(Client $client) {
return $client->credit_balance ?? 0;
}
public function classification(Client $client) {
ctrans("texts.{$client->classification}") ?? '';
}
////////contact details/////////////////
/*
public function phone(Client $client) {
}
public function first_name(Client $client) {
}
public function last_name(Client $client) {
}
public function email(Client $client) {
}
public function custom_value1(Client $client) {
}
public function custom_value2(Client $client) {
}
public function custom_value3(Client $client) {
}
public function custom_value4(Client $client) {
}
*/
}

View File

@ -13,7 +13,7 @@ namespace App\Export\Decorators;
class CreditDecorator implements DecoratorInterface
{
public function transform(): string
public function transform(string $key, mixed $entity): mixed
{
return 'Payment Decorator';
}

View File

@ -28,15 +28,17 @@ use App\Export\Decorators\DecoratorInterface;
class Decorator implements DecoratorInterface{
public $entity;
public function __construct()
{
}
public function transform(string $key, mixed $entity): string
public function transform(string $key, mixed $entity): mixed
{
return 'Decorator';
$index = $this->getKeyPart(0, $key);
$column = $this->getKeyPart(1, $key);
return $this->{$index}()->transform($column, $entity);
}
public function invoice(): InvoiceDecorator
@ -49,6 +51,16 @@ class Decorator implements DecoratorInterface{
return new ClientDecorator();
}
public function contact(): ContactDecorator
{
return new ContactDecorator();
}
public function vendor_contact(): VendorContactDecorator
{
return new VendorContactDecorator();
}
public function payment(): PaymentDecorator
{
return new PaymentDecorator();
@ -99,21 +111,9 @@ class Decorator implements DecoratorInterface{
return new PurchaseOrderDecorator();
}
public function setEntity($entity): self
{
$this->entity = $entity;
return $this;
}
public function getEntity(): mixed
{
return $this->entity;
}
public function getKeyPart(int $index, string $key): string
{
$parts = explode('.', $key ?? '');
$parts = explode('.', $key);
return $parts[$index];
}

View File

@ -12,5 +12,5 @@
namespace App\Export\Decorators;
interface DecoratorInterface {
public function transform(string $key, mixed $entity): string;
public function transform(string $key, mixed $entity): mixed;
}

View File

@ -13,7 +13,7 @@ namespace App\Export\Decorators;
class ExpenseDecorator implements DecoratorInterface
{
public function transform(): string
public function transform(string $key, mixed $entity): mixed
{
return 'Payment Decorator';
}

View File

@ -11,9 +11,9 @@
namespace App\Export\Decorators;
class InvoiceDecorator implements DecoratorInterface
class InvoiceDecorator extends Decorator implements DecoratorInterface
{
public function transform(): string
public function transform(string $key, mixed $entity): mixed
{
return 'Payment Decorator';
}

View File

@ -15,16 +15,128 @@ use App\Models\Payment;
class PaymentDecorator extends Decorator implements DecoratorInterface{
private $key = 'payment';
private $entity_key = 'payment';
public function transform(string $key, $payment): string
public function transform(string $key, $entity): mixed
{
$index = $this->getKeyPart(0,$key);
// match($index)
return 'Payment Decorator';
$payment = false;
if($entity instanceof Payment){
$payment = $entity;
}
elseif($entity->payment) {
$payment = $entity->payment;
}
if($key == 'amount' && (!$entity instanceof Payment)){
return $entity->payments()->exists() ? $entity->payments()->withoutTrashed()->sum('paymentables.amount') : ctrans('texts.unpaid');
}
elseif($key == 'refunded' && (!$entity instanceof Payment)) {
return $entity->payments()->exists() ? $entity->payments()->withoutTrashed()->sum('paymentables.refunded') : '';
}
elseif($key == 'applied' && (!$entity instanceof Payment)) {
$refunded = $entity->payments()->withoutTrashed()->sum('paymentables.refunded');
$amount = $entity->payments()->withoutTrashed()->sum('paymentables.amount');
return $entity->payments()->withoutTrashed()->exists() ? ($amount - $refunded) : '';
}
if($payment && method_exists($this, $key)) {
return $this->{$key}($payment);
}
return '';
}
public function date(Payment $payment) {
return $payment->date ?? '';
}
public function amount(Payment $payment) {
return $payment->amount ?? '';
}
public function refunded(Payment $payment) {
return $payment->refunded ?? '';
}
public function applied(Payment $payment) {
return $payment->applied ?? '';
}
public function transaction_reference(Payment $payment) {
return $payment->transaction_reference ?? '';
}
public function currency(Payment $payment) {
return $payment->currency()->exists() ? $payment->currency->code : $payment->company->currency()->code;
}
public function exchange_rate(Payment $payment) {
return $payment->exchange_rate ?? 1;
}
public function number(Payment $payment) {
return $payment->number ?? '';
}
public function method(Payment $payment) {
return $payment->translatedType();
}
public function status(Payment $payment) {
return $payment->stringStatus($payment->status_id);
}
public function private_notes(Payment $payment) {
return strip_tags($payment->private_notes) ?? '';
}
public function custom_value1(Payment $payment) {
return $payment->custom_value1 ?? '';
}
public function custom_value2(Payment $payment) {
return $payment->custom_value2 ?? '';
}
public function custom_value3(Payment $payment) {
return $payment->custom_value3 ?? '';
}
public function custom_value4(Payment $payment) {
return $payment->custom_value4 ?? '';
}
public function user_id(Payment $payment) {
return $payment->user ? $payment->user->present()->name() : '';
}
public function assigned_user_id(Payment $payment) {
return $payment->assigned_user ? $payment->assigned_user->present()->name() : '';
}
public function project_id(Payment $payment) {
return $payment->project()->exists() ? $payment->project->name : '';
}
///////////////////////////////////////////////////
public function vendor_id(Payment $payment){
return $payment->vendor()->exists() ? $payment->vendor->name : '';
}
public function exchange_currency(Payment $payment){
return $payment->exchange_currency()->exists() ? $payment->exchange_currency->code : '';
}
public function gateway_type_id(Payment $payment) {
return $payment->gateway_type ? $payment->gateway_type->name : 'Unknown Type';
}
public function client_id(Payment $payment) {
return $payment->client->present()->name();
}
public function type_id(Payment $payment) {
return $payment->translatedType();
}
}

View File

@ -13,7 +13,7 @@ namespace App\Export\Decorators;
class ProductDecorator implements DecoratorInterface
{
public function transform(): string
public function transform(string $key, mixed $entity): mixed
{
return 'Payment Decorator';
}

View File

@ -13,7 +13,7 @@ namespace App\Export\Decorators;
class ProjectDecorator implements DecoratorInterface
{
public function transform(): string
public function transform(string $key, mixed $entity): mixed
{
return 'Payment Decorator';
}

View File

@ -11,9 +11,9 @@
namespace App\Export\Decorators;
class PurchaseOrderDecorator implements DecoratorInterface
class PurchaseOrderDecorator extends Decorator implements DecoratorInterface
{
public function transform(): string
public function transform(string $key, mixed $entity): mixed
{
return 'Payment Decorator';
}

View File

@ -11,9 +11,9 @@
namespace App\Export\Decorators;
class QuoteDecorator implements DecoratorInterface
class QuoteDecorator extends Decorator implements DecoratorInterface
{
public function transform(): string
public function transform(string $key, mixed $entity): mixed
{
return 'Payment Decorator';
}

View File

@ -11,9 +11,9 @@
namespace App\Export\Decorators;
class RecurringInvoiceDecorator implements DecoratorInterface
class RecurringInvoiceDecorator extends Decorator implements DecoratorInterface
{
public function transform(): string
public function transform(string $key, mixed $entity): mixed
{
return 'Payment Decorator';
}

View File

@ -11,9 +11,9 @@
namespace App\Export\Decorators;
class TaskDecorator implements DecoratorInterface
class TaskDecorator extends Decorator implements DecoratorInterface
{
public function transform(): string
public function transform(string $key, mixed $entity): mixed
{
return 'Payment Decorator';
}

View File

@ -11,9 +11,9 @@
namespace App\Export\Decorators;
class VendorDecorator implements DecoratorInterface
class VendorDecorator extends Decorator implements DecoratorInterface
{
public function transform(): string
public function transform(string $key, mixed $entity): mixed
{
return 'Payment Decorator';
}

View File

@ -281,12 +281,12 @@ class EventServiceProvider extends ServiceProvider
*/
protected $listen = [
RequestSending::class => [
LogRequestSending::class,
],
ResponseReceived::class => [
LogResponseReceived::class,
],
// RequestSending::class => [
// LogRequestSending::class,
// ],
// ResponseReceived::class => [
// LogResponseReceived::class,
// ],
AccountCreated::class => [
],
MessageSending::class => [