summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/automatic_purchase.py
diff options
context:
space:
mode:
Diffstat (limited to 'indoteknik_custom/models/automatic_purchase.py')
-rw-r--r--indoteknik_custom/models/automatic_purchase.py161
1 files changed, 86 insertions, 75 deletions
diff --git a/indoteknik_custom/models/automatic_purchase.py b/indoteknik_custom/models/automatic_purchase.py
index 83a7cb3c..d9ec17f4 100644
--- a/indoteknik_custom/models/automatic_purchase.py
+++ b/indoteknik_custom/models/automatic_purchase.py
@@ -93,7 +93,7 @@ class AutomaticPurchase(models.Model):
counter = 0
for vendor in vendor_ids:
- self.create_po_by_vendor(vendor['partner_id'][0])
+ self.create_po_for_vendor(vendor['partner_id'][0])
# param_header = {
# 'partner_id': vendor['partner_id'][0],
@@ -191,95 +191,106 @@ class AutomaticPurchase(models.Model):
if qty_pj > qty_outgoing_pj:
raise UserError('Qty yang anda beli lebih dari qty outgoing. %s' %id_po)
- def create_po_by_vendor(self, vendor_id):
+ def create_po_for_vendor(self, vendor_id):
current_time = datetime.now()
name = "/PJ/" if not self.apo_type == 'reordering' else "/A/"
-
PRODUCT_PER_PO = 20
auto_purchase_line = self.env['automatic.purchase.line']
- # Domain untuk semua baris dengan vendor_id tertentu
- domain = [
+ config = self.env['apo.domain.config'].search([
+ ('vendor_id', '=', vendor_id)
+ ], limit=1)
+
+ base_domain = [
('automatic_purchase_id', '=', self.id),
('partner_id', '=', vendor_id),
('qty_purchase', '>', 0)
]
- # Tambahkan domain khusus untuk brand_id 22 dan 564
- special_brand_domain = domain + [('brand_id', 'in', [22, 564])]
- regular_domain = domain + [('brand_id', 'not in', [22, 564])]
-
- # Fungsi untuk membuat PO berdasarkan domain tertentu
- def create_po_for_domain(domain, special_payment_term=False):
- products_len = auto_purchase_line.search_count(domain)
- page = math.ceil(products_len / PRODUCT_PER_PO)
-
- for i in range(page):
- # Buat PO baru
- param_header = {
- 'partner_id': vendor_id,
- 'currency_id': 12,
- 'user_id': self.env.user.id,
- 'company_id': 1, # indoteknik dotcom gemilang
- 'picking_type_id': 28, # indoteknik bandengan receipts
- 'date_order': current_time,
- 'from_apo': True,
- 'note_description': 'Automatic PO'
- }
+ # Kalau vendor punya brand spesial → bikin domain sesuai config
+ if config and config.is_special:
+ special_brand_domain = base_domain + [('brand_id', 'in', config.brand_ids.ids)]
+ self._create_po_for_domain(
+ vendor_id, special_brand_domain, name, PRODUCT_PER_PO, current_time, config.payment_term_id, special=config.is_special
+ )
- new_po = self.env['purchase.order'].create([param_header])
+ # Regular domain (selain brand spesial)
+ regular_domain = base_domain
+ if config and config.is_special and config.brand_ids:
+ regular_domain = base_domain + [('brand_id', 'not in', config.brand_ids.ids)]
- # Set payment_term_id khusus jika diperlukan
- if special_payment_term:
- new_po.payment_term_id = 29
- else:
- new_po.payment_term_id = new_po.partner_id.property_supplier_payment_term_id
+ self._create_po_for_domain(
+ vendor_id, regular_domain, name, PRODUCT_PER_PO, current_time, config.payment_term_id
+ )
- new_po.name = new_po.name + name + str(i + 1)
- self.env['automatic.purchase.match'].create([{
- 'automatic_purchase_id': self.id,
- 'order_id': new_po.id
- }])
+ def _create_po_for_domain(self, vendor_id, domain, name, PRODUCT_PER_PO, current_time, payment_term_id, special=False):
+ auto_purchase_line = self.env['automatic.purchase.line']
+ products_len = auto_purchase_line.search_count(domain)
+ page = math.ceil(products_len / PRODUCT_PER_PO)
+
+ for i in range(page):
+ # Buat header PO
+ param_header = {
+ 'partner_id': vendor_id,
+ 'currency_id': 12,
+ 'user_id': self.env.user.id,
+ 'company_id': 1,
+ 'picking_type_id': 28,
+ 'date_order': current_time,
+ 'from_apo': True,
+ 'note_description': 'Automatic PO'
+ }
+ new_po = self.env['purchase.order'].create(param_header)
+
+ # Set payment term
+ new_po.payment_term_id = payment_term_id.id if special else (
+ new_po.partner_id.property_supplier_payment_term_id
+ )
+
+ new_po.name = new_po.name + name + str(i + 1)
+
+ self.env['automatic.purchase.match'].create([{
+ 'automatic_purchase_id': self.id,
+ 'order_id': new_po.id
+ }])
+
+ # Ambil lines
+ lines = auto_purchase_line.search(
+ domain,
+ offset=i * PRODUCT_PER_PO,
+ limit=PRODUCT_PER_PO
+ )
+
+ # Pre-fetch sales_match biar ga search per line
+ sales_matches = self.env['automatic.purchase.sales.match'].search([
+ ('automatic_purchase_id', '=', self.id),
+ ('product_id', 'in', lines.mapped('product_id').ids),
+ ])
+ match_map = {sm.product_id.id: sm for sm in sales_matches}
+
+ for line in lines:
+ product = line.product_id
+ sales_match = match_map.get(product.id)
+ param_line = {
+ 'order_id': new_po.id,
+ 'product_id': product.id,
+ 'product_qty': line.qty_purchase,
+ 'qty_available_store': product.qty_available_bandengan,
+ 'suggest': product._get_po_suggest(line.qty_purchase),
+ 'product_uom_qty': line.qty_purchase,
+ 'price_unit': line.last_price,
+ 'ending_price': line.last_price,
+ 'taxes_id': [(6, 0, [line.taxes_id.id])] if line.taxes_id else False,
+ 'so_line_id': sales_match.sale_line_id.id if sales_match else None,
+ 'so_id': sales_match.sale_id.id if sales_match else None
+ }
+ new_po_line = self.env['purchase.order.line'].create(param_line)
+ line.current_po_id = new_po.id
+ line.current_po_line_id = new_po_line.id
+
+ self.create_purchase_order_sales_match(new_po)
- # Ambil baris sesuai halaman
- lines = auto_purchase_line.search(
- domain,
- offset=i * PRODUCT_PER_PO,
- limit=PRODUCT_PER_PO
- )
-
- for line in lines:
- product = line.product_id
- sales_match = self.env['automatic.purchase.sales.match'].search([
- ('automatic_purchase_id', '=', self.id),
- ('product_id', '=', product.id),
- ])
- param_line = {
- 'order_id': new_po.id,
- 'product_id': product.id,
- 'product_qty': line.qty_purchase,
- 'qty_available_store': product.qty_available_bandengan,
- 'suggest': product._get_po_suggest(line.qty_purchase),
- 'product_uom_qty': line.qty_purchase,
- 'price_unit': line.last_price,
- 'ending_price': line.last_price,
- 'taxes_id': [line.taxes_id.id] if line.taxes_id else None,
- 'so_line_id': sales_match[0].sale_line_id.id if sales_match else None,
- 'so_id': sales_match[0].sale_id.id if sales_match else None
- }
- new_po_line = self.env['purchase.order.line'].create([param_line])
- line.current_po_id = new_po.id
- line.current_po_line_id = new_po_line.id
-
- self.create_purchase_order_sales_match(new_po)
-
- # Buat PO untuk special brand
- if vendor_id == 23:
- create_po_for_domain(special_brand_domain, special_payment_term=True)
-
- # Buat PO untuk regular domain
- create_po_for_domain(regular_domain, "")
def update_purchase_price_so_line(self, apo):