summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/automatic_purchase.py
diff options
context:
space:
mode:
authorit-fixcomart <it@fixcomart.co.id>2025-05-27 10:19:09 +0700
committerit-fixcomart <it@fixcomart.co.id>2025-05-27 10:19:09 +0700
commitf0f414383b3bd34e6fce12e68e171014c08d2a55 (patch)
treef9eef4c1331f6507fadc680bdd801656ff9f8ea7 /indoteknik_custom/models/automatic_purchase.py
parent431229f2a6f1203fbdfe470229e55da8ebd3ea01 (diff)
parentd3f530b94569059106164172485aaa9665e80709 (diff)
Merge branch 'odoo-backup' into CR/repeat-order
Diffstat (limited to 'indoteknik_custom/models/automatic_purchase.py')
-rw-r--r--indoteknik_custom/models/automatic_purchase.py99
1 files changed, 89 insertions, 10 deletions
diff --git a/indoteknik_custom/models/automatic_purchase.py b/indoteknik_custom/models/automatic_purchase.py
index fbdf8dae..c9edf07c 100644
--- a/indoteknik_custom/models/automatic_purchase.py
+++ b/indoteknik_custom/models/automatic_purchase.py
@@ -1,4 +1,4 @@
-from odoo import models, api, fields
+from odoo import models, api, fields, tools
from odoo.exceptions import UserError
from datetime import datetime
import logging, math
@@ -67,6 +67,15 @@ class AutomaticPurchase(models.Model):
if count > 0:
raise UserError('Ada sekitar %s SO Yang sudah create PO, berikut SO nya: %s' % (count, ', '.join(names)))
+
+ def unlink_note_pj(self):
+ product = self.purchase_lines.mapped('product_id')
+ pj_state = self.env['purchasing.job.state'].search([
+ ('purchasing_job_id', 'in', product.ids)
+ ])
+
+ for line in pj_state:
+ line.unlink()
def create_po_from_automatic_purchase(self):
if not self.purchase_lines:
@@ -75,6 +84,7 @@ class AutomaticPurchase(models.Model):
raise UserError('Sudah pernah di create PO')
current_time = datetime.now()
+ self.unlink_note_pj()
vendor_ids = self.env['automatic.purchase.line'].read_group(
[('automatic_purchase_id', '=', self.id), ('partner_id', '!=', False)],
fields=['partner_id'],
@@ -284,7 +294,7 @@ class AutomaticPurchase(models.Model):
def create_purchase_order_sales_match(self, purchase_order):
matches_so_product_ids = [line.product_id.id for line in purchase_order.order_line]
- matches_so = self.env['automatic.purchase.sales.match'].search([
+ matches_so = self.env['v.sale.notin.matchpo'].search([
('automatic_purchase_id', '=', self.id),
('sale_line_id.product_id', 'in', matches_so_product_ids),
])
@@ -292,6 +302,8 @@ class AutomaticPurchase(models.Model):
sale_ids_set = set()
sale_ids_name = set()
for sale_order in matches_so:
+ # @stephan skip so line yang sudah pernah ada di purchase order sales match sebelumnya
+
salesperson_name = sale_order.sale_id.user_id.name
sale_id_with_salesperson = f"{sale_order.sale_id.name} - {salesperson_name}"
@@ -474,7 +486,7 @@ class AutomaticPurchase(models.Model):
# _logger.info('test %s' % point.product_id.name)
if point.product_id.qty_available_bandengan > point.product_min_qty:
continue
- qty_purchase = point.product_max_qty - point.product_id.qty_available_bandengan
+ qty_purchase = point.product_max_qty - point.product_id.qty_incoming_bandengan - point.product_id.qty_onhand_bandengan
po_line = self.env['purchase.order.line'].search([('product_id', '=', point.product_id.id), ('order_id.state', '=', 'done')], order='id desc', limit=1)
if self.vendor_id:
@@ -577,18 +589,18 @@ class AutomaticPurchaseLine(models.Model):
def _get_valid_purchase_price(self, purchase_price):
price = 0
- taxes = ''
+ taxes = 24
human_last_update = purchase_price.human_last_update or datetime.min
system_last_update = purchase_price.system_last_update or datetime.min
- if purchase_price.taxes_product_id.type_tax_use == 'purchase':
- price = purchase_price.product_price
- taxes = purchase_price.taxes_product_id.id
+ #if purchase_price.taxes_product_id.type_tax_use == 'purchase':
+ price = purchase_price.product_price
+ taxes = purchase_price.taxes_product_id.id or 24
if system_last_update > human_last_update:
- if purchase_price.taxes_system_id.type_tax_use == 'purchase':
- price = purchase_price.system_price
- taxes = purchase_price.taxes_system_id.id
+ #if purchase_price.taxes_system_id.type_tax_use == 'purchase':
+ price = purchase_price.system_price
+ taxes = purchase_price.taxes_system_id.id or 24
return price, taxes
@@ -655,3 +667,70 @@ class SyncPurchasingJob(models.Model):
outgoing = fields.Float(string="Outgoing")
action = fields.Char(string="Status")
date = fields.Datetime(string="Date Sync")
+
+
+class SaleNotInMatchPO(models.Model):
+ # created by @stephan for speed up performance while create po from automatic purchase
+ _name = 'v.sale.notin.matchpo'
+ _auto = False
+ _rec_name = 'id'
+
+ id = fields.Integer()
+ automatic_purchase_id = fields.Many2one('automatic.purchase', string='APO')
+ automatic_purchase_line_id = fields.Many2one('automatic.purchase.line', string='APO Line')
+ sale_id = fields.Many2one('sale.order', string='Sale')
+ sale_line_id = fields.Many2one('sale.order.line', string='Sale Line')
+ picking_id = fields.Many2one('stock.picking', string='Picking')
+ move_id = fields.Many2one('stock.move', string='Move')
+ partner_id = fields.Many2one('res.partner', string='Partner')
+ partner_invoice_id = fields.Many2one('res.partner', string='Partner Invoice')
+ salesperson_id = fields.Many2one('res.user', string='Salesperson')
+ product_id = fields.Many2one('product.product', string='Product')
+ qty_so = fields.Float(string='Qty SO')
+ qty_po = fields.Float(string='Qty PO')
+ create_uid = fields.Many2one('res.user', string='Created By')
+ create_date = fields.Datetime(string='Create Date')
+ write_uid = fields.Many2one('res.user', string='Updated By')
+ write_date = fields.Many2one(string='Updated')
+ purchase_price = fields.Many2one(string='Purchase Price')
+ purchase_tax_id = fields.Many2one('account.tax', string='Purchase Tax')
+ note_procurement = fields.Many2one(string='Note Procurement')
+
+ # 1. yang bug
+ # def init(self):
+ # tools.drop_view_if_exists(self.env.cr, self._table)
+ # self.env.cr.execute("""
+ # CREATE OR REPLACE VIEW %s AS(
+ # select apsm.id, apsm.automatic_purchase_id, apsm.automatic_purchase_line_id, apsm.sale_id, apsm.sale_line_id,
+ # apsm.picking_id, apsm.move_id, apsm.partner_id,
+ # apsm.partner_invoice_id, apsm.salesperson_id, apsm.product_id, apsm.qty_so, apsm.qty_po, apsm.create_uid,
+ # apsm.create_date, apsm.write_uid, apsm.write_date, apsm.purchase_price,
+ # apsm.purchase_tax_id, apsm.note_procurement
+ # from automatic_purchase_sales_match apsm
+ # where apsm.sale_line_id not in (
+ # select distinct coalesce(posm.sale_line_id,0)
+ # from purchase_order_sales_match posm
+ # join purchase_order po on po.id = posm.purchase_order_id
+ # where po.state not in ('cancel')
+ # )
+ # )
+ # """ % self._table)
+
+ def init(self):
+ tools.drop_view_if_exists(self.env.cr, self._table)
+ self.env.cr.execute("""
+ CREATE OR REPLACE VIEW %s AS(
+ select apsm.id, apsm.automatic_purchase_id, apsm.automatic_purchase_line_id, apsm.sale_id, apsm.sale_line_id,
+ apsm.picking_id, apsm.move_id, apsm.partner_id,
+ apsm.partner_invoice_id, apsm.salesperson_id, apsm.product_id, apsm.qty_so, apsm.qty_po, apsm.create_uid,
+ apsm.create_date, apsm.write_uid, apsm.write_date, apsm.purchase_price,
+ apsm.purchase_tax_id, apsm.note_procurement
+ from automatic_purchase_sales_match apsm
+ where apsm.sale_line_id not in (
+ select distinct coalesce(posm.sale_line_id,0)
+ from purchase_order_sales_match posm
+ join purchase_order po on po.id = posm.purchase_order_id
+ where po.state not in ('cancel')
+ )
+ )
+ """ % self._table) \ No newline at end of file