diff --git a/app/Http/Controllers/PaymentController.php b/app/Http/Controllers/PaymentController.php index 3e5b0908b584..e0ab42bdc038 100644 --- a/app/Http/Controllers/PaymentController.php +++ b/app/Http/Controllers/PaymentController.php @@ -208,9 +208,6 @@ class PaymentController extends BaseController { $payment = $this->payment_repo->save($request->all(), PaymentFactory::create(auth()->user()->company()->id, auth()->user()->id)); - if($request->has('email_receipt') && $request->input('email_receipt') == 'true' && !$payment->client->getSetting('client_manual_payment_notification')) - $payment->service()->sendEmail(); - return $this->itemResponse($payment); } diff --git a/app/Http/Middleware/CheckClientExistence.php b/app/Http/Middleware/CheckClientExistence.php index 1a80fb9082d3..a80dc0496543 100644 --- a/app/Http/Middleware/CheckClientExistence.php +++ b/app/Http/Middleware/CheckClientExistence.php @@ -29,6 +29,7 @@ class CheckClientExistence public function handle(Request $request, Closure $next) { $multiple_contacts = ClientContact::query() + ->with('company','client') ->where('email', auth('contact')->user()->email) ->whereNotNull('email') ->where('email', '<>', '') diff --git a/app/Http/Requests/ClientGatewayToken/StoreClientGatewayTokenRequest.php b/app/Http/Requests/ClientGatewayToken/StoreClientGatewayTokenRequest.php index 0ebed562e190..433165940eb9 100644 --- a/app/Http/Requests/ClientGatewayToken/StoreClientGatewayTokenRequest.php +++ b/app/Http/Requests/ClientGatewayToken/StoreClientGatewayTokenRequest.php @@ -37,8 +37,9 @@ class StoreClientGatewayTokenRequest extends Request public function rules() { + //ensure client is present $rules = [ - 'client_id' => 'required', + 'client_id' => 'required|exists:clients,id,company_id,'.auth()->user()->company()->id, 'company_gateway_id' => 'required', 'gateway_type_id' => 'required|integer', 'meta' => 'required', diff --git a/app/Jobs/Ninja/CompanySizeCheck.php b/app/Jobs/Ninja/CompanySizeCheck.php index ff55abb16bb6..ea1dd2efaa84 100644 --- a/app/Jobs/Ninja/CompanySizeCheck.php +++ b/app/Jobs/Ninja/CompanySizeCheck.php @@ -56,7 +56,7 @@ class CompanySizeCheck implements ShouldQueue { Company::cursor()->each(function ($company) { - if ($company->invoices()->count() > 1000 || $company->products()->count() > 1000 || $company->clients()->count() > 1000) { + if ($company->invoices()->count() > 500 || $company->products()->count() > 500 || $company->clients()->count() > 500) { nlog("Marking company {$company->id} as large"); diff --git a/app/Jobs/Ninja/SendReminders.php b/app/Jobs/Ninja/SendReminders.php index 46b02dd589b5..cba282e06d27 100644 --- a/app/Jobs/Ninja/SendReminders.php +++ b/app/Jobs/Ninja/SendReminders.php @@ -29,6 +29,7 @@ use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Carbon; +//@DEPRECATED class SendReminders implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesDates, MakesReminders; diff --git a/app/Jobs/Util/Import.php b/app/Jobs/Util/Import.php index 71d54ea1a3db..f629806aecac 100644 --- a/app/Jobs/Util/Import.php +++ b/app/Jobs/Util/Import.php @@ -393,6 +393,10 @@ class Import implements ShouldQueue foreach ($data['settings'] as $key => $value) { if ($key == 'invoice_design_id' || $key == 'quote_design_id' || $key == 'credit_design_id') { $value = $this->encodePrimaryKey($value); + + if(!$value) + $value = $this->encodePrimaryKey(1); + } if ($key == 'payment_terms' && $key = '') { diff --git a/app/Jobs/Util/ReminderJob.php b/app/Jobs/Util/ReminderJob.php index 7097b7f87375..dd0b1d521764 100644 --- a/app/Jobs/Util/ReminderJob.php +++ b/app/Jobs/Util/ReminderJob.php @@ -11,11 +11,13 @@ namespace App\Jobs\Util; +use App\DataMapper\InvoiceItem; use App\Events\Invoice\InvoiceWasEmailed; use App\Jobs\Entity\EmailEntity; use App\Libraries\MultiDB; use App\Models\Invoice; use App\Utils\Ninja; +use App\Utils\Traits\MakesDates; use App\Utils\Traits\MakesReminders; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; @@ -26,7 +28,7 @@ use Illuminate\Support\Carbon; class ReminderJob implements ShouldQueue { - use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesReminders; + use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, MakesReminders, MakesDates; public function __construct() { @@ -65,6 +67,8 @@ class ReminderJob implements ShouldQueue if ($invoice->isPayable()) { $reminder_template = $invoice->calculateTemplate('invoice'); $invoice->service()->touchReminder($reminder_template)->save(); + + $invoice = $this->calcLateFee($invoice, $reminder_template); $invoice->invitations->each(function ($invitation) use ($invoice, $reminder_template) { EmailEntity::dispatch($invitation, $invitation->company, $reminder_template); @@ -84,4 +88,89 @@ class ReminderJob implements ShouldQueue }); } + + + /** + * Calculates the late if - if any - and rebuilds the invoice + * + * @param Invoice $invoice + * @param string $template + * @return Invoice + */ + private function calcLateFee($invoice, $template) :Invoice + { + $late_fee_amount = 0; + $late_fee_percent = 0; + + switch ($template) { + case 'reminder1': + $late_fee_amount = $invoice->client->getSetting('late_fee_amount1'); + $late_fee_percent = $invoice->client->getSetting('late_fee_percent1'); + break; + case 'reminder2': + $late_fee_amount = $invoice->client->getSetting('late_fee_amount2'); + $late_fee_percent = $invoice->client->getSetting('late_fee_percent2'); + break; + case 'reminder3': + $late_fee_amount = $invoice->client->getSetting('late_fee_amount3'); + $late_fee_percent = $invoice->client->getSetting('late_fee_percent3'); + break; + case 'endless_reminder': + $late_fee_amount = $invoice->client->getSetting('late_fee_endless_amount'); + $late_fee_percent = $invoice->client->getSetting('late_fee_endless_percent'); + break; + default: + $late_fee_amount = 0; + $late_fee_percent = 0; + break; + } + + return $this->setLateFee($invoice, $late_fee_amount, $late_fee_percent); + } + + /** + * Applies the late fee to the invoice line items + * + * @param Invoice $invoice + * @param float $amount The fee amount + * @param float $percent The fee percentage amount + * + * @return Invoice + */ + private function setLateFee($invoice, $amount, $percent) :Invoice + { + $temp_invoice_balance = $invoice->balance; + + if ($amount <= 0 && $percent <= 0) { + return $invoice; + } + + $fee = $amount; + + if ($invoice->partial > 0) { + $fee += round($invoice->partial * $percent / 100, 2); + } else { + $fee += round($invoice->balance * $percent / 100, 2); + } + + $invoice_item = new InvoiceItem; + $invoice_item->type_id = '5'; + $invoice_item->product_key = trans('texts.fee'); + $invoice_item->notes = ctrans('texts.late_fee_added', ['date' => $this->translateDate(now()->startOfDay(), $invoice->client->date_format(), $invoice->client->locale())]); + $invoice_item->quantity = 1; + $invoice_item->cost = $fee; + + $invoice_items = $invoice->line_items; + $invoice_items[] = $invoice_item; + + $invoice->line_items = $invoice_items; + + /**Refresh Invoice values*/ + $invoice = $invoice->calc()->getInvoice(); + + $invoice->client->service()->updateBalance($this->invoice->balance - $temp_invoice_balance)->save(); + $invoice->ledger()->updateInvoiceBalance($this->invoice->balance - $temp_invoice_balance, "Late Fee Adjustment for invoice {$this->invoice->number}"); + + return $invoice; + } } diff --git a/app/Models/ClientGatewayToken.php b/app/Models/ClientGatewayToken.php index 3ff2b3a40a40..cd3d2733602d 100644 --- a/app/Models/ClientGatewayToken.php +++ b/app/Models/ClientGatewayToken.php @@ -35,6 +35,7 @@ class ClientGatewayToken extends BaseModel 'gateway_customer_reference', 'gateway_type_id', 'meta', + 'client_id', ]; public function getEntityType() diff --git a/app/Repositories/CompanyRepository.php b/app/Repositories/CompanyRepository.php index e1a919bca52b..9e56843de497 100644 --- a/app/Repositories/CompanyRepository.php +++ b/app/Repositories/CompanyRepository.php @@ -48,6 +48,25 @@ class CompanyRepository extends BaseRepository private function parseCustomFields($fields) :array { + + if(array_key_exists('account1', $fields)) + $fields['company1'] = $fields['account1']; + + if(array_key_exists('company2', $fields)) + $fields['company2'] = $fields['account2']; + + if(array_key_exists('invoice1', $fields)) + $fields['surcharge1'] = $fields['invoice1']; + + if(array_key_exists('invoice2', $fields)) + $fields['surcharge2'] = $fields['invoice2']; + + if(array_key_exists('invoice_text1', $fields)) + $fields['invoice1'] = $fields['invoice_text1']; + + if(array_key_exists('invoice_text2', $fields)) + $fields['invoice2'] = $fields['invoice_text2']; + foreach ($fields as &$value) { $value = (string) $value; } diff --git a/app/Repositories/GroupSettingRepository.php b/app/Repositories/GroupSettingRepository.php index fdd5bb5e0d33..cbb72b343d89 100644 --- a/app/Repositories/GroupSettingRepository.php +++ b/app/Repositories/GroupSettingRepository.php @@ -29,9 +29,7 @@ class GroupSettingRepository extends BaseRepository $group_setting->save(); } - nlog($data['settings']); - - if(count((array)$data['settings']) == 0){ + if(!array_key_exists('settings', $data) || count((array)$data['settings']) == 0){ $settings = new \stdClass; $settings->entity = Client::class; $group_setting->settings = $settings; diff --git a/app/Repositories/PaymentRepository.php b/app/Repositories/PaymentRepository.php index e17cff099658..411a052aab6a 100644 --- a/app/Repositories/PaymentRepository.php +++ b/app/Repositories/PaymentRepository.php @@ -157,9 +157,11 @@ class PaymentRepository extends BaseRepository { if ( ! $is_existing_payment && ! $this->import_mode ) { - if ($payment->client->getSetting('client_manual_payment_notification')) + if (array_key_exists('email_receipt', $data) && $data['email_receipt'] == true) $payment->service()->sendEmail(); - + elseif(!array_key_exists('email_receipt', $data) && $payment->client->getSetting('client_manual_payment_notification')) + $payment->service()->sendEmail(); + event( new PaymentWasCreated( $payment, $payment->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null) ) ); }