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
|
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']
_order = 'id asc'
name = fields.Char('Name', readonly=True)
partner_id = fields.Many2one('res.partner', string='Customer', readonly=True)
origin = fields.Many2one('sale.order', string='Origin SO', required=True)
schedule_date = fields.Date(
string="Schedule Date",
required=True,
tracking=True
)
start_date = fields.Datetime(
string="Date Processed",
copy=False,
tracking=True
)
done_date = fields.Datetime(string='Date Done', copy=False, tracking=True)
gudang_service_lines = fields.One2many('gudang.service.line', 'gudang_service_id', string='Gudang Service Lines')
remaining_date = fields.Char('Unprocessed Since', compute='_compute_remaining_date')
state = fields.Selection([('draft', 'Backlog'), ('onprogress', 'On Progress'),('done', 'Done'), ('cancel', 'Cancel')], default='draft', tracking=True)
cancel_reason = fields.Text('Cancel Reason', tracking=True)
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
# Safa
md = self.env['res.users'].browse([3425])
# send to logistic and safa
users = users | md
if not users:
return
excluded_users = [7, 17098, 216, 28, 15710]
for rec in self:
for user in users:
if user.id in excluded_users:
continue
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': _(
'Ada Jadwal Service Barang di Document <b>%s</b> Jadwal Service 📅 <b>%s</b>'
) % (rec.name, rec.schedule_date),
# 'date_deadline': fields.Date.today(),
})
# kirim ke private message odoo
channel = self.env['mail.channel'].channel_get([self.env.user.partner_id.id, user.partner_id.id]).get('id')
if not channel:
continue
res = self.env['mail.channel'].browse(channel)
res.with_user(self.env.user.browse(25)).message_post(body=_('Ada Jadwal Service Barang di Document <b>%s</b> Jadwal Service 📅 <b>%s</b>') % (rec.name, rec.schedule_date), message_type='comment', subtype_xmlid='mail.mt_comment')
@api.model
def cron_notify_onprogress_gudang_service(self):
records = self.search([
('state', '=', 'onprogress')
])
if records:
records._send_logistic_notification()
@api.depends('start_date', 'done_date', 'state')
def _compute_remaining_date(self):
today = fields.Date.today()
for rec in self:
if not rec.start_date:
rec.remaining_date = "-"
continue
start = rec.start_date.date()
if rec.state == 'done' and rec.done_date:
end = rec.done_date.date()
else:
end = today
days = (end - start).days
rec.remaining_date = _("%s days") % days
def action_submit(self):
for rec in self:
rec.state = 'onprogress'
rec.start_date = fields.Datetime.now()
# rec.date = fields.Datetime.now()
def action_done(self):
for rec in self:
activities = self.env['mail.activity'].search([
('res_id', '=', rec.id),
('res_model', '=', 'gudang.service'),
('state', '=', 'done')
])
activities.unlink()
rec.state = 'done'
if not rec.done_date:
rec.done_date = fields.Datetime.now()
def action_draft(self):
"""Reset to draft state"""
for rec in self:
rec.cancel_reason = False
if rec.state == 'cancel':
rec.write({'state': 'draft'})
else:
raise UserError("Only Canceled Record Can Be Reset To Draft")
def action_cancel(self):
for rec in self:
activities = self.env['mail.activity'].search([
('res_id', '=', rec.id),
('res_model', '=', 'gudang.service'),
])
activities.unlink()
if rec.state == 'done':
raise UserError("You cannot cancel a done record")
if not rec.cancel_reason:
raise UserError("Cancel Reason must be filled")
rec.start_date = False
rec.done_date = False
rec.state = 'cancel'
@api.model
def create(self, vals):
# Send notification
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
res = super(GudangService, self).create(vals)
# Send notification
res._send_logistic_notification()
return res
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"
_inherit = ['mail.thread', 'mail.activity.mixin']
product_id = fields.Many2one('product.product', string='Product')
quantity = fields.Float(string='Quantity')
origin_so = fields.Many2one('sale.order', string='Origin SO')
gudang_service_id = fields.Many2one('gudang.service', string='Gudang Service ID')
|