mirror of
https://github.com/wekan/wekan.git
synced 2026-11-25 17:45:18 -05:00
- The General/ level is gone: its 109 files moved up into the folders of the same name, and its topic table is the "Shared topics" section of Features.md. - Accessibility is one folder, docs/Features/Accessibility/, with the Admin Panel's accessibility page in it. - Every folder's main page is README.md (110 X/X.md pages renamed with git mv, five merged with an existing README.md, 26 index pages added for folders that had none), so a folder opened on GitHub shows its page. - The 73 "# Moved" forwarding pages are deleted, with the folders that held only them; every link that went to one goes to its final page. - docs/Features/README.md has the website's "Features in WeKan" overview, each feature linking to its page here. 1,129 Markdown links in 262 files and 352 doc paths in code, tests, compose, snap and release files follow. tests/docsFeaturesLayout.test.cjs fails when General/ or a "# Moved" page comes back, a folder has no README.md, or a relative link in the docs or README.md does not resolve. Thanks to xet7 !
77 lines
3.1 KiB
JavaScript
77 lines
3.1 KiB
JavaScript
// Board View / Shop: an order as a Budget and billing invoice
|
|
// (docs/Features/Board-Header/Board-Views/Shop.md, docs/Features/Billing/README.md).
|
|
//
|
|
// When Budget and billing is on for the shop's owner - the board admin whose
|
|
// settings take the orders - server/shop.js shopPlaceOrder makes the order an
|
|
// invoice of the shop's board as well as a card: billing's own invoice
|
|
// collection, numbering (INV-<year>-<number>, counted over every invoice of
|
|
// the year, exactly as server/billing.js insertNumbered counts), issuer
|
|
// profile, VAT and due date, with the ORDER's payment reference, so the card,
|
|
// the invoice and the payment all name the same reference. The invoice is
|
|
// marked sent: the buyer has it, in the order's confirmation. Its lines are
|
|
// product lines naming the product cards (models/lib/shop.js invoiceLinesOf).
|
|
import { BillingInvoices, BillingProfiles } from '/models/billing';
|
|
|
|
const { normalizeProfile, invoiceNumber, defaultDueDate, isInvoiceLineKind } = require('/models/lib/billing');
|
|
const { invoiceLinesOf } = require('/models/lib/shop');
|
|
|
|
function compact(doc) {
|
|
const out = {};
|
|
for (const [k, v] of Object.entries(doc)) if (v !== undefined && v !== null && v !== '') out[k] = v;
|
|
return out;
|
|
}
|
|
|
|
async function boardProfile(boardId) {
|
|
const doc = await BillingProfiles.findOneAsync({ boardId });
|
|
return doc ? doc.profile : normalizeProfile({});
|
|
}
|
|
|
|
/**
|
|
* @param {Object} order { number, reference, currency, netMinor, vatPercent, vatMinor, totalMinor, lines }
|
|
* @returns {{ invoiceId, number }}
|
|
*/
|
|
export async function createOrderInvoice({ boardId, ownerId, order, contact, today = new Date().toISOString().slice(0, 10) }) {
|
|
const profile = await boardProfile(boardId);
|
|
const lines = invoiceLinesOf(order).map(compact);
|
|
if (!lines.every(line => isInvoiceLineKind(line.kind))) throw new Error('Unknown invoice line kind');
|
|
const year = today.slice(0, 4);
|
|
for (let attempt = 0; attempt < 5; attempt += 1) {
|
|
const count = await BillingInvoices.find({ number: { $regex: `^(INV|CRN)-${year}-` } }).countAsync();
|
|
const number = invoiceNumber(year, count + 1 + attempt, 'invoice');
|
|
const doc = compact({
|
|
boardId,
|
|
number,
|
|
kind: 'invoice',
|
|
status: 'sent',
|
|
statusChangedAt: new Date(),
|
|
currency: order.currency,
|
|
issueDate: today,
|
|
dueDate: defaultDueDate(today, profile.paymentTermsDays),
|
|
customer: String(contact.name || '').slice(0, 500),
|
|
reference: order.number,
|
|
notes: `${order.number}`,
|
|
lines,
|
|
netMinor: order.netMinor,
|
|
vatPercent: order.vatPercent || 0,
|
|
vatMinor: order.vatMinor,
|
|
totalMinor: order.totalMinor,
|
|
totalHours: 0,
|
|
paymentReference: order.reference || undefined,
|
|
issuer: profile,
|
|
payments: [],
|
|
createdBy: ownerId,
|
|
});
|
|
try {
|
|
const invoiceId = await BillingInvoices.insertAsync(doc);
|
|
return { invoiceId, number };
|
|
} catch (e) {
|
|
if (!/duplicate key|E11000/i.test(String(e && e.message))) throw e;
|
|
}
|
|
}
|
|
throw new Error('Could not number the invoice');
|
|
}
|
|
|
|
export async function removeOrderInvoice(invoiceId) {
|
|
if (invoiceId) await BillingInvoices.removeAsync({ _id: invoiceId });
|
|
}
|