Description
a new feature request. It will be called “Case Pack” meaning how many pcs come in one master box ??. We want to set this on the items, so if we mark the item as 2pcs. The customer has to order in even numbers of 2s. So they can only order 4, 10, 20 and etc. We won’t allow 3, 5, 21pcs. So if the customer tries to order 3pcs, the website should ask to update to 2 or 4.
Set the maximum quantity for the corresponding item here we consider a max quantity section for the item, the quantity needs to be updated to the latest number which will satisfy the condition.
The min quantity and max quantity of items also need to be considered in this situation.
Note: if the item included for customer max quantity then the quantity must less than the case pack number.
casePAckMaxQtySet: function(items) {
var self = this;
_.each(items, function(item) {
//console.log('itemitem', item);
items.customerMaxQty = new CustomerMaxQtyModel();
//console.log('self.customerMaxQty', self.customerMaxQty);
items.customerMaxQty.fetch({
data: {
internalid: item.get('internalid')
}
}).done(function() {
var maxQuantity = items.customerMaxQty.get("maxquantity");
if (maxQuantity)
item.set('maxquantity', Math.abs(maxQuantity));
else
item.set('maxquantity', null);
var caseitem = item.get('custitembtn_1227_jj_casepackqty'),
quantityavailable = item.get('quantityavailable')
if (caseitem) {
var setQty = maxQuantity ? Math.min(quantityavailable, maxQuantity) : quantityavailable;
//console.log('setQty', setQty)
var resultQty = self.powOf_N(caseitem, setQty);
console.log('resultQtyinitialiseget', resultQty)
item.set('maxquantity', Math.abs(resultQty));
}
});
});
return items;
},
powOf_N: function(K, N) {
var rem = N % K;
if (rem == 0)
return N;
else
return N - rem;
}
Notification sample code for Case Pack.
if (maxQtyAvailable !== null) {
var maxQuantity = maxQtyAvailable;
self.cart = LiveOrderModel.getInstance();
var currentQuantity = 0;
if (maxQuantity) {
//console.log('self.customerMaxQty', self.customerMaxQty)
item.set('CustomerMaxQty', maxQuantity)
var cartList = self.cart.get('lines');
//console.log('cartList', cartList)
var cartLine = cartList.filter(function(line) {
return item.get('internalid') == line.get('item').id;
});
if (cartLine.length > 0) {
_.each(cartLine, function(line) {
currentQuantity += line.get('quantity');
});
}
if (parseInt(maxQuantity) >= currentQuantity) {
item.set('CustomerMaxQty', Math.abs(maxQuantity - currentQuantity));
} else {
item.set('CustomerMaxQty', Math.abs(0));
}
} else if (maxQuantity == 0)
item.set('CustomerMaxQty', Math.abs(0));
//console.log('self.ite,,aaa', item.get('CustomerMaxQty'))
var CustomerMaxItemQty = item.get('CustomerMaxQty');
var maxQty = CustomerMaxItemQty;
var resultMinQty = (CustomerMaxItemQty !== '' && CustomerMaxItemQty >= 0) ? Math.min(quantityavailable, CustomerMaxItemQty) : quantityavailable
//console.log('self.ite,,aaa', resultMinQty)
/********Entry notification for case packed item**********/
var itemId = item.get('_id')
if (CasePackQty) {
$('#productQuantityPLP-msg-' + itemId).text("Quantity must be multiple of " + CasePackQty)
$('#productQuantityPLP-msg-' + itemId).css('display', 'inline-block');
$('#productQuantityPLP-msg-' + itemId).css('color', '#4a7f35');
$('#productQuantityPLP-msg-' + itemId).css('font-size', '11px');
}
/****EOD Entry notification*****/
var CurrentQty = item.get('quantityToBuy');
/******** Find the resulted qty to be updated**********/
var setQty = Math.min(CurrentQty, resultMinQty);
var resultQty = self.powOf_N(CasePackQty, setQty);
//console.log('resultQty', resultQty)
/******** EOD Find the resulted qty to be updated**********/
/********if the item have Customer quantity**********/
if (item.get('CustomerMaxQty') !== '' && item.get('CustomerMaxQty') >= 0) {
/********check if the item is Case Packed**********/
if (CasePackQty) {
/********check if the quantity entered is zero**********/
if ((CurrentQty !== 0)) {
/******** if the quantity is not statisfy the basic case pack condition **********/
if ((CurrentQty % CasePackQty !== 0)) {
//console.log('resultQty', resultQty)
item.set('quantityToBuy', resultQty);
item.set('quantityBought', resultQty);
var message = _('The Quantity must be the multiple of ' + CasePackQty).translate()
if (resultQty == 0 && CurrentQty >= quantityavailable) {
message = _.translate('Currenlty the item quantity not available for purchase ');
}
self.voilateCasepackItems.push(itemid_each)
self.showErrorByLineId(message, itemId);
} else { /******** if the quantity is statisfy the basic case pack condition check additional condition to set qty**********/
/********If the qty is greater than or equal to the Min resulted qty by compare to the customer qty and the quantityavailable**********/
if (CurrentQty <= resultMinQty) {
item.set('quantityToBuy', CurrentQty);
item.set('quantityBought', CurrentQty);
$('[name="quantity"]').val(CurrentQty)
var message = _.translate('Maximum Quantity Exceeded');
item.set('quantity', CurrentQty);
} else {
item.set('quantityToBuy', resultQty);
item.set('quantityBought', resultQty);
$('[name="quantity"]').val(resultQty);
item.set('quantity', resultQty);
if (resultQty != 0) {
//console.log('test23')
var message = _.translate('Maximum Quantity Exceeded');
self.showErrorByLineId(message, itemId);
} else {
//console.log('test24')
var message = _.translate('Currenlty the item quantity not available for purchase ');
self.showErrorByLineId(message, itemId);
}
//self.voilateCasepackItems.push(itemid_each)
}
}
}
} else {
if (CurrentQty > CustomerMaxItemQty) {
item.set('quantityToBuy', maxQty);
item.set('quantityBought', maxQty);
//self._render();
var message = _.translate('Maximum Quantity Exceeded');
self.showErrorByLineId(message, itemId);
//self.voilateCasepackItems.push(itemid_each)
}
}
} else if (CasePackQty) {
var CurrentQty = self.model.get('quantityToBuy');
/********check if the quantity entered is zero**********/
if ((CurrentQty !== 0)) {
/******** if the quantity is not statisfy the basic case pack condition **********/
if ((CurrentQty % CasePackQty !== 0)) {
console.log('resultQty', resultQty)
item.set('quantityToBuy', resultQty);
item.set('quantityBought', resultQty);
var message = _('The Quantity must be the multiple of ' + CasePackQty).translate()
if (resultQty == 0 && CurrentQty >= quantityavailable) {
message = _.translate('Currenlty the item quantity not available for purchase ');
}
self.showErrorByLineId(message, itemId);
} else { /******** if the quantity is statisfy the basic case pack condition check additional condition to set qty**********/
/********If the qty is greater than or equal to the Min resulted qty **********/
if (CurrentQty <= resultMinQty) {
item.set('quantityToBuy', CurrentQty);
item.set('quantityBought', CurrentQty);
$('[name="quantity"]').val(CurrentQty)
var message = _.translate('Maximum Quantity Exceeded');
item.set('quantity', CurrentQty);
} else {
item.set('quantityToBuy', resultQty);
item.set('quantityBought', resultQty);
$('[name="quantity"]').val(resultQty);
item.set('quantity', resultQty);
if (resultQty != 0) {
// console.log('test23')
var message = _.translate('Maximum Quantity Exceeded');
self.showErrorByLineId(message, itemId);
} else {
// console.log('test24')
var message = _.translate('Currenlty the item quantity not available for purchase ');
self.showErrorByLineId(message, itemId);
}
}
}
}
}
}
Check the case Pack and get the case pack quantity
searchCasePack: function searchCasePack(itemIds) {
//console.log('itemIdssearchCasePack', itemIds);
var itemIdsArray = [];
var casePackItemSearch = {};
var ItemSearchArr_set = [];
if (itemIds) {
itemIdsArray = itemIds.split(',');
console.log('itemIdssearchsplititemIdssearchCasePack', itemIds);
}
if (itemIdsArray.length > 0) {
var start = 0;
try {
do {
var RequestUrl = DomainName+"/api/items?offset=" +start + "&limit=50&id=" + itemIds + "&fields=custitembtn_1227_jj_casepackqty"
var itemCasePackData = nlapiRequestURL(RequestUrl, null, { 'Accept': 'application/json' }, "GET");
if (itemCasePackData) {
// if response has body.
if ((itemCasePackData.getBody() && (itemCasePackData.getCode() == 200))) {
var ItemSearchArr = (JSON.parse(JSON.stringify(itemCasePackData.getBody())));
var codeCheck = (JSON.parse(ItemSearchArr)).code;
if (codeCheck == 200) {
var itemObj = (JSON.parse(ItemSearchArr)).items;
_.each(itemObj, function(items) {
ItemSearchArr_set.push(items);
});
}
}
}
start += 50;
} while (start < itemIdsArray.length);
if (ItemSearchArr_set.length > 0) {
var casePAckByItemId = {};
_.each(ItemSearchArr_set, function(itemId) {
if (itemId.custitembtn_1227_jj_casepackqty) {
casePAckByItemId[itemId.internalid] = itemId.custitembtn_1227_jj_casepackqty;
}
});
return casePAckByItemId;
}
} catch (err) {
console.log('err', err)
}
}
return casePackItemSearch;
}