????

Your IP : 3.142.243.141


Current Path : C:/inetpub/vhost/sdoc.gdtsolutions.vn/package/app/controllers/baocao/
Upload File :
Current File : C:/inetpub/vhost/sdoc.gdtsolutions.vn/package/app/controllers/baocao/ReportController.old.js

const Database = require('../../models/Database');

class ReportController extends Database {
  constructor(params) {
    super(params);
  }
  static create(params) {
    return new ReportController(params);
  }
  //for report fonds
  async getFileGroup() {
    var query = 'SELECT Id, UPPER(GroupName) AS GroupName FROM [FileGroup]';
    return await this.select(query);
  }
  async getFondCode() {
    var query = 'SELECT DISTINCT FondCode FROM Fond';
    return await this.select(query);
  }
  async getFondName() {
    var query = 'SELECT DISTINCT FondCode, FondName FROM Fond';
    return await this.select(query);
  }
  async getFondToReport(group, fondCode) {
    var condition = [];
    condition.push('[File].[State] = 1');
    condition.push('[File].EditedBy is not NULL');
    if (group) condition.push('tkHoSo.GroupId = @group');
    if (fondCode) condition.push('Fond.FondCode = @fondCode');
    let query = `WITH tkHoSo AS (SELECT [File].UUID,[File].FondCode,MIN(Document.IssuedDate) startDate,MAX(Document.IssuedDate) endDate,COUNT(Document.UUID) soVanBan, SUM(Document.PageAmount) soTrang FROM [File] LEFT JOIN Document ON Document.FileId = [File].UUID WHERE ${condition.join(' AND ')} GROUP BY [File].UUID,[File].FondCode) SELECT FondName,Fond.FondCode,Fond.FondNumber, MIN(YEAR(tkHoSo.startDate)) startYear, MAX(YEAR(tkHoSo.endDate)) endYear,COUNT(tkHoSo.UUID) tongSoHS, SUM(tkHoSo.soTrang) tongSoTrang, SUM(tkHoSo.soVanBan) tongSoVB FROM Fond LEFT JOIN tkHoSo ON tkHoSo.FondCode = Fond.FondCode `;
    query += 'GROUP BY FondName,Fond.FondCode,Fond.FondNumber ';
    query += 'ORDER BY Fond.FondNumber';
    return await this.select(query, { group: group, fondCode: fondCode });
  }
  async reportFiles(reqBody) {
    var condition = ['[File].[State] = 1'];
    if (reqBody.jobState) condition.push('[File].EditedBy is not NULL');
    else condition.push('[File].EditedBy is null');
    if (reqBody.fondCode) condition.push('Fond.FondCode = @fondCode');
    if (reqBody.boxNumber1) condition.push('[File].BoxNumber >= @boxNumber1');
    if (reqBody.boxNumber2) condition.push('[File].BoxNumber <= @boxNumber2');
    if (reqBody.fileNumber1) condition.push('[dbo].getFileNumber([File].FileNumber) >= @fileNumber1');
    if (reqBody.fileNumber2) condition.push('[dbo].getFileNumber([File].FileNumber) <= @fileNumber2');
    if (reqBody.startDate1) condition.push('[File].StartDate >= @startDate1');
    if (reqBody.startDate2) condition.push('[File].StartDate <= @startDate2');
    if (reqBody.endDate1) condition.push('[File].EndDate >= @endDate1');
    if (reqBody.endDate2) condition.push('[File].EndDate <= @endDate2');
    let fileTable = 'SELECT ROW_NUMBER() OVER(ORDER BY Fond.FondCode,[File].FileCatalog,[File].BoxNumber,[File].FileNumber) as RowNum,[File].UUID,[File].FondCode,FondName,[File].FileCatalog,[File].BoxNumber,[File].FileNumber,[File].Title,[File].StartDate,[File].EndDate,[File].SheetNumber,[File].PageNumber,[File].Maintenance,[File].[Description],[File].[Format],editAcc.FullName as EditedBy, (SELECT COUNT(Document.UUID) FROM Document WHERE Document.FileId = [File].UUID) as totalDoc FROM [File] ';
    fileTable += 'INNER JOIN Fond ON Fond.FondCode = [File].FondCode ';
    fileTable += 'LEFT JOIN Account editAcc ON editAcc.UUID = [File].EditedBy ';
    fileTable += `WHERE ${condition.join(' AND ')}`;
    var query = `WITH ReportFile AS (${fileTable}) SELECT *, COUNT(UUID) OVER() as recordsTotal, SUM(totalDoc) OVER() as docsTotal, SUM(PageNumber) OVER() as pagesTotal FROM ReportFile ORDER BY RowNum`;
    if (reqBody.length) query += ' OFFSET @start rows FETCH NEXT @length rows only';
    return await this.select(query, reqBody);
  }
  async reportDocuments(reqBody) {
    var condition = ['[File].[State] = 1'];
    if (reqBody.state == 2) { condition.push('Document.PageAmount > 0'); }
    else if (reqBody['state'] == 1) { condition.push('(Document.PageAmount = 0 OR Document.PageAmount is null)'); }
    if (reqBody.fondCode) condition.push('Fond.FondCode = @fondCode');
    if (reqBody.boxNumber1) condition.push('[File].BoxNumber >= @boxNumber1');
    if (reqBody.boxNumber2) condition.push('[File].BoxNumber <= @boxNumber2');
    if (reqBody.fileNumber1) condition.push('[dbo].getFileNumber([File].FileNumber) >= @fileNumber1');
    if (reqBody.fileNumber2) condition.push('[dbo].getFileNumber([File].FileNumber) <= @fileNumber2');
    if (reqBody.issuedDate1) condition.push('[Document].IssuedDate >= @issuedDate1');
    if (reqBody.issuedDate2) condition.push('[Document].IssuedDate <= @issuedDate2');
    let docTable = 'SELECT ROW_NUMBER() OVER(ORDER BY Fond.FondCode,[File].FileCatalog,[File].BoxNumber,[File].FileNumber,Document.DocOrdinal) as RowNum,Document.UUID,Document.DocOrdinal,DocType.TypeName,Document.CodeNumber,Document.CodeNotation,Document.IssuedDate,Document.OrganName,Document.[Subject],Document.SheetIndex,Document.SheetAmount,Document.PageAmount,Mode.ModeName,Document.[Description],Fond.FondName,Fond.FondCode,[File].FileCatalog,[File].BoxNumber,[File].FileNumber,Document.[Format],Account.FullName as CreatedBy FROM Document ';
    docTable += 'LEFT JOIN Mode ON Document.ModeId = Mode.Id ';
    docTable += 'LEFT JOIN DocType ON DocType.Id = Document.DocType ';
    docTable += 'LEFT JOIN Account ON Account.UUID = Document.CreatedBy ';
    docTable += 'INNER JOIN [File] ON [File].UUID = Document.FileId ';
    docTable += 'INNER JOIN Fond ON Fond.FondCode = [File].FondCode ';
    docTable += `WHERE ${condition.join(' AND ')}`;
    var query = `WITH ReportDoc AS (${docTable}) SELECT *, COUNT(UUID) OVER() as recordsTotal, SUM(PageAmount) OVER() as tongSoTrang FROM ReportDoc ORDER BY RowNum`;
    if (reqBody.length) query += ' OFFSET @start rows FETCH NEXT @length rows only';
    return await this.select(query, reqBody);
  }
}
module.exports = ReportController;