This repository has been archived by the owner on May 6, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from Tolfix/dev
v3.3
- Loading branch information
Showing
13 changed files
with
179 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Default_Language, d_Days } from "../../Config"; | ||
import OrderModel from "../../Database/Models/Orders.model"; | ||
import Logger from "../../Lib/Logger"; | ||
import GetText from "../../Translation/GetText"; | ||
import dateFormat from "date-and-time"; | ||
import nextRecycleDate from "../../Lib/Dates/DateCycle"; | ||
import { createInvoiceFromOrder } from "../../Lib/Orders/newInvoice"; | ||
import { InvoiceCreatedReport } from "../../Email/Reports/InvoiceReport"; | ||
|
||
// Logger.info(`Checking orders..`); | ||
export function cron_createNewInvoicesFromOrders() | ||
{ | ||
Logger.info(GetText(Default_Language).cron.txt_Orders_Checking); | ||
// Check if the order needs to create a new invoice if order.dates.next_recycle is withing 14 days | ||
OrderModel.find({ | ||
order_status: "active", | ||
// order_status: { | ||
// $not: /fraud|cancelled|draft|refunded/g | ||
// } | ||
}).then(async orders => | ||
{ | ||
const newInvoices = []; | ||
// orders.forEach(async order => { | ||
for await(const order of orders) | ||
{ | ||
Logger.info(GetText(Default_Language).cron.txt_Order_Checking(order.id)); | ||
// Logger.info(`Checking order ${order.id}`); | ||
// Check if order.order_status is not "cancelled" or "fraud" | ||
if(order.dates.next_recycle) | ||
if(dateFormat.parse(order.dates.next_recycle, "YYYY-MM-DD").getTime() - new Date().getTime() <= d_Days * 24 * 60 * 60 * 1000) | ||
{ | ||
const temptNextRecycle = order.dates.next_recycle; | ||
order.dates.last_recycle = temptNextRecycle; | ||
// Update order.dates.next_recycle | ||
order.dates.next_recycle = dateFormat.format(nextRecycleDate( | ||
dateFormat.parse(temptNextRecycle, "YYYY-MM-DD"), order.billing_cycle ?? "monthly") | ||
, "YYYY-MM-DD"); | ||
// Create a new invoice | ||
const newInvoice = await createInvoiceFromOrder(order); | ||
newInvoices.push(newInvoice); | ||
|
||
// Save the invoice in order.invoices array | ||
order.invoices.push(newInvoice.id); | ||
|
||
// mark order updated in dates | ||
order.markModified("dates"); | ||
order.markModified("invoices"); | ||
// Save the order | ||
await order.save(); | ||
} | ||
if(newInvoices.length > 0) | ||
await InvoiceCreatedReport(newInvoices); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,11 @@ | ||
import { CronJob } from "cron"; | ||
import OrderModel from "../Database/Models/Orders.model"; | ||
import Logger from "../Lib/Logger"; | ||
import dateFormat from "date-and-time"; | ||
import { createInvoiceFromOrder } from "../Lib/Orders/newInvoice"; | ||
import nextRecycleDate from "../Lib/Dates/DateCycle"; | ||
import { Default_Language, d_Days } from "../Config"; | ||
import { InvoiceCreatedReport } from "../Email/Reports/InvoiceReport"; | ||
import GetText from "../Translation/GetText"; | ||
import { cron_createNewInvoicesFromOrders } from "./Methods/Orders.cron.methods"; | ||
|
||
export = function Cron_Orders() | ||
{ | ||
// Every hour | ||
new CronJob("0 12 * * *", () => | ||
{ | ||
Logger.info(GetText(Default_Language).cron.txt_Orders_Checking); | ||
// Logger.info(`Checking orders..`); | ||
|
||
// Check if the order needs to create a new invoice if order.dates.next_recycle is withing 14 days | ||
OrderModel.find({ | ||
order_status: "active", | ||
// order_status: { | ||
// $not: /fraud|cancelled|draft|refunded/g | ||
// } | ||
}).then(async orders => | ||
{ | ||
const newInvoices = []; | ||
// orders.forEach(async order => { | ||
for await(const order of orders) | ||
{ | ||
Logger.info(GetText(Default_Language).cron.txt_Order_Checking(order.id)); | ||
// Logger.info(`Checking order ${order.id}`); | ||
// Check if order.order_status is not "cancelled" or "fraud" | ||
if(order.dates.next_recycle) | ||
if(dateFormat.parse(order.dates.next_recycle, "YYYY-MM-DD").getTime() - new Date().getTime() <= d_Days * 24 * 60 * 60 * 1000) | ||
{ | ||
const temptNextRecycle = order.dates.next_recycle; | ||
order.dates.last_recycle = temptNextRecycle; | ||
// Update order.dates.next_recycle | ||
order.dates.next_recycle = dateFormat.format(nextRecycleDate( | ||
dateFormat.parse(temptNextRecycle, "YYYY-MM-DD"), order.billing_cycle ?? "monthly") | ||
, "YYYY-MM-DD"); | ||
// Create a new invoice | ||
const newInvoice = await createInvoiceFromOrder(order); | ||
newInvoices.push(newInvoice); | ||
|
||
// Save the invoice in order.invoices array | ||
order.invoices.push(newInvoice.id); | ||
|
||
// mark order updated in dates | ||
order.markModified("dates"); | ||
order.markModified("invoices"); | ||
// Save the order | ||
await order.save(); | ||
} | ||
if(newInvoices.length > 0) | ||
await InvoiceCreatedReport(newInvoices); | ||
} | ||
}); | ||
|
||
cron_createNewInvoicesFromOrders(); | ||
}, null, true, "Europe/Stockholm"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.