Skip to content
This repository was archived by the owner on May 6, 2022. It is now read-only.

Commit 64a31a6

Browse files
authored
Merge pull request #95 from Tolfix/dev
v2.8
2 parents 50d0b0f + 7229546 commit 64a31a6

20 files changed

+313
-47
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cpg-api",
3-
"version": "v2.7",
3+
"version": "v2.8",
44
"description": "Central Payment Gateway",
55
"main": "./build/Main.js",
66
"dependencies": {

src/Database/Models/Quotes.model.ts

+23-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { IQuotes } from "@interface/Quotes.interface";
55
import Logger from "../../Lib/Logger";
66
import GetText from "../../Translation/GetText";
77
import { A_CC_Payments } from "../../Types/PaymentMethod";
8+
import { currencyCodes } from "../../Lib/Currencies";
89

910
const QuotesSchema = new Schema
1011
(
@@ -27,7 +28,6 @@ const QuotesSchema = new Schema
2728
type: [
2829
{
2930
name: String,
30-
tax_rate: Number,
3131
price: Number,
3232
quantity: Number,
3333
}
@@ -50,6 +50,28 @@ const QuotesSchema = new Schema
5050
default: "",
5151
},
5252

53+
currency: {
54+
type: String,
55+
required: true,
56+
enum: currencyCodes,
57+
default: "EUR",
58+
},
59+
60+
tax_rate: {
61+
type: Number,
62+
default: 0,
63+
},
64+
65+
accepted: {
66+
type: Boolean,
67+
default: false
68+
},
69+
70+
declined: {
71+
type: Boolean,
72+
default: false
73+
},
74+
5375
payment_method: {
5476
type: String,
5577
enum: [...A_CC_Payments],

src/Database/Models/Transactions.model.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import mongoose, { model, Schema } from "mongoose"
1+
import mongoose, { Document, model, Schema } from "mongoose"
22
import increment from "mongoose-auto-increment";
33
import { Default_Language, MongoDB_URI } from "../../Config";
4-
import { IDTransactions } from "@interface/Transactions.interface";
4+
import { ITransactions } from "@interface/Transactions.interface";
55
import Logger from "../../Lib/Logger";
66
import GetText from "../../Translation/GetText";
77
import { A_CC_Payments } from "../../Types/PaymentMethod";
@@ -58,7 +58,7 @@ const TransactionsSchema = new Schema
5858
);
5959

6060
// Log when a transaction is created
61-
TransactionsSchema.post('save', function(doc: IDTransactions)
61+
TransactionsSchema.post('save', function(doc: ITransactions & Document)
6262
{
6363
Logger.db(GetText(Default_Language).database.txt_Model_Created(doc.modelName, doc.uid));
6464
// Logger.db(`Created transaction ${doc.uid}`);
@@ -74,6 +74,6 @@ TransactionsSchema.plugin(increment.plugin, {
7474
incrementBy: 1
7575
});
7676

77-
const TransactionsModel = model<IDTransactions>("transactions", TransactionsSchema);
77+
const TransactionsModel = model<ITransactions & Document>("transactions", TransactionsSchema);
7878

7979
export default TransactionsModel;

src/Email/Templates/Invoices/Invoice.template.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export default async (invoice: IInvoice & IInvoiceMethods, customer: ICustomer)
7878
</p>
7979
${CPG_Customer_Panel_Domain ? `
8080
<p>
81-
<a href="${CPG_Customer_Panel_Domain}/invoices/${invoice.id}">View Invoice</a>
81+
<a href="${CPG_Customer_Panel_Domain}/invoices?id=${invoice.id}">View Invoice</a>
8282
</p>
8383
` : ''}
8484
</div>

src/Email/Templates/Invoices/LateInvoice.Template.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export default async (invoice: IInvoice & IInvoiceMethods, customer: ICustomer)
7878
</p>
7979
${CPG_Customer_Panel_Domain ? `
8080
<p>
81-
<a href="${CPG_Customer_Panel_Domain}/invoices/${invoice.id}">View Invoice</a>
81+
<a href="${CPG_Customer_Panel_Domain}/invoices?id=${invoice.id}">View Invoice</a>
8282
</p>
8383
` : ''}
8484
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { IQuotes } from "@interface/Quotes.interface";
2+
import { GetCurrencySymbol } from "../../../Lib/Currencies";
3+
import GetTableStyle from "../CSS/GetTableStyle";
4+
5+
export default async function printQuotesItemsTable(quote: IQuotes)
6+
{
7+
return `
8+
<table style="${GetTableStyle}">
9+
<thead>
10+
<tr>
11+
<th>Product</th>
12+
<th>Quantity</th>
13+
<th>Price</th>
14+
</tr>
15+
</thead>
16+
<tbody>
17+
${(await Promise.all(quote.items.map(async item => `
18+
<tr>
19+
<td>${item.name}</td>
20+
<td>${item.quantity}</td>
21+
<td>${item.price} ${GetCurrencySymbol(quote.currency)}</td>
22+
</tr>
23+
`))).join('')}
24+
</tbody>
25+
</table>
26+
`
27+
}

src/Email/Templates/Orders/NewOrderCreated.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export default async (order: IOrder, customer: ICustomer) => await UseStyles(str
5252
5353
${CPG_Customer_Panel_Domain ? `
5454
<p>
55-
<a href="${CPG_Customer_Panel_Domain}/orders/${order.id}">View Order</a>
55+
<a href="${CPG_Customer_Panel_Domain}/orders?id=${order.id}">View Order</a>
5656
</p>
5757
` : ''}
5858

src/Email/Templates/Payments/PaymentFailed.template.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export = async (invoice: IInvoice, customer: ICustomer) => UseStyles(stripIndent
4141
${CPG_Customer_Panel_Domain ? `
4242
4343
<p>
44-
<a href="${CPG_Customer_Panel_Domain}/invoices/${invoice.uid}">View invoice</a>
44+
<a href="${CPG_Customer_Panel_Domain}/invoices?id=${invoice.id}">View invoice</a>
4545
</p>
4646
4747
` : ''}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { stripIndents } from "common-tags";
2+
import { CPG_Customer_Panel_Domain } from "../../../Config";
3+
import { ICustomer } from "@interface/Customer.interface";
4+
import getFullName from "../../../Lib/Customers/getFullName";
5+
import UseStyles from "../General/UseStyles";
6+
import { IQuotes } from "@interface/Quotes.interface";
7+
8+
export default async (quote: IQuotes, customer: ICustomer) => await UseStyles(stripIndents`
9+
<div>
10+
<h1>Hello ${getFullName(customer)}.</h1>
11+
<p>
12+
This is a notice that quote <strong>#${quote.id}</strong> has been accepted.
13+
</p>
14+
<p>
15+
We will generate a invoice for you shortly.
16+
</p>
17+
${CPG_Customer_Panel_Domain ? `
18+
<p>
19+
<a href="${CPG_Customer_Panel_Domain}/quotes?id=${quote.id}">View quote</a>.
20+
</p>
21+
` : ''}
22+
</div>
23+
`);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { stripIndents } from "common-tags";
2+
import { Company_Name, CPG_Customer_Panel_Domain } from "../../../Config";
3+
import { ICustomer } from "@interface/Customer.interface";
4+
import getFullName from "../../../Lib/Customers/getFullName";
5+
import UseStyles from "../General/UseStyles";
6+
import { IQuotes } from "@interface/Quotes.interface";
7+
import printQuotesItemsTable from "../Methods/QuotesItems.print";
8+
9+
export default async (quote: IQuotes, customer: ICustomer) => await UseStyles(stripIndents`
10+
<div>
11+
<h1>Hello ${getFullName(customer)}.</h1>
12+
<p>
13+
This is a notice that <strong>${await Company_Name()}</strong> has sent a <strong>quote</strong> to you.
14+
</p>
15+
<p>
16+
<strong>Memo:</strong> ${quote.memo}
17+
</p>
18+
<p>
19+
<strong>Due Date:</strong> ${quote.due_date}
20+
</p>
21+
<p>
22+
<strong>Payment Method:</strong> ${quote.payment_method}
23+
</p>
24+
25+
${await printQuotesItemsTable(quote)}
26+
27+
<p>
28+
<strong>
29+
Total:
30+
</strong>
31+
${quote.items.reduce((total, item) => total + (item.price * item.quantity), 0) + ((quote.tax_rate/100) * quote.items.reduce((total, item) => total + (item.price * item.quantity), 0))}
32+
</p>
33+
${CPG_Customer_Panel_Domain ? `
34+
<p>
35+
<a href="${CPG_Customer_Panel_Domain}/quotes?id=${quote.id}">View quote</a> to accept or decline.
36+
</p>
37+
` : ''}
38+
</div>
39+
`);

src/Email/Templates/Transaction/NewTransaction.template.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export = async (t: ITransactions, c: ICustomer, charged = false) => UseStyles(st
2929
</p>
3030
${CPG_Customer_Panel_Domain ? `
3131
<p>
32-
<a href="${CPG_Customer_Panel_Domain}/transactions/${t.uid}">View Transaction</a>
32+
<a href="${CPG_Customer_Panel_Domain}/transactions?uid=${t.uid}">View Transaction</a>
3333
</p>
3434
` : ''}
3535
</div>

src/Interfaces/Quotes.interface.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { TPaymentCurrency } from "../Lib/Currencies";
2+
import { ICustomer } from "./Customer.interface";
13
import { IInvoice } from "./Invoice.interface";
24
import { IPayments } from "./Payments.interface";
35
import { IPromotionsCodes } from "./PromotionsCodes.interface";
@@ -6,21 +8,24 @@ export interface IQuotes
68
{
79
uid: `QUO_${string}`;
810
id: number;
9-
customer_uid: string;
11+
customer_uid: ICustomer["uid"];
1012
items: IQuoteItem[];
1113
promotion_codes: IPromotionsCodes["id"][] | [];
1214
due_date: string;
1315
memo: string;
1416
payment_method: keyof IPayments;
1517
notified: boolean;
1618
created_invoice: boolean;
19+
tax_rate: number;
20+
currency: TPaymentCurrency;
21+
accepted: boolean;
22+
declined: boolean;
1723
invoice_uid?: IInvoice["uid"] | IInvoice["id"];
1824
}
1925

2026
export interface IQuoteItem
2127
{
2228
name: string;
23-
tax_rate: number;
2429
price: number;
2530
quantity: number;
2631
}

src/Interfaces/Transactions.interface.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { IInvoice } from "./Invoice.interface";
1313
*/
1414
export interface ITransactions
1515
{
16+
id: number;
1617
uid: `TRAN_${string}`;
1718
customer_uid: ICustomer["uid"];
1819
invoice_uid: IInvoice["uid"];
@@ -21,6 +22,4 @@ export interface ITransactions
2122
amount: IInvoice["amount"];
2223
currency: TPaymentCurrency;
2324
fees: number;
24-
}
25-
26-
export interface IDTransactions extends ITransactions, Document {}
25+
}

src/Lib/Invoices/CreatePDFInvoice.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ export default function createPDFInvoice(invoice: IInvoice): Promise<string>
147147
)
148148
data["client"]["custom1"] = `<br/><strong>Innehar ${(await Company_Tax_Registered()) ? "" : "inte"} F-Skattsedel</strong>`;
149149

150-
if(Company_Logo_Url && PDF_Template_Url === "")
150+
if(await Company_Logo_Url() !== "" && PDF_Template_Url === "")
151151
// @ts-ignore
152152
data["images"]["logo"] = await Company_Logo_Url();
153153

src/Lib/Orders/newInvoice.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ export async function createInvoiceFromOrder(order: IOrder)
9090
customer_uid: Customer_Id,
9191
dates: <IInvoice_Dates>{
9292
due_date: order.dates.next_recycle,
93-
invoice_date: dateFormat.format(new Date(), "YYYY-MM-DD"),
93+
// Possible fix to issue #94
94+
invoice_date: order.dates.last_recycle,
95+
// invoice_date: dateFormat.format(new Date(), "YYYY-MM-DD"),
9496
},
9597
amount: items.reduce((acc, item) =>
9698
{

src/Lib/Quotes/CreateQuotePdf.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export default function createQuotePdf(quote: IQuotes): Promise<string>
2929

3030
},
3131
"translate": {
32-
"invoice": `Quote`,
32+
"invoice": `Quote #${quote.id}`,
3333
"number": GetText().invoice.txt_Number,
3434
"date": GetText().invoice.txt_Date,
3535
"due-date": GetText().invoice.txt_DueDate,
@@ -49,11 +49,11 @@ export default function createQuotePdf(quote: IQuotes): Promise<string>
4949
"margin-bottom": 25,
5050
},
5151
"sender": {
52-
"company": Company_Name,
53-
"address": Company_Address,
54-
"zip": Company_Zip,
55-
"city": Company_City,
56-
"country": Company_Country,
52+
"company": (await Company_Name()),
53+
"address": await Company_Address(),
54+
"zip": await Company_Zip(),
55+
"city": await Company_City(),
56+
"country": await Company_Country(),
5757
},
5858
"client": {
5959
"company": Customer.billing.company ?? `${Customer.personal.first_name} ${Customer.personal.last_name}`,
@@ -71,7 +71,7 @@ export default function createQuotePdf(quote: IQuotes): Promise<string>
7171
return {
7272
"quantity": item.quantity,
7373
"description": item.name,
74-
"tax-rate": item.tax_rate,
74+
"tax-rate": quote.tax_rate,
7575
"price": item.price
7676
}
7777
}),
@@ -84,9 +84,9 @@ export default function createQuotePdf(quote: IQuotes): Promise<string>
8484
data["client"]["custom1"] = `<br/><strong>Innehar ${await Company_Tax_Registered() ? "" : "inte"} F-Skattsedel</strong>`;
8585

8686

87-
if(Company_Logo_Url && PDF_Template_Url === "")
87+
if(await Company_Logo_Url() !== "" && PDF_Template_Url === "")
8888
// @ts-ignore
89-
data["images"]["logo"] = Company_Logo_Url;
89+
data["images"]["logo"] = await Company_Logo_Url();
9090

9191
if(PDF_Template_Url !== "")
9292
// @ts-ignore

src/Lib/Quotes/QuoteToInvoice.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { IQuotes } from "@interface/Quotes.interface";
2+
import InvoiceModel from "../../Database/Models/Invoices.model";
3+
import { idInvoice } from "../Generator";
4+
import dateFormat from "date-and-time";
5+
import mainEvent from "../../Events/Main.event";
6+
7+
export default async (quote: IQuotes) =>
8+
{
9+
// Converts quote to invoice
10+
const invoice = await (new InvoiceModel({
11+
uid: idInvoice(),
12+
customer_uid: quote.customer_uid,
13+
items: quote.items.map(item => ({
14+
notes: item.name,
15+
amount: item.price,
16+
quantity: item.quantity,
17+
})),
18+
dates: {
19+
invoice_date: dateFormat.format(new Date(), "YYYY-MM-DD"),
20+
due_date: quote.due_date,
21+
},
22+
amount: quote.items.reduce((acc, item) => acc + item.price * item.quantity, 0),
23+
currency: quote.currency,
24+
tax_rate: quote.tax_rate,
25+
notified: false,
26+
transactions: [],
27+
paid: false,
28+
notes: quote.memo,
29+
payment_method: quote.payment_method,
30+
}).save());
31+
32+
mainEvent.emit("invoice_created", invoice);
33+
34+
return invoice;
35+
}

0 commit comments

Comments
 (0)