diff --git a/src/pages/invoices/$id/_layout.js b/src/pages/invoices/$id/_layout.js index 9b766ee..596ef83 100644 --- a/src/pages/invoices/$id/_layout.js +++ b/src/pages/invoices/$id/_layout.js @@ -28,25 +28,45 @@ import LineItems from '../../../components/invoices/line-items'; import FooterToolbar from '../../../components/layout/footer-toolbar'; import { OrganizationContext } from '../../../providers/contexts'; +let discountValue; +let discountType; + const totals = (lineItems, taxRates) => { let subTotal = currency(0, { separator: '', symbol: '' }); let taxTotal = currency(0, { separator: '', symbol: '' }); + let discount = currency(0, { separator: '', symbol: '' }); + forEach(lineItems, line => { if (has(line, 'subtotal')) { subTotal = subTotal.add(line.subtotal); + + let lineDiscount; + if(discountValue !== 0 | discountValue !== ""){ + if(discountType === "%"){ + lineDiscount = currency(line.subtotal).multiply(discountValue / 100); + } else if(discountType==="currency"){ + lineDiscount = currency(lineDiscount).add(discountValue / lineItems.length); + } + } if (has(line, 'taxRate')) { - const taxRate = get(taxRates.items, line.taxRate); + const taxRate = get(taxRates.items, line.taxRate) + if (taxRate) { - const lineTax = currency(line.subtotal).multiply(taxRate.percentage / 100); + const afterDiscount = currency(line.subtotal).subtract(lineDiscount) + const lineTax = afterDiscount.multiply(taxRate.percentage / 100); taxTotal = taxTotal.add(lineTax); } } + + discount = discount.add(lineDiscount) } }); - const total = subTotal.add(taxTotal); - return { subTotal, taxTotal, total }; + + const total = currency(subTotal, { separator: '', symbol: '' }).subtract(discount).add(taxTotal); + return { subTotal, taxTotal, total, discount}; + }; class InvoiceForm extends Component { @@ -83,6 +103,16 @@ class InvoiceForm extends Component { } }; + onDiscountTypeChange = (value) => { + discountType = value + } + + onDiscountChange = (value) => { + if (value !== "" | value !== 0) { + discountValue = value + } + }; + onStateSelect = (_id, _rev, key) => { this.props.dispatch({ type: 'invoices/state', @@ -132,7 +162,7 @@ class InvoiceForm extends Component { handleSubmit, submitting, } = this.props; - const { subTotal, taxTotal, total } = totals(lineItems, taxRates); + const { subTotal, taxTotal, total, discount } = totals(lineItems, taxRates); const invoice = get(invoices.items, get(this.props, ['match', 'params', 'id'])); const stateMenu = (_id, _rev) => ( @@ -261,6 +291,24 @@ class InvoiceForm extends Component { + + + Discount} + onChange={(event, newValue) => + this.onDiscountChange(newValue) + } + /> + + + e.preventDefault()} label=" " defaultValue="%" onChange={(value) => this.onDiscountTypeChange(value)}> + % + {currency} + + + + { discount > 0 && + + Discount + + + + + } Tax @@ -432,18 +500,22 @@ export default withI18n()( '' ), lineItems: [{}], + discountValue: selector(state, 'discountValue'), + discountType: selector(state, 'discountType'), + discount: selector(state, 'discount'), }, }))( reduxForm({ form: 'invoice', onSubmit: async (data, dispatch, props) => { const { lineItems, taxRates } = props; - const { subTotal, taxTotal, total } = totals(lineItems, taxRates); + const { subTotal, taxTotal, total, discount } = totals(lineItems, taxRates); return await dispatch({ type: 'invoices/save', data: { ...data, subTotal: subTotal.format(), + discount: discount.format(), taxTotal: taxTotal.format(), total: total.format(), }, diff --git a/src/pages/invoices/$id/pdf.js b/src/pages/invoices/$id/pdf.js index ce11a7a..813784c 100644 --- a/src/pages/invoices/$id/pdf.js +++ b/src/pages/invoices/$id/pdf.js @@ -239,6 +239,22 @@ class Invoice extends Component { /> + { invoice.discount > 0 && + + + Discount + + + + + }