Skip to content

Instantly share code, notes, and snippets.

@seksitha
Created November 22, 2019 01:53
Show Gist options
  • Save seksitha/19ef401bfd84695b8d4f823f2362314f to your computer and use it in GitHub Desktop.
Save seksitha/19ef401bfd84695b8d4f823f2362314f to your computer and use it in GitHub Desktop.
addDetail: (values, _, props) => {
//this syntax need to comply with redux form submit
console.log({ values });
const { saleType, saleDetail, products } = props;
if (!values.barcode) {
window.aLert("no barcode");
return { type: null };
}
// if you put only 3 digits nothing will match
// group capture with use question mark at the end capture to make it optional otherwise
// prettier-ignore
const barcodeMatchArray = values.barcode.match(/(\d{2})?(\d{4})?[\d]?(\d{6})?/) || [];
function setOpenAmountDailogState(barcodeFirst2Digit, qty) {
if (barcodeFirst2Digit === "22" && !qty) return true;
}
function getQtyBybarcode(barcodeArray, qty) {
if (barcodeArray[1] === "21") {
return 1;
} //
else if (barcodeArray[1] === "20") {
// need to read code to calculate the qty
if (qty) return qty;
if (barcodeArray[3]) {
const qtyMatch = barcodeArray[3].match(/(\d{2})(\d{4})/);
return parseFloat(qtyMatch[1] + "." + qtyMatch[2]);
}
return 0;
} //
else if (barcodeArray[1] == "22") {
return qty ? qty : 0; // need to open dailog to put qty
}else{
return 1;
}
}
// filter detail to check if there is already product in the list with found.
function getPayload(detailFound, foundInProductList) {
const qty = getQtyBybarcode(barcodeMatchArray, values.qty);
if (detailFound) {
const {
name,
name_eng,
thumbnail,
sub_total,
unit,
margin,
retail_price,
newSalePrice,
...rest // use this only!!!
} = detailFound;
return {
dataToPost: {
...rest, // got use here!!!
product_id: foundInProductList.id,
// prettier-ignore
price: values.updatePriceByPurchase ? values.price : foundInProductList[ `${saleType === "delivery" ? "wholesale" : saleType}_price`] * ((100 - (parseFloat(values.priceDiscount, 10) || 0)) / 100),
discount: parseFloat(values.priceDiscount) || 0,
// prettier-ignore
qty: parseFloat(detailFound.qty) + ((values.statusDiscountUpdate || values.updatePriceByPurchase) ? 0 : parseFloat(values.qty || qty))
},
url: "/api/sale-detail",
typeOnSuccess: "$$model-update_sale_detail_success",
};
} else {
return {
dataToPost: {
main_id: props.sale.id,
product_id: foundInProductList.id,
qty: parseFloat(values.qty) || qty,
// prettier-ignore
price: foundInProductList[ `${saleType === "delivery" ? "wholesale" : saleType}_price`] *((100 - (parseFloat(values.priceDiscount, 10) || 0)) / 100),
discount: parseFloat(values.priceDiscount) || 0,
},
url: "/api/sale-detail",
typeOnSuccess: "$$model-add_sale_detail_success",
};
}
}
function setActionType(details, dailogAmountState) {
if (dailogAmountState && details) {
return "$$mw-UPDATE_SALE_DETAIL_WITH_DIALOG";
} else if (dailogAmountState && !details) {
return "$$mw-ADD_SALE_DETAIL_WITH_DIALOG";
} else if (!dailogAmountState && details) {
return "$$api-UPDATE_DATA_ASYNC";
} else if (!dailogAmountState && !details) {
return "$$api-ADD_DATA_ASYNC";
}
}
function searchProduct(barcodeArray, products) {
if (!barcodeArray[2]) return undefined;
return products.find(obj => {
const codematch = (obj.code || "000000").match(/(\d{2})(\d{4})?/);
return codematch[2] === barcodeArray[2];
});
}
const dialogMountState = setOpenAmountDailogState(
barcodeMatchArray[1],
values.qty
);
const findProductFromBarcodeInput = searchProduct(
barcodeMatchArray,
products
);
console.log(findProductFromBarcodeInput);
// prettier-ignore
if (findProductFromBarcodeInput && getQtyBybarcode(barcodeMatchArray,values.qty)) {
const detailFound = saleDetail.find(
detail => detail.name === findProductFromBarcodeInput.name
);
return {
type: setActionType(detailFound, dialogMountState),
payload: getPayload(detailFound, searchProduct(barcodeMatchArray,products)),
meta: saleType,
};
} else{
return {
type: "$$wm-SEARCH_PRODUCT",
payload: values.barcode,
}
}
},
addDetail: (values, _, props) => {
//this syntax need to comply with redux form submit
// console.log({values, props, _})
if (!values.barcode) {
window.aLert("no barcode");
return { type: null };
}
// group capture with use question mark at the end capture to make it optional otherwise
// if you put only 3 digits nothing will match
const barcodeSpliter =
values.barcode.match(/(\d{2})?(\d{4})?[\d]?(\d{6})?/) || [];
console.log(barcodeSpliter, barcodeSpliter[2], barcodeSpliter[3]);
const { saleType, saleDetail } = props;
let qty = 0;
let barcodeCondition = 0; // we used the first 7 digit code only
// @@@@@@@@
function setOpenAmountDailogState(barcodeFirst2Digit){
if(barcodeFirst2Digit==='22') return true;
}
function setQtyBybarcode (barcodeFirst2Digit){
}
if (barcodeSpliter[1] === "21") {
qty = 1;
} //
else if (barcodeSpliter[1] === "20") {
// need to read code to calculate the qty
if (!barcodeSpliter[3]) {
qty = 0;
} else {
const qtyMatch = barcodeSpliter[3].match(/(\d{2})(\d{4})/);
qty = parseFloat(qtyMatch[1] + "." + qtyMatch[2]);
}
} //
else if (barcodeSpliter[1] == "22") {
qty = 0; // need to open dailog to put qty
} //
else {
qty = 1;
barcodeCondition = 1; // we used the hold digit for product outside code.
}
//&&&&&&&&&
if (values.qty) {
qty = typeof values.qyt === "number" ? parseFloat(values.qty) : 1;
}
const foundInProductList = props.products.find(product => {
if (barcodeCondition === 1) {
return values.barcode === product.code; //used whole code
}
if (values.barcode.length >= 7) {
return (
barcodeSpliter[1] + barcodeSpliter[2] ===
(product.code ? product.code.substr(0, 7) : product.code)
); //used only 7 digit code
}
});
// console.log(foundInProductList);
if (!foundInProductList) {
return {
type: "$$wm-SEARCH_PRODUCT",
payload: values.barcode,
};
}
// if the qty is set we dont show dialog box for 202, 200
const getActionType = details => {
if (qty === 0) {
return details
? "$$mw-UPDATE_SALE_DETAIL_WITH_DIALOG"
: "$$mw-ADD_SALE_DETAIL_WITH_DIALOG";
} //
else {
return `${details ? "$$api-UPDATE" : "$$api-ADD"}_DATA_ASYNC`;
}
};
// filter detail to check if there is already product in the list with found.
const detailFound = saleDetail.find(
detail => detail.name === foundInProductList.name
);
const getPayload = (detailFound, foundInProductList) => {
if (!detailFound) {
return {
dataToPost: {
main_id: props.sale.id,
product_id: foundInProductList.id,
qty: parseFloat(values.qty) || qty,
price:
foundInProductList[
`${saleType === "delivery" ? "wholesale" : saleType}_price`
] *
((100 - (parseFloat(values.priceDiscount, 10) || 0)) / 100),
discount: parseFloat(values.priceDiscount) || 0,
},
url: "/api/sale-detail",
typeOnSuccess: "$$model-add_sale_detail_success",
};
}
const {
name,
name_eng,
thumbnail,
sub_total,
unit,
margin,
retail_price,
newSalePrice,
...rest
} = detailFound;
return {
dataToPost: {
...rest,
// prettier-ignore
product_id: foundInProductList.id,
price: values.updatePriceByPurchase
? values.price
: foundInProductList[
`${saleType === "delivery" ? "wholesale" : saleType}_price`
] *
((100 - (parseFloat(values.priceDiscount, 10) || 0)) / 100),
discount: parseFloat(values.priceDiscount) || 0,
// prettier-ignore
qty: parseFloat(detailFound.qty) + ((values.statusDiscountUpdate || values.updatePriceByPurchase) ? 0 : parseFloat(values.qty || qty))
},
url: "/api/sale-detail",
typeOnSuccess: "$$model-update_sale_detail_success",
};
};
return {
type: getActionType(detailFound),
payload: getPayload(detailFound, foundInProductList),
meta: saleType,
};
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment