Skip to content

Commit 853b628

Browse files
committed
removed "active" from ProdajnoMjesto
implemented storno račun with ProdajnoMjesto
1 parent a468c3d commit 853b628

5 files changed

Lines changed: 33 additions & 54 deletions

File tree

backend/index.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ app.post("/api/receipts", requireAuth, async (req, res) => {
491491
discountValue,
492492
shippingCost,
493493
prodajnoMjestoNaziv: appSettings.prodajnoMjesto.name,
494+
prodajnoMjestoId: appSettings.prodajnoMjesto.id,
494495
items: {
495496
create: items.map((item) => ({
496497
name: item.name,
@@ -621,6 +622,8 @@ app.put("/api/receipts/:id/storno", requireAuth, async (req, res) => {
621622
internalNote: `STORNO of ${originalReceipt.invoiceNumber}`,
622623
discountValue: originalReceipt.discountValue,
623624
shippingCost: originalReceipt.shippingCost,
625+
prodajnoMjestoNaziv: originalReceipt.prodajnoMjestoNaziv,
626+
prodajnoMjestoId: originalReceipt.prodajnoMjestoId,
624627
items: {
625628
create: originalReceipt.items.map((item) => ({
626629
name: item.name,
@@ -646,6 +649,21 @@ app.put("/api/receipts/:id/storno", requireAuth, async (req, res) => {
646649
},
647650
});
648651

652+
// Dohvati prodajno mjesto originalnog računa
653+
const originalProdajnoMjesto = await prisma.prodajnoMjesto.findUnique({
654+
where: { id: originalReceipt.prodajnoMjestoId },
655+
});
656+
if (!originalProdajnoMjesto) {
657+
return res.status(400).json({ error: "Prodajno mjesto originalnog računa nije pronađeno." });
658+
}
659+
let firaApiKey;
660+
try {
661+
firaApiKey = decrypt(originalProdajnoMjesto.firaApiKey);
662+
if (!firaApiKey) throw new Error("Prazan ključ");
663+
} catch {
664+
return res.status(500).json({ error: "Greška pri dešifriranju API ključa prodajnog mjesta." });
665+
}
666+
649667
// Fiscalize storno receipt via FIRA
650668
const firaResult = await handleOrderFiscalization({
651669
id: stornoReceipt.id,
@@ -655,7 +673,7 @@ app.put("/api/receipts/:id/storno", requireAuth, async (req, res) => {
655673
currency: stornoReceipt.currency,
656674
paymentType: stornoReceipt.paymentType,
657675
items: stornoReceipt.items,
658-
});
676+
}, { firaApiKey, prodajnoMjestoNaziv: originalProdajnoMjesto.name });
659677

660678
if (firaResult && firaResult.invoiceNumber) {
661679
await prisma.receipt.update({
@@ -931,20 +949,19 @@ app.get('/api/prodajna-mjesta', requireAuth, async (req, res) => {
931949
// POST new prodajno mjesto
932950
app.post('/api/prodajna-mjesta', requireAuth, async (req, res) => {
933951
try {
934-
const { name, businessSpace, paymentDevice, firaApiKey, active } = req.body;
952+
const { name, businessSpace, paymentDevice, firaApiKey } = req.body;
935953

936954
// Log for debugging
937955
console.log("Attempting to encrypt key...");
938956
const encryptedKey = encrypt(firaApiKey);
939-
957+
940958
console.log("Attempting to save to database...");
941959
const newLocation = await prisma.prodajnoMjesto.create({
942-
data: {
943-
name,
944-
businessSpace,
945-
paymentDevice,
946-
firaApiKey: encryptedKey,
947-
active
960+
data: {
961+
name,
962+
businessSpace,
963+
paymentDevice,
964+
firaApiKey: encryptedKey
948965
}
949966
});
950967

@@ -963,9 +980,9 @@ app.post('/api/prodajna-mjesta', requireAuth, async (req, res) => {
963980
// PUT (update) prodajno mjesto
964981
app.put('/api/prodajna-mjesta/:id', requireAuth, async (req, res) => {
965982
const { id } = req.params;
966-
const { name, businessSpace, paymentDevice, firaApiKey, active } = req.body;
983+
const { name, businessSpace, paymentDevice, firaApiKey } = req.body;
967984
try {
968-
const data = { name, businessSpace, paymentDevice, active };
985+
const data = { name, businessSpace, paymentDevice };
969986
if (firaApiKey && firaApiKey !== "********") {
970987
data.firaApiKey = encrypt(firaApiKey);
971988
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE "ProdajnoMjesto" DROP COLUMN "active";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE "Receipt" ADD COLUMN "prodajnoMjestoId" INTEGER;

backend/prisma/schema.prisma

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ model Receipt {
9999
discountValue Decimal @db.Decimal(10, 2) @default(0)
100100
shippingCost Decimal @db.Decimal(10, 2) @default(0)
101101
prodajnoMjestoNaziv String?
102+
prodajnoMjestoId Int?
102103
status ReceiptStatus @default(RACUN)
103104
items ReceiptItem[]
104105
transaction Transaction?
@@ -184,7 +185,6 @@ model ProdajnoMjesto {
184185
businessSpace String
185186
paymentDevice String
186187
firaApiKey String
187-
active Boolean @default(true)
188188
appSettings AppSettings[]
189189
}
190190

frontend/src/pages/admin/ProdajnaMjesta.jsx

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export default function ProdajnaMjesta() {
1414
businessSpace: "",
1515
paymentDevice: "",
1616
firaApiKey: "",
17-
active: true,
1817
});
1918

2019
const refreshData = async () => {
@@ -59,7 +58,6 @@ export default function ProdajnaMjesta() {
5958
businessSpace: "",
6059
paymentDevice: "",
6160
firaApiKey: "",
62-
active: true,
6361
});
6462
setEditingId(null);
6563
};
@@ -96,7 +94,6 @@ export default function ProdajnaMjesta() {
9694
businessSpace: loc.businessSpace,
9795
paymentDevice: loc.paymentDevice,
9896
firaApiKey: loc.firaApiKey || "",
99-
active: loc.active,
10097
});
10198
setEditingId(loc.id);
10299
setShowForm(true);
@@ -120,21 +117,6 @@ export default function ProdajnaMjesta() {
120117
}
121118
};
122119

123-
const toggleActive = async (id, currentActive) => {
124-
try {
125-
const location = locations.find(l => l.id === id);
126-
await fetch(`${import.meta.env.VITE_API_URL}/api/prodajna-mjesta/${id}`, {
127-
method: "PUT",
128-
headers: { "Content-Type": "application/json" },
129-
credentials: "include",
130-
body: JSON.stringify({ ...location, active: !currentActive }),
131-
});
132-
refreshData();
133-
} catch (error) {
134-
console.error("Error toggling status:", error);
135-
}
136-
};
137-
138120
const handleSelectActive = async (id) => {
139121
setSavingActive(true);
140122
try {
@@ -217,17 +199,6 @@ export default function ProdajnaMjesta() {
217199
/>
218200
</div>
219201

220-
<div className="form-group">
221-
<label>
222-
<input
223-
type="checkbox"
224-
checked={formData.active}
225-
onChange={(e) => setFormData({ ...formData, active: e.target.checked })}
226-
/>
227-
{" "}Aktivno
228-
</label>
229-
</div>
230-
231202
<div style={{ display: "flex", gap: "10px" }}>
232203
<button type="submit" className="btn-success">
233204
{editingId ? "Ažuriraj" : "Spremi"}
@@ -256,7 +227,7 @@ export default function ProdajnaMjesta() {
256227
>
257228
{activeLocationId === null && <option value="" disabled>— odaberite —</option>}
258229
{locations.map(loc => (
259-
<option key={loc.id} value={loc.id}>{loc.name}{!loc.active ? " (neaktivno)" : ""}</option>
230+
<option key={loc.id} value={loc.id}>{loc.name}</option>
260231
))}
261232
</select>
262233
{savingActive && <span style={{ color: "#888", fontSize: "0.85rem" }}>Spremanje...</span>}
@@ -273,27 +244,16 @@ export default function ProdajnaMjesta() {
273244
<th>Prostor</th>
274245
<th>Uređaj</th>
275246
<th>API Ključ</th>
276-
<th>Aktivno</th>
277247
<th>Akcije</th>
278248
</tr>
279249
</thead>
280250
<tbody>
281251
{locations.map(loc => (
282-
<tr key={loc.id} className={!loc.active ? "inactive" : activeLocationId === loc.id ? "active-location" : ""}>
252+
<tr key={loc.id} className={activeLocationId === loc.id ? "active-location" : ""}>
283253
<td>{loc.name}</td>
284254
<td><code>{loc.businessSpace}</code></td>
285255
<td><code>{loc.paymentDevice}</code></td>
286256
<td><code>{loc.firaApiKey || "-"}</code></td>
287-
<td>
288-
<label className="toggle-switch">
289-
<input
290-
type="checkbox"
291-
checked={loc.active}
292-
onChange={() => toggleActive(loc.id, loc.active)}
293-
/>
294-
<span className="toggle-slider"></span>
295-
</label>
296-
</td>
297257
<td className="actions">
298258
<button onClick={() => handleEdit(loc)} className="icon-btn edit" title="Uredi">
299259
<i className="fas fa-edit"></i>

0 commit comments

Comments
 (0)