@harvest-profit/harvest-profit-calculations
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -19,16 +19,17 @@ Object.defineProperty(exports, "__esModule", { | ||
* @global | ||
* @typedef {Object} application The application to calculate. | ||
* @property {number} yield The total yield of the application | ||
* @property {number} planted_acres The planted acres of the application | ||
* @property {string} chemical_cost Chemical cost of the application | ||
* @property {string} crop_insurance_cost Crop Insurance cost of the application | ||
* @property {string} drying_cost Drying cost of the application | ||
* @property {string} fertilizer_cost Fertilizer cost of the application | ||
* @property {string} fuel_cost Fuel cost of the application | ||
* @property {string} labor_cost Labot cost of the application | ||
* @property {string} other_cost Other cost of the application | ||
* @property {string} payments_depreciation_cost Payments/Depreciation cost of the application | ||
* @property {string} repair_cost Repair cost of the application | ||
* @property {string} seed_cost Seed cost of the application | ||
* @property {Object} custom_cost Any custom costs of the application | ||
* @typedef {Object} application The application to calculate. | ||
* @property {number} yield The total yield of the application | ||
* @property {number} planted_acres The planted acres of the application | ||
* @property {string} chemical_cost Chemical cost of the application | ||
* @property {string} crop_insurance_cost Crop Insurance cost of the application | ||
* @property {string} drying_cost Drying cost of the application | ||
* @property {string} fertilizer_cost Fertilizer cost of the application | ||
* @property {string} fuel_cost Fuel cost of the application | ||
* @property {string} labor_cost Labor cost of the application | ||
* @property {string} land_cost Land cost of the application | ||
* @property {string} other_cost Other cost of the application | ||
* @property {string} payments_depreciation_cost Payments/Depreciation cost of the application | ||
* @property {string} repair_cost Repair cost of the application | ||
* @property {string} seed_cost Seed cost of the application | ||
* @property {Object} custom_cost Any custom costs of the application | ||
*/ | ||
@@ -56,15 +57,16 @@ | ||
var cost = 0; | ||
cost += _lodash2['default'].toNumber(application.chemical_cost); | ||
cost += _lodash2['default'].toNumber(application.crop_insurance_cost); | ||
cost += _lodash2['default'].toNumber(application.drying_cost); | ||
cost += _lodash2['default'].toNumber(application.fertilizer_cost); | ||
cost += _lodash2['default'].toNumber(application.fuel_cost); | ||
cost += _lodash2['default'].toNumber(application.labor_cost); | ||
cost += _lodash2['default'].toNumber(application.other_cost); | ||
cost += _lodash2['default'].toNumber(application.payments_depreciation_cost); | ||
cost += _lodash2['default'].toNumber(application.repair_cost); | ||
cost += _lodash2['default'].toNumber(application.seed_cost); | ||
cost += Object.keys(application.custom_cost).reduce(function (value, costGroup) { | ||
return value + _lodash2['default'].toNumber(application.custom_cost[costGroup]); | ||
}, 0); | ||
cost += _lodash2['default'].toNumber(application.chemical_cost || 0); | ||
cost += _lodash2['default'].toNumber(application.crop_insurance_cost || 0); | ||
cost += _lodash2['default'].toNumber(application.drying_cost || 0); | ||
cost += _lodash2['default'].toNumber(application.fertilizer_cost || 0); | ||
cost += _lodash2['default'].toNumber(application.fuel_cost || 0); | ||
cost += _lodash2['default'].toNumber(application.labor_cost || 0); | ||
cost += _lodash2['default'].toNumber(application.other_cost || 0); | ||
cost += _lodash2['default'].toNumber(application.payments_depreciation_cost || 0); | ||
cost += _lodash2['default'].toNumber(application.repair_cost || 0); | ||
cost += _lodash2['default'].toNumber(application.seed_cost || 0); | ||
cost += _lodash2['default'].toNumber(application.land_cost || 0); | ||
cost += _lodash2['default'].sumBy(Object.keys(application.custom_cost || {}), function (costGroup) { | ||
return _lodash2['default'].toNumber(application.custom_cost[costGroup]); | ||
}); | ||
// Return the total cost for the application | ||
@@ -71,0 +73,0 @@ return cost; |
@@ -124,15 +124,56 @@ Object.defineProperty(exports, "__esModule", { | ||
}]; | ||
Contract.basisType = 'basis'; | ||
Contract.cashSaleType = 'cashsale'; | ||
Contract.forwardType = 'forward'; | ||
Contract.futuresType = 'futures'; | ||
Contract.hedgeToArriveType = 'hedgetoarrive'; | ||
Contract.optionSaleType = 'optionsale'; | ||
Contract.isBasisType = function (type) { | ||
return type === Contract.basisType; | ||
}; | ||
Contract.isCashSaleType = function (type) { | ||
return type === Contract.cashSaleType; | ||
}; | ||
Contract.isForwardType = function (type) { | ||
return type === Contract.forwardType; | ||
}; | ||
Contract.isFuturesType = function (type) { | ||
return type === Contract.futuresType; | ||
}; | ||
Contract.isHedgeToArriveType = function (type) { | ||
return type === Contract.hedgeToArriveType; | ||
}; | ||
Contract.isOptionSaleType = function (type) { | ||
return type === Contract.optionSaleType; | ||
}; | ||
Contract.sortPosition = function (type) { | ||
if (Contract.isBasisType(type)) return 3; | ||
if (Contract.isCashSaleType(type)) return 1; | ||
if (Contract.isForwardType(type)) return 2; | ||
if (Contract.isFuturesType(type)) return 5; | ||
if (Contract.isHedgeToArriveType(type)) return 4; | ||
if (Contract.isOptionSaleType(type)) return 6; | ||
return 7; | ||
}; | ||
Contract.sortTypes = function (types) { | ||
return types.sort(function (a, b) { | ||
return Contract.sortPosition(a) - Contract.sortPosition(b); | ||
}); | ||
}; | ||
Contract.translateContractTypeToName = function (type) { | ||
var types = { | ||
basis: 'Basis', | ||
cashsale: 'Cash Sale', | ||
forward: 'Forward', | ||
futures: 'Futures', | ||
hedgetoarrive: 'Hedge To Arrive', | ||
optionsale: 'Option Sale' | ||
}; | ||
if (types[type]) { | ||
return types[type]; | ||
} | ||
if (Contract.isBasisType(type)) return 'Basis'; | ||
if (Contract.isCashSaleType(type)) return 'Cash Sale'; | ||
if (Contract.isForwardType(type)) return 'Forward'; | ||
if (Contract.isFuturesType(type)) return 'Futures'; | ||
if (Contract.isHedgeToArriveType(type)) return 'Hedge To Arrive'; | ||
if (Contract.isOptionSaleType(type)) return 'Option Sale'; | ||
return _lodash2['default'].capitalize(type); | ||
@@ -142,16 +183,51 @@ }; | ||
Contract.translateContractTypeToUrl = function (type) { | ||
var types = { | ||
basis: 'basis', | ||
cashsale: 'cash_sale', | ||
forward: 'forward', | ||
futures: 'futures', | ||
hedgetoarrive: 'hedge_to_arrive', | ||
optionsale: 'option_sale' | ||
}; | ||
if (types[type]) { | ||
return types[type]; | ||
} | ||
if (Contract.isBasisType(type)) return 'basis'; | ||
if (Contract.isCashSaleType(type)) return 'cash_sale'; | ||
if (Contract.isForwardType(type)) return 'forward'; | ||
if (Contract.isFuturesType(type)) return 'futures'; | ||
if (Contract.isHedgeToArriveType(type)) return 'hedge_to_arrive'; | ||
if (Contract.isOptionSaleType(type)) return 'option_sale'; | ||
return type; | ||
}; | ||
Contract.sanitize = function (contract, typeName) { | ||
var includeId = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true; | ||
var sanitationKeys = ['crop_year', 'crop_id', 'notes', 'open_order', 'sub_entity_id', 'year_id', 'date']; | ||
if (includeId) sanitationKeys.push('id'); | ||
if (Contract.isBasisType(typeName)) { | ||
sanitationKeys = sanitationKeys.concat(['amount_sold', 'basis', 'contract_number', 'delivery_location_id', 'futures_month', 'futures_year']); | ||
} | ||
if (Contract.isCashSaleType(typeName)) { | ||
sanitationKeys = sanitationKeys.concat(['amount_sold', 'basis', 'contract_number', 'delivery_location_id', 'discount_or_premium', 'futures_price']); | ||
} | ||
if (Contract.isForwardType(typeName)) { | ||
sanitationKeys = sanitationKeys.concat(['amount_sold', 'basis', 'contract_number', 'delivery_location_id', 'delivery_period_month', 'delivery_period_year', 'discount_or_premium', 'futures_price']); | ||
} | ||
if (Contract.isFuturesType(typeName)) { | ||
sanitationKeys = sanitationKeys.concat(['basis', 'contract_number', 'delivery_location_id', 'futures_month', 'futures_price', 'futures_year', 'gain_loss', 'long_short', 'number_of_contracts', 'size']); | ||
} | ||
if (Contract.isHedgeToArriveType(typeName)) { | ||
sanitationKeys = sanitationKeys.concat(['amount_sold', 'basis', 'contract_number', 'delivery_location_id', 'futures_month', 'futures_price', 'futures_year']); | ||
} | ||
if (Contract.isOptionSaleType(typeName)) { | ||
sanitationKeys = sanitationKeys.concat(['contract_number', 'delivery_location_id', 'futures_month', 'futures_year', 'number_of_contracts', 'open_order', 'gain_loss', 'discount_or_premium', 'option_strategy', 'strike_price']); | ||
} | ||
// Fetch valid keys from contract | ||
var objectKeys = Object.keys(contract).filter(function (key) { | ||
return sanitationKeys.indexOf(key) >= 0; | ||
}); | ||
// Populate an object from valid keys | ||
var sanitizedContract = {}; | ||
objectKeys.forEach(function (key) { | ||
sanitizedContract[key] = contract[key]; | ||
}); | ||
return sanitizedContract; | ||
}; | ||
exports['default'] = Contract; |
@@ -9,2 +9,6 @@ Object.defineProperty(exports, "__esModule", { | ||
var _contract = require('./contract'); | ||
var _contract2 = _interopRequireDefault(_contract); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
@@ -25,36 +29,26 @@ | ||
Contracts.filterValidRevenueContracts = function (contracts) { | ||
var invalidTypes = ['optionsale']; | ||
return contracts.filter(function (contract) { | ||
var isValid = invalidTypes.indexOf(contract.type_name) === -1; | ||
var isClosed = !contract.open_order; | ||
return isValid && isClosed; | ||
Contracts.calculateProduction = function (contracts) { | ||
return _lodash2['default'].sumBy(contracts.filter(function (contract) { | ||
return !_contract2['default'].isOptionSaleType(contract.type_name) && !_contract2['default'].isBasisType(contract.type_name); | ||
}), function (contract) { | ||
return _lodash2['default'].toNumber(contract.amount_sold); | ||
}); | ||
}; | ||
Contracts.filterValidProductionContracts = function (contracts) { | ||
var invalidTypes = ['basis', 'optionsale']; | ||
return contracts.filter(function (contract) { | ||
var isValid = invalidTypes.indexOf(contract.type_name) === -1; | ||
var isClosed = !contract.open_order; | ||
return isValid && isClosed; | ||
}); | ||
}; | ||
Contracts.calculateContractsProduction = function (contracts) { | ||
var filteredContracts = Contracts.filterValidProductionContracts(contracts); | ||
var production = _lodash2['default'].sumBy(filteredContracts, function (contract) { | ||
Contracts.calculateProductionWithBasis = function (contracts) { | ||
return _lodash2['default'].sumBy(contracts.filter(function (contract) { | ||
return !_contract2['default'].isOptionSaleType(contract.type_name); | ||
}), function (contract) { | ||
return _lodash2['default'].toNumber(contract.amount_sold); | ||
}); | ||
return production; | ||
}; | ||
Contracts.calculateContractsRevenue = function (contracts) { | ||
var filteredContracts = Contracts.filterValidRevenueContracts(contracts); | ||
var revenue = _lodash2['default'].sumBy(filteredContracts, function (contract) { | ||
return _lodash2['default'].toNumber(contract.cash_price); | ||
Contracts.calculateRevenue = function (contracts) { | ||
return _lodash2['default'].sumBy(contracts.filter(function (contract) { | ||
return !_contract2['default'].isOptionSaleType(contract.type_name); | ||
}), function (contract) { | ||
return _lodash2['default'].toNumber(contract.cash_price) * _lodash2['default'].toNumber(contract.amount_sold); | ||
}); | ||
return revenue; | ||
}; | ||
exports['default'] = Contracts; |
@@ -9,6 +9,2 @@ Object.defineProperty(exports, "__esModule", { | ||
var _application = require('./application'); | ||
var _application2 = _interopRequireDefault(_application); | ||
var _contracts = require('./contracts'); | ||
@@ -18,2 +14,6 @@ | ||
var _calculations = require('./calculations'); | ||
var _calculations2 = _interopRequireDefault(_calculations); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
@@ -35,16 +35,2 @@ | ||
Crop.calculateCropAcres = function (applications) { | ||
var acres = _lodash2['default'].sumBy(applications, function (application) { | ||
return application.planted_acres; | ||
}); | ||
return acres; | ||
}; | ||
Crop.calculateCropAcres = function (applications) { | ||
var acres = _lodash2['default'].sumBy(applications, function (application) { | ||
return application.planted_acres; | ||
}); | ||
return acres; | ||
}; | ||
Crop.calculateCropBasis = function (settings) { | ||
@@ -61,20 +47,6 @@ var basis = -0.6; | ||
Crop.calculateCropExpense = function (applications) { | ||
var cost = _lodash2['default'].sumBy(applications, function (application) { | ||
return _application2['default'].calculateApplicationCost(application); | ||
}); | ||
return cost; | ||
}; | ||
Crop.calculateCropProduction = function (applications) { | ||
var production = _lodash2['default'].sumBy(applications, function (app) { | ||
return app.planted_acres * app['yield']; | ||
}); | ||
return production; | ||
}; | ||
Crop.calculateCropRevenue = function (contracts, cashPrice, totalProduction, settings) { | ||
var contractRevenue = _contracts2['default'].calculateContractsRevenue(contracts); | ||
var contractProduction = _contracts2['default'].calculateContractsProduction(contracts); | ||
var remainingProduction = totalProduction - contractProduction; | ||
var contractRevenue = _contracts2['default'].calculateRevenue(contracts); | ||
var contractProduction = _contracts2['default'].calculateProduction(contracts); | ||
var remainingProduction = _calculations2['default'].calculateUnsoldProduction(totalProduction, contractProduction); | ||
var price = cashPrice + Crop.calculateCropBasis(settings); | ||
@@ -81,0 +53,0 @@ var totalRevenue = contractRevenue + remainingProduction * price; |
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.Crop = exports.Contracts = exports.Contract = exports.Application = undefined; | ||
exports.Crop = exports.Calculations = exports.Contracts = exports.Contract = exports.Applications = exports.Application = undefined; | ||
@@ -18,2 +18,6 @@ var _application = require('./calculations/application'); | ||
var _applications = require('./calculations/applications'); | ||
var _applications2 = _interopRequireDefault(_applications); | ||
var _crop = require('./calculations/crop'); | ||
@@ -23,7 +27,13 @@ | ||
var _calculations = require('./calculations/calculations'); | ||
var _calculations2 = _interopRequireDefault(_calculations); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
exports.Application = _application2['default']; | ||
exports.Applications = _applications2['default']; | ||
exports.Contract = _contract2['default']; | ||
exports.Contracts = _contracts2['default']; | ||
exports.Calculations = _calculations2['default']; | ||
exports.Crop = _crop2['default']; |
{ | ||
"name": "@harvest-profit/harvest-profit-calculations", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "All the calculations", | ||
@@ -8,2 +8,5 @@ "main": "dist/index.js", | ||
"author": "Jaryd Krishnan <jaryd@harvestprofit.com>", | ||
"contributors": [ | ||
"Jake Humphrey" | ||
], | ||
"license": "UNLICENSED", | ||
@@ -10,0 +13,0 @@ "scripts": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21698
10
447
1