1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 06:32:40 +01:00

Merge pull request #7196 from turbo124/v5-develop

Fixes for currency set for payments
This commit is contained in:
David Bomba 2022-02-08 23:30:23 +11:00 committed by GitHub
commit 91d63f5321
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 251 additions and 5 deletions

View File

@ -2,12 +2,19 @@
namespace App\Http\Controllers\ClientPortal;
use App\Events\Credit\CreditWasViewed;
use App\Events\Invoice\InvoiceWasViewed;
use App\Events\Misc\InvitationWasViewed;
use App\Events\Quote\QuoteWasViewed;
use App\Http\Controllers\Controller;
use App\Utils\Ninja;
use App\Utils\Traits\MakesHash;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use Illuminate\View\View;
class EntityViewController extends Controller
@ -19,7 +26,7 @@ class EntityViewController extends Controller
*
* @var array
*/
private $entity_types = ['invoice', 'quote'];
private $entity_types = ['invoice', 'quote', 'credit', 'recurring_invoice'];
/**
* Show the entity outside client portal.
@ -118,4 +125,54 @@ class EntityViewController extends Controller
return back();
}
public function handlePasswordSet(Request $request)
{
$entity_obj = 'App\Models\\'.ucfirst(Str::camel($request->entity_type)).'Invitation';
$key = $request->entity_type.'_id';
$invitation = $entity_obj::where('key', $request->invitation_key)
->whereHas($request->entity_type, function ($query) {
$query->where('is_deleted',0);
})
->with('contact.client')
->first();
$contact = $invitation->contact;
$contact->password = Hash::make($request->password);
$contact->save();
$request->session()->invalidate();
auth()->guard('contact')->loginUsingId($contact->id, true);
if (! $invitation->viewed_date) {
$invitation->markViewed();
event(new InvitationWasViewed($invitation->{$request->entity_type}, $invitation, $invitation->{$request->entity_type}->company, Ninja::eventVars()));
$this->fireEntityViewedEvent($invitation, $request->entity_type);
}
return redirect()->route('client.'.$request->entity_type.'.show', [$request->entity_type => $this->encodePrimaryKey($invitation->{$key})]);
}
private function fireEntityViewedEvent($invitation, $entity_string)
{
switch ($entity_string) {
case 'invoice':
event(new InvoiceWasViewed($invitation, $invitation->company, Ninja::eventVars()));
break;
case 'quote':
event(new QuoteWasViewed($invitation, $invitation->company, Ninja::eventVars()));
break;
case 'credit':
event(new CreditWasViewed($invitation, $invitation->company, Ninja::eventVars()));
break;
default:
// code...
break;
}
}
}

View File

@ -102,6 +102,17 @@ class InvitationController extends Controller
auth()->guard('contact')->loginUsingId($client_contact->id, true);
} elseif ((bool) $invitation->contact->client->getSetting('enable_client_portal_password') !== false) {
//if no contact password has been set - allow user to set password - then continue to view entity
if(empty($invitation->contact->password)){
return $this->render('view_entity.set_password', [
'root' => 'themes',
'entity_type' => $entity,
'invitation_key' => $invitation_key
]);
}
$this->middleware('auth:contact');
return redirect()->route('client.login');

View File

@ -160,7 +160,7 @@ class QuoteController extends Controller
$quotes = Quote::whereIn('id', $ids)
->where('client_id', auth('contact')->user()->client->id)
->where('company_id', auth('contact')->user()->client->company_id)
->where('status_id', Quote::STATUS_SENT)
->whereIn('status_id', [Quote::STATUS_DRAFT, Quote::STATUS_SENT])
->withTrashed()
->get();

View File

@ -76,6 +76,10 @@ class StoreRecurringInvoiceRequest extends Request
$input['assigned_user_id'] = $this->decodePrimaryKey($input['assigned_user_id']);
}
if (array_key_exists('vendor_id', $input) && is_string($input['vendor_id'])) {
$input['vendor_id'] = $this->decodePrimaryKey($input['vendor_id']);
}
if (isset($input['client_contacts'])) {
foreach ($input['client_contacts'] as $key => $contact) {
if (! array_key_exists('send_email', $contact) || ! array_key_exists('id', $contact)) {

View File

@ -78,6 +78,7 @@ class Credit extends BaseModel
'assigned_user_id',
'exchange_rate',
'subscription_id',
'vendor_id',
];
protected $casts = [
@ -123,6 +124,11 @@ class Credit extends BaseModel
return $this->belongsTo(User::class, 'assigned_user_id', 'id')->withTrashed();
}
public function vendor()
{
return $this->belongsTo(Vendor::class);
}
public function history()
{
return $this->hasManyThrough(Backup::class, Activity::class);

View File

@ -76,6 +76,7 @@ class Quote extends BaseModel
'exchange_rate',
'subscription_id',
'uses_inclusive_taxes',
'vendor_id',
];
protected $casts = [
@ -133,6 +134,11 @@ class Quote extends BaseModel
return $this->belongsTo(Company::class);
}
public function vendor()
{
return $this->belongsTo(Vendor::class);
}
public function history()
{
return $this->hasManyThrough(Backup::class, Activity::class);

View File

@ -107,6 +107,7 @@ class RecurringInvoice extends BaseModel
'design_id',
'assigned_user_id',
'exchange_rate',
'vendor_id',
];
protected $casts = [
@ -157,6 +158,11 @@ class RecurringInvoice extends BaseModel
return $value;
}
public function vendor()
{
return $this->belongsTo(Vendor::class);
}
public function activities()
{
return $this->hasMany(Activity::class)->orderBy('id', 'DESC')->take(50);

View File

@ -202,6 +202,9 @@ class PaymentMigrationRepository extends BaseRepository
*/
private function processExchangeRates($data, $payment)
{
if($payment->exchange_rate != 1)
return $payment;
$client = Client::where('id', $data['client_id'])->withTrashed()->first();
$client_currency = $client->getSetting('currency_id');

View File

@ -210,6 +210,7 @@ class PaymentRepository extends BaseRepository {
$payment->exchange_currency_id = $company_currency;
$payment->currency_id = $client_currency;
return $payment;
}
$payment->currency_id = $company_currency;

View File

@ -111,6 +111,9 @@ class ApplyPaymentAmount extends AbstractService
private function setExchangeRate(Payment $payment)
{
if($payment->exchange_rate != 1)
return;
$client_currency = $payment->client->getSetting('currency_id');
$company_currency = $payment->client->company->settings->currency_id;

View File

@ -79,6 +79,9 @@ class InvoiceService
public function setExchangeRate()
{
if($this->invoice->exchange_rate != 1)
return $this;
$client_currency = $this->invoice->client->getSetting('currency_id');
$company_currency = $this->invoice->company->settings->currency_id;

View File

@ -116,6 +116,9 @@ class MarkPaid extends AbstractService
private function setExchangeRate(Payment $payment)
{
if($payment->exchange_rate != 1)
return;
$client_currency = $payment->client->getSetting('currency_id');
$company_currency = $payment->client->company->settings->currency_id;

View File

@ -89,6 +89,7 @@ class CreditTransformer extends EntityTransformer
'user_id' => $this->encodePrimaryKey($credit->user_id),
'project_id' => $this->encodePrimaryKey($credit->project_id),
'assigned_user_id' => $this->encodePrimaryKey($credit->assigned_user_id),
'vendor_id' => (string) $this->encodePrimaryKey($credit->vendor_id),
'amount' => (float) $credit->amount,
'balance' => (float) $credit->balance,
'client_id' => (string) $this->encodePrimaryKey($credit->client_id),

View File

@ -95,6 +95,7 @@ class QuoteTransformer extends EntityTransformer
'status_id' => (string) $quote->status_id,
'design_id' => (string) $this->encodePrimaryKey($quote->design_id),
'invoice_id' => (string) $this->encodePrimaryKey($quote->invoice_id),
'vendor_id' => (string) $this->encodePrimaryKey($quote->vendor_id),
'updated_at' => (int) $quote->updated_at,
'archived_at' => (int) $quote->deleted_at,
'created_at' => (int) $quote->created_at,

View File

@ -4544,6 +4544,7 @@ $LANG = array(
'activity_123' => ':user deleted recurring expense :recurring_expense',
'activity_124' => ':user restored recurring expense :recurring_expense',
'fpx' => "FPX",
'to_view_entity_set_password' => 'To view the :entity you need to set password.',
);

View File

@ -21,14 +21,12 @@
@endcomponent
@endif
@if($quote->status_id === \App\Models\Quote::STATUS_SENT)
@if(in_array($quote->status_id, [\App\Models\Quote::STATUS_SENT, \App\Models\Quote::STATUS_DRAFT]))
<div class="mb-4">
@include('portal.ninja2020.quotes.includes.actions', ['quote' => $quote])
</div>
@elseif($quote->status_id === \App\Models\Quote::STATUS_APPROVED)
<p class="text-right text-gray-900 text-sm mb-4">{{ ctrans('texts.approved') }}</p>
@else
<p class="text-right text-gray-900 text-sm mb-4">{{ ctrans('texts.quotes_with_status_sent_can_be_approved') }}</p>
@endif
@include('portal.ninja2020.components.entity-documents', ['entity' => $quote])

View File

@ -18,6 +18,7 @@ Route::post('client/password/reset', 'Auth\ContactResetPasswordController@reset'
Route::get('view/{entity_type}/{invitation_key}', 'ClientPortal\EntityViewController@index')->name('client.entity_view');
Route::get('view/{entity_type}/{invitation_key}/password', 'ClientPortal\EntityViewController@password')->name('client.entity_view.password');
Route::post('view/{entity_type}/{invitation_key}/password', 'ClientPortal\EntityViewController@handlePassword');
Route::post('set_password', 'ClientPortal\EntityViewController@handlePasswordSet')->name('client.set_password');
Route::get('tmp_pdf/{hash}', 'ClientPortal\TempRouteController@index')->name('tmp_pdf');

View File

@ -41,6 +41,147 @@ class VendorApiTest extends TestCase
Model::reguard();
}
public function testAddVendorToInvoice()
{
$data = [
'name' => $this->faker->firstName,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/vendors', $data);
$response->assertStatus(200);
$arr = $response->json();
$vendor_id =$arr['data']['id'];
$data = [
'vendor_id' => $vendor_id,
'client_id' => $this->client->hashed_id
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/invoices', $data);
$response->assertStatus(200);
$arr = $response->json();
$this->assertEquals($arr['data']['vendor_id'], $vendor_id);
}
public function testAddVendorToRecurringInvoice()
{
$data = [
'name' => $this->faker->firstName,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/vendors', $data);
$response->assertStatus(200);
$arr = $response->json();
$vendor_id = $arr['data']['id'];
$data = [
'vendor_id' => $vendor_id,
'client_id' => $this->client->hashed_id,
'frequency_id' => 1,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/recurring_invoices', $data);
$response->assertStatus(200);
$arr = $response->json();
$this->assertEquals($arr['data']['vendor_id'], $vendor_id);
}
public function testAddVendorToQuote()
{
$data = [
'name' => $this->faker->firstName,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/vendors', $data);
$response->assertStatus(200);
$arr = $response->json();
$vendor_id =$arr['data']['id'];
$data = [
'vendor_id' => $vendor_id,
'client_id' => $this->client->hashed_id
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/quotes', $data);
$response->assertStatus(200);
$arr = $response->json();
$this->assertEquals($arr['data']['vendor_id'], $vendor_id);
}
public function testAddVendorToCredit()
{
$data = [
'name' => $this->faker->firstName,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/vendors', $data);
$response->assertStatus(200);
$arr = $response->json();
$vendor_id =$arr['data']['id'];
$data = [
'vendor_id' => $vendor_id,
'client_id' => $this->client->hashed_id
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/credits', $data);
$response->assertStatus(200);
$arr = $response->json();
$this->assertEquals($arr['data']['vendor_id'], $vendor_id);
}
public function testVendorPost()
{
$data = [