1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 00:41:34 +02:00

Merge pull request #4002 from turbo124/v2

Tests for gateway fees
This commit is contained in:
David Bomba 2020-08-20 07:40:33 +10:00 committed by GitHub
commit c9d94804de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 150 additions and 39 deletions

View File

@ -109,6 +109,8 @@ class CompanySettings extends BaseSettings
public $reset_counter_date = '';
public $counter_padding = 4;
public $auto_bill = 'off'; //off,always,optin,optout
public $design = 'views/pdf/design1.blade.php';
public $invoice_terms = '';
@ -238,6 +240,7 @@ class CompanySettings extends BaseSettings
public $client_portal_enable_uploads = false;
public static $casts = [
'auto_bill' => 'string',
'lock_invoices' => 'string',
'client_portal_terms' => 'string',
'client_portal_privacy_policy' => 'string',

View File

@ -37,6 +37,9 @@ class Handler extends ExceptionHandler
*/
protected $dontReport = [
\PDOException::class,
\Swift_TransportException::class,
\Illuminate\Queue\MaxAttemptsExceededException::class,
\Symfony\Component\Console\Exception\CommandNotFoundException::class
];
/**

View File

@ -147,6 +147,8 @@ class MigrationController extends BaseController
{
$company->clients()->delete();
$company->products()->delete();
$company->save();
return response()->json(['message' => 'Settings preserved'], 200);

View File

@ -60,7 +60,7 @@ class EmailQuote implements ShouldQueue
return $this->logMailError(Mail::failures());
}
$this->quote_invitation->quote->markSent()->save();
$this->quote_invitation->quote->service()->markSent()->save();
}

View File

@ -460,7 +460,8 @@ class Client extends BaseModel implements HasLocalePreference
$company_gateways = $this->getSetting('company_gateway_ids');
if ($company_gateways) {
if ($company_gateways || $company_gateways == "0") { //we need to check for "0" here as we disable a payment gateway for a client with the number "0"
$transformed_ids = $this->transformKeys(explode(",", $company_gateways));
$gateways = $this->company
->company_gateways

View File

@ -43,6 +43,7 @@ class CompanyGateway extends BaseModel
'custom_value2',
'custom_value3',
'custom_value4',
'token_billing',
];
public static $credit_cards = [
@ -270,8 +271,9 @@ class CompanyGateway extends BaseModel
info("fee after adding fee tax 3 = {$fee}");
}
//TODO CALCULATE FEE CAP HERE
if($fees_and_limits->fee_cap > 0 && ($fee > $fees_and_limits->fee_cap))
$fee = $fees_and_limits->fee_cap;
return $fee;
}

View File

@ -26,6 +26,7 @@ class Document extends BaseModel
*/
protected $fillable = [
'is_default',
'is_public',
];

View File

@ -268,7 +268,7 @@ class BasePaymentDriver
$payment->currency_id = $this->client->getSetting('currency_id');
$payment->date = Carbon::now();
return $payment;
return $payment->service()->applyNumber()->save();
}

View File

@ -160,7 +160,7 @@ class Charge
return false;
$payment_method_type = $response->charges->data[0]->payment_method_details->card->brand;
info($payment_method_type);
//info($payment_method_type);
$data = [
'gateway_type_id' => $cgt->gateway_type_id,

View File

@ -393,9 +393,8 @@ class StripePaymentDriver extends BasePaymentDriver
$payment->date = Carbon::now();
$payment->transaction_reference = $data['transaction_reference'];
$payment->amount = $amount;
$payment->client->getNextPaymentNumber($this->client);
$payment->save();
return $payment;
return $payment->service()->applyNumber()->save();
}
}

View File

@ -69,6 +69,7 @@ class ActivityRepository extends BaseRepository
if (get_class($entity) == Invoice::class || get_class($entity) == Quote::class || get_class($entity) == Credit::class){
$contact = $entity->client->primary_contact()->first();
$backup->html_backup = $this->generateEntityHtml($entity->getEntityDesigner(), $entity, $contact);
$backup->amount = $entity->amount;
}
$backup->activity_id = $activity->id;

View File

@ -71,8 +71,6 @@ class PaymentRepository extends BaseRepository
private function applyPayment(array $data, Payment $payment): ?Payment
{
info(print_r($data,1));
//check currencies here and fill the exchange rate data if necessary
if (!$payment->id) {
$this->processExchangeRates($data, $payment);
@ -149,10 +147,10 @@ info(print_r($data,1));
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
/*info("invoice totals = {$invoice_totals}");
info("credit totals = {$credit_totals}");
info("applied totals = " . array_sum(array_column($data['invoices'], 'amount')));
*/
/*info("invoice totals = {$invoice_totals}");
info("credit totals = {$credit_totals}");
info("applied totals = " . array_sum(array_column($data['invoices'], 'amount')));
*/
//$invoice_totals -= $credit_totals;
////$payment->amount = $invoice_totals; //creates problems when setting amount like this.

View File

@ -39,7 +39,7 @@ class SendEmail
* @param string $this->reminder_template The template name ie reminder1
* @return array
*/
public function run(): array
public function run()
{
if (!$this->reminder_template) {
$this->reminder_template = $this->quote->calculateTemplate();
@ -52,5 +52,7 @@ class SendEmail
EmailQuote::dispatchNow($email_builder, $invitation);
}
});
$this->quote->service()->markSent();
}
}

View File

@ -27,7 +27,6 @@ class CreditTransformer extends EntityTransformer
protected $defaultIncludes = [
'invitations',
'documents',
'history',
];
protected $availableIncludes = [
@ -136,6 +135,7 @@ class CreditTransformer extends EntityTransformer
'custom_surcharge_tax4' => (bool) $credit->custom_surcharge_tax4,
'line_items' => $credit->line_items ?: (array)[],
'entity_type' => 'credit',
'exchange_rate' => (float)$credit->exchange_rate,
];
}
}

View File

@ -35,6 +35,7 @@ class InvoiceHistoryTransformer extends EntityTransformer
'activity_id' => $this->encodePrimaryKey($backup->activity_id),
'json_backup' => (string) $backup->json_backup ?: '',
'html_backup' => (string) $backup->html_backup ?: '',
'amount' => (float) $backup->amount,
'created_at' => (int)$backup->created_at,
'updated_at' => (int)$backup->updated_at,
];

View File

@ -135,6 +135,7 @@ class InvoiceTransformer extends EntityTransformer
'custom_surcharge2' => (float)$invoice->custom_surcharge2,
'custom_surcharge3' => (float)$invoice->custom_surcharge3,
'custom_surcharge4' => (float)$invoice->custom_surcharge4,
'exchange_rate' => (float)$invoice->exchange_rate,
'custom_surcharge_tax1' => (bool) $invoice->custom_surcharge_tax1,
'custom_surcharge_tax2' => (bool) $invoice->custom_surcharge_tax2,
'custom_surcharge_tax3' => (bool) $invoice->custom_surcharge_tax3,

View File

@ -27,7 +27,6 @@ class QuoteTransformer extends EntityTransformer
protected $defaultIncludes = [
'invitations',
'documents',
'history'
];
protected $availableIncludes = [
@ -135,7 +134,7 @@ class QuoteTransformer extends EntityTransformer
'custom_surcharge_taxes' => (bool) $quote->custom_surcharge_taxes,
'line_items' => $quote->line_items ?: (array)[],
'entity_type' => 'quote',
'exchange_rate' => (float)$quote->exchange_rate,
];
}
}

View File

@ -122,7 +122,9 @@ class HtmlEngine
$data['$number'] = ['value' => $this->entity->number ?: ' ', 'label' => ctrans('texts.invoice_number')];
$data['$entity.terms'] = ['value' => $this->entity->terms ?: ' ', 'label' => ctrans('texts.invoice_terms')];
$data['$terms'] = &$data['$entity.terms'];
$data['$view_link'] = ['value' => '<a href="' .$this->invitation->getLink() .'">'. ctrans('texts.view_invoice').'</a>', 'label' => ctrans('texts.view_invoice')];
// $data['$view_link'] = ['value' => '<a href="' .$this->invitation->getLink() .'">'. ctrans('texts.view_invoice').'</a>', 'label' => ctrans('texts.view_invoice')];
$data['$view_link'] = ['value' => $this->invitation->getLink(), 'label' => ctrans('texts.view_invoice')];
}
if ($this->entity_string == 'quote') {
@ -130,7 +132,8 @@ class HtmlEngine
$data['$number'] = ['value' => $this->entity->number ?: '&nbsp;', 'label' => ctrans('texts.quote_number')];
$data['$entity.terms'] = ['value' => $this->entity->terms ?: '&nbsp;', 'label' => ctrans('texts.quote_terms')];
$data['$terms'] = &$data['$entity.terms'];
$data['$view_link'] = ['value' => '<a href="' .$this->invitation->getLink() .'">'. ctrans('texts.view_quote').'</a>', 'label' => ctrans('texts.view_quote')];
// $data['$view_link'] = ['value' => '<a href="' .$this->invitation->getLink() .'">'. ctrans('texts.view_quote').'</a>', 'label' => ctrans('texts.view_quote')];
$data['$view_link'] = ['value' => $this->invitation->getLink(), 'label' => ctrans('texts.view_quote')];
}
if ($this->entity_string == 'credit') {
@ -138,7 +141,8 @@ class HtmlEngine
$data['$number'] = ['value' => $this->entity->number ?: '&nbsp;', 'label' => ctrans('texts.credit_number')];
$data['$entity.terms'] = ['value' => $this->entity->terms ?: '&nbsp;', 'label' => ctrans('texts.credit_terms')];
$data['$terms'] = &$data['$entity.terms'];
$data['$view_link'] = ['value' => '<a href="' .$this->invitation->getLink() .'">'. ctrans('texts.view_credit').'</a>', 'label' => ctrans('texts.view_credit')];
// $data['$view_link'] = ['value' => '<a href="' .$this->invitation->getLink() .'">'. ctrans('texts.view_credit').'</a>', 'label' => ctrans('texts.view_credit')];
$data['$view_link'] = ['value' => $this->invitation->getLink(), 'label' => ctrans('texts.view_credit')];
}
$data['$entity_number'] = &$data['$number'];

View File

@ -82,12 +82,12 @@ class SystemHealth
exec('node -v', $foo, $exitCode);
if ($exitCode === 0) {
return $foo[0];
return empty($foo[0]) ? 'Found node, but no version information' : $foo[0];
}
} catch (\Exception $e) {
return false;
return false;
}
}
@ -98,12 +98,11 @@ class SystemHealth
exec('npm -v', $foo, $exitCode);
if ($exitCode === 0) {
return $foo[0];
return empty($foo[0]) ? 'Found npm, but no version information' : $foo[0];
}
}catch (\Exception $e) {
return false;
return false;
}
}

View File

@ -215,7 +215,8 @@ trait MakesInvoiceValues
$data['$number'] = ['value' => $this->number ?: '&nbsp;', 'label' => ctrans('texts.invoice_number')];
$data['$entity.terms'] = ['value' => $this->terms ?: '&nbsp;', 'label' => ctrans('texts.invoice_terms')];
$data['$terms'] = &$data['$entity.terms'];
$data['$view_link'] = ['value' => '<a href="' .$invitation->getLink() .'">'. ctrans('texts.view_invoice').'</a>', 'label' => ctrans('texts.view_invoice')];
//$data['$view_link'] = ['value' => '<a href="' .$invitation->getLink() .'">'. ctrans('texts.view_invoice').'</a>', 'label' => ctrans('texts.view_invoice')];
$data['$view_link'] = ['value' => $invitation->getLink(), 'label' => ctrans('texts.view_invoice')];
}
if ($this instanceof Quote) {
@ -223,7 +224,8 @@ trait MakesInvoiceValues
$data['$number'] = ['value' => $this->number ?: '&nbsp;', 'label' => ctrans('texts.quote_number')];
$data['$entity.terms'] = ['value' => $this->terms ?: '&nbsp;', 'label' => ctrans('texts.quote_terms')];
$data['$terms'] = &$data['$entity.terms'];
$data['$view_link'] = ['value' => '<a href="' .$invitation->getLink() .'">'. ctrans('texts.view_quote').'</a>', 'label' => ctrans('texts.view_quote')];
// $data['$view_link'] = ['value' => '<a href="' .$invitation->getLink() .'">'. ctrans('texts.view_quote').'</a>', 'label' => ctrans('texts.view_quote')];
$data['$view_link'] = ['value' => $invitation->getLink(), 'label' => ctrans('texts.view_quote')];
}
if ($this instanceof Credit) {
@ -231,7 +233,8 @@ trait MakesInvoiceValues
$data['$number'] = ['value' => $this->number ?: '&nbsp;', 'label' => ctrans('texts.credit_number')];
$data['$entity.terms'] = ['value' => $this->terms ?: '&nbsp;', 'label' => ctrans('texts.credit_terms')];
$data['$terms'] = &$data['$entity.terms'];
$data['$view_link'] = ['value' => '<a href="' .$invitation->getLink() .'">'. ctrans('texts.view_credit').'</a>', 'label' => ctrans('texts.view_credit')];
// $data['$view_link'] = ['value' => '<a href="' .$invitation->getLink() .'">'. ctrans('texts.view_credit').'</a>', 'label' => ctrans('texts.view_credit')];
$data['$view_link'] = ['value' => $invitation->getLink(), 'label' => ctrans('texts.view_credit')];
}
$data['$entity_number'] = &$data['$number'];

View File

@ -30,6 +30,7 @@ return [
'phantomjs_key' => env('PHANTOMJS_KEY', false),
'phantomjs_secret' => env('PHANTOMJS_SECRET', false),
'sentry_dsn' => env('SENTRY_LARAVEL_DSN', 'https://b36c3ae4f26b45689bc3d4e3774fb303@sentry.invoicing.co/4'),
'environment' => env('NINJA_ENVIRONMENT', 'selfhost'), // 'hosted', 'development', 'selfhost', 'reseller'
// Settings used by invoiceninja.com

View File

@ -2,8 +2,8 @@
return [
'dsn' => env('SENTRY_LARAVEL_DSN', env('SENTRY_DSN')),
//'dsn' => env('SENTRY_LARAVEL_DSN', env('SENTRY_DSN')),
'dsn' => config('ninja.sentry_dsn'),
// capture release as git sha
// 'release' => trim(exec('git --git-dir ' . base_path('.git') . ' log --pretty="%h" -n1 HEAD')),

View File

@ -0,0 +1,10 @@
<?php
use Faker\Generator as Faker;
use Illuminate\Support\Str;
$factory->define(App\Models\QuoteInvitation::class, function (Faker $faker) {
return [
'key' => Str::random(40),
];
});

View File

@ -13,9 +13,18 @@ class AddIsPublicToDocumentsTable extends Migration
*/
public function up()
{
Schema::table('documents', function (Blueprint $table) {
$table->boolean('is_public')->default(false);
});
Schema::table('backups', function (Blueprint $table) {
$table->decimal('amount', 16, 4);
});
Schema::table('company_gateways', function (Blueprint $table) {
$table->enum('token_billing', ['off', 'always','optin','optout'])->default('off');
});
}
/**
@ -25,8 +34,6 @@ class AddIsPublicToDocumentsTable extends Migration
*/
public function down()
{
Schema::table('documents', function (Blueprint $table) {
$table->dropColumn('is_public');
});
}
}

View File

@ -26,6 +26,7 @@ use Tests\TestCase;
/**
* @test
* @covers App\Models\CompanyGateway
*/
class CompanyGatewayApiTest extends TestCase
{
@ -352,4 +353,42 @@ class CompanyGatewayApiTest extends TestCase
$this->assertEquals(12, $company_gateway->calcGatewayFee(10));
}
public function testFeesAndLimitsFeePercentAndAmountAndDoubleTaxCalcuationWithFeeCap()
{
//{"1":{"min_limit":1,"max_limit":1000000,"fee_amount":10,"fee_percent":2,"fee_tax_name1":"","fee_tax_name2":"","fee_tax_name3":"","fee_tax_rate1":0,"fee_tax_rate2":0,"fee_tax_rate3":0,"fee_cap":10,"adjust_fee_percent":true}}
$fee = new FeesAndLimits;
$fee->fee_amount = 10;
// $fee->fee_percent = 2;
$fee->fee_tax_name1 = 'GST';
$fee->fee_tax_rate1 = '10.0';
$fee->fee_tax_name2 = 'GST';
$fee->fee_tax_rate2 = '10.0';
$fee->fee_cap = 1;
$fee_arr[1] = (array)$fee;
$data = [
'config' => 'random config',
'gateway_key' => '3b6621f970ab18887c4f6dca78d3f8bb',
'fees_and_limits' => $fee_arr,
];
/* POST */
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token
])->post('/api/v1/company_gateways', $data);
$response->assertStatus(200);
$arr = $response->json();
$id = $this->decodePrimaryKey($arr['data']['id']);
$company_gateway = CompanyGateway::find($id);
$this->assertEquals(1, $company_gateway->calcGatewayFee(10));
}
}

View File

@ -204,7 +204,8 @@ class MultiDBUserTest extends TestCase
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/users?include=company_user', $data);
'X-API-PASSWORD' => 'ALongAndBriliantPassword',
])->post('/api/v1/users?include=company_user', $data);
} catch (ValidationException $e) {
\Log::error('in the validator');
$message = json_decode($e->validator->getMessageBag(), 1);

View File

@ -154,7 +154,7 @@ trait MockAccountData
]);
$contact = factory(\App\Models\ClientContact::class, 1)->create([
$contact = factory(\App\Models\ClientContact::class)->create([
'user_id' => $this->user->id,
'client_id' => $this->client->id,
'company_id' => $this->company->id,
@ -162,12 +162,13 @@ trait MockAccountData
'send_email' => true,
]);
$contact2 = factory(\App\Models\ClientContact::class, 1)->create([
$contact2 = factory(\App\Models\ClientContact::class)->create([
'user_id' => $this->user->id,
'client_id' => $this->client->id,
'company_id' => $this->company->id,
'send_email' => true
]);
// $rels = collect($contact, $contact2);
// $this->client->setRelation('contacts', $rels);
@ -211,7 +212,24 @@ trait MockAccountData
$this->invoice->save();
$this->invoice->service()->createInvitations()->markSent();
//$this->invoice->service()->createInvitations()->markSent();
//$this->invoice->service()->createInvitations();
factory(\App\Models\InvoiceInvitation::class)->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'client_contact_id' => $contact->id,
'invoice_id' => $this->invoice->id,
]);
factory(\App\Models\InvoiceInvitation::class)->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'client_contact_id' => $contact2->id,
'invoice_id' => $this->invoice->id,
]);
$this->invoice->service()->markSent();
$this->quote = factory(\App\Models\Quote::class)->create([
'user_id' => $this->user->id,
@ -229,8 +247,24 @@ trait MockAccountData
$this->quote = $this->quote_calc->getQuote();
$this->quote->status_id = Quote::STATUS_SENT;
$this->quote->number = $this->getNextQuoteNumber($this->client);
$this->quote->service()->createInvitations()->markSent();
//$this->quote->service()->createInvitations()->markSent();
factory(\App\Models\QuoteInvitation::class)->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'client_contact_id' => $contact->id,
'quote_id' => $this->quote->id,
]);
factory(\App\Models\QuoteInvitation::class)->create([
'user_id' => $this->user->id,
'company_id' => $this->company->id,
'client_contact_id' => $contact2->id,
'quote_id' => $this->quote->id,
]);
$this->quote->setRelation('client', $this->client);
$this->quote->setRelation('company', $this->company);