2017-01-15 11:33:37 -08:00
import './Pricing.html' ;
2017-05-26 11:17:32 -07:00
/ * *
* Notes :
* The Product object has a prices field which is an object whose fields names are Measure ID 's. Each field value (for each Measure ID) is an object that has a ' price ', ' effectiveDate ', and ' previousPrice ' .
* The effectiveDate field stores the date as a number in the format YYYYMMDD . Converting this number into a local date is done with moment ( sale . date . toString ( ) , "YYYYMMDD" ) . toDate ( ) , and converting it to a number from a date can be accomplished with ~ ~ ( moment ( date ) . format ( "YYYYMMDD" ) ) , where the ~ ~ is a bitwise not and converts a string to a number quickly and reliably .
* Because the structure of the Product object is so complicated , the normal checking that is done by the framework cannot be used .
* /
2017-01-17 22:31:43 -08:00
let QUERY _LIMIT = 20 ;
let PREFIX = "Pricing." ;
Meteor . subscribe ( "products" ) ;
let measuresSubscription = Meteor . subscribe ( "measures" ) ;
2017-01-15 11:33:37 -08:00
Tracker . autorun ( function ( ) {
2017-01-17 22:31:43 -08:00
let ready = measuresSubscription . ready ( ) ;
if ( ready ) {
console . log ( "setting selected measure session var" ) ;
let firstMeasure = Meteor . collections . Measures . findOne ( { } , { sort : { order : 1 } , fields : { _id : 1 } } ) ;
Session . set ( PREFIX + "selectedMeasure" , firstMeasure . _id ) ;
}
2017-01-15 11:33:37 -08:00
} ) ;
2017-01-17 22:31:43 -08:00
Template . Pricing . onCreated ( function ( ) {
} ) ;
2017-01-15 11:33:37 -08:00
Template . Pricing . onRendered ( function ( ) {
this . $ ( 'input[name="date"]' ) . val ( new Date ( ) . toDateInputValue ( ) ) ;
2017-01-17 22:31:43 -08:00
// this>$('select[name="measures"]').val()
2017-01-15 11:33:37 -08:00
} ) ;
Template . Pricing . helpers ( {
measures : function ( ) {
let measures = Meteor . collections . Measures . find ( { } , { sort : { order : 1 } } ) . fetch ( ) ;
for ( let i = 0 ; i < measures ; i ++ ) {
if ( Meteor . collections . Products . find ( { measures : { $all : [ measures [ i ] . _id ] } } , { sort : { name : 1 } } ) . count ( ) == 0 )
measures . splice ( i , 1 ) ; //Remove the measure from the list.
}
return measures ;
} ,
product : function ( ) {
2017-01-17 22:31:43 -08:00
let skipCount = Session . get ( PREFIX + 'skipCount' ) || 0 ;
let measureId = Session . get ( PREFIX + "selectedMeasure" ) ;
let dbQuery = { measures : { $all : [ measureId ] } , $or : [ { hidden : false } , { hidden : { $exists : false } } ] } ;
2017-01-15 11:33:37 -08:00
2017-01-17 22:31:43 -08:00
Session . set ( PREFIX + 'productCount' , Meteor . collections . Products . find ( dbQuery ) . count ( ) ) ; //Always get a full count.
return Meteor . collections . Products . find ( dbQuery , { limit : QUERY _LIMIT , skip : skipCount , sort : { name : 1 } } ) ;
} ,
disablePrev : function ( ) {
return ( Session . get ( PREFIX + 'skipCount' ) || 0 ) == 0 ;
} ,
disableNext : function ( ) {
return Session . get ( PREFIX + 'productCount' ) - ( Session . get ( PREFIX + 'skipCount' ) || 0 ) - QUERY _LIMIT <= 0 ;
2017-01-15 11:33:37 -08:00
}
} ) ;
Template . Pricing . events ( {
'change select[name="measures"]' : function ( event , template ) {
2017-01-17 22:31:43 -08:00
Session . get ( PREFIX + 'skipCount' , 0 ) ;
Session . set ( PREFIX + "selectedMeasure" , $ ( event . target ) . val ( ) ) ;
2017-01-15 11:33:37 -08:00
} ,
'click .applyButton' : function ( event , template ) {
2017-01-17 22:31:43 -08:00
let measureId = template . $ ( 'select[name="measures"]' ) . val ( ) ;
2017-01-15 11:33:37 -08:00
let $selectedRows = template . $ ( 'tr.selected' ) ;
let price = Number ( template . $ ( 'input[name="price"]' ) . val ( ) ) ;
let setPrevious = template . $ ( 'input[name="setPrevious"]' ) . prop ( 'checked' ) ;
let date = template . $ ( 'input[name="date"]' ) . val ( ) ;
2017-01-17 22:31:43 -08:00
let productIds = [ ] ;
2017-01-15 11:33:37 -08:00
2017-01-17 22:31:43 -08:00
for ( let i = 0 ; i < $selectedRows . length ; i ++ ) {
let product = $ ( $selectedRows [ i ] ) . data ( 'product' ) ;
productIds . push ( product . _id ) ;
2017-01-15 11:33:37 -08:00
}
2017-01-17 22:31:43 -08:00
if ( ! price ) {
Meteor . call ( "clearProductPrice" , productIds , measureId )
2017-01-15 11:33:37 -08:00
}
2017-01-17 22:31:43 -08:00
else {
2017-05-26 11:17:32 -07:00
date = ~ ~ ( moment ( date ? date : new Date ( ) . toDateInputValue ( ) , "YYYY-MM-DD" ) . format ( "YYYYMMDD" ) ) ; // The ~~ is a bitwise not which converts the string into a number in the format of YYYYMMDD for storage in the database; to avoid timezone issues.
2017-01-17 22:31:43 -08:00
setPrevious = setPrevious == true || setPrevious == 'on' || setPrevious == "true" || setPrevious == "yes" ;
if ( setPrevious == true && ! date ) {
sAlert . error ( "Unexpected input." ) ;
}
if ( ! price || isNaN ( price ) || price < 0 ) {
sAlert . error ( "Unexpected input." ) ;
}
2017-01-15 11:33:37 -08:00
2017-01-17 22:31:43 -08:00
Meteor . call ( "setProductPrice" , productIds , measureId , price , setPrevious , date ) ;
2017-01-15 11:33:37 -08:00
}
2017-01-17 22:31:43 -08:00
} ,
'click .resetButton' : function ( event , template ) {
template . $ ( 'input.price' ) . val ( 0 ) ;
template . $ ( 'input.date' ) . val ( new Date ( ) . toDateInputValue ( ) ) ;
template . $ ( 'input[name="setPrevious"]' ) . removeProp ( 'checked' ) ;
} ,
'click .prevProducts' : function ( event , template ) {
if ( ! $ ( event . target ) . hasClass ( 'disabled' ) )
Session . set ( PREFIX + 'skipCount' , Math . max ( 0 , ( Session . get ( PREFIX + 'skipCount' ) || 0 ) - QUERY _LIMIT ) ) ;
} ,
'click .nextProducts' : function ( event , template ) {
if ( ! $ ( event . target ) . hasClass ( 'disabled' ) )
Session . set ( PREFIX + 'skipCount' , ( Session . get ( PREFIX + 'skipCount' ) || 0 ) + QUERY _LIMIT ) ;
2017-01-15 11:33:37 -08:00
}
} ) ;
Template . PricingForProduct . onRendered ( function ( ) {
this . $ ( 'tr' ) . data ( "product" , this . data ) ;
} ) ;
Template . PricingForProduct . helpers ( {
currentPrice : function ( ) {
2017-01-17 22:31:43 -08:00
let measureId = Session . get ( PREFIX + "selectedMeasure" ) ;
2017-01-15 11:33:37 -08:00
let price = this . prices && measureId && this . prices [ measureId ] && this . prices [ measureId ] . price ? this . prices [ measureId ] . price : undefined ;
return price ? price . toLocaleString ( "en-US" , { style : 'currency' , currency : 'USD' , minimumFractionDigits : 2 } ) : "-" ;
} ,
previousPrice : function ( ) {
2017-01-17 22:31:43 -08:00
let measureId = Session . get ( PREFIX + "selectedMeasure" ) ;
2017-01-15 11:33:37 -08:00
let price = this . prices && measureId && this . prices [ measureId ] && this . prices [ measureId ] . previousPrice ? this . prices [ measureId ] . previousPrice : undefined ;
return price ? price . toLocaleString ( "en-US" , { style : 'currency' , currency : 'USD' , minimumFractionDigits : 2 } ) : "-" ;
} ,
priceChangeDate : function ( ) {
2017-01-17 22:31:43 -08:00
let measureId = Session . get ( PREFIX + "selectedMeasure" ) ;
2017-01-15 11:33:37 -08:00
2017-05-26 11:17:32 -07:00
return this . prices && measureId && this . prices [ measureId ] && this . prices [ measureId ] . effectiveDate ? moment ( this . prices [ measureId ] . effectiveDate . toString ( ) , "YYYYMMDD" ) . format ( "MM/DD/YYYY (w)" ) : "-" ;
2017-01-17 22:31:43 -08:00
} ,
rowClass : function ( ) {
return this . deactivated ? "deactivated" : "" ;
2017-01-15 11:33:37 -08:00
}
} ) ;
Template . PricingForProduct . events ( {
'click tr' : function ( event , template ) {
let $row = template . $ ( event . target ) . closest ( "tr" ) ;
let parentTemplate = template . parentTemplate ( 1 ) ;
if ( event . shiftKey ) {
let $lastRow = parentTemplate . $lastClickedRow ;
let $range = ( $row . index ( ) > $lastRow . index ( ) ? $lastRow . nextUntil ( $row ) : $row . nextUntil ( $lastRow ) ) . add ( $row ) ;
if ( event . ctrlKey ) {
$range . toggleClass ( "selected" ) ;
}
else {
$range . addClass ( "selected" ) ;
}
}
else if ( event . ctrlKey ) {
$row . toggleClass ( "selected" ) ;
}
else {
$row . addClass ( "selected" ) ;
$row . siblings ( ) . removeClass ( 'selected' ) ;
}
//Store the last row clicked on in a non-reactive variable attached to the parent template.
parentTemplate . $lastClickedRow = $row ;
}
} ) ;