From b218b7c7e38092ade0c381a2139e55a2cf8a7a0c Mon Sep 17 00:00:00 2001 From: relaxed <> Date: Wed, 25 Dec 2024 12:47:39 +0500 Subject: [PATCH] pizzas: 1 --- src/lib/components/ui/ScreenWrap.svelte | 4 +- src/routes/+page.svelte | 976 +++++++----------------- 2 files changed, 286 insertions(+), 694 deletions(-) diff --git a/src/lib/components/ui/ScreenWrap.svelte b/src/lib/components/ui/ScreenWrap.svelte index b2324b4..60db8d5 100644 --- a/src/lib/components/ui/ScreenWrap.svelte +++ b/src/lib/components/ui/ScreenWrap.svelte @@ -104,7 +104,7 @@ WHERE id = ${current_item.id}`; export let db; /** @type {string} */ - let current_page = "Запросы"; + let current_page = "Пиццерия"; let current_table = ""; let current_view = ""; @@ -113,7 +113,7 @@ WHERE id = ${current_item.id}`; export let is_item_dialog_open = false; export let is_dialog_item_add = false; - let is_logedin = false; + let is_logedin = true; export let data_access; diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index c78260e..b49cfb8 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -11,8 +11,8 @@ import TextInput from "$lib/components/form/TextInput.svelte"; import NumberInput from "$lib/components/form/NumberInput.svelte"; import ItemList from "$lib/components/form/ItemList.svelte"; - // import DateInput from "$lib/components/form/DateInput.svelte"; - import PhoneInput from "$lib/components/form/PhoneInput.svelte"; + import DateInput from "$lib/components/form/DateInput.svelte"; + // import PhoneInput from "$lib/components/form/PhoneInput.svelte"; import Select from "$lib/components/form/Select.svelte"; import Table from "$lib/components/table/Table.svelte"; @@ -23,7 +23,7 @@ import TableCudButtons from "$lib/components/shorts/TableCUDButtons.svelte"; import TableButtonsWrap from "$lib/components/shorts/TableButtonsWrap.svelte"; import ViewTable from "$lib/components/shorts/ViewTable.svelte"; - import TableListShort from "$lib/components/shorts/TableListShort.svelte"; + // import TableListShort from "$lib/components/shorts/TableListShort.svelte"; /** @type {Database} */ let db; @@ -35,68 +35,88 @@ // db = await Database.load(`sqlite:${await path.resolve()}/db/data.db`); await db.execute(` -CREATE TABLE IF NOT EXISTS EmployeeStatus (id INTEGER PRIMARY KEY, name VARCHAR(256)); +DROP TABLE IF EXISTS IdBuffer; +CREATE TABLE IF NOT EXISTS IdBuffer (id INTEGER); -CREATE TABLE IF NOT EXISTS Employees ( -id INTEGER PRIMARY KEY, -employee_status_id INTEGER REFERENCES EmployeeStatus(id) ON DELETE CASCADE, -fname VARCHAR(100), -sname VARCHAR(100), -tname VARCHAR(100) +CREATE TABLE IF NOT EXISTS Ingredients ( + id INTEGER PRIMARY KEY, + name VARCHAR(256), + price INTEGER, + is_allowed BOOLEAN DEFAULT true, + weight FLOAT ); -CREATE TABLE IF NOT EXISTS AccountAccessLevel (id INTEGER PRIMARY KEY, name VARCHAR(256)); - -CREATE TABLE IF NOT EXISTS Accounts ( -id INTEGER PRIMARY KEY, -employee_id INTEGER REFERENCES Employees(id), -account_access_level_id INTEGER REFERENCES AccountAccessLevel(id), -login VARCHAR(256), -password VARCHAR(256) -); - -CREATE TABLE IF NOT EXISTS Clients ( -id INTEGER PRIMARY KEY, -phone VARCHAR(36), -fname VARCHAR(100), -sname VARCHAR(100), -tname VARCHAR(100) -); - -CREATE TABLE IF NOT EXISTS RequestStatus (id INTEGER PRIMARY KEY, name VARCHAR(256)); - -CREATE TABLE IF NOT EXISTS RequestType (id INTEGER PRIMARY KEY, name VARCHAR(256)); - -CREATE TABLE IF NOT EXISTS Requests ( -id INTEGER PRIMARY KEY, -client_id INTEGER REFERENCES Clients(id) ON DELETE CASCADE, -employee_id INTEGER REFERENCES Employees(id) ON DELETE CASCADE, -request_status_id INTEGER REFERENCES RequestStatus(id) ON DELETE CASCADE DEFAULT 1, -request_type_id INTEGER REFERENCES RequestType(id) ON DELETE CASCADE, -initial_date DATE DEFAULT CURRENT_TIMESTAMP, -- not editable by user -description TEXT, -duration FLOAT NULL -- hours -); - -CREATE TABLE IF NOT EXISTS Ingredients (id INTEGER PRIMARY KEY, name VARCHAR(256), price INTEGER); - CREATE TABLE IF NOT EXISTS Pizzas ( - id INTEGER PRIMARY KEY, - name VARCHAR(256), - price INTEGER + id INTEGER PRIMARY KEY, + name VARCHAR(256), + price INTEGER, + is_allowed BOOLEAN ); CREATE TABLE IF NOT EXISTS PizzasIngredientsJoin ( - id INTEGER PRIMARY KEY, - pizza_id INTEGER REFERENCES Pizzas(id) ON DELETE CASCADE, - ingredient_id INTEGER REFERENCES Ingredients(id) ON DELETE CASCADE + id INTEGER PRIMARY KEY, + pizza_id INTEGER REFERENCES Pizzas(id) ON DELETE CASCADE, + ingredient_id INTEGER REFERENCES Ingredients(id) ON DELETE CASCADE ); -DROP TABLE IF EXISTS IdBuffer; -CREATE TABLE IF NOT EXISTS IdBuffer (id INTEGER); +CREATE TABLE IF NOT EXISTS Sales ( + id INTEGER PRIMARY KEY, + date DATE DEFAULT CURRENT_TIMESTAMP, + pizza_id INTEGER REFERENCES Pizzas(id) ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS IngredientsWarehouse ( + id INTEGER PRIMARY KEY, + ingredient_id INTEGER REFERENCES Ingredients(id) ON DELETE CASCADE, + date DATE DEFAULT CURRENT_TIMESTAMP, + count INTEGER +); + + +DROP VIEW IF EXISTS IngredientsWarehouseCount; +CREATE VIEW IF NOT EXISTS IngredientsWarehouseCount (id, name, count) AS +SELECT Ingredients.id AS id, Ingredients.name AS name, sum(IngredientsWarehouse.count) AS count +FROM IngredientsWarehouse +JOIN Ingredients on Ingredients.id = IngredientsWarehouse.ingredient_id +GROUP BY Ingredients.id; + + +DROP VIEW IF EXISTS SalesIngredientsCount; +CREATE VIEW IF NOT EXISTS SalesIngredientsCount (id, name, count) AS +SELECT Ingredients.id AS id, Ingredients.name AS name, sum(Ingredients.weight) as count +FROM Sales +JOIN Pizzas on Pizzas.id = Sales.pizza_id +JOIN PizzasIngredientsJoin on PizzasIngredientsJoin.pizza_id = Sales.pizza_id +JOIN Ingredients on Ingredients.id = PizzasIngredientsJoin.ingredient_id +GROUP BY Ingredients.id; + + +DROP VIEW IF EXISTS IngredientsLeft; +CREATE VIEW IF NOT EXISTS IngredientsLeft (id, name, count) AS +WITH ingredients AS ( +SELECT * FROM IngredientsWarehouseCount +UNION ALL +SELECT id, name, -count FROM SalesIngredientsCount +) +SELECT id, name, sum(count) as count FROM ingredients +GROUP BY id; `); } + /** + select IngredientsWarehouseCount.id, IngredientsWarehouseCount.name, IngredientsWarehouseCount.count from IngredientsWarehouseCount + inner join SalesIngredientsCount on SalesIngredientsCount.id = IngredientsWarehouseCount.id; +**/ + /** + with ingredients as ( + select * from IngredientsWarehouseCount + union all + select id, name, -count from SalesIngredientsCount + ) + select id, name, sum(count) from ingredients + group by id; +**/ + // --SELECT * FROM SalesIngredientsCount; /** @typedef Page * @property {Object} tables @@ -105,64 +125,39 @@ CREATE TABLE IF NOT EXISTS IdBuffer (id INTEGER); const db_scheme = { /** @type Page */ - Запросы: { + Пиццерия: { /** @type {any} */ tables: { Ingredients: "Ингридиенты", Pizzas: "Пиццы", - PizzasIngredientsJoin: "test", - Requests: "Запросы", - Clients: "Клиенты", + Sales: "Продажи", + IngredientsWarehouse: "Склад", + // PizzasIngredientsJoin: "test", }, /** @type {any} */ viewes: { - RequestCount: "Количество заявок", - AvgRequestTime: "Среднее время заявок", - StatByType: "Статистика по типам", + IngredientsLeft: "Остатки Ингридиенты", }, }, - /** @type Page */ - Сотрудники: { - /** @type {any} */ - tables: { - Employees: "Сотрудники", - Accounts: "Аккаунты", - AccountAccessLevel: "Доступ", - }, - /** @type {any} */ - viewes: {}, - }, - - /** @type Page */ - Списки: { - /** @type {any} */ - tables: { - RequestStatus: "Статус запроса", - RequestType: "Тип запроса", - EmployeeStatus: "Должность", - }, - /** @type {any} */ - viewes: {}, - }, }; const data_access = { admin: true, - dicrector: { - Запросы: [ - "Requests", - "Clients", - "RequestCount", - "AvgRequestTime", - "StatByType", - ], - Сотрудники: ["Employees", "Accounts"], - Списки: ["RequestStatus", "RequestType", "EmployeeStatus"], - }, - - worker: { - Запросы: ["Requests", "Clients"], - }, + // dicrector: { + // Запросы: [ + // "Requests", + // "Clients", + // "RequestCount", + // "AvgRequestTime", + // "StatByType", + // ], + // Сотрудники: ["Employees", "Accounts"], + // Списки: ["RequestStatus", "RequestType", "EmployeeStatus"], + // }, + // + // worker: { + // Запросы: ["Requests", "Clients"], + // }, }; /** @type {any} */ @@ -177,6 +172,7 @@ CREATE TABLE IF NOT EXISTS IdBuffer (id INTEGER); * @param {string | null} name */ function check_access(access_level, tab_name, name) { + return true; // @ts-ignore if (data_access[access_level] === true) return true; @@ -193,6 +189,7 @@ CREATE TABLE IF NOT EXISTS IdBuffer (id INTEGER); @param {string} passowrd */ async function check_login(login, passowrd) { + return [true, "admin"]; const access_type = await db.select(`SELECT AccountAccessLevel.name FROM Accounts JOIN AccountAccessLevel ON AccountAccessLevel.id = Accounts.account_access_level_id @@ -234,17 +231,7 @@ insert into IdBuffer (id) values (${ar.join("), (")});`); let:sql_insert_short let:sql_delete_short > - {#if !is_view_open && current_page === "Запросы"} - - - - - - - - - - + {#if !is_view_open && current_page === "Пиццерия"} {#if current_table === "Pizzas"} {@const cur_cols = ["name", "price"]} { await id_buffer(current_item.ingredients_ids); const query = ` @@ -349,78 +338,78 @@ ${current_item.ingredients_ids.map((el) => "(" + current_item.id + "," + el + ") - {:else if current_table === "PizzasIngredientsJoin"} - {@const cur_cols = ["pizza_id", "ingredient_id"]} - - {#if !is_dialog_item_add} -

id {current_item.id}

- {/if} - - - - - - - { - await sql_insert_short(current_table, cur_cols); - }} - on_save={async () => { - await sql_update_short(current_table, cur_cols); - }} - on_delete={sql_delete_short} - > -
- { - current_item = { - id: 0, - pizza_id: null, - ingredient_id: null, - }; - }} - columns={cur_cols} - let:is_searching - let:search_query_result - > - - open_item_edit(item)}> - - -
-
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {:else if current_table === "Ingredients"} - {@const cur_cols = ["name", "price"]} + {@const cur_cols = ["name", "price", "is_allowed", "weight"]} "(" + current_item.id + "," + el + ") {#if !is_dialog_item_add}

id {current_item.id}

{/if} + + + + + + + "(" + current_item.id + "," + el + ") id: 0, name: "", price: 0, + is_allowed: true, + weight: 0, }; }} columns={cur_cols} @@ -475,7 +474,8 @@ ${current_item.ingredients_ids.map((el) => "(" + current_item.id + "," + el + ") - {:else if current_table === "Clients"} + {:else if current_table === "Sales"} + {@const cur_cols = ["date", "pizza_id"]} "(" + current_item.id + "," + el + ") {#if !is_dialog_item_add}

id {current_item.id}

{/if} - - + + + - - + + + - - + + { + await sql_insert_short(current_table, ["pizza_id"]); + }} + on_save={async () => { + await sql_update_short(current_table, cur_cols); + }} + on_delete={sql_delete_short} + > +
+ { + current_item = { + id: 0, + name: "", + date: "", + }; + }} + columns={cur_cols} + let:is_searching + let:search_query_result + > + + open_item_edit(item)}> + + + +
+ {#await db.select(`select name from Pizzas where id = ${item.pizza_id}`) then data} + {data[0].name} + {/await} +
+
+ {:else if current_table === "IngredientsWarehouse"} + {@const cur_cols = ["date", "ingredient_id", "count"]} + + {#if !is_dialog_item_add} +

id {current_item.id}

+ {/if} + + + - - + + + - {@const cur_cols = ["phone", "fname", "sname", "tname"]} + + + + + "(" + current_item.id + "," + el + ") on_add_click={() => { current_item = { id: 0, - phone: "+7 (", - fname: "", - sname: "", - tname: "", + date: "", + ingredient_id: 0, + count: 0, }; }} - columns={["phone", "fname", "sname", "tname"]} + columns={cur_cols} let:is_searching let:search_query_result > - open_item_edit(item)}> - - - -
{item.sname} {item.fname[0]}.{item.tname[0]}
- - {:else if current_table === "Requests"} - - {#if !is_dialog_item_add} -

id {current_item.id}

- {/if} - - - - - - - {#if !is_dialog_item_add} - - - - {/if} - - - - {#if !is_dialog_item_add} -

Дата начала {current_item.initial_date}

- {/if} - - - - {#if !is_dialog_item_add} - - - - {/if} - {@const cur_cols = [ - "client_id", - "employee_id", - "request_status_id", - "request_type_id", - "initial_date", - "description", - "duration", - ]} - - { - await sql_insert_short(current_table, [ - "client_id", - "employee_id", - "request_status_id", - "request_type_id", - "description", - "duration", - ]); - }} - on_save={async () => { - await sql_update_short(current_table, cur_cols); - }} - on_delete={sql_delete_short} - > -
- { - current_item = { - id: 0, - client_id: 0, - employee_id: 1, - request_status_id: 1, - request_type_id: 1, - description: "", - duration: null, - }; - }} - columns={[ - "client_id", - "employee_id", - "request_status_id", - "request_type_id", - "initial_date", - "description", - "duration", - ]} - ex_params={[ - { - name: "Clients.fname", - is_number: false, - }, - { - name: "Clients.sname", - is_number: false, - }, - { - name: "Clients.tname", - is_number: false, - }, - { - name: "Employees.fname", - is_number: false, - }, - { - name: "Employees.sname", - is_number: false, - }, - { - name: "Employees.tname", - is_number: false, - }, - { - name: "RequestStatus.name", - is_number: false, - }, - { - name: "RequestType.name", - is_number: false, - }, - ]} - joins={` -JOIN Clients on Clients.id = Requests.client_id -JOIN Employees on Employees.id = Requests.employee_id -JOIN RequestStatus on RequestStatus.id = Requests.request_status_id -JOIN RequestType on RequestType.id = Requests.request_type_id -`} - let:is_searching - let:search_query_result - > - open_item_edit(item)}> + - - - - - +
- {item.id} - - {#await db.select(`select fname, sname, tname from Clients where id = ${item.client_id}`) then data} - {data[0].fname} {data[0].sname[0]}. {data[0].tname[0]}. - {/await} - - {#await db.select(`select id, fname, sname, tname from Employees where id = ${item.employee_id}`) then data} - {data[0].fname} {data[0].sname[0]}. {data[0].tname[0]}. - {/await} - - {#await db.select(`select id, name from RequestStatus where id = ${item.request_status_id}`) then data} + {#await db.select(`select name from Ingredients where id = ${item.ingredient_id}`) then data} {data[0].name} {/await} - {#await db.select(`select id, name from RequestType where id = ${item.request_type_id}`) then data} - {data[0].name} - {/await} -
{/if} {:else if is_view_open} - {#if current_view === "RequestCount"} + {#if current_view === "IngredientsLeft"} - {:else if current_view === "AvgRequestTime"} - - {:else if current_view === "StatByType"} - - {/if} - {/if} - - {#if current_page === "Сотрудники"} - {#if current_table === "Employees"} - - {#if !is_dialog_item_add} -

id {current_item.id}

- {/if} - - - - - - - - - - - - - - - - - - {@const cur_cols = ["employee_status_id", "fname", "sname", "tname"]} - - { - await sql_insert_short(current_table, cur_cols); - }} - on_save={async () => { - await sql_update_short(current_table, cur_cols); - }} - on_delete={sql_delete_short} - > -
- - { - current_item = { - id: 0, - employee_status_id: 1, - fname: "", - sname: "", - tname: "", - }; - }} - columns={["employee_status_id", "fname", "sname", "tname"]} - joins={`JOIN EmployeeStatus on EmployeeStatus.id = Employees.employee_status_id`} - ex_params={[ - { - name: "EmployeeStatus.name", - is_number: false, - }, - ]} - let:is_searching - let:search_query_result - > - - open_item_edit(item)}> - - - - - -
- {#await db.select(`select name from EmployeeStatus where id = ${item.employee_status_id}`) then data} - {data[0].name} - {/await} - {item.sname} {item.fname[0]}.{item.tname[0]}
-
- {:else if current_table === "Accounts"} - - {#if !is_dialog_item_add} -

id {current_item.id}

- {/if} - - - - - - - - - - - - - - - - - - {@const cur_cols = [ - "employee_id", - "account_access_level_id", - "login", - "password", - ]} - - { - await sql_insert_short(current_table, cur_cols); - }} - on_save={async () => { - await sql_update_short(current_table, cur_cols); - }} - on_delete={sql_delete_short} - > -
- - { - current_item = { - id: 0, - employee_id: 0, - account_access_level_id: 0, - login: "", - password: "", - }; - }} - columns={[ - "employee_id", - "account_access_level_id", - "login", - "password", - ]} - joins={"JOIN Employees on Accounts.employee_id = Employees.id"} - ex_params={[ - { - name: "Employees.fname", - is_number: false, - }, - { - name: "Employees.sname", - is_number: false, - }, - { - name: "Employees.tname", - is_number: false, - }, - ]} - let:is_searching - let:search_query_result - > - - open_item_edit(item)}> - - - - - -
- {#await db.select(`select id, fname, sname, tname from Employees where id = ${item.employee_id}`) then data} - {data[0].fname} {data[0].sname[0]}. {data[0].tname[0]}. - {/await} - - {#await db.select(`select name from AccountAccessLevel where id = ${item.account_access_level_id}`) then data} - {data[0].name} - {/await} -
-
- {:else if current_table === "AccountAccessLevel"} - - {/if} - {/if} - - {#if !is_view_open && current_page === "Списки"} - {#if ["RequestStatus", "RequestType", "EmployeeStatus"].includes(current_table)} - {/if} {/if}