|
@@ -0,0 +1,310 @@
|
|
|
+(function() {
|
|
|
+ openerp.widgetInstanceProductPricelistUtility = null;
|
|
|
+ openerp.parentInstanceProductPricelistUtility = null;
|
|
|
+ var Qweb = openerp.web.qweb;
|
|
|
+ var instance = openerp;
|
|
|
+ var instanceWeb = openerp.web;
|
|
|
+
|
|
|
+ openerp.ProductPricelistUtility = instance.Widget.extend({
|
|
|
+ template: 'productPricelistUtility.Percent',
|
|
|
+ id: undefined,
|
|
|
+
|
|
|
+ priceList: [],
|
|
|
+ percentFormat: {
|
|
|
+ 'decimalSeparator': ',',
|
|
|
+ 'decimalPlaces': 2,
|
|
|
+ 'thousandsSeparator': '.'
|
|
|
+ },
|
|
|
+
|
|
|
+ init: function(parent) {
|
|
|
+ this._super(parent);
|
|
|
+ this.buttons = parent.$buttons;
|
|
|
+ },
|
|
|
+ start: function() {
|
|
|
+ var self = this;
|
|
|
+
|
|
|
+ this.$el.click(function(){
|
|
|
+ self.fectchInitial();
|
|
|
+ });
|
|
|
+ self.buttons.click(function(e) {
|
|
|
+ /* E (Editar) */
|
|
|
+ if (e.target.accessKey === 'E')
|
|
|
+ self.$el.css('display','none');
|
|
|
+ /* S (Guarrdar) */
|
|
|
+ if (e.target.accessKey === 'S')
|
|
|
+ self.$el.css('display','flex');
|
|
|
+ /* D (Cancelar) */
|
|
|
+ if (e.target.accessKey === 'D')
|
|
|
+ self.$el.css('display','flex');
|
|
|
+ /* CREAR */
|
|
|
+ if (e.target.accessKey === 'C')
|
|
|
+ self.$el.css('display','none');
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ updateId: function(id) {
|
|
|
+ var self = this;
|
|
|
+ self.id = id;
|
|
|
+
|
|
|
+ self.$el.css('display','flex');
|
|
|
+ if (!id)
|
|
|
+ self.$el.css('display','none');
|
|
|
+ },
|
|
|
+ /* Remover */
|
|
|
+ removeModal: function() {
|
|
|
+ $('.expired-account-modal').remove();
|
|
|
+ $('.modal-backdrop').remove();
|
|
|
+ },
|
|
|
+ /* Reloada */
|
|
|
+ reloadPage: function() {
|
|
|
+ openerp.parentInstanceProductPricelistUtility.reload();
|
|
|
+ },
|
|
|
+ /* Metodo Inicial */
|
|
|
+ fectchInitial: function() {
|
|
|
+ var self = this;
|
|
|
+
|
|
|
+ self.fetchPriceList(self.id).then(function(priceList) {
|
|
|
+ return priceList;
|
|
|
+ }).then(function(priceList) {
|
|
|
+ self.priceList = _.sortBy(priceList, 'name');
|
|
|
+
|
|
|
+ return self.showModal();
|
|
|
+
|
|
|
+ });
|
|
|
+ },
|
|
|
+ /* Get SALE ORDER */
|
|
|
+ fetchPriceList: function(productID) {
|
|
|
+ var list = new openerp.web.Model('product.pricelist');
|
|
|
+ return list.call('get_pricelist_sale',[productID],{
|
|
|
+ context: new openerp.web.CompoundContext()
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ fornatPriceList: function(){
|
|
|
+ var self = this;
|
|
|
+ _.each(self.priceList, function(list){
|
|
|
+ list.standardPriceFormat = instanceWeb.formatCurrency(list.standardPrice, list.currency);
|
|
|
+ list.priceMinimumFormat = instanceWeb.formatCurrency(list.priceMinimum, list.currency);
|
|
|
+ list.priceMaximumFormat = instanceWeb.formatCurrency(list.priceMaximum, list.currency);
|
|
|
+ list.minimumPercentFormat = instanceWeb.formatCurrency(list.minimumPercent, self.percentFormat);
|
|
|
+ list.maximumPercentFormat = instanceWeb.formatCurrency(list.maximumPercent, self.percentFormat);
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ /* Modal */
|
|
|
+ showModal: function() {
|
|
|
+ var self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var state = true;
|
|
|
+ self.fornatPriceList();
|
|
|
+ var lists = self.priceList
|
|
|
+ var currency = lists[0].currency;
|
|
|
+ /* Modal */
|
|
|
+ var modal = Qweb.render('productPricelistUtility.ModalPercent', {'lists':lists});
|
|
|
+ $('.openerp_webclient_container').after(modal);
|
|
|
+ $('.expired-account-modal').modal();
|
|
|
+
|
|
|
+ // /* table */
|
|
|
+ var tableRow = $('.expired-account-modal').find('.table-tbody').find('tr');
|
|
|
+ var buttonAccept = $('.expired-account-modal').find('.priceList-utility-button');
|
|
|
+
|
|
|
+ /*##################################################################
|
|
|
+ ####### PERCENT MINIMUM ################################
|
|
|
+ ##################################################################*/
|
|
|
+ tableRow.focusin(function(e) {
|
|
|
+ if (($(e.target).index() !== 0) && ($(e.target)[0].className !== 'pricelist-utility-input-percent'))
|
|
|
+ return false;
|
|
|
+
|
|
|
+ var itemClass = ($(e.target)[0].className).split(" ")
|
|
|
+ /* MINIMO */
|
|
|
+ if (!!_.contains(itemClass,'minimum-percent')) {
|
|
|
+ var percent = $($(e.target).closest('tr').children()[2]).find('.minimum-percent');
|
|
|
+ var values = instanceWeb.unFormatCurrency(percent.val());
|
|
|
+ if (values === 0)
|
|
|
+ percent.val('');
|
|
|
+ }
|
|
|
+ /* MAXIMO */
|
|
|
+ if (!!_.contains(itemClass,'maximum-percent')) {
|
|
|
+ var percent = $($(e.target).closest('tr').children()[4]).find('.maximum-percent');
|
|
|
+ var values = instanceWeb.unFormatCurrency(percent.val());
|
|
|
+ if (values === 0)
|
|
|
+ percent.val('');
|
|
|
+ }
|
|
|
+ });
|
|
|
+ tableRow.keyup(function(e) {
|
|
|
+ if (($(e.target).index() !== 0) && ($(e.target)[0].className !== 'pricelist-utility-input-percent'))
|
|
|
+ return false;
|
|
|
+
|
|
|
+ var itemClass = ($(e.target)[0].className).split(" ")
|
|
|
+ /* MINIMO */
|
|
|
+ if (!!_.contains(itemClass,'minimum-percent')) {
|
|
|
+ var percent = $($(e.target).closest('tr').children()[2]).find('.minimum-percent');
|
|
|
+ var values = instanceWeb.unFormatCurrency(percent.val());
|
|
|
+
|
|
|
+ if (e.key === self.percentFormat.decimalSeparator && self.percentFormat.decimalPlaces > 0)
|
|
|
+ return false ;
|
|
|
+
|
|
|
+ percent.val(instanceWeb.formatCurrency(values, self.percentFormat));
|
|
|
+ }
|
|
|
+ /* MAXIMO */
|
|
|
+ if (!!_.contains(itemClass,'maximum-percent')) {
|
|
|
+ var percent = $($(e.target).closest('tr').children()[4]).find('.maximum-percent');
|
|
|
+ var values = instanceWeb.unFormatCurrency(percent.val());
|
|
|
+
|
|
|
+ if (e.key === self.percentFormat.decimalSeparator && self.percentFormat.decimalPlaces > 0)
|
|
|
+ return false ;
|
|
|
+
|
|
|
+ percent.val(instanceWeb.formatCurrency(values, self.percentFormat));
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ tableRow.focusout(function(e) {
|
|
|
+ if (($(e.target).index() !== 0) && ($(e.target)[0].className !== 'pricelist-utility-input-percent'))
|
|
|
+ return false;
|
|
|
+
|
|
|
+ var itemClass = ($(e.target)[0].className).split(" ")
|
|
|
+ /* MINIMO */
|
|
|
+ if (!!_.contains(itemClass,'minimum-percent')) {
|
|
|
+ var percent = $($(e.target).closest('tr').children()[2]).find('.minimum-percent');
|
|
|
+ var values = instanceWeb.unFormatCurrency(percent.val());
|
|
|
+ var minimo = $($(e.target).closest('tr').children()[3]).find('.price-minimum');
|
|
|
+
|
|
|
+ if (values<= 0) {
|
|
|
+ minimo.val("0")
|
|
|
+ return false
|
|
|
+ }
|
|
|
+
|
|
|
+ var standardPrice = $($(e.target).closest('tr').children()[1]).find('.standard-price');
|
|
|
+ var coste = instanceWeb.unFormatCurrency(standardPrice.val());
|
|
|
+
|
|
|
+ self.calculatePriceList(coste,values).then(function(calculate){
|
|
|
+ return calculate
|
|
|
+ }).then(function(calculate){
|
|
|
+ minimo.val(instanceWeb.formatCurrency(calculate,currency))
|
|
|
+ })
|
|
|
+
|
|
|
+ }
|
|
|
+ /* MAXIMO */
|
|
|
+ if (!!_.contains(itemClass,'maximum-percent')) {
|
|
|
+ var percent = $($(e.target).closest('tr').children()[4]).find('.maximum-percent');
|
|
|
+ var values = instanceWeb.unFormatCurrency(percent.val());
|
|
|
+ var maximo = $($(e.target).closest('tr').children()[5]).find('.price-maximum');
|
|
|
+
|
|
|
+ if (values<= 0) {
|
|
|
+ maximo.val("0")
|
|
|
+ return false
|
|
|
+ }
|
|
|
+
|
|
|
+ var standardPrice = $($(e.target).closest('tr').children()[1]).find('.standard-price');
|
|
|
+ var coste = instanceWeb.unFormatCurrency(standardPrice.val());
|
|
|
+
|
|
|
+ self.calculatePriceList(coste,values).then(function(calculate){
|
|
|
+ return calculate
|
|
|
+ }).then(function(calculate){
|
|
|
+ maximo.val(instanceWeb.formatCurrency(calculate,currency))
|
|
|
+ })
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ /*#######################################################
|
|
|
+ ######## ACEPTAR / GUARDAR #####################
|
|
|
+ #######################################################*/
|
|
|
+ buttonAccept.click(function(e) {
|
|
|
+ var listPrice = [];
|
|
|
+
|
|
|
+ _.each(tableRow, function(tr) {
|
|
|
+ var idList = parseInt(($(tr).children()[6].textContent).trim());
|
|
|
+ var nameList = ($(tr).children()[0].textContent).trim();
|
|
|
+ var coste = instanceWeb.unFormatCurrency(($($(tr).children()[1]).find('.standard-price')).val());
|
|
|
+ var percentMinimum = instanceWeb.unFormatCurrency(($($(tr).children()[2]).find('.minimum-percent')).val());
|
|
|
+ var priceMinimum = instanceWeb.unFormatCurrency(($($(tr).children()[3]).find('.price-minimum')).val());
|
|
|
+ var precentMaximum = instanceWeb.unFormatCurrency(($($(tr).children()[4]).find('.maximum-percent')).val());
|
|
|
+ var priceMaximum = instanceWeb.unFormatCurrency(($($(tr).children()[5]).find('.price-maximum')).val());
|
|
|
+
|
|
|
+ listPrice.push({
|
|
|
+ 'list_id': idList,
|
|
|
+ 'name_list': nameList,
|
|
|
+ 'standard_price': coste,
|
|
|
+ 'minimum_percent': percentMinimum,
|
|
|
+ 'price_minimum': priceMinimum,
|
|
|
+ 'maximum_percent': precentMaximum,
|
|
|
+ 'price_maximum': priceMaximum,
|
|
|
+ 'product_id': self.id,
|
|
|
+ })
|
|
|
+ });
|
|
|
+
|
|
|
+ if (!listPrice.length) {
|
|
|
+ instanceWeb.notification.do_warn("Atencion", "No existe lista de precio para ser Guardada.");
|
|
|
+ return false
|
|
|
+ }
|
|
|
+
|
|
|
+ $('.expired-account-modal').find('.widget-content widget-loading-product-pricelist-utility').css('display','flex');
|
|
|
+ self.updatePricelistpercent(listPrice).then(function(list){
|
|
|
+ return list;
|
|
|
+ }).then(function(list){
|
|
|
+ $('.expired-account-modal').find('.widget-content widget-loading-product-pricelist-utility').css('display','none');
|
|
|
+ if (!list.state){
|
|
|
+ instanceWeb.notification.do_warn("Atencion", list.message);
|
|
|
+ }
|
|
|
+
|
|
|
+ self.removeModal(e);
|
|
|
+ state = list.state;
|
|
|
+ self.reloadPage();
|
|
|
+ })
|
|
|
+
|
|
|
+ defer.resolve(state);
|
|
|
+ });
|
|
|
+ /*-----------
|
|
|
+ CERRAR
|
|
|
+ -----------*/
|
|
|
+ $('.expired-account-modal').on('hidden.bs.modal', function (e) {
|
|
|
+ defer.resolve(false);
|
|
|
+ self.removeModal(e);
|
|
|
+ });
|
|
|
+
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+ calculatePriceList: function(coste, percent){
|
|
|
+ var list = new openerp.web.Model('product.pricelist');
|
|
|
+ return list.call('calculate_priceList_percent',[coste, percent],{
|
|
|
+ context: new openerp.web.CompoundContext()
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ updatePricelistpercent: function(listPrice) {
|
|
|
+ var list = new openerp.web.Model('product.pricelist');
|
|
|
+ return list.call('update_priceList_percent',[listPrice],{
|
|
|
+ context: new openerp.web.CompoundContext()
|
|
|
+ });
|
|
|
+ },
|
|
|
+ });
|
|
|
+
|
|
|
+ if (instance.web && instance.web.FormView) {
|
|
|
+ instance.web.FormView.include({
|
|
|
+ load_record: function(record) {
|
|
|
+ this._super.apply(this, arguments);
|
|
|
+
|
|
|
+ if (this.model !== 'product.product')
|
|
|
+ return;
|
|
|
+
|
|
|
+ openerp.parentInstanceProductPricelistUtility = this;
|
|
|
+
|
|
|
+ if (openerp.widgetInstanceProductPricelistUtility) {
|
|
|
+ openerp.widgetInstanceProductPricelistUtility.updateId(record.id);
|
|
|
+ if (this.$el.find('.button-config-percent').length !== 0){
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (this.$el.find('.button-config-percent').length !== 0 )
|
|
|
+ return;
|
|
|
+
|
|
|
+ openerp.widgetInstanceProductPricelistUtility = new openerp.ProductPricelistUtility(this);
|
|
|
+ var elemento = this.$el.find('.oe_form').find('.product-pricelist-utility-config');
|
|
|
+
|
|
|
+ openerp.widgetInstanceProductPricelistUtility.appendTo(elemento);
|
|
|
+ openerp.widgetInstanceProductPricelistUtility.updateId(record.id);
|
|
|
+ },
|
|
|
+
|
|
|
+ });
|
|
|
+ }
|
|
|
+})();
|