????

Your IP : 18.218.24.244


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/ExcelController.js

var xl = require('excel4node');
var path = require('path');
var moment = require('moment');
var { exportPath } = require('../../appconfig');
var fs = require('fs');

function create(filename, name, head, real_head, data, foot, align, width) {
  var xlsxName = `${filename}_${moment().format('YYYYMMDD-hhmmss')}.xlsx`;
  var myPath = path.join(exportPath, xlsxName);
  if (!fs.existsSync(myPath)) {
    var wb = new xl.Workbook({
      defaultFont: {
        size: 12,
        name: 'Times New Roman'
      },
      dateFormat: 'd/m/yy hh:mm:ss',
      pageSetup: {
        orientation: 'landscape'
      },
    });
    var ws = wb.addWorksheet(name);
    var header = wb.createStyle({
      font: {
        bold: true,
      },
      fill: {
        type: 'pattern',
        patternType: 'solid',
        bgColor: '#FFFF00',
        fgColor: '#cce0ff',
      },
      alignment: {
        wrapText: true,
        vertical: 'center',
        horizontal: 'center',
      },
      border: {
        left: { style: 'thin', color: 'black' },
        right: { style: 'thin', color: 'black' },
        top: { style: 'thin', color: 'black' },
        bottom: { style: 'thin', color: 'black' },
      },
    });
    var cellCenter = wb.createStyle({
      alignment: {
        wrapText: true,
        horizontal: 'center',
        vertical: 'center'
      },
      border: {
        left: { style: 'thin', color: 'black' },
        right: { style: 'thin', color: 'black' },
        top: { style: 'thin', color: 'black' },
        bottom: { style: 'thin', color: 'black' },
      },
    });
    var cellRight = wb.createStyle({
      alignment: {
        wrapText: true,
        horizontal: 'right',
        vertical: 'center'
      },
      border: {
        left: { style: 'thin', color: 'black' },
        right: { style: 'thin', color: 'black' },
        top: { style: 'thin', color: 'black' },
        bottom: { style: 'thin', color: 'black' },
      },
    });
    var normal = wb.createStyle({
      alignment: {
        wrapText: true,
        vertical: 'center'
      },
      border: {
        left: { style: 'thin', color: 'black' },
        right: { style: 'thin', color: 'black' },
        top: { style: 'thin', color: 'black' },
        bottom: { style: 'thin', color: 'black' },
      }
    });
    var bold = wb.createStyle({
      font: {
        bold: true,
      }
    });
    ws.cell(2, 1, 2, head.length, true).string(name).style({
      font: {
        bold: true,
        size: 13,
      },
      alignment: {
        horizontal: 'center',
      }
    });
    ws.row(4).setHeight(60);
    for (let i = 0; i < head.length; i++) {
      if (typeof width[i] != 'undefined') {
        ws.column(i + 1).setWidth(width[i]);
      } else {
        ws.column(i + 1).setWidth(10);
      }
      ws.cell(4, i + 1).string(head[i]);
    }
    ws.cell(4, 1, 4, head.length).style(header);
    ws.row(4).freeze();
    var row = 5;
    for (let i = 0; i < data.length; i++) {
      for (let j = 0; j < head.length; j++) {
        if (typeof real_head[j] != 'undefined' && real_head[j] != '') {
          var elem = real_head[j];
          if (typeof data[i][elem] == 'string') {
            ws.cell(row, j + 1).string(data[i][elem]);
          } else if (typeof data[i][elem] == 'number') {
            ws.cell(row, j + 1).number(data[i][elem]);
          } else if (moment(data[i][elem]).isValid()) {
            ws.cell(row, j + 1).string(moment(data[i][elem]).format('DD/MM/YYYY'));
          }
        } else {
          ws.cell(row, j + 1).string('NULL');
        }
      }
      row++;
    }
    var merge1 = 0;
    var merge2 = 0;
    for (let i = 0; i < head.length; i++) {
      if (align[i] == 'center') {
        ws.cell(5, i + 1, row, i + 1).style(cellCenter);
      } else if (align[i] == 'right') {
        ws.cell(5, i + 1, row, i + 1).style(cellRight);
      } else {
        ws.cell(5, i + 1, row, i + 1).style(normal);
      }
      if (foot[i]) {
        if (merge2 - merge1 > 2) {
          ws.cell(row, merge1 + 1, row, merge2 + 1, true);
        }
        var x = xl.getExcelCellRef(5, i + 1);
        var y = xl.getExcelCellRef(row - 1, i + 1);
        ws.cell(row, i + 1).formula('SUM(' + x + ':' + y + ')').style(bold);
        merge1 = i + 1;
        merge2 = i + 1;
      } else {
        merge2 = i;
      }
    }
  }
  return new Promise(function (resolve, reject) {
    wb.write(myPath, function (err, _res) {
      if (err) {
        reject(err);
      }
      else {
        resolve(xlsxName);
      }
    });
  });
}

module.exports.reportFond = function (data) {
  var filename = 'ReportFond';
  var name = 'BÁO CÁO PHÔNG LƯU TRỮ';
  var head = ['Số phông', 'Tên phông', 'Tổng số hồ sơ', 'Tổng số văn bản', 'Tổng số trang'];
  var real_head = ['FondNumber', 'FondName', 'tongSoHS', 'tongSoVB', 'tongSoTrang'];
  var foot = [false, false, true, true, true];
  var align = ['center', 'center', 'left', 'center', 'center', 'center', 'center', 'center'];
  var width = [10, 35, 10, 10, 10];
  return create(filename, name, head, real_head, data, foot, align, width).then(function (res) {
    return res;
  }).catch(function (err) {
    return err;
  });
}
module.exports.reportFile = function (data) {
  var filename = 'ReportFile';
  var name = 'BÁO CÁO MỤC LỤC HỒ SƠ';
  var head = ['STT', 'Mục lục số', 'Hộp số', 'Hồ sơ số', 'Tiêu đề hồ sơ', 'Thời gian bắt đầu', 'Thời gian kết thúc', 'Số lượng tờ', 'Số lượng trang', 'Tình trạng vật lý'];
  var real_head = ['RowNum', 'FileCatalog', 'BoxNumber', 'FileNumber', 'Title', 'StartDate', 'EndDate', 'SheetNumber', 'PageNumber', 'Format'];
  var foot = [false, false, false, false, false, false, false, true, true, false];
  var align = ['center', 'center', 'center', 'center', 'justify', 'center', 'center', 'center', 'center', 'center'];
  var width = [5, 8, 8, 8, 35, 10, 10, 8, 8, 11];
  return create(filename, name, head, real_head, data, foot, align, width).then(function (res) {
    return res;
  }).catch(function (err) {
    return err;
  });
}
module.exports.reportDocument = function (data) {
  for (var i = 0; i < data.length; i++) {
    var code = [];
    if (data[i].CodeNumber) code.push(data[i].CodeNumber);
    if (data[i].CodeNotation) code.push(data[i].CodeNotation);
    data[i].Code = code.join('/')
  }
  var filename = 'ReportDoc';
  var name = 'BÁO CÁO MỤC LỤC VĂN BẢN';
  var head = ['STT', 'Số và ký hiệu VB', 'Ngày, tháng VB', 'Tác giả văn bản', 'Trích yếu nội dung', 'Số trang', 'Tờ số', 'Ghi chú'];
  var real_head = ['RowNum', 'Code', 'IssuedDate', 'OrganName', 'Subject', 'PageAmount', 'SheetIndex', 'Description'];
  var foot = [false, false, false, false, false, true, false, false];
  var align = ['center', 'center', 'center', 'justify', 'justify', 'center', 'center', 'center'];
  var width = [5, 12, 12, 12, 30, 8, 8, 12];
  return create(filename, name, head, real_head, data, foot, align, width).then(function (res) {
    return res;
  }).catch(function (err) {
    return err;
  });
}
module.exports.statisticDocument = function (data) {
  var filename = getName('StatisticDocument', 'xlsx');
  var myPath = path.join(exportPath, filename);
  if (!fs.existsSync(myPath)) {
    var wb = new xl.Workbook({
      defaultFont: {
        size: 12,
        name: 'Times New Roman'
      },
      dateFormat: 'd/m/yy hh:mm:ss',
      pageSetup: {
        orientation: 'landscape'
      },
    });
    var ws = wb.addWorksheet('TỔNG KIỂM KÊ TÀI LIỆU');
    var header = wb.createStyle({
      font: {
        bold: true,
      },
      fill: {
        type: 'pattern',
        patternType: 'solid',
        bgColor: '#FFFF00',
        fgColor: '#cce0ff',
      },
      alignment: {
        wrapText: true,
        vertical: 'center',
        horizontal: 'center',
      },
      border: {
        left: { style: 'thin', color: 'black' },
        right: { style: 'thin', color: 'black' },
        top: { style: 'thin', color: 'black' },
        bottom: { style: 'thin', color: 'black' },
      },
    });
    var cellCenter = wb.createStyle({
      alignment: {
        wrapText: true,
        horizontal: 'center',
        vertical: 'center'
      },
      border: {
        left: { style: 'thin', color: 'black' },
        right: { style: 'thin', color: 'black' },
        top: { style: 'thin', color: 'black' },
        bottom: { style: 'thin', color: 'black' },
      },
    });
    // var cellRight = wb.createStyle({
    //   alignment: {
    //     wrapText: true,
    //     horizontal: 'right',
    //     vertical: 'center'
    //   },
    //   border: {
    //     left: { style: 'thin', color: 'black' },
    //     right: { style: 'thin', color: 'black' },
    //     top: { style: 'thin', color: 'black' },
    //     bottom: { style: 'thin', color: 'black' },
    //   },
    // });
    var normal = wb.createStyle({
      alignment: {
        wrapText: true,
        vertical: 'center'
      },
      border: {
        left: { style: 'thin', color: 'black' },
        right: { style: 'thin', color: 'black' },
        top: { style: 'thin', color: 'black' },
        bottom: { style: 'thin', color: 'black' },
      }
    });
    // var bold = wb.createStyle({
    //   font: {
    //     bold: true,
    //   }
    // });
    ws.cell(2, 1, 2, 3, true).string('TỔNG KIỂM KÊ TÀI LIỆU').style({
      font: {
        bold: true,
        size: 13,
      },
      alignment: {
        horizontal: 'center',
      }
    });
    ws.row(4).setHeight(60);
    ws.column(1).setWidth(40);
    ws.column(2).setWidth(30);
    ws.column(3).setWidth(30);
    ws.cell(4, 2).string('Số lượng');
    ws.cell(4, 3).string('Đơn vị');
    ws.cell(4, 1, 4, 3).style(header);
    ws.row(4).freeze();

    // var row = 5;
    ws.cell(5, 1).string('Tổng số phông lưu trữ').style(normal);
    ws.cell(6, 1).string('Tổng loại hình tài liệu').style(normal);
    ws.cell(7, 1).string('Tổng số mục lục').style(normal);
    ws.cell(8, 1).string('Tổng số hồ sơ').style(normal);
    ws.cell(9, 1).string('Số mét tài liệu').style(normal);
    ws.cell(10, 1).string('Tổng văn bản').style(normal);
    ws.cell(11, 1).string('Tổng số file đính kèm').style(normal);
    ws.cell(12, 1).string('Thời gian bắt đầu').style(normal);
    ws.cell(13, 1).string('Thời gian kết thúc').style(normal);
    ws.cell(5, 2).number(data[0]['countFond']).style(cellCenter);
    ws.cell(6, 2).number(data[0]['countGroupId']).style(cellCenter);
    ws.cell(7, 2).number(data[0]['countCatalog']).style(cellCenter);
    ws.cell(8, 2).number(data[0]['countFile']).style(cellCenter);
    ws.cell(9, 2).number(data[0]['sumPaperTotal']).style(cellCenter);
    ws.cell(10, 2).number(data[0]['countDocument']).style(cellCenter);
    ws.cell(11, 2).number(data[0]['countFileZip']).style(cellCenter);
    ws.cell(12, 2).string(data[0]['minStartDate']).style(cellCenter);
    ws.cell(13, 2).string(data[0]['maxEndDate']).style(cellCenter);
    ws.cell(5, 3).string('phông').style(cellCenter);
    ws.cell(6, 3).string('loại hình').style(cellCenter);
    ws.cell(7, 3).string('mục lục').style(cellCenter);
    ws.cell(8, 3).string('hồ sơ').style(cellCenter);
    ws.cell(9, 3).string('tài liệu').style(cellCenter);
    ws.cell(10, 3).string('văn bản').style(cellCenter);
    ws.cell(11, 3).string('file đính kèm').style(cellCenter);
    ws.cell(12, 3).style(cellCenter);
    ws.cell(13, 3).style(cellCenter);
  }
  return new Promise(function (resolve, reject) {
    wb.write(myPath, function (err, _res) {
      if (err) {
        reject(err);
      }
      else {
        resolve(filename);
      }
    });
  });
}

function getName(name, type) {
  var data = new Date();
  var fullYear = data.getFullYear();
  var month = (data.getMonth() + 1) < 10 ? '0' + (data.getMonth() + 1) : (data.getMonth() + 1);
  var date = data.getDate() < 10 ? '0' + data.getDate() : data.getDate();
  var hours = data.getHours() < 10 ? '0' + data.getHours() : data.getHours();
  var minutes = data.getMinutes() < 10 ? '0' + data.getMinutes() : data.getMinutes();
  var seconds = data.getSeconds() < 10 ? '0' + data.getSeconds() : data.getSeconds();
  return name + '_' + fullYear + '' + month + '' + date + '' + hours + '' + minutes + '' + seconds + '.' + type;
}