|
@@ -0,0 +1,229 @@
|
|
|
+openerp.product_product_utility = function (instance, local) {
|
|
|
+ local.widgetInstance = null;
|
|
|
+
|
|
|
+ local.ProductUtilityWidget = instance.Widget.extend({
|
|
|
+ template: 'product_product_utility.ProductProductUtility',
|
|
|
+ productUtility:[],
|
|
|
+ accountInvoice:[],
|
|
|
+ lineInvoice:[],
|
|
|
+ currencyRate:[],
|
|
|
+ resCompany:[],
|
|
|
+
|
|
|
+ init: function (parent) {
|
|
|
+ this._super(parent);
|
|
|
+ },
|
|
|
+
|
|
|
+ actualizar: function (id) {
|
|
|
+ var self = this;
|
|
|
+ var table = this.$el.find('#table');
|
|
|
+ table.bootstrapTable({data : self.productUtility});
|
|
|
+ self.fetchInitial(id);
|
|
|
+ },
|
|
|
+
|
|
|
+ // Iniciar
|
|
|
+ fetchInitial: function(id){
|
|
|
+ var self= this;
|
|
|
+ self.fetchInvoiceLine(id).then(function(lineInvoice){
|
|
|
+ return lineInvoice;
|
|
|
+ }).then(function(lineInvoice){
|
|
|
+ self.lineInvoice = lineInvoice;
|
|
|
+ return self.fetchInvoice(lineInvoice);
|
|
|
+ }).then(function(accountInvoice){
|
|
|
+ self.accountInvoice = accountInvoice;
|
|
|
+ return self.fecthCurrencyRate();
|
|
|
+ }).then(function(currencyRate){
|
|
|
+ self.currencyRate =currencyRate;
|
|
|
+ return self.fetchResCompany();
|
|
|
+ }).then(function(resCompany){
|
|
|
+ self.resCompany = resCompany;
|
|
|
+ return self.fetchFilterInvoice();
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ // Obtener Linea de la compra/ venta
|
|
|
+ fetchInvoiceLine: function(product_id){
|
|
|
+ var self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var fields=['id','invoice_id','price_unit','product_id','quantity'];
|
|
|
+ var domain=[['product_id', '=', product_id]]
|
|
|
+
|
|
|
+ var invoiceLine = new instance.web.Model('account.invoice.line');
|
|
|
+ invoiceLine.query(fields).filter(domain).all().then(function(results){
|
|
|
+ defer.resolve(results);
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+
|
|
|
+ // obtener la factura de compra / venta
|
|
|
+ fetchInvoice: function(invoice_id){
|
|
|
+ var self= this
|
|
|
+ var defer = $.Deferred();
|
|
|
+
|
|
|
+ var id =_.flatten(_.map(invoice_id,function(map){
|
|
|
+ return map.invoice_id[0];
|
|
|
+ }));
|
|
|
+
|
|
|
+ fields=['id','type','state','origen', 'date_invoice','currency_id'];
|
|
|
+ domain=[['id', '=', id],['state', '=', ['open','paid']]];
|
|
|
+ var invoice = new instance.web.Model('account.invoice');
|
|
|
+
|
|
|
+ invoice.query(fields).filter(domain).all().then(function(results){
|
|
|
+ defer.resolve(results);
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+
|
|
|
+ // Cambio de la moneda
|
|
|
+ fecthCurrencyRate:function(){
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var currency_Rate = new instance.web.Model('res.currency.rate');
|
|
|
+ var fields = ['id', 'name', 'currency_id', 'rate', 'create_date'];
|
|
|
+ var domain = [['currency_id', '=', [166 , 20, 7, 3]]];
|
|
|
+
|
|
|
+ currency_Rate.query(fields).filter(domain).all().then(function (results) {
|
|
|
+ defer.resolve(results);
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+
|
|
|
+ //Comapnia
|
|
|
+ fetchResCompany: function () {
|
|
|
+ var self = this;
|
|
|
+ var defer = $.Deferred();
|
|
|
+ var fields =['id','name', 'currency_id','logo'];
|
|
|
+ var domain =[['id', '=', 1]];
|
|
|
+ var resCompany = new instance.web.Model('res.company');
|
|
|
+
|
|
|
+ resCompany.query(fields).filter(domain).all().then(function (results) {
|
|
|
+ defer.resolve(results);
|
|
|
+ });
|
|
|
+ return defer;
|
|
|
+ },
|
|
|
+
|
|
|
+ // Separa Invoice
|
|
|
+ fetchFilterInvoice(){
|
|
|
+ var self = this;
|
|
|
+ var invoicepurchase;
|
|
|
+ var invoicesale;
|
|
|
+ var linepurchase;
|
|
|
+ var linesale;
|
|
|
+ var company;
|
|
|
+
|
|
|
+ company = _.map(self.resCompany, function(map){
|
|
|
+ return map.currency_id[1];
|
|
|
+ });
|
|
|
+
|
|
|
+ invoicesale = _.filter(self.accountInvoice, function(filter){
|
|
|
+ return filter.type==='out_invoice';
|
|
|
+ });
|
|
|
+ linesale= self.getTotInvoiceLine(invoicesale);
|
|
|
+
|
|
|
+ if(!linesale){
|
|
|
+ linesale={};
|
|
|
+ linesale.total=0;
|
|
|
+ linesale.qty =0;
|
|
|
+ }
|
|
|
+ // Facturas Compras
|
|
|
+ invoicepurchase = _.filter(self.accountInvoice, function(filter){
|
|
|
+ return filter.type==='in_invoice';
|
|
|
+ });
|
|
|
+ linepurchase = self.getTotInvoiceLine(invoicepurchase);
|
|
|
+
|
|
|
+ if(!linepurchase){
|
|
|
+ linepurchase={};
|
|
|
+ linepurchase.total=0;
|
|
|
+ linepurchase.qty =0;
|
|
|
+ }
|
|
|
+
|
|
|
+ var venta = linesale.total / linesale.qty;
|
|
|
+ var compra = linepurchase.total / linepurchase.qty;
|
|
|
+
|
|
|
+ self.$el.find('.product_utility-content.sale').find('a').text(accounting.formatNumber((venta),2, ".", ","));
|
|
|
+ self.$el.find('.product_utility-content.puchase').find('a').text(accounting.formatNumber((compra),2, ".", ","));
|
|
|
+ self.$el.find('.product_utility-content.utility').find('a').text(accounting.formatNumber((venta - compra),2, ".", ","));
|
|
|
+
|
|
|
+ self.$el.find('.product_utility-footer').find('span').text("Moneda Base "+company);
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+ //Get Invoice
|
|
|
+ getTotInvoiceLine: function(invoice){
|
|
|
+ var self = this;
|
|
|
+ var itemInvoice;
|
|
|
+ var itemRate;
|
|
|
+ var itemLine;
|
|
|
+ var qty = 0;
|
|
|
+ var total = 0;
|
|
|
+ var invoiceNew = [];
|
|
|
+
|
|
|
+ for (var i = 0; i < invoice.length; i++) {
|
|
|
+ itemInvoice = invoice[i];
|
|
|
+
|
|
|
+ // Consultar currency_Rate
|
|
|
+ itemRate = self.getCutrrencyRate(itemInvoice.currency_id[0]);
|
|
|
+ if (!itemRate){
|
|
|
+ itemRate={};
|
|
|
+ itemRate.rate=1;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Consultar invoice line
|
|
|
+ itemLine = self.getInvoiceLine(itemInvoice.id);
|
|
|
+ if (!itemLine){
|
|
|
+ itemLine={};
|
|
|
+ itemLine.quantity=0;
|
|
|
+ itemLine.price_unit=0;
|
|
|
+ }
|
|
|
+
|
|
|
+ _.each(itemLine, function(line){
|
|
|
+ qty = qty+line.quantity;
|
|
|
+ total = total+((line.price_unit / itemRate.rate) * line.quantity);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ invoiceNew.push({ qty :qty,
|
|
|
+ total :total
|
|
|
+ });
|
|
|
+
|
|
|
+ return invoiceNew.shift();
|
|
|
+ },
|
|
|
+
|
|
|
+ // Obtener Cambio
|
|
|
+ getCutrrencyRate: function(currency_id){
|
|
|
+ return _.filter(this.currencyRate, function(rate){
|
|
|
+ return rate.currency_id[0] === currency_id;
|
|
|
+ }).shift();
|
|
|
+ },
|
|
|
+
|
|
|
+ // FIltar la Linea de la factura
|
|
|
+ getInvoiceLine: function(id_invoice){
|
|
|
+ var self = this;
|
|
|
+ return _.filter(self.lineInvoice,function(item){
|
|
|
+ return item.invoice_id[0] == id_invoice;
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+ 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;
|
|
|
+
|
|
|
+ if (local.widgetInstance) {
|
|
|
+ local.widgetInstance.actualizar(record.id);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (this.$el.find('.product_utility_class').length != 0) return;
|
|
|
+
|
|
|
+ local.widgetInstance = new local.ProductUtilityWidget(this);
|
|
|
+ var elemento = this.$el.find('.notebook-page-utility');
|
|
|
+ local.widgetInstance.appendTo(elemento);
|
|
|
+ local.widgetInstance.actualizar(record.id);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+}
|