File size: 2,983 Bytes
ef874e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { Request, Response, NextFunction } from 'express';
import { StokService } from '../services/stok.service';
import { sukses } from '../utils/response';
import { AuthenticatedRequest } from '../middlewares/auth.middleware';

export class StokController {
  static async addIncomingStock(req: AuthenticatedRequest, res: Response, next: NextFunction) {
    try {
      const actorId = req.user.id;
      const ip = (req.ip || req.socket.remoteAddress || 'unknown').replace('::ffff:', '');
      const userAgent = (req.headers['user-agent'] || 'unknown') as string;

      const batch = await StokService.tambahBarangMasuk(req.body, actorId, ip, userAgent);
      return sukses(res, batch, 'Barang masuk berhasil dicatat');
    } catch (error) {
      next(error);
    }
  }

  static async createStockOpname(req: AuthenticatedRequest, res: Response, next: NextFunction) {
    try {
      const actorId = req.user.id;
      const ip = (req.ip || req.socket.remoteAddress || 'unknown').replace('::ffff:', '');
      const userAgent = (req.headers['user-agent'] || 'unknown') as string;

      const opname = await StokService.buatStokOpname(req.body, actorId, ip, userAgent);
      return sukses(res, opname, 'Draft stok opname berhasil dibuat');
    } catch (error) {
      next(error);
    }
  }

  static async approveStockOpname(req: AuthenticatedRequest, res: Response, next: NextFunction) {
    try {
      const opnameId = parseInt(req.params.opname_id);
      const actorId = req.user.id;
      const ip = (req.ip || req.socket.remoteAddress || 'unknown').replace('::ffff:', '');
      const userAgent = (req.headers['user-agent'] || 'unknown') as string;

      const opname = await StokService.approveStokOpname(opnameId, actorId, ip, userAgent);
      return sukses(res, opname, 'Stok opname disetujui, kuantitas stok produk disesuaikan');
    } catch (error) {
      next(error);
    }
  }

  static async getStockMutations(req: Request, res: Response, next: NextFunction) {
    try {
      const skip = parseInt(req.query.skip as string) || 0;
      const limit = parseInt(req.query.limit as string) || 20;
      const produkId = req.query.produk_id ? parseInt(req.query.produk_id as string) : undefined;
      const tipe = req.query.tipe as string || undefined;

      const logs = await StokService.getStokLogs(skip, limit, produkId, tipe);
      return sukses(res, logs, 'Kartu stok berhasil dimuat');
    } catch (error) {
      next(error);
    }
  }

  static async getStockBatches(req: Request, res: Response, next: NextFunction) {
    try {
      const skip = parseInt(req.query.skip as string) || 0;
      const limit = parseInt(req.query.limit as string) || 20;
      const produkId = req.query.produk_id ? parseInt(req.query.produk_id as string) : undefined;

      const batches = await StokService.getStokBatches(skip, limit, produkId);
      return sukses(res, batches, 'Batch stok berhasil dimuat');
    } catch (error) {
      next(error);
    }
  }
}