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
|
from odoo import models, api, fields, tools, _
from odoo.exceptions import UserError
from datetime import datetime
import logging, math
from odoo.tools import float_compare, float_round
_logger = logging.getLogger(__name__)
class AutomaticPurchase(models.Model):
_name = 'automatic.purchase'
_order = 'id desc'
_inherit = ['mail.thread']
_rec_name = 'number'
number = fields.Char(string='Document No', index=True, copy=False, readonly=True)
date_doc = fields.Date(string='Date', readonly=True, help='Isi tanggal hari ini', default=fields.Date.context_today, tracking=True)
automatic_purchase_lines = fields.One2many('automatic.purchase.line', 'automatic_purchase_id', string='Lines', auto_join=True)
is_po = fields.Boolean(string='Is PO', tracking=True)
responsible_id = fields.Many2one('res.users', string='Responsible', tracking=True, default=lambda self: self.env.user)
apo_type = fields.Selection([
('purchasing_job', 'Purchasing Job'),
('reordering', 'Reordering Rule'),
('requisition', 'Requisition'),
], string='Type', tracking=True)
purchase_order_ids = fields.One2many(
'purchase.order',
'automatic_purchase_id',
string='Generated Purchase Orders'
)
purchase_order_count = fields.Integer(
string='Purchase Orders',
compute='_compute_purchase_order_count'
)
sale_order_id = fields.Many2one('sale.order', string='Sales Order')
def action_generate_lines_from_so(self):
self.ensure_one()
self.automatic_purchase_lines.unlink()
if not self.sale_order_id:
raise UserError("Please select a Sales Order first.")
lines = []
for line in self.sale_order_id.order_line:
product = line.product_id
manage_stock = self.env['manage.stock'].search([
('product_id', '=', product.id)
], limit=1)
qty_min = manage_stock.min_stock if manage_stock else 0.0
qty_buffer = manage_stock.buffer_stock if manage_stock else 0.0
quant = self.env['stock.quant'].search([
('product_id', '=', product.id)
], limit=1)
qty_available = quant.available_quantity if quant else 0.0
pricelist = self.env['purchase.pricelist'].search([
('product_id', '=', product.id)
], limit=1)
vendor = pricelist.vendor_id if pricelist else False
price = pricelist.price if pricelist else 0.0
subtotal = price * line.product_uom_qty
lines.append({
'automatic_purchase_id': self.id,
'product_id': product.id,
'qty_purchase': line.product_uom_qty,
'qty_min': qty_min,
'qty_buffer': qty_buffer,
'qty_available': qty_available,
'partner_id': vendor.id if vendor else False,
'taxes_id': vendor.tax_id.id if vendor else False,
'price': price,
'product_public_category_id': product.product_public_category_id.id,
'brand_id': product.brand_id.id,
})
self.env['automatic.purchase.line'].create(lines)
def action_view_related_po(self):
self.ensure_one()
return {
'name': 'Related',
'type': 'ir.actions.act_window',
'res_model': 'purchase.order',
'view_mode': 'tree,form',
'target': 'current',
'domain': [('id', 'in', list(self.purchase_order_ids.ids))],
}
def _compute_purchase_order_count(self):
for record in self:
record.purchase_order_count = len(record.purchase_order_ids)
def action_view_purchase_orders(self):
self.ensure_one()
action = self.env.ref('purchase.purchase_form_action').read()[0]
if len(self.purchase_order_ids) > 1:
action['domain'] = [('id', 'in', self.purchase_order_ids.ids)]
action['view_mode'] = 'tree,form'
elif self.purchase_order_ids:
action['views'] = [(self.env.ref('purchase.purchase_order_form').id, 'form')]
action['res_id'] = self.purchase_order_ids[0].id
return action
def delete_note_pj(self):
self.env['purchasing.job.note'].search([
('product_id', 'in', self.automatic_purchase_lines.mapped('product_id').ids)
]).unlink()
def create_purchase_orders(self):
self.ensure_one()
if not self.automatic_purchase_lines:
raise UserError(_("No purchase lines to process!"))
self.delete_note_pj()
if self.is_po:
raise UserError(_("Purchase order already created!"))
vendor_lines = {}
for line in self.automatic_purchase_lines:
partner_id = line.partner_id.id
brand_id = line.brand_id.id if line.brand_id else 0
category_id = line.product_public_category_id.id if line.product_public_category_id else 0
if partner_id != 270:
key = (partner_id,)
elif brand_id == 3: # ryu
key = (partner_id, brand_id, category_id)
else:
key = (partner_id, brand_id)
vendor_lines.setdefault(key, []).append(line)
created_orders = self.env['purchase.order']
for key, lines in vendor_lines.items():
partner_id = key[0]
brand_id = key[1] if len(key) > 1 else None
category_id = key[2] if len(key) > 2 else None
vendor = self.env['res.partner'].browse(partner_id)
chunk_size = 10
line_chunks = [lines[i:i + chunk_size] for i in range(0, len(lines), chunk_size)]
for index, chunk in enumerate(line_chunks):
order = self._create_purchase_order(
vendor,
index + 1,
len(line_chunks),
brand_id=brand_id,
category_id=category_id
)
created_orders += order
for line in chunk:
self._create_purchase_order_line(order, line)
self.purchase_order_ids = [(6, 0, created_orders.ids)]
self.is_po = True
return self.action_view_purchase_orders()
def _create_purchase_order(self, vendor, sequence, total_chunks, brand_id=None, category_id=None):
return self.env['purchase.order'].create({
'partner_id': vendor.id,
'origin': self.number,
'date_order': fields.Datetime.now(),
'company_id': self.env.company.id,
'currency_id': vendor.property_purchase_currency_id.id or self.env.company.currency_id.id,
'automatic_purchase_id': self.id,
'source': self.apo_type,
})
def _create_purchase_order_line(self, order, line):
product = line.product_id
return self.env['purchase.order.line'].create({
'order_id': order.id,
'product_id': product.id,
'name': product.name,
'product_qty': line.qty_purchase,
'product_uom': product.uom_po_id.id,
'price_unit': line.price,
'date_planned': fields.Datetime.now(),
'taxes_id': [(6, 0, [line.taxes_id.id])] if line.taxes_id else False,
'automatic_purchase_line_id': line.id,
})
def generate_automatic_lines(self):
self.ensure_one()
self.automatic_purchase_lines.unlink()
manage_stocks = self.env['manage.stock'].search([])
location_id = 55
lines_to_create = []
for stock in manage_stocks:
quant_records = self.env['stock.quant'].search([
('product_id', '=', stock.product_id.id),
('location_id', '=', location_id)
])
total_available = quant_records.quantity or 0.0
_logger.info(
"Product %s: Available=%.4f, Min=%.4f, Buffer=%.4f",
stock.product_id.display_name,
total_available,
stock.min_stock,
stock.buffer_stock
)
comparison = float_compare(total_available, stock.min_stock, precision_rounding=0.0001)
if comparison <= 0:
qty_purchase = stock.buffer_stock - total_available
if float_compare(qty_purchase, 0.0, precision_rounding=0.0001) <= 0:
_logger.warning(
"Negative purchase quantity for %s: Available=%.4f, Buffer=%.4f, Purchase=%.4f",
stock.product_id.display_name,
total_available,
stock.buffer_stock,
qty_purchase
)
continue
pricelist = self.env['purchase.pricelist'].search([
('product_id', '=', stock.product_id.id),
('vendor_id', '=', stock.vendor_id.id)
], limit=1)
price = pricelist.price if pricelist else 0.0
subtotal = qty_purchase * price
_logger.info(
"Adding product %s: Available=%.4f, Min=%.4f, Purchase=%.4f",
stock.product_id.display_name,
total_available,
stock.min_stock,
qty_purchase
)
lines_to_create.append({
'automatic_purchase_id': self.id,
'product_id': stock.product_id.id,
'qty_purchase': qty_purchase,
'qty_min': stock.min_stock,
'qty_buffer': stock.buffer_stock,
'partner_id': stock.vendor_id.id,
'taxes_id': stock.vendor_id.tax_id.id,
'price': price,
'product_public_category_id': stock.product_id.product_public_category_id.id,
'brand_id': stock.product_id.brand_id.id,
})
else:
_logger.info(
"Skipping product %s: Available=%.4f > Min=%.4f",
stock.product_id.display_name,
total_available,
stock.min_stock
)
if lines_to_create:
self.env['automatic.purchase.line'].create(lines_to_create)
_logger.info("Created %d purchase lines", len(lines_to_create))
else:
_logger.warning("No products need to be purchased")
raise UserError(_("No products need to be purchased based on current stock levels."))
@api.model
def create(self, vals):
vals['number'] = self.env['ir.sequence'].next_by_code('automatic.purchase') or '0'
result = super(AutomaticPurchase, self).create(vals)
return result
class AutomaticPurchaseLine(models.Model):
_name = 'automatic.purchase.line'
_description = 'Automatic Purchase Line'
_order = 'automatic_purchase_id, id'
automatic_purchase_id = fields.Many2one('automatic.purchase', string='Ref', required=True, ondelete='cascade', index=True, copy=False)
product_id = fields.Many2one('product.product', string='Product')
qty_purchase = fields.Float(string='Qty Purchase')
qty_min = fields.Float(string='Qty Min')
qty_buffer = fields.Float(string='Qty Buffer')
qty_available = fields.Float(string='Qty Available', compute='compute_qty_available')
qty_onhand = fields.Float(string='Qty On Hand', compute='compute_qty_onhand')
qty_incoming = fields.Float(string='Qty Incoming', compute='compute_qty_incoming')
qty_outgoing = fields.Float(string='Qty Outgoing', compute='compute_qty_outgoing')
partner_id = fields.Many2one('res.partner', string='Vendor')
price = fields.Float(string='Price')
subtotal = fields.Float(string='Subtotal', compute='compute_subtotal')
is_po = fields.Boolean(String='Is PO')
taxes_id = fields.Many2one('account.tax', string='Taxes')
brand_id = fields.Many2one('brands', string='Brand')
product_public_category_id = fields.Many2one('product.public.category', string='Public Category')
def compute_subtotal(self):
for line in self:
line.subtotal = line.qty_purchase * line.price
@api.onchange('product_id')
def _onchange_product_id(self):
if self.product_id:
manage_stock = self.env['manage.stock'].search([
('product_id', '=', self.product_id.id)
], limit=1)
if manage_stock:
self.qty_min = manage_stock.min_stock
self.qty_buffer = manage_stock.buffer_stock
self.taxes_id = manage_stock.vendor_id.tax_id.id
self.partner_id = manage_stock.vendor_id.id
# pricelist = self.env['purchase.pricelist'].search([
# ('product_id', '=', self.product_id.id),
# ('vendor_id', '=', manage_stock.vendor_id.id)
# ], limit=1)
# if pricelist:
# self.price = pricelist.price
def compute_qty_available(self):
for line in self:
if line.product_id:
stock_quant = self.env['stock.quant'].search([
('product_id', '=', line.product_id.id),
('location_id', '=', 55)
], limit=1)
line.qty_available = stock_quant.available_quantity if stock_quant else 0.0
def compute_qty_onhand(self):
for line in self:
if line.product_id:
stock_quant = self.env['stock.quant'].search([
('product_id', '=', line.product_id.id),
('location_id', '=', 55)
], limit=1)
line.qty_onhand = stock_quant.quantity if stock_quant else 0.0
def compute_qty_incoming(self):
for line in self:
if line.product_id:
stock_move = self.env['stock.move'].search([
('product_id', '=', line.product_id.id),
('location_dest_id', '=', 55),
('location_id', '=', 4),
('state', 'in', ['waiting', 'confirmed', 'assigned', 'partially_available'])
])
line.qty_incoming = sum(move.product_uom_qty for move in stock_move)
def compute_qty_outgoing(self):
for line in self:
if line.product_id:
stock_move = self.env['stock.move'].search([
('product_id', '=', line.product_id.id),
('location_dest_id', '=', 5),
('location_id', '=', 55),
('state', 'in', ['waiting', 'confirmed', 'assigned', 'partially_available'])
])
line.qty_outgoing = sum(move.product_uom_qty for move in stock_move)
|