const Joi = require('joi'); const { pageQuery, limitQuery } = require('../../utils/pagination.validation'); const { GST_TREATMENT_VALUES, SOURCE_OF_SUPPLY_VALUES, } = require('./vendors.constants'); const vendorTypes = ['RAW_MATERIAL', 'PACKING_MATERIAL', 'ASSET_CAPITAL', 'SERVICE', 'GENERAL']; const vendorStatuses = ['active', 'inactive', 'blacklisted']; const addressTypes = ['REGISTERED', 'BILLING', 'DISPATCH']; const accountTypes = ['CURRENT', 'SAVINGS', 'OVERDRAFT']; const createVendorSchema = Joi.object({ vendor_name: Joi.string().max(200).required(), vendor_type: Joi.string() .valid(...vendorTypes) .required(), gst_treatment: Joi.string() .valid(...GST_TREATMENT_VALUES) .allow(null, '') .optional(), source_of_supply: Joi.string() .valid(...SOURCE_OF_SUPPLY_VALUES) .allow(null, '') .optional(), gstin: Joi.string().length(15).allow(null, '').optional(), pan: Joi.string().length(10).allow(null, '').optional(), payment_term_id: Joi.number().integer().positive().allow(null).optional(), credit_period_days: Joi.number().integer().min(0).default(0), remarks: Joi.string().allow(null, '').optional(), is_active: Joi.boolean().default(true), }); const updateVendorSchema = Joi.object({ vendor_name: Joi.string().max(200).optional(), vendor_type: Joi.string() .valid(...vendorTypes) .optional(), gst_treatment: Joi.string() .valid(...GST_TREATMENT_VALUES) .allow(null, '') .optional(), source_of_supply: Joi.string() .valid(...SOURCE_OF_SUPPLY_VALUES) .allow(null, '') .optional(), gstin: Joi.string().length(15).allow(null, '').optional(), pan: Joi.string().length(10).allow(null, '').optional(), payment_term_id: Joi.number().integer().positive().allow(null).optional(), credit_period_days: Joi.number().integer().min(0).optional(), remarks: Joi.string().allow(null, '').optional(), is_active: Joi.boolean().optional(), }).min(1); const vendorStatusSchema = Joi.object({ status: Joi.string() .valid(...vendorStatuses) .required(), }); const listVendorsQuerySchema = Joi.object({ page: pageQuery(), limit: limitQuery(), search: Joi.string().allow('').optional(), status: Joi.string() .valid(...vendorStatuses) .optional(), vendor_type: Joi.string() .valid(...vendorTypes) .optional(), is_active: Joi.boolean().optional(), }); const exportVendorsQuerySchema = listVendorsQuerySchema.keys({ page: Joi.strip(), limit: Joi.strip(), }); const createAddressSchema = Joi.object({ address_type: Joi.string() .valid(...addressTypes) .required(), address_line1: Joi.string().max(200).allow(null, '').optional(), address_line2: Joi.string().max(200).allow(null, '').optional(), city: Joi.string().max(100).allow(null, '').optional(), state: Joi.string().max(100).allow(null, '').optional(), pincode: Joi.string().max(10).allow(null, '').optional(), country: Joi.string().max(100).default('India'), gstin: Joi.string().length(15).allow(null, '').optional(), is_active: Joi.boolean().default(true), }); const updateAddressSchema = createAddressSchema .fork(Object.keys(createAddressSchema.describe().keys), (schema) => schema.optional()) .min(1); const createContactSchema = Joi.object({ contact_name: Joi.string().max(150).required(), designation: Joi.string().max(100).allow(null, '').optional(), phone: Joi.string().max(15).allow(null, '').optional(), email: Joi.string().email().max(150).allow(null, '').optional(), is_primary: Joi.boolean().default(false), is_active: Joi.boolean().default(true), }); const updateContactSchema = createContactSchema .fork(Object.keys(createContactSchema.describe().keys), (schema) => schema.optional()) .min(1); const createBankDetailSchema = Joi.object({ bank_name: Joi.string().max(150).required(), branch: Joi.string().max(150).allow(null, '').optional(), account_number: Joi.string().max(30).required(), ifsc: Joi.string().max(15).required(), account_holder_name: Joi.string().max(200).required(), account_type: Joi.string() .valid(...accountTypes) .default('CURRENT'), is_primary: Joi.boolean().default(false), is_active: Joi.boolean().default(true), }); const updateBankDetailSchema = Joi.object({ bank_name: Joi.string().max(150).optional(), branch: Joi.string().max(150).allow(null, '').optional(), account_number: Joi.string().max(30).optional(), ifsc: Joi.string().max(15).optional(), account_holder_name: Joi.string().max(200).optional(), account_type: Joi.string() .valid(...accountTypes) .optional(), is_primary: Joi.boolean().optional(), is_active: Joi.boolean().optional(), }).min(1); const createItemMappingSchema = Joi.object({ item_id: Joi.number().integer().positive().required(), last_purchase_rate: Joi.number().precision(4).min(0).allow(null).optional(), is_preferred: Joi.boolean().default(false), is_active: Joi.boolean().default(true), }); const updateItemMappingSchema = Joi.object({ last_purchase_rate: Joi.number().precision(4).min(0).allow(null).optional(), is_preferred: Joi.boolean().optional(), is_active: Joi.boolean().optional(), }).min(1); module.exports = { createVendorSchema, updateVendorSchema, vendorStatusSchema, listVendorsQuerySchema, exportVendorsQuerySchema, createAddressSchema, updateAddressSchema, createContactSchema, updateContactSchema, createBankDetailSchema, updateBankDetailSchema, createItemMappingSchema, updateItemMappingSchema, };