Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
turbo124 committed Jan 15, 2025
1 parent dc033ae commit ea3f6f1
Show file tree
Hide file tree
Showing 17 changed files with 158 additions and 78 deletions.
5 changes: 4 additions & 1 deletion app/Helpers/Invoice/InvoiceItemSumInclusive.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ class InvoiceItemSumInclusive
private $tax_collection;

private bool $calc_tax = false;

private $total_discount;

private Client | Vendor $client;

Expand All @@ -118,7 +120,8 @@ class InvoiceItemSumInclusive
public function __construct(RecurringInvoice | Invoice | Quote | Credit | PurchaseOrder | RecurringQuote $invoice)
{
$this->tax_collection = collect([]);

$this->total_discount = 0;

$this->invoice = $invoice;
$this->client = $invoice->client ?? $invoice->vendor;

Expand Down
22 changes: 1 addition & 21 deletions app/Http/Controllers/CreditController.php
Original file line number Diff line number Diff line change
Expand Up @@ -641,27 +641,7 @@ private function performAction(Credit $credit, $action, $bulk = false)
case 'email':
case 'send_email':

$credit->service()->markSent()->save();

$credit->invitations->load('contact.client.country', 'credit.client.country', 'credit.company')->each(function ($invitation) use ($credit) {

$mo = new \App\Services\Email\EmailObject();
$mo->entity_id = $credit->id;
$mo->entity_class = \App\Models\Credit::class;
$mo->invitation_id = $invitation->id;
$mo->client_id = $invitation->contact->client_id ?? null;
$mo->vendor_id = $invitation->contact->vendor_id ?? null;
$mo->settings = $invitation->contact->client->getMergedSettings();
$mo->email_template_body = 'email_template_credit';
$mo->email_template_subject = 'email_subject_credit';

\App\Services\Email\Email::dispatch($mo, $invitation->company);

$credit->entityEmailEvent($invitation, 'credit', 'credit');

});

event(new EntityWasEmailed($credit->invitations->first(), $credit->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), 'credit'));
$credit->service()->sendEmail();

if (! $bulk) {
return response()->json(['message' => 'email sent'], 200);
Expand Down
11 changes: 9 additions & 2 deletions app/Http/Controllers/EmailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function send(SendEmailRequest $request)
$this->entity_transformer = QuoteTransformer::class;

if ($entity_obj->invitations->count() >= 1) {
event(new EntityWasEmailed($entity_obj->invitations->first(), $entity_obj->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), 'invoice'));
event(new EntityWasEmailed($entity_obj->invitations->first(), $entity_obj->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), 'quote'));
$entity_obj->sendEvent(Webhook::EVENT_SENT_QUOTE, "client");
}
}
Expand All @@ -125,7 +125,7 @@ public function send(SendEmailRequest $request)
$this->entity_transformer = CreditTransformer::class;

if ($entity_obj->invitations->count() >= 1) {
event(new EntityWasEmailed($entity_obj->invitations->first(), $entity_obj->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), 'invoice'));
event(new EntityWasEmailed($entity_obj->invitations->first(), $entity_obj->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), 'credit'));
$entity_obj->sendEvent(Webhook::EVENT_SENT_CREDIT, "client");
}
}
Expand All @@ -138,6 +138,13 @@ public function send(SendEmailRequest $request)
if ($entity_obj instanceof PurchaseOrder) {
$this->entity_type = PurchaseOrder::class;
$this->entity_transformer = PurchaseOrderTransformer::class;


if ($entity_obj->invitations->count() >= 1) {
event(new EntityWasEmailed($entity_obj->invitations->first(), $entity_obj->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), 'purchase_order'));
$entity_obj->sendEvent(Webhook::EVENT_SENT_PURCHASE_ORDER, "client");
}

}

return $this->itemResponse($entity_obj->fresh());
Expand Down
13 changes: 9 additions & 4 deletions app/Http/Controllers/InvoiceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -586,13 +586,18 @@ public function bulk(BulkInvoiceRequest $request)

$invoice = $invoices->first();

if ($user->can('edit', $invoice)) {
$invoices->filter(function ($invoice) use ($user) {
return $user->can('edit', $invoice);
})->each(function ($invoice) use ($user, $request) {
$invoice->service()->sendEmail(email_type: $request->input('email_type', $invoice->calculateTemplate('invoice')));
});

$template = $request->input('email_type', $invoice->calculateTemplate('invoice'));
// if ($user->can('edit', $invoice)) {

BulkInvoiceJob::dispatch($invoices->pluck('id')->toArray(), $user->company()->db, $template);
// $template = $request->input('email_type', $invoice->calculateTemplate('invoice'));

}
// BulkInvoiceJob::dispatch($invoices->pluck('id')->toArray(), $user->company()->db, $template);
// }

return $this->listResponse(Invoice::withTrashed()->whereIn('id', $this->transformKeys($ids))->company());

Expand Down
12 changes: 3 additions & 9 deletions app/Http/Controllers/PurchaseOrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -670,18 +670,12 @@ private function performAction(PurchaseOrder $purchase_order, $action, $bulk = f
break;

case 'email':
//check query parameter for email_type and set the template else use calculateTemplate
PurchaseOrderEmail::dispatch($purchase_order, $purchase_order->company);

if (! $bulk) {
return response()->json(['message' => 'email sent'], 200);
}
break;

case 'send_email':
//check query parameter for email_type and set the template else use calculateTemplate
PurchaseOrderEmail::dispatch($purchase_order, $purchase_order->company);
// PurchaseOrderEmail::dispatch($purchase_order, $purchase_order->company);

$purchase_order->service()->sendEmail();

if (! $bulk) {
return response()->json(['message' => 'email sent'], 200);
}
Expand Down
2 changes: 2 additions & 0 deletions app/Listeners/General/EntityEmailedNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public function handle($event)
if (($key = array_search('mail', $methods)) !== false) {
unset($methods[$key]);

nlog((new EntitySentObject($event->invitation, $this->entity_string, $event->template, $company_user->portalType())));

$nmo = new NinjaMailerObject();
$nmo->mailable = new NinjaMailer((new EntitySentObject($event->invitation, $this->entity_string, $event->template, $company_user->portalType()))->build());
$nmo->company = $event->invitation->company;
Expand Down
11 changes: 10 additions & 1 deletion app/Mail/Admin/EntitySentObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,19 @@ public function build()
'settings' => $this->company->settings,
'whitelabel' => $this->company->account->isPaid() ? true : false,
'template' => $this->company->account->isPremium() ? 'email.template.admin_premium' : 'email.template.admin',

'text_body' => ctrans(
$this->template_body,
[
'amount' => $mail_obj->amount,
'vendor' => $this->contact->vendor->present()->name(),
'purchase_order' => $this->entity->number,
]
);
];

$mail_obj->markdown = 'email.admin.generic';
$mail_obj->tag = $this->company->company_key;

} else {
$mail_obj = new stdClass();
$mail_obj->amount = $this->getAmount();
Expand Down
16 changes: 16 additions & 0 deletions app/Models/PurchaseOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace App\Models;

use App\Utils\Ninja;
use App\Utils\Number;
use Laravel\Scout\Searchable;
use Illuminate\Support\Carbon;
Expand All @@ -19,6 +20,7 @@
use App\Helpers\Invoice\InvoiceSumInclusive;
use Illuminate\Database\Eloquent\SoftDeletes;
use App\Services\PurchaseOrder\PurchaseOrderService;
use App\Events\PurchaseOrder\PurchaseOrderWasEmailed;

/**
* App\Models\PurchaseOrder
Expand Down Expand Up @@ -421,4 +423,18 @@ public function taxTypeString($id): string
return $tax_type;
}


public function entityEmailEvent($invitation, $reminder_template, $template = '')
{

switch ($reminder_template) {
case 'purchase_order':
event(new PurchaseOrderWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), $reminder_template));
break;

default:
event(new PurchaseOrderWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), $reminder_template));
break;
}
}
}
5 changes: 3 additions & 2 deletions app/Models/Quote.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,14 +473,15 @@ public function canRemind(): bool
* Translates the email type into an activity + notification
* that matches.
*/
public function entityEmailEvent($invitation, $reminder_template)
public function entityEmailEvent($invitation, $reminder_template, $template = '')
{

switch ($reminder_template) {
case 'quote':
event(new QuoteWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), $reminder_template));
break;
case 'email_quote_template_reminder1':
case 'reminder1':
event(new QuoteReminderWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), $reminder_template));
break;
case 'custom1':
Expand All @@ -489,7 +490,7 @@ public function entityEmailEvent($invitation, $reminder_template)
event(new QuoteWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), $reminder_template));
break;
default:
// code...
event(new QuoteWasEmailed($invitation, $invitation->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), $reminder_template));
break;
}
}
Expand Down
1 change: 0 additions & 1 deletion app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,6 @@ class EventServiceProvider extends ServiceProvider
],
QuoteWasEmailed::class => [
QuoteEmailActivity::class,
QuoteEmailedNotification::class,
],
QuoteWasViewed::class => [
QuoteViewedActivity::class,
Expand Down
19 changes: 17 additions & 2 deletions app/Services/Credit/SendEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use App\Models\ClientContact;
use App\Jobs\Entity\EmailEntity;
use App\Events\Credit\CreditWasEmailed;
use App\Events\General\EntityWasEmailed;

class SendEmail
{
Expand Down Expand Up @@ -45,12 +46,26 @@ public function run()

$this->credit->service()->markSent()->save();

$this->credit->invitations->each(function ($invitation) {
$this->credit->invitations->load('contact.client.country', 'credit.client.country', 'credit.company')->each(function ($invitation) {

$mo = new \App\Services\Email\EmailObject();
$mo->entity_id = $this->credit->id;
$mo->entity_class = \App\Models\Credit::class;
$mo->invitation_id = $invitation->id;
$mo->client_id = $invitation->contact->client_id ?? null;
$mo->vendor_id = $invitation->contact->vendor_id ?? null;
$mo->settings = $invitation->contact->client->getMergedSettings();
$mo->email_template_body = 'email_template_credit';
$mo->email_template_subject = 'email_subject_credit';

if (! $invitation->contact->trashed() && $invitation->contact->email) {
EmailEntity::dispatch($invitation->withoutRelations(), $invitation->company->db, $this->reminder_template)->delay(2);
\App\Services\Email\Email::dispatch($mo, $invitation->company);
$this->credit->entityEmailEvent($invitation, 'credit', 'credit');
}
});

event(new EntityWasEmailed($this->credit->invitations->first(), $this->credit->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), 'credit'));

if ($this->credit->invitations->count() >= 1) {
event(new CreditWasEmailed($this->credit->invitations->first(), $this->credit->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), 'credit'));
$this->credit->sendEvent(Webhook::EVENT_SENT_CREDIT, "client");
Expand Down
6 changes: 3 additions & 3 deletions app/Services/Invoice/InvoiceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function applyPayment(Payment $payment, float $payment_amount)
public function addGatewayFee(CompanyGateway $company_gateway, $gateway_type_id, float $amount, string $payment_hash_string)
{
$this->removeUnpaidGatewayFees();

$this->invoice = (new AddGatewayFee($company_gateway, $gateway_type_id, $this->invoice, $amount, $payment_hash_string))->run();

return $this;
Expand Down Expand Up @@ -218,9 +218,9 @@ public function getEDocument($contact = null)
return $this->getEInvoice($contact);
}

public function sendEmail($contact = null)
public function sendEmail($contact = null, $email_type = 'invoice')
{
$send_email = new SendEmail($this->invoice, null, $contact);
$send_email = new SendEmail($this->invoice, $email_type, $contact);

return $send_email->run();
}
Expand Down
57 changes: 48 additions & 9 deletions app/Services/Invoice/SendEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
use App\Models\Invoice;
use App\Models\Webhook;
use App\Models\ClientContact;
use App\Services\Email\Email;
use App\Jobs\Entity\EmailEntity;
use App\Services\AbstractService;
use App\Services\Email\EmailObject;
use App\Events\General\EntityWasEmailed;
use App\Events\Invoice\InvoiceWasEmailed;

class SendEmail extends AbstractService
Expand All @@ -34,18 +37,54 @@ public function run()
$this->reminder_template = $this->invoice->calculateTemplate('invoice');
}

$this->invoice->invitations->each(function ($invitation) {
if (! $invitation->contact->trashed() && $invitation->contact->email) {
EmailEntity::dispatch($invitation->withoutRelations(), $invitation->company->db, $this->reminder_template)->delay(10);
}
$base_template = $this->reminder_template;

$this->reminder_template = $this->resolveTemplateString($this->reminder_template);

$this->invoice->service()->markSent()->save();

$this->invoice->invitations->filter(function ($invitation){
return (! $invitation->contact->trashed() && $invitation->contact->email);
})->each(function ($invitation) use($base_template) {

$mo = new EmailObject();
$mo->entity_id = $invitation->invoice_id;
$mo->template = $this->reminder_template;
$mo->email_template_body = $this->reminder_template;
$mo->email_template_subject = str_replace("template", "subject", $this->reminder_template);

$mo->entity_class = get_class($invitation->invoice);
$mo->invitation_id = $invitation->id;
$mo->client_id = $invitation->contact->client_id ?? null;
$mo->vendor_id = $invitation->contact->vendor_id ?? null;

Email::dispatch($mo, $invitation->company);

$this->invoice->entityEmailEvent($invitation, $base_template, $base_template);

});

if ($this->invoice->invitations->count() >= 1) {
// event(new InvoiceWasEmailed($this->invoice->invitations->first(), $this->invoice->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), $this->reminder_template ?? 'invoice'));
$this->invoice->entityEmailEvent($this->invoice->invitations->first(), $this->reminder_template ?? 'invoice');
$this->invoice->sendEvent(Webhook::EVENT_SENT_INVOICE, "client");
event(new EntityWasEmailed($this->invoice->invitations->first(), $this->invoice->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null), 'invoice'));

}
$this->invoice->sendEvent(Webhook::EVENT_SENT_INVOICE, "client");

}


private function resolveTemplateString(string $template): string
{

return match ($template) {
'reminder1' => 'email_template_reminder1',
'reminder2' => 'email_template_reminder2',
'reminder3' => 'email_template_reminder3',
'endless_reminder' => 'email_template_reminder_endless',
'custom1' => 'email_template_custom1',
'custom2' => 'email_template_custom2',
'custom3' => 'email_template_custom3',
default => "email_template_{$template}",
};

}

}
2 changes: 1 addition & 1 deletion app/Services/PurchaseOrder/PurchaseOrderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function expense()

public function sendEmail($contact = null)
{
$send_email = new SendEmail($this->purchase_order, null, $contact);
$send_email = new SendEmail($this->purchase_order, 'purchase_order', $contact);

return $send_email->run();
}
Expand Down
Loading

0 comments on commit ea3f6f1

Please sign in to comment.