|
@@ -0,0 +1,220 @@
|
|
|
+openerp.import_cost_calculation = function(instance) {
|
|
|
+
|
|
|
+ instance.widgetInstance = null;
|
|
|
+ instance.parentInstance = {};
|
|
|
+ var QWeb = openerp.web.qweb;
|
|
|
+
|
|
|
+ instance.ImportCostWidget = instance.Widget.extend({
|
|
|
+ template: "import_cost_calculation.CalculateButton",
|
|
|
+
|
|
|
+ init:function(parent){
|
|
|
+ this._super(parent);
|
|
|
+ },
|
|
|
+
|
|
|
+ updateId : function(record){
|
|
|
+ var self = this;
|
|
|
+ self.record=record;
|
|
|
+ },
|
|
|
+
|
|
|
+ start: function () {
|
|
|
+ var self = this;
|
|
|
+ this.$el.click(function (e) {
|
|
|
+ var validation = self.checkValidation();
|
|
|
+ if (validation == true){
|
|
|
+ self.fecthInitial();
|
|
|
+ }else{
|
|
|
+ alert("Necesita cargar al menos una factura de compra y otra de gasto para poder realizar el cálculo!");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ checkValidation: function(){
|
|
|
+ var self = this;
|
|
|
+ if(!self.record.id){
|
|
|
+ return alert("Antes de realizar el cálculo, guarde el documento!");
|
|
|
+ }
|
|
|
+ if(self.record.expenses.length == 0 || self.record.purchases.length == 0){
|
|
|
+ return false;
|
|
|
+ }else {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ fecthInitial: function(){
|
|
|
+ var self = this;
|
|
|
+
|
|
|
+ self.fetchImportPurchaseLine(self.record.id).then(function(ImportPurchaseLine) {
|
|
|
+ return ImportPurchaseLine;
|
|
|
+ }).then(function(ImportPurchaseLine) {
|
|
|
+ self.ImportPurchaseLine = ImportPurchaseLine;
|
|
|
+ return self.fetchImportExpenseLine(self.record.id)
|
|
|
+ }).then(function(ImportExpenseLine) {
|
|
|
+ self.ImportExpenseLine = ImportExpenseLine;
|
|
|
+ return self.fetchAccountInvoiceLine();
|
|
|
+ }).then(function(AccountInvoiceLine){
|
|
|
+ self.AccountInvoiceLine = AccountInvoiceLine;
|
|
|
+ return self.renderModal(self.record);
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ fetchImportPurchaseLine: function(import_id) {
|
|
|
+ var ImportPurchaseLine = new openerp.web.Model('import.purchase.line');
|
|
|
+ var fields = ['id', 'import_id', 'purchase_id', 'purchase_subtotal','purchase_taxes','purchase_total'];
|
|
|
+ var domain = [['import_id', '=', parseInt(import_id)]];
|
|
|
+ return ImportPurchaseLine.query(fields).filter(domain).all();
|
|
|
+ },
|
|
|
+
|
|
|
+ fetchImportExpenseLine: function(import_id) {
|
|
|
+ var ImportExpenseLine = new openerp.web.Model('import.expense.line');
|
|
|
+ var fields = ['id', 'import_id', 'expense_id', 'expense_subtotal','expense_taxes','expense_total'];
|
|
|
+ var domain = [['import_id', '=', parseInt(import_id)]];
|
|
|
+ return ImportExpenseLine.query(fields).filter(domain).all();
|
|
|
+ },
|
|
|
+
|
|
|
+
|
|
|
+ fetchAccountInvoiceLine: function() {
|
|
|
+ var AccountInvoiceLine = new openerp.web.Model('account.invoice.line');
|
|
|
+ var fields = ['id', 'name', 'invoice_id', 'product_id', 'price_unit','quantity', 'price_subtotal'];
|
|
|
+ return AccountInvoiceLine.query(fields).filter().all();
|
|
|
+ },
|
|
|
+
|
|
|
+ getAccountInvoiceLine: function (id) {
|
|
|
+ var self = this;
|
|
|
+ return _.filter(self.AccountInvoiceLine,function (item) {
|
|
|
+ return item.invoice_id[0] == id;
|
|
|
+ })
|
|
|
+ },
|
|
|
+
|
|
|
+ renderModal: function (record){
|
|
|
+ var self = this;
|
|
|
+ var purchases = [];
|
|
|
+ var expenses = [];
|
|
|
+ var params = [];
|
|
|
+ var expenses_subtotal = 0;
|
|
|
+ var expenses_taxes = 0;
|
|
|
+ var expenses_total = 0;
|
|
|
+ var purchases_subtotal = 0;
|
|
|
+ var purchases_taxes = 0;
|
|
|
+ var purchases_total = 0;
|
|
|
+ var total = 0;
|
|
|
+ var factor = 0;
|
|
|
+ var purchase_lines = self.ImportPurchaseLine;
|
|
|
+ var expenses_lines = self.ImportExpenseLine;
|
|
|
+
|
|
|
+ _.each(expenses_lines, function(item){
|
|
|
+ expenses_subtotal = expenses_subtotal + item.expense_subtotal;
|
|
|
+ expenses_taxes = expenses_taxes + item.expense_taxes;
|
|
|
+ expenses_total = expenses_total + item.expense_total;
|
|
|
+ var invoice_line = self.getAccountInvoiceLine(item.expense_id[0]);
|
|
|
+ _.each(invoice_line, function(product) {
|
|
|
+ product['price_unit'] = accounting.formatMoney(product.price_unit, '',0, '.', ',');
|
|
|
+ product['price_subtotal'] = accounting.formatMoney(product.price_subtotal, '',0, '.', ',');
|
|
|
+ expenses.push(product);
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ _.each(purchase_lines, function(item){
|
|
|
+ purchases_subtotal = purchases_subtotal + item.purchase_subtotal;
|
|
|
+ purchases_taxes = purchases_taxes + item.purchase_taxes;
|
|
|
+ purchases_total = purchases_total + item.purchase_total;
|
|
|
+ });
|
|
|
+
|
|
|
+ total = purchases_total + expenses_total;
|
|
|
+ factor = total/purchases_total;
|
|
|
+ factor = parseFloat(factor.toFixed(6));
|
|
|
+
|
|
|
+ _.each(purchase_lines, function(item){
|
|
|
+ var invoice_line = self.getAccountInvoiceLine(item.purchase_id[0]);
|
|
|
+ _.each(invoice_line, function(product) {
|
|
|
+ product['import_cost'] = parseFloat((product.price_unit * factor).toFixed(0));
|
|
|
+ product['import_cost_formated'] = accounting.formatMoney((product.price_unit * factor), '',0, '.', ',');
|
|
|
+ product['price_unit'] = accounting.formatMoney(product.price_unit, '',0, '.', ',');
|
|
|
+ product['price_subtotal'] = accounting.formatMoney(product.price_subtotal, '',0, '.', ',');
|
|
|
+ purchases.push(product);
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ params={
|
|
|
+ title:'Resumen de cálculo de importación',
|
|
|
+ name: record.name,
|
|
|
+ date: record.date,
|
|
|
+ state: record.state,
|
|
|
+ purchases : purchases,
|
|
|
+ purchase_subtotal: accounting.formatMoney(purchases_subtotal, '',0, '.', ','),
|
|
|
+ purchase_taxes: accounting.formatMoney(purchases_taxes, '',0, '.', ','),
|
|
|
+ purchase_total:accounting.formatMoney(purchases_total, '',0, '.', ','),
|
|
|
+ expenses: expenses,
|
|
|
+ expenses_subtotal: accounting.formatMoney(expenses_subtotal, '',0, '.', ','),
|
|
|
+ expenses_taxes: accounting.formatMoney(expenses_taxes, '',0, '.', ','),
|
|
|
+ expenses_total: accounting.formatMoney(expenses_total, '',0, '.', ','),
|
|
|
+ factor:factor,
|
|
|
+ }
|
|
|
+
|
|
|
+ $("body").append(QWeb.render("import_cost_modal", params));
|
|
|
+ $('#myModal').modal('show');
|
|
|
+
|
|
|
+ $('#myModal').on('shown.bs.modal', function (e) {
|
|
|
+ if(record.state === 'approved'){
|
|
|
+ $('#approveImportButton').button("disable");
|
|
|
+}
|
|
|
+ $('#approveImportButton').click(function() {
|
|
|
+ var saveParams= {
|
|
|
+ import_id: record.id,
|
|
|
+ purchases: purchases,
|
|
|
+ factor: factor,
|
|
|
+ }
|
|
|
+
|
|
|
+ self.approveImport(saveParams).then(function(results) {
|
|
|
+ return results;
|
|
|
+ }).then(function(results) {
|
|
|
+ self.reloadPage();
|
|
|
+ self.closeModal();
|
|
|
+ });
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ $('#myModal').on('hidden.bs.modal', function(e) {
|
|
|
+ self.closeModal(e);
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ approveImport:function(saveParams){
|
|
|
+ var self = this;
|
|
|
+ var saveImport = new openerp.web.Model('import.cost');
|
|
|
+ return saveImport.call('approve_import_cost',[saveParams], {
|
|
|
+ context: new openerp.web.CompoundContext()
|
|
|
+ });
|
|
|
+ },
|
|
|
+
|
|
|
+ closeModal: function(){
|
|
|
+ $('#myModal').remove();
|
|
|
+ $('.modal-backdrop').remove();
|
|
|
+ },
|
|
|
+
|
|
|
+ reloadPage: function() {
|
|
|
+ instance.parentInstance.reload();
|
|
|
+ },
|
|
|
+ });
|
|
|
+
|
|
|
+ if (instance.web && instance.web.FormView) {
|
|
|
+ instance.web.FormView.include({
|
|
|
+ load_record: function (record) {
|
|
|
+ this._super.apply(this, arguments);
|
|
|
+ if (this.model !== 'import.cost') return;
|
|
|
+ instance.parentInstance = this;
|
|
|
+
|
|
|
+ if (instance.widgetInstance) {
|
|
|
+ instance.widgetInstance.updateId(record);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ instance.widgetInstance = new instance.ImportCostWidget(this);
|
|
|
+
|
|
|
+ var elemento = this.$el.find('.import_cost_button');
|
|
|
+
|
|
|
+ instance.widgetInstance.appendTo(elemento[0]);
|
|
|
+ instance.widgetInstance.updateId(record);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+};
|