บทที่ 4 / 8

ฟังก์ชันหลัก read.main.table()

หัวใจของโปรแกรม — การอ่านและประมวลผลข้อมูล

4 / 8
1

การเริ่มต้นและ Query หลัก

ฟังก์ชัน read.main.table() เป็นฟังก์ชันหลักที่อ่านข้อมูลจากฐานข้อมูลและส่งไปยังรายงาน

ฟังก์ชัน read.main.table() — ส่วนเริ่มต้น
function extern read.main.table()
{
    domain  tcmcs.str9   temp.coa
    domain  tcyesno      first
    long    i
    long    detail.per.page
    
    detail.per.page = 20      | Detail line per page

    initial.label()
    
    rpt.seq = 0
    row.count = 0
    first = tcyesno.yes
    rpt.show.dt = 1
    out.off.spec = 0
    first.time = true 
    first.time.2 = true 
    
    select  sccoa003.*
    from    sccoa003
    where   sccoa003.coan >= {:coan.f} and sccoa003.coan <= {:coan.t}
        and sccoa003.ofbp >= {:bpid.f} and sccoa003.ofbp <= {:bpid.t}
    order by sccoa003.coan
    selectdo
        ...
    endselect
}
คำอธิบาย

ขั้นตอนสำคัญ:

  1. detail.per.page = 20 — กำหนดจำนวนบรรทัดรายละเอียดต่อหน้ารายงาน (เคยเป็น 25 → 23 → 20)
  2. initial.label() — เรียกฟังก์ชันตั้งค่า Label ที่จะแสดงบนรายงาน
  3. SELECT ... FROM sccoa003 — ดึงข้อมูล COA Header ตามช่วง COA Number และ Business Partner ที่ผู้ใช้กำหนด
Syntax สำคัญ

{:variable} — เป็น Host Variable ใน 4GL SQL ใช้อ้างตัวแปรของโปรแกรมภายใน SQL Statement

2

การแยกรายงานตามประเทศ (Local vs Export)

โปรแกรมแยกรูปแบบรายงานตามประเทศของลูกค้า

การแยกรายงาน Local / Export
    select  tccom100.*
    from    tccom100
    where   tccom100.bpid = {:sccoa003.stbp}
    selectdo
    endselect
    
    select  tccom130.*
    from    tccom130
    where   tccom130.cadr = {:tccom100.cadr}
    selectdo
    endselect
    
    if tccom130.ccty = "THA" then
        coa.type = 1  |Local
        if first.time = true then
            rpt = brp.open("rsccoa060301000", "S", 0)
            first.time = false
        endif
    else
        coa.type = 2  |Export
        if first.time.2 = true then
            rpt2 = brp.open("rsccoa060401000", "S", 0)
            first.time.2 = false
        endif
    endif
คำอธิบาย

Logic การแยกรายงาน:

  1. ค้นหา Business Partner จาก tccom100
  2. ค้นหา Address จาก tccom130
  3. ตรวจ Country code (ccty):
    • "THA" → ใช้รายงาน Local (rsccoa060301000)
    • อื่นๆ → ใช้รายงาน Export (rsccoa060401000)
  4. brp.open() — เปิดรายงาน BRP (Business Report Painter)

first.time ป้องกันการเปิดรายงานซ้ำ — เปิดครั้งแรกครั้งเดียวพอ

3

การตรวจสอบ Out-of-Specification

ก่อนพิมพ์ COA โปรแกรมจะตรวจว่าสินค้าผ่าน QC หรือไม่

การตรวจสอบ Out-of-Specification
    |Check out of specification
    if sccoa003.tbvr = tcyesno.no then
        | ... ดำเนินการพิมพ์รายงาน ...
    else
        message("COA out of specification, please contact QA.")
    endif
คำอธิบาย

sccoa003.tbvr คือตัวบ่งชี้ว่าสินค้าผ่านการตรวจสอบคุณภาพหรือไม่:

  • tcyesno.noผ่าน (ไม่มีปัญหา) → ดำเนินการพิมพ์
  • ค่าอื่น → ไม่ผ่าน → แสดงข้อความเตือน

นี่เป็นจุดควบคุมคุณภาพที่สำคัญ — ป้องกันไม่ให้พิมพ์ COA สำหรับสินค้าที่ไม่ผ่านมาตรฐาน