1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00

Improvements for credits

This commit is contained in:
David Bomba 2022-01-28 16:30:40 +11:00
parent c7c26532c5
commit 548510d686
10 changed files with 107 additions and 10 deletions

View File

@ -1 +1 @@
5.3.52
5.3.53

View File

@ -192,7 +192,7 @@ class Handler extends ExceptionHandler
} elseif ($exception instanceof MethodNotAllowedHttpException && $request->expectsJson()) {
return response()->json(['message'=>'Method not supported for this route'], 404);
} elseif ($exception instanceof ValidationException && $request->expectsJson()) {
nlog($exception->validator->getMessageBag());
// nlog($exception->validator->getMessageBag());
return response()->json(['message' => 'The given data was invalid.', 'errors' => $exception->validator->getMessageBag()], 422);
} elseif ($exception instanceof RelationNotFoundException && $request->expectsJson()) {
return response()->json(['message' => $exception->getMessage()], 400);

View File

@ -203,6 +203,11 @@ class CreditController extends BaseController
->triggeredActions($request)
->save();
if($credit->invoice_id){
$credit = $credit->service()->markSent()->save();
$credit->client->service()->updatePaidToDate(-1 * $credit->balance)->save();
}
event(new CreditWasCreated($credit, $credit->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));
return $this->itemResponse($credit);

View File

@ -42,7 +42,10 @@ class HostedMigrationController extends Controller
}
$account = CreateAccount::dispatchNow($request->all(), $request->getClientIp());
$account->hosted_client_count = 100;
$account->hosted_company_count = 10;
$account->save();
$company = $account->companies->first();
$company_token = CompanyToken::where('user_id', auth()->user()->id)

View File

@ -16,7 +16,7 @@ class Cors
// ALLOW OPTIONS METHOD
$headers = [
'Access-Control-Allow-Methods'=> 'POST, GET, OPTIONS, PUT, DELETE',
'Access-Control-Allow-Headers'=> 'X-API-COMPANY-KEY,X-CLIENT-VERSION,X-API-SECRET,X-API-TOKEN,X-API-PASSWORD,DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,X-CSRF-TOKEN,X-XSRF-TOKEN,X-LIVEWIRE',
'Access-Control-Allow-Headers'=> 'X-API-PASSWORD-BASE64,X-API-COMPANY-KEY,X-CLIENT-VERSION,X-API-SECRET,X-API-TOKEN,X-API-PASSWORD,DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,X-CSRF-TOKEN,X-XSRF-TOKEN,X-LIVEWIRE',
];
return Response::make('OK', 200, $headers);
@ -26,7 +26,7 @@ class Cors
$response->headers->set('Access-Control-Allow-Origin', '*');
$response->headers->set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
$response->headers->set('Access-Control-Allow-Headers', 'X-API-COMPANY-KEY,X-API-SECRET,X-API-TOKEN,X-API-PASSWORD,DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,X-CSRF-TOKEN,X-XSRF-TOKEN,X-LIVEWIRE');
$response->headers->set('Access-Control-Allow-Headers', 'X-API-PASSWORD-BASE64,X-API-COMPANY-KEY,X-API-SECRET,X-API-TOKEN,X-API-PASSWORD,DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,X-CSRF-TOKEN,X-XSRF-TOKEN,X-LIVEWIRE');
$response->headers->set('Access-Control-Expose-Headers', 'X-APP-VERSION,X-MINIMUM-CLIENT-VERSION');
$response->headers->set('X-APP-VERSION', config('ninja.app_version'));
$response->headers->set('X-MINIMUM-CLIENT-VERSION', config('ninja.minimum_client_version'));

View File

@ -13,6 +13,7 @@ namespace App\Http\Requests\Credit;
use App\Http\Requests\Request;
use App\Http\ValidationRules\Credit\UniqueCreditNumberRule;
use App\Http\ValidationRules\Credit\ValidInvoiceCreditRule;
use App\Models\Credit;
use App\Utils\Traits\CleanLineItems;
use App\Utils\Traits\MakesHash;
@ -58,7 +59,8 @@ class StoreCreditRequest extends Request
$rules['number'] = ['nullable', Rule::unique('credits')->where('company_id', auth()->user()->company()->id)];
$rules['discount'] = 'sometimes|numeric';
if($this->invoice_id)
$rules['invoice_id'] = new ValidInvoiceCreditRule();
$rules['line_items'] = 'array';

View File

@ -0,0 +1,79 @@
<?php
/**
* Credit Ninja (https://creditninja.com).
*
* @link https://github.com/creditninja/creditninja source repository
*
* @copyright Copyright (c) 2021. Credit Ninja LLC (https://creditninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\ValidationRules\Credit;
use App\Models\Invoice;
use Illuminate\Contracts\Validation\Rule;
/**
* Class ValidInvoiceCreditRule.
*/
class ValidInvoiceCreditRule implements Rule
{
public $error_message;
public function __construct()
{
}
/**
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
return $this->checkIfCreditInvoiceValid($value); //if it exists, return false!
}
/**
* @return string
*/
public function message()
{
return $this->error_message;
}
/**
* @return bool
*/
private function checkIfCreditInvoiceValid($value) : bool
{
$invoice = Invoice::withTrashed()->find($value);
if($invoice->balance >= $invoice->amount){
$this->error_message = "Cannot reverse an invoice with no payment applied.";
return false;
}
$existing_credit_amounts = $invoice->credits()->sum('amount');
if($this->sumCredit() > ($invoice->amount - $invoice->balance - $existing_credit_amounts)){
$this->error_message = "Credit cannot exceed the payment / credits already applied to invoice.";
return false;
}
return true;
}
private function sumCredit()
{
$cost = 0;
foreach(request()->input('line_items') as $item)
{
$cost += $item['cost'] * $item['quantity'];
}
return $cost;
}
}

View File

@ -82,7 +82,8 @@ class CreateAccount
if(Ninja::isHosted())
{
$sp794f3f->hosted_client_count = config('ninja.quotas.free.clients');
$sp794f3f->hosted_company_count = config('ninja.quotas.free.max_companies');
$sp794f3f->trial_started = now();
$sp794f3f->trial_plan = 'pro';

View File

@ -327,6 +327,8 @@ class BaseRepository
if (! $model->design_id)
$model->design_id = $this->decodePrimaryKey($client->getSetting('credit_design_id'));
if(array_key_exists('invoice_id', $data) && $data['invoice_id'])
$model->invoice_id = $data['invoice_id'];
if($this->new_model)
event('eloquent.created: App\Models\Credit', $model);

View File

@ -14,8 +14,8 @@ return [
'require_https' => env('REQUIRE_HTTPS', true),
'app_url' => rtrim(env('APP_URL', ''), '/'),
'app_domain' => env('APP_DOMAIN', 'invoicing.co'),
'app_version' => '5.3.52',
'app_tag' => '5.3.52',
'app_version' => '5.3.53',
'app_tag' => '5.3.53',
'minimum_client_version' => '5.0.16',
'terms_version' => '1.0.1',
'api_secret' => env('API_SECRET', ''),
@ -134,14 +134,19 @@ return [
],
'quotas' => [
'free' => [
'clients' => 50,
'daily_emails' => 50,
'clients' => 20,
'max_companies' => 1,
],
'pro' => [
'daily_emails' => 100,
'clients' => 1000000,
'max_companies' => 10,
],
'enterprise' => [
'daily_emails' => 200,
'clients' => 1000000,
'max_companies' => 10,
],
],
'auth' => [