Skip to content

Commit

Permalink
added super rough tax calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
bnvk committed Mar 6, 2017
1 parent e2a42d0 commit 335726d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 38 deletions.
22 changes: 22 additions & 0 deletions data/_template.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@
"last_updated": "2015-02-08",
"resources": [{
"path": "data/_template.csv",
"format": "csv",
"mediatype": "text/csv",
"client": {
"name": "",
"tax_id": "",
"address": "",
"city": "",
"postal": "",
"country": ""
},
"payment": {
"bank": "",
"iban": "",
"bic": "",
"account": "",
"routing": "",
"address_one": "",
"address_two": "",
"country": "",
"currency": "",
"tax": ""
},
"schema": {
"fields": [{
"name": "date",
Expand Down
78 changes: 49 additions & 29 deletions lib/conjuror.basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ var csv = require('csv')
var wkhtmltopdf = require('wkhtmltopdf')
var read = require('datapackage-read')

// CLI UI
var line = "---------------------------------------------------------------------"

// File Manipulation
var SaveFile = require('./save_file')
Expand All @@ -19,23 +21,24 @@ var SaveFile = require('./save_file')
var Conjuror = require('./conjuror.prepareRecipe.js')

Conjuror.summonUser = function(callback){
// TODO: We should probably remove the depency on args here, and pass it in
// as a variable.
if (Conjuror.args.config !== undefined && Conjuror.args.config.user !== undefined){
// TODO: Should probably remove the depency on args here & pass as a variable
if (Conjuror.args.config !== undefined && Conjuror.args.config.user !== undefined) {
return callback(Conjuror.args.config.user)
} else {
return callback({'exists': false})
}
}

Conjuror.magickData = function(data, schema, date) {
Conjuror.magickData = function(data, resource, date) {

var outputs = {
totals: {
hours: 0,
money: 0.00,
tax: 0.00,
total: 0.00
},
csv: _.pluck(schema.fields, 'name').join(','),
csv: _.pluck(resource.schema.fields, 'name').join(','),
cli: '',
html: ''
}
Expand Down Expand Up @@ -74,7 +77,7 @@ Conjuror.magickData = function(data, schema, date) {
date_filter = 'month'
}

console.log(chalk.blue(' - Date filter ' + date_filter + ': ' + date))
console.log(chalk.blue(' - Date filter: ' + date_filter + ': ' + date))
}

// Make Totals
Expand Down Expand Up @@ -127,7 +130,7 @@ Conjuror.magickData = function(data, schema, date) {
outputs.csv += '\n' + line.join(',')
}

var item_output = Conjuror.murmurLineToSchema(line, schema)
var item_output = Conjuror.murmurLineToSchema(line, resource.schema)
increment_output(item_output)
}
}
Expand All @@ -136,10 +139,19 @@ Conjuror.magickData = function(data, schema, date) {
// Outputs - hook into from recipe instructions #43
outputs.totals.hours = outputs.totals.hours.toFixed(2)
outputs.totals.money = outputs.totals.money.toFixed(2)

if (resource.payment.tax_percent > 0.0) {

outputs.totals.tax = (resource.payment.tax_percent * outputs.totals.money).toFixed(2)
outputs.totals.total = (parseFloat(outputs.totals.tax) + parseFloat(outputs.totals.money)).toFixed(2)
} else {
outputs.totals.tax = "0.0"
outputs.total.total = outputs.totals.money
}

return outputs
}


Conjuror.castToCSV = function(outputs) {

if (_.indexOf(Conjuror.args.options.formats, 'csv') > -1) {
Expand Down Expand Up @@ -180,6 +192,7 @@ Conjuror.castToHTML = function(outputs, user, resource) {
console.log(chalk.green(chalk.blue(" - Loaded template: " + template_path + template + '.html')))
var output_name = 'Invoice - ' + moment().format('D MMMM YYYY')

// Name
if (outputs.client) {
output_name = Conjuror.args.options.invoicenumber + ' - ' +
outputs.client.name + ' - ' + moment().format('DD.MM.YYYY')
Expand All @@ -195,7 +208,7 @@ Conjuror.castToHTML = function(outputs, user, resource) {
var generated_date = moment().format('Do MMMM, YYYY')
}

// To-Do: point to hook into for recipes #43
// TODO: hook into recipes #43
if (Conjuror.args.options.details) {
var data_details = Conjuror.args.options.details
} else {
Expand All @@ -222,8 +235,11 @@ Conjuror.castToHTML = function(outputs, user, resource) {
hours_rows: outputs.html,
hours_total: outputs.totals.hours,
money_total: outputs.totals.money,
tax_total: outputs.totals.tax,
total_total: outputs.totals.total
}

// Currency
if (user !== undefined) {
template_data.user = user
template_data.currency = user.currency
Expand Down Expand Up @@ -252,22 +268,21 @@ Conjuror.castToHTML = function(outputs, user, resource) {
}

}, function(err) {
console.log(chalk.red("Failed to find template: " + template_path + template + '.html'))
console.log(chalk.red("Failed to find template: " + template_path + template + '.html'))
})
}
}

Conjuror.castHTMLToPDF = function(output_path, output_html, output_name) {
if (_.indexOf(Conjuror.args.options.formats, 'pdf') > -1) {
console.log(chalk.blue(' - Saving as a PDF to: ' + output_path + output_name + '.pdf'))
console.log(chalk.gray('-----------------------------------------------------------------------------'))

console.log(chalk.blue(' - Saving as PDF: ' + output_path + output_name + '.pdf'))
console.log(chalk.gray(line))
wkhtmltopdf(output_html, {
pageSize: 'letter',
output: output_path + output_name + '.pdf'
})
} else {
console.log(chalk.red('Oops somehow exporting to PDF failed'))
console.log(chalk.red('Somehow exporting to PDF failed'))
}
}

Expand All @@ -285,32 +300,38 @@ Conjuror.Twirl = function(path, resource, callback) {
csv.parse(data, function(err, data) {
if (err) console.log(err)

var outputs = Conjuror.magickData(data, resource.schema, Conjuror.args.options.date)
var outputs = Conjuror.magickData(data, resource, Conjuror.args.options.date)

// Output CSV - rough implementation https://github.com/bnvk/Conjuror/issues/36
Conjuror.castToCSV(outputs)

var cli_print_hours = outputs.totals.hours
var cli_hours = outputs.totals.hours + ' hours'

// This Section is needed if there are "totals"
// Overwrite outputs.money when we have a fixed price.
// Overwrite "outputs.totals" when there is a fixed price
if ((Conjuror.args.options.price) && (Conjuror.args.options.price != 'tally')) {
outputs.totals.money = Conjuror.args.options.price
outputs.totals.tax = (Conjuror.args.options.price * resource.payment.tax_percent)
outputs.totals.total = (parseFloat(Conjuror.args.options.price) + parseFloat(outputs.totals.tax))
}

var cli_print_money = outputs.totals.money + ' ' + (Conjuror.args.options.currency || 'USD')
var cli_currency = (Conjuror.args.options.currency || 'USD')
var cli_money = outputs.totals.money + ' ' + cli_currency
var cli_tax = outputs.totals.tax + ' ' + cli_currency
var cli_total = outputs.totals.total + ' ' + cli_currency

if (Conjuror.args.options.formats) {
console.log(chalk.gray('-----------------------------------------------------------------------------'))
console.log(chalk.gray(line))
console.log(chalk.green('Output Formats: ' + Conjuror.args.options.formats.join(',')))
}

console.log(chalk.gray('-----------------------------------------------------------------------------'))
console.log(chalk.gray(line))
console.log(chalk.gray(outputs.cli))
console.log(chalk.gray('-----------------------------------------------------------------------------'))
console.log(chalk.green('Total hours worked: ' + cli_print_hours))
console.log(chalk.green('Total money earned: ' + cli_print_money))
console.log(chalk.gray('-----------------------------------------------------------------------------'))
console.log(chalk.gray(line))
console.log(chalk.green('Time worked: ' + cli_hours))
console.log(chalk.green('Cash earned: ' + cli_money))
console.log(chalk.green('Taxes added: ' + cli_tax))
console.log(chalk.green('Total amount: ' + cli_total))
console.log(chalk.gray(line))

// Get User
Conjuror.summonUser(function(user_data) {
Expand All @@ -325,12 +346,11 @@ Conjuror.Twirl = function(path, resource, callback) {

})
}).catch(function(error) {
console.log(chalk.red("Error while Twirling: "), error)
console.log(chalk.red("Error processing: "), error)
if (callback) return callback(Error)
})
}


// Load Schema
Conjuror.Grow = function(args) {

Expand All @@ -344,15 +364,15 @@ Conjuror.Grow = function(args) {
read.load(schema_file, function(error, json_data) {

if (error) {
return console.error(chalk.red('Had a project with opening file'), error)
return console.error(chalk.red('Had error opening file'), error)
}

console.log(chalk.blue(' - Get item from schema'))

// If Schema Contains Multiple
_.each(json_data.resources, function(resource, key) {

console.log(chalk.blue(' - Twirl resource: ' + resource.path))
console.log(chalk.blue(' - Processing resource: ' + resource.path))
// Open Data
Conjuror.Twirl(dataPath, resource)
})
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "conjuror",
"description": "A magical CSV data parsing and outputing wizard or witch",
"version": "0.2.2",
"version": "0.2.4",
"repository": {
"url": "https://github.com/bnvk/Conjuror"
},
Expand All @@ -15,7 +15,7 @@
"bin": {
"conjuror": "./bin/conjuror"
},
"author": "Brennan Novak <hi@bnvk.me>",
"author": "Brennan Novak <hi@brennannovak.com>",
"contributors": [
"Simon Vansintjan <[email protected]>"
],
Expand Down
13 changes: 6 additions & 7 deletions templates/invoice.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@
table { border-collapse: collapse; border-spacing: 0; }
strong { font-weight:bold; }
em { font-style:italic; }

/* Social-Igniter : Theme : site_simple
* Author : Brennan Novak
* Contact : [email protected]
*/
* { margin: 0; }
body { height: 100%; margin: 0; font-family: 'Charter', Georgia, serif; font-size: 14px; line-height: 21px; }
h1 { font-size: 26px; line-height: 1.6; margin: 0 0 10px 0; padding: 0px; font-weight: bold; }
Expand Down Expand Up @@ -129,8 +124,12 @@ <h4>Invoice</h4>
<p><%= data_message %></p>
<% } %>
<p>
<strong>Total Hours Worked:</strong> <%= hours_total %><br>
<strong>Payment Due:</strong> <%= money_total %> <%= currency %>
<strong>Hours Worked:</strong> <%= hours_total %><br>
<strong>Money Earned:</strong> <%= money_total % <%= currency %>><br>
<strong>Tax <%= tax_percent %>% <%= tax %>:</strong> <%= tax_total %> <%= currency %>
</p>
<p class="section-important">
<strong>Total Due:</strong> <%= total_total %> <%= currency %>
</p>
<% if (resource.client) { %>
<p>
Expand Down

0 comments on commit 335726d

Please sign in to comment.