import '../api/odoo_client.dart'; import '../models/portfolio.dart'; import '../models/saham_transaksi.dart'; /// Layanan domain utk model Odoo saat_invest. class StockService { StockService(this.client); final OdooClient client; // ---- Saham ---- Future> listSaham() async { final fields = ['name', 'perusahaan', 'sector']; final res = await client.executeSearchReadPayload("saat.saham", fields); return (res as List).map((e) => Saham.fromOdoo(e as Map)).toList(); } // Tambah saham baru ke Odoo saat.saham.create Future addSaham({ required String kode, String? perusahaan, String? sektor, }) async { final vals = { 'name': kode, if (perusahaan != null && perusahaan.isNotEmpty) 'perusahaan': perusahaan, if (sektor != null && sektor.isNotEmpty) 'sector': sektor, }; final res = await client.executeKwd('saat.saham', 'create', args: [vals]); return (res as num).toInt(); } // Update data saham (perusahaan, sektor) Future updateSaham(int id, {String? perusahaan, String? sektor}) async { final vals = { if (perusahaan != null) 'perusahaan': perusahaan, if (sektor != null) 'sector': sektor, }; await client.executeKwd('saat.saham', 'write', args: [[id], vals]); } // --- Hapus saham (optional) --- Future hapusSaham(int id) async { await client.executeKwd('saat.saham', 'unlink', args: [[id]]); } // ---- Portofolio ---- Future> listPortofolio() async { final fields = [ 'saham_id', 'sisa_lembar', 'avg_cost', 'harga_sekarang', 'nilai_sekarang', 'realized_pl', 'unrealized_pl', ]; final res = await client.executeSearchReadPayload("saat.portofolio", fields); return (res as List).map((e) => Portfolio.fromOdoo(e as Map)).toList(); } // ---- Transaksi ---- Future> listTransaksi() async { final fields = ['tanggal', 'tipe', 'saham_id', 'volume', 'harga', 'fee', 'total', 'state']; final res = await client.executeSearchReadPayload("saat.transaksi", fields); return (res as List).map((e) => Transaksi.fromOdoo(e as Map)).toList(); } Future addTransaksi({ required int sahamId, required String tipe, // buy | sell required int volume, required double harga, required double fee, String? tanggal, // 'YYYY-MM-DD' }) async { final vals = { 'saham_id': sahamId, 'tipe': tipe, 'volume': volume, 'harga': harga, 'fee': fee, if (tanggal != null) 'tanggal': tanggal, }; final res = await client.executeKwd('saat.transaksi', 'create', args: [vals]); return (res as num).toInt(); } Future cancelTransaksi(int id) async { await client.executeKwd('saat.transaksi', 'action_cancel', args: [[id]]); } Future hapusTransaksi(List ids) async { await client.executeKwd('saat.transaksi', 'unlink', args: [ids]); } }