diff --git a/db/changes/10491-august/00-invoiceInPdf.sql b/db/changes/10491-august/00-invoiceInPdf.sql new file mode 100644 index 000000000..d7dc038aa --- /dev/null +++ b/db/changes/10491-august/00-invoiceInPdf.sql @@ -0,0 +1,4 @@ +INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) + VALUES + ('InvoiceIn', 'invoiceInPdf', 'READ', 'ALLOW', 'ROLE', 'administrative'), + ('InvoiceIn', 'invoiceInEmail', 'WRITE', 'ALLOW', 'ROLE', 'administrative'), diff --git a/modules/invoiceIn/back/methods/invoice-in/invoiceInEmail.js b/modules/invoiceIn/back/methods/invoice-in/invoiceInEmail.js new file mode 100644 index 000000000..0768541a8 --- /dev/null +++ b/modules/invoiceIn/back/methods/invoice-in/invoiceInEmail.js @@ -0,0 +1,53 @@ +const {Email} = require('vn-print'); + +module.exports = Self => { + Self.remoteMethodCtx('invoiceInEmail', { + description: 'Sends the invoice in email with an attached PDF', + accessType: 'WRITE', + accepts: [ + { + arg: 'id', + type: 'number', + required: true, + description: 'The invoice id', + http: {source: 'path'} + }, + { + arg: 'recipient', + type: 'string', + description: 'The recipient email', + required: true, + }, + { + arg: 'recipientId', + type: 'number', + description: 'The recipient id to send to the recipient preferred language', + required: false + } + ], + returns: { + type: ['object'], + root: true + }, + http: { + path: '/:id/invoice-in-email', + verb: 'POST' + } + }); + + Self.invoiceInEmail = async ctx => { + const args = Object.assign({}, ctx.args); + const params = { + recipient: args.recipient, + lang: ctx.req.getLocale() + }; + + delete args.ctx; + for (const param in args) + params[param] = args[param]; + + const email = new Email('invoiceIn', params); + + return email.send(); + }; +}; diff --git a/modules/invoiceIn/back/methods/invoice-in/invoiceInPdf.js b/modules/invoiceIn/back/methods/invoice-in/invoiceInPdf.js new file mode 100644 index 000000000..e7962c93f --- /dev/null +++ b/modules/invoiceIn/back/methods/invoice-in/invoiceInPdf.js @@ -0,0 +1,50 @@ +const {Report} = require('vn-print'); + +module.exports = Self => { + Self.remoteMethodCtx('invoiceInPdf', { + description: 'Returns the invoiceIn pdf', + accessType: 'READ', + accepts: [ + { + arg: 'id', + type: 'number', + required: true, + description: 'The invoiceIn id', + http: {source: 'path'} + } + ], + returns: [ + { + arg: 'body', + type: 'file', + root: true + }, { + arg: 'Content-Type', + type: 'String', + http: {target: 'header'} + }, { + arg: 'Content-Disposition', + type: 'String', + http: {target: 'header'} + } + ], + http: { + path: '/:id/invoice-in-pdf', + verb: 'GET' + } + }); + + Self.invoiceInPdf = async(ctx, id) => { + const args = Object.assign({}, ctx.args); + const params = {lang: ctx.req.getLocale()}; + delete args.ctx; + + for (const param in args) + params[param] = args[param]; + + const report = new Report('invoiceIn', params); + const stream = await report.toPdfStream(); + + return [stream, 'application/pdf', `filename="doc-${id}.pdf"`]; + }; +}; diff --git a/modules/invoiceIn/back/models/invoice-in.js b/modules/invoiceIn/back/models/invoice-in.js index 3b5aa65d9..95ccc7b20 100644 --- a/modules/invoiceIn/back/models/invoice-in.js +++ b/modules/invoiceIn/back/models/invoice-in.js @@ -4,4 +4,6 @@ module.exports = Self => { require('../methods/invoice-in/clone')(Self); require('../methods/invoice-in/toBook')(Self); require('../methods/invoice-in/getTotals')(Self); + require('../methods/invoice-in/invoiceInPdf')(Self); + require('../methods/invoice-in/invoiceInEmail')(Self); }; diff --git a/modules/invoiceIn/back/models/invoice-in.json b/modules/invoiceIn/back/models/invoice-in.json index c6a736b06..fa8a1d8f8 100644 --- a/modules/invoiceIn/back/models/invoice-in.json +++ b/modules/invoiceIn/back/models/invoice-in.json @@ -94,6 +94,11 @@ "model": "Supplier", "foreignKey": "supplierFk" }, + "supplierContact": { + "type": "hasMany", + "model": "SupplierContact", + "foreignKey": "supplierFk" + }, "currency": { "type": "belongsTo", "model": "Currency", diff --git a/modules/invoiceIn/front/card/index.js b/modules/invoiceIn/front/card/index.js index 582c2abb8..c7ac08cc7 100644 --- a/modules/invoiceIn/front/card/index.js +++ b/modules/invoiceIn/front/card/index.js @@ -8,6 +8,14 @@ class Controller extends ModuleCard { { relation: 'supplier' }, + { + relation: 'supplierContact', + scope: { + where: { + email: {neq: null} + } + } + }, { relation: 'invoiceInDueDay' }, diff --git a/modules/invoiceIn/front/descriptor/index.html b/modules/invoiceIn/front/descriptor/index.html index 33f9ee8c6..819615c20 100644 --- a/modules/invoiceIn/front/descriptor/index.html +++ b/modules/invoiceIn/front/descriptor/index.html @@ -1,5 +1,5 @@ - @@ -10,7 +10,6 @@ translate> To book - Clone Invoice + + Show agricultural invoice as PDF + + + Send agricultural invoice as PDF +
@@ -37,7 +46,7 @@ - {{$ctrl.invoiceIn.supplier.nickname}} + {{$ctrl.invoiceIn.supplier.nickname}}
@@ -57,9 +66,9 @@ icon="icon-invoice-in"> - + - +
- - \ No newline at end of file + + + + + + Are you sure you want to send it? + + + + + + + + diff --git a/modules/invoiceIn/front/descriptor/index.js b/modules/invoiceIn/front/descriptor/index.js index cde324296..5cd00d743 100644 --- a/modules/invoiceIn/front/descriptor/index.js +++ b/modules/invoiceIn/front/descriptor/index.js @@ -96,6 +96,20 @@ class Controller extends Descriptor { .then(() => this.$state.reload()) .then(() => this.vnApp.showSuccess(this.$t('InvoiceIn booked'))); } + + showPdfInvoice() { + this.vnReport.show(`InvoiceIns/${this.id}/invoice-in-pdf`); + } + + sendPdfInvoice($data) { + if (!$data.email) + return this.vnApp.showError(this.$t(`The email can't be empty`)); + + return this.vnEmail.send(`InvoiceIns/${this.entity.id}/invoice-in-email`, { + recipient: $data.email, + recipientId: this.entity.supplier.id + }); + } } ngModule.vnComponent('vnInvoiceInDescriptor', { diff --git a/modules/invoiceIn/front/locale/es.yml b/modules/invoiceIn/front/locale/es.yml index 4f36b33fa..1deff32d3 100644 --- a/modules/invoiceIn/front/locale/es.yml +++ b/modules/invoiceIn/front/locale/es.yml @@ -19,3 +19,5 @@ To book: Contabilizar Total amount: Total importe Total net: Total neto Total stems: Total tallos +Show agricultural invoice as PDF: Ver factura agrícola como PDF +Send agricultural invoice as PDF: Enviar factura agrícola como PDF diff --git a/print/templates/email/delivery-note-link/locale/pt.yml b/print/templates/email/delivery-note-link/locale/pt.yml index 1aab4b6d8..9a70fc4cd 100644 --- a/print/templates/email/delivery-note-link/locale/pt.yml +++ b/print/templates/email/delivery-note-link/locale/pt.yml @@ -7,4 +7,4 @@ copyLink: 'Como alternativa, podes copiar o siguinte link no teu navegador:' poll: Si o deseja, podes responder nosso questionário de satiscação para ajudar-nos a prestar-vos um melhor serviço. Tua opinião é muito importante para nós! help: Cualquer dúvida que surja, no hesites em consultar-la, Estamos aqui para atender-te! -conclusion: Obrigado por tua atenção! \ No newline at end of file +conclusion: Obrigado por tua atenção! diff --git a/print/templates/email/invoiceIn/assets/css/import.js b/print/templates/email/invoiceIn/assets/css/import.js new file mode 100644 index 000000000..4b4bb7086 --- /dev/null +++ b/print/templates/email/invoiceIn/assets/css/import.js @@ -0,0 +1,11 @@ +const Stylesheet = require(`vn-print/core/stylesheet`); + +const path = require('path'); +const vnPrintPath = path.resolve('print'); + +module.exports = new Stylesheet([ + `${vnPrintPath}/common/css/spacing.css`, + `${vnPrintPath}/common/css/misc.css`, + `${vnPrintPath}/common/css/layout.css`, + `${vnPrintPath}/common/css/email.css`]) + .mergeStyles(); diff --git a/print/templates/email/invoiceIn/attachments.json b/print/templates/email/invoiceIn/attachments.json new file mode 100644 index 000000000..cd23d3f92 --- /dev/null +++ b/print/templates/email/invoiceIn/attachments.json @@ -0,0 +1,6 @@ +[ + { + "filename": "invoiceIn.pdf", + "component": "invoiceIn" + } +] diff --git a/print/templates/email/invoiceIn/invoiceIn.html b/print/templates/email/invoiceIn/invoiceIn.html new file mode 100644 index 000000000..65453ccd6 --- /dev/null +++ b/print/templates/email/invoiceIn/invoiceIn.html @@ -0,0 +1,47 @@ + + + + + + {{ $t('subject') }} + + + + + + + + +
+ +
+
+
+ +
+
+ +
+
+ +
+
+

{{ $t('title') }}

+

{{$t('dear')}},

+

+

+
+
+ +
+
+ +
+
+ +
+
+
+
+ + diff --git a/print/templates/email/invoiceIn/invoiceIn.js b/print/templates/email/invoiceIn/invoiceIn.js new file mode 100755 index 000000000..f7a472eb2 --- /dev/null +++ b/print/templates/email/invoiceIn/invoiceIn.js @@ -0,0 +1,11 @@ +const Component = require(`vn-print/core/component`); +const emailHeader = new Component('email-header'); +const emailFooter = new Component('email-footer'); + +module.exports = { + name: 'invoiceIn', + components: { + 'email-header': emailHeader.build(), + 'email-footer': emailFooter.build() + } +}; diff --git a/print/templates/email/invoiceIn/locale/en.yml b/print/templates/email/invoiceIn/locale/en.yml new file mode 100644 index 000000000..47ebc3966 --- /dev/null +++ b/print/templates/email/invoiceIn/locale/en.yml @@ -0,0 +1,5 @@ +subject: Your agricultural invoice +title: Your agricultural invoice +dear: Dear supplier +description: Attached you can find agricultural receipt generated from your last deliveries. Please return a signed and stamped copy to our administration department. +conclusion: Thanks for your attention! diff --git a/print/templates/email/invoiceIn/locale/es.yml b/print/templates/email/invoiceIn/locale/es.yml new file mode 100644 index 000000000..2698763cf --- /dev/null +++ b/print/templates/email/invoiceIn/locale/es.yml @@ -0,0 +1,5 @@ +subject: Tu factura agrícola +title: Tu factura agrícola +dear: Estimado proveedor +description: Adjunto puede encontrar recibo agrícola generado de sus últimas entregas. Por favor, devuelva una copia firmada y sellada a nuestro de departamento de administración. +conclusion: ¡Gracias por tu atención! diff --git a/print/templates/email/invoiceIn/locale/fr.yml b/print/templates/email/invoiceIn/locale/fr.yml new file mode 100644 index 000000000..1c38f3c25 --- /dev/null +++ b/print/templates/email/invoiceIn/locale/fr.yml @@ -0,0 +1,5 @@ +subject: Votre facture agricole +title: Votre facture agricole +dear: Cher Fournisseur +description: Vous trouverez en pièce jointe le reçu agricole généré à partir de vos dernières livraisons. Veuillez retourner une copie signée et tamponnée à notre service administratif. +conclusion: Merci pour votre attention! diff --git a/print/templates/email/invoiceIn/locale/pt.yml b/print/templates/email/invoiceIn/locale/pt.yml new file mode 100644 index 000000000..a43e3a79d --- /dev/null +++ b/print/templates/email/invoiceIn/locale/pt.yml @@ -0,0 +1,5 @@ +subject: A sua fatura agrícola +title: A sua fatura agrícola +dear: Caro Fornecedor +description: Em anexo encontra-se o recibo agrícola gerado a partir das suas últimas entregas. Por favor, devolva uma cópia assinada e carimbada ao nosso departamento de administração. +conclusion: Obrigado pela atenção. diff --git a/print/templates/reports/invoiceIn/assets/css/import.js b/print/templates/reports/invoiceIn/assets/css/import.js new file mode 100644 index 000000000..37a98dfdd --- /dev/null +++ b/print/templates/reports/invoiceIn/assets/css/import.js @@ -0,0 +1,12 @@ +const Stylesheet = require(`vn-print/core/stylesheet`); + +const path = require('path'); +const vnPrintPath = path.resolve('print'); + +module.exports = new Stylesheet([ + `${vnPrintPath}/common/css/spacing.css`, + `${vnPrintPath}/common/css/misc.css`, + `${vnPrintPath}/common/css/layout.css`, + `${vnPrintPath}/common/css/report.css`, + `${__dirname}/style.css`]) + .mergeStyles(); diff --git a/print/templates/reports/invoiceIn/assets/css/style.css b/print/templates/reports/invoiceIn/assets/css/style.css new file mode 100644 index 000000000..9fda2a613 --- /dev/null +++ b/print/templates/reports/invoiceIn/assets/css/style.css @@ -0,0 +1,42 @@ +h2 { + font-weight: 100; + color: #555 +} + +.table-title { + margin-bottom: 15px; + font-size: .8rem +} + +.table-title h2 { + margin: 0 15px 0 0 +} + +.ticket-info { + font-size: 22px +} + + +#nickname h2 { + max-width: 400px; + text-overflow: ellipsis; + white-space: nowrap; + overflow: hidden; +} + +#phytosanitary { + padding-right: 10px +} + +#phytosanitary .flag img { + width: 100% +} + +#phytosanitary .flag .flag-text { + padding-left: 10px; + box-sizing: border-box; +} + +.phytosanitary-info { + margin-top: 10px +} \ No newline at end of file diff --git a/print/templates/reports/invoiceIn/assets/images/europe.png b/print/templates/reports/invoiceIn/assets/images/europe.png new file mode 100644 index 000000000..673be92ae Binary files /dev/null and b/print/templates/reports/invoiceIn/assets/images/europe.png differ diff --git a/print/templates/reports/invoiceIn/invoiceIn.html b/print/templates/reports/invoiceIn/invoiceIn.html new file mode 100644 index 000000000..8919403b9 --- /dev/null +++ b/print/templates/reports/invoiceIn/invoiceIn.html @@ -0,0 +1,214 @@ + + + + + + + + + +
+ + + + + +
+
+
+
+
+

{{$t('title')}}

+ + + + + + + + + + + + + + + +
{{$t('supplierId')}}{{invoice.supplierId}}
{{$t('invoiceId')}}{{invoice.id}}
{{$t('date')}}{{invoice.created | date('%d-%m-%Y')}}
+
+
+
+
+
{{$t('invoiceData')}}
+
+

{{invoice.name}}

+
+ {{invoice.postalAddress}} +
+
+ {{invoice.postcodeCity}} +
+
+ {{$t('fiscalId')}}: {{invoice.nif}} +
+
+ {{$t('phone')}}: {{invoice.phone}} +
+
+
+
+
+ +
+
+
+

{{$t('invoiceId')}} +

+
+
+ {{entry.id}} +
+
+
+

{{$t('date')}}

+
+
+
+ {{entry.landed | date}} +
+
+ +
+

{{$t('reference')}}

+
+
+
+ {{entry.ref}} +
+
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
{{$t('item')}}{{$t('quantity')}}{{$t('buyingValue')}}{{$t('amount')}}
{{buy.name}}{{buy.quantity}}{{buy.buyingValue}}{{buyImport(buy) | currency('EUR', $i18n.locale)}}
+ + {{buy.tag5}} {{buy.value5}} + + + {{buy.tag6}} {{buy.value6}} + + + {{buy.tag7}} {{buy.value7}} + +
+ {{$t('subtotal')}} + {{entrySubtotal(entry) | currency('EUR', $i18n.locale)}}
+
+ + +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
{{$t('taxBreakdown')}}
{{$t('type')}} + {{$t('taxBase')}} + {{$t('tax')}}{{$t('fee')}}
{{tax.name}} + {{tax.taxableBase | currency('EUR', $i18n.locale)}} + {{tax.rate | percentage}}{{tax.vat | currency('EUR', $i18n.locale)}}
{{$t('subtotal')}} + {{sumTotal(taxes, 'taxableBase') | currency('EUR', $i18n.locale)}} + {{sumTotal(taxes, 'vat') | currency('EUR', $i18n.locale)}}
{{$t('total')}}{{taxTotal() | currency('EUR', $i18n.locale)}}
+ +
+
{{$t('notes')}}
+
+ {{invoice.footNotes}} +
+
+
+ + + +
+
+
+
{{$t('observations')}}
+
+
{{$t('payMethod')}}
+
{{invoice.payMethod}}
+
+
+
+
+ + +
+
+
+ + + +
+ + diff --git a/print/templates/reports/invoiceIn/invoiceIn.js b/print/templates/reports/invoiceIn/invoiceIn.js new file mode 100755 index 000000000..526c40fc6 --- /dev/null +++ b/print/templates/reports/invoiceIn/invoiceIn.js @@ -0,0 +1,94 @@ +const Component = require(`vn-print/core/component`); +const reportHeader = new Component('report-header'); +const reportFooter = new Component('report-footer'); + +module.exports = { + name: 'invoiceIn', + async serverPrefetch() { + this.invoice = await this.fetchInvoice(this.id); + this.taxes = await this.fetchTaxes(this.id); + + if (!this.invoice) + throw new Error('Something went wrong'); + + const entries = await this.fetchEntry(this.id); + const buys = await this.fetchBuy(this.id); + + const map = new Map(); + + for (let entry of entries) { + entry.buys = []; + + map.set(entry.id, entry); + } + + for (let buy of buys) { + const entry = map.get(buy.entryFk); + + if (entry) entry.buys.push(buy); + } + + this.entries = entries; + }, + computed: { + }, + methods: { + fetchInvoice(id) { + return this.findOneFromDef('invoice', [id]); + }, + fetchEntry(id) { + return this.rawSqlFromDef('entry', [id]); + }, + fetchBuy(id) { + return this.rawSqlFromDef('buy', [id]); + }, + async fetchTaxes(id) { + const taxes = await this.rawSqlFromDef(`taxes`, [id]); + return this.taxVat(taxes); + }, + buyImport(buy) { + return buy.quantity * buy.buyingValue; + }, + entrySubtotal(entry) { + let subTotal = 0.00; + for (let buy of entry.buys) + subTotal += this.buyImport(buy); + + return subTotal; + }, + sumTotal(rows, prop) { + let total = 0.00; + for (let row of rows) + total += parseFloat(row[prop]); + + return total; + }, + taxVat(taxes) { + for (tax of taxes) { + let vat = 0; + if (tax.rate && tax.taxableBase) + vat = (tax.rate / 100) * tax.taxableBase; + + tax.vat = vat; + } + + return taxes; + }, + taxTotal() { + const base = this.sumTotal(this.taxes, 'taxableBase'); + const vat = this.sumTotal(this.taxes, 'vat'); + + return base + vat; + } + }, + components: { + 'report-header': reportHeader.build(), + 'report-footer': reportFooter.build(), + }, + props: { + id: { + type: Number, + description: 'The invoice id' + } + } +}; diff --git a/print/templates/reports/invoiceIn/locale/en.yml b/print/templates/reports/invoiceIn/locale/en.yml new file mode 100644 index 000000000..92d3b0c2d --- /dev/null +++ b/print/templates/reports/invoiceIn/locale/en.yml @@ -0,0 +1,25 @@ +reportName: invoice +title: Agricultural invoice +invoiceId: Agricultural invoice +supplierId: Proveedor +invoiceData: Invoice data +reference: Reference +fiscalId: FI / NIF +phone: Phone +date: Date +item: Item +quantity: Qty. +concept: Concept +buyingValue: PSP/u +discount: Disc. +vat: VAT +amount: Amount +type: Type +taxBase: Tax base +tax: Tax +fee: Fee +total: Total +subtotal: Subtotal +taxBreakdown: Tax breakdown +observations: Observations +payMethod: Pay method diff --git a/print/templates/reports/invoiceIn/locale/es.yml b/print/templates/reports/invoiceIn/locale/es.yml new file mode 100644 index 000000000..f2fb28e64 --- /dev/null +++ b/print/templates/reports/invoiceIn/locale/es.yml @@ -0,0 +1,25 @@ +reportName: factura +title: Factura Agrícola +invoiceId: Factura Agrícola +supplierId: Proveedor +invoiceData: Datos de facturación +reference: Referencia +fiscalId: CIF / NIF +phone: Tlf +date: Fecha +item: Artículo +quantity: Cant. +concept: Concepto +buyingValue: PVP/u +discount: Dto. +vat: IVA +amount: Importe +type: Tipo +taxBase: Base imp. +tax: Tasa +fee: Cuota +total: Total +subtotal: Subtotal +taxBreakdown: Desglose impositivo +observations: Observaciones +payMethod: Método de pago diff --git a/print/templates/reports/invoiceIn/sql/buy.sql b/print/templates/reports/invoiceIn/sql/buy.sql new file mode 100644 index 000000000..28a80ff9c --- /dev/null +++ b/print/templates/reports/invoiceIn/sql/buy.sql @@ -0,0 +1,18 @@ +SELECT + b.id, + e.id entryFk, + it.name, + b.quantity, + b.buyingValue, + (b.quantity * b.buyingValue) total, + it.tag5, + it.value5, + it.tag6, + it.value6, + it.tag7, + it.value7 + FROM entry e + JOIN invoiceIn i ON i.id = e.invoiceInFk + JOIN buy b ON b.entryFk = e.id + JOIN item it ON it.id = b.itemFk + WHERE i.id = ? diff --git a/print/templates/reports/invoiceIn/sql/entry.sql b/print/templates/reports/invoiceIn/sql/entry.sql new file mode 100644 index 000000000..0b29cd81c --- /dev/null +++ b/print/templates/reports/invoiceIn/sql/entry.sql @@ -0,0 +1,8 @@ +SELECT + e.id, + t.landed, + e.ref + FROM entry e + JOIN invoiceIn i ON i.id = e.invoiceInFk + JOIN travel t ON t.id = e.travelFk + WHERE i.id = ? diff --git a/print/templates/reports/invoiceIn/sql/invoice.sql b/print/templates/reports/invoiceIn/sql/invoice.sql new file mode 100644 index 000000000..fe9ef1e9e --- /dev/null +++ b/print/templates/reports/invoiceIn/sql/invoice.sql @@ -0,0 +1,14 @@ +SELECT + i.id, + s.id supplierId, + i.created, + s.name, + s.street AS postalAddress, + s.nif, + s.phone, + p.name payMethod + FROM invoiceIn i + JOIN supplier s ON s.id = i.supplierFk + JOIN company c ON c.id = i.companyFk + JOIN payMethod p ON p.id = s.payMethodFk + WHERE i.id = ? diff --git a/print/templates/reports/invoiceIn/sql/taxes.sql b/print/templates/reports/invoiceIn/sql/taxes.sql new file mode 100644 index 000000000..20df33f83 --- /dev/null +++ b/print/templates/reports/invoiceIn/sql/taxes.sql @@ -0,0 +1,8 @@ +SELECT + ti.iva as name, + ti.PorcentajeIva as rate, + iit.taxableBase + FROM invoiceIn ii + JOIN invoiceInTax iit ON ii.id = iit.invoiceInFk + JOIN sage.TiposIva ti ON ti.CodigoIva = iit.taxTypeSageFk + WHERE ii.id = ?;