报表-未扫码查询

CREATE OR REPLACE PACKAGE BODY unscanned_barcode_biz IS

  PROCEDURE get_data(p_inv_org_id     IN NUMBER -- 组织ID
                    ,p_date_begin     IN DATE -- 开始日期 (必选)
                    ,p_date_end       IN DATE -- 结束日期 (必选)
                    ,p_mo_code        IN VARCHAR2 --作业名称
                    ,p_workshop_id    IN VARCHAR2 -- 车间
                    ,p_line_id        IN VARCHAR2 -- 产线
                    ,p_material_desc  IN VARCHAR2 --装配件描述
                    ,p_mo_status      IN VARCHAR2 --作业状态
                    ,p_mo_status_code IN bas_types.t_varray_string32 --工单状态 (可选、多选)
                    ,p_page_count     IN NUMBER -- 分页每页大小
                    ,p_page_num       IN NUMBER -- 第几页
                    ,x_page_total     OUT NUMBER -- 总记录条数
                    ,x_ret_code       OUT VARCHAR2
                    ,x_ret_msg        OUT VARCHAR2
                    ,x_ret_data       OUT resultcursor) IS
    v_date_mod             NUMBER; --日期条件
    v_mostatus             NUMBER := 1;
    v_mo_status_code_array t_varray := NEW t_varray();
    v_mo_status_code_count NUMBER := 0;
    v_start                NUMBER := 0;
    v_end                  NUMBER := 0;
    v_plandate_begin       DATE := p_date_begin;
    v_plandate_end         DATE := p_date_end;
  BEGIN
    IF p_page_count > 0
       AND p_page_num > 0
    THEN
      v_start := p_page_count * (p_page_num - 1);
      v_end   := p_page_count * p_page_num;
    END IF;
  
    
    IF upper(p_mo_status) = 'OFFLINE'
    THEN
      v_mostatus := 2;
    ELSIF upper(p_mo_status) = 'ONLINE'
    THEN
      v_mostatus := 3;
    ELSE
      v_mostatus := 1;
    END IF;
    --时间条件
    IF p_date_begin IS NULL
    THEN
      IF p_date_end IS NULL
      THEN
        v_date_mod := 1;
      ELSE
        v_date_mod := 3; --只有结束时间
      END IF;
    ELSE
      IF p_date_end IS NULL
      THEN
        --只有开始时间
        v_date_mod := 2;
      ELSE
        v_date_mod := 4;
      END IF;
    END IF;
    --工单状态
    FOR k IN 1 .. p_mo_status_code.count
    LOOP
      IF p_mo_status_code(k) IS NOT NULL
      THEN
        v_mo_status_code_count := v_mo_status_code_count + 1;
        v_mo_status_code_array.extend(1);
        v_mo_status_code_array(k) := p_mo_status_code(k);
      END IF;
    END LOOP;
  
    --日期
    IF v_date_mod >= 3
    THEN
      v_plandate_end := to_date(to_char(trunc(p_date_end)
                                       ,'yyyy-MM-dd ') || '23:59:59'
                               ,'yyyy-MM-dd HH24:mi:ss');
      IF v_date_mod = 4
      THEN
        v_plandate_begin := to_date(to_char(trunc(p_date_begin)
                                           ,'yyyy-MM-dd ') || '00:00:00'
                                   ,'yyyy-MM-dd HH24:mi:ss');
      END IF;
    END IF;
  
    -----------------------------------------------------------------------------------------------------
  
    --获取总记录数
    SELECT COUNT(*)
      INTO x_page_total
      FROM (SELECT 1
              FROM sfc_mo mo
             INNER JOIN sfc_barcode bc
                ON (CASE
                     WHEN p_mo_code IS NULL THEN
                      1
                     WHEN p_mo_code IS NOT NULL
                          AND mo.mo_name LIKE '%' || p_mo_code || '%' THEN
                      1
                     ELSE
                      0
                   END) = 1 --作业号                 
               AND (CASE
                     WHEN v_date_mod = 1 THEN
                      1
                     WHEN v_date_mod = 2
                          AND mo.datetime_sche_start > p_date_begin THEN
                      1
                     WHEN v_date_mod = 3
                          AND mo.datetime_sche_start < v_plandate_end
                     /*to_date(to_char(trunc(p_date_end)
                             ,'yyyy-MM-dd ') || '23:59:59'
                     ,'yyyy-MM-dd HH24:mi:ss')*/
                      THEN
                      1
                     WHEN v_date_mod = 4
                          AND mo.datetime_sche_start BETWEEN v_plandate_begin AND v_plandate_end
                     /* to_date(to_char(trunc(p_date_begin)
                                        ,'yyyy-MM-dd ') || '00:00:00'
                                ,'yyyy-MM-dd HH24:mi:ss') AND
                     to_date(to_char(trunc(p_date_end)
                                    ,'yyyy-MM-dd ') || '23:59:59'
                            ,'yyyy-MM-dd HH24:mi:ss')*/
                      THEN
                      1
                     ELSE
                      0
                   END) = 1 --作业开始时间
                  --AND mo.mo_status_code = p_mo_status_code --工单状态
               AND (CASE
                     WHEN v_mo_status_code_count <= 0 THEN
                      1
                     WHEN v_mo_status_code_count > 0
                         /*AND mo.mo_status_code IN
                         (SELECT column_value FROM TABLE(v_mo_status_code_array)) THEN*/
                          AND EXISTS (SELECT 1
                             FROM TABLE(v_mo_status_code_array)
                            WHERE mo.mo_status_code = column_value) THEN
                      1
                     ELSE
                      0
                   END) = 1
               AND mo.inv_org_id = p_inv_org_id
               AND mo.id = bc.mo_id
               AND mo.state = 'A'
                  /*AND mo.id IS NOT NULL
                  AND bc.id IS NOT NULL*/
               AND bc.barcode_state <> 'SCRAP'
             INNER JOIN sys_org ws
                ON (CASE
                     WHEN p_workshop_id IS NULL THEN
                      1
                     WHEN p_workshop_id IS NOT NULL
                          AND (ws.id = p_workshop_id OR ws.org_name = p_workshop_id) THEN
                      1
                     ELSE
                      0
                   END) = 1 --车间
               AND ws.id = mo.workshop_id
               AND ws.inv_org_id = mo.inv_org_id
               AND ws.state = 'A'
             INNER JOIN sys_org line
                ON (CASE
                     WHEN p_line_id IS NULL THEN
                      1
                     WHEN p_line_id IS NOT NULL
                          AND (line.id = p_line_id OR line.org_name = p_line_id) THEN
                      1
                     ELSE
                      0
                   END) = 1 --产线
               AND line.id = mo.line_id
               AND line.inv_org_id = mo.inv_org_id
               AND line.state = 'A'
             INNER JOIN sfc_mitem m
                ON m.id = mo.mitem_id
               AND m.inv_org_id = mo.inv_org_id
               AND m.state = 'A'
               AND (CASE
                     WHEN p_material_desc IS NULL THEN
                      1
                     WHEN p_material_desc IS NOT NULL
                          AND (m.mitem_desc LIKE '%' || p_material_desc || '%') THEN
                      1
                     ELSE
                      0
                   END) = 1 --装配件描述 p_material_desc
              LEFT JOIN (SELECT wip.inv_org_id
                              ,wip.sn
                              ,wip.is_completed
                              ,wip.inv_id 
                              ,p.process_code
                              ,p.process_desc
                              ,nvl(wip.user_modified
                                  ,wip.user_created) maintainer
                              ,nvl(wip.datetime_modified
                                  ,wip.datetime_created) maintenance_datetime
                              ,wip.mo_id
                              ,wip.line_id
                              ,wip.workshop_id
                          FROM sfc_wip wip
                         INNER JOIN mespro.sfc_mo m
                            ON m.id = wip.mo_id
                           AND m.inv_org_id = wip.inv_org_id
                           AND m.state = wip.state
                           AND m.state = 'A'
                         INNER JOIN sfc_process p
                            ON p.inv_org_id = wip.inv_org_id
                           AND p.state = 'A'
                           AND p.id = wip.cur_process_id) w2p
                ON mo.id = w2p.mo_id
               AND w2p.inv_org_id = m.inv_org_id
               AND w2p.sn = bc.barcode
               AND line.id = w2p.line_id
               AND ws.id = w2p.workshop_id
               AND bc.state = 'A'
             WHERE (CASE
                     WHEN v_mostatus = 1 THEN
                      1
                     WHEN v_mostatus = 2
                          AND (w2p.is_completed = 'Y' AND w2p.process_code <> 'WRK' AND
                          w2p.process_code <> 'NRK') THEN
                      1 --offline
                     WHEN v_mostatus = 3
                          AND (w2p.is_completed = 'N' OR w2p.sn IS NULL) THEN
                     /*AND (w2p.is_completed = 'N' OR bc.barcode IS NULL) THEN*/
                      1 --online
                     ELSE
                      0
                   END) = 1);
  
    -----------------------------------------------------------------------------------------------------
  
    --返回结果集
    OPEN x_ret_data FOR
      SELECT *
        FROM (SELECT bc.id
                    ,rownum AS rn
                    , --行号
                     bc.inv_org_id
                    , --库存组织id          
                     mo.mo_name
                    , --工单名称
                     mo.datetime_sche_start
                    , --计划开始时间
                     m.mitem_desc
                    , --装配件描述
                     ws.org_name workshop_name
                    , --车间代码
                     line.org_name line_name
                    , --产线代码
                     bc.barcode
                    , --未扫条码
                     w2p.process_code
                    , --当前工序
                     w2p.process_desc
                    , --当前工序描述
                     w2p.is_completed
                    , --是否下线  新增与2015.10.14 by bjc.ex_sutz
                     w2p.sn
                    , --产品条码  新增与2015.10.14 by bjc.ex_sutz
                     w2p.inv_id
                    , --入库ID  新增与2015.10.14 by bjc.ex_sutz 
                     nvl(w2p.maintainer
                        ,nvl(bc.user_modified
                            ,bc.user_created)) maintainer
                    , --维护人员
                     nvl(w2p.maintenance_datetime
                        ,nvl(bc.datetime_modified
                            ,bc.datetime_created)) maintenance_datetime --维护时间
                FROM sfc_mo mo
               INNER JOIN sfc_barcode bc
                  ON (CASE
                       WHEN p_mo_code IS NULL THEN
                        1
                       WHEN p_mo_code IS NOT NULL
                            AND mo.mo_name LIKE '%' || p_mo_code || '%' THEN
                        1
                       ELSE
                        0
                     END) = 1 --作业号                 
                 AND (CASE
                       WHEN v_date_mod = 1 THEN
                        1
                       WHEN v_date_mod = 2
                            AND mo.datetime_sche_start > p_date_begin THEN
                        1
                       WHEN v_date_mod = 3
                            AND mo.datetime_sche_start < v_plandate_end
                       /*to_date(to_char(trunc(p_date_end)
                               ,'yyyy-MM-dd ') || '23:59:59'
                       ,'yyyy-MM-dd HH24:mi:ss')*/
                        THEN
                        1
                       WHEN v_date_mod = 4
                            AND mo.datetime_sche_start BETWEEN v_plandate_begin AND v_plandate_end
                       /* AND mo.datetime_sche_start BETWEEN 
                       to_date(to_char(trunc(p_date_begin)
                                          ,'yyyy-MM-dd ') || '00:00:00'
                                  ,'yyyy-MM-dd HH24:mi:ss') AND
                       to_date(to_char(trunc(p_date_end)
                                      ,'yyyy-MM-dd ') || '23:59:59'
                              ,'yyyy-MM-dd HH24:mi:ss')*/
                        THEN
                        1
                       ELSE
                        0
                     END) = 1 --作业开始时间
                    --AND mo.mo_status_code = p_mo_status_code --工单状态
                 AND (CASE
                       WHEN v_mo_status_code_count <= 0 THEN
                        1
                       WHEN v_mo_status_code_count > 0
                           /*AND mo.mo_status_code IN
                           (SELECT column_value FROM TABLE(v_mo_status_code_array)) THEN*/
                            AND EXISTS (SELECT 1
                               FROM TABLE(v_mo_status_code_array)
                              WHERE mo.mo_status_code = column_value) THEN
                        1
                       ELSE
                        0
                     END) = 1
                 AND mo.inv_org_id = p_inv_org_id
                 AND mo.id = bc.mo_id
                 AND mo.state = 'A'
                    /*AND mo.id IS NOT NULL
                    AND bc.id IS NOT NULL*/
                 AND bc.barcode_state <> 'SCRAP'
               INNER JOIN sys_org ws
                  ON (CASE
                       WHEN p_workshop_id IS NULL THEN
                        1
                       WHEN p_workshop_id IS NOT NULL
                            AND (ws.id = p_workshop_id OR ws.org_name = p_workshop_id) THEN
                        1
                       ELSE
                        0
                     END) = 1 --车间
                 AND ws.id = mo.workshop_id
                 AND ws.inv_org_id = mo.inv_org_id
                 AND ws.state = 'A'
               INNER JOIN sys_org line
                  ON (CASE
                       WHEN p_line_id IS NULL THEN
                        1
                       WHEN p_line_id IS NOT NULL
                            AND (line.id = p_line_id OR line.org_name = p_line_id) THEN
                        1
                       ELSE
                        0
                     END) = 1 --产线
                 AND line.id = mo.line_id
                 AND line.inv_org_id = mo.inv_org_id
                 AND line.state = 'A'
               INNER JOIN sfc_mitem m
                  ON m.id = mo.mitem_id
                 AND m.inv_org_id = mo.inv_org_id
                 AND m.state = 'A'
                 AND (CASE
                       WHEN p_material_desc IS NULL THEN
                        1
                       WHEN p_material_desc IS NOT NULL
                            AND (m.mitem_desc LIKE '%' || p_material_desc || '%') THEN
                        1
                       ELSE
                        0
                     END) = 1 --装配件描述 p_material_desc
                LEFT JOIN (SELECT wip.inv_org_id
                                ,wip.sn
                                ,wip.is_completed
                                ,wip.inv_id 
                                ,p.process_code
                                ,p.process_desc
                                ,nvl(wip.user_modified
                                    ,wip.user_created) maintainer
                                ,nvl(wip.datetime_modified
                                    ,wip.datetime_created) maintenance_datetime
                                ,wip.mo_id
                                ,wip.line_id
                                ,wip.workshop_id
                            FROM sfc_wip wip
                           INNER JOIN mespro.sfc_mo m
                              ON m.id = wip.mo_id
                             AND m.inv_org_id = wip.inv_org_id
                             AND m.state = wip.state
                             AND m.state = 'A'
                           INNER JOIN sfc_process p
                              ON p.inv_org_id = wip.inv_org_id
                             AND p.state = 'A'
                             AND p.id = wip.cur_process_id) w2p
                  ON mo.id = w2p.mo_id
                 AND w2p.inv_org_id = m.inv_org_id
                 AND w2p.sn = bc.barcode
                 AND line.id = w2p.line_id
                 AND ws.id = w2p.workshop_id
                 AND bc.state = 'A'
               WHERE (CASE
                       WHEN v_mostatus = 1 THEN
                        1
                       WHEN v_mostatus = 2
                            AND (w2p.is_completed = 'Y' AND w2p.process_code <> 'WRK' AND
                            w2p.process_code <> 'NRK') THEN
                        1 --offline
                       WHEN v_mostatus = 3
                            AND (w2p.is_completed = 'N' OR w2p.sn IS NULL) THEN
                       /*AND (w2p.is_completed = 'N' OR bc.barcode IS NULL) THEN*/
                        1 --online
                       ELSE
                        0
                     END) = 1)
       WHERE (
             --rn > p_page_count * (p_page_num - 1) AND rn <= p_page_count * p_page_num
              rn BETWEEN v_start AND v_end);
  
    x_ret_code := '0';
  EXCEPTION
    WHEN OTHERS THEN
      x_ret_code := SQLCODE;
      x_ret_msg  := SQLERRM;
  END;

 
  PROCEDURE main(p_inv_org_id     IN NUMBER -- 库存组织ID
                ,p_date_begin     IN DATE -- 开始日期 (必选)
                ,p_date_end       IN DATE -- 结束日期 (必选)
                ,p_mo_code        IN VARCHAR2 --作业名称
                ,p_workshop_id    IN VARCHAR2 -- 车间
                ,p_line_id        IN VARCHAR2 -- 产线
                ,p_material_desc  IN VARCHAR2 --装配件描述
                ,p_mo_status      IN VARCHAR2 --作业状态
                ,p_mo_status_code IN bas_types.t_varray_string32 --工单状态 (可选、多选)
                ,p_page_count     IN NUMBER -- 分页每页大小
                ,p_page_num       IN NUMBER -- 第几页
                ,x_page_total     OUT NUMBER -- 总记录条数
                ,x_ret_code       OUT VARCHAR2
                ,x_ret_msg        OUT VARCHAR2
                ,x_ret_data       OUT resultcursor) IS
  
  BEGIN
  
    get_data(p_inv_org_id
            ,p_date_begin
            ,p_date_end
            ,p_mo_code
            ,p_workshop_id
            ,p_line_id
            ,p_material_desc
            ,p_mo_status
            ,p_mo_status_code
            ,p_page_count
            ,p_page_num
            ,x_page_total
            ,x_ret_code
            ,x_ret_msg
            ,x_ret_data);
  
  END;

END unscanned_barcode_biz;


你可能感兴趣的:(配件)