module.exports = Self => {
    Self.remoteMethod('getClaimableFromTicket', {
        description: 'Gets the claimable sales for a client',
        accessType: 'READ',
        accepts: [{
            arg: 'ticketFk',
            type: 'Number',
            required: true,
            description: 'ticketFk'
        }],
        returns: {
            type: 'string',
            root: true
        },
        http: {
            path: `/getClaimableFromTicket`,
            verb: 'GET'
        }
    });

    Self.getClaimableFromTicket = async(ticketFk, options) => {
        const myOptions = {};

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

        const query = `
            SELECT 
                s.id AS saleFk, 
                t.id AS ticketFk, 
                t.landed,
                s.concept, 
                s.itemFk, 
                s.quantity, 
                s.price, 
                s.discount, 
                t.nickname
            FROM vn.ticket t 
                INNER JOIN vn.sale s ON s.ticketFk = t.id
                LEFT JOIN vn.claimBeginning cb ON cb.saleFk = s.id

            WHERE (t.landed) >= TIMESTAMPADD(DAY, -7, CURDATE()) 
                AND t.id = ? AND cb.id IS NULL
            ORDER BY t.landed DESC, t.id DESC`;

        const claimableSales = await Self.rawSql(query, [ticketFk], myOptions);

        return claimableSales;
    };
};