unit U_TradeCardInPut;

interface

uses
  Data.DB, Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
  Generics.Collections, Forms, Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin,
  cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit,
  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, Vcl.Printers, cxButtons, cxImageList, cxMemo, cxRichEdit,
  dxSkinsCore, dxSkinsDefaultPainters;

type
  TfrmTradeCardInPut = class(TfrmBaseInput)
    GPM_1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    PM_1: TPopupMenu;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    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;
    ADO_1: TADOQuery;
    RM1: TRMGridReport;
    RMDB_1: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    pnl_tjzs: TPanel;
    Label7: TLabel;
    Label9: TLabel;
    TTE_BatchNo: TcxTextEdit;
    TTE_CardQty: TcxTextEdit;
    cxButton1: TcxButton;
    cxButton3: TcxButton;
    pnl_djxm: TPanel;
    Label10: TLabel;
    Label27: TLabel;
    Label15: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label21: TLabel;
    PCQty: TcxTextEdit;
    PCGrade: TcxComboBox;
    PackWidth: TcxTextEdit;
    PackLength: TcxTextEdit;
    ActualWidth: TcxTextEdit;
    PackHeight: TcxTextEdit;
    Label22: TLabel;
    Position: TcxButtonEdit;
    Label14: TLabel;
    OrdUnit: TcxTextEdit;
    Panel4: TPanel;
    ToolBar1: TToolBar;
    ToolButton12: TToolButton;
    ToolButton1: TToolButton;
    ToolButton3: TToolButton;
    ToolButton5: TToolButton;
    ToolButton4: TToolButton;
    TBClose: TToolButton;
    Panel6: TPanel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    CMSel: TcxComboBox;
    Label4: TLabel;
    ADO_2: TADOQuery;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    ToolButton6: TToolButton;
    GPM_2: TcxGridPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    ToolButton7: TToolButton;
    pnl_JAGL: TPanel;
    Label11: TLabel;
    Label20: TLabel;
    SettlementType: TcxButtonEdit;
    Downgrade: TcxButtonEdit;
    cxButton4: TcxButton;
    Label23: TLabel;
    GrossWeight: TcxTextEdit;
    NetWeight: TcxTextEdit;
    Label24: TLabel;
    StkCoName: TcxButtonEdit;
    Label25: TLabel;
    Label26: TLabel;
    C_StyleNo: TcxTextEdit;
    Panel1: TPanel;
    cxRichEdit1: TcxRichEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v3Column8: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    Tv1Column18: TcxGridDBColumn;
    Tv1Column20: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    Tv1Column21: TcxGridDBColumn;
    Tv1Column22: TcxGridDBColumn;
    Tv1Column23: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ToolButton10: TToolButton;
    Tv1Column24: TcxGridDBColumn;
    ToolButton2: TToolButton;
    ToolButton8: TToolButton;
    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 ToolButton3Click(Sender: TObject);
    procedure cxButton1Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure PCQtyKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton6Click(Sender: TObject);
    procedure PackLengthKeyPress(Sender: TObject; var Key: Char);
    procedure PackWidthKeyPress(Sender: TObject; var Key: Char);
    procedure ActualWidthKeyPress(Sender: TObject; var Key: Char);
    procedure TV2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure DowngradePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton7Click(Sender: TObject);
    procedure cxButton4Click(Sender: TObject);
    procedure GrossWeightKeyPress(Sender: TObject; var Key: Char);
    procedure NetWeightKeyPress(Sender: TObject; var Key: Char);
    procedure PositionPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure cxButtonEdit1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton10Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
  private
    FWSql: string;
    procedure InitOrd();
    procedure InitGrid1();
    procedure InitGrid2();
    procedure PrtData(MCIID: string);
    function SaveQty(): Boolean;
    procedure SaveCard(MCardQty: Integer; MBatchNo: string);
    { Private declarations }
  public
    FMainId, FSubId, FPursId: string;
    { Public declarations }
  end;

var
  frmTradeCardInPut: TfrmTradeCardInPut;

implementation

uses
  U_DataLink, U_RTFun, U_CustomFun, U_ZDYHelp, U_LabelPrint, U_TradePlanSel,
  U_PrintLabFun, U_dynamicsDll, U_printPdf, U_CompanySel;
  {$R *.dfm}

function TfrmTradeCardInPut.SaveQty(): Boolean;
var
  MBCIOID, MBCIONo, MFtyPCID: string;
begin

  ADOQueryCmd.Connection.BeginTrans;
  try
    if CDS_1.IsEmpty then
      raise Exception.Create('请选择批号！');

    MFtyPCID := Trim(CDS_1.FieldByName('FtyPCID').AsString);

    if (CMSel.text = '全部') or (CMSel.text = '数量') then
    begin
      if StrToFloatDef(PCQty.Text, 0) = 0 then
        raise Exception.Create('请输入数量！');

      if trim(StkCoName.Text) = '' then
        raise Exception.Create('请输入仓位！');

      if trim(Position.Text) = '' then
        raise Exception.Create('请输入库位！');

    end;
    if (CMSel.text = '全部') or (CMSel.text = '体积') then
    begin
      if StrToFloatDef(PackLength.Text, 0) = 0 then
        raise Exception.Create('请输入长！');

      if StrToFloatDef(PackWidth.Text, 0) = 0 then
        raise Exception.Create('请输入宽！');

      if StrToFloatDef(PackHeight.Text, 0) = 0 then
        raise Exception.Create('请输入高！');

      if ActualWidth.Text = '' then
        raise Exception.Create('请输入全幅！');
    end;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add(' select top 1 * from BS_Cloth_IO A  ');
      sql.Add(' where  FtyPCID=' + QuotedStr(MFtyPCID));
      sql.Add(' and C_Pack=''抄码'' ');
      if Trim(FPursId) <> '' then
        sql.Add(' and IOType=''采购入库'' and FromPursId =''' + trim(FPursId) + '''')
      else
        sql.Add(' and IOType=''加工完成'' and FromSubid =''' + trim(FSubid) + '''');

      if (CMSel.text = '全部') or (CMSel.text = '数量') then
        sql.Add(' and Qty=0 and Piece=0  ');

      if (CMSel.text = '全部') or (CMSel.text = '体积') then
        sql.Add(' and  isnull(PackLength,0)=0  ');

      sql.Add(' order by  PieceNo ');
      Open;
    end;
    if ADOQueryTemp.IsEmpty then
      raise Exception.Create('当前数据无需抄码！');

    MBCIOID := ADOQueryTemp.FieldByName('BCIOID').AsString;
    MBCIONo := ADOQueryTemp.FieldByName('BCIONO').AsString;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update BS_Cloth_IO ');
      sql.Add('  set EditID=' + quotedstr(DCode));
      sql.Add(', Editer=' + quotedstr(DName));
      sql.Add(', Edittime=getdate()');
      if (CMSel.text = '全部') or (CMSel.text = '数量') then
      begin
        sql.Add(', Piece=1,Qty=' + FloatToStr(StrToFloatDef(PCQty.Text, 0)));
        sql.Add(', StkPiece=1,StkQty=' + FloatToStr(StrToFloatDef(PCQty.Text, 0)));
        sql.Add(', GrossWeight=' + FloatToStr(StrToFloatDef(GrossWeight.Text, 0)));
        sql.Add(', NetWeight=' + FloatToStr(StrToFloatDef(NetWeight.Text, 0)));

        sql.Add(', Grade=' + QuotedStr(PCGrade.Text));
        sql.Add(', StkCoNo=' + QuotedStr(StkCoName.Properties.LookupItems.Text));
        sql.Add(', StkCoName=' + QuotedStr(StkCoName.Text));
        sql.Add(', IOPosition=' + QuotedStr(Position.Text));
        sql.Add(', StkPosition=' + QuotedStr(Position.Text));
        if trim(C_StyleNo.Text) <> '' then
          sql.Add(', C_StyleNo=' + QuotedStr(C_StyleNo.Text));

      end;
      if (CMSel.text = '全部') or (CMSel.text = '体积') then
      begin
        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(' , PackVolume=CAST(ROUND((' + FloatToStr(StrToFloatDef(PackLength.Text, 0)) + '*' + FloatToStr(StrToFloatDef(PackWidth.Text, 0)) + '*' + FloatToStr(StrToFloatDef(PackHeight.Text, 0)) + '), 4) AS DECIMAL(18,4))   ');
      end;
      sql.Add('  where BCIOID=' + QuotedStr(MBCIOID));
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into Finance_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)       ');
      sql.Add('values(''贸易布坯入库''    ');
      sql.Add(',' + quotedstr(MBCIONo));
      sql.Add(',''入库''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(MBCIOID));
      sql.Add(')  ');
      execsql;
    end;

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

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

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

procedure TfrmTradeCardInPut.PCQtyKeyPress(Sender: TObject; var Key: Char);
var
  MBCIOID: string;
begin

  if Key = #13 then
  begin
    if CMSel.text = '数量' then
    begin
      if not SaveQty() then
      begin
        application.MessageBox('保存抄码失败！', '提示信息', MB_ICONERROR);
      end
      else
      begin
//        application.MessageBox('保存抄码成功！', '提示信息');
      end;
      PCQty.SetFocus;
      PCQty.Text := '';
      InitGrid1();
    end
    else
    begin
      PackLength.SetFocus;
    end;
  end;
end;

procedure TfrmTradeCardInPut.PositionPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'StkPosition';
      flagname := '库位';
      MainType := '贸易布匹';
      if ShowModal = 1 then
      begin
        Self.Position.Text := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTradeCardInPut.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.BCIOID=X.RTValue )  ');
      ExecSQL;
    end;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmTradeCardInPut.GrossWeightKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    NetWeight.SetFocus;
  end;
end;

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

procedure TfrmTradeCardInPut.InitOrd();
begin

  if Trim(FPursId) <> '' then
  begin
    Label8.Caption := '采购单';
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add(' select A.*,B.*,OrderNo=purno,OrdUnit=qtyunit');
      sql.Add(',YAPPS= (select SUM(X.Piece) from BS_Cloth_IO X where X.FromPurSId = B.PurSId and X.IOType = ''采购入库'' and STKName=''贸易布匹'')');
      sql.Add(',WAPPS= (select SUM(X.Piece)   from BS_Cloth_IO X where X.FromPurSId = B.PurSId and X.IOType = ''采购入库'' and STKName=''贸易布匹'')');
      sql.Add(' from Pur_ClothPlan_Main A inner join Pur_ClothPlan_sub B on A.PurMId=B.PurMId  ');
      sql.Add(' where B.PursId=' + QuotedStr(FPursId));
      Open;
    end;
    SCSHData(ADOQueryMain, Panel3, 0);
  end
  else
  begin
    Label8.Caption := '订单号';
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add(' select A.*,B.*');
      sql.Add(',YAPPS= (select SUM(X.Piece) from BS_Cloth_IO X where X.FromMainId = B.MainId and X.IOType = ''加工完成'' and STKName=''贸易布匹'')');
      sql.Add(',WAPPS= OrdPiece-(select SUM(X.Piece)   from BS_Cloth_IO X where X.FromMainId = B.MainId and X.IOType = ''加工完成'' and STKName=''贸易布匹'')');
      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));
      Open;
    end;
    SCSHData(ADOQueryMain, Panel3, 0);
  end;
end;

procedure TfrmTradeCardInPut.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_1, True);
end;

procedure TfrmTradeCardInPut.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_1, false);
end;

procedure TfrmTradeCardInPut.N3Click(Sender: TObject);
begin
  CopyTVFocusValue(TV1);
end;

procedure TfrmTradeCardInPut.InitGrid2();
var
  fwhere: string;
begin
  try
    ADO_2.DisableControls;
    with ADO_2 do
    begin
      Close;
      sql.Clear;
      sql.Add(' select DISTINCT FtyPCId from BS_Cloth_IO A ');
      sql.Add(' where C_Pack=''抄码'' ');

      if Trim(FPursId) <> '' then
        sql.Add(' and IOType=''采购入库'' and FromPursId =''' + trim(FPursId) + '''')
      else
        sql.Add(' and IOType=''加工完成'' and FromSubid =''' + trim(FSubid) + '''');

      Open;
    end;
    SCreateCDS(ADO_2, CDS_2);
    SInitCDSData(ADO_2, CDS_2);
  finally
    ADO_2.EnableControls;
  end;
end;

procedure TfrmTradeCardInPut.InitGrid1();
var
  fwhere: string;
begin
  if CDS_2.IsEmpty then
    Exit;

  try
    ADO_1.DisableControls;
    with ADO_1 do
    begin
      Close;
      sql.Clear;
      sql.Add(' select * from BS_Cloth_IO A');
      sql.Add(' where  FtyPCID=' + QuotedStr(Trim(CDS_2.FieldByName('FtyPCID').AsString)));
      sql.Add(' and C_Pack=''抄码'' ');
      if Trim(FPursId) <> '' then
        sql.Add(' and IOType=''采购入库'' and FromPursId =''' + trim(FPursId) + '''')
      else
        sql.Add(' and IOType=''加工完成'' and FromSubid =''' + trim(FSubid) + '''');
      sql.Add('  order by PieceNo ');
      Open;
    end;
    SCreateCDS(ADO_1, CDS_1);
    SInitCDSData(ADO_1, CDS_1);

    cxRichEdit1.Text := GetMergedResultsAsString(CDS_1);

  finally
    ADO_1.EnableControls;
  end;

end;

procedure TfrmTradeCardInPut.FormDestroy(Sender: TObject);
begin
  inherited;
  frmTradeCardInPut := nil;
end;

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

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

end;

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

end;

procedure TfrmTradeCardInPut.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '指示单管理');
//  ComboBox_Print.Properties.Items.Assign(printer.Printers);
  InitOrd();
  InitGrid2();
  InitGrid1();
end;

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

  if Key = #13 then
  begin
    if not SaveQty() then
    begin
      application.MessageBox('保存抄码失败！', '提示信息', MB_ICONERROR);
    end
    else
      application.MessageBox('保存抄码成功！', '提示信息');

    PCQty.Text := '';
    PackLength.Text := '';
    PackWidth.Text := '';
    PackHeight.Text := '';
    ActualWidth.Text := '';
    if CMSel.Text = '全部' then
      PCQty.SetFocus
    else
      PackLength.SetFocus;

    InitGrid1();

  end;
end;

procedure TfrmTradeCardInPut.cxButton1Click(Sender: TObject);
begin

  SaveCard(StrToIntDef(TTE_CardQty.Text, 0), TTE_BatchNo.Text);
  InitGrid2();
  pnl_tjzs.Visible := not pnl_tjzs.Visible;
end;

procedure TfrmTradeCardInPut.cxButton4Click(Sender: TObject);
var
  MBCIOIDS, MBCIONOS: string;
begin
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要执行操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  MBCIOIDS := SelCDSKey(CDS_1, ['BCIOID'])[0];
  MBCIONOS := SelCDSKey(CDS_1, ['BCIONO'])[0];

  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add(' select top 1 * from BS_Cloth_IO A  ');
      sql.Add(' where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(MBCIOIDS) + ','','') X where A.STKID=X.RTValue )  ');
      sql.Add(' and IOFLAG=''出库''  ');
      Open;
    end;
    if not ADOQueryTemp.IsEmpty then
      raise Exception.Create('当前数据已出库无法操作！');

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update BS_Cloth_IO ');
      sql.Add('  set EditID=' + quotedstr(DCode));
      sql.Add(', Editer=' + quotedstr(DName));
      sql.Add(', Edittime=getdate()');
      sql.Add(', Downgrade=' + quotedstr(Downgrade.Text));
      sql.Add(', SettlementType=' + quotedstr(SettlementType.Text));
      sql.Add('  where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(MBCIOIDS) + ','','') X where BS_Cloth_IO.BCIOID=X.RTValue )  ');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      sql.Add('insert into Finance_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)       ');
      sql.Add('values(''贸易布坯入库''    ');
      sql.Add(',' + quotedstr(MBCIONOS));
      sql.Add(',''结案''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(MBCIOIDS));
      sql.Add(')  ');
      ExecSQL;
    end;

    ADOQueryCmd.Connection.CommitTrans;

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

procedure TfrmTradeCardInPut.cxButtonEdit1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        self.StkCoName.Properties.LookupItems.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.StkCoName.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;

  finally
    frmCompanySel.Free;
  end;
end;

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

procedure TfrmTradeCardInPut.DowngradePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TcxButtonEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TcxButtonEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmTradeCardInPut.ToolButton10Click(Sender: TObject);
begin
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    frmLabelPrint := TfrmLabelPrint.Create(Application);
    with frmLabelPrint do
    begin
      FLMType := 'TradeLabVolume1';
      FFiltration1 := Trim(SelCDSKey(self.CDS_1, ['BCIOID'])[0]);
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmLabelPrint.Free;
  end;
end;

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

procedure TfrmTradeCardInPut.ToolButton1Click(Sender: TObject);
begin
  pnl_tjzs.Visible := not pnl_tjzs.Visible;
end;

procedure TfrmTradeCardInPut.ToolButton2Click(Sender: TObject);
var
  MBCIOIDS, Maxno: string;
begin
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要执行操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  MBCIOIDS := SelCDSKey(CDS_1, ['BCIOID'])[0];

  if not GetLSNo(ADOQueryCmd, Maxno, 'MR', 'BS_Cloth_IO', 4, 1) then
  begin
    Application.MessageBox('创建入库单失败!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add(' select top 1 * from BS_Cloth_IO A  ');
      sql.Add(' where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(MBCIOIDS) + ','','') X where A.STKID=X.RTValue )  ');
      sql.Add(' and IOFLAG=''出库''  ');
      Open;
    end;
    if not ADOQueryTemp.IsEmpty then
      raise Exception.Create('当前数据已出库无法操作！');

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update BS_Cloth_IO ');
      sql.Add('  set EditID=' + quotedstr(DCode));
      sql.Add(', Editer=' + quotedstr(DName));
      sql.Add(', Edittime=getdate()');
      sql.Add(', BCIONO=' + quotedstr(Maxno));
      sql.Add('  where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(MBCIOIDS) + ','','') X where BS_Cloth_IO.BCIOID=X.RTValue )  ');
      sql.Add('  and  isnull(BCIONO,'''')=''000000'' ');
      ExecSQL;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into Finance_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)       ');
      sql.Add('values(''贸易布坯入库''    ');
      sql.Add(',' + quotedstr(Maxno));
      sql.Add(',''生成财务''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(MBCIOIDS));
      sql.Add(')  ');
      execsql;
    end;
    ADOQueryCmd.Connection.CommitTrans;

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

procedure TfrmTradeCardInPut.ToolButton3Click(Sender: TObject);
var
  MBCIOIDS, MBCIONOS: string;
begin

  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  MBCIOIDS := SelCDSKey(CDS_1, ['BCIOID'])[0];
  MBCIONOS := SelCDSKey(CDS_1, ['BCIONO'])[0];
  if Application.MessageBox('确定要执行操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_Fin_Flow_Judge ');
    Sql.Add(' @FFIDS=' + quotedstr(MBCIOIDS));
    Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
    Sql.Add(',@DName=' + quotedstr(Trim(DName)));
    Open;
  end;
  if ADOQueryTemp.FieldByName('intReturn').AsInteger = -1 then
  begin
    Application.MessageBox(PChar(ADOQueryTemp.fieldbyname('ShowMsg').AsString), '提示', 0);
    exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      sql.Add('insert into Finance_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)       ');
      sql.Add('values(''贸易布坯入库''    ');
      sql.Add(',' + quotedstr(MBCIONOS));
      sql.Add(',''删除''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(MBCIOIDS));
      sql.Add(')  ');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('exec P_BS_Cloth_In_Del ');
      Sql.Add(' @BCIOIDS=' + quotedstr(MBCIOIDS));
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(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 TfrmTradeCardInPut.ToolButton4Click(Sender: TObject);
var
  MBCIOIDS, MBCIONOS: string;
begin
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要执行操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  MBCIOIDS := SelCDSKey(CDS_1, ['BCIOID'])[0];
  MBCIONOS := SelCDSKey(CDS_1, ['BCIONO'])[0];

  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add(' select top 1 * from BS_Cloth_IO A  ');
      sql.Add(' where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(MBCIOIDS) + ','','') X where A.STKID=X.RTValue )  ');
      sql.Add(' and IOFLAG=''出库''  ');
      Open;
    end;
    if not ADOQueryTemp.IsEmpty then
      raise Exception.Create('当前数据已出库无法操作！');

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update BS_Cloth_IO ');
      sql.Add('  set EditID=' + quotedstr(DCode));
      sql.Add(', Editer=' + quotedstr(DName));
      sql.Add(', Edittime=getdate()');
      sql.Add(', PackLength=null,PackWidth=null,PackHeight=null,PackVolume=null,ActualWidth=null ');
      sql.Add('  where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(MBCIOIDS) + ','','') X where BS_Cloth_IO.BCIOID=X.RTValue )  ');
      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(MBCIONOS));
      sql.Add(',''撤销入库''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(MBCIOIDS));
      sql.Add(')  ');
      execsql;
    end;
    ADOQueryCmd.Connection.CommitTrans;

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

procedure TfrmTradeCardInPut.ToolButton5Click(Sender: TObject);
begin
  pnl_djxm.Visible := not pnl_djxm.Visible;
end;

procedure TfrmTradeCardInPut.ToolButton6Click(Sender: TObject);
var
  MBCIOIDS, MBCIONOS: string;
begin
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要执行操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  MBCIOIDS := SelCDSKey(CDS_1, ['BCIOID'])[0];
  MBCIONOS := SelCDSKey(CDS_1, ['BCIONO'])[0];
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_Fin_Flow_Judge ');
    Sql.Add(' @FFIDS=' + quotedstr(MBCIOIDS));
    Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
    Sql.Add(',@DName=' + quotedstr(Trim(DName)));
    Open;
  end;
  if ADOQueryTemp.FieldByName('intReturn').AsInteger = -1 then
  begin
    Application.MessageBox(PChar(ADOQueryTemp.fieldbyname('ShowMsg').AsString), '提示', 0);
    exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add(' select top 1 * from BS_Cloth_IO A  ');
      sql.Add(' where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(MBCIOIDS) + ','','') X where A.STKID=X.RTValue )  ');
      sql.Add(' and IOFLAG=''出库''  ');
      Open;
    end;
    if not ADOQueryTemp.IsEmpty then
      raise Exception.Create('当前数据已出库无法操作！');

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update BS_Cloth_IO ');
      sql.Add('  set EditID=' + quotedstr(DCode));
      sql.Add(', Editer=' + quotedstr(DName));
      sql.Add(', Edittime=getdate()');
      sql.Add(', Piece=0,Qty=0, StkPiece=0,StkQty=0,NetWeight=0,GrossWeight=0,IOPosition=NULL,Grade=NULL');
      sql.Add('  where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(MBCIOIDS) + ','','') X where BS_Cloth_IO.BCIOID=X.RTValue )  ');
      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(MBCIONOS));
      sql.Add(',''撤销入库''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(MBCIOIDS));
      sql.Add(')  ');
      execsql;
    end;
    ADOQueryCmd.Connection.CommitTrans;

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

procedure TfrmTradeCardInPut.ToolButton7Click(Sender: TObject);
begin
  pnl_JAGL.Visible := not pnl_JAGL.Visible;
end;

procedure TfrmTradeCardInPut.ToolButton8Click(Sender: TObject);
var
  MBCIOIDS, MBCIONOS: string;
begin
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要执行操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  MBCIOIDS := SelCDSKey(CDS_1, ['BCIOID'])[0];
  MBCIONOS := SelCDSKey(CDS_1, ['BCIONO'])[0];
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_Fin_Flow_Judge ');
    Sql.Add(' @FFIDS=' + quotedstr(MBCIOIDS));
    Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
    Sql.Add(',@DName=' + quotedstr(Trim(DName)));
    Open;
  end;
  if ADOQueryTemp.FieldByName('intReturn').AsInteger = -1 then
  begin
    Application.MessageBox(PChar(ADOQueryTemp.fieldbyname('ShowMsg').AsString), '提示', 0);
    exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add(' select top 1 * from BS_Cloth_IO A  ');
      sql.Add(' where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(MBCIOIDS) + ','','') X where A.STKID=X.RTValue )  ');
      sql.Add(' and IOFLAG=''出库''  ');
      Open;
    end;
    if not ADOQueryTemp.IsEmpty then
      raise Exception.Create('当前数据已出库无法操作！');

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update BS_Cloth_IO ');
      sql.Add('  set EditID=' + quotedstr(DCode));
      sql.Add(', Editer=' + quotedstr(DName));
      sql.Add(', Edittime=getdate()');
      sql.Add(', BCIONO=''000000'' ');
      sql.Add('  where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(MBCIOIDS) + ','','') X where BS_Cloth_IO.BCIOID=X.RTValue )  ');
      ExecSQL;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into Finance_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)       ');
      sql.Add('values(''贸易布坯入库''    ');
      sql.Add(',' + quotedstr(MBCIONOS));
      sql.Add(',''撤销财务''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(MBCIOIDS));
      sql.Add(')  ');
      execsql;
    end;
    ADOQueryCmd.Connection.CommitTrans;

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

procedure TfrmTradeCardInPut.TV2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitGrid1();
end;

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

  if MCardQty = 0 then
    Exit;

  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('exec P_BS_Cloth_In_ChaoMa ');
      if Trim(FPursId) <> '' then
        Sql.Add(' @PursId=' + quotedstr(Trim(FPursId)))
      else
        Sql.Add(' @Subid=' + quotedstr(Trim(FSubid)));

      Sql.Add(',@FtyPCID=' + quotedstr(Trim(MBatchNo)));
      Sql.Add(',@Piece=' + (Trim(IntToStr(MCardQty))));
      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
    else
    begin
      FWSql := trim(ADOQueryCmd.FieldByName('BCIOIDS').AsString);
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid1();
    application.MessageBox('保存成功！', '提示信息');
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

end.

