Switch currency_id back to settings object

This commit is contained in:
David Bomba 2019-10-10 21:43:50 +11:00
parent 39eac5709e
commit 8d14c6b2bd
16 changed files with 55 additions and 114 deletions

View File

@ -22,11 +22,11 @@ class CompanySettings extends BaseSettings
public $timezone_id = '';
public $date_format_id = '';
public $datetime_format_id = '';
public $military_time = false;
public $language_id = '';
public $show_currency_code = false;
public $currency_id = '1';
public $payment_terms = 1;
@ -180,6 +180,7 @@ class CompanySettings extends BaseSettings
'phone' => 'string',
'email' => 'string',
'country_id' => 'string',
'currency_id' => 'string',
'vat_number' => 'string',
'id_number' => 'string',
'tax_name1' => 'string',
@ -194,7 +195,6 @@ class CompanySettings extends BaseSettings
'show_accept_invoice_terms' => 'bool',
'timezone_id' => 'string',
'date_format_id' => 'string',
'datetime_format_id' => 'string',
'military_time' => 'bool',
'language_id' => 'string',
'show_currency_code' => 'bool',
@ -300,10 +300,10 @@ class CompanySettings extends BaseSettings
unset($data->protected_fields);
$data->timezone_id = (string)config('ninja.i18n.timezone_id');
$data->currency_id = (string)config('ninja.i18n.currency_id');
$data->language_id = (string)config('ninja.i18n.language_id');
$data->payment_terms = (int)config('ninja.i18n.payment_terms');
$data->datetime_format_id = (string)config('ninja.i18n.datetime_format_id');
$data->military_time = (bool )config('ninja.i18n.military_time');
$data->military_time = (bool)config('ninja.i18n.military_time');
$data->date_format_id = (string)config('ninja.i18n.date_format_id');
$data->country_id = (string)config('ninja.i18n.country_id');
$data->translations = (object) [];

View File

@ -30,7 +30,6 @@ class ClientFactory
$client->country_id = 4;
$client->is_deleted = 0;
$client->client_hash = Str::random(40);
$client->currency_id = config('ninja.i18n.currency_id');
$client->settings = ClientSettings::defaults();
$client_contact = ClientContactFactory::create($company_id, $user_id);

View File

@ -50,7 +50,7 @@ class InvoiceItemCalc
public function process()
{
$this->line_total = $this->formatValue($this->item->cost, $this->invoice->client->currency->precision) * $this->formatValue($this->item->quantity, $this->invoice->client->currency->precision);
$this->line_total = $this->formatValue($this->item->cost, $this->invoice->client->currency()->precision) * $this->formatValue($this->item->quantity, $this->invoice->client->currency()->precision);
$this->setDiscount()
->calcTaxes();
@ -65,7 +65,7 @@ class InvoiceItemCalc
if($this->item->is_amount_discount)
{
$discount = $this->formatValue($this->item->discount, $this->invoice->client->currency->precision);
$discount = $this->formatValue($this->item->discount, $this->invoice->client->currency()->precision);
$this->line_total -= $discount;
@ -73,7 +73,7 @@ class InvoiceItemCalc
}
else
{
$discount = $this->formatValue(($this->line_total * $this->item->discount / 100), $this->invoice->client->currency->precision);
$discount = $this->formatValue(($this->line_total * $this->item->discount / 100), $this->invoice->client->currency()->precision);
$this->line_total -= $discount;
@ -91,12 +91,12 @@ class InvoiceItemCalc
if(isset($this->item->tax_rate1) && $this->item->tax_rate1 > 0)
{
$tax_rate1 = $this->formatValue($this->item->tax_rate1, $this->invoice->client->currency->precision);
$tax_rate1 = $this->formatValue($this->item->tax_rate1, $this->invoice->client->currency()->precision);
if($this->settings->inclusive_taxes)
$item_tax_rate1_total = $this->formatValue(($this->line_total - ($this->line_total / (1+$tax_rate1/100))) , $this->invoice->client->currency->precision);
$item_tax_rate1_total = $this->formatValue(($this->line_total - ($this->line_total / (1+$tax_rate1/100))) , $this->invoice->client->currency()->precision);
else
$item_tax_rate1_total = $this->formatValue(($this->line_total * $tax_rate1/100), $this->invoice->client->currency->precision);
$item_tax_rate1_total = $this->formatValue(($this->line_total * $tax_rate1/100), $this->invoice->client->currency()->precision);
$item_tax += $item_tax_rate1_total;
@ -105,12 +105,12 @@ class InvoiceItemCalc
if(isset($this->item->tax_rate2) && $this->item->tax_rate2 > 0)
{
$tax_rate2 = $this->formatValue($this->item->tax_rate2, $this->invoice->client->currency->precision);
$tax_rate2 = $this->formatValue($this->item->tax_rate2, $this->invoice->client->currency()->precision);
if($this->settings->inclusive_taxes)
$item_tax_rate2_total = $this->formatValue(($this->line_total - ($this->line_total / (1+$tax_rate2/100))) , $this->invoice->client->currency->precision);
$item_tax_rate2_total = $this->formatValue(($this->line_total - ($this->line_total / (1+$tax_rate2/100))) , $this->invoice->client->currency()->precision);
else
$item_tax_rate2_total = $this->formatValue(($this->line_total * $tax_rate2/100), $this->invoice->client->currency->precision);
$item_tax_rate2_total = $this->formatValue(($this->line_total * $tax_rate2/100), $this->invoice->client->currency()->precision);
$item_tax += $item_tax_rate2_total;

View File

@ -21,7 +21,6 @@
* @OA\Property(property="client_hash", type="string", example="", description="________"),
* @OA\Property(property="industry_id", type="string", example="", description="________"),
* @OA\Property(property="size_id", type="string", example="", description="________"),
* @OA\Property(property="currency_id", type="string", example="", description="________"),
* @OA\Property(property="address1", type="string", example="", description="________"),
* @OA\Property(property="address2", type="string", example="", description="________"),
* @OA\Property(property="city", type="string", example="", description="________"),

View File

@ -5,7 +5,6 @@
* type="object",
* @OA\Property(property="timezone_id", type="string", example="15", description="The timezone id"),
* @OA\Property(property="date_format_id", type="string", example="15", description="____________"),
* @OA\Property(property="datetime_format_id", type="string", example="15", description="____________"),
* @OA\Property(property="financial_year_start", type="string", example="2000-01-01", description="____________"),
* @OA\Property(property="language_id", type="string", example="1", description="____________"),
* @OA\Property(property="company_logo_url", type="string", example="https://example.com/logo.png", description="The URL to the company Logo"),

View File

@ -35,7 +35,7 @@ class UpdateClientRequest extends Request
$rules['company_logo'] = 'mimes:jpeg,jpg,png,gif|max:10000';
$rules['industry_id'] = 'integer|nullable';
$rules['size_id'] = 'integer|nullable';
$rules['currency_id'] = 'integer|nullable';
//$rules['currency_id'] = 'integer|nullable';
$rules['country_id'] = 'integer|nullable';
$rules['shipping_country_id'] = 'integer|nullable';
//$rules['id_number'] = 'unique:clients,id_number,,id,company_id,' . auth()->user()->company()->id;

View File

@ -43,7 +43,7 @@ class CreateInvoicePdf implements ShouldQueue
$invoice = $event->invoice;
$invoice->load('client', 'client.currency');
$invoice->load('client');
$path = 'public/' . $invoice->client->client_hash . '/invoices/';
$file_path = $path . $invoice->invoice_number . '.pdf';

View File

@ -63,7 +63,7 @@ class Client extends BaseModel
'private_notes',
'industry_id',
'size_id',
'currency_id',
// 'currency_id',
'address1',
'address2',
'city',
@ -167,14 +167,10 @@ class Client extends BaseModel
return DateFormat::find($this->getSetting('date_format_id'))->format;
}
public function datetime_format()
{
return DatetimeFormat::find($this->getSetting('datetime_format_id'))->format;
}
public function currency()
{
return $this->belongsTo(Currency::class);
return Currency::find($this->getSetting('currency_id'));
//return $this->belongsTo(Currency::class);
}
/**
@ -226,6 +222,8 @@ class Client extends BaseModel
}
/*Company Settings*/
// \Log::error($setting);
// \Log::error(print_r($this->company->settings,1));
if((property_exists($this->company->settings, $setting) != false ) && (isset($this->company->settings->{$setting}) !== false) ){
return $this->company->settings->{$setting};
}
@ -296,8 +294,8 @@ class Client extends BaseModel
public function getCurrencyCode()
{
if ($this->currency) {
return $this->currency->code;
if ($this->currency()) {
return $this->currency()->code;
}
return 'USD';

View File

@ -215,8 +215,8 @@ class StripePaymentDriver extends BasePaymentDriver
public function processPaymentView(array $data)
{
$payment_intent_data = [
'amount' => $this->convertToStripeAmount($data['amount_with_fee'], $this->client->currency->precision),
'currency' => $this->client->getCurrencyCode(),
'amount' => $this->convertToStripeAmount($data['amount_with_fee'], $this->client->currency()->precision),
'->currency()' => $this->client->getCurrencyCode(),
'customer' => $this->findOrCreateCustomer(),
'description' => $data['invoices']->pluck('id'), //todo more meaningful description here:
];
@ -252,7 +252,7 @@ class StripePaymentDriver extends BasePaymentDriver
+"client_secret": "pi_1FMR7JKmol8YQE9DuC4zMeN3_secret_J3yseWJG6uV0MmsrAT1FlUklV"
+"confirmation_method": "automatic"
+"created": 1569381877
+"currency": "usd"
+"->currency()": "usd"
+"description": "[3]"
+"last_payment_error": null
+"livemode": false
@ -391,7 +391,7 @@ class StripePaymentDriver extends BasePaymentDriver
$client_contact = $this->getContact();
$client_contact_id = $client_contact ? $client_contact->id : null;
$payment->amount = $this->convertFromStripeAmount($data['amount'], $this->client->currency->precision);
$payment->amount = $this->convertFromStripeAmount($data['amount'], $this->client->currency()->precision);
$payment->payment_type_id = $data['payment_type'];
$payment->transaction_reference = $data['payment_method'];
$payment->client_contact_id = $client_contact_id;

View File

@ -59,7 +59,7 @@ class ClientTransformer extends EntityTransformer
'website' => $client->website ?: '',
'private_notes' => $client->private_notes ?: '',
'balance' => (float) $client->balance,
'currency_id' => (string)$client->currency_id ?: '',
// 'currency_id' => (string)$client->currency_id ?: '',
'group_settings_id' => (string)$client->group_settings_id ?: '',
'paid_to_date' => (float) $client->paid_to_date,
'last_login' => (int)$client->last_login,

View File

@ -63,10 +63,10 @@ class Number
public static function formatMoney($value, $client) :string
{
$thousand = $client->currency->thousand_separator;
$decimal = $client->currency->decimal_separator;
$precision = $client->currency->precision;
$code = $client->currency->code;
$thousand = $client->currency()->thousand_separator;
$decimal = $client->currency()->decimal_separator;
$precision = $client->currency()->precision;
$code = $client->currency()->code;
$swapSymbol = $client->country->swap_currency_symbol;
/* Country settings override client settings */
@ -77,7 +77,7 @@ class Number
$decimal = $client->country->decimal_separator;
$value = number_format($value, $precision, $decimal, $thousand);
$symbol = $client->currency->symbol;
$symbol = $client->currency()->symbol;
if ($client->getSetting('show_currency_code') === true) {
return "{$value} {$code}";
@ -86,7 +86,7 @@ class Number
} elseif ($client->getSetting('show_currency_code') === false) {
return "{$symbol}{$value}";
} else {
return self::formatValue($value, $client->currency);
return self::formatValue($value, $client->currency());
}
}
}

View File

@ -154,7 +154,7 @@ trait MakesInvoiceValues
*/
public function makeValues() :array
{
if(!$this->client->currency || !$this->client){
if(!$this->client->currency() || !$this->client){
throw new Exception(debug_backtrace()[1]['function'], 1);
exit;
}

View File

@ -22,7 +22,6 @@ $factory->define(App\Models\Client::class, function (Faker $faker) {
'city' => $faker->city,
'state' => $faker->state,
'postal_code' => $faker->postcode,
'currency_id' => 1,
'country_id' => 4,
'shipping_address1' => $faker->buildingNumber,
'shipping_address2' => $faker->streetAddress,
@ -30,7 +29,7 @@ $factory->define(App\Models\Client::class, function (Faker $faker) {
'shipping_state' => $faker->state,
'shipping_postal_code' => $faker->postcode,
'shipping_country_id' => 4,
'settings' => new ClientSettings(ClientSettings::defaults()),
'settings' => ClientSettings::defaults(),
'client_hash' => \Illuminate\Support\Str::random(40),
];
});

View File

@ -272,7 +272,7 @@ class CreateUsersTable extends Migration
$table->timestamp('last_login')->nullable();
$table->unsignedInteger('industry_id')->nullable();
$table->unsignedInteger('size_id')->nullable();
$table->unsignedInteger('currency_id')->nullable();
// $table->unsignedInteger('currency_id')->nullable();
$table->string('address1')->nullable();
$table->string('address2')->nullable();
@ -304,7 +304,7 @@ class CreateUsersTable extends Migration
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->foreign('industry_id')->references('id')->on('industries');
$table->foreign('size_id')->references('id')->on('sizes');
$table->foreign('currency_id')->references('id')->on('currencies');
// $table->foreign('currency_id')->references('id')->on('currencies');
});

View File

@ -9,9 +9,9 @@
"/vendors/js/perfect-scrollbar.min.js": "/vendors/js/perfect-scrollbar.min.js?id=4a10bcfa0a9c9fa9d503",
"/vendors/js/jSignature.min.js": "/vendors/js/jSignature.min.js?id=4dc38fc88461b30ab711",
"/vendors/js/flashcanvas.min.js": "/vendors/js/flashcanvas.min.js?id=50f6e0a09e8a939c1da9",
"/vendors/js/flashcanvas.swf": "/vendors/js/flashcanvas.swf?id=ed2a06bb83b57f2639b1",
"/vendors/css/select2.min.css": "/vendors/css/select2.min.css?id=8e44c39add2364bdb469",
"/vendors/js/select2.min.js": "/vendors/js/select2.min.js?id=0a96cf2d3a193019a91b",
"/vendors/js/flashcanvas.swf": "/vendors/js/flashcanvas.swf?id=d1a52ac12da100808048",
"/vendors/css/select2.min.css": "/vendors/css/select2.min.css?id=9f54e6414f87e0d14b9e",
"/vendors/js/select2.min.js": "/vendors/js/select2.min.js?id=708724c21c5073e9e228",
"/vendors/css/select2-bootstrap4.css": "/vendors/css/select2-bootstrap4.css?id=85167d868d2bf2dc5603",
"/vendors/css/dropzone.min.css": "/vendors/css/dropzone.min.css?id=2f735dbf472afcd77604",
"/vendors/css/dropzone-basic.min.css": "/vendors/css/dropzone-basic.min.css?id=960bcd6d5cb8351ac0d0",

View File

@ -14,6 +14,7 @@ use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Session;
use Tests\MockAccountData;
use Tests\TestCase;
/**
@ -26,6 +27,7 @@ class InvoiceTest extends TestCase
use MakesHash;
use DatabaseTransactions;
use MockAccountData;
public function setUp() :void
{
@ -38,6 +40,7 @@ class InvoiceTest extends TestCase
Model::reguard();
$this->makeTestData();
}
@ -107,96 +110,40 @@ class InvoiceTest extends TestCase
public function testInvoiceRESTEndPoints()
{
$data = [
'first_name' => $this->faker->firstName,
'last_name' => $this->faker->lastName,
'name' => $this->faker->company,
'email' => $this->faker->unique()->safeEmail,
'password' => 'ALongAndBrilliantPassword123',
'_token' => csrf_token(),
'privacy_policy' => 1,
'terms_of_service' => 1
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
])->post('/api/v1/signup', $data);
$acc = $response->json();
$account = Account::find($this->decodePrimaryKey($acc['data'][0]['account']['id']));
$company_token = $account->default_company->tokens()->first();
$token = $company_token->token;
$company = $company_token->company;
$user = $company_token->user;
$this->assertNotNull($company_token);
$this->assertNotNull($token);
$this->assertNotNull($user);
$this->assertNotNull($company);
//$this->assertNotNull($user->token->company);
factory(\App\Models\Client::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id, 'currency_id' => 1])->each(function ($c) use ($user, $company){
factory(\App\Models\ClientContact::class,1)->create([
'user_id' => $user->id,
'client_id' => $c->id,
'company_id' => $company->id,
'is_primary' => 1
]);
factory(\App\Models\ClientContact::class,1)->create([
'user_id' => $user->id,
'client_id' => $c->id,
'company_id' => $company->id
]);
});
$client = Client::all()->first();
factory(\App\Models\Invoice::class, 1)->create(['user_id' => $user->id, 'company_id' => $company->id, 'client_id' => $client->id]);
$invoice = Invoice::where('user_id',$user->id)->first();
$invoice->settings = $client->getMergedSettings();
$invoice->save();
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->get('/api/v1/invoices/'.$this->encodePrimaryKey($invoice->id));
'X-API-TOKEN' => $this->token,
])->get('/api/v1/invoices/'.$this->encodePrimaryKey($this->invoice->id));
$response->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->get('/api/v1/invoices/'.$this->encodePrimaryKey($invoice->id).'/edit');
'X-API-TOKEN' => $this->token,
])->get('/api/v1/invoices/'.$this->encodePrimaryKey($this->invoice->id).'/edit');
$response->assertStatus(200);
$invoice_update = [
'status_id' => Invoice::STATUS_PAID
'tax_name1' => 'dippy',
];
$this->assertNotNull($invoice);
$this->assertNotNull($invoice->settings);
$this->assertNotNull($this->invoice);
$this->assertNotNull($this->invoice->settings);
$this->assertTrue(property_exists($invoice->settings, 'custom_invoice_taxes1'));
$this->assertTrue(property_exists($this->invoice->settings, 'custom_invoice_taxes1'));
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->put('/api/v1/invoices/'.$this->encodePrimaryKey($invoice->id), $invoice_update)
'X-API-TOKEN' => $this->token,
])->put('/api/v1/invoices/'.$this->encodePrimaryKey($this->invoice->id), $invoice_update)
->assertStatus(200);
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $token,
])->delete('/api/v1/invoices/'.$this->encodePrimaryKey($invoice->id));
'X-API-TOKEN' => $this->token,
])->delete('/api/v1/invoices/'.$this->encodePrimaryKey($this->invoice->id));
$response->assertStatus(200);