Skip to content

Commit

Permalink
2024-09-14T22-09-22
Browse files Browse the repository at this point in the history
  • Loading branch information
sspilleman committed Sep 14, 2024
1 parent 30b213e commit 7156104
Show file tree
Hide file tree
Showing 8 changed files with 964 additions and 3 deletions.
863 changes: 863 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"eslint": "^8.53.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-svelte": "^2.35.0",
"exceljs": "^4.4.0",
"flowbite": "^2.0.0",
"flowbite-svelte": "^0.44.19",
"flowbite-svelte-icons": "^0.4.5",
Expand Down
44 changes: 44 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const words = [
["10000", "10,000"],
["CAP", "Capacity"],
["EXCU", "Execution"],
["GB", "Gigabyte"],
["HR", "Hour"],
["HSTD", "Hosted"],
["INST", "Instance"],
["M", "Million"],
["MNTR", "Monitored"],
["MO", "Month"],
["PR", "Per"],
["PROCESS", "Processed"],
["PRTN", "Partition"],
["RSRC", "Resources"],
["TB", "Terabyte"],
["TX", "Transfer"],
["UN", "Unit"],
["UN", "Units"],
["WKSP", "Workspace"],
];

const replace = (word: string): string[] => {
const terms = words.filter((r) => r[0] === word.toUpperCase());
return terms.length ? terms.map((t) => t[1]) : [word];
};

export const cleanName = (name: string, uom: string) => {
const abbrevs = uom.split(" ");
for (let i = abbrevs.length - 1; i >= 0; i--) {
const searchterms = replace(abbrevs[i]);
for (const term of searchterms) {
name = name.replace(new RegExp(` +${term}$`, "i"), "");
}
name = name.replace(new RegExp(` +${abbrevs[i]}$`, "i"), "");
const lastword = / +([a-z]+)$/i.exec(name);
if (i !== 0 && lastword && lastword.length === 2) {
name = name.replace(new RegExp(` +${lastword[1]}$`, "i"), "");
}
}
name = name.replace(/ *- *$/, "");
// console.log('name', name);
return name.trim();
};
2 changes: 1 addition & 1 deletion src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import { ChartLineUpSolid, EuroSolid, ClipboardOutline } from 'flowbite-svelte-icons';
import { computation } from '$lib/stores/computation';
import { ratecard,usage } from '$lib/db';
import { ratecard, usage } from '$lib/db';
</script>

<svelte:head>
Expand Down
3 changes: 2 additions & 1 deletion src/routes/ratecard/Proposal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import { Heading, Button } from 'flowbite-svelte';
import type { Rate, Quote, Line } from '$lib/interfaces/index';
import { CheckSolid, EditOutline, TrashBinOutline } from 'flowbite-svelte-icons';
import { exportToXls } from './xlsx';
// import { exportToXls } from './xlsx';
import { exportToXls } from './exceljs';
let lines: Line[];
let tpm: number;
const recalculate = () => {
Expand Down
1 change: 1 addition & 0 deletions src/routes/ratecard/Table.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
><CirclePlusOutline class="outline-none w-5 h-5 text-lightgreen" /></td
>
<td class="px-2 font-semibold text-black dark:text-white">{rate['Product Part']}</td>
<!-- <td class="px-2">{cleanName(rate['Product Name'], rate['UOM'])}</td> -->
<td class="px-2">{rate['Product Name']}</td>
<td class="px-2">{rate['UOM']}</td>
<td class="px-2 font-semibold text-black dark:text-white">{rate['Net Unit Price']}</td>
Expand Down
51 changes: 51 additions & 0 deletions src/routes/ratecard/exceljs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import type { Line } from "$lib/interfaces/index";
import Excel, { Row, Workbook, Worksheet } from "exceljs";
import { cleanName } from "$lib/utils";

const type =
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

const getWorkBook = async (): Promise<Workbook> => {
const url = "/sizing.xlsx";
const arrayBuffer: ArrayBuffer = await (await fetch(url)).arrayBuffer();
const workbook = new Excel.Workbook();
return await workbook.xlsx.load(arrayBuffer);
};

const changeWorkBook = (workbook: Workbook, lines: Line[]) => {
const worksheet: Worksheet | undefined = workbook.getWorksheet("Quote");
if (worksheet) {
// console.log(worksheet.getCell("I11").value);
let idx = 5;
for (const line of lines) {
const row: Row = worksheet.getRow(idx);
const formula = line.multiplier === 1 ? `G${idx}` : `$D$2*G${idx}`;
row.getCell("C").value = cleanName(line.name, line.uom);
row.getCell("D").value = line.part;
row.getCell("E").value = line.uom;
row.getCell("F").value = line.quantity;
row.getCell("G").value = line.rate;
row.getCell("H").value = { formula };
++idx;
}
return true;
}
return false;
};

const downloadWorkBook = async (workbook: Workbook) => {
const buffer = await workbook.xlsx.writeBuffer();
const blob = new Blob([buffer], { type });
const link = document.createElement("a");
link.href = URL.createObjectURL(blob);
link.download = "formulae.xlsx";
link.click();
URL.revokeObjectURL(link.href);
};

export const exportToXls = async (lines: Line[]) => {
const workbook: Workbook = await getWorkBook();
const success = changeWorkBook(workbook, lines);
if (success) await downloadWorkBook(workbook);
};
2 changes: 1 addition & 1 deletion tailwind.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const config = {
900: '#FF1A93'
},
secondary: '#009EFF',
poppy: '#D64045FF',
poppy: '#FF0051FF',
marianblue: '#324376FF',
verdigris: '#7EBDC2FF',
lightgreen: '#93FF96FF',
Expand Down

1 comment on commit 7156104

@deno-deploy
Copy link
Contributor

@deno-deploy deno-deploy bot commented on 7156104 Sep 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Failed to deploy:

Relative import path "crypto" not prefixed with / or ./ or ../ and not in import map from "file:///src/server.js"

Please sign in to comment.