採購到入庫所經歷的表

From (http://www.cnblogs.com/benio/archive/2010/12/31/1923321.html)


--採購到入庫所經歷的表

--0.請購單

--創建請購單方式有

--a.從外掛系統導入請購的接口表PO_REQUISITIONS_INTERFACE_ALL,並允許請求(名稱:導入申請)

select *

  from po_requisitions_interface_all

 where interface_source_code = 'TEST KHJ';

--b.在系統中創建請購單(路徑:PO/申請/申請)

--請購單頭信息

select prh.requisition_header_id,
       prh.authorization_status --未審批時爲INCOMPLETE,審批完後爲

  from po_requisition_headers_all prh

 where prh.segment1 = '600000'
      
   and prh.type_lookup_code = 'PURCHASE';

--請購單行信息

select prl.requisition_line_id,
       prl.*

  from po_requisition_lines_all prl

 where prl.requisition_header_id in
      
       (select prh.requisition_header_id
        
          from po_requisition_headers_all prh
        
         where prh.segment1 = '600000'
              
           and prh.type_lookup_code = 'PURCHASE');

--請購單分配行

select *

  from po_req_distributions_all prda

 where prda.requisition_line_id in
      
       (select prl.requisition_line_id
        
          from po_requisition_lines_all prl
        
         where prl.requisition_header_id in
              
               (select prh.requisition_header_id
                
                  from po_requisition_headers_all prh
                
                 where prh.segment1 = '600000'
                      
                   and prh.type_lookup_code = 'PURCHASE'));

--1.採購訂單的創建(路徑:PO/採購訂單/採購訂單)

--po_headers_all 採購訂單頭表

select pha.po_header_id,
       
       pha.segment1,
       
       pha.agent_id,
       
       pha.type_lookup_code, --標準採購單爲STANDARD,一攬子協議爲BLANKET
       
       decode(pha.approved_flag,
              
              'R',
              
              pha.approved_flag,
              
              nvl(pha.authorization_status, 'INCOMPLETE')), --審批,未審批時爲INCOMPLETE,審批後爲APPROVED
       
       po_headers_sv3.get_po_status(pha.po_header_id) --剛下完採購單,未審批時,po狀態爲未完成,審批後,狀態爲批准

  from po_headers_all pha

 where segment1 = 300446; --採購單號碼

--po_lines_all 採購訂單行表

select pla.po_line_id,
       pla.line_type_id

  from po_lines_all pla

 where po_header_id =
      
       (select po_header_id from po_headers_all where segment1 = 300446);

/*

取已審批銷售訂單頭和行的數據:

涉及表: Po_headers_all,Po_lines_all

邏輯如下:

限制頭表的如下屬性,並通過Po_header_id把頭、行表關聯起來

APPROVED_FLAG=Y

*/

--po_line_locations_all 採購訂單行的發送表(路徑:PO/採購訂單/採購訂單/發運(T))

--po_line_id=po_lines_all.po_line_id

--當點擊發運按鈕時,系統會自動創建第一行發運行,可根據需要手工創建新的發運行

--(例如同一採購訂單行的物料可能會發往不同的地點,此表記錄物料發送情況)

--下面爲取訂單與其發運的關係(可能存在多次發運)

select *

  from po_line_locations_all plla

 where plla.po_line_id =
      
       (select pla.po_line_id
        
          from po_lines_all pla
        
         where po_header_id = (select po_header_id
                               
                                 from po_headers_all
                               
                                where segment1 = 300446));

--或者

select *

  from po_line_locations_all plla

 where plla.po_header_id =
      
       (select po_header_id from po_headers_all where segment1 = 300446);

--4、po_distributions_all 採購訂單發送行的分配表(路徑:PO/採購訂單/採購訂單/發運(T)/分配(T))

--line_location_id=po_line_location_all.line_location_id

--發往同一地點的物料也可能放在不同的子庫存,此表記錄物料分配情況

select *

  from po_distributions_all pda

 where pda.line_location_id in
      
       (select plla.line_location_id
        
          from po_line_locations_all plla
        
         where plla.po_line_id =
              
               (select pla.po_line_id
                
                  from po_lines_all pla
                
                 where po_header_id =
                      
                       (select po_header_id
                        
                          from po_headers_all
                        
                         where segment1 = 300446)));

--或者

select *

  from po_distributions_all

 where po_header_id =
      
       (select po_header_id from po_headers_all where segment1 = 300446);

--或者

select *

  from po_distributions_all pda

 where pda.po_line_id =
      
       (select pla.po_line_id
        
          from po_lines_all pla
        
         where po_header_id = (select po_header_id
                               
                                 from po_headers_all
                               
                                where segment1 = 300446));

--對於po_distribution_all 表而言,如果其SOURCE_DISTRIBUTION_ID 有值, 其對應於計劃採購單發放

/*以上各表從上到下是一對多關係的 */

--po_releases_all 訂單發放

--該表包含一攬子協議以及計劃採購單的release,對於每一張發放的一攬子協議或者計劃採購單都有相關行與之對應

--其包含採購員,日期,釋放狀態,釋放號碼,每一個釋放行都有至少一條的採購單的發運信息與之對應(PO_LINE_LOCATIONS_ALL).

--每做一次Realese,PO_distributions_all就會新增一條記錄。這是計劃訂單的特性。

-- 

select * from po_releases_all where po_header_id = &po_header_id;

--接收(路徑:INV/事務處理/接收/接收)

--1.rcv_shipment_headers 接收發送頭表

--記錄採購訂單的接收情況的頭表

select *

  from rcv_shipment_headers rsh

 where rsh.shipment_header_id in
      
       (select shipment_header_id
        
          from rcv_shipment_lines
        
         where po_header_id = 4105);

--2.rcv_shipment_lines 接收發送行表

--記錄採購訂單的發送的行的接收情況

select * from rcv_shipment_lines where po_header_id = 4105;

--3.rcv_transactions 接收事務處理表

--記錄採購訂單的發送行的RECEIVE的信息

select rt.transaction_id,
       
       rt.transaction_type,
       
       rt.destination_type_code,
       
       rt.*

  from rcv_transactions rt

 where rt.interface_source_code = 'RCV'
      
   and rt.source_document_code = 'PO'
      
   and (rt.po_header_id = (select pha.po_header_id
                           
                             from po_headers_all pha
                           
                            where segment1 = 300446) or
       
       rt.po_line_id in
       
       (select pla.po_line_id
         
           from po_lines_all pla
         
          where po_header_id = (select po_header_id
                                
                                  from po_headers_all
                                
                                 where segment1 = 300446)) or
       
       rt.shipment_header_id =
       
       (select rsh.shipment_header_id
         
           from rcv_shipment_headers rsh
         
          where shipment_header_id in
               
                (select shipment_header_id
                 
                   from rcv_shipment_lines
                 
                  where po_header_id = 4105)) or
       
       rt.shipment_line_id in
       
       (select shipment_line_id
         
           from rcv_shipment_lines
         
          where po_header_id = 4105));

--4.rcv_receiving_sub_ledger 暫記應付表

--記錄採購訂單接收後,產生的暫記應付信息(接收事務處理產生的分配行)

--產生分錄的程序: RCV_SeedEvents_PVT=>RCV_CreateAccounting_PVT

/* po_line_locations.accrue_on_receipt_flag 控制是否產生分錄*/


select nvl(poll.accrue_on_receipt_flag, 'N')

  into l_accrue_on_receipt_flag

  from po_line_locations poll

 where poll.line_location_id = p_rcv_events_tbl(l_ctr_first).po_line_location_id;

IF ((l_accrue_on_receipt_flag = 'Y' OR

p_rcv_events_tbl(i).procurement_org_flag = 'N') AND

p_rcv_events_tbl(i).event_type_id NOT IN

(RCV_SeedEvents_PVT.INTERCOMPANY_INVOICE,RCV_SeedEvents_PVT.INTERCOMPANY_REVERSAL)) THEN

l_stmt_num := 50

IF G_DEBUG = 'Y' AND FND_LOG.LEVEL_EVENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN

FND_LOG.string(FND_LOG.LEVEL_EVENT,G_LOG_HEAD'.'l_api_name'.'l_stmt_num

,'Creating accounting entries in RRS')

END IF

 

IF G_DEBUG = 'Y' AND FND_LOG.LEVEL_STATEMENT >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN

FND_LOG.string(FND_LOG.LEVEL_STATEMENT,G_LOG_HEAD'.'l_api_name'.'l_stmt_num

,'Creating accounting entries for accounting_event_id : 'l_accounting_event_id)

END IF

 

-- Call Account generation API to create accounting entries

RCV_CreateAccounting_PVT.Create_AccountingEntry(

p_api_version => 1.0,

x_return_status => l_return_status,

x_msg_count => l_msg_count,

x_msg_data => l_msg_data,

p_accounting_event_id => l_accounting_event_id,

/* Support for Landed Cost Management */

p_lcm_flag => p_lcm_flag)

IF l_return_status <> FND_API.g_ret_sts_success THEN

l_api_message := 'Error in Create_AccountingEntry API'

IF G_DEBUG = 'Y' AND FND_LOG.LEVEL_UNEXPECTED >= FND_LOG.G_CURRENT_RUNTIME_LEVEL THEN

FND_LOG.string(FND_LOG.LEVEL_UNEXPECTED,G_LOG_HEAD '.'l_api_namel_stmt_num

,'Insert_RAEEvents : 'l_stmt_num' : 'l_api_message)

END IF

RAISE FND_API.g_exc_unexpected_error

END IF

select *

  from rcv_receiving_sub_ledger

 where rcv_transaction_id in
      
       (select transaction_id
        
          from rcv_transactions
        
         where po_header_id = 4105);

--接受(路徑:INV/事務處理/接收/接收事務處理)

--接收事務處理:接收之後,其實現在還並沒有入庫。

--rcv_transactions 接收事務處理表

--記錄採購訂單的發送行的ACCEPT的信息

select rt.transaction_id,
       
       rt.transaction_type,
       
       rt.destination_type_code,
       
       rt.*

  from rcv_transactions rt

 where rt.interface_source_code = 'RCV' --做接收的條件
      
   and rt.source_document_code = 'PO' --做接收的條件
      
   and rt.transaction_type = 'RECEIVE' --做接收的條件
      
   and rt.destination_type_code = 'RECEIVE' --做接收的條件
      
   and (rt.po_header_id = (select pha.po_header_id
                           
                             from po_headers_all pha
                           
                            where segment1 = 300446) or
       
       rt.po_line_id in
       
       (select pla.po_line_id
         
           from po_lines_all pla
         
          where po_header_id = (select po_header_id
                                
                                  from po_headers_all
                                
                                 where segment1 = 300446)) or
       
       rt.shipment_header_id =
       
       (select rsh.shipment_header_id
         
           from rcv_shipment_headers rsh
         
          where shipment_header_id in
               
                (select shipment_header_id
                 
                   from rcv_shipment_lines
                 
                  where po_header_id = 4105)) or
       
       rt.shipment_line_id in
       
       (select shipment_line_id
         
           from rcv_shipment_lines
         
          where po_header_id = 4105));

-- 入庫

--因爲涉及入庫操作,所以,在庫存事務處理表中會留下相應的記錄。

--即在Mtl_material_transactions表中,會存在相應的兩條入庫記錄。

select mmt.*

  from mtl_material_transactions mmt

 where mmt.transaction_type_id = 18 --po接收
      
   and mmt.transaction_action_id = 27 --接收至庫存
      
   and mmt.transaction_source_type_id = 1 --採購訂單
      
   and (mmt.transaction_source_id = 4105 --po_header_id
       
       or mmt.rcv_transaction_id in
       
       (select rt.transaction_id
            
              from rcv_transactions rt
            
             where rt.interface_source_code = 'RCV'
                  
               and rt.source_document_code = 'PO'
                  
               and (rt.po_header_id =
                   
                   (select pha.po_header_id
                     
                       from po_headers_all pha
                     
                      where segment1 = 300446))));

--此時,rcv_transactions的狀態變爲

select rt.transaction_id,
       
       rt.transaction_type,
       
       rt.destination_type_code,
       
       rt.*

  from rcv_transactions rt

 where rt.interface_source_code = 'RCV' --做入庫的條件
      
   and rt.source_document_code = 'PO' --做入庫的條件
      
   and rt.transaction_type = 'DELIVER' --做入庫的條件
      
   and rt.destination_type_code = 'INVENTORY' --做入庫的條件
      
   and (rt.po_header_id = (select pha.po_header_id
                           
                             from po_headers_all pha
                           
                            where segment1 = 300446) or
       
       rt.po_line_id in
       
       (select pla.po_line_id
         
           from po_lines_all pla
         
          where po_header_id = (select po_header_id
                                
                                  from po_headers_all
                                
                                 where segment1 = 300446)) or
       
       rt.shipment_header_id =
       
       (select rsh.shipment_header_id
         
           from rcv_shipment_headers rsh
         
          where shipment_header_id in
               
                (select shipment_header_id
                 
                   from rcv_shipment_lines
                 
                  where po_header_id = 4105)) or
       
       rt.shipment_line_id in
       
       (select shipment_line_id
         
           from rcv_shipment_lines
         
          where po_header_id = 4105));

--退貨

--說明:

--退貨至接收時,產生一條記錄,退貨至供應商時,產生兩條數據。 可見退貨的實際順序爲: 庫存----> 接收----> 供應商

--不管是退貨至接收還是退貨至供應商,在事務處理中,都會產生兩條記錄。

--而且,數量符號與接收的數據正好相反。而且產生的記錄都是RETURN TO RECEIVING。

--1.庫存退貨至接受

select rt.destination_type_code,
       rt.interface_source_code,
       rt.*

  from rcv_transactions rt

 where rt.interface_source_code is null
      
   and rt.transaction_type = 'RETURN TO RECEIVING' --退貨至接受
      
   and rt.source_document_code = 'PO'
      
   and rt.destination_type_code = 'RECEIVING'
      
   and po_header_id = 4105
      
   and po_line_id = 9938;

select mmt.*

  from mtl_material_transactions mmt

 where mmt.transaction_source_id = 4105
      
   and mmt.transaction_type_id = 36
      
   and mmt.transaction_action_id = 1
      
   and mmt.transaction_source_type_id = 1;

--2.庫存退貨至供應商(產生兩條數據。順序爲: 庫存----> 接收----> 供應商)

--a.庫存退貨至接收

select rt.destination_type_code,
       rt.interface_source_code,
       rt.*

  from rcv_transactions rt

 where rt.interface_source_code is null
      
   and rt.transaction_type = 'RETURN TO RECEIVING' --先退貨至接收
      
   and rt.source_document_code = 'PO'
      
   and rt.destination_type_code = 'INVENTORY'
      
   and po_header_id = 4105;

--b.接收退貨至供應商

select rt.destination_type_code,
       rt.interface_source_code,
       rt.*

  from rcv_transactions rt

 where rt.interface_source_code is null
      
   and rt.transaction_type = 'RETURN TO VENDOR' --退貨至供應商
      
   and rt.source_document_code = 'PO'
      
   and rt.destination_type_code = 'RECEIVING'
      
   and po_header_id = 4105;

select mmt.*

  from mtl_material_transactions mmt

 where mmt.transaction_source_id = 4105
      
   and mmt.transaction_type_id = 36 --向供應商退貨
      
   and mmt.transaction_action_id = 1 --從庫存發放
      
   and mmt.transaction_source_type_id = 1; --採購訂單


發佈了1 篇原創文章 · 獲贊 3 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章