unit U_TradeClothTotalInInput1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  System.Types, Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxCalendar, cxDropDownEdit, ComCtrls,
  ToolWin, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, ADODB, DBClient, cxButtonEdit,
  cxTextEdit, StdCtrls, ExtCtrls, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, dxDateRanges, dxBarBuiltInMenu, U_BaseInput, System.ImageList,
  Vcl.ImgList, ComObj, dxScrollbarAnnotations, cxContainer, dxCore, cxDateUtils,
  cxMaskEdit, cxImageList, cxCurrencyEdit, Vcl.Menus, Clipbrd;

type
  TfrmTradeClothTotalInInput1 = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    GPM_1: TcxGridPopupMenu;
    CDS_LM: TClientDataSet;
    OpenDialog1: TOpenDialog;
    Panel1: TPanel;
    Label12: TLabel;
    Label1: TLabel;
    IOType: TcxComboBox;
    IOTime: TcxDateEdit;
    Label14: TLabel;
    FromCoName: TcxButtonEdit;
    Label2: TLabel;
    Label5: TLabel;
    Label15: TLabel;
    Label22: TLabel;
    Label4: TLabel;
    Label7: TLabel;
    Label6: TLabel;
    Label11: TLabel;
    Label10: TLabel;
    Label16: TLabel;
    Label21: TLabel;
    C_Name: TcxTextEdit;
    C_Color: TcxTextEdit;
    FtyPCId: TcxTextEdit;
    C_EName: TcxTextEdit;
    C_Code: TcxButtonEdit;
    Label27: TLabel;
    QtyUnit: TcxComboBox;
    Label3: TLabel;
    StkCoName: TcxButtonEdit;
    C_EComposition: TcxTextEdit;
    C_GramWeight: TcxTextEdit;
    C_Composition: TcxTextEdit;
    C_Width: TcxTextEdit;
    C_Spec: TcxTextEdit;
    C_Pattern: TcxTextEdit;
    Label8: TLabel;
    C_Colorno: TcxTextEdit;
    pnl1: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    tlb1: TToolBar;
    btn1: TToolButton;
    btn3: TToolButton;
    btn4: TToolButton;
    pnl2: TPanel;
    inPiece: TcxCurrencyEdit;
    Label9: TLabel;
    inQty: TcxCurrencyEdit;
    Label13: TLabel;
    Label17: TLabel;
    C_Degree: TcxComboBox;
    Label18: TLabel;
    BCIONO: TcxTextEdit;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    btndaoru: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    btn2: TToolButton;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBAddClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1Column14PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column16PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure FromCoNameDblClick(Sender: TObject);
    procedure FromCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure C_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure inQtyKeyPress(Sender: TObject; var Key: Char);
    procedure StkCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure btndaoruClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure btn2Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
  private
    { Private declarations }
    function SaveCKData(): Boolean;
    procedure JSKC();
    procedure NianTie();
    function Split(const s: string; Separator: char): TStringDynArray;
  public
    { Public declarations }
    FBCId, canshu3, FSTKName, FKHName: string;
  end;

var
  frmTradeClothTotalInInput1: TfrmTradeClothTotalInInput1;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_CompanySel, U_TradePlanSel, U_ClothInfoSel,
  U_RPFun;

{$R *.dfm}

function TfrmTradeClothTotalInInput1.Split(const s: string; Separator: char): TStringDynArray;
var
  i, ItemIndex: Integer;
  len: Integer;
  SeparatorCount: Integer;
  Start: Integer;
begin
  len := Length(s);
  if len = 0 then
  begin
    Result := nil;
    exit;
  end;
  SeparatorCount := 0;
  for i := 1 to len do
  begin
    if s[i] = Separator then
    begin
      inc(SeparatorCount);
    end;
  end;
  SetLength(Result, SeparatorCount + 1);
  ItemIndex := 0;
  Start := 1;
  for i := 1 to len do
  begin
    if s[i] = Separator then
    begin
      Result[ItemIndex] := Copy(s, Start, i - Start);
      inc(ItemIndex);
      Start := i + 1;
    end;
  end;
  Result[ItemIndex] := Copy(s, Start, len - Start + 1);
end;

procedure TfrmTradeClothTotalInInput1.N1Click(Sender: TObject);
var
  hmem: THandle;
  ClipboardText: PChar;
  Lines: TStringDynArray;
  Cells: TStringDynArray;
  i: Integer;
begin
  // 假设已经从剪贴板获取到了文本，存放在ClipboardText变量中

  OpenClipboard(0);
  hmem := GetClipboardData(CF_TEXT);

  ClipboardText := GlobalLock(hmem);

  // 首先按换行符拆分成行
  Lines := Split(ClipboardText, #13); // Windows系统中换行符通常是#13#10

  // 遍历每一行
  for i := 0 to Length(Lines) - 1 do
  begin
    // 按逗号拆分成单元格数据
    Cells := Split(Lines[i], ',');

    // 此时Cells数组中就存储了每一行的单元格数据
    // 例如第一行拆分后，Cells[0]='姓名'，Cells[1]='年龄'，Cells[2]='性别'
    // 可以根据需要将这些数据导入到cxGrid中
  end;
end;

procedure TfrmTradeClothTotalInInput1.NianTie();
var
  hmem: THandle;
  pstr: PChar;
  ArrStr: TStringList;
  i, x, h, Row, Z: Integer;
  FColumn: string;
begin
  //检查剪贴板类容类型
  if IsClipboardFormatAvailable(CF_TEXT) then
  begin
    OpenClipboard(0);
    hmem := GetClipboardData(CF_TEXT);
    pstr := GlobalLock(hmem);
//    Memo1.Text := pstr;
    pstr := PChar(StringReplace(pstr, #$A, #$A#$3F, [rfReplaceAll]));
//    pstr := PAnsiChar(StringReplace(pstr, #$D#$A#$D, #$D#$A#$20#$D, [rfReplaceAll]));
    ArrStr := TStringList.Create;
    ArrStr.Clear;
    ExtractStrings([#13], [], PChar(pstr), ArrStr);
    GlobalUnlock(hmem);
    CloseClipboard;
  end;
  ArrStr.Delete(ArrStr.Count - 1);

  Row := Tv1.Controller.FocusedRowIndex;

  CDS_Sub.Locate('xhno', Row + 1, []);

  x := (Tv1.DataController.RowCount - (Row + 1));

//  showmessage(ArrStr.Text);
  //自动增行
  if x < ArrStr.count then
  begin
    for i := x to ArrStr.count - 2 do
    begin
      if VarIsNull(Tv1.DataController.Summary.FooterSummaryValues[0]) = True then
      begin
        h := 0;
      end
      else
        h := Tv1.DataController.Summary.FooterSummaryValues[0];
      h := h + 1;
      with CDS_Sub do
      begin
        Append;
        FieldByName('XHNO').Value := IntToStr(h);
        Post;
      end;
    end;
  end;

  CDS_Sub.Locate('xhno', Row + 1, []);
  i := 0;
  Z := 0;

  FColumn := Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;

  with CDS_Sub do
  begin
    DisableControls;
    while not Eof do
    begin
      edit;
      if ArrStr.Count > i then
      begin
        CDS_Sub.FieldByName(FColumn).Value := StringReplace(ArrStr[i], '?', '', [rfReplaceAll]);
      end
      else
        Break;
      i := i + 1;
      Post;
      Next;
    end;
    EnableControls;
  end;
end;

procedure TfrmTradeClothTotalInInput1.JSKC();
begin

  with CDS_Sub do
  begin
    DisableControls;
    First;
    while not Eof do
    begin
      Edit;
      FieldByName('Qty').Value := StrTofloatDef(inQty.Text, 0);

      if QtyUnit.Text = 'M' then
      begin
        FieldByName('Qty').Value := FieldByName('Meter').Value;
      end
      else if QtyUnit.Text = 'Y' then
      begin
        FieldByName('Qty').Value := FieldByName('Yardage').Value;
      end
      else
      begin
        FieldByName('Qty').Value := FieldByName('NetWeight').Value;
      end;
      Post;

      Next;
    end;
    EnableControls;
  end;

end;

procedure TfrmTradeClothTotalInInput1.btn2Click(Sender: TObject);
var
  RowData: TArray<string>;
  rowColData: TArray<string>;
  RowIndex, ColIndex, i: Integer;
  clipboardText, rowStr: string;
  lRect: TRect;
begin
  if Clipboard.HasFormat(CF_TEXT) then
  begin
    clipboardText := Clipboard.AsText;
    RowData := clipboardText.Split([#13#10]); // 根据换行符拆分行数据
      // 遍历行数据
    for RowIndex := Low(RowData) to High(RowData) do
    begin
      rowStr := RowData[RowIndex];
      if trim(rowStr) = '' then
        continue;
      rowColData := rowStr.Split([#9]);
      ShowMessage(rowStr);
//      lRect := FGrid.Selection;
//      ColIndex := 0;
//      for i := lRect.Left to lRect.Right do
//      begin
//        if lRect.Top + RowIndex < FGrid.RowCount then
//        begin
//          FGrid.Cells[i, lRect.Top + RowIndex].Text := trim(rowColData[ColIndex]);
//          ColIndex := ColIndex + 1;
//        end;
//      end;
    end;
  end;
end;

procedure TfrmTradeClothTotalInInput1.btndaoruClick(Sender: TObject);
var
  excelApp, WorkBook: Variant;
  i, j, k, LX, ExcelRowCount: integer;
  maxId, FCPID, FCPName, t1, t2, t3, FFID: string;
begin

  try
    excelApp := CreateOleObject('Excel.Application');
    openDialog1.Filter := '*.CSV;*.xls';
    if opendialog1.Execute then
    begin
      WorkBook := excelApp.WorkBooks.Open(OpenDialog1.FileName);
    end
    else
      exit;
    excelApp.Visible := false;
    ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count;
  except
    application.MessageBox('加载EXCEL错误！', '错误信息', MB_ICONERROR);
    exit;
  end;

  CDS_LM.EmptyDataSet;

  for j := 0 to Tv1.ColumnCount - 1 do
  begin
    with CDS_LM do
    begin
      Append;
      FieldByName('LCode').Value := trim(Tv1.Columns[j].DataBinding.FieldName);
      FieldByName('LName').Value := trim(Tv1.Columns[j].Caption);
      Post;
    end;
  end;

  try
    for i := 1 to 50 do
    begin
      if trim(WorkBook.WorkSheets[1].Cells[1, i].value) = '' then
        continue;
      if CDS_LM.Locate('LName', trim(WorkBook.WorkSheets[1].Cells[1, i].value), []) then
      begin
        with CDS_LM do
        begin
          Edit;
          FieldByName('LXH').Value := i;
          Post;
        end;
      end;
    end;
  except
    application.MessageBox('加载数组错误！', '错误信息', MB_ICONERROR);
    exit;
  end;

  try
    for i := 2 to ExcelRowCount do
    begin
      with CDS_Sub do
      begin
        Append;
        CDS_LM.First;
        while not CDS_LM.Eof do
        begin
          if CDS_LM.FieldByName('LXH').AsInteger > 0 then
            CDS_Sub.fieldbyname(CDS_LM.FieldByName('LCode').AsString).Value := WorkBook.WorkSheets[1].Cells[i, CDS_LM.FieldByName('LXH').AsInteger].Value;
          CDS_LM.Next;
        end;
        Post;
      end;
    end;
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
  except
    WorkBook.Close;
    excelApp.Quit;
    excelApp := Unassigned;
    WorkBook := Unassigned;
    exit;
  end;

end;

procedure TfrmTradeClothTotalInInput1.C_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin

  frmClothInfoSel := TfrmClothInfoSel.Create(self);
  with frmClothInfoSel do
  begin

    if showmodal = 1 then
    begin
      self.C_Code.text := frmClothInfoSel.CDS_1.fieldbyname('C_Code').asstring;
      self.C_Name.text := frmClothInfoSel.CDS_1.fieldbyname('C_Name').asstring;
      self.C_EName.text := frmClothInfoSel.CDS_1.fieldbyname('C_EName').asstring;
      self.C_Spec.text := frmClothInfoSel.CDS_1.fieldbyname('C_Spec').asstring;
      self.C_Composition.text := frmClothInfoSel.CDS_1.fieldbyname('C_Composition').asstring;
      self.C_EComposition.text := frmClothInfoSel.CDS_1.fieldbyname('C_EComposition').asstring;
      self.C_Width.text := frmClothInfoSel.CDS_1.fieldbyname('C_Width').asstring;
      self.C_GramWeight.text := frmClothInfoSel.CDS_1.fieldbyname('C_GramWeight').asstring;

      self.C_Color.text := frmClothInfoSel.CDS_3.fieldbyname('C_Color').asstring;
      self.C_Colorno.text := frmClothInfoSel.CDS_3.fieldbyname('C_Colorno').asstring;
      QtyUnit.text := 'M';
    end;
    free;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

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

procedure TfrmTradeClothTotalInInput1.TBAddClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty = False then
  begin
    CopyAddRowCDS(CDS_Sub);
    with CDS_Sub do
    begin
      Edit;
      FieldByName('BCIOID').Value := null;
      FieldByName('StkCoNo').Value := '本厂仓库';
      FieldByName('StkCoName').Value := '本厂仓库';
      post;
    end;
  end
  else
  begin
    with CDS_Sub do
    begin
      Append;
      FieldByName('IOTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
      FieldByName('StkCoNo').Value := '本厂仓库';
      FieldByName('StkCoName').Value := '本厂仓库';
      FieldByName('QtyUnit').Value := 'M';
      Post;
    end;
  end;

end;

function TfrmTradeClothTotalInInput1.SaveCKData(): Boolean;
var
  Maxno, MBCIOID: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not eof do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from BS_Cloth_IO where BCIOID=''' + Trim(CDS_Sub.fieldbyname('BCIOID').AsString) + '''');
          open;
        end;
        MBCIOID := Trim(ADOQueryTemp.fieldbyname('BCIOID').AsString);
        if Trim(MBCIOID) = '' then
        begin
          if not GetLSNo(ADOQueryCmd, Maxno, 'MR', 'BS_Cloth_IO', 4, 1) then
            raise Exception.Create('取染色坯布入库编号失败！');
        end
        else
        begin
          Maxno := Trim(MBCIOID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from BS_Cloth_IO where BCIOID=''' + Trim(Maxno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(MBCIOID) = '' then
          begin
            Append;
            FieldByName('Fillid').Value := Trim(Dcode);
            FieldByName('Filler').Value := Trim(DName);
            FieldByName('BCIOID').Value := Trim(Maxno);
            FieldByName('STKID').Value := Trim(Maxno);
            FieldByName('CIID').Value := Trim(Maxno);
          end
          else
          begin
            Edit;
            FieldByName('Editid').Value := Trim(Dcode);
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDate(ADOQueryTemp);
          end;
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'BS_Cloth_IO', 0);
          RTSetsavedata(ADOQueryCmd, 'BS_Cloth_IO', Panel1, 0);
          FieldByName('FromCoNo').Value := Trim(FromCoName.Properties.LookupItems.Text);
          FieldByName('CustNo').Value := CDS_Sub.fieldbyname('CustNo').Value;
          FieldByName('FromCoNo').Value := CDS_Sub.fieldbyname('FromCoNo').Value;
          FieldByName('StkCoNo').Value := Trim(StkCoName.Properties.LookupItems.Text);
          FieldByName('FromMainId').Value := CDS_Sub.fieldbyname('FromMainId').Value;
          FieldByName('FromSubId').Value := CDS_Sub.fieldbyname('FromSubId').Value;
          FieldByName('STKName').Value := FSTKName;
          FieldByName('IOFlag').Value := '入库';
          FieldByName('IOQtyFlag').Value := 1;
          FieldByName('Qty').Value := CDS_Sub.FieldByName('Qty').Value;
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update BS_Cloth_IO Set StkPiece=(select Sum(isnull(Piece,0)*IOQtyFlag) from BS_Cloth_IO A where A.STKID=BS_Cloth_IO.BCIOID )');
          sql.Add(',StkQty=(select Sum(isnull(Qty,0)*IOQtyFlag) from BS_Cloth_IO A where A.STKID=BS_Cloth_IO.BCIOID )');
          sql.Add(' where BCIOID=''' + Trim(Maxno) + '''');
          ExecSQL;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select stkQty,StkPiece from BS_Cloth_IO where BCIOID=''' + Trim(Maxno) + '''');
          Open;
        end;
        if ADOQueryTemp.FieldByName('stkQty').Value < 0 then
          raise Exception.Create('入库数量小于出库数量不能保存!');
        if ADOQueryTemp.FieldByName('StkPiece').Value < 0 then
          raise Exception.Create('入库匹数小于出库匹数不能保存!');
        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(Maxno));
          sql.Add(')  ');
          ExecSQL;
        end;
        Edit;
        FieldByName('BCIOID').Value := Trim(Maxno);
        Post;
        Next;
      end;
    end;

    CDS_Sub.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;

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

end;

procedure TfrmTradeClothTotalInInput1.StkCoNamePropertiesButtonClick(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 TfrmTradeClothTotalInInput1.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmTradeClothTotalInInput1.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(FSTKName + '入库登记', Tv1, '贸易汇总仓库');

  IOTime.Date := SGetServerDate(ADOQueryTemp);
//  IOType.text :='采购入库';

  with CDS_LM do
  begin
    FieldDefs.Clear;
    FieldDefs.Add('LXH', ftInteger, 0);
    FieldDefs.Add('lCode', ftString, 40);
    FieldDefs.Add('LName', ftString, 40);
    close;
    CreateDataSet;
  end;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from BS_Cloth_IO A');
    sql.Add(' where BCIOID=''' + Trim(FBCId) + '''');
    Open;
  end;
  SCSHData(ADOQueryTemp, Panel1, 0);
  FromCoName.Properties.LookupItems.Text := Trim(ADOQueryTemp.FieldByName('FromCoNo').AsString);
  StkCoName.Properties.LookupItems.Text := Trim(ADOQueryTemp.FieldByName('StkCoNo').AsString);
  SCreateCDS(ADOQueryTemp, CDS_Sub);
  SInitCDSData(ADOQueryTemp, CDS_Sub);
end;

procedure TfrmTradeClothTotalInInput1.FromCoNameDblClick(Sender: TObject);
begin
  TcxButtonEdit(Sender).Text := '';
  TcxButtonEdit(Sender).Properties.LookupItems.Text := '';
end;

procedure TfrmTradeClothTotalInInput1.FromCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      if IoType.text = '销售退回' then
      begin
        FCoType := '客户';
      end
      else
      begin
        FCoType := '供应商';
      end;
      if ShowModal = 1 then
      begin
        self.FromCoName.Properties.LookupItems.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.FromCoName.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmTradeClothTotalInInput1.inQtyKeyPress(Sender: TObject; var Key: Char);
var
  i: Integer;
begin
  if Key = #13 then
  begin
    with CDS_Sub do
    begin
      DisableControls;
      for i := 1 to StrToIntDef(inPiece.Text, 1) do
      begin
        Append;
        FieldByName('Qty').Value := StrTofloatDef(inQty.Text, 0);

        if QtyUnit.Text = 'M' then
        begin
          FieldByName('Meter').Value := StrTofloatDef(inQty.Text, 0);
        end
        else if QtyUnit.Text = 'Y' then
        begin
          FieldByName('Yardage').Value := StrTofloatDef(inQty.Text, 0);
        end
        else
        begin
          FieldByName('NetWeight').Value := StrTofloatDef(inQty.Text, 0);
        end;

        Post;
      end;
      EnableControls;
    end;
    inPiece.Text := '1';
    inQty.Text := '';
  end;
end;

procedure TfrmTradeClothTotalInInput1.TBDelClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if Trim(CDS_Sub.fieldbyname('BCIOID').AsString) <> '' then
  begin
    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(Trim(CDS_Sub.fieldbyname('BCIOID').AsString)));
      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(Trim(CDS_Sub.fieldbyname('BCIOID').AsString)));
        sql.Add(',''删除''   ');
        sql.Add(',' + quotedstr(DName));
        sql.Add(',' + quotedstr(trim(self.Caption)));
        sql.Add(',' + quotedstr(Trim(CDS_Sub.fieldbyname('BCIOID').AsString)));
        sql.Add(')  ');

        Sql.Add('exec P_BS_Cloth_In_Del ');
        Sql.Add(' @BCIOIDS=' + quotedstr(Trim(CDS_Sub.fieldbyname('BCIOID').AsString)));
        Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
        Sql.Add(',@DName=' + quotedstr(Trim(DName)));
        Open;
      end;
      if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
        raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
      ADOQueryCmd.Connection.CommitTrans;
      CDS_Sub.Delete;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    end;
  end
  else
  begin
    CDS_Sub.Delete;
  end;

end;

procedure TfrmTradeClothTotalInInput1.TBSaveClick(Sender: TObject);
var
  Maxno: string;
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if not GetLSNo(ADOQueryCmd, Maxno, 'BRO', 'BS_Cloth_IO', 4, 1) then
  begin
    Application.MessageBox('取出库单号失败!', '提示', 0);
    Exit;
  end
  else
    BCIONO.Text := Maxno;

  with CDS_Sub do
  begin
    DisableControls;
    First;
    while not Eof do
    begin

      Edit;
      if QtyUnit.Text = 'M' then
      begin
        FieldByName('Qty').Value := FieldByName('Meter').Value;
      end
      else if QtyUnit.Text = 'Y' then
      begin
        FieldByName('Qty').Value := FieldByName('Yardage').Value;
      end
      else
      begin
        FieldByName('Qty').Value := FieldByName('NetWeight').Value;
      end;

      Post;
      Next;
    end;
    EnableControls;
  end;

  if trim(IOType.text) = '' then
  begin
    Application.MessageBox('入库类型不能为空!', '提示', 0);
    Exit;
  end;
  if trim(C_Code.text) = '' then
  begin
    Application.MessageBox('产品编号不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('Qty', null, []) = True then
  begin
    Application.MessageBox('数量不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('Qty', 0, []) = True then
  begin
    Application.MessageBox('数量不能为0!', '提示', 0);
    Exit;
  end;
  if SaveCKData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    //ModalResult:=1;
    Exit;
  end;
end;

procedure TfrmTradeClothTotalInInput1.ToolButton1Click(Sender: TObject);
begin
  if CDS_Sub.Locate('BCIOID', '', []) = True then
  begin
    Application.MessageBox('有数据未保存请先保存!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('BCIOID', null, []) = True then
  begin
    Application.MessageBox('有数据未保存请先保存!', '提示', 0);
    Exit;
  end;

  BCIONO.Text := '';

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from BS_Cloth_IO A');
    sql.Add(' where 1<>1');
    Open;
  end;
  SCSHData(ADOQueryTemp, Panel1, 0);
  SCreateCDS(ADOQueryTemp, CDS_Sub);
  SInitCDSData(ADOQueryTemp, CDS_Sub);
end;

procedure TfrmTradeClothTotalInInput1.ToolButton2Click(Sender: TObject);
var
  MFiltration: string;
  MPrintJson: string;
begin
  if CDS_sub.IsEmpty then
    Exit;

  if CDS_Sub.Locate('BCIOID', '', []) = True then
  begin
    Application.MessageBox('有数据未保存请先保存!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('BCIOID', null, []) = True then
  begin
    Application.MessageBox('有数据未保存请先保存!', '提示', 0);
    Exit;
  end;
  MFiltration := '';
  with CDS_sub do
  begin

    First;
    while not eof do
    begin

      MFiltration := MFiltration + ',' + CDS_sub.FieldByName('BCIOID').AsString;

      next;
    end;

  end;
//     MFiltration:=
//  MFiltration := SelCDSKey(CDS_sub, ['BCIOID'])[0];
  MPrintJson := '              {"LMType": "TradeFinishClothInPrt1"    ';
  MPrintJson := MPrintJson + ' ,"PreviewPrint": true      ';
  MPrintJson := MPrintJson + ' ,"PrtArgs": [ { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '" }, ';
  MPrintJson := MPrintJson + '               { "IsSql": false, "Filtration":" ' + MFiltration + '"  } ] }    ';
  FromPrintFr3(Application, PChar(DConString), PChar(MPrintJson));
end;

procedure TfrmTradeClothTotalInInput1.Tv1Column16PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '客户';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('CustNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
          FieldByName('CustName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
        end;
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmTradeClothTotalInInput1.Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('StkCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
          FieldByName('StkCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
        end;
      end;
    end;
  finally
    frmCompanySel.Free;
  end;

end;

procedure TfrmTradeClothTotalInInput1.Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmTradePlanSel := TfrmTradePlanSel.Create(Application);
    with frmTradePlanSel do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('FromCoNo').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('FtyNo').AsString);
          FieldByName('FromCoName').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('FtyName').AsString);
          FieldByName('FromOrdNo').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('OrderNo').AsString);
          FieldByName('FromMainId').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('MainId').AsString);
          FieldByName('FromSubId').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('SubId').AsString);
          FieldByName('C_Code').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('C_Code').AsString);
          FieldByName('C_Name').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('C_Name').AsString);
          FieldByName('C_Spec').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('C_Spec').AsString);
          FieldByName('C_Composition').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('C_Composition').AsString);
          FieldByName('C_Width').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('C_Width').AsString);
          FieldByName('C_GramWeight').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('C_GramWeight').AsString);
          FieldByName('C_Color').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('C_Color').AsString);
          FieldByName('C_ColorNo').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('C_ColorNo').AsString);
          FieldByName('C_Pattern').Value := Trim(frmTradePlanSel.CDS_1.fieldbyname('C_Pattern').AsString);
        end;
      end;
    end;
  finally
    frmTradePlanSel.Free;
  end;

end;

procedure TfrmTradeClothTotalInInput1.Tv1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmClothInfoSel := TfrmClothInfoSel.Create(Application);
    with frmClothInfoSel do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          edit;
          FieldByName('C_Code').Value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Code').AsString);
          FieldByName('C_Name').Value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Name').AsString);
          FieldByName('C_Spec').Value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Spec').AsString);
          FieldByName('C_Composition').Value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Composition').AsString);
          FieldByName('C_Width').Value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Width').AsString);
          FieldByName('C_GramWeight').Value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_GramWeight').AsString);

          Fieldbyname('C_ColorNo').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_ColorNo').AsString);
          Fieldbyname('C_Color').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Color').AsString);
          Fieldbyname('C_Pattern').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('C_Pattern').AsString);
          Fieldbyname('GC_Name').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('GC_Name').AsString);
          Fieldbyname('CraftCode').value := trim(frmClothInfoSel.CDS_1.Fieldbyname('CraftCode').AsString);
        end;

      end;
    end;
  finally
    frmClothInfoSel.Free;
  end;

end;

procedure TfrmTradeClothTotalInInput1.v1Column14PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin

  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('FromCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
          FieldByName('FromCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);

          FieldByName('SalesER').Value := Trim(CDS_1.fieldbyname('SalesER').AsString);
        end;
      end;
    end;
  finally
    frmCompanySel.Free;
  end;

end;

procedure TfrmTradeClothTotalInInput1.v2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'JWLYCLName';
      flagname := '品名';
      MainType := FSTKName;
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('C_Name').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;

end;

procedure TfrmTradeClothTotalInInput1.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'StkPosition';
      flagname := '库位';
      MainType := FSTKName;
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('StkPosition').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTradeClothTotalInInput1.v1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'C_Spec';
      flagname := '规格';
      MainType := FSTKName;
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('C_Spec').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.

