/var/www/greso.tech/server/nsm/controllers/order/inventory
Edit: /var/www/greso.tech/server/nsm/controllers/order/inventory/create.js (20357B)
module.exports = function ({ db, logger }) {
const { NotFound, BadRequest } = require('../../../responseModels/errors')
const { isSet, isEmail, isPhone, isEmpty, minLength, isNumber, parseBool } = require('../../../utils/validator.util')
const uuid = require('uuid');
return async (req, res, next) => {
logger.child({ path: req.path, request: req.query }).info()
let t1 = performance.now()
let transaction = await db.sequelize.transaction()
try {
var {
id,
orderId,
type,
status,
} = req.body
var params = {
orderId,
}
// load order
const order = await db.order.findOne({
where: { id: orderId }
})
if (!order) {
throw new BadRequest({
code: 2032,
lang: req.user?.languageCode
})
}
// load last inventory
if (!isSet(order.orderInventoryId) || isEmpty(order.orderInventoryId)) {
throw new BadRequest({
code: 2087,
lang: req.user?.languageCode
})
}
if (!isSet(id) || isEmpty(id)) {
id = uuid.v4()
}
const lastInventory = await db.orderInventory.findOne({
where: { id: order.orderInventoryId }
})
if (!lastInventory) {
throw new BadRequest({
code: 2087,
lang: req.user?.languageCode
})
}
const lastDocuments = await db.orderDocument.findAll({
where: { inventoryId: order.orderInventoryId }
})
if (type == INVENTORY_TYPE.NEW_ESTIMATE
|| type == INVENTORY_TYPE.REVICE
|| type == INVENTORY_TYPE.DELIVERY
) {
lastInventory.status = INVENTORY_STATUS.CLOSE
await lastInventory.save()
}
const lastInventoryItems = await db.inventoryItems.findAll({
where: { orderInventoryId: order.orderInventoryId },
raw: true,
// attributes: { exclude: ['id'] }
})
const lastInventoryExtras = await db.inventoryExtras.findAll({
where: { orderInventoryId: order.orderInventoryId },
raw: true,
attributes: { exclude: ['id'] }
})
const lastInventoryItemsCategory = await db.inventoryItemsCategories.findAll({
where: { orderInventoryId: order.orderInventoryId },
raw: true,
// attributes: { exclude: ['id'] }
})
localRateId = lastInventory.localRateId
pickupFrom = lastInventory.pickupFrom
pickupTo = lastInventory.pickupTo
pickupHours = lastInventory.pickupHours
drivingFrom = lastInventory.drivingFrom
drivingTo = lastInventory.drivingTo
drivingHours = lastInventory.drivingHours
deliveryFrom = lastInventory.deliveryFrom
deliveryTo = lastInventory.deliveryTo
deliveryHours = lastInventory.deliveryHours
doubleDriving = lastInventory.doubleDriving
rate = lastInventory.rate
rate_multiply = lastInventory.rate_multiply
estimate_type = lastInventory.estimate_type
storage_rate = lastInventory.storage_rate
storage = lastInventory.storage
valuation = lastInventory.valuation
items_weight = lastInventory.items_weight
items_volume = lastInventory.items_volume
weight_brutto = lastInventory.weight_brutto
weight_tare = lastInventory.weight_tare
items_count = lastInventory.items_count
items_total = lastInventory.items_total
bulky_count = lastInventory.bulky_count
bulky_total = lastInventory.bulky_total
extras_count = lastInventory.extras_count
extras_total = lastInventory.extras_total
packages_count = lastInventory.packages_count
packages_total = lastInventory.packages_total
fuel_percent = lastInventory.fuel_percent
fuel_total = lastInventory.fuel_total
edited_packages = lastInventory.edited_packages
custom_packages = lastInventory.custom_packages
edited_weight = lastInventory.edited_weight
custom_weight = lastInventory.custom_weight
edited_volume = lastInventory.edited_volume
custom_volume = lastInventory.custom_volume
edited_total = lastInventory.edited_total
custom_total = lastInventory.custom_total
discount_comment = lastInventory.discount_comment
discount_cash = lastInventory.discount_cash
total = lastInventory.total
paid = lastInventory.paid
due = lastInventory.due
// check id
// if (!isSet(id) || isEmpty(id)) {
// id = uuid.v4()
// }else{
// const inventoryExists = await db.orderInventory.findOne({
// where: { id: id }
// })
// if (inventoryExists) {
// throw new BadRequest({
// code: 2000,
// lang: req.user?.languageCode
// })
// }
// }
// status
if(isSet(status)){
params['status'] = status
}else{
params['status'] = INVENTORY_STATUS.EDIT
}
// type
if(isSet(type)){
params['type'] = type
}else{
params['type'] = INVENTORY_TYPE.NEW_ESTIMATE
}
// not required
if (isSet(localRateId)){
if(isEmpty(localRateId)) {
throw new BadRequest({
code: 2108,
lang: req.user?.languageCode
})
}else{
params['localRateId'] = localRateId
}
}
if (isSet(pickupFrom)){
if(isEmpty(pickupFrom)) {
throw new BadRequest({
code: 2109,
lang: req.user?.languageCode
})
}else{
params['pickupFrom'] = pickupFrom
}
}
if (isSet(pickupTo)){
if(isEmpty(pickupTo)) {
throw new BadRequest({
code: 2110,
lang: req.user?.languageCode
})
}else{
params['pickupTo'] = pickupTo
}
}
if (isSet(pickupHours)){
if(isEmpty(pickupHours)) {
throw new BadRequest({
code: 2111,
lang: req.user?.languageCode
})
}else{
params['pickupHours'] = pickupHours
}
}
if (isSet(drivingFrom)){
if(isEmpty(drivingFrom)) {
throw new BadRequest({
code: 2112,
lang: req.user?.languageCode
})
}else{
params['drivingFrom'] = drivingFrom
}
}
if (isSet(drivingTo)){
if(isEmpty(drivingTo)) {
throw new BadRequest({
code: 2113,
lang: req.user?.languageCode
})
}else{
params['drivingTo'] = drivingTo
}
}
if (isSet(drivingHours)){
if(isEmpty(drivingHours)) {
throw new BadRequest({
code: 2114,
lang: req.user?.languageCode
})
}else{
params['drivingHours'] = drivingHours
}
}
if (isSet(deliveryFrom)){
if(isEmpty(deliveryFrom)) {
throw new BadRequest({
code: 2115,
lang: req.user?.languageCode
})
}else{
params['deliveryFrom'] = deliveryFrom
}
}
if (isSet(deliveryTo)){
if(isEmpty(deliveryTo)) {
throw new BadRequest({
code: 2116,
lang: req.user?.languageCode
})
}else{
params['deliveryTo'] = deliveryTo
}
}
if (isSet(deliveryHours)){
if(isEmpty(deliveryHours)) {
throw new BadRequest({
code: 2117,
lang: req.user?.languageCode
})
}else{
params['deliveryHours'] = deliveryHours
}
}
if (isSet(doubleDriving)){
if(isEmpty(doubleDriving)) {
throw new BadRequest({
code: 2120,
lang: req.user?.languageCode
})
}else{
params['doubleDriving'] = doubleDriving
}
}
if (isSet(rate)){
if(isEmpty(rate)) {
throw new BadRequest({
code: 2027,
lang: req.user?.languageCode
})
}else{
params['rate'] = rate
}
}
if (isSet(rate_multiply)){
if(isEmpty(rate_multiply)) {
throw new BadRequest({
code: 2101,
lang: req.user?.languageCode
})
}else{
params['rate_multiply'] = rate_multiply
}
}
if (isSet(estimate_type)){
if(isEmpty(estimate_type)) {
throw new BadRequest({
code: 2104,
lang: req.user?.languageCode
})
}else{
params['estimate_type'] = estimate_type
}
}
if (isSet(storage_rate)){
if(isEmpty(storage_rate)) {
throw new BadRequest({
code: 2027,
lang: req.user?.languageCode
})
}else{
params['storage_rate'] = storage_rate
}
}
if (isSet(storage)){
if(isEmpty(storage)) {
throw new BadRequest({
code: 2101,
lang: req.user?.languageCode
})
}else{
params['storage'] = storage
}
}
if (isSet(valuation)){
if(!isEmpty(valuation)) {
// throw new BadRequest({
// code: 2085,
// lang: req.user?.languageCode
// })
// }else{
params['valuation'] = valuation
}
}
if (isSet(weight_brutto)){
if(isEmpty(weight_brutto)) {
throw new BadRequest({
code: 2125,
lang: req.user?.languageCode
})
}else{
params['weight_brutto'] = weight_brutto
}
}
if (isSet(weight_tare)){
if(isEmpty(weight_tare)) {
throw new BadRequest({
code: 2126,
lang: req.user?.languageCode
})
}else{
params['weight_tare'] = weight_tare
}
}
if (isSet(items_weight)){
if(isEmpty(items_weight)) {
throw new BadRequest({
code: 2009,
lang: req.user?.languageCode
})
}else{
params['items_weight'] = items_weight
}
}
if (isSet(items_volume)){
if(isEmpty(items_volume)) {
throw new BadRequest({
code: 2010,
lang: req.user?.languageCode
})
}else{
params['items_volume'] = items_volume
}
}
if (isSet(items_count)){
if(isEmpty(items_count)) {
throw new BadRequest({
code: 2089,
lang: req.user?.languageCode
})
}else{
params['items_count'] = items_count
}
}
if (isSet(items_total)){
if(isEmpty(items_total)) {
throw new BadRequest({
code: 2090,
lang: req.user?.languageCode
})
}else{
params['items_total'] = items_total
}
}
if (isSet(bulky_count)){
if(isEmpty(bulky_count)) {
throw new BadRequest({
code: 2091,
lang: req.user?.languageCode
})
}else{
params['bulky_count'] = bulky_count
}
}
if (isSet(bulky_total)){
if(isEmpty(bulky_total)) {
throw new BadRequest({
code: 2092,
lang: req.user?.languageCode
})
}else{
params['bulky_total'] = bulky_total
}
}
if (isSet(extras_count)){
if(isEmpty(extras_count)) {
throw new BadRequest({
code: 2093,
lang: req.user?.languageCode
})
}else{
params['extras_count'] = extras_count
}
}
if (isSet(extras_total)){
if(isEmpty(extras_total)) {
throw new BadRequest({
code: 2094,
lang: req.user?.languageCode
})
}else{
params['extras_total'] = extras_total
}
}
if (isSet(packages_count)){
if(isEmpty(packages_count)) {
throw new BadRequest({
code: 2095,
lang: req.user?.languageCode
})
}else{
params['packages_count'] = packages_count
}
}
if (isSet(packages_total)){
if(isEmpty(packages_total)) {
throw new BadRequest({
code: 2096,
lang: req.user?.languageCode
})
}else{
params['packages_total'] = packages_total
}
}
if (isSet(fuel_percent)){
if(isEmpty(fuel_percent)) {
throw new BadRequest({
code: 2097,
lang: req.user?.languageCode
})
}else{
params['fuel_percent'] = fuel_percent
}
}
if (isSet(fuel_total)){
if(isEmpty(fuel_total)) {
throw new BadRequest({
code: 2098,
lang: req.user?.languageCode
})
}else{
params['fuel_total'] = fuel_total
}
}
if (isSet(edited_packages)){
if(isEmpty(edited_packages)) {
throw new BadRequest({
code: 2078,
lang: req.user?.languageCode
})
}else{
params['edited_packages'] = parseBool(edited_packages.toString())
}
}
if (isSet(custom_packages)){
if(isEmpty(custom_packages)) {
throw new BadRequest({
code: 2079,
lang: req.user?.languageCode
})
}else{
params['custom_packages'] = custom_packages
}
}
if (isSet(edited_weight)){
if(isEmpty(edited_weight)) {
throw new BadRequest({
code: 2070,
lang: req.user?.languageCode
})
}else{
params['edited_weight'] = parseBool(edited_weight.toString())
}
}
if (isSet(custom_weight)){
if(isEmpty(custom_weight)) {
throw new BadRequest({
code: 2071,
lang: req.user?.languageCode
})
}else{
params['custom_weight'] = custom_weight
}
}
if (isSet(edited_volume)){
if(isEmpty(edited_volume)) {
throw new BadRequest({
code: 2072,
lang: req.user?.languageCode
})
}else{
params['edited_volume'] = parseBool(edited_volume.toString())
}
}
if (isSet(custom_volume)){
if(isEmpty(custom_volume)) {
throw new BadRequest({
code: 2073,
lang: req.user?.languageCode
})
}else{
params['custom_volume'] = custom_volume
}
}
if (isSet(edited_total)){
if(isEmpty(edited_total)) {
throw new BadRequest({
code: 2061,
lang: req.user?.languageCode
})
}else{
params['edited_total'] = parseBool(edited_total.toString())
}
}
if (isSet(custom_total)){
if(isEmpty(custom_total)) {
throw new BadRequest({
code: 2062,
lang: req.user?.languageCode
})
}else{
params['custom_total'] = custom_total
}
}
if (isSet(discount_comment)){
params['discount_comment'] = discount_comment
}
if (isSet(discount_cash)){
if(isEmpty(discount_cash)) {
throw new BadRequest({
code: 2007,
lang: req.user?.languageCode
})
}else{
params['discount_cash'] = discount_cash
}
}
if (isSet(total)){
if(isEmpty(total)) {
throw new BadRequest({
code: 2007,
lang: req.user?.languageCode
})
}else{
params['total'] = total
}
}
if (isSet(paid)){
if(isEmpty(paid)) {
throw new BadRequest({
code: 2080,
lang: req.user?.languageCode
})
}else{
params['paid'] = paid
}
}
if (isSet(due)){
if(isEmpty(due)) {
throw new BadRequest({
code: 2099,
lang: req.user?.languageCode
})
}else{
params['due'] = due
}
}
const inventory = await db.orderInventory.create(params)
if (isSet(inventory)){
var changes = ''
if(order.orderInventoryId != inventory.id){
order.orderInventoryId = inventory.id
switch(inventory.type){
case INVENTORY_TYPE.ESTIMATE:
order.orderEstimateChargesId = inventory.id
break;
case INVENTORY_TYPE.NEW_ESTIMATE:
order.orderEstimateChargesId = inventory.id
break;
case INVENTORY_TYPE.REVICE:
order.orderReviceChargesId = inventory.id
break;
case INVENTORY_TYPE.DELIVERY:
order.orderDeliveryChargesId = inventory.id
break;
}
changes += 'inventory'
await order.save()
await db.orderHistory.create({
orderId: order.id,
userId: req.user.id || null,
status: order.status,
comment: 'Edit ' + changes,
createdAt: Date.now(),
updatedAt: Date.now(),
})
}
// items categories
if(lastInventoryItemsCategory != null){
for (let index = 0; index < lastInventoryItemsCategory.length; index++) {
const newId = uuid.v4()
if (lastInventoryItems != null) {
for (let index2 = 0; index2 < lastInventoryItems.length; index2++) {
if (lastInventoryItems[index2].orderItemsCategoryId == lastInventoryItemsCategory[index].id) {
lastInventoryItems[index2].orderItemsCategoryId = newId
}
}
}
lastInventoryItemsCategory[index].id = newId
lastInventoryItemsCategory[index].orderInventoryId = inventory.id
await db.inventoryItemsCategories.create(lastInventoryItemsCategory[index])
}
}
// items
if(lastInventoryItems != null){
for (let index = 0; index < lastInventoryItems.length; index++) {
lastInventoryItems[index].orderInventoryId = inventory.id
const lastItemId = lastInventoryItems[index].id
delete lastInventoryItems[index].id
const item = await db.inventoryItems.create(lastInventoryItems[index])
// condition
const lastInventoryItemsContitions = await db.inventoryItemsConditions.findAll({
where: { inventoryItemId: lastItemId },
raw: true,
attributes: { exclude: ['id'] }
})
if(lastInventoryItemsContitions != null){
for (let index = 0; index < lastInventoryItemsContitions.length; index++) {
lastInventoryItemsContitions[index].inventoryItemId = item.id
await db.inventoryItemsConditions.create(lastInventoryItemsContitions[index])
}
}
// dest condition
const lastInventoryItemsDestContitions = await db.inventoryItemsDestConditions.findAll({
where: { inventoryItemId: lastItemId },
raw: true,
attributes: { exclude: ['id'] }
})
if(lastInventoryItemsDestContitions != null){
for (let index = 0; index < lastInventoryItemsDestContitions.length; index++) {
lastInventoryItemsDestContitions[index].inventoryItemId = item.id
await db.inventoryItemsDestConditions.create(lastInventoryItemsDestContitions[index])
}
}
// package
const lastInventoryItemsPackages = await db.inventoryItemsPackages.findAll({
where: { inventoryItemId: lastItemId },
raw: true,
attributes: { exclude: ['id'] }
})
if(lastInventoryItemsPackages != null){
for (let index = 0; index < lastInventoryItemsPackages.length; index++) {
lastInventoryItemsPackages[index].inventoryItemId = item.id
await db.inventoryItemsPackages.create(lastInventoryItemsPackages[index])
}
}
}
}
if(lastInventoryExtras != null){
for (let index = 0; index < lastInventoryExtras.length; index++) {
lastInventoryExtras[index].orderInventoryId = inventory.id
await db.inventoryExtras.create(lastInventoryExtras[index])
}
}
// check order documents
if (lastDocuments!=null) {
for (let index = 0; index < lastDocuments.length; index++) {
var recreate = false
if (lastDocuments[index].status == 'signed') {
// load document
const document = await db.documents.findOne({
where: { id: lastDocuments[index].documentId }
})
if (document) {
try {
// FIND RECREATE ACTION
var pages = JSON.parse(document.objects)
for (var page in pages) {
var objects = JSON.parse(pages[page].objects)
for (var obj in objects) {
if (objects[obj].type == 'action') {
if (objects[obj].model == 'actionOnNewEstimate') {
if (objects[obj].key == 'actionReCreateThisDocument') {
if (inventory.type == INVENTORY_TYPE.ESTIMATE || inventory.type == INVENTORY_TYPE.NEW_ESTIMATE) {
recreate = true
}
}
}
}
}
}
} catch (e) {
// throw new NotFound({ code: 2119, lang: req.user?.languageCode })
}
}
}
const doc = await db.orderDocument.findOne({
where: { id: lastDocuments[index].id }
})
if (!doc) {
throw new NotFound({ lang: req.user?.languageCode })
}
if (recreate) {
var params = {
id: uuid.v4(),
orderId: doc.orderId,
inventoryId: inventory.id,
documentId: doc.documentId,
userId: doc.userId,
name: doc.name,
rootDocumentId: doc.rootDocumentId,
status: 'edit',
link: doc.link,
objects: ''
}
await db.orderDocument.create(params)
await db.orderHistory.create({
orderId: order.id,
userId: req.user.id || order.updateUserId,
status: order.status,
comment: 'Recreate document ' + doc.name
})
doc.status = 'revoked'
} else {
doc.inventoryId = inventory.id
}
doc.save()
}
}
}
await transaction.commit()
// transaction.afterCommit((t) => {
// })
transaction = null
await SERVICES.OrderService.OrderStatus(order, 'orderEdit')
const time = performance.now() - t1
return res.status(200).json({ status: 'success', id: inventory.id, time })
} catch (err) {
transaction?.rollback()
next(err)
throw new BadRequest({
code: 2000,
lang: req.user?.languageCode
})
}
}
}