ยกตัวอย่าง Code ที่ผมเขียนใน Sql ดังนี้
โค้ด: เลือกทั้งหมด
CREATE OR REPLACE FUNCTION barcode(so integer)
RETURNS void AS
$BODY$
DECLARE
tqty float;
count float;
so_line integer;
item integer;
cur_sale CURSOR FOR select id,product_uom_qty from sale_order_line where order_id=so order by id;
BEGIN
DELETE FROM mrp_sticker_barcode;
OPEN cur_sale;
item = 1;
LOOP
FETCH NEXT FROM cur_sale into so_line,tqty;
EXIT WHEN NOT FOUND;
count = 1;
loop
insert into mrp_sticker_barcode VALUES(so_line,count,tqty,item);
count = count + 1;
exit when count > tqty;
end loop;
item = item+1;
END LOOP;
CLOSE cur_sale;
END
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION barcode(integer)
OWNER TO openpg;ซึ่งอ่านตาม Code ผมได้ดังนี้
เรียก cursor ไปที่ Table sale_order_line โดย where ที่ order_id (รับค่าจาก ตัวแปล) สมมุติว่าได้ค่า
sale_line_id /กะ / qty
1 / 3
2 / 4
code ผมจะ วน loop ออกมาได้ดังนี้
sale_line_id / qty / total_qty
1 / 1 / 3
1 / 2 / 3
1 / 3 / 3
2 / 1 / 4
2 / 2 / 4
2 / 3 / 4
2 / 4 / 4
เป้าหมายของ code นี้คือ จะเอาไปทำ barcode report เวลา Print จะได้จำนวน barcode = จำนวน qty ที่สั่ง และ run serial ด้วยว่าเป็น แผ่นที่เท่าไร ของจำนวนเต็ม
รบกวนผู้เชี่ยวชาญ python ด้วยครับ
Log in with LINE

