const asyncHandler = require("../Middleware/async"); const sequelize = require('sequelize') const db = require('../Config/db') const SupplierModel = require('../Model/SupplierModel') const ErrorResponse = require('../utils/errorResponse') const SuccessResponse = require('../utils/SuccessResponse') const fs = require('fs'); const DeliveryAdressModel = require('../Model/DeliveryModel') const dateFormat = require('dateformat'); exports.getAllDeliveryAddress = asyncHandler(async (req, res, next) => { try { const deliveryAddress = await DeliveryAdressModel.findAll({ where: { DEL_TYPE: "1" } }) // console.log(obj); // const supplier = await SupplierModel.findAll({ // where: { // DB_CODE: req.user.DB_CODE // } // } // ) res.status(200).send({ success: true, data: deliveryAddress }) } catch (e) { console.log("ERROR : ", e) throw new ErrorResponse(e, 400) } }) exports.getAllDeliveryAddressBySupplier = asyncHandler(async (req, res, next) => { try { const deliveryAddress = await DeliveryAddressModel.findAll({ where: { DB_CODE: req.dbcode, DEL_CODE: req.params.id } }) res.status(200).send({ success: true, data: deliveryAddress }) } catch (e) { throw new ErrorResponse(e, 400) } }) exports.postDeliveryAddress = asyncHandler(async (req, res, next) => { const { DEL_ADD_1, DEL_NAME } = req.body; const data = req.body; if (!DEL_ADD_1) { return next( new ErrorResponse("Please provide a delivery address information!", 400) ); } if (!DEL_NAME) { return next( new ErrorResponse("Please provide a delivery name!", 400) ); } let ADD_CODE if (!data["ADD_CODE"]) { ADD_CODE = await generateNextAddressCode(req.user.DB_CODE); } try { await DeliveryAdressModel.sequelize.transaction(async (t) => { await DeliveryAdressModel.create( { ADD_CODE: "", // if it is supplier then no need to add ADD_CODE in this table DB_CODE: req.user.DB_CODE, DEL_CODE: data["ADD_CODE"] ?? ADD_CODE, DEL_NAME: data["DEL_NAME"], DEL_ADD_1: DEL_ADD_1, DEL_ADD_2: "", DEL_ADD_3: "", DEL_ADD_4: "", DEL_ADD_5: "", DEL_TEL: data["DEL_TEL"] ?? "", DEL_FAX: data["DEL_FAX"] ?? "", DEL_EMAIL: "", DEL_WEB: "", DEL_CONT: "", DEL_COM_1: "", DEL_COM_2: "", DEL_TYPE: "0", // 1 for CUSTOMER 0 for SUPPLIER USER_CREA: dateFormat(new Date(), "yyyy-mm-dd"), USER_UPDT: dateFormat(new Date(), "yyyy-mm-dd"), USER_CODE: req.user.USER_NAME, }, { transaction: t } ); res.send(new SuccessResponse("success")).status(200); }); } catch (error) { return next(new ErrorResponse(`Server Error ${error}`, 500)); } }) async function generateNextAddressCode(dbCode) { const lastSupplier = await DeliveryAddressModel.findOne( { where: { DB_CODE: dbCode }, order: [ [sequelize.cast(sequelize.col('ADD_CODE'), 'integer'), 'DESC'] ] }); if (!lastSupplier) { return '0001'; // Starting point if no suppliers are present } console.log("LAST SUPPLIER CODE ", lastSupplier.ADD_CODE) let nextCode = parseInt(lastSupplier.ADD_CODE, 10) + 1; // Check if the number is less than 10000 for zero-padding if (nextCode < 10000) { return nextCode.toString().padStart(4, '0'); } else { // If it's 10000 or more, just return the number return nextCode.toString(); } }