summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/gudang_service.py
blob: 600febfda7ddb4501e870e727d9f38d8add4afac (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
from odoo import models, fields, api, _
from odoo.exceptions import UserError, ValidationError
import logging
from datetime import datetime
from collections import defaultdict


class GudangService(models.Model):
    _name = "gudang.service"
    _description = "Gudang Service"
    _inherit = ['mail.thread', 'mail.activity.mixin']

    name = fields.Char('Name', readonly=True)
    partner_id = fields.Many2one('res.partner', string='Customer', readonly=True)
    origin = fields.Many2one('sale.order', string='Origin SO')
    picking_id = fields.Many2one('stock.picking', string = 'Picking ID', domain="[('sale_id', '=', origin)]")
    date = fields.Datetime('Date', default=fields.Datetime.now, required=True)
    gudang_service_lines = fields.One2many('gudang.service.line', 'gudang_service_id', string='Gudang Service Lines')
    remaining_date = fields.Char('Remaining Date', compute='_compute_remaining_date')
    state = fields.Selection([('draft', 'Draft'), ('onprogress', 'On Progress'),('done', 'Done'), ('cancel', 'Cancel')], default='draft')

    @api.onchange('picking_id')
    def _onchange_picking_id(self):
        if not self.picking_id:
            self.gudang_service_lines = [(5, 0, 0)]
            return

        lines = [(5, 0, 0)]
        for move in self.picking_id.move_ids_without_package:
            if move.product_id:
                lines.append((0, 0, {
                    'product_id': move.product_id.id,
                    'quantity': move.product_uom_qty,
                    'origin_so': self.origin.id,
                }))

        self.gudang_service_lines = lines


    def _send_logistic_notification(self):
        group = self.env.ref('indoteknik_custom.group_role_logistic', raise_if_not_found=False)
        if not group:
            return

        users = group.users
        if not users:
            return

        for rec in self:
            for user in users:
                self.env['mail.activity'].create({
                    'res_model_id': self.env['ir.model']._get_id('gudang.service'),
                    'res_id': rec.id,
                    'activity_type_id': self.env.ref('mail.mail_activity_data_todo').id,
                    'user_id': user.id,
                    'summary': 'Gudang Service On Progress',
                    'note': _(
                        'Gudang Service <b>%s</b> masih <b>On Progress</b> sejak %s'
                    ) % (rec.name, rec.date),
                    'date_deadline': fields.Date.today(),
                })

    @api.model
    def cron_notify_onprogress_gudang_service(self):
        records = self.search([
            ('state', '=', 'onprogress')
        ])

        if records:
            records._send_logistic_notification()


    @api.depends('date', 'state')
    def _compute_remaining_date(self):
        today = fields.Date.today()
        for rec in self:
            if rec.state in ['done', 'cancel', 'draft'] or not rec.date:
                rec.remaining_date = 0
                continue
            rec.remaining_date = (today - rec.date.date()).days

    def action_submit(self):
        self.state = 'onprogress'
        self._send_logistic_notification
        # self.send_odoo_notification()

    def action_done(self):
        self.state = 'done'
        # self.send_odoo_notification()

    def action_draft(self):
        """Reset to draft state"""
        for record in self:
            if record.state == 'cancel':
                record.write({'state': 'draft'})
            else:
                raise UserError("Hanya record yang di-cancel yang bisa dikembalikan ke draft")

    def action_cancel(self):
        self.state = 'cancel'

    # def write(vals, self):
        # self.send_odoo_notification()
        # return super(GudangService, self).write(vals)

    @api.model
    def create(self, vals):
        if not vals.get('name') or vals['name'] == 'New':
            vals['name'] = self.env['ir.sequence'].next_by_code('gudang.service')

        if vals.get('origin') and not vals.get('partner_id'):
            so = self.env['sale.order'].browse(vals['origin'])
            vals['partner_id'] = so.partner_id.id

        return super(GudangService, self).create(vals)
    
    def write(self, vals):
        if vals.get('origin'):
            so = self.env['sale.order'].browse(vals['origin'])
            vals['partner_id'] = so.partner_id.id

        return super(GudangService, self).write(vals)



    @api.onchange('origin')
    def _onchange_origin(self):
        if not self.origin:
            self.gudang_service_lines = [(5, 0, 0)]
            return
        
        self.partner_id = self.origin.partner_id

        lines = []
        for line in self.origin.order_line:
            lines.append((0, 0, {
                'product_id': line.product_id.id,
                'quantity': line.product_uom_qty,
                'origin_so': self.origin.id,
            }))

        # hapus line lama lalu isi baru
        self.gudang_service_lines = [(5, 0, 0)] + lines



class GudangServiceLine(models.Model):
    _name = "gudang.service.line"

    product_id = fields.Many2one('product.product', string='Product')
    quantity = fields.Float(string='Quantity')
    # picking_id = fields.Many2one('stock.picking', string='Nomor Picking')
    origin_so = fields.Many2one('sale.order', string='Origin SO')
    gudang_service_id = fields.Many2one('gudang.service', string='Gudang Service ID')