Merge pull request #352 from expsa/new_dev_tr
[FIX]FIX issue in barcode text img
This commit is contained in:
commit
ac04e87b88
|
|
@ -8,6 +8,7 @@ import base64
|
||||||
import os
|
import os
|
||||||
from odoo.exceptions import ValidationError
|
from odoo.exceptions import ValidationError
|
||||||
|
|
||||||
|
|
||||||
# import barcode as barcode
|
# import barcode as barcode
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from PIL import ImageDraw
|
from PIL import ImageDraw
|
||||||
|
|
@ -28,6 +29,7 @@ from odoo.tools.translate import _
|
||||||
# from odoo.osv.orm import setup_modifiers
|
# from odoo.osv.orm import setup_modifiers
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Transaction(models.Model):
|
class Transaction(models.Model):
|
||||||
_inherit = 'transaction.transaction'
|
_inherit = 'transaction.transaction'
|
||||||
|
|
||||||
|
|
@ -35,89 +37,42 @@ class Transaction(models.Model):
|
||||||
|
|
||||||
@api.constrains('ean13', 'name', 'transaction_date', 'type')
|
@api.constrains('ean13', 'name', 'transaction_date', 'type')
|
||||||
def binary_compute_constraint(self):
|
def binary_compute_constraint(self):
|
||||||
fonts = [os.path.dirname(__file__) + '/img/KacstOffice.ttf',
|
font_path = os.path.join(os.path.dirname(__file__), 'img', 'amiri-regular.ttf')
|
||||||
os.path.dirname(__file__) + '/img/amiri-regular.ttf']
|
font_size = 22
|
||||||
|
|
||||||
img = Image.new("RGBA", (500, 420), "white")
|
img = Image.new("RGBA", (500, 420), "white")
|
||||||
draw = ImageDraw.Draw(img)
|
draw = ImageDraw.Draw(img)
|
||||||
number_word = "الرقم : "
|
font = ImageFont.truetype(font_path, font_size)
|
||||||
number_word_reshaped = arabic_reshaper.reshape(
|
|
||||||
u'' + number_word)
|
|
||||||
number_word_artext = get_display(number_word_reshaped)
|
|
||||||
draw.text((220, 20),
|
|
||||||
number_word_artext, "black",
|
|
||||||
font=ImageFont.truetype(fonts[1], 18))
|
|
||||||
|
|
||||||
number_value = self.name
|
def draw_text(draw, text, position, font, alignment="left"):
|
||||||
number_value_reshaped = arabic_reshaper.reshape(
|
text_size = draw.textsize(text, font=font)
|
||||||
u'' + number_value if number_value else '')
|
|
||||||
number_value_artext = get_display(number_value_reshaped)
|
|
||||||
draw.text((80, 20),
|
|
||||||
number_value_artext, "black",
|
|
||||||
font=ImageFont.truetype(fonts[1], 18))
|
|
||||||
#
|
|
||||||
date_hijri = "التاريخ : "
|
|
||||||
date_hijri_reshaped = arabic_reshaper.reshape(
|
|
||||||
u'' + date_hijri)
|
|
||||||
date_hijri_artext = get_display(date_hijri_reshaped)
|
|
||||||
draw.text((211, 40),
|
|
||||||
date_hijri_artext, "black",
|
|
||||||
font=ImageFont.truetype(fonts[1], 18))
|
|
||||||
|
|
||||||
date_hijri_value = self.transaction_date_hijri
|
if alignment == "right":
|
||||||
date_hijri_value_reshaped = arabic_reshaper.reshape(
|
position = (position[0] - text_size[0], position[1])
|
||||||
u'' + date_hijri_value if date_hijri_value else '')
|
elif alignment == "center":
|
||||||
date_hijri_artext = get_display(date_hijri_value_reshaped)
|
position = (position[0] - text_size[0] / 2, position[1])
|
||||||
draw.text((120, 40),
|
|
||||||
date_hijri_artext.replace('-', '/'), "black",
|
|
||||||
font=ImageFont.truetype(fonts[1], 18))
|
|
||||||
|
|
||||||
date_m = "الموافق : "
|
draw.text(position, text, "black", font=font)
|
||||||
date_m_reshaped = arabic_reshaper.reshape(
|
|
||||||
u'' + date_m)
|
|
||||||
date_m_artext = get_display(date_m_reshaped)
|
|
||||||
draw.text((210, 65),
|
|
||||||
date_m_artext, "black",
|
|
||||||
font=ImageFont.truetype(fonts[1], 18))
|
|
||||||
|
|
||||||
date_m_value = self.transaction_date
|
# Draw text elements
|
||||||
date_m_value_reshaped = arabic_reshaper.reshape(
|
draw_text(draw, (self.name or '')+' :رقم المعاملة ', (10, 20), font)
|
||||||
u'' + str(date_m_value) if date_m_value else '')
|
draw_text(draw, (self.transaction_date_hijri or '').replace('-', '/')+" :تاريخ المعاملة الهجري ", (10, 60), font)
|
||||||
date_m_value_artext = get_display(date_m_value_reshaped)
|
draw_text(draw,(str(self.transaction_date) if self.transaction_date else '').replace('-', '/')+" :تاريخ المعاملة الميلادي ", (10, 100), font)
|
||||||
draw.text((120, 65),
|
draw_text(draw,(str(self.attachment_num) if self.attachment_num else '0')+ " :رتبة المعاملة " , (10, 140), font)
|
||||||
date_m_value_artext.replace('-', '/'), "black",
|
|
||||||
font=ImageFont.truetype(fonts[1], 18))
|
|
||||||
|
|
||||||
attach_m = "المرفقات : "
|
|
||||||
attach_m_reshaped = arabic_reshaper.reshape(
|
|
||||||
u'' + attach_m)
|
|
||||||
date_m_artext = get_display(attach_m_reshaped)
|
|
||||||
draw.text((200, 85),
|
|
||||||
date_m_artext, "black",
|
|
||||||
font=ImageFont.truetype(fonts[1], 18))
|
|
||||||
|
|
||||||
attach_m_value = str(self.attachment_num) if self.attachment_num else '0'
|
|
||||||
attach_m_value_reshaped = arabic_reshaper.reshape(
|
|
||||||
u'' + attach_m_value)
|
|
||||||
attach_mvalue_artext = get_display(attach_m_value_reshaped)
|
|
||||||
draw.text((180, 85),
|
|
||||||
attach_mvalue_artext, "black",
|
|
||||||
font=ImageFont.truetype(fonts[1], 18))
|
|
||||||
# barcode_symbology = options.get('symbology', 'Code128')
|
|
||||||
barcode = self.env['ir.actions.report'].barcode('Code11', self.name, width=250, height=100,
|
|
||||||
humanreadable=0)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Generate barcode
|
||||||
|
barcode = self.env['ir.actions.report'].barcode('Code11', self.name, width=250, height=100, humanreadable=0)
|
||||||
barcode_buffer = BytesIO(barcode)
|
barcode_buffer = BytesIO(barcode)
|
||||||
barcode_image_file = Image.open(barcode_buffer)
|
barcode_image_file = Image.open(barcode_buffer)
|
||||||
ImageDraw.Draw(img)
|
img.paste(barcode_image_file, (20, 180))
|
||||||
|
|
||||||
|
# Save image to binary field
|
||||||
buffered = BytesIO()
|
buffered = BytesIO()
|
||||||
img.paste(barcode_image_file, (20, 110))
|
|
||||||
img.save(buffered, format="png")
|
img.save(buffered, format="png")
|
||||||
img_str = base64.b64encode(buffered.getvalue())
|
img_str = base64.b64encode(buffered.getvalue())
|
||||||
self.binary_barcode = img_str
|
self.binary_barcode = img_str
|
||||||
|
|
||||||
|
|
||||||
class AttachmentInherit(models.Model):
|
class AttachmentInherit(models.Model):
|
||||||
_inherit = 'ir.attachment'
|
_inherit = 'ir.attachment'
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue