unit U_TradeCardInStk;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGrid, DBClient, cxCalendar, cxButtonEdit, cxSplitter, RM_Common, RM_Class,
  RM_e_Xls, RM_Dataset, RM_System, RM_GridReport, cxTextEdit, cxCheckBox,
  BtnEdit, cxPC, MovePanel, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxBarBuiltInMenu,   dxDateRanges,
  U_BaseList, Vcl.Menus, U_BaseInput, System.ImageList, Vcl.ImgList, StrUtils,
   dxScrollbarAnnotations, cxContainer, cxMaskEdit, cxDropDownEdit;

type
  TfrmTradeCardInStk = class(TfrmBaseInput)
    GPM_1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    PM_1: TPopupMenu;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v3Column8: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Panel3: TPanel;
    Label1: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label6: TLabel;
    Label5: TLabel;
    Label8: TLabel;
    C_Name: TcxTextEdit;
    C_Code: TcxTextEdit;
    ConNo: TcxTextEdit;
    GC_Name: TcxTextEdit;
    CraftCode: TcxTextEdit;
    C_Color: TcxTextEdit;
    C_Pattern: TcxTextEdit;
    C_ColorConfirm: TcxTextEdit;
    C_ColorNo: TcxTextEdit;
    OrderNo: TcxTextEdit;
    ToolBar1: TToolBar;
    ToolButton12: TToolButton;
    TBClose: TToolButton;
    ADO_1: TADOQuery;
    RM1: TRMGridReport;
    ADOQueryPrint: TADOQuery;
    RMDB_1: TRMDBDataSet;
    btnRK: TToolButton;
    Panel1: TPanel;
    Label9: TLabel;
    Label20: TLabel;
    PCQty: TcxTextEdit;
    PCWeight: TcxTextEdit;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Label4: TLabel;
    BatchNo: TcxTextEdit;
    Tv1Column8: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Label27: TLabel;
    PCGrade: TcxComboBox;
    Tv1Column9: TcxGridDBColumn;
    Label7: TLabel;
    PCTare: TcxTextEdit;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    cxComboBox1: TcxComboBox;
    Tv1Column12: TcxGridDBColumn;
    Label10: TLabel;
    OrdUnit: TcxTextEdit;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Label11: TLabel;
    Label14: TLabel;
    PackWidth: TcxTextEdit;
    PackLength: TcxTextEdit;
    Label15: TLabel;
    Label18: TLabel;
    ActualWidth: TcxTextEdit;
    PackHeight: TcxTextEdit;
    Tv1Column17: TcxGridDBColumn;
    ToolButton2: TToolButton;
    Tv1Column18: TcxGridDBColumn;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    Label19: TLabel;
    PCQty2: TcxTextEdit;
    Label21: TLabel;
    CMSel: TcxComboBox;
    Label22: TLabel;
    Position: TcxButtonEdit;
    Tv1Column19: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton12Click(Sender: TObject);
    procedure PCQtyKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton1Click(Sender: TObject);
    procedure btnRKClick(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ActualWidthKeyPress(Sender: TObject; var Key: Char);
    procedure PackLengthKeyPress(Sender: TObject; var Key: Char);
    procedure PackWidthKeyPress(Sender: TObject; var Key: Char);
    procedure PCWeightKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure PCQty2KeyPress(Sender: TObject; var Key: Char);
    procedure PCTareKeyPress(Sender: TObject; var Key: Char);
    procedure StkPositionPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    procedure CardInStk();
    procedure InitOrd();
    procedure InitGrid1();
    procedure PrtData(MCIID: string);
    procedure SaveCard(MCardQty: Integer; MBatchNo: string);
    { Private declarations }
  public
    FMainId, FSubId: string;
    { Public declarations }
  end;

var
  frmTradeCardInStk: TfrmTradeCardInStk;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_InputBoxCard, U_LabelPrint, U_TradePlanSel,
  U_CompanySel;
  {$R *.dfm}

procedure TfrmTradeCardInStk.PrtData(MCIID: string);
var
  fPrintFile, FLabVolume: string;
  Txt, fImagePath, Txt2, fImagePath2: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add(' EXEC P_Trade_Card_Prt1 ');
    SQL.Add(' @Filtration=''' + Trim(MCIID) + '''');
//    ShowMessage(sql.Text);
    Open;
  end;
  if ADOQueryPrint.IsEmpty then
  begin
    application.MessageBox('标签内容未找到！', '提示信息', MB_ICONERROR);
    exit;
  end;
  FLabVolume := Trim(ADOQueryPrint.fieldbyname('LabVolume').AsString);
  ExportFtErpFile(FLabVolume + '.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + FLabVolume + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.DefaultCopies := 1;
    RM1.PrintReport;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update Trade_Plan_Card         ');
      sql.Add('set PRTer=' + quotedstr(DName));
      sql.Add(', PRTCount=PRTCount+1');
      sql.Add(', PRTDate=getdate()');
      sql.Add(' where  EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(MCIID) + ','','') X where Trade_Plan_Card.PCID=X.RTValue )  ');
      ExecSQL;
    end;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmTradeCardInStk.InitOrd();
begin

  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.*,B.*');
    sql.Add(' ,YAPPS=ISNULL((select count(*) from Trade_Plan_Card X where X.MainId=A.MainId),0)');
    sql.Add(' ,WAPPS=OrdPiece-ISNULL((select count(*) from Trade_Plan_Card X where X.MainId=A.MainId),0)');
    sql.Add(' from Trade_Plan_Main A');
    sql.Add(' inner join Trade_Plan_Sub B on A.MainId=B.MainId ');
    sql.Add(' where B.SubId=' + QuotedStr(FSubId));
//    ShowMessage(sql.Text);
    Open;
  end;
  SCSHData(ADOQueryMain, Panel3, 0);
  if Trim(OrdUnit.Text) = 'M' then
    cxComboBox1.ItemIndex := 0;
  if Trim(OrdUnit.Text) = 'Y' then
    cxComboBox1.ItemIndex := 1;

end;

procedure TfrmTradeCardInStk.InitGrid1();
var
  Msql: string;
  SqlStr: string;
begin
  if trim(BatchNo.Text) <> '' then
    Msql := ' BatchNo=' + QuotedStr(BatchNo.Text);
  SqlStr := ' select *,PCWeight1=PCWeight-PCTare,RollVolume=round(PackLength* PackWidth* PackHeight,4)    ';
  SqlStr := SqlStr + ' ,Status1= case when Status=''10'' then ''已抄码'' else ''待抄码'' end ';
  SqlStr := SqlStr + '  from Trade_Plan_Card A where Subid =''' + trim(FSubid) + '''';
  InitCDSData(ADO_1, CDS_1, Tv1, SqlStr, Msql, '');
end;

procedure TfrmTradeCardInStk.FormDestroy(Sender: TObject);
begin
  inherited;
  frmTradeCardInStk := nil;
end;

procedure TfrmTradeCardInStk.PackLengthKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    PackWidth.SetFocus;
  end;
end;

procedure TfrmTradeCardInStk.PackWidthKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    PackHeight.Text := PackWidth.Text;
    ActualWidth.SetFocus;
  end;
end;

procedure TfrmTradeCardInStk.PCQty2KeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    PCQty.SetFocus;
  end;

end;

procedure TfrmTradeCardInStk.CardInstk();
var
  MPCID: string;
begin
  if Trim(BatchNo.Text) = '' then
  begin
    application.MessageBox('请填写选择批号！', '提示信息');
    Exit;
  end;
  if StrToFloatDef(PCQty.Text, 0) = 0 then
  begin
    application.MessageBox('请输入数量！', '提示信息');
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select top 1 * from Trade_Plan_Card A  ');
    sql.Add(' where Subid =''' + trim(FSubid) + '''');
    sql.Add(' and  BatchNo=' + QuotedStr(BatchNo.Text));
    sql.Add(' and  isnull(Status,''0'')=''0''  ');
    sql.Add(' order by  PCNo ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    application.MessageBox('当前数据无需抄码！', '提示信息');
    Exit;
  end;
  MPCID := ADOQueryTemp.FieldByName('PCID').AsString;

  ADOQueryCmd.Connection.BeginTrans;
  try
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update Trade_Plan_Card ');
      sql.Add(' set Status=''10''   ');
      sql.Add(', CompId=' + quotedstr(DCode));
      sql.Add(', Comper=' + quotedstr(DName));
      sql.Add(', Comptime=getdate()');
      if cxComboBox1.Text = 'M' then
      begin
        sql.Add(' , PCMeter=' + FloatToStr(StrToFloatDef(PCQty.Text, 0)));
        sql.Add(' , PCYardage=' + FloatToStr(RoundFloat(StrToFloatDef(PCQty.Text, 0) / 0.9144, 1)));
      end;
      if cxComboBox1.Text = 'Y' then
      begin
        sql.Add(' , PCMeter=' + FloatToStr(RoundFloat(StrToFloatDef(PCQty.Text, 0) * 0.9144, 1)));
        sql.Add(' , PCYardage=' + FloatToStr(StrToFloatDef(PCQty.Text, 0)));
      end;
      sql.Add(' , PCWeight=' + FloatToStr(StrToFloatDef(PCWeight.Text, 0)));
      sql.Add(' , PCTare=' + FloatToStr(StrToFloatDef(PCTare.Text, 0)));
      sql.Add(' , PCGrade=' + QuotedStr(PCGrade.Text));
      sql.Add('where PCID=' + QuotedStr(MPCID));
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into Trade_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)       ');
      sql.Add('values(''贸易成品入库''    ');
      sql.Add(',' + quotedstr(MPCID));
      sql.Add(',''入库''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(MPCID));
      sql.Add(')  ');
      execsql;
    end;
    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('exec P_Trade_Card_In_Stk ');
      Sql.Add('@PCID=' + quotedstr(trim(MPCID)));
      Sql.Add(',@FillId=' + Quotedstr(DCode));
      Sql.Add(',@Filler=' + Quotedstr(DName));
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    end;
    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('数据入库成功！', '提示信息');
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmTradeCardInStk.PCQtyKeyPress(Sender: TObject; var Key: Char);
var
  MPCID: string;
begin

  if Key = #13 then
  begin
    if CMSel.text = '仅数量重量' then
    begin
      if Trim(BatchNo.Text) = '' then
      begin
        application.MessageBox('请填写选择批号！', '提示信息');
        Exit;
      end;
      if Trim(OrdUnit.Text) = 'Kg' then
      begin
        if StrToFloatDef(PCQty2.Text, 0) = 0 then
        begin
          application.MessageBox('请输入数量2！', '提示信息');
          Exit;
        end;
      end
      else
      begin
        if StrToFloatDef(PCQty.Text, 0) = 0 then
        begin
          application.MessageBox('请输入数量！', '提示信息');
          Exit;
        end;
      end;

      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add(' select top 1 * from Trade_Plan_Card A  ');
        sql.Add(' where Subid =''' + trim(FSubid) + '''');
        sql.Add(' and  BatchNo=' + QuotedStr(BatchNo.Text));
        sql.Add(' and  isnull(Status,''0'')=''0''  ');
        sql.Add(' order by  PCNo ');
        Open;
      end;
      if ADOQueryTemp.IsEmpty then
      begin
        application.MessageBox('当前数据无需抄码！', '提示信息');
        Exit;
      end;
      MPCID := ADOQueryTemp.FieldByName('PCID').AsString;

      ADOQueryCmd.Connection.BeginTrans;
      try
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('update Trade_Plan_Card ');
          sql.Add(' set Status=''10''   ');
          sql.Add(', CompId=' + quotedstr(DCode));
          sql.Add(', Comper=' + quotedstr(DName));
          sql.Add(', Comptime=getdate()');
          if cxComboBox1.Text = 'M' then
          begin
            sql.Add(' , PCMeter=' + FloatToStr(StrToFloatDef(PCQty.Text, 0)));
            sql.Add(' , PCYardage=' + FloatToStr(RoundFloat(StrToFloatDef(PCQty.Text, 0) / 0.9144, 1)));
          end;
          if cxComboBox1.Text = 'Y' then
          begin
            sql.Add(' , PCMeter=' + FloatToStr(RoundFloat(StrToFloatDef(PCQty.Text, 0) * 0.9144, 1)));
            sql.Add(' , PCYardage=' + FloatToStr(StrToFloatDef(PCQty.Text, 0)));
          end;
          sql.Add(' , PCWeight=' + FloatToStr(StrToFloatDef(PCWeight.Text, 0)));
          sql.Add(' , PCTare=' + FloatToStr(StrToFloatDef(PCTare.Text, 0)));
          sql.Add(' , PCGrade=' + QuotedStr(PCGrade.Text));
          sql.Add(' , Weight=' + FloatToStr(StrToFloatDef(PCQty2.Text, 0)));

          sql.Add(' , IOPosition=' + QuotedStr(Position.Text));
          sql.Add('where PCID=' + QuotedStr(MPCID));
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('insert into Trade_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)       ');
          sql.Add('values(''贸易成品入库''    ');
          sql.Add(',' + quotedstr(MPCID));
          sql.Add(',''入库''   ');
          sql.Add(',' + quotedstr(DName));
          sql.Add(',' + quotedstr(trim(self.Caption)));
          sql.Add(',' + quotedstr(MPCID));
          sql.Add(')  ');
          execsql;
        end;
        with ADOQueryCmd do
        begin
          Close;
          Sql.Clear;
          Sql.Add('exec P_Trade_Card_In_Stk ');
          Sql.Add(' @PCID=' + quotedstr(trim(MPCID)));
          Sql.Add(',@FillId=' + Quotedstr(DCode));
          Sql.Add(',@Filler=' + Quotedstr(DName));
          Open;
        end;
        if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
        begin
          raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
        end;
        ADOQueryCmd.Connection.CommitTrans;
        application.MessageBox('数据入库成功！', '提示信息');
      except
        ADOQueryCmd.Connection.RollbackTrans;
        application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
      end;

      if Trim(OrdUnit.Text) = 'Kg' then
      begin
        PCQty2.SetFocus;
      end
      else
      begin
        PCQty.SetFocus;
      end;

      PCQty2.Text := '';
      PCQty.Text := '';
      PCWeight.Text := '';

      InitGrid1();
    end
    else
    begin
      PackLength.SetFocus;
    end;
  end;
end;

procedure TfrmTradeCardInStk.PCTareKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    PCWeight.SetFocus;
  end;
end;

procedure TfrmTradeCardInStk.PCWeightKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if OrdUnit.text = 'Kg' then
    begin
      PCQty2.SetFocus;
    end
    else
    begin
      PCQty.SetFocus;
    end;
  end;
end;

procedure TfrmTradeCardInStk.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := caFree;
end;

procedure TfrmTradeCardInStk.TBCloseClick(Sender: TObject);
begin
  Close;

end;

procedure TfrmTradeCardInStk.TBRafreshClick(Sender: TObject);
begin
  InitGrid1();

end;

procedure TfrmTradeCardInStk.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '指示单管理');
  InitOrd();
  InitGrid1();
end;

procedure TfrmTradeCardInStk.cxTabControl1Change(Sender: TObject);
begin
  InitGrid1();
end;

procedure TfrmTradeCardInStk.ToolButton12Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv1', Tv1, '指示单管理');
end;

procedure TfrmTradeCardInStk.ToolButton1Click(Sender: TObject);
begin
  InitGrid1();
end;

procedure TfrmTradeCardInStk.ToolButton2Click(Sender: TObject);
var
  MSTKIDS: string;
begin
  MSTKIDS := SelCDSKey(CDS_1, ['pcid'])[0];
  if Application.MessageBox('确定要执行操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into Trade_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)       ');
      sql.Add('values(''贸易成品入库''    ');
      sql.Add(',' + quotedstr(MSTKIDS));
      sql.Add(',''撤销入库''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(MSTKIDS));
      sql.Add(')  ');
      execsql;
    end;

    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('exec P_Trade_Cloth_ReIn ');
      Sql.Add('@StkIds=' + quotedstr(trim(MSTKIDS)));
      Sql.Add(',@IOIDS=' + quotedstr(trim('')));
      Sql.Add(',@FillId=' + Quotedstr(DCode));
      Sql.Add(',@Filler=' + Quotedstr(DName));
      Open;
    end;

    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    end;

    ADOQueryCmd.Connection.CommitTrans;

    InitGrid1();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmTradeCardInStk.ToolButton3Click(Sender: TObject);
var
  MSTKIDS: string;
begin
  MSTKIDS := SelCDSKey(CDS_1, ['pcid'])[0];
  if Application.MessageBox('确定要执行操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update Trade_Plan_Card ');
      sql.Add(' set Editer=' + quotedstr(DName) + ', Edittime=getdate()');
      sql.Add(',PackLength=null,PackWidth=null,PackHeight=null,ActualWidth=null,PCGrade=null');
      sql.Add(' where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(MSTKIDS) + ','','') X where Trade_Plan_Card.PCID=X.RTValue )  ');
      ExecSQL;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update Trade_Cloth_Stock ');
      sql.Add(' set PackLength=null,PackWidth=null,PackHeight=null,ActualWidth=null');
      sql.Add(' where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(MSTKIDS) + ','','') X where Trade_Cloth_Stock.StkId=X.RTValue )  ');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;

    InitGrid1();
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmTradeCardInStk.ToolButton4Click(Sender: TObject);
var
  WSql: string;
begin
  if CDS_1.IsEmpty then
    Exit;

  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;

  WSql := SelCDSKey(CDS_1, ['PCID'])[0];

  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('exec P_Trade_Card_Del ');
      Sql.Add('@PCIDS=' + quotedstr(Trim(WSql)));
      Sql.Add(',@DCode=' + Quotedstr(DCode));
      Sql.Add(',@DName=' + Quotedstr(DName));
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid1();
    application.MessageBox('删除成功！', '提示信息');
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure TfrmTradeCardInStk.Tv1DblClick(Sender: TObject);
begin
  BatchNo.Text := CDS_1.FieldByName('BatchNo').AsString;
end;

procedure TfrmTradeCardInStk.ActualWidthKeyPress(Sender: TObject; var Key: Char);
var
  MPCID: string;
begin

  if Key = #13 then
  begin
    CardInStk();
    if Trim(BatchNo.Text) = '' then
    begin
      application.MessageBox('请填写选择批号！', '提示信息');
      Exit;
    end;
    if StrToFloatDef(PackLength.Text, 0) = 0 then
    begin
      application.MessageBox('请输入长！', '提示信息');
      Exit;
    end;

    if StrToFloatDef(PackWidth.Text, 0) = 0 then
    begin
      application.MessageBox('请输入宽！', '提示信息');
      Exit;
    end;

    if StrToFloatDef(PackHeight.Text, 0) = 0 then
    begin
      application.MessageBox('请输入高！', '提示信息');
      Exit;
    end;

    if ActualWidth.Text = '' then
    begin
      application.MessageBox('请输入全幅！', '提示信息');
      Exit;
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add(' select top 1 * from Trade_Plan_Card A  ');
      sql.Add(' where Subid =''' + trim(FSubid) + '''');
      sql.Add(' and  BatchNo=' + QuotedStr(BatchNo.Text));
      sql.Add(' and  isnull(PackLength,0)=0  ');
      sql.Add(' order by  PCNo ');
      Open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      application.MessageBox('当前数据无需录入体积！', '提示信息');
      Exit;
    end;
    MPCID := ADOQueryTemp.FieldByName('PCID').AsString;
    ADOQueryCmd.Connection.BeginTrans;
    try
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('update Trade_Plan_Card ');
        sql.Add(' set Editer=' + quotedstr(DName));
        sql.Add(', Edittime=getdate()');
        sql.Add(' , PackLength=' + FloatToStr(StrToFloatDef(PackLength.Text, 0)));
        sql.Add(' , PackWidth=' + FloatToStr(StrToFloatDef(PackWidth.Text, 0)));
        sql.Add(' , PackHeight=' + FloatToStr(StrToFloatDef(PackHeight.Text, 0)));
        sql.Add(' , ActualWidth=' + QuotedStr(ActualWidth.Text));
        sql.Add(' , PCGrade=' + QuotedStr(PCGrade.Text));
        sql.Add('where PCID=' + QuotedStr(MPCID));
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('update Trade_Cloth_Stock ');
        sql.Add(' set  PackLength=' + FloatToStr(StrToFloatDef(PackLength.Text, 0)));
        sql.Add(' , PackWidth=' + FloatToStr(StrToFloatDef(PackWidth.Text, 0)));
        sql.Add(' , PackHeight=' + FloatToStr(StrToFloatDef(PackHeight.Text, 0)));
        sql.Add(' , ActualWidth=' + FloatToStr(StrToFloatDef(ActualWidth.Text, 0)));
        sql.Add('where StkId=' + QuotedStr(MPCID));
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    end;

    PCQty.Text := '';
    PCWeight.Text := '';
    InitGrid1();
    PackLength.SetFocus;
  end;
end;

procedure TfrmTradeCardInStk.btnRKClick(Sender: TObject);
var
  MINo, MStkIds: string;
begin
  if CDS_1.IsEmpty then
    exit;
  btnRK.Enabled := False;
  with CDS_1 do
  begin
    DisableControls;
    First;
    while not eof do
    begin
      MStkIds := MStkIds + Trim(CDS_1.FieldByName('PCID').AsString) + ',';
      next;
    end;
    EnableControls;
  end;
  MStkIds := copy(MStkIds, 1, Length(MStkIds) - 1);
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select * from Trade_Cloth_IO A where IOFlag=''入库''  ');
    SQL.Add(' and EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + quotedstr(MStkIds) + ','','')  X where X.RTValue=A.StkID )');
    Open;
  end;
  if not ADOQueryTemp.IsEmpty then
  begin
    application.MessageBox('请撤销入库后再入库！', '提示信息');
    Exit;
  end;
  if GetLSNo(ADOQueryCmd, MINo, '9', 'Trade_Cloth_IO', 4, 1) = False then
  begin
    Application.MessageBox('取入库单号失败!', '提示', 0);
    Exit;
  end;

  ADOQueryCmd.Connection.BeginTrans;
  try
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into Trade_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)       ');
      sql.Add('values(''贸易成品入库''    ');
      sql.Add(',' + quotedstr(MStkIds));
      sql.Add(',''入库''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(MStkIds));
      sql.Add(')  ');
      execsql;
    end;
    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('exec P_Trade_Cloth_In ');
      Sql.Add('@StkIds=' + quotedstr(trim(MStkIds)));
      Sql.Add(',@IOTime=' + Quotedstr(FormatDateTime('yyyy-MM-dd', SGetServerDate(ADOQueryTemp))));
      Sql.Add(',@IOType=' + Quotedstr('检验入库'));
      Sql.Add(',@IONO=' + Quotedstr(MINo));
      Sql.Add(',@StkPosition=' + Quotedstr(''));
      Sql.Add(',@FillId=' + Quotedstr(DCode));
      Sql.Add(',@Filler=' + Quotedstr(DName));
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    end;
    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('数据入库成功！', '提示信息');

    btnRK.Enabled := True;
  except
    btnRK.Enabled := True;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure TfrmTradeCardInStk.SaveCard(MCardQty: Integer; MBatchNo: string);
var
  maxno, maxPCId, WSql: string;
  i, j, MPCNo: integer;
begin

  if MCardQty = 0 then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select PCNo=isnull(Max(PCNo),0)  ');
      sql.Add('from Trade_Plan_Card A ');
      sql.Add('where A.Subid=' + Quotedstr(Trim(FSubid)));

      sql.Add('and A.BatchNo=' + Quotedstr(Trim(MBatchNo)));
      Open;
    end;
    if ADOQueryTemp.IsEmpty then
      MPCNo := 1
    else
      MPCNo := ADOQueryTemp.FieldByName('PCNo').AsInteger + 1;
//////////////////            插入布票               //////////////////////
    if GetLSNo(ADOQueryTemp, maxno, 'PC', 'Trade_Plan_Mach', 2, 1) = False then
    begin
      raise Exception.Create('取布票流水号失败!');
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' insert into Trade_Plan_Card(MainId,SubId,PCId,PCNo,PCDate,Filler,BatchNo) values');
      for j := 1 to MCardQty do
      begin
        maxPCId := maxno + RightStr('00' + inttostr(j), 3);
        if j > 1 then
          sql.Add(',');
        sql.Add(' (' + Quotedstr(Trim(FMainId)));
        sql.Add(',' + Quotedstr(Trim(FSubId)));
        sql.Add(',' + Quotedstr(maxPCId));
        sql.Add(',' + Quotedstr(IntToStr(MPCNo)));
        sql.Add(',getdate() ');
        sql.Add(',' + Quotedstr(Trim(DName)));
        sql.Add(',' + Quotedstr(Trim(MBatchNo)));
        sql.Add(' )');
        MPCNo := MPCNo + 1;

        if WSql <> '' then
        begin
          WSql := WSql + ',' + QuotedStr(Trim(maxPCId));
        end
        else
        begin
          WSql := QuotedStr(Trim(maxPCId));
        end;
      end;
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    PrtData(WSql);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    if GetLSNo(ADOQueryTemp, maxPCId, 'PC', 'Trade_Plan_Mach', 3, 1) = False then
    begin
//      raise Exception.Create('取布票流水号失败!');
    end;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

  InitGrid1();
end;

procedure TfrmTradeCardInStk.StkPositionPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
//      if Self.FAuthority = '录入' then
//        FAuthority := '理单业务';
      if ShowModal = 1 then
      begin

        self.Position.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);

      end;
    end;

  finally
    frmCompanySel.Free;
  end;

end;

end.

