diff --git a/app/Http/Controllers/TransaksiController.php b/app/Http/Controllers/TransaksiController.php index f79d172..2628dbb 100644 --- a/app/Http/Controllers/TransaksiController.php +++ b/app/Http/Controllers/TransaksiController.php @@ -42,51 +42,64 @@ class TransaksiController extends Controller } // Membuat transaksi baru - public function store(Request $request) - { - $request->validate([ - 'id_kasir' => 'required|exists:akun,id', - 'id_sales' => 'nullable|exists:sales,id', - 'nama_sales' => 'nullable|string', - 'no_hp' => 'nullable|string', - 'alamat' => 'nullable|string', - 'ongkos_bikin' => 'nullable|numeric', - 'total_harga' => 'required|numeric', - 'items' => 'required|array', - 'items.*.id_item' => 'required|exists:item,id', - 'items.*.harga_deal' => 'required|numeric', + public function store(Request $request) +{ + // Ambil user yang login via Sanctum + $kasir = $request->user(); // user authenticated + if (!$kasir) { + return response()->json(['error' => 'Unauthorized'], 401); + } + + // Validasi request (id_kasir dihapus karena otomatis dari token) + $request->validate([ + 'id_sales' => 'nullable|exists:sales,id', + 'nama_sales' => 'required|string', + 'no_hp' => 'required|string', + 'alamat' => 'required|string', + 'ongkos_bikin' => 'nullable|numeric|min:0', + 'total_harga' => 'required|numeric', + 'items' => 'required|array', + 'items.*.id_item' => 'required|exists:items,id', + 'items.*.harga_deal' => 'required|numeric', + ]); + + DB::beginTransaction(); + try { + $transaksi = Transaksi::create([ + 'id_kasir' => $kasir->id, // ambil dari token + 'id_sales' => $request->id_sales, + 'nama_sales' => $request->nama_sales, + 'no_hp' => $request->no_hp, + 'alamat' => $request->alamat, + 'ongkos_bikin' => $request->ongkos_bikin ?? 0, + 'total_harga' => $request->total_harga, ]); - DB::beginTransaction(); - try { - $transaksi = Transaksi::create([ - 'id_kasir' => $request->id_kasir, - 'id_sales' => $request->id_sales, - 'nama_sales' => $request->nama_sales, - 'no_hp' => $request->no_hp, - 'alamat' => $request->alamat, - 'ongkos_bikin' => $request->ongkos_bikin, - 'total_harga' => $request->total_harga, + foreach ($request->items as $it) { + ItemTransaksi::create([ + 'id_transaksi' => $transaksi->id, + 'id_item' => $it['id_item'], + 'harga_deal' => $it['harga_deal'], ]); - foreach ($request->items as $it) { - ItemTransaksi::create([ - 'id_transaksi' => $transaksi->id, - 'id_item' => $it['id_item'], - 'harga_deal' => $it['harga_deal'], - ]); - - Item::where('id', $it['id_item'])->update(['is_sold' => true]); - } - - DB::commit(); - return response()->json($transaksi->load('items'), 201); - - } catch (\Exception $e) { - DB::rollBack(); - return response()->json(['error' => $e->getMessage()], 500); + Item::where('id', $it['id_item'])->update(['is_sold' => true]); } - } + + DB::commit(); + return response()->json( + $transaksi->load(['itemTransaksi.item.produk.foto', 'kasir', 'sales']), + 201 + ); + + } catch (\Exception $e) { + DB::rollBack(); + return response()->json([ + 'error' => $e->getMessage(), + 'trace' => $e->getTrace() + ], 500); +} +} + // Update transaksi public function update(Request $request, $id) diff --git a/app/Models/Item.php b/app/Models/Item.php index fb02e07..ab88fe7 100644 --- a/app/Models/Item.php +++ b/app/Models/Item.php @@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; class Item extends Model { use HasFactory; + protected $table = 'items'; protected $fillable = [ 'id_produk', diff --git a/app/Models/ItemTransaksi.php b/app/Models/ItemTransaksi.php index 994e6e2..b596de9 100644 --- a/app/Models/ItemTransaksi.php +++ b/app/Models/ItemTransaksi.php @@ -11,8 +11,8 @@ class ItemTransaksi extends Model use HasFactory; protected $fillable = [ - 'id_transaksi', - 'id_item', + 'id_transaksi', + 'id_item', 'harga_deal', 'posisi_asal' ]; diff --git a/resources/js/components/CreateAkun.vue b/resources/js/components/CreateAkun.vue index 3876180..0cd144b 100644 --- a/resources/js/components/CreateAkun.vue +++ b/resources/js/components/CreateAkun.vue @@ -7,33 +7,45 @@
- +{{ errorMessage }}
@@ -72,25 +84,65 @@ export default { data() { return { form: { nama: "", password: "", role: "" }, + errors: { nama: "", password: "", role: "" }, errorMessage: "", }; }, methods: { + clearError(field) { + this.errors[field] = ""; + this.errorMessage = ""; + }, + validateForm() { + let valid = true; + this.errors = { nama: "", password: "", role: "" }; + + if (!this.form.nama) { + this.errors.nama = "Nama wajib diisi"; + valid = false; + } + if (!this.form.password) { + this.errors.password = "Password wajib diisi"; + valid = false; + } else if (this.form.password.length < 6) { + this.errors.password = "Password minimal 6 karakter"; + valid = false; + } + if (!this.form.role) { + this.errors.role = "Role wajib dipilih"; + valid = false; + } else if (!["owner", "kasir"].includes(this.form.role)) { + this.errors.role = "Role harus owner atau kasir"; + valid = false; + } + + return valid; + }, async createAkun() { + if (!this.validateForm()) return; + try { - await axios.post("api/user", this.form, { + await axios.post("/api/user", this.form, { headers: { - Authorization: `Bearer ${localStorage.getItem( - "token" - )}`, + Authorization: `Bearer ${localStorage.getItem("token")}`, }, }); + + // reset form this.form = { nama: "", password: "", role: "" }; this.$emit("refresh"); this.$emit("close"); } catch (err) { - this.errorMessage = - err.response?.data?.message || "Gagal menambah akun."; + if (err.response?.status === 422 && err.response.data.errors) { + // tampilkan error validasi backend + const backendErrors = err.response.data.errors; + Object.keys(backendErrors).forEach((key) => { + this.errors[key] = backendErrors[key][0]; + }); + } else { + this.errorMessage = + err.response?.data?.message || "Gagal menambah akun."; + } console.error("Gagal tambah akun:", err); } }, diff --git a/resources/js/components/EditAkun.vue b/resources/js/components/EditAkun.vue index 3007cd0..9730213 100644 --- a/resources/js/components/EditAkun.vue +++ b/resources/js/components/EditAkun.vue @@ -1,118 +1,174 @@ -+ {{ errorMessage }} +
Selasa/20-08-2025
+ +{{ getCurrentDate() }}
+TRS-XXX-XXX
+ +{{ generateTransactionCode() }}
+|
- |
- A1, Brankas | -2,4 g | -23 K | -Rp9.000.000 | -2 | -Rp18.000.000 | -|||||
|
- |
- A2 | -2,4 g | -23 K | -Rp3.000.000 | +{{ item.posisi || 'Brankas' }} | +{{ item.produk.berat || '-' }} | +{{ item.produk.kadar || '-' }} | +Rp{{ item.harga_deal.toLocaleString() }} | 1 | -Rp3.000.000 | +Rp{{ item.harga_deal.toLocaleString() }} |
|
Sales + |
@@ -132,13 +130,19 @@
-
|
||||||||||