UNPKG

synt_backend

Version:

Synt light-weight node backend service

121 lines (106 loc) 2.72 kB
const db = require("./../mysql/models/index"); import { calculateAllocations, getLots, getPurchases, } from "../database/accounting"; import { findCommissioners } from "../helpers/format"; import { between } from "./utils"; export async function getOwnersDB({ vme, include_past, start_date, end_date }) { const commissioners = await findCommissioners(vme, Boolean(include_past)); const Purchases = await getPurchases({ start_date, end_date, VMEId: vme.id, }); const Lots = await getLots({ VME: vme }); const AllAllocations = calculateAllocations({ start_date, end_date, Lots, Purchases, }); await Promise.all( commissioners.map(async (user) => { const Allocations = AllAllocations.filter( (a) => String(a.UserId) === String(user.id) ); const provisions = await db.Provision.findAll({ include: [ { model: db.User, where: { id: user.id, }, }, { model: db.VME, }, { model: db.Lot, attributes: ["name", "id"], }, { model: db.FinancialYear, }, ], where: between( "invoice_date", new Date(start_date), new Date(end_date) ), }); user.setDataValue("provisions", provisions); user.setDataValue("allocations", Allocations); }) ); return { success: true, commissioners, start_date, end_date, vme, }; } export async function getVmePurchases({ vme, start_date, end_date }) { const purchases = await vme.getPurchases({ include: [{ model: db.Supplier, include: [{ model: db.Company }] }], where: between("invoice_date", new Date(start_date), new Date(end_date)), }); return purchases; } export async function getSuppliersDB({ vme, start_date, end_date }) { const purchases = await getVmePurchases({ vme, start_date, end_date }); return { success: true, purchases, start_date, end_date, vme, }; } export async function getGLAPurchasesDB({ vme, start_date, end_date }) { const purchases = await vme.getPurchases({ include: [ { model: db.PurchaseEntry, include: [ { model: db.PurchaseAllocation }, { model: db.GeneralLedgerAccount, include: [{ model: db.DistributionKey, include: db.Lot }], }, ], }, { model: db.Supplier, include: [{ model: db.Company }] }, ], where: between("invoice_date", start_date, end_date), }); return { success: true, purchases, start_date, end_date, vme, }; }