const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; const buildFilter = require('vn-loopback/util/filter').buildFilter; const mergeFilters = require('vn-loopback/util/filter').mergeFilters; module.exports = Self => { Self.remoteMethodCtx('getBuyList', { description: 'Returns buys for editing of one entry', accessType: 'READ', accepts: [{ arg: 'entryFk', type: 'number', required: true, description: 'The entry id', http: {source: 'path'} }, { arg: 'filter', type: 'object', description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string' }, { arg: 'isIgnored', type: 'boolean', description: 'check if the buy is ignored', http: {source: 'query'} }, { arg: 'itemFk', type: 'number', description: 'item id', http: {source: 'query'} }, { arg: 'name', type: 'string', description: 'item name', http: {source: 'query'} }, { arg: 'size', type: 'number', description: 'item size', http: {source: 'query'} }, { arg: 'stickers', type: 'number', description: 'sticker quantity', http: {source: 'query'} }, { arg: 'packagingFk', type: 'number', description: 'packaging id', http: {source: 'query'} }, { arg: 'weight', type: 'number', description: 'weight', http: {source: 'query'} }, { arg: 'packing', type: 'number', description: 'packing quantity', http: {source: 'query'} }, { arg: 'grouping', type: 'number', description: 'grouping quantity', http: {source: 'query'} }, { arg: 'quantity', type: 'number', http: {source: 'query'} }, { arg: 'buyingValue', type: 'number', http: {source: 'query'} }, { arg: 'amount', type: 'number', description: 'buying value * quantity', http: {source: 'query'} }, { arg: 'price2', type: 'number', description: 'price for the package', http: {source: 'query'} }, { arg: 'price3', type: 'number', description: 'price for the box', http: {source: 'query'} }, { arg: 'minPrice', type: 'number', description: 'item minimum price', http: {source: 'query'} }, { arg: 'packingOut', type: 'number', description: 'quantity of package on a vn box', http: {source: 'query'} }, { arg: 'comment', type: 'string', description: 'item comment', http: {source: 'query'} }, { arg: 'subName', type: 'string', description: 'supplier name', http: {source: 'query'} }, { arg: 'subName', type: 'string', description: 'supplier name', http: {source: 'query'} }, { arg: 'company_name', type: 'string', description: 'company name', http: {source: 'query'} }, { arg: 'groupBy', type: 'string', description: 'group by', http: {source: 'query'} }, ], returns: { type: ['object'], root: true }, http: { path: `/:entryFk/getBuyList`, verb: 'GET' } }); Self.getBuyList = async(ctx, entryFk, filter, options) => { const myOptions = {}; if (typeof options == 'object') Object.assign(myOptions, options); let conn = Self.dataSource.connector; let where = buildFilter(ctx.args, (param, value) => { switch (param) { case 'name': case 'subName': case 'company_name': case 'comment': return {[param]: {like: `%${value}%`}}; case 'size': case 'isIgnored': case 'itemFk': case 'stickers': case 'packagingFk': case 'weight': case 'packing': case 'grouping': case 'quantity': case 'buyingValue': case 'amount': case 'price2': case 'price3': case 'packingOut': case 'minPrice': return {[param]: value}; } }); filter = mergeFilters(filter, {where}); let stmts = []; let stmt; const selectFields = `b.id, b.isIgnored, b.itemFk, b.printedStickers, b.stickers, b.packagingFk, b.weight, b.packing, b.groupingMode, b.grouping, b.quantity, b.buyingValue, ROUND(b.buyingValue * b.quantity, 2) amount, b.isChecked, b.price2, b.price3, i.name, i.size, i.minPrice, i.hasMinPrice, i.packingOut, i.comment, i.subName, i.tag5, i.value5, i.tag6, i.value6, i.tag7, i.value7, i.tag8, i.value8, i.tag9, i.value9, i.tag10, i.value10, s.company_name, ik.hexJson`; const groupByFields = `SUM(b.printedStickers) printedStickers, SUM(b.stickers) stickers, SUM(b.weight) weight, SUM(b.quantity) quantity, SUM(ROUND(b.buyingValue * b.quantity, 2)) amount `; const groupBy = ctx.args.groupBy; stmt = new ParameterizedSQL( `SELECT * FROM( SELECT ${ groupBy ? groupByFields : selectFields} FROM item i LEFT JOIN ink ik ON ik.id = i.inkFk LEFT JOIN buy b ON b.itemFk = i.id LEFT JOIN edi.ekt e ON e.id = b.ektFk LEFT JOIN edi.supplier s ON e.pro = s.supplier_id WHERE b.entryFk = ? ${groupBy ?? ''} ) sub`, [entryFk] ); stmt.merge(conn.makeSuffix(filter)); let itemsIndex = stmts.push(stmt) - 1; let sql = ParameterizedSQL.join(stmts, ';'); let result = await conn.executeStmt(sql, myOptions); if (groupBy) { const buys = await Self.app.models.Buy.find({where: {entryFk}}, myOptions); const buysChecked = buys.filter(buy => buy?.isChecked); result[0].isAllChecked = buysChecked.length === buys.length; } return itemsIndex === 0 ? result : result[itemsIndex]; }; };