const asyncHandler = require("../Middleware/async"); const sequelize = require('sequelize') const DBInfo = require("../Model/Database"); const ItemModel = require("../Model/ItemModel"); const ErrorResponse = require("../utils/errorResponse"); const fs = require('fs') const dateFormat = require("dateformat"); exports.getAllItemsByStore = asyncHandler(async (req, res, next) => { try { let items = await ItemModel.findAll({ where: { DB_CODE: req.user.DB_CODE } }) console.log("ITEMS ", items) if (!items) { return next(new ErrorResponse("No item found", 400)); } res.status(200).send({ success: true, data: items }) } catch (err) { console.log("ERROR GET ALL ITEMS", err) return next(new ErrorResponse(err, 400)); } }) exports.createItemByStore = asyncHandler(async (req, res, next) => { try { var date = dateFormat(new Date(), "yyyy-mm-dd HH:mm:ss") const data = JSON.parse(req.body['data']); const items = data['items']; var photo = req.file.path let imageData = fs.readFileSync(req.file.path) const itemCode = items['ITEM_CODE']; const itemBarCode = items['ITEM_BCODE'] const itemDes = items['ITEM_DESC']; const unitStock = items['UNIT_STOCK'] ?? '1'; const unitSale = items['UNIT_SALE'] ?? '1'; const catCode = items['CAT_CODE']; // CAT_CODE -> CAT_DETAIL_ID const itemCost1 = items['ITEM_COST1']; const itemPrice1 = items['ITEM_PRICE1']; if (!itemCode || !itemBarCode || !itemDes || !unitStock || !unitSale || !catCode || !itemCost1 || !itemPrice1) { throw ("Please provide all required fields"); } const existingItem = await ItemModel.findOne({ where: { ITEM_CODE: itemCode, DB_CODE: req.user.DB_CODE } }); if (existingItem) { throw (`ITEM_CODE ${itemCode} already exists`); } console.log("DATE : ", date) const item = await ItemModel.create({ ITEM_CODE: itemCode, ITEM_BCODE: itemBarCode, ITEM_DESC: itemDes, DB_CODE: req.user.DB_CODE, UNIT_STOCK: unitStock, UNIT_SALE: unitSale, CAT_CODE: catCode, ITEM_STAT: "A", USER_CREA: req.user.USER_ID, DATE_CREA: date.toString(), ITEM_TYPE: "S", // S -> Kit Stock N -> Min Kit Stock I-> ដំណើរការទំនិញ ITEM_PRICE1: itemPrice1, ITEM_COST1: itemCost1, ITEM_COST2: 0, ITEM_PRICE2: 0, ITEM_CUS1: "", ITEM_CUS2: "", ITEM_CUS3: "", ITEM_CUS4: "", ITEM_CUS5: "", ITEM_CUS6: "", ITEM_CUS7: "", ITEM_CUS8: "", ITEM_CUS9_KH: "", ITEM_CUS10_KH: "", USER_UPDT: req.user.USER_NAME, DATE_UPDT: date, }) let folderPath = process.env.ITEM_IMAGE_LOCATION + "/" + req.user.DB_CODE + "/" if (!fs.existsSync(folderPath)) fs.mkdirSync(folderPath, { recursive: true, }) fs.writeFileSync(folderPath + item.ITEM_CODE + '.jpg', imageData) fs.unlinkSync(req.file.path) res.status(200).send({ success: true, data: item }) } catch (err) { console.log("ERROR CREATE ITEM", err) fs.unlinkSync(req.file.path) return next(new ErrorResponse(err, 400)); } }) exports.checkIfItemExist = asyncHandler(async (req, res, next) => { try { const item = await ItemModel.findOne({ where: { ITEM_CODE: req.params.item_code, DB_CODE: req.user.DB_CODE } }) if (!item) { return next(new ErrorResponse("Item not found", 400)); } res.status(200).send({ success: true, data: item }) } catch (err) { console.log("ERROR CHECK ITEM", err) return next(new ErrorResponse(err, 400)); } } )