2018-10-25 16:51:20 +02:00
|
|
|
/* eslint-disable
|
|
|
|
handle-callback-err,
|
|
|
|
no-return-assign,
|
|
|
|
no-undef,
|
|
|
|
no-useless-escape,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2018-10-25 16:50:16 +02:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2018-11-21 11:08:53 +01:00
|
|
|
'use strict';
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Controller used in the admin invoices listing page
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
Application.Controllers.controller('InvoicesController', ['$scope', '$state', 'Invoice', 'invoices', '$uibModal', 'growl', '$filter', 'Setting', 'settings', '_t',
|
|
|
|
function ($scope, $state, Invoice, invoices, $uibModal, growl, $filter, Setting, settings, _t) {
|
2018-10-25 16:50:16 +02:00
|
|
|
/* PRIVATE STATIC CONSTANTS */
|
2016-05-31 10:02:27 +02:00
|
|
|
|
2018-10-25 16:51:20 +02:00
|
|
|
// number of invoices loaded each time we click on 'load more...'
|
2018-11-21 11:08:53 +01:00
|
|
|
const INVOICES_PER_PAGE = 20;
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-10-25 16:51:20 +02:00
|
|
|
/* PUBLIC SCOPE */
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// List of all users invoices
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.invoices = invoices;
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-10-25 16:51:20 +02:00
|
|
|
// Invoices filters
|
|
|
|
$scope.searchInvoice = {
|
|
|
|
date: null,
|
|
|
|
name: '',
|
|
|
|
reference: ''
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-10-25 16:51:20 +02:00
|
|
|
// currently displayed page of invoices (search results)
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.page = 1;
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-10-25 16:51:20 +02:00
|
|
|
// true when all invoices are loaded
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.noMoreResults = false;
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// Default invoices ordering/sorting
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.orderInvoice = '-reference';
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// Invoices parameters
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.invoice = {
|
|
|
|
logo: null,
|
|
|
|
reference: {
|
|
|
|
model: '',
|
|
|
|
help: null,
|
|
|
|
templateUrl: 'editReference.html'
|
|
|
|
},
|
|
|
|
code: {
|
|
|
|
model: '',
|
|
|
|
active: true,
|
|
|
|
templateUrl: 'editCode.html'
|
|
|
|
},
|
|
|
|
number: {
|
|
|
|
model: '',
|
|
|
|
help: null,
|
|
|
|
templateUrl: 'editNumber.html'
|
|
|
|
},
|
|
|
|
VAT: {
|
|
|
|
rate: 19.6,
|
|
|
|
active: false,
|
|
|
|
templateUrl: 'editVAT.html'
|
|
|
|
},
|
|
|
|
text: {
|
|
|
|
content: ''
|
|
|
|
},
|
|
|
|
legals: {
|
|
|
|
content: ''
|
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// Placeholding date for the invoice creation
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.today = moment();
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// Placeholding date for the reservation begin
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.inOneWeek = moment().add(1, 'week').startOf('hour');
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// Placeholding date for the reservation end
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.inOneWeekAndOneHour = moment().add(1, 'week').add(1, 'hour').startOf('hour');
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Change the invoices ordering criterion to the one provided
|
|
|
|
* @param orderBy {string} ordering criterion
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.setOrderInvoice = function (orderBy) {
|
|
|
|
if ($scope.orderInvoice === orderBy) {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.orderInvoice = `-${orderBy}`;
|
2018-10-25 16:51:20 +02:00
|
|
|
} else {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.orderInvoice = orderBy;
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
|
|
|
|
2018-11-21 11:08:53 +01:00
|
|
|
resetSearchInvoice();
|
|
|
|
return invoiceSearch();
|
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Open a modal window asking the admin the details to refund the user about the provided invoice
|
|
|
|
* @param invoice {Object} invoice inherited from angular's $resource
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.generateAvoirForInvoice = function (invoice) {
|
2019-01-03 16:50:50 +01:00
|
|
|
// open modal
|
2018-10-25 16:51:20 +02:00
|
|
|
const modalInstance = $uibModal.open({
|
|
|
|
templateUrl: '<%= asset_path "admin/invoices/avoirModal.html" %>',
|
|
|
|
controller: 'AvoirModalController',
|
|
|
|
resolve: {
|
2018-11-21 11:08:53 +01:00
|
|
|
invoice () { return invoice; }
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
});
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-10-25 16:51:20 +02:00
|
|
|
// once done, update the invoice model and inform the admin
|
|
|
|
return modalInstance.result.then(function (res) {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.invoices.unshift(res.avoir);
|
2018-10-25 16:51:20 +02:00
|
|
|
return Invoice.get({ id: invoice.id }, function (data) {
|
2018-11-21 11:08:53 +01:00
|
|
|
invoice.has_avoir = data.has_avoir;
|
2019-01-03 17:39:50 +01:00
|
|
|
return growl.success(_t('invoices.refund_invoice_successfully_created'));
|
2018-11-21 11:08:53 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Generate an invoice reference sample from the parametrized model
|
|
|
|
* @returns {string} invoice reference sample
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.mkReference = function () {
|
2018-11-21 11:08:53 +01:00
|
|
|
let sample = $scope.invoice.reference.model;
|
2018-10-25 16:51:20 +02:00
|
|
|
if (sample) {
|
|
|
|
// invoice number per day (dd..dd)
|
2018-11-21 11:08:53 +01:00
|
|
|
sample = sample.replace(/d+(?![^\[]*])/g, function (match, offset, string) { return padWithZeros(2, match.length); });
|
2018-10-25 16:51:20 +02:00
|
|
|
// invoice number per month (mm..mm)
|
2018-11-21 11:08:53 +01:00
|
|
|
sample = sample.replace(/m+(?![^\[]*])/g, function (match, offset, string) { return padWithZeros(12, match.length); });
|
2018-10-25 16:51:20 +02:00
|
|
|
// invoice number per year (yy..yy)
|
2018-11-21 11:08:53 +01:00
|
|
|
sample = sample.replace(/y+(?![^\[]*])/g, function (match, offset, string) { return padWithZeros(8, match.length); });
|
2018-10-25 16:51:20 +02:00
|
|
|
// date information
|
2018-11-21 11:08:53 +01:00
|
|
|
sample = sample.replace(/[YMD]+(?![^\[]*])/g, function (match, offset, string) { return $scope.today.format(match); });
|
2018-10-25 16:51:20 +02:00
|
|
|
// information about online selling (X[text])
|
2018-11-21 11:08:53 +01:00
|
|
|
sample = sample.replace(/X\[([^\]]+)\]/g, function (match, p1, offset, string) { return p1; });
|
2018-10-25 16:51:20 +02:00
|
|
|
// information about wallet (W[text]) - does not apply here
|
2018-11-21 11:08:53 +01:00
|
|
|
sample = sample.replace(/W\[([^\]]+)\]/g, '');
|
2018-10-25 16:51:20 +02:00
|
|
|
// information about refunds (R[text]) - does not apply here
|
2018-11-21 11:08:53 +01:00
|
|
|
sample = sample.replace(/R\[([^\]]+)\]/g, '');
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
return sample;
|
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Generate an order nmuber sample from the parametrized model
|
|
|
|
* @returns {string} invoice reference sample
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.mkNumber = function () {
|
2018-11-21 11:08:53 +01:00
|
|
|
let sample = $scope.invoice.number.model;
|
2018-10-25 16:51:20 +02:00
|
|
|
if (sample) {
|
|
|
|
// global order number (nn..nn)
|
2018-11-21 11:08:53 +01:00
|
|
|
sample = sample.replace(/n+(?![^\[]*])/g, function (match, offset, string) { return padWithZeros(327, match.length); });
|
2018-10-25 16:51:20 +02:00
|
|
|
// order number per year (yy..yy)
|
2018-11-21 11:08:53 +01:00
|
|
|
sample = sample.replace(/y+(?![^\[]*])/g, function (match, offset, string) { return padWithZeros(8, match.length); });
|
2018-10-25 16:51:20 +02:00
|
|
|
// order number per month (mm..mm)
|
2018-11-21 11:08:53 +01:00
|
|
|
sample = sample.replace(/m+(?![^\[]*])/g, function (match, offset, string) { return padWithZeros(12, match.length); });
|
2018-10-25 16:51:20 +02:00
|
|
|
// order number per day (dd..dd)
|
2018-11-21 11:08:53 +01:00
|
|
|
sample = sample.replace(/d+(?![^\[]*])/g, function (match, offset, string) { return padWithZeros(2, match.length); });
|
2018-10-25 16:51:20 +02:00
|
|
|
// date information
|
2018-11-21 11:08:53 +01:00
|
|
|
sample = sample.replace(/[YMD]+(?![^\[]*])/g, function (match, offset, string) { return $scope.today.format(match); });
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
return sample;
|
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Open a modal dialog allowing the user to edit the invoice reference generation template
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.openEditReference = function () {
|
|
|
|
const modalInstance = $uibModal.open({
|
|
|
|
animation: true,
|
|
|
|
templateUrl: $scope.invoice.reference.templateUrl,
|
|
|
|
size: 'lg',
|
|
|
|
resolve: {
|
|
|
|
model () {
|
2018-11-21 11:08:53 +01:00
|
|
|
return $scope.invoice.reference.model;
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-10-25 16:50:16 +02:00
|
|
|
},
|
2018-11-21 15:29:35 +01:00
|
|
|
controller: ['$scope', '$uibModalInstance', 'model', function ($scope, $uibModalInstance, model) {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.model = model;
|
|
|
|
$scope.ok = function () { $uibModalInstance.close($scope.model); };
|
|
|
|
$scope.cancel = function () { $uibModalInstance.dismiss('cancel'); };
|
2018-11-21 15:29:35 +01:00
|
|
|
}]
|
2018-11-21 11:08:53 +01:00
|
|
|
});
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-20 12:26:06 +01:00
|
|
|
modalInstance.result.then(function (model) {
|
2018-10-25 16:51:20 +02:00
|
|
|
Setting.update({ name: 'invoice_reference' }, { value: model }, function (data) {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.invoice.reference.model = model;
|
2019-01-03 17:39:50 +01:00
|
|
|
growl.success(_t('invoices.invoice_reference_successfully_saved'));
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
|
|
|
, function (error) {
|
2019-01-03 17:39:50 +01:00
|
|
|
growl.error(_t('invoices.an_error_occurred_while_saving_invoice_reference'));
|
2018-11-21 11:08:53 +01:00
|
|
|
console.error(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Open a modal dialog allowing the user to edit the invoice code
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.openEditCode = function () {
|
|
|
|
const modalInstance = $uibModal.open({
|
|
|
|
animation: true,
|
|
|
|
templateUrl: $scope.invoice.code.templateUrl,
|
|
|
|
size: 'lg',
|
|
|
|
resolve: {
|
|
|
|
model () {
|
2018-11-21 11:08:53 +01:00
|
|
|
return $scope.invoice.code.model;
|
2018-10-25 16:51:20 +02:00
|
|
|
},
|
|
|
|
active () {
|
2018-11-21 11:08:53 +01:00
|
|
|
return $scope.invoice.code.active;
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
|
|
|
},
|
2018-11-21 15:29:35 +01:00
|
|
|
controller: ['$scope', '$uibModalInstance', 'model', 'active', function ($scope, $uibModalInstance, model, active) {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.codeModel = model;
|
|
|
|
$scope.isSelected = active;
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.ok = function () { $uibModalInstance.close({ model: $scope.codeModel, active: $scope.isSelected }); };
|
|
|
|
$scope.cancel = function () { $uibModalInstance.dismiss('cancel'); };
|
2018-11-21 15:29:35 +01:00
|
|
|
}]
|
2018-11-21 11:08:53 +01:00
|
|
|
});
|
2018-10-25 16:51:20 +02:00
|
|
|
|
|
|
|
return modalInstance.result.then(function (result) {
|
|
|
|
Setting.update({ name: 'invoice_code-value' }, { value: result.model }, function (data) {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.invoice.code.model = result.model;
|
2018-10-25 16:51:20 +02:00
|
|
|
if (result.active) {
|
2019-01-03 17:39:50 +01:00
|
|
|
return growl.success(_t('invoices.invoicing_code_succesfully_saved'));
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2018-10-25 16:51:20 +02:00
|
|
|
, function (error) {
|
2019-01-03 17:39:50 +01:00
|
|
|
growl.error(_t('invoices.an_error_occurred_while_saving_the_invoicing_code'));
|
2018-11-21 11:08:53 +01:00
|
|
|
return console.error(error);
|
|
|
|
});
|
2018-10-25 16:51:20 +02:00
|
|
|
|
|
|
|
return Setting.update({ name: 'invoice_code-active' }, { value: result.active ? 'true' : 'false' }, function (data) {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.invoice.code.active = result.active;
|
2018-10-25 16:51:20 +02:00
|
|
|
if (result.active) {
|
2019-01-03 17:39:50 +01:00
|
|
|
return growl.success(_t('invoices.code_successfully_activated'));
|
2018-10-25 16:51:20 +02:00
|
|
|
} else {
|
2019-01-03 17:39:50 +01:00
|
|
|
return growl.success(_t('invoices.code_successfully_disabled'));
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2018-10-25 16:51:20 +02:00
|
|
|
, function (error) {
|
2019-01-03 17:39:50 +01:00
|
|
|
growl.error(_t('invoices.an_error_occurred_while_activating_the_invoicing_code'));
|
2018-11-21 11:08:53 +01:00
|
|
|
return console.error(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Open a modal dialog allowing the user to edit the invoice number
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.openEditInvoiceNb = function () {
|
|
|
|
const modalInstance = $uibModal.open({
|
|
|
|
animation: true,
|
|
|
|
templateUrl: $scope.invoice.number.templateUrl,
|
|
|
|
size: 'lg',
|
|
|
|
resolve: {
|
|
|
|
model () {
|
2018-11-21 11:08:53 +01:00
|
|
|
return $scope.invoice.number.model;
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-10-25 16:50:16 +02:00
|
|
|
},
|
2018-11-21 15:29:35 +01:00
|
|
|
controller: ['$scope', '$uibModalInstance', 'model', function ($scope, $uibModalInstance, model) {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.model = model;
|
|
|
|
$scope.ok = function () { $uibModalInstance.close($scope.model); };
|
|
|
|
$scope.cancel = function () { $uibModalInstance.dismiss('cancel'); };
|
2018-11-21 15:29:35 +01:00
|
|
|
}]
|
2018-11-21 11:08:53 +01:00
|
|
|
});
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-20 12:26:06 +01:00
|
|
|
return modalInstance.result.then(function (model) {
|
2018-10-25 16:51:20 +02:00
|
|
|
Setting.update({ name: 'invoice_order-nb' }, { value: model }, function (data) {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.invoice.number.model = model;
|
2019-01-03 17:39:50 +01:00
|
|
|
return growl.success(_t('invoices.order_number_successfully_saved'));
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2018-10-25 16:51:20 +02:00
|
|
|
, function (error) {
|
2019-01-03 17:39:50 +01:00
|
|
|
growl.error(_t('invoices.an_error_occurred_while_saving_the_order_number'));
|
2018-11-21 11:08:53 +01:00
|
|
|
return console.error(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Open a modal dialog allowing the user to edit the VAT parameters for the invoices
|
|
|
|
* The VAT can be disabled and its rate can be configured
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.openEditVAT = function () {
|
|
|
|
const modalInstance = $uibModal.open({
|
|
|
|
animation: true,
|
|
|
|
templateUrl: $scope.invoice.VAT.templateUrl,
|
|
|
|
size: 'lg',
|
|
|
|
resolve: {
|
|
|
|
rate () {
|
2018-11-21 11:08:53 +01:00
|
|
|
return $scope.invoice.VAT.rate;
|
2018-10-25 16:51:20 +02:00
|
|
|
},
|
|
|
|
active () {
|
2018-11-21 11:08:53 +01:00
|
|
|
return $scope.invoice.VAT.active;
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
|
|
|
},
|
2018-11-21 15:29:35 +01:00
|
|
|
controller: ['$scope', '$uibModalInstance', 'rate', 'active', function ($scope, $uibModalInstance, rate, active) {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.rate = rate;
|
|
|
|
$scope.isSelected = active;
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.ok = function () { $uibModalInstance.close({ rate: $scope.rate, active: $scope.isSelected }); };
|
|
|
|
return $scope.cancel = function () { $uibModalInstance.dismiss('cancel'); };
|
2018-11-21 15:29:35 +01:00
|
|
|
}]
|
2018-11-21 11:08:53 +01:00
|
|
|
});
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-10-25 16:51:20 +02:00
|
|
|
return modalInstance.result.then(function (result) {
|
|
|
|
Setting.update({ name: 'invoice_VAT-rate' }, { value: result.rate + '' }, function (data) {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.invoice.VAT.rate = result.rate;
|
2018-10-25 16:51:20 +02:00
|
|
|
if (result.active) {
|
2019-01-03 17:39:50 +01:00
|
|
|
return growl.success(_t('invoices.VAT_rate_successfully_saved'));
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
, function (error) {
|
2019-01-03 17:39:50 +01:00
|
|
|
growl.error(_t('invoices.an_error_occurred_while_saving_the_VAT_rate'));
|
2018-11-21 11:08:53 +01:00
|
|
|
return console.error(error);
|
|
|
|
});
|
2018-10-25 16:51:20 +02:00
|
|
|
|
|
|
|
return Setting.update({ name: 'invoice_VAT-active' }, { value: result.active ? 'true' : 'false' }, function (data) {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.invoice.VAT.active = result.active;
|
2018-10-25 16:51:20 +02:00
|
|
|
if (result.active) {
|
2019-01-03 17:39:50 +01:00
|
|
|
return growl.success(_t('invoices.VAT_successfully_activated'));
|
2018-10-25 16:51:20 +02:00
|
|
|
} else {
|
2019-01-03 17:39:50 +01:00
|
|
|
return growl.success(_t('invoices.VAT_successfully_disabled'));
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
, function (error) {
|
2019-01-03 17:39:50 +01:00
|
|
|
growl.error(_t('invoices.an_error_occurred_while_activating_the_VAT'));
|
2018-11-21 11:08:53 +01:00
|
|
|
return console.error(error);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Callback to save the value of the text zone when editing is done
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.textEditEnd = function (event) {
|
2018-11-21 11:08:53 +01:00
|
|
|
const parsed = parseHtml($scope.invoice.text.content);
|
2018-10-25 16:51:20 +02:00
|
|
|
return Setting.update({ name: 'invoice_text' }, { value: parsed }, function (data) {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.invoice.text.content = parsed;
|
2019-01-03 17:39:50 +01:00
|
|
|
return growl.success(_t('invoices.text_successfully_saved'));
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2018-10-25 16:51:20 +02:00
|
|
|
, function (error) {
|
2019-01-03 17:39:50 +01:00
|
|
|
growl.error(_t('invoices.an_error_occurred_while_saving_the_text'));
|
2018-11-21 11:08:53 +01:00
|
|
|
return console.error(error);
|
|
|
|
});
|
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Callback to save the value of the legal information zone when editing is done
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.legalsEditEnd = function (event) {
|
2018-11-21 11:08:53 +01:00
|
|
|
const parsed = parseHtml($scope.invoice.legals.content);
|
2018-10-25 16:51:20 +02:00
|
|
|
return Setting.update({ name: 'invoice_legals' }, { value: parsed }, function (data) {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.invoice.legals.content = parsed;
|
2019-01-03 17:39:50 +01:00
|
|
|
return growl.success(_t('invoices.address_and_legal_information_successfully_saved'));
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
|
|
|
, function (error) {
|
2019-01-03 17:39:50 +01:00
|
|
|
growl.error(_t('invoices.an_error_occurred_while_saving_the_address_and_the_legal_information'));
|
2018-11-21 11:08:53 +01:00
|
|
|
return console.error(error);
|
|
|
|
});
|
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Callback when any of the filters changes.
|
|
|
|
* Full reload the results list
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.handleFilterChange = function () {
|
2018-11-21 11:08:53 +01:00
|
|
|
resetSearchInvoice();
|
|
|
|
return invoiceSearch();
|
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Callback for the 'load more' button.
|
|
|
|
* Will load the next results of the current search, if any
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.showNextInvoices = function () {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.page += 1;
|
|
|
|
return invoiceSearch(true);
|
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-10-25 16:51:20 +02:00
|
|
|
/* PRIVATE SCOPE */
|
2016-05-31 10:02:27 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Kind of constructor: these actions will be realized first when the controller is loaded
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
const initialize = function () {
|
2018-10-25 16:50:16 +02:00
|
|
|
if (!invoices[0] || (invoices[0].maxInvoices <= $scope.invoices.length)) {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.noMoreResults = true;
|
2018-10-25 16:50:16 +02:00
|
|
|
}
|
2016-05-31 10:02:27 +02:00
|
|
|
|
2018-10-25 16:51:20 +02:00
|
|
|
// retrieve settings from the DB through the API
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.invoice.legals.content = settings['invoice_legals'];
|
|
|
|
$scope.invoice.text.content = settings['invoice_text'];
|
|
|
|
$scope.invoice.VAT.rate = parseFloat(settings['invoice_VAT-rate']);
|
|
|
|
$scope.invoice.VAT.active = (settings['invoice_VAT-active'] === 'true');
|
|
|
|
$scope.invoice.number.model = settings['invoice_order-nb'];
|
|
|
|
$scope.invoice.code.model = settings['invoice_code-value'];
|
|
|
|
$scope.invoice.code.active = (settings['invoice_code-active'] === 'true');
|
|
|
|
$scope.invoice.reference.model = settings['invoice_reference'];
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.invoice.logo = {
|
|
|
|
filetype: 'image/png',
|
|
|
|
filename: 'logo.png',
|
|
|
|
base64: settings['invoice_logo']
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2016-05-31 10:02:27 +02:00
|
|
|
|
2018-10-25 16:51:20 +02:00
|
|
|
// Watch the logo, when a change occurs, save it
|
|
|
|
return $scope.$watch('invoice.logo', function () {
|
|
|
|
if ($scope.invoice.logo && $scope.invoice.logo.filesize) {
|
2018-11-20 12:26:06 +01:00
|
|
|
return Setting.update(
|
|
|
|
{ name: 'invoice_logo' },
|
|
|
|
{ value: $scope.invoice.logo.base64 },
|
2019-01-03 17:39:50 +01:00
|
|
|
function (data) { growl.success(_t('invoices.logo_successfully_saved')); },
|
2018-11-20 12:26:06 +01:00
|
|
|
function (error) {
|
2019-01-03 17:39:50 +01:00
|
|
|
growl.error(_t('invoices.an_error_occurred_while_saving_the_logo'));
|
2018-11-21 11:08:53 +01:00
|
|
|
return console.error(error);
|
2018-11-20 12:26:06 +01:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
);
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
});
|
|
|
|
};
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Output the given integer with leading zeros. If the given value is longer than the given
|
|
|
|
* length, it will be truncated.
|
|
|
|
* @param value {number} the integer to pad
|
|
|
|
* @param length {number} the length of the resulting string.
|
|
|
|
*/
|
2018-11-21 11:08:53 +01:00
|
|
|
var padWithZeros = function (value, length) { return (1e15 + value + '').slice(-length); };
|
2018-10-25 16:51:20 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Remove every unsupported html tag from the given html text (like <p>, <span>, ...).
|
|
|
|
* The supported tags are <b>, <u>, <i> and <br>.
|
|
|
|
* @param html {string} single line html text
|
|
|
|
* @return {string} multi line simplified html text
|
|
|
|
*/
|
2018-11-20 12:26:06 +01:00
|
|
|
var parseHtml = function (html) {
|
|
|
|
return html.replace(/<\/?(\w+)((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)\/?>/g, function (match, p1, offset, string) {
|
2018-10-25 16:51:20 +02:00
|
|
|
if (['b', 'u', 'i', 'br'].includes(p1)) {
|
2018-11-21 11:08:53 +01:00
|
|
|
return match;
|
2018-10-25 16:51:20 +02:00
|
|
|
} else {
|
2018-11-21 11:08:53 +01:00
|
|
|
return '';
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
});
|
|
|
|
};
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Reinitialize the context of invoices' search to display new results set
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
var resetSearchInvoice = function () {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.page = 1;
|
|
|
|
return $scope.noMoreResults = false;
|
|
|
|
};
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Run a search query with the current parameters set concerning invoices, then affect or concat the results
|
|
|
|
* to $scope.invoices
|
|
|
|
* @param concat {boolean} if true, the result will be append to $scope.invoices instead of being affected
|
|
|
|
*/
|
2018-11-21 10:59:07 +01:00
|
|
|
var invoiceSearch = function (concat) {
|
2018-10-25 16:51:20 +02:00
|
|
|
Invoice.list({
|
|
|
|
query: {
|
|
|
|
number: $scope.searchInvoice.reference,
|
|
|
|
customer: $scope.searchInvoice.name,
|
|
|
|
date: $scope.searchInvoice.date,
|
|
|
|
order_by: $scope.orderInvoice,
|
|
|
|
page: $scope.page,
|
|
|
|
size: INVOICES_PER_PAGE
|
|
|
|
}
|
|
|
|
}, function (invoices) {
|
|
|
|
if (concat) {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.invoices = $scope.invoices.concat(invoices);
|
2018-10-25 16:51:20 +02:00
|
|
|
} else {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.invoices = invoices;
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-10-25 16:51:20 +02:00
|
|
|
if (!invoices[0] || (invoices[0].maxInvoices <= $scope.invoices.length)) {
|
2018-11-21 11:08:53 +01:00
|
|
|
return $scope.noMoreResults = true;
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
});
|
|
|
|
};
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// !!! MUST BE CALLED AT THE END of the controller
|
2018-11-21 11:08:53 +01:00
|
|
|
return initialize();
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
]);
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Controller used in the invoice refunding modal window
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
Application.Controllers.controller('AvoirModalController', ['$scope', '$uibModalInstance', 'invoice', 'Invoice', 'growl', '_t',
|
|
|
|
function ($scope, $uibModalInstance, invoice, Invoice, growl, _t) {
|
2019-01-03 16:50:50 +01:00
|
|
|
/* PUBLIC SCOPE */
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// invoice linked to the current refund
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.invoice = invoice;
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// Associative array containing invoice_item ids associated with boolean values
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.partial = {};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// Default refund parameters
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.avoir = {
|
|
|
|
invoice_id: invoice.id,
|
|
|
|
subscription_to_expire: false,
|
|
|
|
invoice_items_ids: []
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// Possible refunding methods
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.avoirModes = [
|
2019-01-03 17:39:50 +01:00
|
|
|
{ name: _t('invoices.none'), value: 'none' },
|
|
|
|
{ name: _t('invoices.by_cash'), value: 'cash' },
|
|
|
|
{ name: _t('invoices.by_cheque'), value: 'cheque' },
|
|
|
|
{ name: _t('invoices.by_transfer'), value: 'transfer' },
|
|
|
|
{ name: _t('invoices.by_wallet'), value: 'wallet' }
|
2018-11-21 11:08:53 +01:00
|
|
|
];
|
2018-10-25 16:51:20 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// If a subscription was took with the current invoice, should it be canceled or not
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.subscriptionExpireOptions = {};
|
|
|
|
$scope.subscriptionExpireOptions[_t('yes')] = true;
|
|
|
|
$scope.subscriptionExpireOptions[_t('no')] = false;
|
2018-10-25 16:51:20 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// AngularUI-Bootstrap datepicker parameters to define when to refund
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.datePicker = {
|
|
|
|
format: Fablab.uibDateFormat,
|
|
|
|
opened: false, // default: datePicker is not shown
|
|
|
|
options: {
|
|
|
|
startingDay: Fablab.weekStartingDay
|
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Callback to open the datepicker
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.openDatePicker = function ($event) {
|
2018-11-21 11:08:53 +01:00
|
|
|
$event.preventDefault();
|
|
|
|
$event.stopPropagation();
|
2019-01-03 16:50:50 +01:00
|
|
|
$scope.datePicker.opened = true;
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Validate the refunding and generate a refund invoice
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
$scope.ok = function () {
|
2019-01-03 16:50:50 +01:00
|
|
|
// check that at least 1 element of the invoice is refunded
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.avoir.invoice_items_ids = [];
|
2018-10-25 16:51:20 +02:00
|
|
|
for (let itemId in $scope.partial) {
|
2018-11-21 11:08:53 +01:00
|
|
|
const refundItem = $scope.partial[itemId];
|
|
|
|
if (refundItem) { $scope.avoir.invoice_items_ids.push(parseInt(itemId)); }
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-10-25 16:50:16 +02:00
|
|
|
|
2018-10-25 16:51:20 +02:00
|
|
|
if ($scope.avoir.invoice_items_ids.length === 0) {
|
2019-01-03 17:39:50 +01:00
|
|
|
return growl.error(_t('invoices.you_must_select_at_least_one_element_to_create_a_refund'));
|
2018-10-25 16:51:20 +02:00
|
|
|
} else {
|
2018-11-20 12:26:06 +01:00
|
|
|
return Invoice.save(
|
|
|
|
{ avoir: $scope.avoir },
|
2018-11-21 10:59:07 +01:00
|
|
|
function (avoir) { // success
|
2018-11-21 11:08:53 +01:00
|
|
|
$uibModalInstance.close({ avoir, invoice: $scope.invoice });
|
2018-11-20 12:26:06 +01:00
|
|
|
},
|
|
|
|
function (err) { // failed
|
2019-01-03 17:39:50 +01:00
|
|
|
growl.error(_t('invoices.unable_to_create_the_refund'));
|
2018-11-20 12:26:06 +01:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
);
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2019-01-03 16:50:50 +01:00
|
|
|
/**
|
2018-11-19 16:17:49 +01:00
|
|
|
* Cancel the refund, dismiss the modal window
|
|
|
|
*/
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.cancel = function () { $uibModalInstance.dismiss('cancel'); };
|
2018-10-25 16:51:20 +02:00
|
|
|
|
|
|
|
/* PRIVATE SCOPE */
|
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
/**
|
|
|
|
* Kind of constructor: these actions will be realized first when the controller is loaded
|
|
|
|
*/
|
2018-10-25 16:51:20 +02:00
|
|
|
const initialize = function () {
|
2018-11-19 16:17:49 +01:00
|
|
|
// if the invoice was payed with stripe, allow to refund through stripe
|
2018-10-25 16:51:20 +02:00
|
|
|
Invoice.get({ id: invoice.id }, function (data) {
|
2018-11-21 11:08:53 +01:00
|
|
|
$scope.invoice = data;
|
2018-10-25 16:51:20 +02:00
|
|
|
// default : all elements of the invoice are refund
|
2018-11-20 12:26:06 +01:00
|
|
|
return Array.from(data.items).map(function (item) {
|
2018-11-21 11:08:53 +01:00
|
|
|
return ($scope.partial[item.id] = (typeof item.avoir_item_id !== 'number'));
|
|
|
|
});
|
|
|
|
});
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-10-25 16:51:20 +02:00
|
|
|
if (invoice.stripe) {
|
2019-01-03 17:39:50 +01:00
|
|
|
return $scope.avoirModes.push({ name: _t('invoices.online_payment'), value: 'stripe' });
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
};
|
2016-03-23 18:39:41 +01:00
|
|
|
|
2018-11-19 16:17:49 +01:00
|
|
|
// !!! MUST BE CALLED AT THE END of the controller
|
2018-11-21 11:08:53 +01:00
|
|
|
return initialize();
|
2018-10-25 16:51:20 +02:00
|
|
|
}
|
2018-11-21 11:08:53 +01:00
|
|
|
]);
|