Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
2080 Weergaven

I have the problem that the function i call with rcp via javascript is executed multple times after the first call. The function is called via a button on the webview


First load of the page the function is called ones. If i execute it again the function get called two time and if i execute it again it is executed three time and so on.


This is my code:


fileUploaded: function () {
let pdf_file = this.$buttons.find('.o_file_importpdf')[0];
const curFiles = pdf_file.files;
if(curFiles.length === 0) {
console.log("no file selected");
} else {
for(const file of curFiles) {
if(file.type === "application/pdf") {
var reader = new FileReader();
var self = this;
reader.onloadend = function () {
var b64 = reader.result.replace(/^data:.+;base64,/, '');
self._rpc({
model: 'import.pdf',
method: 'parsefile',
args: [b64],
}).then(function(result) {
self.trigger_up('reload');
});
};
reader.readAsDataURL(file);
} else {
console.log("file is no pdf");
}
}
}
}
# -*- coding: utf-8 -*-

import base64
import datetime
from odoo import fields, models, api
from odoo_ocr_pdf import importpdf
from odoo_ocr_pdf.parsexml import Invoice,Product
from odoo_ocr_pdf.importpdf import Pdf

class Import_pdf(models.Model):
_name = "import.pdf"
_description = "parse pdf files"

@api.model
def parsefile(self, pdf):
base64_bytes = pdf.encode('utf-8')
decoded_data = base64.decodebytes(base64_bytes)
data = importpdf.convertbinary(decoded_data)
content = data.content
self.create_invoice(data)

def create_invoice(self, data):
partner_id = self.env['res.partner'].sudo().name_search(name=data.company, args=None, operator='ilike', limit=1)[0][0]
content = data.content
invoice_date = datetime.datetime.strptime(content.shippingdate, '%d.%m.%Y').date()
ref = "Belegnummer " + content.belegnumber
journal_id = self.env['account.journal'].sudo().name_search(name="Eingangsrechnungen", args=None, operator='ilike', limit=1)[0][0]
account_move = self.env['account.move'].sudo().create({
'partner_id': partner_id,
'type': 'in_invoice',
'invoice_date': invoice_date,
'journal_id': journal_id,
'ref': ref,
})
product_ids = self.env['product.template'].sudo().search([])
account = self.env['account.account'].sudo().search([('code', '=', '3400')])
if len(account) > 0:
account_id = account.id
else:
account_id = 0
taxs = []
for product in content.products:
name = product.name
taxs.append(float(content.tax))
tax = self.env['account.tax'].sudo().search([('type_tax_use', '=', 'purchase') , ('amount', '=', float(content.tax))])
if len(tax) > 0:
tax_id = tax.id
else:
tax_id = 0
for product_id in product_ids:
if product_id.name in product.name:
current_product_id = product_id.id
product.name = product_id.name
break
else:
current_product_id = 0
price_unit = float(product.price) * ((100 - product.discount) / 100)
line = self.env['account.move.line'].sudo().with_context(check_move_validity=False).create({
'partner_id': partner_id,
'name': product.name,
'product_id': current_product_id,
'move_id': account_move.id,
'account_id': account_id,
'journal_id': journal_id,
'quantity': float(product.count),
'price_unit': float(price_unit),
'product_uom_id': 1,
'date': invoice_date,
'tax_ids': [(6,0,[tax_id])],
})
tax_total = sum(taxs) / len(taxs)
skonto = content.price_netto * ((100 + tax_total) / 100) / 100 * float(content.skonto) * -1
account = self.env['account.account'].sudo().search([('code', '=', '3730')])
if len(account) > 0:
account_id = account.id
else:
account_id = 0
self.env['account.move.line'].sudo().with_context(check_move_validity=False).create({
'partner_id': partner_id,
'name': 'Skonto',
'product_id': 0,
'move_id': account_move.id,
'account_id': account_id,
'journal_id': journal_id,
'quantity': 1,
'price_unit': float(skonto),
'product_uom_id': 1,
'date': invoice_date,
})
account_move.with_context(check_move_validity=False)._recompute_tax_lines(recompute_tax_base_amount=True)
self.env['import.pdf'].invalidate_cache()


Avatar
Annuleer