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

Commit

Permalink
Merge pull request #95 from Tolfix/dev
Browse files Browse the repository at this point in the history
v2.8
  • Loading branch information
Tolfx authored Mar 12, 2022
2 parents 50d0b0f + 7229546 commit 64a31a6
Show file tree
Hide file tree
Showing 20 changed files with 313 additions and 47 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cpg-api",
"version": "v2.7",
"version": "v2.8",
"description": "Central Payment Gateway",
"main": "./build/Main.js",
"dependencies": {
Expand Down
24 changes: 23 additions & 1 deletion src/Database/Models/Quotes.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IQuotes } from "@interface/Quotes.interface";
import Logger from "../../Lib/Logger";
import GetText from "../../Translation/GetText";
import { A_CC_Payments } from "../../Types/PaymentMethod";
import { currencyCodes } from "../../Lib/Currencies";

const QuotesSchema = new Schema
(
Expand All @@ -27,7 +28,6 @@ const QuotesSchema = new Schema
type: [
{
name: String,
tax_rate: Number,
price: Number,
quantity: Number,
}
Expand All @@ -50,6 +50,28 @@ const QuotesSchema = new Schema
default: "",
},

currency: {
type: String,
required: true,
enum: currencyCodes,
default: "EUR",
},

tax_rate: {
type: Number,
default: 0,
},

accepted: {
type: Boolean,
default: false
},

declined: {
type: Boolean,
default: false
},

payment_method: {
type: String,
enum: [...A_CC_Payments],
Expand Down
8 changes: 4 additions & 4 deletions src/Database/Models/Transactions.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import mongoose, { model, Schema } from "mongoose"
import mongoose, { Document, model, Schema } from "mongoose"
import increment from "mongoose-auto-increment";
import { Default_Language, MongoDB_URI } from "../../Config";
import { IDTransactions } from "@interface/Transactions.interface";
import { ITransactions } from "@interface/Transactions.interface";
import Logger from "../../Lib/Logger";
import GetText from "../../Translation/GetText";
import { A_CC_Payments } from "../../Types/PaymentMethod";
Expand Down Expand Up @@ -58,7 +58,7 @@ const TransactionsSchema = new Schema
);

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

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

export default TransactionsModel;
2 changes: 1 addition & 1 deletion src/Email/Templates/Invoices/Invoice.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default async (invoice: IInvoice & IInvoiceMethods, customer: ICustomer)
</p>
${CPG_Customer_Panel_Domain ? `
<p>
<a href="${CPG_Customer_Panel_Domain}/invoices/${invoice.id}">View Invoice</a>
<a href="${CPG_Customer_Panel_Domain}/invoices?id=${invoice.id}">View Invoice</a>
</p>
` : ''}
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/Email/Templates/Invoices/LateInvoice.Template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export default async (invoice: IInvoice & IInvoiceMethods, customer: ICustomer)
</p>
${CPG_Customer_Panel_Domain ? `
<p>
<a href="${CPG_Customer_Panel_Domain}/invoices/${invoice.id}">View Invoice</a>
<a href="${CPG_Customer_Panel_Domain}/invoices?id=${invoice.id}">View Invoice</a>
</p>
` : ''}
</div>
Expand Down
27 changes: 27 additions & 0 deletions src/Email/Templates/Methods/QuotesItems.print.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { IQuotes } from "@interface/Quotes.interface";
import { GetCurrencySymbol } from "../../../Lib/Currencies";
import GetTableStyle from "../CSS/GetTableStyle";

export default async function printQuotesItemsTable(quote: IQuotes)
{
return `
<table style="${GetTableStyle}">
<thead>
<tr>
<th>Product</th>
<th>Quantity</th>
<th>Price</th>
</tr>
</thead>
<tbody>
${(await Promise.all(quote.items.map(async item => `
<tr>
<td>${item.name}</td>
<td>${item.quantity}</td>
<td>${item.price} ${GetCurrencySymbol(quote.currency)}</td>
</tr>
`))).join('')}
</tbody>
</table>
`
}
2 changes: 1 addition & 1 deletion src/Email/Templates/Orders/NewOrderCreated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default async (order: IOrder, customer: ICustomer) => await UseStyles(str
${CPG_Customer_Panel_Domain ? `
<p>
<a href="${CPG_Customer_Panel_Domain}/orders/${order.id}">View Order</a>
<a href="${CPG_Customer_Panel_Domain}/orders?id=${order.id}">View Order</a>
</p>
` : ''}
Expand Down
2 changes: 1 addition & 1 deletion src/Email/Templates/Payments/PaymentFailed.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export = async (invoice: IInvoice, customer: ICustomer) => UseStyles(stripIndent
${CPG_Customer_Panel_Domain ? `
<p>
<a href="${CPG_Customer_Panel_Domain}/invoices/${invoice.uid}">View invoice</a>
<a href="${CPG_Customer_Panel_Domain}/invoices?id=${invoice.id}">View invoice</a>
</p>
` : ''}
Expand Down
23 changes: 23 additions & 0 deletions src/Email/Templates/Quotes/Quote.accepted.template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { stripIndents } from "common-tags";
import { CPG_Customer_Panel_Domain } from "../../../Config";
import { ICustomer } from "@interface/Customer.interface";
import getFullName from "../../../Lib/Customers/getFullName";
import UseStyles from "../General/UseStyles";
import { IQuotes } from "@interface/Quotes.interface";

export default async (quote: IQuotes, customer: ICustomer) => await UseStyles(stripIndents`
<div>
<h1>Hello ${getFullName(customer)}.</h1>
<p>
This is a notice that quote <strong>#${quote.id}</strong> has been accepted.
</p>
<p>
We will generate a invoice for you shortly.
</p>
${CPG_Customer_Panel_Domain ? `
<p>
<a href="${CPG_Customer_Panel_Domain}/quotes?id=${quote.id}">View quote</a>.
</p>
` : ''}
</div>
`);
39 changes: 39 additions & 0 deletions src/Email/Templates/Quotes/Quote.create.template.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { stripIndents } from "common-tags";
import { Company_Name, CPG_Customer_Panel_Domain } from "../../../Config";
import { ICustomer } from "@interface/Customer.interface";
import getFullName from "../../../Lib/Customers/getFullName";
import UseStyles from "../General/UseStyles";
import { IQuotes } from "@interface/Quotes.interface";
import printQuotesItemsTable from "../Methods/QuotesItems.print";

export default async (quote: IQuotes, customer: ICustomer) => await UseStyles(stripIndents`
<div>
<h1>Hello ${getFullName(customer)}.</h1>
<p>
This is a notice that <strong>${await Company_Name()}</strong> has sent a <strong>quote</strong> to you.
</p>
<p>
<strong>Memo:</strong> ${quote.memo}
</p>
<p>
<strong>Due Date:</strong> ${quote.due_date}
</p>
<p>
<strong>Payment Method:</strong> ${quote.payment_method}
</p>
${await printQuotesItemsTable(quote)}
<p>
<strong>
Total:
</strong>
${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))}
</p>
${CPG_Customer_Panel_Domain ? `
<p>
<a href="${CPG_Customer_Panel_Domain}/quotes?id=${quote.id}">View quote</a> to accept or decline.
</p>
` : ''}
</div>
`);
2 changes: 1 addition & 1 deletion src/Email/Templates/Transaction/NewTransaction.template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export = async (t: ITransactions, c: ICustomer, charged = false) => UseStyles(st
</p>
${CPG_Customer_Panel_Domain ? `
<p>
<a href="${CPG_Customer_Panel_Domain}/transactions/${t.uid}">View Transaction</a>
<a href="${CPG_Customer_Panel_Domain}/transactions?uid=${t.uid}">View Transaction</a>
</p>
` : ''}
</div>
Expand Down
9 changes: 7 additions & 2 deletions src/Interfaces/Quotes.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { TPaymentCurrency } from "../Lib/Currencies";
import { ICustomer } from "./Customer.interface";
import { IInvoice } from "./Invoice.interface";
import { IPayments } from "./Payments.interface";
import { IPromotionsCodes } from "./PromotionsCodes.interface";
Expand All @@ -6,21 +8,24 @@ export interface IQuotes
{
uid: `QUO_${string}`;
id: number;
customer_uid: string;
customer_uid: ICustomer["uid"];
items: IQuoteItem[];
promotion_codes: IPromotionsCodes["id"][] | [];
due_date: string;
memo: string;
payment_method: keyof IPayments;
notified: boolean;
created_invoice: boolean;
tax_rate: number;
currency: TPaymentCurrency;
accepted: boolean;
declined: boolean;
invoice_uid?: IInvoice["uid"] | IInvoice["id"];
}

export interface IQuoteItem
{
name: string;
tax_rate: number;
price: number;
quantity: number;
}
5 changes: 2 additions & 3 deletions src/Interfaces/Transactions.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { IInvoice } from "./Invoice.interface";
*/
export interface ITransactions
{
id: number;
uid: `TRAN_${string}`;
customer_uid: ICustomer["uid"];
invoice_uid: IInvoice["uid"];
Expand All @@ -21,6 +22,4 @@ export interface ITransactions
amount: IInvoice["amount"];
currency: TPaymentCurrency;
fees: number;
}

export interface IDTransactions extends ITransactions, Document {}
}
2 changes: 1 addition & 1 deletion src/Lib/Invoices/CreatePDFInvoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export default function createPDFInvoice(invoice: IInvoice): Promise<string>
)
data["client"]["custom1"] = `<br/><strong>Innehar ${(await Company_Tax_Registered()) ? "" : "inte"} F-Skattsedel</strong>`;

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

Expand Down
4 changes: 3 additions & 1 deletion src/Lib/Orders/newInvoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ export async function createInvoiceFromOrder(order: IOrder)
customer_uid: Customer_Id,
dates: <IInvoice_Dates>{
due_date: order.dates.next_recycle,
invoice_date: dateFormat.format(new Date(), "YYYY-MM-DD"),
// Possible fix to issue #94
invoice_date: order.dates.last_recycle,
// invoice_date: dateFormat.format(new Date(), "YYYY-MM-DD"),
},
amount: items.reduce((acc, item) =>
{
Expand Down
18 changes: 9 additions & 9 deletions src/Lib/Quotes/CreateQuotePdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default function createQuotePdf(quote: IQuotes): Promise<string>

},
"translate": {
"invoice": `Quote`,
"invoice": `Quote #${quote.id}`,
"number": GetText().invoice.txt_Number,
"date": GetText().invoice.txt_Date,
"due-date": GetText().invoice.txt_DueDate,
Expand All @@ -49,11 +49,11 @@ export default function createQuotePdf(quote: IQuotes): Promise<string>
"margin-bottom": 25,
},
"sender": {
"company": Company_Name,
"address": Company_Address,
"zip": Company_Zip,
"city": Company_City,
"country": Company_Country,
"company": (await Company_Name()),
"address": await Company_Address(),
"zip": await Company_Zip(),
"city": await Company_City(),
"country": await Company_Country(),
},
"client": {
"company": Customer.billing.company ?? `${Customer.personal.first_name} ${Customer.personal.last_name}`,
Expand All @@ -71,7 +71,7 @@ export default function createQuotePdf(quote: IQuotes): Promise<string>
return {
"quantity": item.quantity,
"description": item.name,
"tax-rate": item.tax_rate,
"tax-rate": quote.tax_rate,
"price": item.price
}
}),
Expand All @@ -84,9 +84,9 @@ export default function createQuotePdf(quote: IQuotes): Promise<string>
data["client"]["custom1"] = `<br/><strong>Innehar ${await Company_Tax_Registered() ? "" : "inte"} F-Skattsedel</strong>`;


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

if(PDF_Template_Url !== "")
// @ts-ignore
Expand Down
35 changes: 35 additions & 0 deletions src/Lib/Quotes/QuoteToInvoice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { IQuotes } from "@interface/Quotes.interface";
import InvoiceModel from "../../Database/Models/Invoices.model";
import { idInvoice } from "../Generator";
import dateFormat from "date-and-time";
import mainEvent from "../../Events/Main.event";

export default async (quote: IQuotes) =>
{
// Converts quote to invoice
const invoice = await (new InvoiceModel({
uid: idInvoice(),
customer_uid: quote.customer_uid,
items: quote.items.map(item => ({
notes: item.name,
amount: item.price,
quantity: item.quantity,
})),
dates: {
invoice_date: dateFormat.format(new Date(), "YYYY-MM-DD"),
due_date: quote.due_date,
},
amount: quote.items.reduce((acc, item) => acc + item.price * item.quantity, 0),
currency: quote.currency,
tax_rate: quote.tax_rate,
notified: false,
transactions: [],
paid: false,
notes: quote.memo,
payment_method: quote.payment_method,
}).save());

mainEvent.emit("invoice_created", invoice);

return invoice;
}
Loading

0 comments on commit 64a31a6

Please sign in to comment.