diff --git a/app/Export/CSV/ClientExport.php b/app/Export/CSV/ClientExport.php index 29178f5236e8..6579ed3f04fc 100644 --- a/app/Export/CSV/ClientExport.php +++ b/app/Export/CSV/ClientExport.php @@ -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() diff --git a/app/Export/Decorators/ClientDecorator.php b/app/Export/Decorators/ClientDecorator.php index 83fbdad948ca..3af0cef27610 100644 --- a/app/Export/Decorators/ClientDecorator.php +++ b/app/Export/Decorators/ClientDecorator.php @@ -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) { + + } + */ + + } diff --git a/app/Export/Decorators/CreditDecorator.php b/app/Export/Decorators/CreditDecorator.php index 41648b9b88af..61dc464a60dc 100644 --- a/app/Export/Decorators/CreditDecorator.php +++ b/app/Export/Decorators/CreditDecorator.php @@ -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'; } diff --git a/app/Export/Decorators/Decorator.php b/app/Export/Decorators/Decorator.php index fd8918fe7cd0..7891caa3594b 100644 --- a/app/Export/Decorators/Decorator.php +++ b/app/Export/Decorators/Decorator.php @@ -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]; } diff --git a/app/Export/Decorators/DecoratorInterface.php b/app/Export/Decorators/DecoratorInterface.php index 60225d257c8d..cd8b6fa03597 100644 --- a/app/Export/Decorators/DecoratorInterface.php +++ b/app/Export/Decorators/DecoratorInterface.php @@ -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; } diff --git a/app/Export/Decorators/ExpenseDecorator.php b/app/Export/Decorators/ExpenseDecorator.php index 828d28690eb5..718a442855ac 100644 --- a/app/Export/Decorators/ExpenseDecorator.php +++ b/app/Export/Decorators/ExpenseDecorator.php @@ -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'; } diff --git a/app/Export/Decorators/InvoiceDecorator.php b/app/Export/Decorators/InvoiceDecorator.php index 2898965c7a82..f3eb80c4c823 100644 --- a/app/Export/Decorators/InvoiceDecorator.php +++ b/app/Export/Decorators/InvoiceDecorator.php @@ -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'; } diff --git a/app/Export/Decorators/PaymentDecorator.php b/app/Export/Decorators/PaymentDecorator.php index 82f619fdd0f4..f9af06b182c1 100644 --- a/app/Export/Decorators/PaymentDecorator.php +++ b/app/Export/Decorators/PaymentDecorator.php @@ -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(); + } } \ No newline at end of file diff --git a/app/Export/Decorators/ProductDecorator.php b/app/Export/Decorators/ProductDecorator.php index b22d940fbe2c..d7b9fc729525 100644 --- a/app/Export/Decorators/ProductDecorator.php +++ b/app/Export/Decorators/ProductDecorator.php @@ -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'; } diff --git a/app/Export/Decorators/ProjectDecorator.php b/app/Export/Decorators/ProjectDecorator.php index 62b5d8ee9101..91ce5b87ff45 100644 --- a/app/Export/Decorators/ProjectDecorator.php +++ b/app/Export/Decorators/ProjectDecorator.php @@ -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'; } diff --git a/app/Export/Decorators/PurchaseOrderDecorator.php b/app/Export/Decorators/PurchaseOrderDecorator.php index c5d8dd3a3411..b25f7001d555 100644 --- a/app/Export/Decorators/PurchaseOrderDecorator.php +++ b/app/Export/Decorators/PurchaseOrderDecorator.php @@ -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'; } diff --git a/app/Export/Decorators/QuoteDecorator.php b/app/Export/Decorators/QuoteDecorator.php index 96161c96d40e..49e073452c6f 100644 --- a/app/Export/Decorators/QuoteDecorator.php +++ b/app/Export/Decorators/QuoteDecorator.php @@ -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'; } diff --git a/app/Export/Decorators/RecurringInvoiceDecorator.php b/app/Export/Decorators/RecurringInvoiceDecorator.php index d14e6f705e09..02cd646b5d17 100644 --- a/app/Export/Decorators/RecurringInvoiceDecorator.php +++ b/app/Export/Decorators/RecurringInvoiceDecorator.php @@ -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'; } diff --git a/app/Export/Decorators/TaskDecorator.php b/app/Export/Decorators/TaskDecorator.php index 60005ad59a9c..a6f8254c666b 100644 --- a/app/Export/Decorators/TaskDecorator.php +++ b/app/Export/Decorators/TaskDecorator.php @@ -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'; } diff --git a/app/Export/Decorators/VendorDecorator.php b/app/Export/Decorators/VendorDecorator.php index 2dc22177a5fe..cafdd420addf 100644 --- a/app/Export/Decorators/VendorDecorator.php +++ b/app/Export/Decorators/VendorDecorator.php @@ -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'; } diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index f11194164b98..601451423631 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -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 => [