summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/sourcing.py
blob: 62540700ce1c5489d8988fc3adfde716df059e4f (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
from odoo import models, fields, api
from odoo.exceptions import UserError
import requests
import json
import logging, subprocess

_logger = logging.getLogger(__name__)

class SourcingOrderJob(models.Model):
    _name = 'sourcing.job.order'
    _descriptions = 'Sourcing Job Order MD'
    _rec_name = 'name'
    _inherit = ['mail.thread', 'mail.activity.mixin']

    name = fields.Char(string='Job Number', default='name', copy=False, readonly=True)
    leads_id = fields.Many2One('crm.lead', string='Leads Number')
    product_name = fields.Char(string='Nama Barang', required=True)
    descriptions = fields.Text(string='Deskripsi / Spesifikasi', required=True)
    quantity = fields.Float(string='Quantity Product', required=True)
    eta_sales = fiels.Date(string='Expected Ready')
    sla_product = fields.Date(string='Product Ready Date')
    price = fields.Float(string='Harga Product Vendor')
    vendor_id = fields.Many2One('res.partner', string="Vendor")
    tax_id = fields.Many2One('account.tax', string='Tax', domain=['|', ('active', '=', False), ('active', '=', True)])
    user_id = fields.Many2One('res.users', string='MD Person')
    subtotal = fields.Float(string='Total Purchase', compute='_compute_subtotal_purchase')
    state = fields.Selection([
        ('draft', 'Untaken'),
        ('taken', 'On Sourcing'),
        ('done', 'Complete'),
        ('cancel', 'Cancel')
    ], string='Status')

    @api.depends('quantity', 'price', 'tax_id')
    def _compute_subtotal_purchase(self):
        """Menghitung subtotal termasuk pajak."""
        for record in self:
            subtotal = (record.quantity or 0.0) * (record.price or 0.0)
            if record.tax_id:
                tax_amount = subtotal * (record.tax_id.amount / 100)
                subtotal += tax_amount

            record.subtotal = subtotal

    @api.model
    def create(self, vals):
        if not (self.env.user.has_group('indoteknik_custom.group_role_sales') or self.env.user.has_group('indoteknik_custom.group_role_merchandiser')):
            raise UserError("❌ Hanya Sales dan Merchandiser yang boleh membuat Sourcing Job.")

        if vals.get('name', 'New') == 'New':
            vals['name'] = self.env['ir.sequence'].next_by_code('sourcing.job.order') or 'New'
        
        if self.env.user.has_group('indoteknik_custom.group_role_merchandiser'):
            vals['user_id'] = self.env.user.id
            vals['state'] = 'taken'

        return super().create(vals)

    def write(self, vals):
        if not (self.env.user.has_group('indoteknik_custom.group_role_sales') or self.env.user.has_group('indoteknik_custom.group_role_merchandiser')):
            raise UserError("❌ Hanya Sales dan Merchandiser yang boleh membuat Sourcing Job.")

        return super().write(vals)