<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.prolog.upcloud.base.wh.operate.dao.OmsStockAdjustmentRecordMapper">
    <resultMap id="BaseResultMap" type="com.prolog.upcloud.base.wh.operate.model.OmsStockAdjustmentRecord">
        <result column="BILLID" jdbcType="VARCHAR" property="billid"/>
        <result column="BILLNO" jdbcType="VARCHAR" property="billno"/>
        <result column="BRANCHID" jdbcType="VARCHAR" property="branchid"/>
        <result column="BRANCHNAME" jdbcType="VARCHAR" property="branchname"/>
        <result column="CONSIGNORID" jdbcType="VARCHAR" property="consignorid"/>
        <result column="CONSIGNORNAME" jdbcType="VARCHAR" property="consignorname"/>
        <result column="IAR_DATE" jdbcType="VARCHAR" property="iarDate"/>
        <result column="ITEMID" jdbcType="VARCHAR" property="itemid"/>
        <result column="ITEMCODE" jdbcType="VARCHAR" property="itemcode"/>
        <result column="ITEMNAME" jdbcType="VARCHAR" property="itemname"/>
        <result column="BATCH_ID" jdbcType="VARCHAR" property="batchId"/>
        <result column="BATCH_NUM" jdbcType="VARCHAR" property="batchNum"/>
        <result column="LOT_NO" jdbcType="VARCHAR" property="lotNo"/>
        <result column="STOCK_STATUS" jdbcType="VARCHAR" property="stockStatus"/>
        <result column="VENDOR_BATCH_NUM" jdbcType="VARCHAR" property="vendorBatchNum"/>
        <result column="PRODUCTION_PLACE" jdbcType="VARCHAR" property="productionPlace"/>
        <result column="GRADE" jdbcType="VARCHAR" property="grade"/>
        <result column="PRODUCTION_DATE" jdbcType="TIMESTAMP" property="productionDate"/>
        <result column="SHELF_LIFE_DAYS" jdbcType="DECIMAL" property="shelfLifeDays"/>
        <result column="EXPIRED_DATE" jdbcType="TIMESTAMP" property="expiredDate"/>
        <result column="QUANTITY_PRE" jdbcType="DECIMAL" property="quantityPre"/>
        <result column="QUANTITY_BALANCE" jdbcType="DECIMAL" property="quantityBalance"/>
        <result column="QUANTITY" jdbcType="DECIMAL" property="quantity"/>
        <result column="ADJUST_REASON" jdbcType="VARCHAR" property="adjustReason"/>
        <result column="AUDIT_PERSON" jdbcType="VARCHAR" property="auditPerson"/>
        <result column="AUDIT_STATUS" jdbcType="VARCHAR" property="auditStatus"/>
        <result column="AUDIT_OPINION" jdbcType="VARCHAR" property="auditOpinion"/>
        <result column="AUDIT_TIME" jdbcType="TIMESTAMP" property="auditTime"/>
        <result column="STATUS" jdbcType="VARCHAR" property="status"/>
        <result column="REMARK" jdbcType="VARCHAR" property="remark"/>
        <result column="ATTR_STR1" jdbcType="VARCHAR" property="attrStr1"/>
        <result column="ATTR_STR2" jdbcType="VARCHAR" property="attrStr2"/>
        <result column="ATTR_STR3" jdbcType="VARCHAR" property="attrStr3"/>
        <result column="ATTR_STR4" jdbcType="VARCHAR" property="attrStr4"/>
        <result column="ATTR_STR5" jdbcType="VARCHAR" property="attrStr5"/>
        <result column="CREATEBY" jdbcType="VARCHAR" property="createby"/>
        <result column="CREATEDATE" jdbcType="TIMESTAMP" property="createdate"/>
        <result column="LASTMODIFYBY" jdbcType="VARCHAR" property="lastmodifyby"/>
        <result column="LASTMODIFYDATE" jdbcType="TIMESTAMP" property="lastmodifydate"/>
        <result column="P_ENTERPRISEID" jdbcType="VARCHAR" property="pEnterpriseid"/>
        <result column="P_ENTERPRISENAME" jdbcType="VARCHAR" property="pEnterprisename"/>
        <result column="UNQUALIFY_REASON" jdbcType="VARCHAR" property="unqualifyReason"/>
        <result column="CHECKSTATUS" jdbcType="VARCHAR" property="checkstatus"/>
        <result column="WMSBILLNO" jdbcType="VARCHAR" property="wmsbillno"/>
        <result column="BILLTYPE" jdbcType="VARCHAR" property="billtype"/>
        <result column="STOCK_STATUS_OLD" jdbcType="VARCHAR" property="stockStatusOld"/>
        <result column="BILLDATE" jdbcType="TIMESTAMP" property="billdate"/>
        <result column="LINEID" jdbcType="VARCHAR" property="lineid"/>
        <result column="SEQNO" jdbcType="VARCHAR" property="seqno"/>
        <result column="DATASOURCE" jdbcType="VARCHAR" property="datasource"/>
        <result column="BRANCHCODE" jdbcType="VARCHAR" property="branchcode"/>
        <result column="CONSIGNORCODE" jdbcType="VARCHAR" property="consignorcode"/>
    </resultMap>
    <select id="getOmsMoveHeadList" resultType="com.prolog.upcloud.base.wh.operate.vo.MoveErpHeadListVO">
        select distinct
               billid           BILL_ID,
               billno           BILL_NO,
               consignorid      CARGO_OWNER_ID,
               consignorname    CARGO_OWNER_NAME,
               IAR_DATE
        from OMS_STOCK_ADJUSTMENT_RECORD record
        where DATASOURCE = 2
          and BILLTYPE = #{billType}
          and BRANCHID = #{warehouseId}
          and P_ENTERPRISEID = #{enterpriseId}
          and ((SELECT COALESCE(SUM(COALESCE(dd.QUANTITY, 0)),0) FROM OMS_STOCK_ADJUSTMENT_RECORD  dd WHERE dd.BILLID =  record.BILLID)
					- (SELECT COALESCE(SUM(COALESCE(d.PIECE_QUANTITY, 0)),0) FROM WMS_MOVE_D  d WHERE d.BILL_ID =  record.BILLID) 
					 &gt;  0)
<!--           and not exists(select 1
                         from WMS_MOVE_D
                         WHERE record.BILLID = BILL_ID
                           AND record.BILLNO = BILL_NO) -->
    </select>
    <select id="getOmsMoveList" resultType="com.prolog.upcloud.base.wh.operate.vo.MoveErpListVO">
    select temp.* from(
        select lineid           LINE_ID,
               billid           BILL_ID,
               billno           BILL_NO,
               branchid         WAREHOUSE_ID,
               branchname       WAREHOUSE_NAME,
               consignorid      CARGO_OWNER_ID,
               consignorname    CARGO_OWNER_NAME,
               itemid           ITEM_ID,
               itemcode         ITEM_CODE,
               itemname         ITEM_NAME,
               lot_no           MANUFACTURER_LOT_NUM,
               stock_status     IN_STOCK_STATUS,
               vendor_batch_num,
               production_place,
               grade,
               production_date  BATCH_PRODUCTION_DATE,
               shelf_life_days,
               expired_date     BATCH_EXPIRED_DATE,
               ATTR_STR2        BATCH_PRODUCTION_DATE_SHOW,
               ATTR_STR3        BATCH_EXPIRED_DATE_SHOW,
               <!-- quantity         ERP_PLAN_QUANTITY,
               quantity         oddPieceQuantity, -->
           	   quantity - COALESCE((SELECT sum(COALESCE(d.PIECE_QUANTITY, 0)) FROM WMS_MOVE_D d WHERE d.BILL_ID = record.BILLID AND d.LINE_ID = record.LINEID),0)
							as	ERP_PLAN_QUANTITY,
               quantity - COALESCE((SELECT sum(COALESCE(d.PIECE_QUANTITY, 0)) FROM WMS_MOVE_D d WHERE d.BILL_ID = record.BILLID AND d.LINE_ID = record.LINEID),0)    
                			as	oddPieceQuantity,
               0                caseQuantity,
               adjust_reason    REASON,
               stock_status_old OUT_STOCK_STATUS,
               branchcode       WAREHOUSE_CODE,
               consignorcode    CARGO_OWNER_CODE,
               BATCH_ID   
        from OMS_STOCK_ADJUSTMENT_RECORD record
        where DATASOURCE = 2
          and BILLTYPE = #{billType}
          and P_ENTERPRISEID = #{enterpriseId}
          and billid = #{erpBillId}
          and billno = #{erpBillNo}
          ) temp  where temp.ERP_PLAN_QUANTITY > 0
    </select>
</mapper>
