(function () { 'use strict'; angular.module('myapp', []) .service('Commission', function () { return window.Commission; }) .controller('TddCtrl', function (Commission, $scope) { var comm = new Commission(); $scope.$watch('value', function (nv, ov) { if (nv != ov) { var calc = comm.calc($scope.value); if (calc) { $scope.fee = calc.fee; $scope.liquid = calc.liquid; } else { $scope.fee = 0; $scope.liquid = 0; } } }) }); })(); // // Requires dependencies for node // if (typeof module !== 'undefined' && module.exports) { var _ = require('lodash'); } (function (_) { 'use strict'; // // @usage // var commission = new Commission(options); // var Commission = function (options) { // // Default rules // this.rules = [{ range: { min: 0, max: 50 }, percent: 0, value: 5 }, { range: { min: 51, max: 500 }, percent: 10, value: 0 }, { range: { min: 501, max: 99999 }, percent: 8, value: 10 }]; // // Extend by options // _.merge(this, options); }; function calc(arg) { if (_.isArray(arg)) { var fee = 0, total = 0; _.each(arg, function (row) { var sub = this.sub(row.product.price); fee += sub.fee * row.qty; total += row.product.price * row.qty; }.bind(this)); total -= fee; return { fee: fee.toFixed(2), liquid: total.toFixed(2) } } else if (_.isNumber(arg)) { var sub = this.sub(arg); return { fee: sub.fee, liquid: sub.liquid } } } function sub(value) { var fee = 0, liquid = value; _.each(this.rules, function (plan) { if (value >= plan.range.min && value <= plan.range.max) { if (plan.percent) fee += (value * plan.percent) / 100; if (plan.value) fee += plan.value; } }); liquid -= fee; return { fee: fee.toFixed(2), liquid: liquid.toFixed(2) } } // // API // Commission.prototype.calc = calc; Commission.prototype.sub = sub; // // Node // if (typeof module !== 'undefined' && module.exports) { module.exports = Commission; } // // Browser // else { window.Commission = Commission; } })(_);
<div ng-app="myapp" ng-controller="TddCtrl"> <br /> <br /> Enter the value to be paid <input ng-model="value" type="number"> <br /> Value to be paid: ${{value}} <br /> Value of commission: ${{fee}} <br /> Remaining amount: ${{liquid}} </div>