|
| 1 | +import MJMLElement from '../decorators/MJMLElement' |
| 2 | +import React, { Component } from 'react' |
| 3 | +import _ from 'lodash' |
| 4 | +import numeral from 'numeral' |
| 5 | + |
| 6 | +import MJTable from '../Table' |
| 7 | + |
| 8 | +@MJMLElement({ |
| 9 | + tagName: 'mj-invoice', |
| 10 | + |
| 11 | + attributes: { |
| 12 | + 'color': '#b9b9b9', |
| 13 | + 'font-family': 'Roboto, Ubuntu, Helvetica, Arial, sans-serif', |
| 14 | + 'font-size': '13px', |
| 15 | + 'line-height': '22px', |
| 16 | + 'border': '1px solid #ecedee', |
| 17 | + |
| 18 | + 'intl': 'name:Name;price:Price;quantity:Quantity' |
| 19 | + } |
| 20 | +}) |
| 21 | +class Invoice extends Component { |
| 22 | + |
| 23 | + constructor(props) { |
| 24 | + super(props) |
| 25 | + |
| 26 | + const format = props.mjAttribute('format') |
| 27 | + const currencies = format.match(/([^-\d.,])/g) |
| 28 | + |
| 29 | + this.items = props.mjChildren().filter((child) => child.get('tagName') === 'mj-invoice-item') |
| 30 | + this.format = format.replace(/([^-\d.,])/g, '$') |
| 31 | + this.currency = (currencies) ? currencies[0] : null |
| 32 | + } |
| 33 | + |
| 34 | + static baseStyles = { |
| 35 | + th: { |
| 36 | + padding: '10px 20px', |
| 37 | + fontWeight: 700, |
| 38 | + textTransform: 'uppercase', |
| 39 | + textAlign: 'left' |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + static intl = { |
| 44 | + name : 'Name', |
| 45 | + price : 'Price', |
| 46 | + quantity: 'Quantity', |
| 47 | + total : 'Total:' |
| 48 | + } |
| 49 | + |
| 50 | + getStyles() { |
| 51 | + const { mjAttribute } = this.props |
| 52 | + |
| 53 | + const styles = _.merge({}, this.constructor.baseStyles, { |
| 54 | + table: { |
| 55 | + color: mjAttribute('color'), |
| 56 | + fontFamily: mjAttribute('font-family'), |
| 57 | + fontSize: mjAttribute('font-size'), |
| 58 | + lineHeight: mjAttribute('line-height') |
| 59 | + }, |
| 60 | + th: { |
| 61 | + fontFamily: mjAttribute('font-family'), |
| 62 | + fontSize: mjAttribute('font-size'), |
| 63 | + lineHeight: mjAttribute('line-height') |
| 64 | + }, |
| 65 | + thead: { |
| 66 | + borderBottom: mjAttribute('border') |
| 67 | + }, |
| 68 | + tfoot: { |
| 69 | + borderTop: mjAttribute('border') |
| 70 | + }, |
| 71 | + total: { |
| 72 | + padding: '10px 20px', |
| 73 | + fontFamily: mjAttribute('font-family'), |
| 74 | + fontSize: mjAttribute('font-size'), |
| 75 | + fontWeight: 700, |
| 76 | + lineHeight: mjAttribute('line-height'), |
| 77 | + textAlign: 'right' |
| 78 | + } |
| 79 | + }) |
| 80 | + |
| 81 | + styles.thQuantity = _.merge({}, styles.th, { textAlign: 'right' }) |
| 82 | + |
| 83 | + return styles |
| 84 | + } |
| 85 | + |
| 86 | + getAttributes() { |
| 87 | + const { mjAttribute } = this.props |
| 88 | + |
| 89 | + return { |
| 90 | + table: { |
| 91 | + color: mjAttribute('color'), |
| 92 | + fontFamily: mjAttribute('font-family'), |
| 93 | + fontSize: mjAttribute('font-size'), |
| 94 | + lineHeight: mjAttribute('line-height') |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + getIntl() { |
| 100 | + const { mjAttribute } = this.props |
| 101 | + |
| 102 | + const intl = _.cloneDeep(this.constructor.intl) |
| 103 | + |
| 104 | + mjAttribute('intl').split(';').forEach((t) => { |
| 105 | + if (t && t.indexOf(':') != -1) { |
| 106 | + t = t.split(':') |
| 107 | + intl[t[0].trim()] = t[1].trim() |
| 108 | + } |
| 109 | + }) |
| 110 | + |
| 111 | + return intl |
| 112 | + } |
| 113 | + |
| 114 | + getTotal() { |
| 115 | + const format = this.format |
| 116 | + const currency = this.currency |
| 117 | + const total = this.items.reduce((prev, item) => { |
| 118 | + const unitPrice = parseFloat(numeral().unformat(item.getIn(['attributes', 'price']))) |
| 119 | + const quantity = parseInt(item.getIn(['attributes', 'quantity'])) |
| 120 | + |
| 121 | + return prev + unitPrice * quantity |
| 122 | + }, 0) |
| 123 | + |
| 124 | + return numeral(total).format(format).replace(/([^-\d.,])/g, currency) |
| 125 | + } |
| 126 | + |
| 127 | + render() { |
| 128 | + this.styles = this.getStyles() |
| 129 | + |
| 130 | + const { renderChildren } = this.props |
| 131 | + |
| 132 | + const intl = this.getIntl() |
| 133 | + const attrs = this.getAttributes() |
| 134 | + |
| 135 | + const total = this.getTotal() |
| 136 | + |
| 137 | + return ( |
| 138 | + <MJTable {...attrs.table}> |
| 139 | + <thead> |
| 140 | + <tr style={this.styles.thead}> |
| 141 | + <th style={this.styles.th}>{intl['name']}</th> |
| 142 | + <th style={this.styles.th}>{intl['price']}</th> |
| 143 | + <th style={this.styles.thQuantity}>{intl['quantity']}</th> |
| 144 | + </tr> |
| 145 | + </thead> |
| 146 | + <tbody> |
| 147 | + {renderChildren()} |
| 148 | + </tbody> |
| 149 | + <tfoot> |
| 150 | + <tr style={this.styles.tfoot}> |
| 151 | + <th style={this.styles.th} colSpan="2">{intl['total']}</th> |
| 152 | + <td style={this.styles.total}>{total}</td> |
| 153 | + </tr> |
| 154 | + </tfoot> |
| 155 | + </MJTable> |
| 156 | + ) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +export default Invoice |
0 commit comments