Перейти к содержимому
Меню
Чтобы взаимодействовать с сообществом, необходимо зарегистрироваться.
Этот вопрос был отмечен
3 Ответы
2337 Представления
Hello, how could I add a button to my invoice form that would allow me to import from a csv file the lines with the products that the invoice will refer to without leaving the form?


Аватар
Отменить
Лучший ответ

hello  *Danny
please go though this video it wil help you on Csv/Xlsx file import using button, here is the referance.
https://www.youtube.com/watch?v=KsjaQnXahqU

Аватар
Отменить
Лучший ответ

Hi  Danny Gonzalez,

I think this video can help you: https://www.youtube.com/watch?v=UOBxxnYDIsM&list=PLSKcWRTtEl5qzvRaI-VTGavfReiHS_EEb&index=1

Аватар
Отменить
Лучший ответ

Hi,

In your Button action you can write this code based on your file you can change the record position
This example for XLSX you can try on this method to import the CSV file
class ImportLines(models.Model):
    _name = 'import.lines'

    xls_file = fields.Binary('File')

    def import_xls(self):
        """  Import xlsx report to Invoice line """
        wb = openpyxl.load_workbook(
            filename=BytesIO(base64.b64decode(self.xls_file)),
            read_only=True
        )
        ws = wb.active
        for record in ws.iter_rows(min_row=2, max_row=None, min_col=None,
                                   max_col=None, values_only=True):
            active_id = self.env.context.get('active_id')
            product = record[2]
            if product:
                val = product.split()
            # pro = val[0]
                string = re.sub("[\([{})\]]", "", val[0])
                new = self.env['account.move'].create({
                    'product_id': active_id,
                    'display_type': False,
                    'product_id':  self.env['product.product'].search([
                                ('default_code', '=', string)]).id,
                    'name': record[0],
                    'qyanity': record[1],
                    'price_unit': record[3],
                    'product_uom_id':  self.env['uom.uom'].search([
                                ('name', '=', record[4])]).id
                })

if you used csv file

buffer = tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx")
        buffer.write(binascii.a2b_base64(self.file))
        buffer.seek(0)
        book = xlrd.open_workbook(buffer.name)
        sheet = book.sheet_by_index(0)
        for row_no in range(sheet.nrows):
            if row_no <= 0:
                fields = map(lambda row: row.value.encode('utf-8'),
                             sheet.row(row_no))
            else:
                line = list(
                    map(lambda row: isinstance(row.value,
                                               bytes) and row.value.encode(
                        'utf-8') or str(row.value),
                        sheet.row(row_no)))
                       
                       
              Try this code and based on  your field value you can find the record details Use the above code you can create invoice lines


Hope it helps

Аватар
Отменить
Related Posts Ответы Просмотры Активность
4
авг. 24
2216
1
июл. 22
1509
2
мая 25
731
2
мар. 24
2970
1
окт. 22
4012