Skip to content

Commit c96ebcf

Browse files
committed
recent invoice generation code for node-scheduler changed
1 parent 3f278cb commit c96ebcf

File tree

5 files changed

+83
-56
lines changed

5 files changed

+83
-56
lines changed

frontend/src/app/invoice/invoice-recent/invoice-recent.component.ts

+28-32
Original file line numberDiff line numberDiff line change
@@ -71,40 +71,36 @@ export class InvoiceRecentComponent implements OnInit {
7171
this.invoiceService.getRecentInvoiceDB(this.paginator)
7272
.subscribe(
7373
(res: Invoice[]) => {
74-
if (res.length == 0) {
75-
this.generateNewInvoice();
76-
} else {
77-
_.each(res, (invoice: Invoice) => {
78-
let customer: Customer;
79-
this.customerService.getCustomerDetails(invoice.customer_id)
80-
.subscribe(
81-
(res: Customer) => {
82-
if (res['status']) {
83-
customer = res;
84-
customer.productData = [];
85-
if (invoice.productList.length > 0) {
86-
_.each(invoice.productList, (element) => {
87-
this.productService.getProductById(element)
88-
.subscribe(
89-
(res: Product) => {
90-
customer["productData"].push(res);
91-
}
92-
)
93-
});
94-
}
95-
this.areaService.getAreaById(customer.area)
96-
.subscribe(
97-
(res) => {
98-
customer["areaData"] = res;
99-
},
100-
)
101-
invoice.customerData = customer;
102-
this.invoiceList.push(invoice);
74+
_.each(res, (invoice: Invoice) => {
75+
let customer: Customer;
76+
this.customerService.getCustomerDetails(invoice.customer_id)
77+
.subscribe(
78+
(res: Customer) => {
79+
if (res['status']) {
80+
customer = res;
81+
customer.productData = [];
82+
if (invoice.productList.length > 0) {
83+
_.each(invoice.productList, (element) => {
84+
this.productService.getProductById(element)
85+
.subscribe(
86+
(res: Product) => {
87+
customer["productData"].push(res);
88+
}
89+
)
90+
});
10391
}
92+
this.areaService.getAreaById(customer.area)
93+
.subscribe(
94+
(res) => {
95+
customer["areaData"] = res;
96+
},
97+
)
98+
invoice.customerData = customer;
99+
this.invoiceList.push(invoice);
104100
}
105-
)
106-
});
107-
}
101+
}
102+
)
103+
});
108104
},
109105
(err) => {
110106

node-api/app/controllers/area.controller.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { Router, Request, Response } from 'express';
2-
import { AreaModel } from '../database/models/area.model';
1+
import {Router, Request, Response} from 'express';
2+
import {AreaModel} from '../database/models/area.model';
33

44

55
export class AreaController {
6-
constructor() { }
6+
constructor() {
7+
}
78

89
//create new areas
910
static create(res: Response, data: any) {
@@ -13,9 +14,9 @@ export class AreaController {
1314
});
1415
area.save(function (err) {
1516
if (err) {
16-
res.send({ status: false });
17+
res.send({status: false});
1718
} else {
18-
res.send({ status: true });
19+
res.send({status: true});
1920
}
2021
});
2122
}
@@ -32,11 +33,11 @@ export class AreaController {
3233

3334
//changing status - active/inactive
3435
static changeStatus(res: Response, data: any) {
35-
AreaModel.update({ _id: data.id }, { $set: { status: data.status } }, function (err) {
36+
AreaModel.update({_id: data.id}, {$set: {status: data.status}}, function (err) {
3637
if (err) {
37-
res.send({ status: false });
38+
res.send({status: false});
3839
} else {
39-
res.send({ status: true });
40+
res.send({status: true});
4041
}
4142
});
4243
}
@@ -52,16 +53,16 @@ export class AreaController {
5253

5354
//update area
5455
static update(res: Response, data: any) {
55-
AreaModel.update({ _id: data.id }, {
56+
AreaModel.update({_id: data.id}, {
5657
$set: {
5758
name: data.name,
5859
status: true
5960
}
6061
}, function (err) {
6162
if (err) {
62-
res.send({ status: false });
63+
res.send({status: false});
6364
} else {
64-
res.send({ status: true });
65+
res.send({status: true});
6566
}
6667
});
6768
}
+34-10
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,46 @@
11
import {Router, Request, Response} from 'express';
22
import * as _ from 'underscore';
33
import {CustomerModel} from '../database/models/customer.model';
4+
import {AllInvoiceModel, RecentInvoiceModel} from '../database/models/invoice.model';
5+
46

57
export class DeamonController {
68
constructor() {
79
}
810

911
static autoGenerateInvoice() {
10-
CustomerModel.find({
11-
$and: [
12-
{isGenerateInvoiceMonthly: true},
13-
{
14-
productList: {
15-
$exists: true, $not: {$size: 0}
12+
/*
13+
clean recent invoice and push data into all invoice
14+
*/
15+
RecentInvoiceModel.find({}, (err, data) => {
16+
_.each(data, (invoice) => {
17+
//save to all InvoiceDB
18+
let newInvoice = new AllInvoiceModel({
19+
customer_id: invoice["customer_id"],
20+
payment_due_date: invoice["payment_due_date"],
21+
amount_due: invoice["amount_due"],
22+
status: invoice["status"],
23+
total: invoice["total"],
24+
discount: invoice["discount"],
25+
invoice_created_date: invoice["invoice_created_date"],
26+
paid_date: invoice["paid_date"],
27+
productList: invoice["productList"],
28+
amount_partially_paid: invoice["amount_partially_paid"],
29+
type: "all"
30+
});
31+
32+
newInvoice.save((err, newData) => {
33+
if (err) {
34+
console.log("Error in allInvoice save");
35+
} else {
36+
RecentInvoiceModel.find({_id: invoice['_id']}).remove((err) => {
37+
if (!err) {
38+
console.log("removed recent invoice");
39+
}
40+
});
1641
}
17-
}
18-
]
19-
})
42+
});
43+
});
44+
});
2045
}
21-
2246
}

node-api/app/controllers/payDateCounter.controller.ts

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {Router, Request, Response} from 'express';
22
declare var Date: any;
33
import {PayDateCounterModel} from '../database/models/payDateCounter.model';
44

5-
65
export class PayDateCounterController {
76
constructor() {
87
}

node-api/app/server.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,15 @@ app.use(bodyParser.urlencoded({limit: '50mb', extended: true}));
2424
app.use('/api', ApiRoute);
2525

2626
// handling a daemon job
27-
let rule = {hour: 12, minute: 26, dayOfMonth: 23, month: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]};
28-
schedule.scheduleJob(rule, function () {
27+
let date = new Date();
28+
let rule = {hour: 16, minute: 5, dayOfMonth: 29, month: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]};
29+
30+
// setInterval(() => {
31+
// DeamonController.autoGenerateInvoice();
32+
// DeamonController.cleanRecentInvoiceDB();
33+
// }, 10000)
34+
35+
schedule.scheduleJob(rule, () => {
2936
DeamonController.autoGenerateInvoice();
3037
});
3138

0 commit comments

Comments
 (0)