medusa-invoice
Version:
Generate invoice from Medusa
101 lines • 11.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const medusa_1 = require("@medusajs/medusa");
const document_invoice_settings_1 = require("../models/document-invoice-settings");
const utils_1 = require("@medusajs/utils");
class DocumentInvoiceSettingsService extends medusa_1.TransactionBaseService {
copySettingsIfPossible(newSettings, lastSettings) {
if (lastSettings) {
newSettings.invoice_forced_number = lastSettings.invoice_forced_number;
newSettings.invoice_number_format = lastSettings.invoice_number_format;
newSettings.invoice_template = lastSettings.invoice_template;
}
}
async getInvoiceForcedNumber() {
const lastDocumentInvoiceSettings = await this.getLastDocumentInvoiceSettings();
if (lastDocumentInvoiceSettings &&
lastDocumentInvoiceSettings.invoice_forced_number) {
const nextNumber = lastDocumentInvoiceSettings.invoice_forced_number.toString();
return nextNumber;
}
return undefined;
}
async resetForcedNumberByCreatingNewSettings() {
const documentInvoiceSettingsRepository = this.activeManager_.getRepository(document_invoice_settings_1.DocumentInvoiceSettings);
const newDocumentInvoiceSettings = this.activeManager_.create(document_invoice_settings_1.DocumentInvoiceSettings);
const lastDocumentInvoiceSettings = await this.getLastDocumentInvoiceSettings();
this.copySettingsIfPossible(newDocumentInvoiceSettings, lastDocumentInvoiceSettings);
newDocumentInvoiceSettings.invoice_forced_number = undefined;
const result = await documentInvoiceSettingsRepository.save(newDocumentInvoiceSettings);
return result;
}
async getLastDocumentInvoiceSettings() {
const documentInvoiceSettingsRepository = this.activeManager_.getRepository(document_invoice_settings_1.DocumentInvoiceSettings);
const lastDocumentInvoiceSettings = await documentInvoiceSettingsRepository
.createQueryBuilder("documentInvoiceSettings")
.orderBy("documentInvoiceSettings.created_at", "DESC")
.getOne();
if (lastDocumentInvoiceSettings === null) {
return undefined;
}
return lastDocumentInvoiceSettings;
}
async getInvoiceTemplate() {
const lastDocumentInvoiceSettings = await this.getLastDocumentInvoiceSettings();
if (lastDocumentInvoiceSettings) {
return lastDocumentInvoiceSettings.invoice_template;
}
return undefined;
}
async updateInvoiceForcedNumber(forcedNumber) {
if (forcedNumber && !isNaN(Number(forcedNumber))) {
const documentInvoiceSettingsRepository = this.activeManager_.getRepository(document_invoice_settings_1.DocumentInvoiceSettings);
const lastDocumentInvoiceSettings = await this.getLastDocumentInvoiceSettings();
const newDocumentInvoiceSettings = this.activeManager_.create(document_invoice_settings_1.DocumentInvoiceSettings);
this.copySettingsIfPossible(newDocumentInvoiceSettings, lastDocumentInvoiceSettings);
newDocumentInvoiceSettings.invoice_forced_number = parseInt(forcedNumber);
const result = await documentInvoiceSettingsRepository.save(newDocumentInvoiceSettings);
return result;
}
else {
throw new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, "You need to set proper number");
}
}
async updateInvoiceTemplate(invoiceTemplate) {
const documentInvoiceSettingsRepository = this.activeManager_.getRepository(document_invoice_settings_1.DocumentInvoiceSettings);
const lastDocumentInvoiceSettings = await this.getLastDocumentInvoiceSettings();
const newDocumentInvoiceSettings = this.activeManager_.create(document_invoice_settings_1.DocumentInvoiceSettings);
this.copySettingsIfPossible(newDocumentInvoiceSettings, lastDocumentInvoiceSettings);
newDocumentInvoiceSettings.invoice_template = invoiceTemplate;
const result = await documentInvoiceSettingsRepository.save(newDocumentInvoiceSettings);
return result;
}
async updateFormatNumber(newFormatNumber) {
const documentInvoiceSettingsRepository = this.activeManager_.getRepository(document_invoice_settings_1.DocumentInvoiceSettings);
const lastDocumentInvoiceSettings = await this.getLastDocumentInvoiceSettings();
const newDocumentInvoiceSettings = this.activeManager_.create(document_invoice_settings_1.DocumentInvoiceSettings);
this.copySettingsIfPossible(newDocumentInvoiceSettings, lastDocumentInvoiceSettings);
newDocumentInvoiceSettings.invoice_number_format = newFormatNumber;
const result = await documentInvoiceSettingsRepository.save(newDocumentInvoiceSettings);
return result;
}
async updateSettings(newFormatNumber, forcedNumber, invoiceTemplate) {
const documentInvoiceSettingsRepository = this.activeManager_.getRepository(document_invoice_settings_1.DocumentInvoiceSettings);
const newDocumentInvoiceSettings = this.activeManager_.create(document_invoice_settings_1.DocumentInvoiceSettings);
const lastDocumentInvoiceSettings = await this.getLastDocumentInvoiceSettings();
this.copySettingsIfPossible(newDocumentInvoiceSettings, lastDocumentInvoiceSettings);
if (newFormatNumber) {
newDocumentInvoiceSettings.invoice_number_format = newFormatNumber;
}
if (forcedNumber !== undefined && !isNaN(Number(forcedNumber))) {
newDocumentInvoiceSettings.invoice_forced_number = parseInt(forcedNumber);
}
if (invoiceTemplate) {
newDocumentInvoiceSettings.invoice_template = invoiceTemplate;
}
const result = await documentInvoiceSettingsRepository.save(newDocumentInvoiceSettings);
return result;
}
}
exports.default = DocumentInvoiceSettingsService;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZG9jdW1lbnQtaW52b2ljZS1zZXR0aW5ncy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9zZXJ2aWNlcy9kb2N1bWVudC1pbnZvaWNlLXNldHRpbmdzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQUEsNkNBQTBEO0FBQzFELG1GQUE4RTtBQUM5RSwyQ0FBOEM7QUFHOUMsTUFBcUIsOEJBQStCLFNBQVEsK0JBQXNCO0lBQ3hFLHNCQUFzQixDQUM1QixXQUFvQyxFQUNwQyxZQUFzQztRQUV0QyxJQUFJLFlBQVksRUFBRTtZQUNoQixXQUFXLENBQUMscUJBQXFCLEdBQUcsWUFBWSxDQUFDLHFCQUFxQixDQUFDO1lBQ3ZFLFdBQVcsQ0FBQyxxQkFBcUIsR0FBRyxZQUFZLENBQUMscUJBQXFCLENBQUM7WUFDdkUsV0FBVyxDQUFDLGdCQUFnQixHQUFHLFlBQVksQ0FBQyxnQkFBZ0IsQ0FBQztTQUM5RDtJQUNILENBQUM7SUFFRCxLQUFLLENBQUMsc0JBQXNCO1FBQzFCLE1BQU0sMkJBQTJCLEdBQy9CLE1BQU0sSUFBSSxDQUFDL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