Einkaufspreise als Einstandspreise übernehmen

Ein SQL Script kann helfen

Jamotion GmbH
Renzo Meister

Das folgende SQL Script bereinigt die Einstandspreise (Feld standard_price) auf den Produkten und übernimmt jeweils den Einkaufspreis des Standardlieferanten (gleich erster Lieferant im Produkt).

Zuerst löschen wir alle Einstandspreise mit Wert 0. Dies ist reine "Kosmetik" aber hilft, weniger unnötige Datensätze zu führen.

DELETE
FROM
product_price_history
WHERE
cost = 0;

Nun übertragen wir die Einkaufspreise in die product_price_history Tabelle zu einem fixen Datum.

INSERT INTO
product_price_history (company_id, product_id, datetime, cost, create_uid, create_date, write_uid, write_date)
SELECT
1 company_id,
id product_id,
'2019-12-31 04:00:00' datetime,
ekprice "cost",
1 create_uid,
'2019-12-31 04:00:00' create_date,
1 write_uid,
'2019-12-31 04:00:00' write_date
FROM
(SELECT
pp.id,
(SELECT price FROM product_supplierinfo ps WHERE ps.product_tmpl_id = pt.id ORDER BY ps.sequence LIMIT 1) ekprice
FROM
product_template pt
INNER JOIN product_product pp ON pt.id = pp.product_tmpl_id
LEFT JOIN product_price_history pph ON pp.id = pph.product_id
WHERE
pph.id IS NULL) sub
WHERE
ekprice > 0;

Da die Einstandspreise Unternehmensabhängig sind, werden diese zusätzlich noch in der ir_property Tabelle geführt. Diese Einträge generieren wir jetzt.

INSERT INTO
ir_property(name, res_id, company_id, fields_id, value_float, type,
create_uid, create_date, write_uid, write_date)
SELECT
'standard_price' "name",
'product.product,' || pph.product_id::text res_id,
1 company_id,
2347 fields_id,
cost value_float,
'float' "type",
1 create_uid,
'2019-12-31 04:00:00' create_date,
1 write_uid,
'2019-12-31 04:00:00' write_date
FROM
product_price_history pph
INNER JOIN (SELECT product_id, max(datetime) datetime FROM product_price_history GROUP BY product_id) sub
ON sub.product_id = pph.product_id AND sub.datetime = pph.datetime;