module.exports = Self => {
    Self.remoteMethod('summary', {
        description: 'The invoiceIn summary',
        accessType: 'READ',
        accepts: [{
            arg: 'id',
            type: 'number',
            required: true,
            description: 'The invoiceIn id',
            http: {source: 'path'}
        }],
        returns: {
            type: 'object',
            root: true
        },
        http: {
            path: `/:id/summary`,
            verb: 'GET'
        }
    });

    Self.summary = async(id, options) => {
        const models = Self.app.models;
        const myOptions = {};

        if (typeof options == 'object')
            Object.assign(myOptions, options);

        const filter = {
            include: [
                {
                    relation: 'company',
                    scope: {
                        fields: ['id', 'code']
                    }
                },
                {
                    relation: 'supplier',
                    scope: {
                        fields: ['id', 'name']
                    }
                },
                {
                    relation: 'sageWithholding',
                    scope: {
                        fields: ['withholding']
                    }
                },
                {
                    relation: 'invoiceInDueDay',
                    scope: {
                        fields: [
                            'id',
                            'invoiceInFk',
                            'dueDated',
                            'bankFk',
                            'amount',
                            'foreignValue'],
                        include: [{
                            relation: 'bank',
                            scope: {
                                fields: ['bank']
                            }
                        }]
                    }
                },
                {
                    relation: 'invoiceInIntrastat',
                    scope: {
                        fields: [
                            'id',
                            'invoiceInFk',
                            'net',
                            'intrastatFk',
                            'amount',
                            'stems',
                            'countryFk',
                            'statisticalValue'],
                        include: [{
                            relation: 'intrastat',
                            scope: {
                                fields: [
                                    'id',
                                    'description']
                            }
                        },
                        {
                            relation: 'country',
                            scope: {
                                fields: ['code']
                            }
                        }]
                    }
                },
                {
                    relation: 'invoiceInTax',
                    scope: {
                        fields: [
                            'id',
                            'invoiceInFk',
                            'taxableBase',
                            'expenseFk',
                            'taxTypeSageFk',
                            'transactionTypeSageFk',
                            'foreignValue'],
                        include: [{
                            relation: 'transactionTypeSage',
                            scope: {
                                fields: ['transaction']
                            }
                        },
                        {
                            relation: 'taxTypeSage',
                            scope: {
                                fields: ['vat', 'rate']
                            }
                        }]
                    }
                },
                {
                    relation: 'expenseDeductible',
                    scope: {
                        fields: ['id', 'name', 'taxTypeFk']
                    }
                },
                {
                    relation: 'currency',
                    scope: {
                        fields: ['id', 'code']

                    }
                }
            ]
        };
        let summaryObj = await models.InvoiceIn.findById(id, filter, myOptions);

        summaryObj.totals = await models.InvoiceIn.getTotals(id, myOptions);
        return summaryObj;
    };
};