92d16567db1ff0dcb435ff511e843a14313e5d98
[motion2.git] / server / components / export / xlsx.js
1 // *************************************************************************
2 // *                                                                       *
3 // * xCALLY Motion -  The Omnichannel Contact Center                       *
4 // * Copyright (c) Xenialab s.r.l. All Rights Reserved                     *
5 // *                                                                       *
6 // *************************************************************************
7 // *                                                                       *
8 // * Email: info@xcally.com                                                *
9 // * Website: https://www.xcally.com                                       *
10 // *                                                                       *
11 // *************************************************************************
12 // *                                                                       *
13 // * The SOFTWARE PRODUCT is protected by copyright laws and international *
14 // * copyright treaties, as well as other intellectual property laws and   *
15 // * treaties. The SOFTWARE PRODUCT is licensed, not sold.                         *
16 // *                                                                       *
17 // *************************************************************************
18 var _0x05be=['push','%s\x20limit\x20%d\x20OFFSET\x20%d','SELECT','dirname','basename','.xlsx','existsSync','join','each','%s%d.%s','xlsx','binary','zip','info','Extraction\x20report\x20with\x20id\x20%d\x20in\x20%s\x20format\x20completed','Completed','%s.%s','catch','Error\x20during\x20extraction\x20report\x20with\x20id\x20%d\x20in\x20%s\x20format','inspect','Failed','finally','util','lodash','json2xls','bluebird','zip-dir','rimraf','../../mysqldb','../../config/logger','analytics','argv','savename','AnalyticExtractedReport','format','XLSX','Setting','find','split','splitSizeXlsx','then','sequelize','query','SELECT\x20count(*)\x20as\x20num\x20FROM\x20(%s)\x20t','QueryTypes','num','length'];(function(_0x8e8597,_0x4d3ff3){var _0x20d704=function(_0x2dad62){while(--_0x2dad62){_0x8e8597['push'](_0x8e8597['shift']());}};_0x20d704(++_0x4d3ff3);}(_0x05be,0xa3));var _0xe05b=function(_0x494595,_0x34c39a){_0x494595=_0x494595-0x0;var _0x4e0f78=_0x05be[_0x494595];return _0x4e0f78;};'use strict';var util=require(_0xe05b('0x0'));var path=require('path');var _=require(_0xe05b('0x1'));var fs=require('fs');var json2xls=require(_0xe05b('0x2'));var BPromise=require(_0xe05b('0x3'));var zipdir=require(_0xe05b('0x4'));var rimraf=require(_0xe05b('0x5'));var db=require('../../mysqldb')['db'];var dbH=require(_0xe05b('0x6'))['dbH'];var logger=require(_0xe05b('0x7'))(_0xe05b('0x8'));var EXTRACTED_REPORT_ID=Number(process[_0xe05b('0x9')][0x2]);var QUERY=process[_0xe05b('0x9')][0x3];var PATH=process[_0xe05b('0x9')][0x4];var EXIT_CODE=0x0;function analyticExtractedReport(_0x1edcc1,_0x33ad15){var _0x5b030c={'status':_0x1edcc1};var _0x1152bf={'where':{'id':EXTRACTED_REPORT_ID}};if(_0x33ad15){_0x5b030c[_0xe05b('0xa')]=_0x33ad15;}return db[_0xe05b('0xb')]['update'](_0x5b030c,_0x1152bf);}function main(){var _0x2f8bfe=0x1388;var _0xbab4dd;logger['info'](util[_0xe05b('0xc')]('Start\x20extraction\x20report\x20with\x20id\x20%d\x20in\x20%s\x20format',EXTRACTED_REPORT_ID,_0xe05b('0xd')));return db[_0xe05b('0xe')][_0xe05b('0xf')]({'where':{'id':0x1},'raw':!![],'attributes':['id',_0xe05b('0x10'),_0xe05b('0x11')]})[_0xe05b('0x12')](function(_0x2ea0ee){if(_0x2ea0ee){_0xbab4dd=_0x2ea0ee;_0x2f8bfe=_0xbab4dd[_0xe05b('0x11')]||0x1388;}})[_0xe05b('0x12')](function(){return dbH[_0xe05b('0x13')][_0xe05b('0x14')](util[_0xe05b('0xc')](_0xe05b('0x15'),QUERY),{'type':dbH[_0xe05b('0x13')][_0xe05b('0x16')]['SELECT']});})[_0xe05b('0x12')](function(_0x23717){if(!_0x23717[0x0][_0xe05b('0x17')]){return analyticExtractedReport('Empty');}var _0x33aaed=[];var _0x2291c3=0x0;var _0x1dc714=!![];if(_0xbab4dd[_0xe05b('0x10')]){if(_0x23717&&_0x23717[_0xe05b('0x18')]){var _0x3bd291=_0x23717[0x0][_0xe05b('0x17')]%(_0x2291c3+_0x2f8bfe);for(;_0x1dc714;_0x2291c3+=_0x2f8bfe,_0x3bd291=_0x23717[0x0]['num']%(_0x2291c3+_0x2f8bfe)){if(_0x3bd291==0x0||_0x3bd291==_0x23717[0x0]['num']){_0x1dc714=![];}_0x33aaed[_0xe05b('0x19')](dbH[_0xe05b('0x13')][_0xe05b('0x14')](util[_0xe05b('0xc')](_0xe05b('0x1a'),QUERY,_0x2f8bfe,_0x2291c3),{'type':dbH[_0xe05b('0x13')][_0xe05b('0x16')][_0xe05b('0x1b')]}));}}}else{_0x33aaed[_0xe05b('0x19')](dbH[_0xe05b('0x13')]['query'](QUERY,{'type':dbH[_0xe05b('0x13')][_0xe05b('0x16')]['SELECT']}));}var _0x30cac6=path[_0xe05b('0x1c')](PATH);var _0x465797=path[_0xe05b('0x1d')](PATH,_0xe05b('0x1e'));var _0x2cf6c2=_0xbab4dd[_0xe05b('0x10')]&&_0x33aaed[_0xe05b('0x18')]>0x1;var _0x1cf7a1=[];if(!fs[_0xe05b('0x1f')](path[_0xe05b('0x20')](_0x30cac6,_0x465797))&&_0x2cf6c2){fs['mkdirSync'](path[_0xe05b('0x20')](_0x30cac6,_0x465797));}return BPromise[_0xe05b('0x21')](_0x33aaed,function(_0x5729f0){_0x1cf7a1[_0xe05b('0x19')](_0x5729f0);})['then'](function(){for(var _0x3d9994=0x0;_0x3d9994<_0x1cf7a1[_0xe05b('0x18')];_0x3d9994+=0x1){var _0x5a3b5d;var _0x2b33c6=_0x1cf7a1[_0x3d9994];if(_0x2cf6c2){_0x5a3b5d=path[_0xe05b('0x20')](_0x30cac6,_0x465797,util[_0xe05b('0xc')](_0xe05b('0x22'),_0x465797,_0x3d9994,_0xe05b('0x23')));}else{_0x5a3b5d=path[_0xe05b('0x20')](_0x30cac6,util['format']('%s.%s',_0x465797,'xlsx'));}fs['writeFileSync'](_0x5a3b5d,json2xls(_0x2b33c6),_0xe05b('0x24'));}})[_0xe05b('0x12')](function(){if(_0x2cf6c2){zipdir(path[_0xe05b('0x20')](_0x30cac6,_0x465797),{'saveTo':path[_0xe05b('0x20')](_0x30cac6,util['format']('%s.%s',_0x465797,_0xe05b('0x25')))},function(_0x4fa072,_0xa8dacc){});rimraf(path['join'](_0x30cac6,_0x465797),function(){});}})[_0xe05b('0x12')](function(){logger[_0xe05b('0x26')](util[_0xe05b('0xc')](_0xe05b('0x27'),EXTRACTED_REPORT_ID,_0xe05b('0xd')));return analyticExtractedReport(_0xe05b('0x28'),_0x2cf6c2?util['format'](_0xe05b('0x29'),_0x465797,_0x2cf6c2?_0xe05b('0x25'):_0xe05b('0x23')):null);});})[_0xe05b('0x2a')](function(_0x45b6fa){logger['info'](util[_0xe05b('0xc')](_0xe05b('0x2b'),EXTRACTED_REPORT_ID,_0xe05b('0xd')),util[_0xe05b('0x2c')](_0x45b6fa,{'showHidden':![],'depth':null}));EXIT_CODE=0x1;return analyticExtractedReport(_0xe05b('0x2d'));})[_0xe05b('0x2e')](function(){process['exit'](EXIT_CODE);});}main();