Skip to content

Commit

Permalink
Added the ability to apply discounts to the invoice.
Browse files Browse the repository at this point in the history
  • Loading branch information
Saska Kolehmainen authored and Saska Kolehmainen committed Oct 28, 2020
1 parent 592ca42 commit 3f49de9
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 6 deletions.
84 changes: 78 additions & 6 deletions src/pages/invoices/$id/_layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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) => (
Expand Down Expand Up @@ -261,6 +291,24 @@ class InvoiceForm extends Component {
</Col>
</Row>

<Row gutter={16}>
<Col span={5} style={{ marginTop: '20px' }}>
<Field
name="discountValue"
component={AInput}
label={<Trans>Discount</Trans>}
onChange={(event, newValue) =>
this.onDiscountChange(newValue)
}
/>
</Col>
<Col span={3} style={{ marginTop: '20px'}}>
<Field name="discountType" component={ASelect} onDrop={e => e.preventDefault()} label=" " defaultValue="%" onChange={(value) => this.onDiscountTypeChange(value)}>
<Select.Option value="%">%</Select.Option>
<Select.Option value="currency">{currency}</Select.Option>
</Field>
</Col>
</Row>
<Row gutter={16}>
<Col span={8} style={{ marginTop: '20px' }}>
<Field
Expand Down Expand Up @@ -299,6 +347,26 @@ class InvoiceForm extends Component {
</h4>
</td>
</tr>
{ discount > 0 && <tr>
<td style={{ textAlign: 'right' }}>
<Trans>Discount</Trans>
</td>
<td style={{ textAlign: 'right' }}>
<NumberFormat
value={discount}
format={{
style: 'currency',
currency:
currency || get(context.state, 'organization.currency', 'EUR'),
minimumFractionDigits: get(
context.state,
'organization.minimum_fraction_digits',
2
),
}}
/>
</td>
</tr>}
<tr>
<td style={{ textAlign: 'right' }}>
<Trans>Tax</Trans>
Expand Down Expand Up @@ -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(),
},
Expand Down
16 changes: 16 additions & 0 deletions src/pages/invoices/$id/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,22 @@ class Invoice extends Component {
/>
</td>
</tr>
{ invoice.discount > 0 && <tr>
<td colSpan="2" className="border-top-0" />
<td colSpan="2" className="border-top-0">
<Trans>Discount</Trans>
</td>
<td className="text-right border-top-0">
<NumberFormat
value={invoice.discount}
format={{
style: 'currency',
currency: invoice.currency,
minimumFractionDigits: get(organization, 'minimum_fraction_digits', 2),
}}
/>
</td>
</tr>}
<tr>
<td colSpan="2" className="border-top-0" />
<td colSpan="2" className="border-top-0">
Expand Down

0 comments on commit 3f49de9

Please sign in to comment.