admin/modules/return.js

/* ============================================================================ *\
|| ########################################################################## ||
|| # Auction Software Marketplace          Release: 0.6   Build 0.7         # ||
|| # ---------------------------------------------------------------------- # ||
|| # License # 35YAHCNR9344X6O666C123AB                                     # ||
|| # ---------------------------------------------------------------------- # ||
|| # Copyright ©2014–2021 Develop Scripts LLC. All Rights Reserved          # ||
|| # This file may not be redistributed in whole or significant part.       # ||
|| # ------------- AUCTION SOFTWARE IS NOT FREE SOFTWARE ------------------ # ||
|| # http://www.auctionsoftwaremarketplace.com|support@auctionsoftware.com  # ||
|| # ---------------------------------------------------------------------- # ||
|| ########################################################################## ||
\* ============================================================================ */

const dateFormat = require('dateformat')
const md5 = require('md5')
const _ = require('underscore')
const moment = require('moment')

const mysqclass = require('./mysqli').default
/**
 * @class class to handle return functions
 */
class adminReturnModule {
    /**
     * fetch All Retunr Invoices
     * @param {object} req request data
     * @param {string} count count for the pagination
     * @returns {object} sql response
     */
    static async fetchInvoicesAll(req, count) {
        const mysql = {}
        mysql.where = ''
        const { action } = req.body
        const dateNow = dateFormat(new Date(), 'yyyy-mm-dd HH:MM:ss')
        if (action === 'pending') {
            mysql.where += 'and rc.paid = 0'
        } else if (action === 'completed') {
            mysql.where += 'and rc.paid = 1'
        }

        mysql.order = 'order by rc.id desc'
        const order = req.body.order === '' ? 'asc' : req.body.order
        const orderby = req.body.orderby === '' && !req.body.orderby ? '' : req.body.orderby
        if (orderby !== '') {
            mysql.order = ` order by ${orderby} ${order}`
        }
        if (req.body.searchterm !== '' && req.body.searchterm !== undefined) {
            let changed = req.body.searchterm.replace(/\\/g, '\\\\\\\\')
            changed = changed.replace(/"/g, '\\"')
            mysql.where += ` and (bn.return_invoice like "%${changed}%")`
        }
        const dateSortValue = req.body.fielddate ? req.body.fielddate : 'rc.paiddate'
        if (req.body.fromdate !== '' && req.body.fromdate !== undefined) {
            const date1 = dateFormat(
                new Date(
                    moment(req.body.fromdate, global.configurations.variables.time_format).format(),
                ),
                'yyyy-mm-dd',
            )
            mysql.where += ` and ${dateSortValue} >= "${date1} 00:00:00"`
        }
        if (req.body.todate !== '' && req.body.todate !== undefined) {
            const date2 = dateFormat(
                new Date(
                    moment(req.body.todate, global.configurations.variables.time_format).format(),
                ),
                'yyyy-mm-dd',
            )
            mysql.where += ` and ${dateSortValue} <= "${date2} 23:59:59"`
        }

        let row = ''
        const pagen = (req.body.page - 1) * req.body.limit
        const limitf = ` limit ${pagen},${req.body.limit}`
        mysql.limit = limitf
        mysql.dateNow = dateNow
        const escapeData = []
        if (count === 1) {
            row = 'return_get_all_invoices_limit'
        } else {
            row = 'return_get_all_invoices'
        }
        const strQuery = await mysqclass.mysqli(mysql, row)
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * Update status of the  Invoice
     * @param {number} pid return invoice ID which has to be marked as paid
     * @param {number} status status which has to be updated
     * @returns {object} sql response
     */
    static async invoiceStatusUpdate(status, pid) {
        const mysql = {}
        const escapeData = [status === 'paid' ? 1 : 0, pid]
        const strQuery = await mysqclass.mysqli(mysql, 'returninvoice_change_status')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

    /**
     * Update status of the  Invoice in Buynow entry
     * @param {number} pid return invoice ID which has to be marked as paid
     * @param {number} status status which has to be updated
     * @returns {object} sql response
     */
    static async invoiceBuynowStatusUpdate(status, pid) {
        const mysql = {}
        const escapeData = [status === 'paid' ? 1 : 0, pid]
        const strQuery = await mysqclass.mysqli(mysql, 'returninvoice_buynow_change_status')
        const dataReturn = await global.mysql.query(strQuery, escapeData)
        return dataReturn
    }

}

module.exports.default = adminReturnModule