Prototyped the barcode idea; Added a basic production system.
This commit is contained in:
130
imports/api/Label.js
Normal file
130
imports/api/Label.js
Normal file
@@ -0,0 +1,130 @@
|
||||
import { Meteor } from 'meteor/meteor';
|
||||
import { Mongo } from 'meteor/mongo';
|
||||
import { check } from 'meteor/check';
|
||||
import {SimpleSchema} from 'meteor/aldeed:simple-schema';
|
||||
|
||||
|
||||
if(Meteor.isServer) {
|
||||
const puppeteer = require('puppeteer');
|
||||
//let Future = Npm.require('fibers/future');
|
||||
//
|
||||
//async function printLabels(data, callback) {
|
||||
// let params = "";
|
||||
// let url = Meteor.absoluteUrl("/LabelPrint");
|
||||
//
|
||||
// params = Object.keys(data).map(function(k) {
|
||||
// return encodeURIComponent(k) + "=" + encodeURIComponent(data[k]);
|
||||
// }).join('&');
|
||||
//
|
||||
// url += "?" + params;
|
||||
//
|
||||
// const browser = await puppeteer.launch();
|
||||
// const page = await browser.newPage();
|
||||
// console.log("Going to: " + url);
|
||||
// await page.goto(url, {waitUntil: 'networkidle0'});
|
||||
// // By removing the `path` option, we will receive a `Buffer` from `page.pdf`.
|
||||
// const pdf = await page.pdf({ width: "6in", height: "4in"}); // format: "A4" //path: 'C:\\Users\\Grumpy\\label.pdf'
|
||||
//
|
||||
// await browser.close();
|
||||
// callback(null, pdf);
|
||||
//}
|
||||
|
||||
Meteor.methods({
|
||||
//printLabels: function(width, height, layout, title1, title2, ingredients, date) {
|
||||
// console.log("Loaded Label");
|
||||
// let future = new Future();
|
||||
//
|
||||
// let boundCallback = Meteor.bindEnvironment(function(err, res) {
|
||||
// if(err) {
|
||||
// future.throw(err);
|
||||
// }
|
||||
// else {
|
||||
// future.return(res);
|
||||
// }
|
||||
// });
|
||||
//
|
||||
// printLabels({width, height, layout, title1, title2, ingredients, date}, boundCallback);
|
||||
//
|
||||
// return future.wait();
|
||||
//}
|
||||
|
||||
async printLabels(width, height, layout, title1, title2, ingredients, date) {
|
||||
let data = {width, height, layout, title1, title2, ingredients, date};
|
||||
let params = "";
|
||||
let url = Meteor.absoluteUrl("/PrintLabel");
|
||||
|
||||
//Switch to the static page - for some reason the Meteor page is not loading correctly (it just appears blank).
|
||||
//url = Meteor.absoluteUrl("/LabelPrint.html");
|
||||
|
||||
params = Object.keys(data).map(function(k) {
|
||||
return encodeURIComponent(k) + "=" + encodeURIComponent(data[k]);
|
||||
}).join('&');
|
||||
|
||||
url += "?" + params;
|
||||
|
||||
const browser = await puppeteer.launch();
|
||||
const page = await browser.newPage();
|
||||
//url = Meteor.absoluteUrl("/StaticTest.html");
|
||||
//url = Meteor.absoluteUrl("/StaticTest.html");
|
||||
console.log("Going to: " + url);
|
||||
await page.goto(url, {waitUntil: 'networkidle0'}); //, {waitUntil: 'networkidle0'}
|
||||
const pdf = await page.pdf({width: '3in', height: '2in'}); //path: 'C:\\Users\\Grumpy\\label.pdf',
|
||||
//const pdf = await page.pdf({format: 'A4'});
|
||||
//await page.pdf({path: 'C:\\Users\\Grumpy\\label.pdf', width: '6in', height: '4in'});
|
||||
await browser.close();
|
||||
|
||||
return new Uint8Array(pdf, 0, pdf.length);
|
||||
}
|
||||
});
|
||||
|
||||
//Returns a JSON containing a denormalized list of products {product_id, measure_id, product_name, measure_name, price, }
|
||||
WebApp.connectHandlers.use("/labels/GetBarCodeData", (req, res, next) => {
|
||||
try {
|
||||
let barcodes = Meteor.collections.Barcodes.find({}, {fields: {_id: 0, barcodeId: 1, productAndMeasureId: 1}});
|
||||
let measures = Meteor.collections.Measures.find({}, {fields: {_id: 1, name: 1}, sort: {order: 1}}).fetch();
|
||||
//Note: Price data looks like this: {XZ5Z3CM49NDrJNADA /* MeasureID */: {price: 10.5, effectiveDate: ISODate("2017-01-12T13:14:18.876-08:00"), previousPrice: 9}, ...}
|
||||
//Measures is an array of MeasureIDs valid for this product.
|
||||
let products = Meteor.collections.Products.find({}, {fields: {_id: 1, name: 1, measures: 1, prices: 1}, sort: {order: 1}}).fetch();
|
||||
//let measuresById = measures.reduce((map, measure) => (map[measure._id] = measure), {});
|
||||
let measuresById = {};
|
||||
let barcodesByProductAndMeasureIds = {};
|
||||
let result = {};
|
||||
let today = new Date();
|
||||
|
||||
for(measure of measures) measuresById[measure._id] = measure;
|
||||
for(barcode of barcodes) barcodesByProductAndMeasureIds[barcode.productAndMeasureId] = barcode.barcodeId;
|
||||
//console.log(measuresById);
|
||||
|
||||
//for(let measureId of Object.keys(measuresById)) {
|
||||
// console.log(measureId + ":" + measuresById[measureId].name);
|
||||
//}
|
||||
|
||||
for(let product of products) {
|
||||
for(let measureId of product.measures) {
|
||||
let measureName = measuresById[measureId] ? measuresById[measureId].name : undefined;
|
||||
let priceData = product.prices ? product.prices[measureId] : undefined;
|
||||
let price = (priceData ? (priceData.effectiveDate && moment(priceData.effectiveDate).isAfter(today) ? priceData.previousPrice : priceData.price) : 0); //Get the price based on the effective date - whether we should use the new price or the old.
|
||||
let barcodeId = barcodesByProductAndMeasureIds[productId + " " + measureId];
|
||||
|
||||
//Ignore any product/measure combinations that don't have barcodes.
|
||||
if(barcodeId) {
|
||||
result[barcodeId] = {productId: product._id, measureId: measureId, productName: product.name, measureName, price};
|
||||
}
|
||||
|
||||
//TODO: Pass the product & measure data separately from the barcodes also to handle the missing barcode scenario. When a user types in a product name and picks a measure, we can record in the sale the product ID and measure ID, and we can lookup any pricing data.
|
||||
|
||||
//Log any errors so we can figure out what is going on.
|
||||
if(measureName === undefined) {
|
||||
//Note: We will pass a price of zero if the price is unknown. This should be fine for now.
|
||||
console.log(product._id + " " + product.name + " references a measure (" + measureId + ") which is not in the measures array.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
res.end(JSON.stringify(result), "JSON");
|
||||
} catch(err) {
|
||||
console.log(err);
|
||||
res.end();
|
||||
}
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user