create project

This commit is contained in:
2023-08-11 20:13:22 +02:00
commit 1390855517
38 changed files with 6008 additions and 0 deletions

12
src/app.d.ts vendored Normal file
View 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
View 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
View 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);
});
});

View 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}
</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>

View 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);
});
});

View 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;
}

View 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);
}

View 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;
}

View File

@@ -0,0 +1 @@
export const vatRates = [];

View File

@@ -0,0 +1,6 @@
export default interface CalculatorValues {
basePrice: number;
vatRate: number;
vatAmount: number;
finalPrice: number;
}

View File

@@ -0,0 +1,4 @@
export default interface VatRate {
title: string;
rate: number;
}

1
src/lib/index.ts Normal file
View File

@@ -0,0 +1 @@
// place files you want to import through the `$lib` alias in this folder.

11
src/routes/+layout.svelte Normal file
View 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
View 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 />