|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292 |
- /**
- Copyright Souke (2018)
-
- contact@souke.fr
-
- Ce logiciel est un programme informatique servant à aider les producteurs
- à distribuer leur production en circuits courts.
-
- Ce logiciel est régi par la licence CeCILL soumise au droit français et
- respectant les principes de diffusion des logiciels libres. Vous pouvez
- utiliser, modifier et/ou redistribuer ce programme sous les conditions
- de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA
- sur le site "http://www.cecill.info".
-
- En contrepartie de l'accessibilité au code source et des droits de copie,
- de modification et de redistribution accordés par cette licence, il n'est
- offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons,
- seule une responsabilité restreinte pèse sur l'auteur du programme, le
- titulaire des droits patrimoniaux et les concédants successifs.
-
- A cet égard l'attention de l'utilisateur est attirée sur les risques
- associés au chargement, à l'utilisation, à la modification et/ou au
- développement et à la reproduction du logiciel par l'utilisateur étant
- donné sa spécificité de logiciel libre, qui peut le rendre complexe à
- manipuler et qui le réserve donc à des développeurs et des professionnels
- avertis possédant des connaissances informatiques approfondies. Les
- utilisateurs sont donc invités à charger et tester l'adéquation du
- logiciel à leurs besoins dans des conditions permettant d'assurer la
- sécurité de leurs systèmes et ou de leurs données et, plus généralement,
- à l'utiliser et l'exploiter dans les mêmes conditions de sécurité.
-
- Le fait que vous puissiez accéder à cet en-tête signifie que vous avez
- pris connaissance de la licence CeCILL, et que vous en avez accepté les
- termes.
- */
-
- var selector = '#app-distribution-index';
- if($(selector).length) {
- var app = new Vue({
- el: selector,
- data() {
- return Object.assign(
- {
- UrlManager: UrlManager,
- baseUrl: $('meta[name=baseurl]').attr('content'),
- date: null,
- dateFormat: null,
- loading: true,
- distribution: {
- active: false
- },
- producer: null,
- oneDistributionWeekActive: false,
- products: [],
- countActiveProducts: 0,
- pointsSale: [],
- meansPayment: [],
- idActivePointSale: 0,
- pointSaleActive: null,
- idDefaultPointSale: 0,
- countActivePointsSale: 0,
- countOrdersByPointSale: [],
- orders: [],
- ordersUpdate: [],
- countOrders: 0,
- users: [],
- deliveryNotes: [],
- showModalProducts: false,
- showModalPointsSale: false,
- showModalFormOrderCreate: false,
- orderCreate: null,
- showModalFormOrderUpdate: false,
- idOrderUpdate: 0,
- showViewProduct: false,
- idOrderView: 0,
- showModalPayment: false,
- idOrderPayment: 0,
- showLoading: false,
- tillerIsAuthenticated: false,
- tillerUrlAuthorizeCode: '#',
- tillerIsSynchro: false,
- checkboxSelectAllOrders: false,
- messageGenerateDeliveryNoteDisplayed: false,
- missingSubscriptions: false,
- loadingUpdateProductOrder: false,
- units: [],
- calendar: {
- mode: 'single',
- attrs: [],
- themeStyles: {
- wrapper: {
- background: '#F7F7F7',
- color: '#333',
- border: 'solid 1px #e0e0e0'
- },
- header: {
- padding: '10px 10px',
- },
- headerHorizontalDivider: {
- borderTop: 'solid rgba(255, 255, 255, 0.2) 1px',
- width: '80%',
- },
- weekdays: {
- color: '#e0e0e0',
- fontWeight: '600',
- padding: '10px 10px',
- fontSize: '2rem'
- },
- weeks: {
- padding: '0 15px 15px 15px',
- },
- dayContent: function (object) {
- var style = {
- fontSize: '2rem',
- padding: '16px',
- };
-
- if (object.isHovered || object.isFocus) {
- style.backgroundColor = '#F39C12';
- }
-
- return style;
- },
- },
- formats: {
- dayPopover: 'DD/MM/YYYY'
- }
- },
- }, window.vueValues);
- },
- mounted: function () {
- if ($('#distribution-date').size()) {
- this.date = new Date($('#distribution-date').html());
- this.dateFormat = ('0' + this.date.getDate()).slice(-2) + '/'
- + ('0' + (this.date.getMonth() + 1)).slice(-2) + '/'
- + this.date.getFullYear();
- }
- this.init(false, true);
- this.loading = false;
- },
- methods: {
- getDate: function () {
- return this.formatDate(this.date);
- },
- formatDate: function (date) {
- if (date) {
- return date.getFullYear() + '-'
- + ('0' + (date.getMonth() + 1)).slice(-2) + '-'
- + ('0' + date.getDate()).slice(-2);
- }
- return false;
- },
- isDistributionToday: function () {
- var today = new Date();
- return this.date
- && today.getFullYear() == this.date.getFullYear()
- && today.getMonth() == this.date.getMonth()
- && today.getDay() == this.date.getDay();
- },
- init: function (idActivePointSale, displayFormOrderCreate) {
- var app = this;
- this.showLoading = true;
-
- axios.get("ajax-infos", {params: {date: this.getDate()}})
- .then(function (response) {
- app.calendar.attrs = [];
- app.units = response.data.units;
- app.distribution = response.data.distribution;
- app.producer = response.data.producer;
- app.products = response.data.products;
- app.initCountActiveProducts();
- app.meansPayment = response.data.means_payment;
- app.oneDistributionWeekActive = response.data.one_distribution_week_active;
- app.missingSubscriptions = response.data.missing_subscriptions;
- app.countOrders = 0;
- if (response.data.orders) {
- app.orders = JSON.parse(JSON.stringify(response.data.orders));
- app.ordersUpdate = JSON.parse(JSON.stringify(response.data.orders));
-
- for (i = 0; i < app.orders.length; i++) {
- if (app.orders[i].order_status_alias == 'ordered' || app.orders[i].order_status_alias == 'updated') {
- app.countOrders++;
- }
- }
- } else {
- app.orders = [];
- }
-
- if (response.data.order_create) {
- app.orderCreate = response.data.order_create;
- app.idDefaultPointSale = app.orderCreate.id_point_sale;
- }
-
- if (response.data.points_sale) {
- app.pointsSale = response.data.points_sale;
- app.initPointsSale(idActivePointSale);
- } else {
- app.pointsSale = [];
- }
-
- if (response.data.users) {
- app.users = response.data.users;
- }
-
- if (response.data.delivery_notes) {
- app.deliveryNotes = response.data.delivery_notes;
- }
-
- app.tillerUrlAuthorizeCode = response.data.tiller_url_authorize_code;
- app.tillerIsAuthenticated = response.data.tiller_is_authenticated;
- app.tillerIsSynchro = response.data.tiller_is_synchro;
-
- app.calendar.attrs = [];
- var distributions = response.data.distributions;
- var leave_period_dates = response.data.leave_period_dates;
- var dayCurrentIsDistributionActive = false;
- var dateFormatCompare = false;
- if (app.date) {
- dateFormatCompare = app.date.getFullYear() + '-'
- + ('0' + (app.date.getMonth() + 1)).slice(-2) + '-'
- + ('0' + app.date.getDate()).slice(-2);
- }
-
- if (distributions.length) {
- var fillMode = 'solid';
- var dayToday = app.formatDate(new Date());
- for (var i = 0; i < distributions.length; i++) {
- if (distributions[i].date < dayToday) {
- fillMode = 'outline';
- } else {
- fillMode = 'solid';
- }
-
- app.calendar.attrs.push({
- key: distributions[i].date,
- dates: distributions[i].date,
- highlight: {
- color: 'green',
- fillMode: fillMode
- }
- });
-
- if (distributions[i].date == dateFormatCompare) {
- dayCurrentIsDistributionActive = true;
- }
- }
- }
-
- // leave period
- if (leave_period_dates.length) {
- app.calendar.attrs.push({
- key: 'leave_period',
- dates: leave_period_dates,
- highlight: {
- color: 'gray',
- fillMode: 'solid'
- }
- });
- }
-
- app.showLoading = false;
- app.checkboxSelectAllOrders = false;
-
- let searchParams = new URLSearchParams(window.location.search);
- if (searchParams.has('message_generate_bl') && !app.messageGenerateDeliveryNoteDisplayed) {
- appAlerts.alert('info', 'Pour générer un bon de livraison, sélectionnez tout d\'abord un jour et un lieu de distribution.', 6000);
- app.messageGenerateDeliveryNoteDisplayed = true;
- }
-
- if (app.idOrderUpdate) {
- app.updateOrderFromUrl();
- }
-
- setTimeout("opendistrib_popover(); opendistrib_dropdown_tooltip();", 500);
-
- var highlightStyle = {
- color: 'black',
- fillMode: 'light'
- }
- if (dayCurrentIsDistributionActive) {
- highlightStyle = {
- color: 'orange',
- fillMode: 'solid'
- }
- }
- app.calendar.attrs.push({
- key: 'current',
- highlight: highlightStyle,
- dates: app.date
- });
-
- if(displayFormOrderCreate && searchParams.has('displayFormOrderCreate')) {
- app.openModalFormOrderCreate();
- }
- });
- },
- initCountActiveProducts: function () {
- this.countActiveProducts = 0;
- for (var i = 0; i < this.products.length; i++) {
- if (this.products[i].productDistribution && this.products[i].productDistribution[0].active == 1) {
- this.countActiveProducts++;
- }
- }
- },
- initPointsSale: function (idActivePointSale) {
- this.countActivePointsSale = 0;
- this.setIdActivePointSale(0);
- for (var i = 0; i < this.pointsSale.length; i++) {
- if (this.pointsSale[i].pointSaleDistribution[0].delivery == 1) {
- this.countActivePointsSale++;
- this.setIdActivePointSale(this.pointsSale[i].id);
- }
- }
- if (this.countActivePointsSale > 1) {
- this.setIdActivePointSale(0);
- }
- if (idActivePointSale) {
- this.setIdActivePointSale(idActivePointSale);
- }
-
- this.countOrdersByPointSale = [];
- for (var i = 0; i < this.pointsSale.length; i++) {
- this.countOrdersByPointSale[this.pointsSale[i].id] = 0;
- }
- for (var i = 0; i < this.orders.length; i++) {
- this.countOrdersByPointSale[this.orders[i].id_point_sale]++;
- }
- },
- dayClicked: function (day) {
- this.date = day.date;
- this.dateFormat = ('0' + this.date.getDate()).slice(-2) + '/'
- + ('0' + (this.date.getMonth() + 1)).slice(-2) + '/'
- + this.date.getFullYear();
- this.init();
- },
- productQuantityMaxChange: function (event) {
- var quantityMax = event.currentTarget.value;
- axios.get("ajax-process-product-quantity-max", {
- params: {
- idDistribution: this.distribution.id,
- idProduct: event.currentTarget.getAttribute('data-id-product'),
- quantityMax: (!quantityMax || quantityMax.length === 0) ? -1 : quantityMax
- }
- })
- .then(function (response) {
-
- });
- },
- productActiveClick: function (event) {
- var idProduct = event.currentTarget.getAttribute('data-id-product');
- var activeProduct = event.currentTarget.getAttribute('data-active-product');
- axios.get("ajax-process-active-product", {
- params: {
- idDistribution: this.distribution.id,
- idProduct: idProduct,
- active: activeProduct
- }
- })
- .then(function (response) {
-
- });
-
- for (i = 0; i < this.products.length; i++) {
- if (this.products[i].productDistribution && this.products[i].id == idProduct) {
- this.products[i].productDistribution[0].active = activeProduct;
- }
- }
- this.initCountActiveProducts();
- },
- isOneProductMaximumQuantityExceeded: function () {
- for (var i = 0; i < this.products.length; i++) {
- if (this.isProductMaximumQuantityExceeded(this.products[i])) {
- return true;
- }
- }
-
- return false;
- },
- isProductMaximumQuantityExceeded: function (product) {
- return
- this.getProductDistribution(product)
- && this.getProductDistribution(product).quantity_max
- && product.quantity_ordered
- && product.quantity_ordered > this.getProductDistribution(product).quantity_max;
- },
- pointSaleActiveClick: function (event) {
- var idPointSale = event.currentTarget.getAttribute('data-id-point-sale');
- var deliveryPointSale = event.currentTarget.getAttribute('data-delivery-point-sale');
- axios.get("ajax-process-active-point-sale", {
- params: {
- idDistribution: this.distribution.id,
- idPointSale: idPointSale,
- delivery: deliveryPointSale
- }
- })
- .then(function (response) {
-
- });
-
- for (i = 0; i < this.pointsSale.length; i++) {
- if (this.pointsSale[i].id == idPointSale) {
- this.pointsSale[i].pointSaleDistribution[0].delivery = deliveryPointSale;
- }
- }
-
- this.initPointsSale();
- },
- activeDistribution: function (event) {
- var app = this;
- var active = parseInt(event.currentTarget.getAttribute('data-active'));
-
- axios.get("ajax-process-active-distribution", {
- params: {
- idDistribution: this.distribution.id,
- active: active
- }
- })
- .then(function (response) {
- app.init();
- app.alertsActiveDistribution(active, 'Distribution');
- });
- },
- activeWeekDistribution: function (event) {
- var app = this;
- var active = parseInt(event.currentTarget.getAttribute('data-active'));
-
- axios.get("ajax-process-active-week-distribution", {
- params: {
- date: this.date.getFullYear() + '-'
- + ('0' + (this.date.getMonth() + 1)).slice(-2) + '-'
- + ('0' + this.date.getDate()).slice(-2),
- active: active
- }
- })
- .then(function (response) {
- app.init();
- app.alertsActiveDistribution(active, 'Semaine de distribution');
- });
- },
- alertsActiveDistribution: function (active, label) {
- if (!active) {
- appAlerts.alert(
- 'success',
- label + ' désactivée.',
- );
- appAlerts.alert(
- 'info',
- 'Pensez à bien rembourser les clients qui auraient passé commande en utilisant leur cagnotte.',
- 6000
- );
- } else {
- appAlerts.alert(
- 'success',
- label + ' activée.',
- );
- }
- },
- pointSaleClick: function (event) {
- this.setIdActivePointSale(event.currentTarget.getAttribute('data-id-point-sale'));
- },
- setIdActivePointSale: function (id) {
- this.idActivePointSale = id;
-
- for (key in this.pointsSale) {
- if (this.pointsSale[key].id == id) {
- this.pointSaleActive = this.pointsSale[key];
- }
- }
-
- if (!id) {
- this.orderCreate.id_point_sale = this.idDefaultPointSale;
- } else {
- this.orderCreate.id_point_sale = id;
- }
- setTimeout("opendistrib_popover(); opendistrib_dropdown_tooltip();", 500);
- },
- orderCreatedUpdated: function () {
- this.showModalFormOrderCreate = false;
- this.showModalFormOrderUpdate = false;
- this.init(this.idActivePointSale);
- },
- deleteOrderClick: function (event) {
- var app = this;
- var idOrder = event.currentTarget.getAttribute('data-id-order');
- axios.get(UrlManager.getBaseUrlAbsolute() + "order/ajax-delete", {
- params: {
- idOrder: idOrder
- }
- })
- .then(function (response) {
- app.init(app.idActivePointSale);
- });
- },
- updateOrderFromUrl: function () {
- this.initModalFormOrder();
- this.updateProductOrders(false);
- },
- updateOrderClick: function (event) {
- var idOrder = event.currentTarget.getAttribute('data-id-order');
- this.idOrderUpdate = idOrder;
- this.showModalFormOrderUpdate = true;
- this.initModalFormOrder();
- this.updateProductOrders(false);
- },
- openModalFormOrderCreate: function () {
- this.showModalFormOrderCreate = true;
- this.initModalFormOrder();
- this.updateProductOrders(false);
- },
- initModalFormOrder: function () {
- var app = this;
- setTimeout(function () {
- $('.modal-body').css('height', $(window).height());
- $('.modal-body').css('maxHeight', 'unset');
-
- $('.select2-order-form').select2({
- width: 'resolve'
- });
- $('.select2-order-form').on('select2:select', function (e) {
- var idUser = e.params.data.id;
- if (app.showModalFormOrderCreate) {
- Vue.set(app.orderCreate, 'id_user', idUser);
- }
- if (app.showModalFormOrderUpdate) {
- Vue.set(app.ordersUpdate[app.getOrderUpdateKey()], 'id_user', idUser);
- }
- });
-
- // Passage à la ligne du dessous quand on appuie sur "Entrée"
- $('.modal-form-order .input-quantity').keypress(function (e) {
- if (e.which == 13) {
- $(this).parent().parent().parent()
- .next().find('.input-quantity').focus();
- }
- });
- }, 500);
- },
- getOrderUpdateKey: function () {
- if (app.showModalFormOrderUpdate && app.idOrderUpdate) {
- for (keyOrderUpdate in app.ordersUpdate) {
- if (app.ordersUpdate[keyOrderUpdate].id == app.idOrderUpdate) {
- return keyOrderUpdate;
- }
- }
- }
- },
- orderPaymentModalClick: function (event) {
- var idOrder = event.currentTarget.getAttribute('data-id-order');
- this.idOrderPayment = idOrder;
- this.showModalPayment = true;
- },
- getTypePayment: function (order) {
- if (order.amount_paid < order.amount) {
- return 'payment';
- } else {
- return 'refund';
- }
- },
- isTypePayment: function (order) {
- return this.getTypePayment(order) == 'payment';
- },
- getLabelPaymentRefund: function (order, labelPay, labelRefund, labelMeanPayment) {
- var str = '';
-
- if (this.isTypePayment(order)) {
- str += labelPay;
- } else {
- str += labelRefund;
- }
-
- str += ' ' + labelMeanPayment;
-
- return str;
- },
- orderPaymentClick: function (event) {
- var app = this;
- var idOrder = event.currentTarget.getAttribute('data-id-order');
- if (!idOrder) {
- idOrder = this.idOrderPayment;
- }
- axios.get(UrlManager.getBaseUrlAbsolute() + "order/ajax-payment", {
- params: {
- idOrder: idOrder,
- type: event.currentTarget.getAttribute('data-type'),
- meanPayment: event.currentTarget.getAttribute('data-mean-payment')
- //amount: event.currentTarget.getAttribute('data-amount')
- }
- })
- .then(function (response) {
- app.init(app.idActivePointSale);
- });
- },
- orderViewClick: function (event) {
- var currentIdOrderView = event.currentTarget.getAttribute('data-id-order');
- if (this.idOrderView == currentIdOrderView) {
- this.showViewProduct = !this.showViewProduct;
- } else {
- this.showViewProduct = true;
- this.idOrderView = currentIdOrderView;
- }
-
- },
- closeModalProducts: function () {
- this.showModalProducts = false;
- this.init(this.idActivePointSale);
- },
- closeModalOrderForm: function (create) {
- if (create) {
- this.showModalFormOrderCreate = false
- } else {
- this.showModalFormOrderUpdate = false
- }
-
- this.init(this.idActivePointSale);
- },
- cloneOrder: function (order) {
- var clone = Object.assign({}, order);
-
- clone.productOrder = {};
- for (var key in order.productOrder) {
- clone.productOrder[key] = order.productOrder[key];
- }
-
- return clone;
- },
- addSubscriptions: function () {
- var app = this;
- axios.get(UrlManager.getBaseUrlAbsolute() + "distribution/ajax-process-add-subscriptions", {
- params: {
- date: this.getDate()
- }
- })
- .then(function (response) {
- app.init(app.idActivePointSale);
- appAlerts.alertResponse(response);
- });
- },
- synchroTiller: function () {
- var app = this;
- this.showLoading = true;
- axios.get(UrlManager.getBaseUrlAbsolute() + "distribution/ajax-process-synchro-tiller", {
- params: {
- date: this.getDate()
- }
- })
- .then(function (response) {
- app.init(app.idActivePointSale);
- });
- },
- forceSynchronizeOrderTiller: function(idOrder) {
- var app = this;
- this.showLoading = true;
- axios.get(UrlManager.getBaseUrlAbsolute() + "distribution/ajax-force-synchronize-order-tiller", {
- params: {
- idOrder: idOrder
- }
- })
- .then(function (response) {
- app.init(app.idActivePointSale);
- });
- },
- authTiller: function () {
-
- },
- totalActivePointSale: function () {
- var total = 0;
- for (var i = 0; i < this.orders.length; i++) {
- if (this.orders[i].id_point_sale == this.idActivePointSale) {
- total += parseFloat(this.orders[i].amount);
- }
- }
- return total.toFixed(2);
- },
- weightActivePointSale: function () {
- var weight = 0;
- for (var i = 0; i < this.orders.length; i++) {
- if (this.orders[i].id_point_sale == this.idActivePointSale) {
- weight += parseFloat(this.orders[i].weight);
- }
- }
- return weight.toFixed(2);
- },
- changeSynchroTiller: function (event) {
- var app = this;
- var idOrder = event.currentTarget.getAttribute('data-id-order');
- axios.get(UrlManager.getBaseUrlAbsolute() + "order/ajax-change-synchro-tiller", {
- params: {
- idOrder: idOrder,
- boolSynchroTiller: event.currentTarget.checked ? 1 : 0
- }
- })
- .then(function (response) {
- app.init();
- });
- },
- selectAllOrdersEvent: function (event) {
- var bool = event.currentTarget.checked;
- this.selectAllOrders(bool);
- },
- selectAllOrders: function (bool) {
- for (var key in this.orders) {
- if (this.orders[key].id_point_sale == this.idActivePointSale) {
- this.orders[key].selected = bool;
- }
- }
- },
- oneOrderSelected: function () {
- for (var key in this.orders) {
- if (this.orders[key].selected == true) {
- return true;
- }
- }
- return false;
- },
- generateDeliveryNotePointSale: function () {
- if (!this.oneOrderSelected()) {
- this.selectAllOrders(true);
- }
-
- if (this.oneOrderSelected()) {
- var app = this;
- var idOrders = {};
- for (var key in this.orders) {
- if (this.orders[key].selected == true) {
- idOrders[key] = this.orders[key].id;
- }
- }
- axios.get(UrlManager.getBaseUrlAbsolute() + "distribution/ajax-generate-delivery-note-point-sale", {
- params: {
- idOrders: JSON.stringify(idOrders)
- }
- })
- .then(function (response) {
- appAlerts.alertResponse(response);
- app.init(app.idActivePointSale);
- });
- } else {
- appAlerts.alert('danger', 'Veuillez sélectionner au moins une commande pour générer un bon de livraison');
- }
- },
- generateDeliveryNoteEachUser: function () {
- if (!this.oneOrderSelected()) {
- this.selectAllOrders(true);
- }
- if (this.oneOrderSelected()) {
- var app = this;
- var idOrders = {};
- for (var key in this.orders) {
- if (this.orders[key].selected == true) {
- idOrders[key] = this.orders[key].id;
- }
- }
- axios.get(UrlManager.getBaseUrlAbsolute() + "distribution/ajax-generate-delivery-note-each-user", {
- params: {
- idOrders: JSON.stringify(idOrders)
- }
- })
- .then(function (response) {
- appAlerts.alertResponse(response);
- app.init(app.idActivePointSale);
- });
- } else {
- appAlerts.alert('danger', 'Veuillez sélectionner au moins une commande pour générer un bon de livraison');
- }
- },
- validateDeliveryNotes: function () {
- if (!this.oneOrderSelected()) {
- this.selectAllOrders(true);
- }
- if (this.oneOrderSelected()) {
- var app = this;
- var idOrders = {};
- for (var key in this.orders) {
- if (this.orders[key].selected == true) {
- idOrders[key] = this.orders[key].id;
- }
- }
- axios.get(UrlManager.getBaseUrlAbsolute() + "distribution/ajax-validate-delivery-notes", {
- params: {
- idOrders: JSON.stringify(idOrders)
- }
- })
- .then(function (response) {
- appAlerts.alertResponse(response);
- app.init(app.idActivePointSale);
- });
- } else {
- appAlerts.alert('danger', 'Veuillez sélectionner au moins une commande pour valider un bon de livraison');
- }
- },
- generateDeliveryNote: function (event) {
- var app = this;
- var idOrder = event.currentTarget.getAttribute('data-id-order');
- axios.get(UrlManager.getBaseUrlAbsolute() + "distribution/ajax-generate-delivery-note", {
- params: {
- idOrder: idOrder
- }
- })
- .then(function (response) {
- appAlerts.alertResponse(response);
- app.init(app.idActivePointSale);
- });
- },
- sendDeliveryNote: function (event) {
- var app = this;
- var idOrder = event.currentTarget.getAttribute('data-id-order');
- axios.get(UrlManager.getBaseUrlAbsolute() + "distribution/ajax-send-delivery-note", {
- params: {
- idOrder: idOrder
- }
- })
- .then(function (response) {
- appAlerts.alertResponse(response);
- app.init(app.idActivePointSale);
- });
- },
- deliveryNoteExist: function (idPointSale) {
- return typeof this.deliveryNotes[this.idActivePointSale] != 'undefined';
- },
- payOrders: function () {
- var app = this;
- axios.get(UrlManager.getBaseUrlAbsolute() + "cron/pay-orders", {
- params: {
- date: app.getDate()
- }
- })
- .then(function (response) {
- appAlerts.alertResponse(response);
- app.init(app.idActivePointSale);
- });
- },
- getProductOrderArrayRequest: function (order) {
- var productOrderArrayRequest = {};
- for (var key in order.productOrder) {
- productOrderArrayRequest[key] = {
- quantity: order.productOrder[key].quantity,
- unit: order.productOrder[key].unit,
- price: order.productOrder[key].price,
- invoice_price: order.productOrder[key].invoice_price
- };
- }
- return JSON.stringify(productOrderArrayRequest);
- },
- updateProductOrders: function (updatePricesOnUpdateOrder) {
- var app = this;
- app.loadingUpdateProductOrder = true;
- var order = null;
-
- if (app.showModalFormOrderCreate) {
- order = app.orderCreate;
- }
-
- if (app.showModalFormOrderUpdate && app.idOrderUpdate) {
- for (keyOrderUpdate in app.ordersUpdate) {
- if (app.ordersUpdate[keyOrderUpdate].id == app.idOrderUpdate) {
- order = app.ordersUpdate[keyOrderUpdate];
- }
- }
- }
-
- if (order) {
- axios.get(UrlManager.getBaseUrlAbsolute() + "distribution/ajax-update-product-order", {
- params: {
- idDistribution: app.distribution.id,
- idUser: order.id_user,
- idPointSale: order.id_point_sale,
- idOrder: order.id,
- productOrderFormArray: app.getProductOrderArrayRequest(order)
- }
- })
- .then(function (response) {
- if (response.data) {
- for (idProduct in response.data) {
-
- if (app.showModalFormOrderCreate) {
- Vue.set(app.orderCreate.productOrder[idProduct], 'quantity_remaining', response.data[idProduct].quantity_remaining);
- Vue.set(app.orderCreate.productOrder[idProduct], 'prices', response.data[idProduct].prices);
- Vue.set(app.orderCreate.productOrder[idProduct], 'active', response.data[idProduct].active);
- Vue.set(app.orderCreate.productOrder[idProduct], 'price', app.getBestProductPrice(app.orderCreate, idProduct, app.orderCreate.productOrder[idProduct].quantity, false));
- Vue.set(app.orderCreate.productOrder[idProduct], 'price_with_tax', app.getBestProductPrice(app.orderCreate, idProduct, app.orderCreate.productOrder[idProduct].quantity, true));
- }
-
- if (app.showModalFormOrderUpdate && app.idOrderUpdate) {
- for (keyOrderUpdate in app.ordersUpdate) {
- if (order.id == app.idOrderUpdate) {
- Vue.set(app.ordersUpdate[keyOrderUpdate].productOrder[idProduct], 'quantity_remaining', response.data[idProduct].quantity_remaining);
- Vue.set(app.ordersUpdate[keyOrderUpdate].productOrder[idProduct], 'prices', response.data[idProduct].prices);
- Vue.set(app.ordersUpdate[keyOrderUpdate].productOrder[idProduct], 'active', response.data[idProduct].active);
- Vue.set(app.ordersUpdate[keyOrderUpdate].productOrder[idProduct], 'invoice_price', response.data[idProduct].invoice_price);
-
- if (updatePricesOnUpdateOrder) {
- Vue.set(
- app.ordersUpdate[keyOrderUpdate].productOrder[idProduct],
- 'price',
- app.getBestProductPrice(app.ordersUpdate[keyOrderUpdate], idProduct, app.ordersUpdate[keyOrderUpdate].productOrder[idProduct].quantity, false));
- Vue.set(
- app.ordersUpdate[keyOrderUpdate].productOrder[idProduct],
- 'price_with_tax',
- app.getBestProductPrice(app.ordersUpdate[keyOrderUpdate], idProduct, app.ordersUpdate[keyOrderUpdate].productOrder[idProduct].quantity, true));
- }
- }
- }
- }
- }
-
- if (updatePricesOnUpdateOrder) {
- appAlerts.alert('info', 'Prix rechargés');
- }
- }
-
- app.loadingUpdateProductOrder = false;
- });
- }
- },
- updateInvoicePrices: function () {
- var order = null;
-
- if (app.showModalFormOrderUpdate && app.idOrderUpdate) {
- for (keyOrderUpdate in app.ordersUpdate) {
- if (app.ordersUpdate[keyOrderUpdate].id == app.idOrderUpdate) {
- order = app.ordersUpdate[keyOrderUpdate];
- }
- }
- }
-
- if (order) {
- axios.get(UrlManager.getBaseUrlAbsolute() + "distribution/ajax-update-invoice-prices", {
- params: {
- idOrder: order.id
- }
- })
- .then(function (response) {
- app.updateProductOrders(false);
- appAlerts.alert('info', 'Prix facturés réinitialisés.');
- });
- }
- },
- getBestProductPrice: function (order, idProduct, theQuantity, withTax) {
- var product = this.getProduct(idProduct);
- var thePrice = 9999;
- var pricesArray = order.productOrder[idProduct].prices;
-
- var unitCoefficient = this.getUnitCoefficient(product.unit);
- if (theQuantity) {
- theQuantity = theQuantity / unitCoefficient;
- }
-
- if (pricesArray) {
- for (var i = 0; i < pricesArray.length; i++) {
- var price = pricesArray[i].price;
- if (withTax) {
- price = pricesArray[i].price_with_tax
- }
-
- var fromQuantity = pricesArray[i].from_quantity;
-
- if (price < thePrice && fromQuantity <= theQuantity) {
- thePrice = price;
- }
- }
- } else {
- if (withTax) {
- thePrice = getPriceWithTax(product.price, product.taxRate.value);
- } else {
- thePrice = product.price;
- }
- }
-
- if (thePrice == 9999) {
- return 0;
- } else {
- return thePrice;
- }
- },
- getProduct: function (idProduct) {
- for (var i = 0; i < this.products.length; i++) {
- if (this.products[i].id == idProduct) {
- return this.products[i];
- }
- }
-
- return false;
- },
- countDocuments: function (order) {
- var count = 0;
-
- if (order.id_delivery_note) {
- count++;
- }
-
- if (order.id_quotation) {
- count++;
- }
-
- if (order.id_invoice) {
- count++;
- }
-
- return count;
- },
- getUnitCoefficient: function (unit) {
- return this.units[unit].coefficient;
- },
- getProductDistribution: function (product) {
- if (typeof product.productDistribution !== 'undefined' && product.productDistribution[0]) {
- return product.productDistribution[0];
- }
-
- return null;
- },
- copyLinkOrder: function ($event, urlOrder) {
- $event.preventDefault();
- navigator.clipboard.writeText(urlOrder);
- appAlerts.alert('success', 'Lien vers la page de commande de la distribution copié.');
- return false;
- }
- },
- });
-
- Vue.component('order-state-payment', {
- props: ['order', 'producer'],
- template: '#order-state-payment'
- });
-
- Vue.component('modal', {
- template: '#modal-template'
- });
-
- Vue.component('order-form', {
- props: ['distribution', 'date', 'dateFormat', 'pointsSale', 'idActivePointSale', 'meansPayment', 'users', 'products', 'order', 'orders', 'producer', 'loadingUpdateProductOrder', 'create', 'units'],
- emits: ['updateProductPrice', 'updateInvoicePrices'],
- data: function () {
- return {
- errors: [],
- idPointSale: 0,
- idUser: 0,
- username: '',
- comment: '',
- baseUrl: $('meta[name=baseurl]').attr('content'),
- vatMode: 'all' // 'with_tax'
- };
- },
- template: '#order-form-template',
- watch: {
- 'order.id_user': function () {
- this.userChange();
- }
- },
- methods: {
- checkForm: function () {
- this.errors = [];
-
- var countProducts = 0;
- for (var key in this.order.productOrder) {
- if (this.order.productOrder[key].quantity > 0) {
- countProducts++;
- }
- }
-
- if (this.order.id_point_sale
- && (this.order.id_user > 0 || (this.order.username && this.order.username.length))
- && countProducts > 0) {
- return true;
- }
-
- if (!this.order.id_point_sale) {
- this.errors.push('Veuillez sélectionner un point de vente');
- }
-
- if ((!this.order.id_user || this.order.id_user == 0) && !this.order.username.length) {
- this.errors.push('Veuillez sélectionner ou saisir un utilisateur');
- }
-
- if (!countProducts) {
- this.errors.push('Veuillez sélectionner au moins un produit');
- }
- },
- getProductOrderArrayRequest: function () {
- var productOrderArrayRequest = {};
- for (var key in this.order.productOrder) {
- productOrderArrayRequest[key] = {
- quantity: this.order.productOrder[key].quantity,
- unit: this.order.productOrder[key].unit,
- price: this.order.productOrder[key].price,
- invoice_price: this.order.productOrder[key].invoice_price
- };
- }
- return JSON.stringify(productOrderArrayRequest);
- },
- getPointSale: function (idPointSale) {
- for (key in this.pointsSale) {
- if (this.pointsSale[key].id == idPointSale) {
- return this.pointsSale[key];
- }
- }
-
- return false;
- },
- isPointSaleCreditFunctioningOptional: function (idPointSale) {
- var pointSale = this.getPointSale(idPointSale);
- if (pointSale && pointSale.credit_functioning == 'optional') {
- return true;
- }
-
- return false;
- },
- submitFormCreate: function (event) {
- var app = this;
- if (this.checkForm()) {
- axios.get(UrlManager.getBaseUrlAbsolute() + "order/ajax-create", {
- params: {
- date: this.date.getFullYear() + '-'
- + ('0' + (this.date.getMonth() + 1)).slice(-2) + '-'
- + ('0' + this.date.getDate()).slice(-2),
- idPointSale: this.order.id_point_sale,
- idUser: this.order.id_user,
- username: this.order.username,
- meanPayment: this.order.mean_payment,
- products: app.getProductOrderArrayRequest(),
- comment: this.order.comment,
- debitCredit: this.order.debitCredit
- }
- })
- .then(function (response) {
- app.order.id_point_sale = 0;
- app.order.id_user = 0;
- app.order.username = '';
- app.order.comment = '';
- for (i = 0; i < app.order.productOrder.length; i++) {
- app.order.productOrder[i] = 0;
- }
-
- app.$emit('ordercreatedupdated');
- });
- }
- },
- submitFormUpdate: function (event) {
- var app = this;
- if (this.checkForm()) {
- var data = new FormData();
- data.append('date', this.date.getFullYear() + '-'
- + ('0' + (this.date.getMonth() + 1)).slice(-2) + '-'
- + ('0' + this.date.getDate()).slice(-2));
- data.append('idOrder', this.order.id);
- data.append('idPointSale', this.order.id_point_sale);
- data.append('meanPayment', this.order.mean_payment);
- data.append('idUser', this.order.id_user);
- data.append('username', '' + this.order.username);
- data.append('products', app.getProductOrderArrayRequest());
- data.append('comment', this.order.comment && this.order.comment.length ? this.order.comment : '');
- data.append('debitCredit', this.order.debitCredit ? this.order.debitCredit : 0);
-
- axios.post(UrlManager.getBaseUrlAbsolute() + "order/ajax-update", data)
- .then(function (response) {
- app.$emit('ordercreatedupdated');
- });
- }
- },
- productQuantityClick: function (id_product, quantity) {
- if (!this.order.productOrder[id_product].quantity) {
- this.order.productOrder[id_product].quantity = 0;
- }
- if (parseFloat(this.order.productOrder[id_product].quantity) + quantity >= 0) {
- var theQuantity = (parseFloat(this.order.productOrder[id_product].quantity) + parseFloat(quantity)).toFixed(2);
- var theQuantityDecimal = theQuantity % 1;
- if (theQuantityDecimal == 0) {
- theQuantity = parseInt(theQuantity);
- }
-
- Vue.set(this.order.productOrder[id_product], 'quantity', theQuantity);
- Vue.set(this.order.productOrder[id_product], 'price', app.getBestProductPrice(this.order, id_product, theQuantity, false));
- Vue.set(this.order.productOrder[id_product], 'price_with_tax', app.getBestProductPrice(this.order, id_product, theQuantity, true));
- }
-
- this.updateProductOrders(false);
- },
- getProduct: function (idProduct) {
- for (var i = 0; i < this.products.length; i++) {
- if (this.products[i].id == idProduct) {
- return this.products[i];
- }
- }
-
- return false;
- },
- productPriceChange: function (event) {
- var idProduct = event.currentTarget.getAttribute('data-id-product');
- var product = this.getProduct(idProduct);
-
- if (product) {
- var taxRateValue = parseFloat(product.taxRate.value);
- var withTax = event.currentTarget.getAttribute('data-with-tax');
- var price = 0;
- var priceWithTax = 0;
- var priceValue = parseFloat(event.currentTarget.value.replace(',', '.'));
-
- if (withTax) {
- priceWithTax = priceValue.toFixed(2);
- price = getPrice(priceWithTax, taxRateValue);
- } else {
- price = priceValue.toFixed(5);
- priceWithTax = getPriceWithTax(price, taxRateValue);
- }
-
- if (isNaN(price)) {
- price = 0;
- }
-
- if (isNaN(priceWithTax)) {
- priceWithTax = 0;
- }
-
- Vue.set(this.order.productOrder, idProduct, {
- active: this.order.productOrder[idProduct].active,
- quantity: this.order.productOrder[idProduct].quantity,
- unit: this.order.productOrder[idProduct].unit,
- price: price,
- price_with_tax: priceWithTax
- });
- }
- },
- userChange: function (event) {
- var app = this;
- axios.get(UrlManager.getBaseUrlAbsolute() + "distribution/ajax-point-sale-favorite", {
- params: {
- idUser: app.order.id_user,
- idDistribution: app.distribution.id
- }
- })
- .then(function (response) {
- if (app.idActivePointSale == 0) {
- app.order.id_point_sale = response.data.id_favorite_point_sale;
- }
- app.updateProductOrders(true);
- });
- },
- pointSaleChange: function (event) {
- this.updateProductOrders(true);
- },
- updateProductOrders: function (updateProductOrders) {
- this.$emit('updateproductorders', updateProductOrders);
- },
- updateInvoicePrices: function () {
- this.$emit('updateinvoiceprices');
- },
- toggleVatMode: function () {
- if (this.vatMode == 'all') {
- this.vatMode = 'with_tax';
- } else {
- this.vatMode = 'all';
- }
- },
- getProductQuantityMax: function(product) {
- return product.quantity_max;
- },
- getProductQuantityOrder: function(product) {
- var productQuantityOrder = 0;
- for (key in this.orders) {
- productQuantityOrder += this.getProductQuantityProductOrder(this.orders[key], product);
- }
- if (this.create == 1) {
- productQuantityOrder += this.getProductQuantityProductOrder(this.order, product);
- }
- return productQuantityOrder;
- },
- getProductQuantityRemaining: function (order, product) {
- var productQuantityRemaining = order.productOrder[product.id].quantity_remaining;
-
- // format
- if (productQuantityRemaining && product.unit != 'piece') {
- productQuantityRemaining = productQuantityRemaining.toFixed(2);
- }
-
- return productQuantityRemaining;
- },
- getProductQuantityProductOrder: function (order, product) {
- var productOrder = order.productOrder[product.id];
- var unit = productOrder.unit;
- var unitCoefficient = this.getUnitCoefficient(unit);
- return parseFloat(productOrder.quantity / unitCoefficient);
- },
- getUnitCoefficient: function (unit) {
- return this.units[unit].coefficient;
- },
- getProductDistribution: function (product) {
- if (typeof product.productDistribution !== 'undefined' && product.productDistribution[0]) {
- return product.productDistribution[0];
- }
- }
- }
- });
- }
|