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
|
from odoo import models, fields, api
from odoo.exceptions import UserError, ValidationError
class TukarGuling(models.Model):
_name = 'tukar.guling'
_description = 'Tukar Guling'
_order = 'date desc, id desc'
_rec_name = 'name'
name = fields.Char('Number', required=True, copy=False, readonly=True, default='New')
date = fields.Datetime('Date', default=fields.Datetime.now, required=True)
out_num = fields.Many2one('stock.picking', 'Nomor BU/Out',
domain=[('picking_type_id.code', '=', 'outgoing')])
ba_num = fields.Text('Nomor BA')
notes = fields.Text('Notes')
return_type = fields.Selection(String='Return Type', selection=[
('tukar_guling', 'Tukar Guling'), # -> barang yang sama
('revisi_so', 'Revisi SO'), # -> ganti barang ?
('credit_memo', 'Credit Memo')]) # -> dijadiin credit memo
state = fields.Selection(string='Status', selection=[
('draft', 'Draft'),
('approval_sales', ' Approval Sales'),
('approval_logistic', 'Approval Logistic'),
('approval_finance', 'Approval Finance'),
('done', 'Done'),
('cancel', 'Canceled')
], default='draft', tracking=True, required=True)
line_ids = fields.One2many('tukar.guling.line', 'tukar_guling_id', string='Product Lines')
@api.constrains('return_type', 'out_num')
def _check_required_bu_fields(self):
for record in self:
if record.return_type in ['revisi_so', 'credit_memo', 'tukar_guling'] and not record.out_num:
raise ValidationError("BU/Out harus diisi!")
@api.constrains('line_ids', 'state')
def _check_product_lines(self):
"""Constraint: Product lines harus ada jika state bukan draft"""
for record in self:
if record.state in ('approval_sales', 'approval_logistic', 'approval_finance', 'done') and not record.line_ids:
raise ValidationError("Product lines harus diisi sebelum submit atau approve!")
def _validate_product_lines(self):
"""Helper method untuk validasi product lines"""
self.ensure_one()
# Check ada product lines
if not self.line_ids:
raise UserError("Belum ada product lines yang ditambahkan!")
# Check product sudah diisi
empty_lines = self.line_ids.filtered(lambda line: not line.product_id)
if empty_lines:
raise UserError("Ada product lines yang belum diisi productnya!")
# Check quantity > 0
zero_qty_lines = self.line_ids.filtered(lambda line: line.product_uom_qty <= 0)
if zero_qty_lines:
raise UserError("Quantity product tidak boleh kosong atau 0!")
return True
@api.model
def create(self, vals):
if not vals.get('name') or vals['name'] == 'New':
vals['name'] = self.env['ir.sequence'].next_by_code('tukar.guling') or 'New'
return super(TukarGuling, self).create(vals)
def copy(self, default=None):
if default is None:
default = {}
if 'name' not in default:
default.update({
'name': self.env['ir.sequence'].next_by_code(self._name) or 'New',
})
default.update({
'state': 'draft',
'date': fields.Datetime.now(),
})
new_record = super(TukarGuling, self).copy(default)
# Re-sequence lines
if new_record.line_ids:
for i, line in enumerate(new_record.line_ids):
line.sequence = (i + 1) * 10
return new_record
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_submit(self):
self.ensure_one()
if self.state != 'draft':
raise UserError("Submit hanya bisa dilakukan dari Draft.")
self.state = 'approval_sales'
def action_approve(self):
self.ensure_one()
if not self.out_num:
raise UserError("BU/Out harus diisi!")
if not self.return_type:
raise UserError("Return Type harus diisi!")
# Cek hak akses berdasarkan state
if self.state == 'approval_sales':
if not self.env.user.has_group('indoteknik_custom.group_sales_manager'):
raise UserError("Hanya Sales Manager yang boleh approve tahap ini.")
self.state = 'approval_logistic'
elif self.state == 'approval_logistic':
if not self.env.user.has_group('indoteknik_custom.group_logistic'):
raise UserError("Hanya Logistic Manager yang boleh approve tahap ini.")
self.state = 'approval_finance'
elif self.state == 'approval_finance':
if not self.env.user.has_group('indoteknik_custom.group_finance'):
raise UserError("Hanya Finance Manager yang boleh approve tahap ini.")
self.state = 'done'
else:
raise UserError("Status ini tidak bisa di-approve.")
def action_cancel(self):
self.ensure_one()
# if self.state == 'done':
# raise UserError("Tidak bisa cancel jika sudah done")
self.state = 'cancel'
class TukarGulingPO(models.Model):
_name = 'tukar.guling.po'
_inherit = 'tukar.guling'
_description = 'Tukar Guling PO'
# tukar_guling_id = fields.Many2one(
# 'tukar.guling', required=True, ondelete='cascade', string='Tukar Guling Ref'
# )
return_type = fields.Selection([
('tukar_guling', 'Tukar Guling'),
('revisi_po', 'Revisi PO'),
('debit_memo', 'Debit Memo'),
], string='Return Type', required=True)
@api.constrains('return_type', 'out_num')
def _check_required_bu_fields(self):
for record in self:
if record.return_type in ['tukar_guling', 'revisi_po', 'debit_memo'] and not record.out_num:
raise ValidationError("BU/Out harus diisi!")
@api.constrains('line_ids', 'state')
def _check_product_lines(self):
"""Constraint: Product lines harus ada jika state bukan draft"""
for record in self:
if record.state in ('approval_sales', 'approval_logistic', 'approval_finance', 'done') and not record.line_ids:
raise ValidationError("Product lines harus diisi sebelum submit atau approve!")
def _validate_product_lines(self):
"""Helper method untuk validasi product lines"""
self.ensure_one()
# Check ada product lines
if not self.line_ids:
raise UserError("Belum ada product lines yang ditambahkan!")
# Check product sudah diisi
empty_lines = self.line_ids.filtered(lambda line: not line.product_id)
if empty_lines:
raise UserError("Ada product lines yang belum diisi productnya!")
# Check quantity > 0
zero_qty_lines = self.line_ids.filtered(lambda line: line.product_uom_qty <= 0)
if zero_qty_lines:
raise UserError("Quantity product tidak boleh kosong atau 0!")
return True
@api.model
def create(self, vals):
if not vals.get('name') or vals['name'] in ('New', False):
vals['name'] = self.env['ir.sequence'].next_by_code('tukar.guling.po') or 'New'
return super(TukarGulingPO, self).create(vals)
def copy(self, default=None):
if default is None:
default = {}
# Generate sequence satu-satunya di sini
default['name'] = self.env['ir.sequence'].next_by_code('tukar.guling.po') or 'New'
default['state'] = 'draft'
default['date'] = fields.Datetime.now()
new_record = super(TukarGulingPO, self).copy(default)
# Re-sequence lines
if new_record.line_ids:
for i, line in enumerate(new_record.line_ids):
line.sequence = (i + 1) * 10
return new_record
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_submit(self):
self.ensure_one()
if self.state != 'draft':
raise UserError("Submit hanya bisa dilakukan dari Draft.")
self.state = 'approval_sales'
def action_approve(self):
self.ensure_one()
if not self.out_num:
raise UserError("BU/Out harus diisi!")
if not self.return_type:
raise UserError("Return Type harus diisi!")
if self.state == 'approval_sales':
self.state = 'approval_logistic'
elif self.state == 'approval_logistic':
self.state = 'approval_finance'
elif self.state == 'approval_finance':
self.state = 'done'
else:
raise UserError("Status ini tidak bisa di-approve.")
def action_cancel(self):
self.ensure_one()
# if self.state == 'done':
# raise UserError("Tidak bisa cancel jika sudah done")
self.state = 'cancel'
class TukarGulingLine(models.Model):
_name = 'tukar.guling.line'
_description = 'Tukar Guling Line'
_order = 'sequence, id'
sequence = fields.Integer('Sequence', default=10, copy=False)
tukar_guling_id = fields.Many2one('tukar.guling', string='Tukar Guling', required=True, ondelete='cascade')
product_id = fields.Many2one('product.product', string='Product', required=True)
product_uom_qty = fields.Float('Quantity', digits='Product Unit of Measure', required=True, default=1.0)
product_uom = fields.Many2one('uom.uom', string='Unit of Measure')
name = fields.Text('Description')
@api.model_create_multi
def create(self, vals_list):
"""Override create to auto-assign sequence"""
for vals in vals_list:
if 'sequence' not in vals or vals.get('sequence', 0) <= 0:
# Get max sequence untuk tukar_guling yang sama
tukar_guling_id = vals.get('tukar_guling_id')
if tukar_guling_id:
max_seq = self.search([
('tukar_guling_id', '=', tukar_guling_id)
], order='sequence desc', limit=1)
vals['sequence'] = (max_seq.sequence or 0) + 10
else:
vals['sequence'] = 10
return super(TukarGulingLine, self).create(vals_list)
@api.onchange('product_id')
def _onchange_product_id(self):
if self.product_id:
self.name = self.product_id.display_name
self.product_uom = self.product_id.uom_id
|