from odoo import models from io import BytesIO import datetime from base64 import encodebytes import xlsxwriter class KartuStokWizardInherit(models.TransientModel): _inherit = 'kartu.stok.wizard' def action_kartu_stok_excel_single_sheet(self): active_ids_tmp = self.env.context.get('active_ids') active_model = self.env.context.get('active_model') if active_model == 'product.template': active_ids = self.env['product.product'].search( [('product_tmpl_id', 'in', active_ids_tmp), ('active', '=', True)]).ids else: active_ids = active_ids_tmp data = { 'location_id': self.location_id.id, 'day_date': self.day_date, 'previous_number_days': self.previous_number_days, 'date_from': self.date_from, 'date_to': self.date_to, 'ids': active_ids, 'context': {'active_model': active_model} } file_io = BytesIO() workbook = xlsxwriter.Workbook(file_io) self.generate_xlsx_single_sheet(workbook, data) workbook.close() fout = encodebytes(file_io.getvalue()) datetime_string = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") filename = f'Kartu_Stok_Single_{datetime_string}.xlsx' self.write({ 'fileout': fout, 'fileout_filename': filename }) file_io.close() return { 'type': 'ir.actions.act_url', 'target': 'new', 'url': 'web/content/?model=' + self._name + '&id=' + str(self.id) + '&field=fileout&download=true&filename=' + filename, } def generate_xlsx_single_sheet(self, workbook, data): bold = workbook.add_format({'bold': True}) border_date_right = workbook.add_format({'border':1, 'num_format': 'DD-MM', 'bg_color': '#dddddd', 'align': 'right'}) border_int_right = workbook.add_format({'border':1, 'num_format': '#,##0', 'bg_color': '#dddddd', 'align': 'right'}) border_int_right_bold = workbook.add_format({'border':1, 'num_format': '#,##0', 'bg_color': '#dddddd', 'align': 'right', 'bold': True}) border_text_center = workbook.add_format({'border':1, 'bg_color': '#dddddd', 'align': 'center'}) header_format = workbook.add_format({'bold': True, 'border':1, 'bg_color': '#808080', 'align': 'center'}) sheet = workbook.add_worksheet('Kartu Stok') docs = self.env['product.product'].browse(data['ids']) location = self.env['stock.location'].browse(data['location_id']) location_name = location.display_name.split('/')[0] row = 0 for doc in docs: # ========================= # HEADER PRODUCT # ========================= sheet.write(row, 0, doc.display_name, bold) row += 1 sheet.write(row, 0, location_name, bold) row += 2 # ========================= # TABLE HEADER # ========================= sheet.write(row, 0, 'Date', header_format) sheet.write(row, 1, 'In', header_format) sheet.write(row, 2, 'Out', header_format) sheet.write(row, 3, 'Stock', header_format) sheet.write(row, 4, 'Distributor', header_format) sheet.write(row, 5, 'Buyer', header_format) sheet.write(row, 6, 'Document', header_format) sheet.write(row, 7, 'Source Document', header_format) row += 1 stock_total = 0 stock_show_initial = False # ========================= # MOVE LOOP (SAMA LOGIC ASLI) # ========================= for move in doc.stock_move_ids.sorted(key=lambda sm: sm.date): for line in move.move_line_ids: if line.state != 'done': continue if line.location_id.id != data['location_id'] and \ line.location_dest_id.id != data['location_id']: continue if not stock_show_initial: sheet.write(row, 3, stock_total, border_int_right_bold) sheet.write(row, 4, 'Initial Stock', border_text_center) stock_show_initial = True row += 1 qty_in = 0 qty_out = 0 if line.location_dest_id.id == data['location_id']: qty_in = line.qty_done stock_total += qty_in if line.location_id.id == data['location_id']: qty_out = line.qty_done stock_total -= qty_out sheet.write(row, 0, line.date, border_date_right) sheet.write(row, 1, qty_in, border_int_right) sheet.write(row, 2, qty_out, border_int_right) sheet.write(row, 3, stock_total, border_int_right) # Distributor col = 4 if line.location_dest_id.id == data['location_id']: if line.picking_id and line.picking_id.origin: sheet.write(row, col, line.picking_id.partner_id.display_name, border_text_center) else: if line.location_id: if line.location_id.name == 'Inventory adjustment': sheet.write(row, col, 'Adjust *', border_text_center) else: sheet.write(row, col, line.location_id.location_id.name + ' *', border_text_center) else: sheet.write(row, col, doc.seller_ids[0].name + ' *' if doc.seller_ids else '', border_text_center) else: sheet.write(row, col, '', border_text_center) # Buyer col = 5 if line.location_id.id == data['location_id']: if line.picking_id and line.picking_id.origin: sheet.write(row, col, line.picking_id.partner_id.display_name, border_text_center) else: if line.location_dest_id: if line.location_dest_id.name == 'Inventory adjustment': sheet.write(row, col, 'Adjust *', border_text_center) else: sheet.write(row, col, line.location_dest_id.location_id.name + ' *', border_text_center) else: sheet.write(row, col, doc.seller_ids[0].name + ' *' if doc.seller_ids else '', border_text_center) else: sheet.write(row, col, '', border_text_center) # Document col = 6 if line.picking_id and line.picking_id.origin: sheet.write(row, col, line.picking_id.name, border_text_center) else: sheet.write(row, col, line.reference or '', border_text_center) # Source Document col = 7 if line.picking_id and line.picking_id.origin: sheet.write(row, col, line.picking_id.origin, border_text_center) else: sheet.write(row, col, line.reference or '', border_text_center) row += 1 row += 3 # jarak antar product