Skip to content

Commit

Permalink
feat: added Bill calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
ali-master committed Sep 27, 2020
1 parent 54327bc commit 6c6b945
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 166 deletions.
53 changes: 33 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,42 +205,49 @@ URLfix("https://en.wikipedia.org/wiki/Persian_alphabet"); // "https://en.wikiped
URLfix("Sample Text"); // "Sample Text"
```
### Bill calculator

| Method | Description | Return type
|--- |--- |---
| getResult | Result of bill calculated information | BillResult
| getAmount | Calculate Bill amount by payment id and bill id which entered by the Bill constructor | number
| getBillType | Get Bill provider type name | BillTypes
| getBarcode | Calculate and get Bill's barcode | string
| verificationBill | Validate entered both Bill id and payment id, and return true if bill id and payment id relation was true | boolean
| verificationBillId | Validate entered Bill id | boolean
| verificationBillPayment | Validate entered Bill payment id | boolean
```js
import { BillInfo } from "persian-tools2";
import { Bill } from "persian-tools2";

// Calculate bill amount by bill id and payment id

// Convert to Iranian Rials
BillInfo({ billId: 1117753200140, paymentId: 12070160, currency: "rial" }).getData().amount; // 120000
// Return bill amount by Iranian Toman by default
BillInfo({ billId: 1117753200140, paymentId: 12070160 }).getData().amount; // 12000
new Bill({ billId: 1117753200140, paymentId: 12070160, currency: "rial" }).getResult().amount; // 120000
// Return bill amount by Toman(Iranian currency type) by default
new Bill({ billId: 1117753200140, paymentId: 12070160 }).getResult().amount; // 12000

// Find Bill's type by bill id and payment id
BillInfo({ billId: 7748317800142, paymentId: 1770160 }).getData().type; // تلفن ثابت
BillInfo({ billId: 9174639504124, paymentId: 12908197 }).getData().type; // برق
BillInfo({ billId: 2050327604613, paymentId: 1070189 }).getData().type; // آب
BillInfo({ billId: 9100074409151, paymentId: 12908190 }).getData().type; // تلفن همراه
BillInfo({ billId: 7748317800105, paymentId: 1770160 }).getData().type; // unknown
new Bill({ billId: 7748317800142, paymentId: 1770160 }).getResult().type; // تلفن ثابت
new Bill({ billId: 9174639504124, paymentId: 12908197 }).getResult().type; // برق
new Bill({ billId: 2050327604613, paymentId: 1070189 }).getResult().type; // آب
new Bill({ billId: 9100074409151, paymentId: 12908190 }).getResult().type; // تلفن همراه
new Bill({ billId: 7748317800105, paymentId: 1770160 }).getResult().type; // unknown

// Check Bill id validation
BillInfo({ billId: 7748317800142, paymentId: 1770160 }).getData().isValidBillId; // true
BillInfo({ billId: 2234322344613, paymentId: 1070189 }).getData().isValidBillId; // false
new Bill({ billId: 7748317800142, paymentId: 1770160 }).getResult().isValidBillId; // true
new Bill({ billId: 2234322344613, paymentId: 1070189 }).getResult().isValidBillId; // false

// Check Bill's payment id validation
BillInfo({ billId: 7748317800142, paymentId: 1770160 }).getData().isValidBillPayment; // true
BillInfo({ billId: 9174639504124, paymentId: 12908197 }).getData().isValidBillPayment; // false
new Bill({ billId: 7748317800142, paymentId: 1770160 }).getResult().isValidBillPayment; // true
new Bill({ billId: 9174639504124, paymentId: 12908197 }).getResult().isValidBillPayment; // false

// Check Bill id and payment id relations which is valid or not
BillInfo({ billId: 7748317800142, paymentId: 1770160 }).getData().isValid; // true
BillInfo({ billId: 2234322344613, paymentId: 1070189 }).getData().isValid; // false
new Bill({ billId: 7748317800142, paymentId: 1770160 }).getResult().isValid; // true
new Bill({ billId: 2234322344613, paymentId: 1070189 }).getResult().isValid; // false

// Get barcode from billId and paymentId
BillInfo({ billId: 7748317800142, paymentId: 1770160 }).getData().barcode; // 77483178001420001770160
BillInfo({ billId: 9174639504124, paymentId: 12908197 }).getData().barcode; // 917463950412400012908197
new Bill({ billId: 7748317800142, paymentId: 1770160 }).getResult().barcode; // 77483178001420001770160
new Bill({ billId: 9174639504124, paymentId: 12908197 }).getResult().barcode; // 917463950412400012908197

// Get bill bill id and payment id by bill's barcode
BillInfo({ barcode: "22343223446130001070189" }).findByBarcode(); // { billId: 2234322344613 , paymentId: 1070189 }
new Bill({ barcode: "22343223446130001070189" }).findByBarcode(); // { billId: 2234322344613 , paymentId: 1070189 }
```

### Fix Persian zero-width non-joiner(Replace spaces by half-space)
Expand All @@ -251,6 +258,12 @@ import { halfSpace } from "persian-tools2";
halfSpace("نمی ‌خواهی درخت ها را ببینیم؟") // "نمی‌خواهی درخت‌ها را ببینیم؟"
```

### Todo
- [ ] Write typescript document
- [ ] Complete Bill methods documents one by one
- [ ] Check Iranian iban number validation
- [ ] Find Bank's name by Iban number

## Contributing

Thank you for your interest in contributing! Please feel free to put up a PR for any issue or feature request.
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ module.exports = {
preset: "ts-jest",
testEnvironment: "node",
moduleFileExtensions: ["js", "ts", "json"],
collectCoverage: true
collectCoverage: true,
};
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export { default as URLfix } from "./modules/URLfix";
export { default as SortText } from "./modules/SortText";
export { default as halfSpace } from "./modules/halfSpace";
// Bill info
export { default as BillInfo } from "./modules/Bill";
export { default as Bill } from "./modules/bill";

export {
// Digits
Expand Down
146 changes: 65 additions & 81 deletions src/modules/bill.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type BillFaType =
export type BillTypes =
| "آب"
| "برق"
| "گاز"
Expand All @@ -8,144 +8,133 @@ type BillFaType =
| "سازمان مالیات"
| "جرایم راهنمایی و رانندگی"
| "unknown";
type Currency = "toman" | "rial";

interface IBillTypes {
[key: number]: BillFaType;
interface BillTypesModel {
[key: number]: BillTypes;
}

interface FindByBarcode {
billId: string;
paymentId: string;
const billTypes: BillTypesModel = {
1: "آب",
2: "برق",
3: "گاز",
4: "تلفن ثابت",
5: "تلفن همراه",
6: "عوارض شهرداری",
8: "سازمان مالیات",
9: "جرایم راهنمایی و رانندگی",
};

export type Currency = "toman" | "rial";
interface BillBarcodeModel {
billId: number;
paymentId: number;
}

interface IBillData {
interface BillResult {
// bill amount
amount: number;

// bill type
type: string;

// bill barcode
barcode: string;

// bill validation
isValid: boolean;

// is valid bill id that should be true if bill id and true
// Bill id validation
isValidBillId: boolean;

// id valid bill payment code
// payment id validation
isValidBillPayment: boolean;

// find billId and billPayment by barcode
findByBarcode: FindByBarcode;
}

interface Params {
billId?: number | string;
paymentId?: number | string;
interface BillParams {
billId?: number;
paymentId?: number;
currency?: Currency;
barcode?: string;
}

class Bill {
barcode: string | null;
currency: Currency;
billTypes: IBillTypes;
billId: string;
billPayment: string;
private readonly barcode: string | null;
private readonly currency: Currency;
private readonly billTypes: BillTypesModel;
private billId: number | null;
private billPayment: number | null;

constructor({ billId, paymentId, currency, barcode }: Params) {
constructor({ billId, paymentId, currency, barcode }: BillParams) {
this.barcode = barcode || null;
this.currency = currency || "toman";
this.billId = "";
this.billPayment = "";
this.billTypes = {
1: "آب",
2: "برق",
3: "گاز",
4: "تلفن ثابت",
5: "تلفن همراه",
6: "عوارض شهرداری",
8: "سازمان مالیات",
9: "جرایم راهنمایی و رانندگی",
};
this.billId = null;
this.billPayment = null;
this.billTypes = billTypes;

if (billId && paymentId) {
this.setId(String(billId));
this.setPeymentId(String(paymentId));
this.setId(billId);
this.setPaymentId(paymentId);
}
}

private setId(billId: string): void {
private setId(billId: number): void {
this.billId = billId;
}

private setPeymentId(billPayment: string): void {
private setPaymentId(billPayment: number): void {
this.billPayment = billPayment;
}

public setBarcode(barcode: string): void {
this.barcode = barcode;
}

public getBillAmount(): number {
public getAmount(): number {
const currency = this.currency == "rial" ? 1000 : 100;
const amount = parseInt(String(this.billPayment).slice(0, -5)) * currency;

return amount;
}

public getBillType(): BillFaType {
public getBillType(): BillTypes {
return this.billTypes[Number(String(this.billId)?.slice(-2, -1))] ?? "unknown";
}

public getBarcode(): string {
return this.billId + "000" + this.billPayment;
}
public findByBarcode(): FindByBarcode {
if (this.barcode) {
return {
billId: this.barcode.substr(0, 13),
paymentId: this.barcode.substr(16, 10),
};
} else {
return {
billId: "",
paymentId: "",
};
}
public findByBarcode(barcode?: string): BillBarcodeModel {
const $barcode = (barcode || this.barcode) as string;

return {
billId: Number($barcode.substr(0, 13)),
paymentId: Number($barcode.substr(16, 10)),
};
}

private verificationBillPayment(): boolean {
let paymentId = parseInt(this.billPayment, 10).toString();
const billId = parseInt(this.billId, 10).toString();
public verificationBillPayment(): boolean {
const billId = `${this.billId}`;
let paymentId = `${this.billPayment}`;

let result = false;
if (!paymentId || paymentId.length < 6) {
return result;
}
const firstControllBit = paymentId.charAt(paymentId.length - 2) + "";
const firstControlBit = paymentId.charAt(paymentId.length - 2) + "";
const secondControlBit = paymentId.charAt(paymentId.length - 1) + "";
paymentId = paymentId.substr(0, paymentId.length - 2);
result =
this.CalTheBit(paymentId) === Number(firstControllBit) &&
this.CalTheBit(billId + paymentId + firstControllBit) === Number(secondControlBit);
this.CalTheBit(paymentId) === Number(firstControlBit) &&
this.CalTheBit(billId + paymentId + firstControlBit) === Number(secondControlBit);

return result;
}

private verificationBillId(): boolean {
let newBillId = parseInt(this.billId, 10).toString();
public verificationBillId(): boolean {
let newBillId = `${this.billId}`;

let result = false;
if (!newBillId || newBillId.length < 6) {
return false;
}
const controlBit = newBillId.substr(newBillId.length - 1);
newBillId = newBillId.substr(0, newBillId.length - 1);
const tempResult = this.CalTheBit(newBillId);
result = tempResult === Number(controlBit);

const $result = this.CalTheBit(newBillId);
result = $result === Number(controlBit);

const billType = this.getBillType();

return result && billType !== "unknown";
}

Expand All @@ -166,14 +155,14 @@ class Bill {
return sum;
}

private verificationBill(): boolean {
public verificationBill(): boolean {
return this.verificationBillPayment() && this.verificationBillId();
}

public getData(): IBillData {
public getResult(): BillResult {
return {
// bill amount
amount: this.getBillAmount(),
amount: this.getAmount(),

// bill type
type: this.getBillType(),
Expand All @@ -189,13 +178,8 @@ class Bill {

// id valid bill payment code
isValidBillPayment: this.verificationBillPayment(),

// find billId and billPayment by barcode
findByBarcode: this.findByBarcode(),
};
}
}

const BillInstance = (data: Params): Bill => new Bill(data);

export default BillInstance;
export default Bill;
Loading

0 comments on commit 6c6b945

Please sign in to comment.