240c68da85bca9edd5fed2f6f764c3f2749de569
[motion2.git] / server / api / saProductQuotation / saProductQuotation.controller.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 _0xbaf0=['name','send','index','SAProductQuotation','rawAttributes','model','keys','query','intersection','attributes','fields','length','hasOwnProperty','nolimit','order','sort','filters','pick','filter','where','merge','map','key','VIRTUAL','field','options','includeAll','include','findAll','rows','show','params','catch','create','body','find','describe','ProformaId','InvoiceId','transaction','companyName','companyId','productName','productCode','typeName','pricelistName','ProductPriceListId','netPrice','discount','tax','agentFullname','subscriptionName','subscriptionMonthly','quantity','unitName','SAInvoiceDetail','sequelize','SAInvoice','log','result','all','WARNING','TCL:\x20exports.automaticInvoice\x20->\x20err','SequelizeUniqueConstraintError','note','subscriptionId','SAProformaDetail','automaticProforma','SAProforma','No\x20product\x20to\x20assign\x20proforma.\x20Proforma\x20not\x20created!!','TCL:\x20exports.automaticProforma\x20->\x20err','Duplicate\x20entry.','eml-format','rimraf','zip-dir','fast-json-patch','request-promise','moment','mustache','util','path','sox','to-csv','ejs','lodash','squel','crypto','desk.js','querystring','papaparse','ioredis','../../components/auth/service','../../components/parsers/qs','../../config/license/hardware','../../config/logger','api','../../config/utils','../../config/environment','../../mysqldb','sendStatus','status','end','json','count','offset','limit','Content-Range','apply','update','then','destroy','error','stack'];(function(_0x1b51a1,_0x36e565){var _0x3e48c8=function(_0x481ed0){while(--_0x481ed0){_0x1b51a1['push'](_0x1b51a1['shift']());}};_0x3e48c8(++_0x36e565);}(_0xbaf0,0x12a));var _0x0baf=function(_0x2b4411,_0x2625ed){_0x2b4411=_0x2b4411-0x0;var _0x8e41ef=_0xbaf0[_0x2b4411];return _0x8e41ef;};'use strict';var pdf=require('html-pdf');var emlformat=require(_0x0baf('0x0'));var rimraf=require(_0x0baf('0x1'));var zipdir=require(_0x0baf('0x2'));var jsonpatch=require(_0x0baf('0x3'));var rp=require(_0x0baf('0x4'));var moment=require(_0x0baf('0x5'));var BPromise=require('bluebird');var Mustache=require(_0x0baf('0x6'));var util=require(_0x0baf('0x7'));var path=require(_0x0baf('0x8'));var sox=require(_0x0baf('0x9'));var csv=require(_0x0baf('0xa'));var ejs=require(_0x0baf('0xb'));var fs=require('fs');var _=require(_0x0baf('0xc'));var squel=require(_0x0baf('0xd'));var crypto=require(_0x0baf('0xe'));var jsforce=require('jsforce');var deskjs=require(_0x0baf('0xf'));var toCsv=require('to-csv');var querystring=require(_0x0baf('0x10'));var Papa=require(_0x0baf('0x11'));var Redis=require(_0x0baf('0x12'));var authService=require(_0x0baf('0x13'));var qs=require(_0x0baf('0x14'));var hardwareService=require(_0x0baf('0x15'));var logger=require(_0x0baf('0x16'))(_0x0baf('0x17'));var utils=require(_0x0baf('0x18'));var config=require(_0x0baf('0x19'));var db=require(_0x0baf('0x1a'))['db'];function respondWithStatusCode(_0x51d93c,_0x5a53e2){_0x5a53e2=_0x5a53e2||0xcc;return function(_0x465dd7){if(_0x465dd7){return _0x51d93c[_0x0baf('0x1b')](_0x5a53e2);}return _0x51d93c[_0x0baf('0x1c')](_0x5a53e2)[_0x0baf('0x1d')]();};}function respondWithResult(_0x5a0583,_0x2ab33e){_0x2ab33e=_0x2ab33e||0xc8;return function(_0x49e44b){if(_0x49e44b){return _0x5a0583[_0x0baf('0x1c')](_0x2ab33e)[_0x0baf('0x1e')](_0x49e44b);}};}function respondWithFilteredResult(_0x7fbf67,_0x2bc381){return function(_0xb8339e){if(_0xb8339e){var _0x469599=_0xb8339e[_0x0baf('0x1f')],_0x4f80a8=_0x2bc381[_0x0baf('0x20')],_0x4cd79d=_0x2bc381[_0x0baf('0x20')]+_0x2bc381[_0x0baf('0x21')],_0x2312c7;if(_0x4cd79d>=_0x469599){_0x4cd79d=_0x469599;_0x2312c7=0xc8;}else{_0x2312c7=0xce;}_0x7fbf67[_0x0baf('0x1c')](_0x2312c7);return _0x7fbf67['set'](_0x0baf('0x22'),_0x4f80a8+'-'+_0x4cd79d+'/'+_0x469599)[_0x0baf('0x1e')](_0xb8339e);}return null;};}function patchUpdates(_0x1ef0e5){return function(_0x3e9cb4){try{jsonpatch[_0x0baf('0x23')](_0x3e9cb4,_0x1ef0e5,!![]);}catch(_0x42b7f2){return BPromise['reject'](_0x42b7f2);}return _0x3e9cb4['save']();};}function saveUpdates(_0x1410ec,_0x154f48){return function(_0x32e839){if(_0x32e839){return _0x32e839[_0x0baf('0x24')](_0x1410ec)[_0x0baf('0x25')](function(_0x3e3e20){return _0x3e3e20;});}return null;};}function removeEntity(_0x210cd8,_0x636a13){return function(_0x2c42e2){if(_0x2c42e2){return _0x2c42e2[_0x0baf('0x26')]()['then'](function(){_0x210cd8[_0x0baf('0x1c')](0xcc)['end']();});}};}function handleEntityNotFound(_0xf6c158,_0x5dbf00){return function(_0x4de74c){if(!_0x4de74c){_0xf6c158[_0x0baf('0x1b')](0x194);}return _0x4de74c;};}function handleError(_0x48a0e9,_0x4be1b9){_0x4be1b9=_0x4be1b9||0x1f4;return function(_0x47c4b){logger[_0x0baf('0x27')](_0x47c4b[_0x0baf('0x28')]);if(_0x47c4b[_0x0baf('0x29')]){delete _0x47c4b[_0x0baf('0x29')];}_0x48a0e9['status'](_0x4be1b9)[_0x0baf('0x2a')](_0x47c4b);};}exports[_0x0baf('0x2b')]=function(_0x1ca35a,_0x366e71){var _0x3843b8={'raw':!![]},_0x3b9b31={},_0x453525={'count':0x0,'rows':[]};var _0x549fd0=db[_0x0baf('0x2c')][_0x0baf('0x2d')];_0x3b9b31[_0x0baf('0x2e')]=_[_0x0baf('0x2f')](_0x549fd0);_0x3b9b31['query']=_[_0x0baf('0x2f')](_0x1ca35a[_0x0baf('0x30')]);_0x3b9b31['filters']=_[_0x0baf('0x31')](_0x3b9b31[_0x0baf('0x2e')],_0x3b9b31[_0x0baf('0x30')]);_0x3843b8[_0x0baf('0x32')]=_[_0x0baf('0x31')](_0x3b9b31[_0x0baf('0x2e')],qs[_0x0baf('0x33')](_0x1ca35a['query'][_0x0baf('0x33')]));_0x3843b8[_0x0baf('0x32')]=_0x3843b8[_0x0baf('0x32')][_0x0baf('0x34')]?_0x3843b8[_0x0baf('0x32')]:_0x3b9b31[_0x0baf('0x2e')];if(!_0x1ca35a[_0x0baf('0x30')][_0x0baf('0x35')](_0x0baf('0x36'))){_0x3843b8[_0x0baf('0x21')]=qs[_0x0baf('0x21')](_0x1ca35a[_0x0baf('0x30')][_0x0baf('0x21')]);_0x3843b8[_0x0baf('0x20')]=qs['offset'](_0x1ca35a[_0x0baf('0x30')]['offset']);}_0x3843b8[_0x0baf('0x37')]=qs[_0x0baf('0x38')](_0x1ca35a[_0x0baf('0x30')][_0x0baf('0x38')]);_0x3843b8['where']=qs[_0x0baf('0x39')](_[_0x0baf('0x3a')](_0x1ca35a['query'],_0x3b9b31[_0x0baf('0x39')]));if(_0x1ca35a[_0x0baf('0x30')][_0x0baf('0x3b')]){_0x3843b8[_0x0baf('0x3c')]=_[_0x0baf('0x3d')](_0x3843b8[_0x0baf('0x3c')],{'$or':_[_0x0baf('0x3e')](_0x549fd0,function(_0x675461){if(_0x675461['type'][_0x0baf('0x3f')]!==_0x0baf('0x40')){var _0x5b991f={};_0x5b991f[_0x675461[_0x0baf('0x41')]]={'$like':'%'+_0x1ca35a['query']['filter']+'%'};return _0x5b991f;}})});}_0x3843b8=_[_0x0baf('0x3d')]({},_0x3843b8,_0x1ca35a[_0x0baf('0x42')]);var _0x5d3ad1={'where':_0x3843b8['where']};return db[_0x0baf('0x2c')][_0x0baf('0x1f')](_0x5d3ad1)[_0x0baf('0x25')](function(_0xb2d9ab){_0x453525[_0x0baf('0x1f')]=_0xb2d9ab;if(_0x1ca35a[_0x0baf('0x30')][_0x0baf('0x43')]){_0x3843b8[_0x0baf('0x44')]=[{'all':!![]}];}return db[_0x0baf('0x2c')][_0x0baf('0x45')](_0x3843b8);})[_0x0baf('0x25')](function(_0x148540){_0x453525[_0x0baf('0x46')]=_0x148540;return _0x453525;})[_0x0baf('0x25')](respondWithFilteredResult(_0x366e71,_0x3843b8))['catch'](handleError(_0x366e71,null));};exports[_0x0baf('0x47')]=function(_0x1435d5,_0x4f1de9){var _0x38006a={'raw':!![],'where':{'id':_0x1435d5[_0x0baf('0x48')]['id']}},_0x324369={};_0x324369[_0x0baf('0x2e')]=_[_0x0baf('0x2f')](db[_0x0baf('0x2c')]['rawAttributes']);_0x324369[_0x0baf('0x30')]=_[_0x0baf('0x2f')](_0x1435d5['query']);_0x324369[_0x0baf('0x39')]=_[_0x0baf('0x31')](_0x324369[_0x0baf('0x2e')],_0x324369[_0x0baf('0x30')]);_0x38006a['attributes']=_[_0x0baf('0x31')](_0x324369[_0x0baf('0x2e')],qs[_0x0baf('0x33')](_0x1435d5[_0x0baf('0x30')][_0x0baf('0x33')]));_0x38006a[_0x0baf('0x32')]=_0x38006a[_0x0baf('0x32')][_0x0baf('0x34')]?_0x38006a[_0x0baf('0x32')]:_0x324369[_0x0baf('0x2e')];if(_0x1435d5['query'][_0x0baf('0x43')]){_0x38006a[_0x0baf('0x44')]=[{'all':!![]}];}_0x38006a=_[_0x0baf('0x3d')]({},_0x38006a,_0x1435d5[_0x0baf('0x42')]);return db[_0x0baf('0x2c')]['find'](_0x38006a)[_0x0baf('0x25')](handleEntityNotFound(_0x4f1de9,null))[_0x0baf('0x25')](respondWithResult(_0x4f1de9,null))[_0x0baf('0x49')](handleError(_0x4f1de9,null));};exports[_0x0baf('0x4a')]=function(_0x35b5db,_0x54ed41){return db[_0x0baf('0x2c')][_0x0baf('0x4a')](_0x35b5db[_0x0baf('0x4b')],{})[_0x0baf('0x25')](respondWithResult(_0x54ed41,0xc9))[_0x0baf('0x49')](handleError(_0x54ed41,null));};exports[_0x0baf('0x24')]=function(_0x178995,_0x5679d9){if(_0x178995[_0x0baf('0x4b')]['id']){delete _0x178995[_0x0baf('0x4b')]['id'];}return db[_0x0baf('0x2c')]['find']({'where':{'id':_0x178995[_0x0baf('0x48')]['id']}})[_0x0baf('0x25')](handleEntityNotFound(_0x5679d9,null))[_0x0baf('0x25')](saveUpdates(_0x178995[_0x0baf('0x4b')],null))[_0x0baf('0x25')](respondWithResult(_0x5679d9,null))[_0x0baf('0x49')](handleError(_0x5679d9,null));};exports[_0x0baf('0x26')]=function(_0x5a54bb,_0x22c451){return db[_0x0baf('0x2c')][_0x0baf('0x4c')]({'where':{'id':_0x5a54bb[_0x0baf('0x48')]['id']}})[_0x0baf('0x25')](handleEntityNotFound(_0x22c451,null))[_0x0baf('0x25')](removeEntity(_0x22c451,null))[_0x0baf('0x49')](handleError(_0x22c451,null));};exports[_0x0baf('0x4d')]=function(_0x2be7fe,_0x19c008){return db[_0x0baf('0x2c')]['describe']()['then'](respondWithResult(_0x19c008,null))[_0x0baf('0x49')](handleError(_0x19c008,null));};function assignProductQuotationToProformaInvoice(_0x3d6b19,_0x1cd114){if(_0x3d6b19[_0x0baf('0x4b')]['id']){delete _0x3d6b19[_0x0baf('0x4b')]['id'];}return db[_0x0baf('0x2c')][_0x0baf('0x24')]({'ProformaId':_0x3d6b19['body'][_0x0baf('0x4e')],'InvoiceId':_0x3d6b19[_0x0baf('0x4b')][_0x0baf('0x4f')]},{'where':{'QuotationId':_0x3d6b19[_0x0baf('0x48')]['id'],'ProformaId':_0x3d6b19[_0x0baf('0x30')][_0x0baf('0x4e')]||{'$eq':null},'InvoiceId':_0x3d6b19['query'][_0x0baf('0x4f')]||{'$eq':null}},'transaction':_0x1cd114});}exports['updateBulk']=function(_0x151741,_0x59f732,_0x25a1b2){return db['sequelize'][_0x0baf('0x50')](function(_0x4c035b){return assignProductQuotationToProformaInvoice(_0x151741,_0x4c035b)[_0x0baf('0x25')](function(_0xcf7b8d){return _0x59f732[_0x0baf('0x1b')](0xc8);})[_0x0baf('0x49')](handleError(_0x59f732,null));});};function addInvoiceDetail(_0x42a68b,_0x576475,_0x21f179){var _0x36b7d2={'companyName':_0x42a68b[_0x0baf('0x51')],'companyId':_0x42a68b[_0x0baf('0x52')],'productName':_0x42a68b[_0x0baf('0x53')],'productCode':_0x42a68b[_0x0baf('0x54')],'productDescription':_0x42a68b['productDescription'],'typeName':_0x42a68b[_0x0baf('0x55')],'pricelistName':_0x42a68b[_0x0baf('0x56')],'priceListId':_0x42a68b[_0x0baf('0x57')],'netPrice':_0x42a68b[_0x0baf('0x58')],'discount':_0x42a68b[_0x0baf('0x59')],'tax':_0x42a68b[_0x0baf('0x5a')],'agentFullname':_0x42a68b[_0x0baf('0x5b')],'note':_0x42a68b['note'],'subscriptionId':_0x42a68b['subscriptionId'],'subscriptionName':_0x42a68b[_0x0baf('0x5c')],'subscriptionMonthly':_0x42a68b[_0x0baf('0x5d')],'quantity':_0x42a68b[_0x0baf('0x5e')],'InvoiceId':_0x576475,'unitName':_0x42a68b[_0x0baf('0x5f')]};return db[_0x0baf('0x60')][_0x0baf('0x4a')](_0x36b7d2,{'transaction':_0x21f179})[_0x0baf('0x25')](function(){return db[_0x0baf('0x2c')][_0x0baf('0x24')]({'InvoiceId':_0x576475},{'where':{'id':_0x42a68b['id']},'transaction':_0x21f179});});}exports['automaticInvoice']=function(_0x3238f9,_0x268be8,_0x3c2d03){if(_0x3238f9[_0x0baf('0x4b')]['id']){delete _0x3238f9[_0x0baf('0x4b')]['id'];}return db[_0x0baf('0x61')][_0x0baf('0x50')](function(_0x51d75f){return db[_0x0baf('0x62')][_0x0baf('0x4a')](_0x3238f9[_0x0baf('0x4b')],{'transaction':_0x51d75f})[_0x0baf('0x25')](function(_0x1c7d88){console[_0x0baf('0x63')](_0x0baf('0x64'),_0x1c7d88);var _0x12543e=_0x3238f9['params']['id'];return db[_0x0baf('0x2c')]['findAll']({'where':{'QuotationId':_0x12543e,'productAsset':{'$ne':0x1},'ProformaId':{'$eq':null},'InvoiceId':{'$eq':null}},'raw':!![],'logging':console[_0x0baf('0x63')]})['then'](function(_0x50fa87){if(_0x50fa87[_0x0baf('0x34')]>0x0){var _0x4a2cb0=[];for(var _0x16f2e6=0x0;_0x16f2e6<_0x50fa87[_0x0baf('0x34')];_0x16f2e6++){_0x4a2cb0['push'](addInvoiceDetail(_0x50fa87[_0x16f2e6],_0x1c7d88['id'],_0x51d75f));}return BPromise[_0x0baf('0x65')](_0x4a2cb0);}else{var _0xfb6708={'errors':[{'type':_0x0baf('0x66'),'message':'No\x20product\x20to\x20assign\x20invoice.\x20Invoice\x20not\x20created!!'}]};throw _0xfb6708;}});});})['then'](function(){return _0x268be8[_0x0baf('0x1b')](0xc8);})[_0x0baf('0x49')](function(_0x6d789f){console[_0x0baf('0x63')](_0x0baf('0x67'),_0x6d789f);if(_0x6d789f['name']===_0x0baf('0x68')){var _0x6d789f={'errors':[{'message':'Duplicate\x20entry.'}]};}_0x268be8['status'](0x1f4)[_0x0baf('0x2a')](_0x6d789f);});};function addProformaDetail(_0x58a366,_0x4946d4,_0x11a81e){var _0x288f17={'companyName':_0x58a366[_0x0baf('0x51')],'companyId':_0x58a366[_0x0baf('0x52')],'productName':_0x58a366[_0x0baf('0x53')],'productCode':_0x58a366[_0x0baf('0x54')],'productDescription':_0x58a366['productDescription'],'typeName':_0x58a366[_0x0baf('0x55')],'pricelistName':_0x58a366[_0x0baf('0x56')],'priceListId':_0x58a366[_0x0baf('0x57')],'netPrice':_0x58a366[_0x0baf('0x58')],'discount':_0x58a366[_0x0baf('0x59')],'tax':_0x58a366[_0x0baf('0x5a')],'agentFullname':_0x58a366['agentFullname'],'note':_0x58a366[_0x0baf('0x69')],'subscriptionId':_0x58a366[_0x0baf('0x6a')],'subscriptionName':_0x58a366[_0x0baf('0x5c')],'subscriptionMonthly':_0x58a366['subscriptionMonthly'],'quantity':_0x58a366[_0x0baf('0x5e')],'ProformaId':_0x4946d4};return db[_0x0baf('0x6b')][_0x0baf('0x4a')](_0x288f17,{'transaction':_0x11a81e})[_0x0baf('0x25')](function(){return db[_0x0baf('0x2c')][_0x0baf('0x24')]({'ProformaId':_0x4946d4},{'where':{'id':_0x58a366['id']},'transaction':_0x11a81e});});}exports[_0x0baf('0x6c')]=function(_0x5874cc,_0x54353a,_0x4b7caa){if(_0x5874cc[_0x0baf('0x4b')]['id']){delete _0x5874cc[_0x0baf('0x4b')]['id'];}return db[_0x0baf('0x61')][_0x0baf('0x50')](function(_0x3e65d4){return db[_0x0baf('0x6d')]['create'](_0x5874cc['body'],{'transaction':_0x3e65d4})[_0x0baf('0x25')](function(_0x519b30){console[_0x0baf('0x63')](_0x0baf('0x64'),_0x519b30);var _0x4cca39=_0x5874cc[_0x0baf('0x48')]['id'];return db[_0x0baf('0x2c')][_0x0baf('0x45')]({'where':{'QuotationId':_0x4cca39,'productAsset':{'$ne':0x1},'ProformaId':{'$eq':null},'InvoiceId':{'$eq':null}},'raw':!![],'logging':console[_0x0baf('0x63')]})[_0x0baf('0x25')](function(_0x33a5af){if(_0x33a5af[_0x0baf('0x34')]>0x0){var _0x2e3b1d=[];for(var _0xfcc697=0x0;_0xfcc697<_0x33a5af['length'];_0xfcc697++){_0x2e3b1d['push'](addProformaDetail(_0x33a5af[_0xfcc697],_0x519b30['id'],_0x3e65d4));}return BPromise['all'](_0x2e3b1d);}else{var _0x493784={'errors':[{'type':'WARNING','message':_0x0baf('0x6e')}]};throw _0x493784;}});});})[_0x0baf('0x25')](function(){return _0x54353a[_0x0baf('0x1b')](0xc8);})[_0x0baf('0x49')](function(_0x5093f5){console[_0x0baf('0x63')](_0x0baf('0x6f'),_0x5093f5);if(_0x5093f5['name']==='SequelizeUniqueConstraintError'){var _0x5093f5={'errors':[{'message':_0x0baf('0x70')}]};}_0x54353a[_0x0baf('0x1c')](0x1f4)[_0x0baf('0x2a')](_0x5093f5);});};