summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/kartu_stock.py
blob: 22f90df06c9b22df5613615450ce11b64122f8e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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