app.controller('QuotationsCtrl', function($scope, $location, $http) { $scope.QuotationsOverview = { name: 'Offertes', controller: 'quotation', function: 'getForOverview', panelClass: 'panel-trainingskampen', showFilters: true, pageLimit: 10, fields: [ 'id', 'quotation_number', 'customer_id', 'customer_email', 'managing_user_id', 'camp_name', 'destination', 'amount', 'expire_date', 'status', 'payment_status_id', ], style:{ row:{ 'danger':{ 'row': '==danger', }, 'warning':{ 'row': '==warning', }, }, }, subQueries: { 'customer':{ 'lfield': 'customer_id', 'ffield': 'id', 'controller': 'customer', }, 'manager':{ 'lfield': 'managing_user_id', 'ffield': 'id', 'controller': 'user', }, 'payment_status':{ 'lfield': 'payment_status_id', 'ffield': 'id', 'controller': 'payment_status', }, }, fieldDetails: { 'id': { 'label': 'Id', 'hideEdit': 'true', type: 'number', }, 'quotation_number': { 'label': 'Offerte nummer', 'hideEdit': 'true', type: 'number', }, 'customer_id':{ 'label': 'Klant', 'changeTo': 'customer.company_name', hideList: $scope.user.role.id < 4, }, 'customer': { 'label': 'Email', 'changeTo': 'customer_email', }, 'customer_email': { 'label': "Email" }, 'managing_user_id':{ 'label': 'Tk manager', 'changeTo': 'manager.name', }, 'camp_name': { 'label': 'Kampnaam', }, 'destination': { 'label': 'Bestemming', }, 'amount': { 'label': 'Bedrag', }, 'expire_date':{ 'label': 'Verval datum', }, 'status':{ 'label': 'Status' }, 'payment_status_id': { 'label': 'Aanbetaal status', 'changeTo': 'payment_status.name', } }, buttons: { 'C':{ 'id': 'c', 'name': 'Offerte aanmaken', 'label': 'Offerte aanmaken', 'cancelName': 'Annuleren', 'confirmName': 'maak aan', 'confirmClass': 'success', 'icon': 'fa-plus-square', 'header': $scope.user.role.id >= 4, 'action': 'toWizard', 'parentScope': true, }, 'R':{ 'id': 'r', 'name': 'Bekijk offerte', 'label': 'Bekijk', 'cancelName': 'Annuleren', 'confirmName': 'Bekijk', 'confirmClass': 'info', 'icon':'fa-arrow-right', 'action': 'toQuotation', 'parentScope': true, 'inline': true, }, 'U':{ 'id': 'u', 'inline': false, }, 'V':{ 'id': 'v', 'name': 'Bekijk de versies', 'label': 'Versies', 'cancelName': 'Annuleren', 'confirmName': 'Versies', 'confirmClass': 'info', 'icon': 'fa-align-justify', 'action':'showVersions', 'inline': $scope.user.role.id >= 4, 'parentScope': true, }, 'VV':{ 'id': 'vv', 'name': 'Verleng vervaldatum', 'label': 'Verleng vervaldatum', 'icon': 'fa-repeat', 'action':'extendExpireDate', 'inline': $scope.user.rights != undefined && $scope.user.rights.quotation != undefined && $scope.user.rights.quotation.extendexpiredate != undefined && $scope.user.rights.quotation.extendexpiredate, 'parentScope': true, 'whereField': 'status', 'where': { '==': 'Verzonden', }, }, 'S':{ 'id': 's', 'name': 'Mail offerte', 'label': 'Verstuur', 'cancelName': 'Annuleren', 'confirmName': 'Stuur', 'confirmClass': 'send', 'icon': 'fa-envelope', 'action':'sendQuotation', 'inline': $scope.user.role.id >= 4, 'parentScope': true, 'whereField': 'showSend', 'where': { '!=': false, }, }, 'OML': { 'id': 'oml', 'name': 'log', 'label': 'Log', 'cancelName': 'Annuleren', 'confirmName': 'Ok', 'icon': 'fa-align-justify', 'action':'openLog', 'inline': $scope.user.role.id >= 4, 'parentScope': true, }, 'PDF':{ 'id': 'pdf', 'name': 'Zie offerte', 'label': 'PDF', 'cancelName': 'Annuleren', 'confirmName': 'Stuur', 'confirmClass': 'getPdf', 'icon': 'fa-file', 'action':'getPdf', 'inline': $scope.user.role.id >= 4, 'parentScope': true, }, 'DQ':{ 'id': 'dq', 'name': 'Weiger offerte', 'label': 'Weiger', 'header': false, 'inline': true, 'icon': 'fa-times', 'action': 'showDeclineModal', 'parentScope': true, 'whereField': 'showDeny', 'where':{ '!=': false, }, }, 'DU': { 'id': 'du', 'name': 'Offerte dupliceren', 'label': 'Kopie', 'cancelName': 'Annuleren', 'confirmName': 'Dupliceren', 'confirmClass': 'duplicate', 'header': false, 'inline': $scope.user.role.id >= 4, 'icon': 'fa-copy', 'action': 'duplicate', 'parentScope': true, }, 'CP': { // getekend contract 'id': 'cp', 'name': 'Zie contract', 'label': 'Contract', 'confirmClass': 'getPdf', 'icon': 'fa-file', 'action':'getContractPdf', 'inline': $scope.user.role.id >= 4, 'whereField': 'status', 'where': { '==': 'Geaccepteerd', }, 'parentScope': true, }, 'SC':{ 'id': 'sc', 'name': 'Mail contract', 'label': 'Verstuur contract', 'cancelName': 'Annuleren', 'confirmName': 'Stuur', 'confirmClass': 'send', 'icon': 'fa-envelope', 'action':'sendContract', 'inline': $scope.user.role.id >= 4, 'parentScope': true, 'whereField': 'status', 'where': { '==': 'Geaccepteerd', }, },/* 'O': { 'id': 'o', 'name': 'Handmatig openzetten draaiboek', 'label': 'Handmatig openzetten draaiboek', 'cancelName': 'Annuleren', 'confirmName': 'Naar draaiboek', 'confirmClass': 'offline', 'header': false, 'inline': $scope.user.role.id >= 5, 'icon': 'fa-lock', 'action': 'toScenario', 'parentScope': true, 'whereField': 'status', 'where':{ '==': 'Concept', }, }, 'O': { 'id': 'o', 'name': 'Handmatig openzetten draaiboek', 'label': 'Handmatig openzetten draaiboek', 'cancelName': 'Annuleren', 'confirmName': 'Naar draaiboek', 'confirmClass': 'offline', 'header': false, 'inline': $scope.user.role.id >= 5, 'icon': 'fa-lock', 'action': 'toScenario', 'parentScope': true, 'whereField': 'status', 'where':{ '==': 'Verzonden', }, },*/ 'OZ': { 'id': 'o', 'name': 'Handmatig openzetten draaiboek', 'label': 'Handmatig openzetten draaiboek', 'cancelName': 'Annuleren', 'confirmName': 'Naar draaiboek', 'confirmClass': 'offline', 'header': false, 'inline': $scope.user['rights'] !== undefined && $scope.user['rights']['scenario'] && $scope.user['rights']['scenario']['manualenable'] !== undefined && $scope.user['rights']['scenario']['manualenable'], 'icon': 'fa-lock', 'action': 'manualEnable', 'parentScope': true, 'whereField': 'status', 'where':{ '==': 'Geaccepteerd', }, }, 'A':{ 'id': 'a', 'name': 'Archiveer offerte', 'label': 'Archiveer', 'cancelName': 'Annuleren', 'confirmName': 'Archiveer', 'confirmClass': 'warning', 'header': false, 'inline': true, 'icon': 'fa-archive', 'action': 'toArchive', 'parentScope': true, }, 'D':{ 'id': 'd', 'name': 'Verwijder offerte', 'label': 'Verwijder', 'cancelName': 'Annuleren', 'confirmName': 'Verwijder', 'confirmClass': 'warning', 'header': false, 'inline': $scope.user.role.id >= 4, 'icon': 'fa-trash', 'action': 'delete', 'whereField': 'status', 'where':{ '!=': 'Geaccepteerd', }, }, } } $scope.manualEnable = function(btn, row, blnSubmit) { var req = { method: 'GET', url: '/scenario/manualEnable/' + row.scenario_id, } $http(req).then((response) => { if (response.status && response.data.status) { // Payment Manually processed swal({ title: 'Handmatig openzetten draaiboek verwerkt', icon: 'success', }); } else { // Error swal({ title: 'Handmatig openzetten draaiboek mislukt', icon: 'warning', }); } }); } $scope.toArchive = function(btn, row, blnSubmit) { updateData = {archived: 1}; var req = { method: 'POST', url: '/quotation/update/' + row.id, data: updateData, } $http(req).then(function(response) { if (response.data.status){ $scope.QuotationsOverview.call('initiate'); } }); } $scope.QuotationMailLog = { name: 'Mail logs', controller: 'quotation_mail_log', function: 'getByQuotationId', panelClass: 'panel-trainingskampen', showFilters: true, pageLimit: 10, fields: [ 'id', 'sent_date', 'sent_to', 'successfully_sent', ], fieldDetails: { 'id': { 'hideList': true }, 'sent_date': { 'label': 'Verstuurd op' }, 'sent_to': { 'label': 'Verstuurd naar' }, 'successfully_sent': { 'type': 'boolean', 'label': 'succesvol verstuurd', 'booleanOn': 1, 'booleanOff': 0, }, }, buttons: { 'C': { 'id': 'c', 'header': false, 'inline': false, }, 'R': { 'id': 'r', 'header': false, 'inline': false, }, 'U': { 'id': 'u', 'header': false, 'inline': false, }, 'D': { 'id': 'd', 'header': false, 'inline': false, }, } } $scope.QuotationCheckLog = { name: 'Check logs', controller: 'quotation_check_log', function: 'getByQuotationNumber/0', panelClass: 'panel-trainingskampen', showFilters: true, pageLimit: 10, autoChangeTo: ['user_id'], fields: [ 'id', 'user_id', 'message', 'approved', 'created_at', ], fieldDetails: { 'id': { 'hideList': true }, 'user_id': { 'label': 'Medewerker' }, 'message': { 'label': 'Bericht' }, 'approved': { 'type': 'boolean', 'label': 'Geaccepteerd', 'booleanOn': 1, 'booleanOff': 0, }, 'created_at': { 'label': 'Tijdstip' }, }, buttons: { 'C': { 'id': 'c', 'header': false, 'inline': false, }, 'R': { 'id': 'r', 'header': false, 'inline': false, }, 'U': { 'id': 'u', 'header': false, 'inline': false, }, 'D': { 'id': 'd', 'header': false, 'inline': false, }, } } $scope.QuotationReadLog = { name: 'Read logs', controller: 'quotation_read_log', function: 'getByQuotationNumber/0', panelClass: 'panel-trainingskampen', showFilters: true, pageLimit: 10, autoChangeTo: ['user_id'], fields: [ 'id', 'ip', 'created_at', ], fieldDetails: { 'id': { 'hideList': true }, 'ip': { 'label': 'Ip' }, 'created_at': { 'label': 'Tijdstip' }, }, buttons: { 'C': { 'id': 'c', 'header': false, 'inline': false, }, 'R': { 'id': 'r', 'header': false, 'inline': false, }, 'U': { 'id': 'u', 'header': false, 'inline': false, }, 'D': { 'id': 'd', 'header': false, 'inline': false, }, } } $scope.openLog = function(btn, row, blnSubmit) { let req = { method: 'Get', url: '/quotation_mail_log/getByQuotationId/' + row.id, } $http(req).then(function(response) { if (response.status && response.data.status) { $scope.QuotationMailLog.function = 'getByQuotationId/' + row.id; $scope.QuotationMailLog.data.rows = response.data.data; } }); $scope.QuotationCheckLog.function = 'getByQuotationNumber/' + row.quotation_number; $scope.QuotationCheckLog.call('initiate'); $scope.QuotationReadLog.function = 'getByQuotationNumber/' + row.quotation_number; $scope.QuotationReadLog.call('initiate'); $('#logModal').modal('show'); } $scope.sendContract = function(btn, row, blnSubmit) { var req = { method: 'Get', url: '/quotation/sendContract/' + row.quotation_number, } $http(req).then(function(response) { if (response.status && response.data.status) { // Refresh the overview $scope.QuotationsOverview.call('initiate'); swal('Email verstuurd','De email is met succes verstuurd naar ' + response.data.data.email, 'success'); } else { var msg = 'De e-mail kon helaas niet verzonden worden.' if (response.data.message) { msg += '\nFout: ' + response.data.message } swal('Foutmelding', msg, 'error'); } }).catch(err => { if (err) { swal('Foutmelding', 'Er is een onbekende fout opgetreden!', 'error'); console.error(err) } else { swal.stopLoading(); swal.close(); } }) } if ($scope['user']['role']['id'] == 1) { $scope.QuotationsOverview['buttons']['R']['whereField'] = 'status' $scope.QuotationsOverview['buttons']['R']['where'] = { '==': 'Verzonden', } } $scope.tableReady = function (tableName) { if (tableName === 'Offertes') { $scope.QuotationsOverview.labels = { 'of': 'van', 'rows': 'rijen', 'lastUpdated': 'Laatste wijziging', 'showNotSelectedRows': 'Toon niet geselecteerde rijen', 'showSelectedRows': 'Toon geselecteerde rijen', 'selectAllRows': 'Selecteer alle rijen', 'deSelectAllRows': 'De-selecteer alle rijen', 'selectFilteredRows': 'Selecteer gefilterde rijen', 'deSelectFilteredRows': 'De-selecteer gefilterde rijen', 'false': 'Mislukt', 'true': 'Gelukt', 'statistics': 'Statistieken', 'deleteAllRowsMessage': 'Let op! Je staat op het punt alle geselecteerde rijen te verwijderen', 'ready' : 'Klaar', 'processSubFields' : 'Verwerk subvelden', 'alreadyLoaded' : 'al ingeladen in data.', 'errorInFetching' : 'Error in ophalen', 'dataAvailable' : 'Data al beschikbaar', 'loading' : 'Laden...', }; } } $scope.toWizard = function(btn, row, blnSubmit) { $location.url('/page/wizard'); } $scope.showVersions = function(btn, row, blnSubmit) { $location.url('/page/offerteversies/' + row.quotation_number); } $scope.getPdf = function(btn, row, blnSubmit) { window.open('/quotation/createPdf/' + row.quotation_number); } $scope.getContractPdf = function(btn, row, blnSubmit) { window.open('/quotation/generatecontractWithSignature/' + row.quotation_number); } $scope.toQuotationVersions = function(btn, row, blnSubmit) { $location.url('/page/offerte_versies/'+ row['hash']); } $scope.toScenario = function(btn, row, blnSubmit) { swal({ title: 'Let op!', text: 'Weet u zeker dat het draaiboek voor offerte ' + row.quotation_number + ' aangemaakt kan worden?\nDe betaal status zal ongewijzigd blijven.\n', icon: 'warning', type: 'warning', buttons: { cancel: 'Annuleren', confirm: { text: 'Ja!', closeModal: false, }, }, }).then(confirm => { if (confirm) { // Make scenario for the quotation var req = { method: 'GET', url: '/scenario/createFromQuotation/' + row.hash, } // Make backend request to create scenario $http(req).then(function(response) { if (response.status && response.data.status) { // manually enable the scenario var req = { method: 'GET', url: '/scenario/manualEnable/' + response.data.data.id, } // Make backend request to create scenario $http(req).then(function(response) { if (response.status && response.data.status) { swal('Geslaagd', 'Het draaiboek is aangemaakt!', 'success'); } else { swal('Fout', 'Het activeren van het draaiboek is mislukt', 'error'); } $scope.QuotationsOverview.call('initiate'); }); swal('Geslaagd', 'Het draaiboek is aangemaakt!', 'success'); } else { swal('Fout', 'Het aanmaken van het draaiboek is mislukt', 'error'); } }).catch(function(err) { swal('Fout', 'Er is een onbekende fout opgetreden', 'error'); }); } }); } $scope.toQuotation = function(btn, row, blnSubmit) { if ($scope['user']['role']['id'] == 1) { if (row['status'] == 'Geaccepteerd' || row['status'] == 'Vervallen') { swal('Waarschuwing', 'Een offerte met status \'' + row['status'] + '\' kan u niet meer inkijken', 'warning'); return false; } window.open('/page/offertevalidatie/' + row['hash'], '_blank'); } else { $location.url('/page/offerte/' + row['hash']); } } // $scope.dlQuotation = function(btn, row, blnSubmit) { // $location.url('/quotation/download/' + row['hash']); // }, $scope.showDeclineModal = function(btn, row, blnSubmit) { $('#declineModal').modal('show'); $scope.hash = row['hash']; } // The quotation is declined $scope.declineOkFunction = function() { // Refresh the overview $scope.QuotationsOverview.call('initiate'); } $scope.sendQuotation = function(btn, row, blnSubmit) { var req = { method: 'POST', url: '/quotation/email/' + row.hash, } $http(req).then(function(response) { if (response.status && response.data.status) { // Refresh the overview $scope.QuotationsOverview.call('initiate'); swal('Email verstuurd','De email is met succes verstuurd naar ' + response.data.data.email, 'success'); } else { var msg = 'De e-mail kon helaas niet verzonden worden.' if (response.data.message) { msg += '\nFout: ' + response.data.message } swal('Foutmelding', msg, 'error'); } }).catch(err => { if (err) { swal('Foutmelding', 'Er is een onbekende fout opgetreden!', 'error'); console.error(err) } else { swal.stopLoading(); swal.close(); } }) } // Duplicate the quotation $scope.duplicate = function(btn, row, blnSubmit) { swal({ title: 'Let op!', text: 'U wilt offerte ' + row.quotation_number + ' dupliceren.\nWelke trainingskampnaam wenst u?', type: 'warning', content: 'input', buttons: { cancel: 'Terug', confirm: { text: 'OK', closeModal: false, }, }, }).then(name => { if (!name) { throw null; } // If the user confirms if (name.length >= 3) { // Duplicate the quotation var req = { method: 'POST', url: '/quotation/duplicate/' + row.hash, data: { campname: name, } } $http(req).then(function(response) { swal.stopLoading(); if (response.status && response.data.status) { // OK $scope.QuotationsOverview.call('initiate'); swal('Geslaagd', 'De offerte is gedupliceerd.', 'success'); } else { swal('Fout', 'Het dupliceren is mislukt', 'error'); } }).catch(function(err) { swal('Fout', 'Er is een onbekende fout opgetreden.', 'error'); }); } else { // No valid camp name swal({ title: 'Let op!', text: 'Voer een geldige kampnaam in.', type: 'warning' }); } }).catch(err => { if (err) { swal('Fout', 'Er ging iets fout.', 'error') } else { swal.stopLoading(); swal.close(); } }); } // Extend expire date of the quotation $scope.extendExpireDate = function(btn, row, blnSubmit) { swal({ title: 'Let op!', text: 'U wilt de verval datum van offerte ' + row.quotation_number + ' verlengen.\nMet hoeveel dagen wilt u de de vervaldatum verlengen?\nGeef het aantal dagen op vanaf vandaag, wil je de offerte verlengen tot aan vandaag geef dan 0 op.', type: 'warning', content: 'input', input: 'number', buttons: { cancel: 'Terug', confirm: { text: 'OK', closeModal: false, }, }, }).then(amountOfDays => { // If the user confirms if (amountOfDays != undefined && !isNaN(amountOfDays)) { // Extend expire date of the quotation var req = { method: 'POST', url: '/quotation/extendExpireDate/' + row.hash, data: { amount_of_days: amountOfDays, } } $http(req).then(function(response) { swal.stopLoading(); if (response.status && response.data.status) { // OK $scope.QuotationsOverview.call('initiate'); swal('Gelukt!', 'De vervaldatum van de offerte is verlengt met ' + amountOfDays + (amountOfDays == 1 ? ' dag': ' dagen') + '.', 'success'); } else { swal('Fout', 'Het verlengen van de vervaldatum is mislukt', 'error'); } }).catch(function(err) { swal('Fout', 'Er is een onbekende fout opgetreden.', 'error'); }); } else { // No valid amount of days swal('Let op!', 'Voer een geldig aantal dagen in.', 'warning'); } }).catch(err => { if (err) { swal('Fout', 'Er ging iets fout.', 'error') } else { swal.stopLoading(); swal.close(); } }); } });