import './Sales.html'; import '/imports/util/selectize/selectize.js' import ResizeSensor from '/imports/util/resize/ResizeSensor.js'; Tracker.autorun(function() { Meteor.subscribe("products"); Meteor.subscribe("sales", Session.get('searchQuery')); }); Template.Sales.onRendered(function() { // console.log("moving headers"); // try { // //Move the headers into the header table that will maintain its position. // //Link the column widths to the header widths. // let newHeaderRow = this.$('.dataTableHeader thead tr:first'); // let newFooterRow = this.$('.dataTableFooter thead tr:first'); // let oldHeaders = this.$('.dataTable thead tr.headers th'); // let oldFooters = this.$('.dataTable thead tr.footers th'); // // console.log("header count " + oldHeaders.length); // // for(let index = 0; index < oldHeaders.length; index++) { // let width = this.$('.dataTable tbody tr:first td:eq(' + index + ')').width(); // let oldHeader = oldHeaders.eq(index); // let newHeader = $(""); // oldHeader.replaceWith(newHeader); // newHeader.width(width); // oldHeader.appendTo(newHeaderRow); // //Link the two headers so that the visible header changes size with the hidden one. // //TODO: Turn this off if manually resizing the visible headers - while resizing. // new ResizeSensor(newHeader, function() { // oldHeader.width(newHeader.width()); // }); // } // // for(let index = 0; index < oldFooters.length; index++) { // let width = this.$('.dataTable tbody tr:first td:eq(' + index + ')').width(); // let oldFooter = oldFooters.eq(index); // let newFooter = $(""); // oldFooter.replaceWith(newFooter); // newFooter.width(width); // oldFooter.appendTo(newFooterRow); // //Link the two headers so that the visible header changes size with the hidden one. // //TODO: Turn this off if manually resizing the visible headers - while resizing. // new ResizeSensor(newFooter, function() { // oldFooter.width(newFooter.width()); // }); // } // } // catch(err) { // console.log(err); // } }); Template.Sales.helpers({ sales: function() { return Meteor.collections.Sales.find({}, {sort: {date: -1}}); } }); Template.Sale.onCreated(function() { }); Template.Sale.events({ "click .saleRemove": function(event, template) { let _this = this; bootbox.confirm({ message: "Delete the sale?", buttons: {confirm: {label: "Yes", className: 'btn-success'}, cancel: {label: "No", className: "btn-danger"}}, callback: function(result) { if(result) { // Meteor.collections.Sales.remove(_this._id); Meteor.call('deleteSale', _this._id); } } }); } }); Template.Sale.helpers({ measureName: function(id) { return Meteor.collections.Measures.findOne({_id: id}, {fields: {name: 1}}).name; }, venueName: function(id) { return Meteor.collections.Venues.findOne({_id: id}, {fields: {name: 1}}).name; }, productName: function(id) { return Meteor.collections.Products.findOne({_id: id}, {fields: {name: 1}}).name; }, formatDate: function(date) { return moment(date).format("MM/DD/YYYY (w)"); }, formatPrice: function(price) { return price.toLocaleString("en-US", {style: 'currency', currency: 'USD', minimumFractionDigits: 2}); }, formatTotalPrice: function(price, amount) { return (price * amount).toLocaleString("en-US", {style: 'currency', currency: 'USD', minimumFractionDigits: 2}); }, showTotalPrice: function(amount) { return amount > 1; } }); Template.SaleSearch.events({ "keyup .searchInput": _.throttle(function(event, template) { let searchQuery = Session.get('searchQuery') || {}; let searchFields = Session.get('searchFields') || {}; let searchValue = template.$('.searchInput').val(); if(searchValue) { if(this.number) searchValue = parseFloat(searchValue); if(this.collection) { let ids = Meteor.collections[this.collection].find({[this.collectionQueryColumnName]: {$regex: searchValue, $options: 'i'}}, {fields: {[this.collectionResultColumnName]: 1}}).fetch(); //Convert the ids to an array of ids instead of an array of objects containing an id. for(let i = 0; i < ids.length; i++) {ids[i] = ids[i]._id;} searchQuery[this.columnName] = {$in: ids}; searchFields[this.columnName] = searchValue; } else { searchFields[this.columnName] = searchQuery[this.columnName] = searchValue; } } else { //Remove columns from the search query whose values are empty so we don't bother the database with them. delete searchQuery[this.columnName]; delete searchFields[this.columnName]; } Session.set('searchQuery', searchQuery); }, 500) }); Template.SaleSearch.helpers({ searchValue: function() { let searchFields = Session.get('searchFields'); return (searchFields && searchFields[this.columnName]) ? searchFields[this.columnName] : ''; } }); // let SelectedProduct = new ReactiveVar(); Template.InsertSale.onCreated(function() { // $('#insertSale').validator(); // $('#insertSale').data('bs.validator'); // this.products = new ReactiveVar([]); this.selectedProduct = new ReactiveVar(); this.selectedVenue = new ReactiveVar(); }); Template.InsertSale.onRendered(function() { $('#insertSale').validator(); // this.$('[name="product"]'). // this.autorun(function() { // this.$('[name="product"]').buildCombo(Meteor.collections.Products.find({}).fetch(), {textAttr: 'name', listClass: 'comboList'}); // }); this.$('[name="product"]').buildCombo({cursor: Meteor.collections.Products.find({}), selection: this.selectedProduct, textAttr: 'name', listClass: 'comboList'}); this.$('[name="venue"]').buildCombo({cursor: Meteor.collections.Venues.find({}), selection: this.selectedVenue, textAttr: 'name', listClass: 'comboList'}); // this.autorun(function(){ // this.products.set(Meteor.collections.Products.find({})); // }.bind(this)); }); Template.InsertSale.events({ 'change #InsertSaleProduct': function(event, template) { let selectedId = $('#InsertSaleProduct').val(); let selected = Meteor.collections.Products.findOne(selectedId); template.selectedProduct.set(selected); }, 'click input[type="submit"]': function(event, template) { event.preventDefault(); $('#insertSale').data('bs.validator').validate(function(isValid) { if(isValid) { let sales = []; let sale = { date: moment(template.find("[name='date']").value, "YYYY-MM-DD").toDate(), productId: template.selectedProduct.get()._id, venueId: template.selectedVenue.get()._id }; let insertSaleMeasures = template.$(".insertSaleMeasure"); for(let next = 0; next < insertSaleMeasures.length; next++) { let nextMeasure = $(insertSaleMeasures[next]); let measureId = nextMeasure.find(".measureId").val(); let price = parseFloat(nextMeasure.find(".price").val()).toFixed(2); let amount = parseFloat(nextMeasure.find(".amount").val()).toFixed(2); if(amount > 0) { let nextSale = _.clone(sale); nextSale.measureId = measureId; nextSale.price = price; nextSale.amount = amount; sales.push(nextSale); } } // let debug = "Inserting: "; // for(next in sales) { // debug += "\n\t" + next; // } // console.log(debug); for(let index = 0; index < sales.length; index++) { let next = sales[index]; console.log("Inserting: " + JSON.stringify(next)); // Meteor.collections.Sales.insert(next, function(err, id) { // if(err) console.log(err); // }); Meteor.call('insertSale', next); } } }); } }); Template.InsertSale.helpers({ // sales: function() { // return Meteor.collections.Sales; // }, products: function() { //return Meteor.collections.Products.find({}); //return this.products; return [{label: "Hermies", value: 1}, {label: "Ralfe", value: 2}, {label: "Bob", value: 3}]; }, productMeasures: function() { let product = Template.instance().selectedProduct.get(); let result = product ? product.measures : []; for(let i = 0; i < result.length; i++) { result[i] = Meteor.collections.Measures.findOne(result[i]); } if(product) console.log("Found " + result.length + " measures for the product " + product.name); else console.log("No product!"); return result; }, venues: function() { return Meteor.collections.Venues.find({}); } }); Template.InsertSaleMeasure.onCreated(function() { let prices = this.parentTemplate().selectedProduct.get().prices; let price = 0; if(prices) price = prices[this._id]; this.price = new ReactiveVar(price); this.amount = new ReactiveVar(0); }); Template.InsertSaleMeasure.events({ 'change .price': function(event, template) { template.price.set(parseFloat($(event.target).val()).toFixed(2)); }, 'change .amount': function(event, template) { template.amount.set(parseFloat($(event.target).val()).toFixed(2)); } }); Template.InsertSaleMeasure.helpers({ price: function() { return Template.instance().price.get(); }, total: function() { let template = Template.instance(); return template.price.get() * template.amount.get(); }, amount: function() { return Template.instance().amount.get(); } });