create project
This commit is contained in:
12
src/app.d.ts
vendored
Normal file
12
src/app.d.ts
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
// See https://kit.svelte.dev/docs/types#app
|
||||
// for information about these interfaces
|
||||
declare global {
|
||||
namespace App {
|
||||
// interface Error {}
|
||||
// interface Locals {}
|
||||
// interface PageData {}
|
||||
// interface Platform {}
|
||||
}
|
||||
}
|
||||
|
||||
export {};
|
||||
12
src/app.html
Normal file
12
src/app.html
Normal file
@@ -0,0 +1,12 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
|
||||
<meta name="viewport" content="width=device-width" />
|
||||
%sveltekit.head%
|
||||
</head>
|
||||
<body data-sveltekit-preload-data="hover">
|
||||
<main>%sveltekit.body%</main>
|
||||
</body>
|
||||
</html>
|
||||
7
src/index.test.ts
Normal file
7
src/index.test.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
|
||||
describe('sum test', () => {
|
||||
it('adds 1 + 2 to equal 3', () => {
|
||||
expect(1 + 2).toBe(3);
|
||||
});
|
||||
});
|
||||
110
src/lib/calculator/Calculator.svelte
Normal file
110
src/lib/calculator/Calculator.svelte
Normal file
@@ -0,0 +1,110 @@
|
||||
<script lang="ts">
|
||||
import type CalculatorValues from '$lib/calculator/type/CalculatorValues';
|
||||
import {
|
||||
refreshBasePrice,
|
||||
refreshFinalPrice,
|
||||
refreshVatAmount
|
||||
} from '$lib/calculator/functions/RefreshFunctions';
|
||||
|
||||
let calcValues: CalculatorValues = {
|
||||
basePrice: 0.0,
|
||||
vatRate: 0.0,
|
||||
vatAmount: 0.0,
|
||||
finalPrice: 0.0
|
||||
};
|
||||
|
||||
let calcValuesCache: CalculatorValues = {
|
||||
...calcValues
|
||||
};
|
||||
|
||||
function refreshCache(calcValues: CalculatorValues) {
|
||||
calcValuesCache = {
|
||||
...calcValues
|
||||
};
|
||||
}
|
||||
|
||||
function refreshPrices(calcValues: CalculatorValues) {
|
||||
console.log(calcValues);
|
||||
if (
|
||||
calcValues.basePrice !== calcValuesCache.basePrice ||
|
||||
calcValues.vatRate !== calcValuesCache.vatRate
|
||||
) {
|
||||
refreshFinalPrice(calcValues);
|
||||
} else if (calcValues.finalPrice !== calcValuesCache.finalPrice) {
|
||||
refreshBasePrice(calcValues);
|
||||
}
|
||||
|
||||
refreshVatAmount(calcValues);
|
||||
refreshCache(calcValues);
|
||||
}
|
||||
|
||||
$: refreshPrices(calcValues);
|
||||
|
||||
const invalidNumberErrorText = 'Zadejte platné číslo.';
|
||||
</script>
|
||||
|
||||
<form>
|
||||
<label for="vatRate">Sazba DPH (%)</label>
|
||||
<input
|
||||
id="vatRate"
|
||||
type="number"
|
||||
min="0"
|
||||
max="100"
|
||||
bind:value={calcValues.vatRate}
|
||||
class:invalid={calcValues.vatRate === null}
|
||||
/>
|
||||
{#if calcValues.vatRate === null}
|
||||
<p class="invalid">{invalidNumberErrorText}</p>
|
||||
{/if}
|
||||
<label for="basePrice">Cena bez DPH (Kč)</label>
|
||||
<input
|
||||
id="basePrice"
|
||||
type="number"
|
||||
min="0"
|
||||
bind:value={calcValues.basePrice}
|
||||
class:invalid={calcValues.basePrice === null}
|
||||
/>
|
||||
{#if calcValues.basePrice === null}
|
||||
<p class="invalid">{invalidNumberErrorText}</p>
|
||||
{/if}
|
||||
<label for="finalPrice">Cena s DPH (Kč)</label>
|
||||
<input
|
||||
id="finalPrice"
|
||||
type="number"
|
||||
min="0 "
|
||||
bind:value={calcValues.finalPrice}
|
||||
class:invalid={calcValues.finalPrice === null}
|
||||
/>
|
||||
{#if calcValues.finalPrice === null}
|
||||
<p class="invalid">{invalidNumberErrorText}</p>
|
||||
{/if}
|
||||
</form>
|
||||
|
||||
<div class="vatAmount">
|
||||
Výše DPH: {calcValues.vatAmount} Kč
|
||||
</div>
|
||||
|
||||
<style>
|
||||
p.invalid {
|
||||
color: var(--invalid-color);
|
||||
}
|
||||
|
||||
input {
|
||||
width: 100%;
|
||||
border: 2px solid gray;
|
||||
border-radius: 3px;
|
||||
}
|
||||
|
||||
input.invalid {
|
||||
border: 4px solid var(--invalid-color);
|
||||
}
|
||||
|
||||
input[type='number'] {
|
||||
-moz-appearance: textfield;
|
||||
appearance: textfield;
|
||||
}
|
||||
|
||||
input[type='number']::-webkit-inner-spin-button {
|
||||
-webkit-appearance: none;
|
||||
}
|
||||
</style>
|
||||
80
src/lib/calculator/Calculator.test.ts
Normal file
80
src/lib/calculator/Calculator.test.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import Calculator from '$lib/calculator/Calculator.svelte';
|
||||
import { describe, it } from 'vitest';
|
||||
import '@testing-library/jest-dom';
|
||||
import { fireEvent, render, screen } from '@testing-library/svelte';
|
||||
|
||||
function initFields() {
|
||||
render(Calculator);
|
||||
|
||||
return {
|
||||
vatRate: screen.getByLabelText('Sazba DPH (%)', { selector: 'input' }),
|
||||
basePrice: screen.getByLabelText('Cena bez DPH', { selector: 'input' }),
|
||||
finalPrice: screen.getByLabelText('Cena s DPH', { selector: 'input' })
|
||||
};
|
||||
}
|
||||
|
||||
describe('Test Calculator.svelte', async () => {
|
||||
it('Initial prices should be zero', async () => {
|
||||
const { vatRate, basePrice, finalPrice } = initFields();
|
||||
|
||||
expect(vatRate).toHaveValue(0);
|
||||
expect(basePrice).toHaveValue(0);
|
||||
expect(finalPrice).toHaveValue(0);
|
||||
});
|
||||
|
||||
it('Calculation #1 should be correct (225*1.21=272.25)', async () => {
|
||||
const { vatRate, basePrice, finalPrice } = initFields();
|
||||
|
||||
expect(vatRate).toHaveValue(0);
|
||||
expect(basePrice).toHaveValue(0);
|
||||
expect(finalPrice).toHaveValue(0);
|
||||
|
||||
await fireEvent.input(vatRate, {
|
||||
target: {
|
||||
value: 21
|
||||
}
|
||||
});
|
||||
|
||||
expect(vatRate).toHaveValue(21);
|
||||
expect(basePrice).toHaveValue(0);
|
||||
expect(finalPrice).toHaveValue(0);
|
||||
|
||||
await fireEvent.input(basePrice, {
|
||||
target: {
|
||||
value: 225
|
||||
}
|
||||
});
|
||||
|
||||
expect(vatRate).toHaveValue(21);
|
||||
expect(basePrice).toHaveValue(225);
|
||||
expect(finalPrice).toHaveValue(272.25);
|
||||
});
|
||||
|
||||
it('Calculation #2 should be correct (272.25/1.21=225)', async () => {
|
||||
const { vatRate, basePrice, finalPrice } = initFields();
|
||||
|
||||
expect(vatRate).toHaveValue(0);
|
||||
expect(basePrice).toHaveValue(0);
|
||||
expect(finalPrice).toHaveValue(0);
|
||||
|
||||
await fireEvent.input(vatRate, {
|
||||
target: {
|
||||
value: 21
|
||||
}
|
||||
});
|
||||
|
||||
expect(vatRate).toHaveValue(21);
|
||||
expect(basePrice).toHaveValue(0);
|
||||
expect(finalPrice).toHaveValue(0);
|
||||
|
||||
await fireEvent.input(finalPrice, {
|
||||
target: {
|
||||
value: 272.25
|
||||
}
|
||||
});
|
||||
|
||||
expect(vatRate).toHaveValue(21);
|
||||
expect(basePrice).toHaveValue(225);
|
||||
expect(finalPrice).toHaveValue(272.25);
|
||||
});
|
||||
});
|
||||
7
src/lib/calculator/functions/CentsConvertFunctions.ts
Normal file
7
src/lib/calculator/functions/CentsConvertFunctions.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export function toCents(price: number): number {
|
||||
return Math.round(price * 100);
|
||||
}
|
||||
|
||||
export function fromCents(priceCents: number): number {
|
||||
return priceCents / 100;
|
||||
}
|
||||
18
src/lib/calculator/functions/RefreshFunctions.ts
Normal file
18
src/lib/calculator/functions/RefreshFunctions.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import type CalculatorValues from '$lib/calculator/type/CalculatorValues';
|
||||
import { getVatAmount, getVatCoefficient } from '$lib/calculator/functions/VatFunctions';
|
||||
import { fromCents, toCents } from '$lib/calculator/functions/CentsConvertFunctions';
|
||||
|
||||
export function refreshFinalPrice(calcValues: CalculatorValues) {
|
||||
const finalPriceCents = toCents(calcValues.basePrice * getVatCoefficient(calcValues.vatRate));
|
||||
calcValues.finalPrice = fromCents(finalPriceCents);
|
||||
}
|
||||
|
||||
export function refreshBasePrice(calcValues: CalculatorValues) {
|
||||
const basePriceCents = toCents(calcValues.finalPrice / getVatCoefficient(calcValues.vatRate));
|
||||
calcValues.basePrice = fromCents(basePriceCents);
|
||||
}
|
||||
|
||||
export function refreshVatAmount(calcValues: CalculatorValues) {
|
||||
const vatAmountCents = toCents(getVatAmount(calcValues.basePrice, calcValues.finalPrice));
|
||||
calcValues.vatAmount = fromCents(vatAmountCents);
|
||||
}
|
||||
7
src/lib/calculator/functions/VatFunctions.ts
Normal file
7
src/lib/calculator/functions/VatFunctions.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export function getVatCoefficient(vatRate: number): number {
|
||||
return 1 + vatRate / 100;
|
||||
}
|
||||
|
||||
export function getVatAmount(basePrice: number, finalPrice: number): number {
|
||||
return finalPrice - basePrice;
|
||||
}
|
||||
1
src/lib/calculator/rates/VatRates.ts
Normal file
1
src/lib/calculator/rates/VatRates.ts
Normal file
@@ -0,0 +1 @@
|
||||
export const vatRates = [];
|
||||
6
src/lib/calculator/type/CalculatorValues.ts
Normal file
6
src/lib/calculator/type/CalculatorValues.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export default interface CalculatorValues {
|
||||
basePrice: number;
|
||||
vatRate: number;
|
||||
vatAmount: number;
|
||||
finalPrice: number;
|
||||
}
|
||||
4
src/lib/calculator/type/VatRate.ts
Normal file
4
src/lib/calculator/type/VatRate.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export default interface VatRate {
|
||||
title: string;
|
||||
rate: number;
|
||||
}
|
||||
1
src/lib/index.ts
Normal file
1
src/lib/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
// place files you want to import through the `$lib` alias in this folder.
|
||||
11
src/routes/+layout.svelte
Normal file
11
src/routes/+layout.svelte
Normal file
@@ -0,0 +1,11 @@
|
||||
<script>
|
||||
import 'ress/dist/ress.min.css';
|
||||
</script>
|
||||
|
||||
<slot />
|
||||
|
||||
<style>
|
||||
:root {
|
||||
--invalid-color: red;
|
||||
}
|
||||
</style>
|
||||
9
src/routes/+page.svelte
Normal file
9
src/routes/+page.svelte
Normal file
@@ -0,0 +1,9 @@
|
||||
<script lang="ts">
|
||||
import Calculator from '$lib/calculator/Calculator.svelte';
|
||||
</script>
|
||||
|
||||
<h1>DPHčko.cz</h1>
|
||||
|
||||
<p>Kalkulačka DPH – spočítejte si daň z přidané hodnoty</p>
|
||||
|
||||
<Calculator />
|
||||
Reference in New Issue
Block a user