summaryrefslogtreecommitdiff
path: root/fixco_custom/models/purchase_order.py
blob: 23936188309ac4d1dfc5d7c239dd564f8369f2e0 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
from odoo import fields, models, api, _
from odoo.exceptions import AccessError, UserError, ValidationError
from dateutil.relativedelta import relativedelta
from datetime import datetime, timedelta
import logging
from pytz import timezone, utc
import io
import requests
import json
import base64
try:
    from odoo.tools.misc import xlsxwriter
except ImportError:
    import xlsxwriter

_logger = logging.getLogger(__name__)


            
class PurchaseOrder(models.Model):
    _inherit = 'purchase.order'

    automatic_purchase_id = fields.Many2one(
        'automatic.purchase',
        string='Automatic Purchase Reference',
        ondelete='set null',
        index=True,
        copy=False
    )
    sale_order_id = fields.Many2one('sale.order', string='Sales Order', copy=False)
    move_entry_id = fields.Many2one('account.move', string='Journal Entries', copy=False)
    amount_discount = fields.Monetary(
        string='Total Discount',
        compute='_compute_amount_discount',
        store=True
    )

    biaya_lain_lain = fields.Float(
        'Biaya Lain Lain',
        default=0.0,
        tracking=True,
        copy=False
    )

    source = fields.Selection([
        ('requisition', 'Requisition'),
        ('reordering', 'Reordering'),
        ('purchasing_job', 'Purchasing Job'),
        ('manual', 'Manual')
    ], string='Source', default='manual', copy=False)
    count_journals = fields.Integer('Count Payment', compute='_compute_count_journals')
    shipping_cost = fields.Float(string='Shipping Cost', help='Total shipping cost for this PO')
    order_altama_id = fields.Integer('Req Order Altama', copy=False)
    soo_number = fields.Char('SOO Number', copy=False)
    soo_price = fields.Float('SOO Price', copy=False)
    soo_discount = fields.Float('SOO Discount', copy=False)
    soo_tax = fields.Float('SOO Tax', copy=False)

    def _get_fixco_token(self, source='auto'):
        ICP = self.env['ir.config_parameter'].sudo()
        TokenLog = self.env['token.log'].sudo()

        token_url = ICP.get_param('token.adempiere.altama')
        client_id = ICP.get_param('client.adempiere.altama')
        client_secret = ICP.get_param('secret_key.adempiere.altama')

        active_token = TokenLog.search([
            ('is_active', '=', True),
            ('token_from', '=', 'Adempiere Altama'),
            ('expires_at', '>', datetime.utcnow()),
        ], limit=1, order='id desc')

        if active_token:
            return active_token.token

        headers = {
            "Authorization": "Basic " + base64.b64encode(f"{client_id}:{client_secret}".encode()).decode(),
            "Content-Type": "application/x-www-form-urlencoded",
        }
        data = {"grant_type": "client_credentials"}

        response = requests.post(token_url, data=data, headers=headers, timeout=15)
        if response.status_code == 200:
            result = response.json()
            token = result.get("access_token")
            expires_in = result.get("expires_in", 3600)
            expiry_time = datetime.utcnow() + timedelta(seconds=expires_in - 60)

            TokenLog.search([
                ('token_from', '=', 'Adempiere Altama'),
                ('is_active', '=', True),
            ]).write({'is_active': False})

            TokenLog.create({
                'token': token,
                'expires_at': expiry_time,
                'is_active': True,
                'created_by': self.env.user.id if self.env.user else None,
                'source': source,
                'token_from': 'Adempiere Altama',
            })

            return token

        else:
            raise Exception(f"Gagal ambil token: {response.status_code} - {response.text}")

    def action_create_order_altama(self):
        ICP = self.env['ir.config_parameter'].sudo()
        for order in self:
            try:
                token = self._get_fixco_token(source='manual')
                url = ICP.get_param('endpoint.create.order.adempiere.altama')
                headers = {
                    "Authorization": f"Bearer {token}",
                    "Content-Type": "application/json",
                }

                payload = {
                    "date_po": order.date_approve.strftime("%Y%m%d%H%M%S"),
                    "no_po": order.name,
                    "details": [
                        {
                            "item_code": line.product_id.default_code or "",
                            "price": line.price_unit,
                            "qty": line.product_qty,
                        }
                        for line in order.order_line
                    ],
                }

                response = requests.post(url, json=payload, headers=headers, timeout=20)

                try:
                    result = response.json()
                except json.JSONDecodeError:
                    raise Exception(f"Response bukan JSON valid: {response.text}")

                if response.status_code == 200 and result.get("code") == "00":
                    contents = result.get("contents", {})
                    if isinstance(contents, dict):
                        order.order_altama_id = contents.get("req_id")
                    else:
                        order.order_altama_id = contents.get("req_id")

                elif response.status_code == 404:
                    raise Exception("URL endpoint gak ditemukan (404). Pastikan path-nya benar di Altama API.")
                elif response.status_code == 401:
                    token = self._get_fixco_token(source='auto')
                    headers["Authorization"] = f"Bearer {token}"
                    response = requests.post(url, json=payload, headers=headers, timeout=20)
                elif response.status_code not in (200, 201):
                    raise Exception(f"Gagal kirim ke Altama: {response.status_code} - {response.text}")

                self.message_post(body=f"✅ PO berhasil dikirim ke Altama!\nResponse: {json.dumps(result, indent=2)}")

            except Exception as e:
                self.message_post(body=f"❌ Gagal kirim ke Altama:<br/><pre>{str(e)}</pre>")


    def action_get_order_altama(self):
        ICP = self.env['ir.config_parameter'].sudo()

        for order in self:
            try:
                # ============================
                #  Get Token
                # ============================
                token = self._get_fixco_token(source='manual')

                url = ICP.get_param('endpoint.get.order.adempiere.altama')
                if not url:
                    raise Exception("Parameter 'endpoint.adempiere.altama' belum diset di System Parameters.")

                headers = {
                    "Authorization": f"Bearer {token}",
                    "Content-Type": "application/json",
                }

                params = {
                    "orderreq_id": order.order_altama_id or 0,
                }

                # ============================
                #  Request ke API
                # ============================
                response = requests.get(url, headers=headers, params=params, timeout=20)

                if response.status_code == 401:
                    token = self._get_fixco_token(source='auto')
                    headers["Authorization"] = f"Bearer {token}"
                    response = requests.get(url, headers=headers, params=params, timeout=20)

                if response.status_code not in (200, 201):
                    raise Exception(f"Gagal ambil data dari Altama: {response.status_code} - {response.text}")

                data = response.json()

                # ============================
                #   Extract Data
                # ============================
                contents_root = data.get("contents", {})
                contents_list = contents_root.get("contents", [])

                if not isinstance(contents_list, list):
                    raise Exception("Format data contents dari Altama tidak sesuai (expected list).")

                order.message_post(
                    body=f"✅ Berhasil ambil data dari Altama. Ditemukan {len(contents_list)} record."
                )

                # =====================================================
                #                  LOOP MAIN DATA
                # =====================================================
                for item in contents_list:

                    req_id = item.get("req_id")
                    no_po = item.get("no_po")
                    list_item_po = item.get("list_Item_po", [])
                    list_soo = item.get("list_soo", [])

                    # ============================
                    #  Isi Data SOO Ke Order
                    # ============================
                    for soo in list_soo:
                        order.soo_number = soo.get("no_soo")
                        order.soo_price = soo.get("totalprice")
                        order.soo_discount = soo.get("diskon")
                        order.soo_tax = soo.get("ppn")

                    order.order_altama_id = req_id

                    # ============================
                    #  Update Order Lines
                    # ============================
                    for item_line in list_item_po:

                        line = order.order_line.filtered(
                            lambda l: l.product_id.default_code == item_line.get("item_code")
                        )

                        if line:
                            line.write({
                                "description": item_line.get("description", ""),
                                "altama_ordered": item_line.get("qtyordered", 0),
                                "altama_delivered": item_line.get("qtydelivered", 0),
                                "altama_invoiced": item_line.get("qtyinvoiced", 0),
                                "docstatus_altama": item_line.get("docstatus", ""),
                            })

                    # =====================================================
                    #            BUILD HTML TABLES FOR CHATTER
                    # =====================================================

                    # ---- SOO TABLE ----
                    soo_rows = ""
                    for s in list_soo:
                        soo_rows += f"""
                            <tr>
                                <td>{s.get('no_soo')}</td>
                                <td>{s.get('totalprice')}</td>
                                <td>{s.get('diskon')}</td>
                                <td>{s.get('ppn')}</td>
                            </tr>
                        """

                    soo_table = f"""
                        <table style="width:100%; border-collapse: collapse; margin-top: 10px;">
                            <thead>
                                <tr style="background:#f1f1f1;">
                                    <th style="border:1px solid #ccc; padding:6px;">SOO Number</th>
                                    <th style="border:1px solid #ccc; padding:6px;">Total Price</th>
                                    <th style="border:1px solid #ccc; padding:6px;">Diskon</th>
                                    <th style="border:1px solid #ccc; padding:6px;">PPN</th>
                                </tr>
                            </thead>
                            <tbody>
                                {soo_rows or '<tr><td colspan="4">Tidak ada data SOO</td></tr>'}
                            </tbody>
                        </table>
                    """

                    # ---- ITEM PO TABLE ----
                    po_rows = ""
                    for l in list_item_po:

                        desc = l.get("description") or ""

                        # Flag: row error kalau description tidak mulai dengan SOO/
                        is_error = not desc.startswith("SOO/")

                        # Style row merah
                        row_style = "color:red; font-weight:bold;" if is_error else ""

                        po_rows += f"""
                            <tr style="{row_style}">
                                <td>{l.get('item_code')}</td>
                                <td>{desc}</td>
                                <td>{l.get('qtyordered')}</td>
                                <td>{l.get('qtydelivered')}</td>
                                <td>{l.get('qtyinvoiced')}</td>
                                <td>{l.get('docstatus')}</td>
                            </tr>
                        """


                    po_table = f"""
                        <table style="width:100%; border-collapse: collapse; margin-top: 10px;">
                            <thead>
                                <tr style="background:#f1f1f1;">
                                    <th style="border:1px solid #ccc; padding:6px;">Item Code</th>
                                    <th style="border:1px solid #ccc; padding:6px;">Description</th>
                                    <th style="border:1px solid #ccc; padding:6px;">Ordered</th>
                                    <th style="border:1px solid #ccc; padding:6px;">Delivered</th>
                                    <th style="border:1px solid #ccc; padding:6px;">Invoiced</th>
                                    <th style="border:1px solid #ccc; padding:6px;">Status</th>
                                </tr>
                            </thead>
                            <tbody>
                                {po_rows or '<tr><td colspan="6">Tidak ada item PO</td></tr>'}
                            </tbody>
                        </table>
                    """

                    # ---- POST TO CHATTER ----
                    order.message_post(
                        body=f"""
                            <b>📦 Data SOO</b><br/>{soo_table}
                            <br/><br/>
                            <b>📦 Data Item PO</b><br/>{po_table}
                        """
                    )

            except Exception as e:
                order.message_post(
                    body=f"❌ Gagal ambil data dari Altama:<br/><pre>{str(e)}</pre>"
                )


    def button_confirm(self):
        res = super(PurchaseOrder, self).button_confirm()
        self.action_create_order_altama()
        return res

    @api.onchange('shipping_cost')
    def _onchange_shipping_cost(self):
        for order in self:
            if not order.order_line:
                continue

            total_subtotal = sum(order.order_line.mapped('original_price_unit'))
            if total_subtotal == 0:
                continue

            if order.shipping_cost and order.shipping_cost > 0:
                # Distribusi ongkos kirim prorata
                for line in order.order_line:
                    proportion = (line.original_price_subtotal / total_subtotal)
                    allocated_cost = order.shipping_cost * proportion
                    extra_cost_per_unit = allocated_cost / (line.product_qty or 1)
                    line.price_unit = line.original_price_unit + extra_cost_per_unit
            else:
                # Balikin harga ke semula kalau shipping_cost = 0
                for line in order.order_line:
                    line.price_unit = line.original_price_unit

    def _compute_count_journals(self):
        for order in self:
            journals = self.env['account.move'].search([
                ('purchase_order_id', '=', order.id),
                ('move_type', '!=', 'in_invoice')
            ])
            order.count_journals = len(journals)

    def action_view_related_journals(self):
        self.ensure_one()

        journals = self.env['account.move'].search([
            ('purchase_order_id', '=', self.id),
            ('move_type', '!=', 'in_invoice')
        ])

        return {
            'name': 'Journals',
            'type': 'ir.actions.act_window',
            'res_model': 'account.move',
            'view_mode': 'tree,form',
            'target': 'current',
            'domain': [('id', 'in', journals.ids)],
        }
    
    @api.depends('order_line.price_total', 'biaya_lain_lain')
    def _amount_all(self):
        super(PurchaseOrder, self)._amount_all()
        
        for order in self:
            amount_total = order.amount_untaxed + order.amount_tax - order.biaya_lain_lain
            order.amount_total = order.currency_id.round(amount_total)

    @api.depends('order_line.discount_amount')
    def _compute_amount_discount(self):
        for order in self:
            order.amount_discount = sum(line.discount_amount for line in order.order_line)