const pool = require('../db/connection');


const getpurchases = async (req, res) => 
{   
    try{
    const result = await pool.query('SELECT * From getpurchases()');
     const purchases = result.rows.map(re => ({
        id_purchase_dt: re.id_purchase_dt, 
        id_expense :re.id_expense  , 
        id_product :re.id_product ,
        product_name: re.product_name,
        quantity: re.quantity ,
        delivered: re.delivered ,
        id_tax :re.id_tax ,
        total : re.total
        
    }));

    res.json({
        message: "Se obtuvieron todos los Purchases details",
        data:purchases
    });

    }catch(error)
    {
    console.error(error);
    res.status(500).json({ message: 'Error al obtener los purchases details.',error });   
    }
};
const purchaseid = async (req, res) => 
{   
    try{
    const {id} = req.params;
    const {checking} = req.body;

    console.log(id,checking);
    const result = await pool.query('SELECT * FROM purchaseentry($1,$2) as status',[id,checking]);;
    const newentry = result.rows[0].status;
    res.json({
        message: 'Se obtuvo el Purchases details', newentry
        
    });
    }catch(error)
    {
    console.error(error);
    res.status(500).json({ message: 'Error al obtener el Purchases details',error });   
    }
};

module.exports = {getpurchases,purchaseid};