这是indexloc提供的服务,不要输入任何密码
Skip to content
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## 0.0.7

- Adding nomenclature

## 0.0.1 - 0.0.6

- Base billing structure (charges, modifiers and payments)
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ bill.modifiers //Modifier collection
bill.payments //Payment collection
bill.total //The whole number or amount (charges and modifiers)
bill.balance //Bill remainder (total - payments)
bill.toJson() //A JSON object (without circular references)
```

### Billing collections
Expand All @@ -71,9 +72,49 @@ bill.balance //Bill remainder (total - payments)

### Validations

<item>.isValid // check validity
boolean <item>.isValid // check validity
<item>.errors // errors format [{ propertyName: { validationName: 'Human readable error' }}]

## Using the nomenclature ([samples/node/usingNomenclature.js](samples/node/usingNomenclature.js))
```javascript
var billing = require('../../').Billing;

billing.config({
nomenclature: {
taxGroups: [
{ id: 1, name: '20%', percentRatio: 0.2 },
{ id: 2, name: '9%', percentRatio: 0.09 }
],
departments: [
{ id: 1, name: 'Food', taxGroupId: 1 },
{ id: 2, name: 'Accommodation', taxGroupId: 2 }
],
plus: [
{ id: 1, name: 'Pizza', departmentId: 1, price: 20.5 },
{ id: 2, name: 'Steak', departmentId: 1, price: 30.2 },
{ id: 3, name: 'Room', departmentId: 2, price: 200 }
],
paymentTypes: [
{ id: 1, name: 'Cash', isCash: true, isFiscal: true },
{ id: 2, name: 'Card', isCash: false, isFiscal: true },
{ id: 3, name: 'External', isCash: false, isFiscal: false }
]
}
});

var bill = billing.bills.new();

bill.charges.new({ pluId: 1 }); // -> { qty: 1, price: 20.5, name: 'Pizza', taxRatio: 0.2 }
bill.charges.new({ qty: 2, price: 150, departmentId: 2 });
bill.payments.new({ paymentTypeId: 1 }); // -> { name: 'Cash', value: 20.5, isCash: true, isFiscal: true }

bill.toJson(); // -> {
// charges:
// [ { qty: 1, price: 20.5, name: 'Pizza', taxRatio: 0.2 },
// { qty: 2, price: 150, name: 'Accommodation', taxRatio: 0.09 } ],
// payments: [ { name: 'Cash', value: 320.5, isCash: true, isFiscal: true } ] }
```

## Build

`npm run build` - Generate node library. (<project>/lib/*)
Expand Down
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = function (config) {
"**/*.ts": ["karma-typescript"]
},

reporters: ["dots", "karma-typescript"],
reporters: ["mocha", "karma-typescript"],

browsers: ["PhantomJS"],

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "billing",
"version": "0.0.6",
"version": "0.0.7",
"description": "Billing Module Js",
"author": "AlexV",
"repository": {
Expand Down Expand Up @@ -29,6 +29,7 @@
"karma": "^1.2.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-mocha-reporter": "^2.2.0",
"karma-phantomjs-launcher": "^1.0.2",
"karma-typescript": "^2.0.5",
"source-map": "^0.5.6",
Expand Down
32 changes: 32 additions & 0 deletions samples/node/usingNomenclature.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var billing = require('../../').Billing;

billing.config({
nomenclature: {
taxGroups: [
{ id: 1, name: '20%', percentRatio: 0.2 },
{ id: 2, name: '9%', percentRatio: 0.09 }
],
departments: [
{ id: 1, name: 'Food', taxGroupId: 1 },
{ id: 2, name: 'Accommodation', taxGroupId: 2 }
],
plus: [
{ id: 1, name: 'Pizza', departmentId: 1, price: 20.5 },
{ id: 2, name: 'Steak', departmentId: 1, price: 30.2 },
{ id: 3, name: 'Room', departmentId: 2, price: 200 }
],
paymentTypes: [
{ id: 1, name: 'Cash', isCash: true, isFiscal: true },
{ id: 2, name: 'Card', isCash: false, isFiscal: true },
{ id: 3, name: 'External', isCash: false, isFiscal: false }
]
}
});

var bill = billing.bills.new();

bill.charges.new({ pluId: 1 }); // -> { qty: 1, price: 20.5, name: 'Pizza', taxRatio: 0.2 }
bill.charges.new({ qty: 2, price: 150, departmentId: 2 });
bill.payments.new({ paymentTypeId: 1 }); // -> { name: 'Cash', value: 20.5, isCash: true, isFiscal: true }

console.log(bill.toJson());
32 changes: 31 additions & 1 deletion src/billing/bill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { ChargesCollection } from './charge';
import { Modifier, ModifiersCollection } from './modifier';
import { PaymentsCollection } from './payment';
import { ValidationModel } from './concerns/validations';
import { Operator } from './nomenclature';
import { IBillAttributes } from './interface';

export declare type GlobalModifier = Modifier;
/**
*
*
Expand All @@ -18,7 +21,7 @@ import { ValidationModel } from './concerns/validations';
export class Bill extends ValidationModel {
isSaved: boolean;

constructor(attributes :any = {}) {
constructor(attributes :IBillAttributes = {}) {
super();
}

Expand All @@ -34,13 +37,26 @@ export class Bill extends ValidationModel {
* @type {ModifiersCollection}
*/
modifiers :ModifiersCollection = new ModifiersCollection(this);
/**
*
*
* @type {number}
* @memberOf Bill
*/
operatorId :number;
/**
*
*
* @type {PaymentsCollection}
*/
payments :PaymentsCollection = new PaymentsCollection(this);

get modifier() :GlobalModifier {
for (let m of this.modifiers) {
if (!(<Modifier>m).charge) return <GlobalModifier>m;
}
}

/**
*
*
Expand All @@ -67,6 +83,20 @@ export class Bill extends ValidationModel {
return this.isSaved = success;
}

toJson(useNomenclatureIds = false) :any {
if (this.isValid) {
let json = {};
if (this.charges.length) json['charges'] = this.charges.toJson(useNomenclatureIds);
if (this.payments.length) json['payments'] = this.payments.toJson(useNomenclatureIds);
if (this.modifier) json['modifier'] = this.modifier.toJson();
return json;
}
}

get operator() {
if (this.operatorId) return Operator.find(this.operatorId);
}

static new(attributes :any = {}) :Bill {
return new Bill(attributes);
}
Expand Down
Loading