summaryrefslogtreecommitdiff
path: root/fixco_custom/models/automatic_purchase.py
diff options
context:
space:
mode:
Diffstat (limited to 'fixco_custom/models/automatic_purchase.py')
-rw-r--r--fixco_custom/models/automatic_purchase.py93
1 files changed, 60 insertions, 33 deletions
diff --git a/fixco_custom/models/automatic_purchase.py b/fixco_custom/models/automatic_purchase.py
index 1ef12ca..98a4136 100644
--- a/fixco_custom/models/automatic_purchase.py
+++ b/fixco_custom/models/automatic_purchase.py
@@ -77,6 +77,8 @@ class AutomaticPurchase(models.Model):
'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)
@@ -111,42 +113,72 @@ class AutomaticPurchase(models.Model):
self.ensure_one()
if not self.automatic_purchase_lines:
raise UserError(_("No purchase lines to process!"))
-
+
if self.is_po:
raise UserError(_("Purchase order already created!"))
-
+
vendor_lines = {}
+
for line in self.automatic_purchase_lines:
- if line.partner_id.id not in vendor_lines:
- vendor_lines[line.partner_id.id] = []
- vendor_lines[line.partner_id.id].append(line)
-
+ 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:
+ 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 vendor_id, lines in vendor_lines.items():
- vendor = self.env['res.partner'].browse(vendor_id)
-
- chunk_size = 10
+
+ 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 = 20
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))
+ 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):
- """Membuat purchase order untuk vendor"""
+ def _create_purchase_order(self, vendor, sequence, total_chunks, brand_id=None, category_id=None):
order_name = f"{self.number} - {vendor.name}"
+
+ if brand_id:
+ brand = self.env['brands'].browse(brand_id)
+ order_name += f" - {brand.name}"
+
+ if category_id:
+ cat = self.env['product.public.category'].browse(category_id)
+ order_name += f" - {cat.name}"
+
if total_chunks > 1:
order_name += f" ({sequence}/{total_chunks})"
-
+
return self.env['purchase.order'].create({
'partner_id': vendor.id,
'origin': self.number,
@@ -155,10 +187,12 @@ class AutomaticPurchase(models.Model):
'currency_id': vendor.property_purchase_currency_id.id or self.env.company.currency_id.id,
'automatic_purchase_id': self.id,
'source': self.apo_type,
+ 'name': order_name,
})
+
+
def _create_purchase_order_line(self, order, line):
- """Membuat purchase order line dari automatic purchase line"""
product = line.product_id
return self.env['purchase.order.line'].create({
@@ -175,24 +209,20 @@ class AutomaticPurchase(models.Model):
def generate_automatic_lines(self):
self.ensure_one()
- # Hapus lines yang sudah ada
self.automatic_purchase_lines.unlink()
manage_stocks = self.env['manage.stock'].search([])
- location_id = 55 # Lokasi gudang ID 55
+ location_id = 55
lines_to_create = []
for stock in manage_stocks:
- # Cari semua stock.quant untuk produk ini di lokasi 55
quant_records = self.env['stock.quant'].search([
('product_id', '=', stock.product_id.id),
('location_id', '=', location_id)
])
- # Hitung total qty_available (quantity - reserved_quantity) untuk lokasi tersebut
total_available = quant_records.quantity or 0.0
- # Log nilai untuk debugging
_logger.info(
"Product %s: Available=%.4f, Min=%.4f, Buffer=%.4f",
stock.product_id.display_name,
@@ -201,14 +231,11 @@ class AutomaticPurchase(models.Model):
stock.buffer_stock
)
- # Gunakan float_compare untuk perbandingan yang akurat
comparison = float_compare(total_available, stock.min_stock, precision_rounding=0.0001)
- if comparison <= 0: # Termasuk saat sama dengan min_stock
- # Hitung qty yang perlu dipesan
+ if comparison <= 0:
qty_purchase = stock.buffer_stock - total_available
- # Pastikan qty_purchase positif
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",
@@ -219,7 +246,6 @@ class AutomaticPurchase(models.Model):
)
continue
- # Dapatkan harga dari purchase.pricelist
pricelist = self.env['purchase.pricelist'].search([
('product_id', '=', stock.product_id.id),
('vendor_id', '=', stock.vendor_id.id)
@@ -228,7 +254,6 @@ class AutomaticPurchase(models.Model):
price = pricelist.price if pricelist else 0.0
subtotal = qty_purchase * price
- # Log penambahan produk
_logger.info(
"Adding product %s: Available=%.4f, Min=%.4f, Purchase=%.4f",
stock.product_id.display_name,
@@ -237,7 +262,6 @@ class AutomaticPurchase(models.Model):
qty_purchase
)
- # Kumpulkan data untuk pembuatan lines
lines_to_create.append({
'automatic_purchase_id': self.id,
'product_id': stock.product_id.id,
@@ -247,6 +271,8 @@ class AutomaticPurchase(models.Model):
'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(
@@ -256,7 +282,6 @@ class AutomaticPurchase(models.Model):
stock.min_stock
)
- # Buat lines sekaligus untuk efisiensi
if lines_to_create:
self.env['automatic.purchase.line'].create(lines_to_create)
_logger.info("Created %d purchase lines", len(lines_to_create))
@@ -291,6 +316,8 @@ class AutomaticPurchaseLine(models.Model):
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: