/**
 * Copyright (c) 2003-2004 System Integrator Corporation.
 *                 All Rights Reserved.
 */
package jp.co.sint.basic;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import jp.co.sint.beans.mallmgr.UIOrderBasic;
import jp.co.sint.config.SIConfig;
import jp.co.sint.config.SIFlagConf;
import jp.co.sint.database.SIDBUtil;
import jp.co.sint.tools.SIUtil;

import org.apache.log4j.Category;

/**
 * @version $Id: SIOrderCmdty.java,v 1.0 2004/09/24 Exp $
 * @author  Akao Shinya
* <br>Description:
 * <p>History</p>
 * <p>Author&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Date&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Reason</p>
 *  ============&nbsp;&nbsp;&nbsp;==========&nbsp;&nbsp;===========================<br>
 * Akao Shinya     2004/09/24 16:10:00  Original
 */
public class SIOrderCmdty extends UIOrderBasic{
  //ログ用のインスタンスの生成
  private static Category log=Category.getInstance(SIConfig.SILOG4J_WEBSHOP_CATEGORY_NAME);

  //受注番号
  private String orderCode="";

  //ショップコード
  private String shopCode="";

  //ショップ名
  private String shopName="";

  //商品コード
  private String cmdtyCode="";
  
  //在庫コード PI-NES0501 追加
  private String individualCode="";

  //商品名
  private String cmdtyName="";

  //規格1
  private String stndrdCode1="";

  //エレメント1
  private String elementCode1="";

  //規格2
  private String stndrdCode2="";

  //エレメント2
  private String elementCode2="";

  //規格名1
  private String stndrdName1="";

  //エレメント名1
  private String elementName1="";

  //規格名2
  private String stndrdName2="";

  //エレメント名2
  private String elementName2="";

  //数量
  private String amount="0";

  //本体値段
  private String orgPrice="0";

  //購入値段
  private String price="0";

  //購入価格
  private String priceIncTax="0";

  //本体価格税区分
  private String taxFlg="1";

  //税率
  private String taxRate="0";

  //総合計金額(数量×)
  private String priceIncGift = "0";

  public SIOrderCmdty(){
  }

  public SIOrderCmdty(String lOrderCode) {
    setOrderCode(lOrderCode);
  }

  //setter of 受注番号
  public void setOrderCode(String lOrderCode){
    if (SIUtil.isNull(lOrderCode)) lOrderCode="";
    this.orderCode=SIUtil.changeTo(lOrderCode.trim(),this.encode);
  }

  //setter of ショップコード
  public void setShopCode(String lShopCode){
    if (SIUtil.isNull(lShopCode)) lShopCode="";
    this.shopCode=SIUtil.changeTo(lShopCode.trim(),this.encode);
  }

  //setter of ショップ名
  public void setShopName(String lShopName){
    if (SIUtil.isNull(lShopName)) lShopName="";
    this.shopName=SIUtil.changeTo(lShopName.trim(),this.encode);
  }

  //setter of 商品コード
  public void setCmdtyCode(String lCmdtyCode){
    if (SIUtil.isNull(lCmdtyCode)) lCmdtyCode="";
    this.cmdtyCode=SIUtil.changeTo(lCmdtyCode.trim(),this.encode);
  }

  //setter of 商品名(必須)
  public void setCmdtyName(String lCmdtyName){
    if (SIUtil.isNull(lCmdtyName)) lCmdtyName="";
    this.cmdtyName=SIUtil.changeTo(lCmdtyName.trim(),this.encode);
  }

  //setter of 規格1
  public void setStndrdCode1(String lStndrdCode1){
    if (SIUtil.isNull(lStndrdCode1)) lStndrdCode1="";
    this.stndrdCode1=SIUtil.changeTo(lStndrdCode1.trim(),this.encode);
  }

  //setter of エレメント1
  public void setElementCode1(String lElementCode1){
    if (SIUtil.isNull(lElementCode1)) lElementCode1="";
    this.elementCode1=SIUtil.changeTo(lElementCode1.trim(),this.encode);
  }

  //setter of 規格2
  public void setStndrdCode2(String lStndrdCode2){
    if (SIUtil.isNull(lStndrdCode2)) lStndrdCode2="";
    this.stndrdCode2=SIUtil.changeTo(lStndrdCode2.trim(),this.encode);
  }

  //setter of エレメント2
  public void setElementCode2(String lElementCode2){
    if (SIUtil.isNull(lElementCode2)) lElementCode2="";
    this.elementCode2=SIUtil.changeTo(lElementCode2.trim(),this.encode);
  }
  //setter of 規格名1
  public void setStndrdName1(String lStndrdName1){
    if (SIUtil.isNull(lStndrdName1)) lStndrdName1="";
    this.stndrdName1=SIUtil.changeTo(lStndrdName1.trim(),this.encode);
  }

  //setter of エレメント名1
  public void setElementName1(String lElementName1){
    if (SIUtil.isNull(lElementName1)) lElementName1="";
    this.elementName1=SIUtil.changeTo(lElementName1.trim(),this.encode);
  }

  //setter of 規格名2
  public void setStndrdName2(String lStndrdName2){
    if (SIUtil.isNull(lStndrdName2)) lStndrdName2="";
    this.stndrdName2=SIUtil.changeTo(lStndrdName2.trim(),this.encode);
  }

  //setter of エレメント名2
  public void setElementName2(String lElementName2){
    if (SIUtil.isNull(lElementName2)) lElementName2="";
    this.elementName2=SIUtil.changeTo(lElementName2.trim(),this.encode);
  }

  //setter of 数量(必須)
  public void setAmount(String lAmount){
    if (SIUtil.isNull(lAmount)) lAmount="0";
    this.amount=SIUtil.changeTo(lAmount.trim(),this.encode);
  }

  //setter of 本体値段
  public void setOrgPrice(String lOrgPrice){
    if (SIUtil.isNull(lOrgPrice)) lOrgPrice="0";
    this.orgPrice=SIUtil.changeTo(lOrgPrice.trim(),this.encode);
  }

  //setter of 購入値段
  public void setPrice(String lPrice){
    if (SIUtil.isNull(lPrice)) lPrice="0";
    this.price=SIUtil.changeTo(lPrice.trim(),this.encode);
  }

  //setter of 購入価格
  public void setPriceIncTax(String lPriceIncTax){
    if (SIUtil.isNull(lPriceIncTax)) lPriceIncTax="0";
    this.priceIncTax=SIUtil.changeTo(lPriceIncTax.trim(),this.encode);
  }

  //setter of 本体価格税区分
  public void setTaxFlg(String lTaxFlg){
    if (SIUtil.isNull(lTaxFlg)) lTaxFlg="1";
    this.taxFlg=SIUtil.changeTo(lTaxFlg.trim(),this.encode);
  }

  //setter of 税率
  public void setTaxRate(String lTaxRate){
    if (SIUtil.isNull(lTaxRate)) lTaxRate="0";
    this.taxRate=SIUtil.changeTo(lTaxRate.trim(),this.encode);
  }

  //getter of 受注番号
  public String getOrderCode(){
    return this.orderCode;
  }

  //getter of ショップコード
  public String getShopCode(){
    return this.shopCode;
  }

  //getter of ショップ名
  public String getShopName(){
    return this.shopName;
  }

  //getter of 商品コード
  public String getCmdtyCode(){
    return this.cmdtyCode;
  }

  public String getCompCode(){
    String lRes=getShopCode()+SIConfig.SIWEBSHOPPING_TOKEN+
           getCmdtyCode()+SIConfig.SIWEBSHOPPING_TOKEN+
           getStndrdCode1()+SIConfig.SIWEBSHOPPING_TOKEN+
           getElementCode1()+SIConfig.SIWEBSHOPPING_TOKEN+
           getStndrdCode2()+SIConfig.SIWEBSHOPPING_TOKEN+
           getElementCode2();
    log.debug("getCompCode():lRes="+lRes);
    return lRes;
  }

  //getter of 商品名(必須)
  public String getCmdtyName(){
    return this.cmdtyName;
  }

  //getter of 商品名(規格名)
  public String getCartCmdtyName(){
    String lStndElementName="";

    if (SIUtil.isNotNull(getStndrdName1())&&SIUtil.isNotNull(getElementName1())){
      lStndElementName="("+getStndrdName1()+":"+getElementName1();
    }
    if (SIUtil.isNotNull(lStndElementName)&&SIUtil.isNotNull(getStndrdName2())&&SIUtil.isNotNull(getElementName2())){
      lStndElementName=lStndElementName+"/"+getStndrdName2()+":"+getElementName2()+")";
    }else if (SIUtil.isNotNull(lStndElementName)&&(SIUtil.isNull(getStndrdName2())||SIUtil.isNull(getElementName2()))){
      lStndElementName=lStndElementName+")";
    }
    return getCmdtyName()+lStndElementName;
  }

  //getter of 規格1
  public String getStndrdCode1(){
    return this.stndrdCode1;
  }

  //getter of エレメント1
  public String getElementCode1(){
    return this.elementCode1;
  }

  //getter of 規格2
  public String getStndrdCode2(){
    return this.stndrdCode2;
  }

  //getter of エレメント2
  public String getElementCode2(){
    return this.elementCode2;
  }

  //getter of 規格名1
  public String getStndrdName1(){
    return this.stndrdName1;
  }

  //getter of エレメント名1
  public String getElementName1(){
    return this.elementName1;
  }

  //getter of 規格名2
  public String getStndrdName2(){
    return this.stndrdName2;
  }

  //getter of エレメント名2
  public String getElementName2(){
    return this.elementName2;
  }

  //getter of 数量(必須)
  public String getAmount(){
    return this.amount;
  }

  //getter of 本体値段
  public String getOrgPrice(){
    return this.orgPrice;
  }

  //getter of 購入値段
  public String getPrice(){
    return this.price;
  }

  //getter of 本体価格税区分
  public String getTaxFlg(){
    return this.taxFlg;
  }
  //getter of 在庫コード
  public String getIndividualCode() {
    return individualCode;
  }
  //setter of 在庫コード
  public void setIndividualCode(String individualCode) {
    this.individualCode = individualCode;
  }
  
  public String getTaxFlgName(){
    if (getTaxFlg().equals("0")) return "("+SIFlagConf.SIFLAG_TAX_0+")";
    else if (getTaxFlg().equals("1")) return "("+SIFlagConf.SIFLAG_TAX_1+")";
    else if (getTaxFlg().equals("2")) return "("+SIFlagConf.SIFLAG_TAX_2+")";
    else return "";
  }

  //商品の課税区分
  public String getCmdtyTaxName(){
    if (getTaxFlg().equals("0")) return SIFlagConf.SIFLAG_TAX_0;
    else if (getTaxFlg().equals("1")) return SIFlagConf.SIFLAG_TAX_1;
    else if (getTaxFlg().equals("2")) return SIFlagConf.SIFLAG_TAX_2;
    else return "";
  }

  //getter of 税率
  public String getTaxRate(){
    return this.taxRate;
  }

  //購入価格=値段(税込み)*数量
  public String getPriceIncTax(){
    return this.priceIncTax;
  }

  //商品合計額(数量(税込み)*単価)
  public String getPriceOfCmdty(){
    return SIUtil.multi_LL(this.getAmount(),this.getPriceIncTax());
  }

  /**
   * <b>reset</b>
   * 修正モードの場合には、PKのデータを元に、明細データをデータベースから取り込んで
   * 画面に表示します。
   * @param lConnection データベースへのコネンクション
   * @return なし
   * @throws なし
   */
  public boolean reset(Connection lConnection){
    boolean lResult=false;
    if (SIUtil.isNull(this.getOrderCode())) return lResult;
    Statement lStatement=null;
    ResultSet lResultSet=null;
    StringBuffer lSqlBuf=new StringBuffer();
    lSqlBuf.append("SELECT * FROM OrderDetailVW ");
    lSqlBuf.append("WHERE OrderCode=").append(SIDBUtil.SQL2Str(this.getOrderCode()," "));
    lSqlBuf.append("AND ShopCode=").append(SIDBUtil.SQL2Str(this.getShopCode()," "));
    lSqlBuf.append("AND CmdtyCode=").append(SIDBUtil.SQL2Str(this.getCmdtyCode()));

    log.debug("reset:lSqlBuf="+lSqlBuf.toString());
    try {
      lStatement=lConnection.createStatement();
      lResultSet=lStatement.executeQuery(lSqlBuf.toString());

      if (lResultSet.next()){
        this.setEncode(SIConfig.SIENCODE_NONE);
        lResult=true;
        this.setOrderCode(lResultSet.getString("orderCode"));//受注番号
        this.setShopCode(lResultSet.getString("shopCode"));//ショップコード
        this.setShopName(lResultSet.getString("shopName"));//ショップ名
        this.setCmdtyCode(lResultSet.getString("cmdtyCode"));//商品コード
        this.setIndividualCode(lResultSet.getString("individualCode"));//在庫コード PI-NES0501 追加
        this.setCmdtyName(lResultSet.getString("cmdtyName"));//商品名(必須)
        this.setStndrdCode1(lResultSet.getString("stndrdCode1"));//規格1
        this.setElementCode1(lResultSet.getString("elementCode1"));//エレメント1
        this.setStndrdCode2(lResultSet.getString("stndrdCode2"));//規格2
        this.setElementCode2(lResultSet.getString("elementCode2"));//エレメント2
        this.setStndrdName1(lResultSet.getString("stndrdName1"));//規格名1
        this.setElementName1(lResultSet.getString("elementName1"));//エレメント名1
        this.setStndrdName2(lResultSet.getString("stndrdName2"));//規格名2
        this.setElementName2(lResultSet.getString("elementName2"));//エレメント名2
        this.setAmount(lResultSet.getString("amount"));//数量(必須)
        this.setOrgPrice(lResultSet.getString("OrgPrice"));//本体価格
        this.setPrice(lResultSet.getString("price"));//購入価格
        this.setTaxFlg(lResultSet.getString("taxFlg"));//本体価格税区分
        this.setTaxRate(lResultSet.getString("taxRate"));//税率
        this.setPriceIncTax(lResultSet.getString("priceIncTax"));//購入価格
      }else {
        log.error("not find record for ordercode="+getOrderCode());
      }
    }catch(SQLException sqle){
      sqle.printStackTrace();
    }finally{
      SIDBUtil.close(lStatement,lResultSet);
    }
    return lResult;
  }
}
