Copy Menu Category to another Menu

Hello

Is there any easy way to copy a Menu Category to another Menu? In the case below I am building a nice "Options Category: that are applicable to most Departments which have there own Menu.

Illustration:

So need to Copy Options Category to Kitchen Menu:

Since the some Menus are already done I do not wish Clone Bar, Delete all Categories not wanted for the Other Menus and then Rebuild other Menus around it. Seems like a long way around…

I put a script to do that somewhere :wink:

If you dig it out of your posted topics that would be great (as long as its safe :sweat_smile:). I did search but must of missed your post?

Can you search the forum by Poster - I really could of used quite a few times as I imagine your Topics would be LARGE!

function cloneCategoryToMenu(categoryName,fromMenuName,toMenuName) {
	if (fromMenuName == toMenuName) {
		var response					= 'Category can only be cloned to diferent menu with this function'; 
		} else {
		var fromMenuId					= menuNameId(fromMenuName);
		var fromCategoryId				= categoryNameId(categoryName,fromMenuId);
		var toMenuId					= menuNameId(toMenuName);
		var toCategoryExists			= checkCategoryNameExists(categoryName,toMenuId);
		if (toCategoryExists > 0) {
			var toCategoryId			= categoryNameId(categoryName,toMenuId);
			delectCategoryItems(toCategoryId)
			delectCategory(toCategoryId)
			}
		var fromCategoryValues			= categoryDetailsList(fromCategoryId);
		insertCategoryRow(fromCategoryValues,toMenuId)
		var toCategoryId				= categoryNameId(categoryName,toMenuId);
		var fromCategoryItemCount		= categoryItemsCount(fromCategoryId);
		var fromCategoryItemIdList		= categoryItemsIdList(fromCategoryId)
		for (r = 0; r < fromCategoryItemCount ; r++) {
			var loopItemId				= fromCategoryItemIdList[r];
			var loopItemRowValues		= categoryItemRowList(loopItemId);
			var loopInsert				= insertItemRow(loopItemRowValues,toCategoryId)
			}
		var response					= 'Category: '+categoryName+' [ID:'+fromCategoryId+'] has been cloned from Menu: '+fromMenuName+' [ID: '+fromMenuId+'] to Menu: '+toMenuName+' [ID: '+toMenuId+'] as Category: '+categoryName+' [ID: '+toCategoryId+']';
		}
	return response
}

function menuNameId(inputMenuName) {
	var qry = "SELECT [Id] FROM [ScreenMenus] WHERE [Name] = '"+inputMenuName+"'";
	var menuId = sql.Query(qry).First;
	return menuId
}

function categoryNameId(inputCategoryName,inputMenuId) {
	var qry = "SELECT [Id] FROM [ScreenMenuCategories] WHERE [Name] = '"+inputCategoryName+"' AND [ScreenMenuId] = '"+inputMenuId+"'";
	var categoryId = sql.Query(qry).First;
	return categoryId
}

function checkCategoryNameExists(inputCategoryName,inputMenuId) {
	var qry = "SELECT COUNT([Id]) AS CT FROM [ScreenMenuCategories] WHERE [Name] = '"+inputCategoryName+"' AND [ScreenMenuId] = '"+inputMenuId+"'";
	var categoryCount = sql.Query(qry).First;
	return categoryCount
}

function categoryItemsCount(inputCategoryId) {
	var qry = "SELECT COUNT([Id]) AS CT FROM [ScreenMenuItems] WHERE [ScreenMenuCategoryId] = '"+inputCategoryId+"'";
	var categoryItemCount = sql.Query(qry).First;
	return categoryItemCount
}

function categoryItemsIdList(inputCategoryId) {
	var qry = "SELECT [Id] FROM [ScreenMenuItems] WHERE [ScreenMenuCategoryId] = '"+inputCategoryId+"'";
	var categoryItemsIdList = sql.Query(qry).Delimit('~').All;
	return categoryItemsIdList
}

function categoryItemRowList(categoryItemId) {
	var qry = "SELECT [Name],[Header],[Appearance],[MenuItemId],[SortOrder],[AutoSelect],[ButtonColor],[Quantity],[ImagePath],[FontSize],[SubMenuTag],[ItemPortion],[OrderTags],[OrderStates],[AutomationCommand],[AutomationCommandValue] ";
	qry += "FROM [ScreenMenuItems] WHERE [Id] = '"+categoryItemId+"'";
	var categoryItemRow = sql.Query(qry).Delimit('~').First;
	var rowarray	= categoryItemRow.split('~');
	var rowcount	= rowarray.length;
	var rowValuesList	= '';
	for (i = 0; i < rowcount ; i++) {
		if (!rowarray[i] && i != 10) {
			rowValuesList += "NULL,";
			} else {
			rowValuesList += "'"+rowarray[i]+"',";
			}
		}
	var responce = rowValuesList.slice(0,-1)
	return responce
}

function insertItemRow(inputValues,inputScreenCategoryId) {
	var qry = "INSERT INTO [ScreenMenuItems]";
	qry += "([Name],[Header],[Appearance],[MenuItemId],[SortOrder],[AutoSelect],[ButtonColor],[Quantity],[ImagePath],[FontSize],[SubMenuTag],[ItemPortion],[OrderTags],[OrderStates],[AutomationCommand],[AutomationCommandValue],[ScreenMenuCategoryId])";
	qry += "VALUES ("+inputValues+",'"+inputScreenCategoryId+"')";
	sql.ExecSql(qry)
}

function insertCategoryRow(inputValues,inputScreenMenuId) {
	var qry = "INSERT INTO [ScreenMenuCategories]";
	qry += "([Name],[Header],[Appearance],[SortOrder],[MostUsedItemsCategory],[ColumnCount],[MenuItemButtonHeight],[MenuItemButtonColor],[MenuItemFontSize],[WrapText],[PageCount],[SortAlphabetically],[MainButtonHeight],[MainButtonColor],[MainFontSize],[SubButtonHeight],[SubButtonRows],[SubButtonColorDef],[NumeratorType],[NumeratorValues],[AlphaButtonValues],[ImagePath],[NumberPadPercent],[MaxItems],[ScreenMenuId])";
	qry += "VALUES ("+inputValues+",'"+inputScreenMenuId+"')";
	sql.ExecSql(qry)
}

function delectCategoryItems(inputScreenCategory) {
	var qry = "DELETE FROM [ScreenMenuItems] WHERE [ScreenMenuCategoryId] = '"+inputScreenCategory+"'";
	sql.ExecSql(qry)
}

function delectCategory(inputScreenCategoryId) {
	var qry = "DELETE FROM [ScreenMenuCategories] WHERE [Id] = '"+inputScreenCategoryId+"'";
	sql.ExecSql(qry)
}

function categoryDetailsList(categoryId) {
	var qry = "SELECT [Name],[Header],[Appearance],[SortOrder],[MostUsedItemsCategory],[ColumnCount],[MenuItemButtonHeight],[MenuItemButtonColor],[MenuItemFontSize],[WrapText],[PageCount],[SortAlphabetically],[MainButtonHeight],[MainButtonColor],[MainFontSize],[SubButtonHeight],[SubButtonRows],[SubButtonColorDef],[NumeratorType],[NumeratorValues],[AlphaButtonValues],[ImagePath],[NumberPadPercent],[MaxItems] ";
	qry += "FROM [ScreenMenuCategories] WHERE [Id] = '"+categoryId+"'";
	var categoryItemRow = sql.Query(qry).Delimit('~').First;
	var rowarray	= categoryItemRow.split('~');
	var rowcount	= rowarray.length;
	var rowValuesList	= '';
	for (i = 0; i < rowcount ; i++) {
		if (!rowarray[i] && i != 17) {
			rowValuesList += "NULL,";
			} else {
			rowValuesList += "'"+rowarray[i]+"',";
			}
		}
	var responce = rowValuesList.slice(0,-1)
	return responce
}

Use with caution, not tested a huge about but seemed to do as expected. It obviously does allot of SQL changes BACKUP first :wink:

1 Like

Gees @JTRTech are you working fast enough!!! - So what does this do and how do I drive it…

Ok you call this from within SambaPOS and supply Parameters.
We calling with just an execute Script Automation Command.

Anything else?

All the SQL is in the script.

cloneCategoryToMenu(categoryName,fromMenuName,toMenuName)

The function I think is fairly self explanatory: categoryName, fromMenuName and toMenuName

You dont even need to do that.
Can just use the test field on the script page. :slight_smile:

:scream: Just a bit too easy…naaaar think I will do it the long way :laughing:

@pauln few notes I can remember…

If category already exists it will delete and replace (was meant to be a method to update as well.
Uses names, no need to look up IDs it does that itself.

Cool thanks JTRTech - if you think of anything else drop it back in notes.

Think I will hit the sack as if I break anything now at 11.36pm I will be very unhappy with myself. Best have a crack tomorrow - good shout JTR.

Will defiantly want to test, encase any version changes have effected like new column or something, put this together a while back now.

1 Like