unit U_DyeGreyClothInEdit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  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, dxSkinsCore,
  dxSkinsDefaultPainters, dxDateRanges, dxBarBuiltInMenu, U_BaseInput,
  System.ImageList, Vcl.ImgList, dxSkinWXI, dxScrollbarAnnotations;

type
  TfrmDyeGreyClothInEdit = class(TfrmBaseInput)
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBSave: TToolButton;
    TBClose: TToolButton;
    v1Column4: TcxGridDBColumn;
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    v1Column12: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1SPMF: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Tv1Column4: TcxGridDBColumn;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    Tv1Column12: TcxGridDBColumn;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ToolButton7: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1ioTime: TcxGridDBColumn;
    Tv1StkCoName: TcxGridDBColumn;
    Tv1StkCoNo: TcxGridDBColumn;
    Tv1QtyUnit: TcxGridDBColumn;
    Tv1OrderNo: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1C_Code: TcxGridDBColumn;
    TbImport: TToolButton;
    Tv1F_description: TcxGridDBColumn;
    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 v2Column6PropertiesEditValueChanged(Sender: TObject);
    procedure Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1OrderNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TbImportClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  private
    { Private declarations }
    function SaveCKData(): Boolean;
  public
    { Public declarations }
    FWorkshop, FBCId, canshu3, FSTKName, FKHName: string;
  end;

var
  frmDyeGreyClothInEdit: TfrmDyeGreyClothInEdit;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_DyePlanCardSel, U_CompanySel,
  U_GreyClothStkSel, U_GreyClothHXSel, U_GreyClothDYSel, U_GreyClothDJHXSel,
  U_GreyClothHXDJSel, U_ClothInfoSel, U_DyePlanForCardSel, U_DyePBImport;

{$R *.dfm}

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

procedure TfrmDyeGreyClothInEdit.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 hh:mm:ss', Now));
      FieldByName('StkCoNo').Value := '本厂仓库';
      FieldByName('StkCoName').Value := '本厂仓库';
      FieldByName('QtyUnit').Value := 'M';
      Post;
    end;
  end;

end;

function TfrmDyeGreyClothInEdit.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, 'PR', 'BS_Cloth_IO', 3, 1) then
            raise Exception.Create('取染色坯布入库编号失败！');
        end
        else
        begin
          Maxno := Trim(MBCIOID);
        end;





//        with ADOQueryCmd do
//        begin
//          Close;
//          SQL.Clear;
//          sql.Add('select * from Dye_Plan_Main where OrderNo = ' + QuotedStr(Trim(CDS_Sub.FieldByName('OrderNo').Value)));
//          Open;
//        end;
//        if (ADOQueryCmd.FieldByName('GC_STKID').Value <> '') and (ADOQueryCmd.FieldByName('GC_STKID').Value <> null) then
//        begin
//
//          if Pos(',' + Trim(Maxno) + ',', ',' + Trim(ADOQueryCmd.FieldByName('GC_STKID').AsString) + ',') > 0 then
//          begin
//
//          end
//          else
//          begin
//            with ADOQueryCmd do
//            begin
//              Edit;
//              FieldByName('GC_STKID').Value := FieldByName('GC_STKID').value + ',' + Maxno;
//              Post;
//            end;
//
//          end;
//
//        end
//        else
//        begin
//          with ADOQueryCmd do
//          begin
//            edit;
//            FieldByName('GC_STKID').Value := Maxno;
//            Post;
//          end;
//        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);
          end
          else
          begin
            Edit;
            FieldByName('Editid').Value := Trim(Dcode);
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTemp);
          end;
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'BS_Cloth_IO', 0);
          FieldByName('FromCoNo').Value := CDS_Sub.fieldbyname('FromCoNo').Value;
          FieldByName('StkCoNo').Value := CDS_Sub.fieldbyname('StkCoNo').Value;

//          FieldByName('LZORDERNO').Value := CDS_Sub.fieldbyname('LZORDERNO').Value;
          FieldByName('LZSUBID').Value := CDS_Sub.fieldbyname('LZSUBID').Value;
          FieldByName('BCIOID').Value := Trim(Maxno);
          FieldByName('STKID').Value := Trim(Maxno);
          FieldByName('STKName').Value := FSTKName;
          FieldByName('Workshop').Value := FWorkshop;
          FieldByName('IOFlag').Value := '入库';
          FieldByName('IOQtyFlag').Value := 1;
          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('入库匹数小于出库匹数不能保存!');

        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 TfrmDyeGreyClothInEdit.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmDyeGreyClothInEdit.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_ESCAPE then  // VK_ESCAPE 是 ESC 键的常量
  begin
    Key := 0;  // 清除按键，防止继续传递
    Close;     // 或者 ModalResult := mrCancel;
  end;
end;

procedure TfrmDyeGreyClothInEdit.FormShow(Sender: TObject);
var
  fsj: string;
begin
  inherited;
  ReadCxGrid(FSTKName + '坯布入库录入', Tv1, '坯布入库录入');
  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;
  SCreateCDS(ADOQueryTemp, CDS_Sub);
  SInitCDSData(ADOQueryTemp, CDS_Sub);
  KeyPreview := True;  // 确保表单可以预览按键
//  fsj := 'select name=CJName ,Code=null from CheJian';
//  SInitCxGridComboBoxBySql(ADOQueryTemp, v1Column7, fsj, 0, True, '');
end;

procedure TfrmDyeGreyClothInEdit.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;

    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        Sql.Clear;
        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 TfrmDyeGreyClothInEdit.TbImportClick(Sender: TObject);
begin
  try
    frmDyePBImport := TfrmDyePBImport.Create(Application);
    with frmDyePBImport do
    begin
//      FCTID := Trim(Self.CDS_Tree.fieldbyname('CTID').AsString);
      if ShowModal = 1 then
      begin
//        Self.InitGrid();
      end;
    end;
  finally
    frmDyePBImport.Free;
  end;
end;

procedure TfrmDyeGreyClothInEdit.TBSaveClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if CDS_Sub.Locate('IOTime', null, []) = True then
  begin
    Application.MessageBox('入库时间不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('IOType', null, []) = True then
  begin
    Application.MessageBox('入库类型不能为空!', '提示', 0);
    Exit;
  end;
//  if CDS_Sub.Locate('C_XINHAO', null, []) = True then
//  begin
//    Application.MessageBox('型号不能为空!', '提示', 0);
//    Exit;
//  end;

  if CDS_Sub.Locate('Piece', null, []) = True 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('QtyUnit', null, []) = True then
//  begin
//    Application.MessageBox('数量单位不能为空!', '提示', 0);
//    Exit;
//  end;
  {if CDS_Sub.Locate('CRType;LCKNO', VarArrayOf(['回仓', Null]), [loPartialKey]) then
  begin
    Application.MessageBox('回仓的数据流程卡号不能为空!','提示',0);
    Exit;
  end; }
  if SaveCKData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    close;
  end;
end;

procedure TfrmDyeGreyClothInEdit.ToolButton1Click(Sender: TObject);
begin
  inherited;
  WriteCxGrid(FSTKName + '坯布入库录入', Tv1, '坯布入库录入');
end;

procedure TfrmDyeGreyClothInEdit.ToolButton2Click(Sender: TObject);
begin
  inherited;
  if CDS_sub.IsEmpty then
    Exit;
  ToolBar1.SetFocus;
  OneKeyPost(Tv1, CDS_sub);
end;

procedure TfrmDyeGreyClothInEdit.ToolButton3Click(Sender: TObject);
begin
  inherited;
  try
    frmGreyClothStkSel := TfrmGreyClothStkSel.Create(Application);
    with frmGreyClothStkSel do
    begin
      FstkName := Self.FstkName;
      if ShowModal = 1 then
      begin
        frmGreyClothStkSel.CDS_Main.DisableControls;
        with frmGreyClothStkSel.CDS_Main do
        begin
          First;
          while frmGreyClothStkSel.CDS_Main.Locate('SSel', True, []) do
          begin
            with Self.CDS_Sub do
            begin
              Append;
              FieldByName('IOTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
              FieldByName('StkCoNo').Value := '本厂仓库';
              FieldByName('IOType').Value := '加工入库';
              FieldByName('StkCoName').Value := '本厂仓库';
              FieldByName('FromCoName').Value := '恒益坯布仓库';
              FieldByName('C_COde').Value := Trim(CDS_Main.fieldbyname('C_Code').AsString);
              FieldByName('C_xinhao').Value := Trim(CDS_Main.fieldbyname('C_Code').AsString);
              FieldByName('NOTE').Value := Trim(CDS_Main.fieldbyname('crnote').AsString);
              FieldByName('C_NAME').Value := Trim(CDS_Main.fieldbyname('C_CodeName').AsString);
              FieldByName('C_Color').Value := Trim(CDS_Main.fieldbyname('C_Color').AsString);
              FieldByName('C_JITAI').Value := Trim(CDS_Main.fieldbyname('CarNo').AsString);
              FieldByName('C_ZHUANGKUANG').Value := Trim(CDS_Main.fieldbyname('ZHUANGKUANG').AsString);
              FieldByName('C_JIAJI').Value := Trim(CDS_Main.fieldbyname('JJZT').AsString);

              FieldByName('C_Width').Value := Trim(CDS_Main.fieldbyname('PBMF').AsString);
              FieldByName('C_GramWeight').Value := Trim(CDS_Main.fieldbyname('PBKZ').AsString);

              FieldByName('C_gongyi').Value := Trim(CDS_Main.fieldbyname('gongyi3').AsString);
              FieldByName('LZCKORDNO').Value := Trim(CDS_Main.fieldbyname('CKORDNO').AsString);
              FieldByName('LZORDERNO').Value := Trim(CDS_Main.fieldbyname('ORDERNO').AsString);
              FieldByName('LZSUBID').Value := Trim(CDS_Main.fieldbyname('SUBID').AsString);
              FieldByName('Piece').Value := CDS_Main.fieldbyname('PS').ASFLOAT;
              //
              FieldByName('Qty').Value := 0;
            //  CDS_Main.fieldbyname('mjgw').ASFLOAT
              FieldByName('QtyUnit').Value := 'KG';
            end;
            frmGreyClothStkSel.CDS_Main.Delete;
          end;
        end;
        frmGreyClothStkSel.CDS_Main.EnableControls;
      end;
    end;
  finally
    frmGreyClothStkSel.Free;
  end;
end;

procedure TfrmDyeGreyClothInEdit.ToolButton4Click(Sender: TObject);
begin
  inherited;
  try
    frmGreyClothHXSel := TfrmGreyClothHXSel.Create(Application);
    with frmGreyClothHXSel do
    begin
      FstkName := Self.FstkName;
      if ShowModal = 1 then
      begin
        frmGreyClothHXSel.CDS_Main.DisableControls;
        with frmGreyClothHXSel.CDS_Main do
        begin
          First;
          while frmGreyClothHXSel.CDS_Main.Locate('SSel', True, []) do
          begin
            with Self.CDS_Sub do
            begin
              Append;
              FieldByName('IOTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
              FieldByName('StkCoNo').Value := '本厂仓库';
              FieldByName('IOType').Value := '回修入库';
              FieldByName('StkCoName').Value := '本厂仓库';
              FieldByName('FromCoName').Value := '恒益坯布仓库';
              FieldByName('C_COde').Value := Trim(CDS_Main.fieldbyname('C_Code').AsString);
              FieldByName('C_xinhao').Value := Trim(CDS_Main.fieldbyname('C_Code').AsString);
              FieldByName('NOTE').Value := Trim(CDS_Main.fieldbyname('note').AsString);
              FieldByName('C_NAME').Value := Trim(CDS_Main.fieldbyname('C_CodeName').AsString);
              FieldByName('C_Color').Value := Trim(CDS_Main.fieldbyname('C_Color').AsString);

              FieldByName('C_ZHUANGKUANG').Value := Trim(CDS_Main.fieldbyname('ZHUANGKUANG').AsString);
              FieldByName('C_JIAJI').Value := Trim(CDS_Main.fieldbyname('JJZT').AsString);

              FieldByName('C_Width').Value := Trim(CDS_Main.fieldbyname('PBMF').AsString);
              FieldByName('C_GramWeight').Value := Trim(CDS_Main.fieldbyname('PBKZ').AsString);

              FieldByName('C_gongyi').Value := Trim(CDS_Main.fieldbyname('gongyi3').AsString);
              FieldByName('LZCKORDNO').Value := Trim(CDS_Main.fieldbyname('CKORDNO').AsString);
              FieldByName('LZORDERNO').Value := Trim(CDS_Main.fieldbyname('ORDERNO').AsString);
              FieldByName('LZSUBID').Value := Trim(CDS_Main.fieldbyname('SUBID').AsString);
              FieldByName('Piece').Value := CDS_Main.fieldbyname('PS').ASFLOAT;
//              FieldByName('Qty').Value := CDS_Main.fieldbyname('mjgw').ASFLOAT;
//              FieldByName('Piece').Value := 0;
              FieldByName('Qty').Value := 0;
              FieldByName('QtyUnit').Value := 'KG';
            end;
            frmGreyClothHXSel.CDS_Main.Delete;
          end;
        end;
        frmGreyClothHXSel.CDS_Main.EnableControls;
      end;
    end;
  finally
    frmGreyClothHXSel.Free;
  end;
end;

procedure TfrmDyeGreyClothInEdit.ToolButton5Click(Sender: TObject);
begin
  inherited;
  try
    frmGreyClothDYSel := TfrmGreyClothDYSel.Create(Application);
    with frmGreyClothDYSel do
    begin
      FstkName := Self.FstkName;
      if ShowModal = 1 then
      begin
        frmGreyClothDYSel.CDS_Main.DisableControls;
        with frmGreyClothDYSel.CDS_Main do
        begin
          First;
          while frmGreyClothDYSel.CDS_Main.Locate('SSel', True, []) do
          begin
            with Self.CDS_Sub do
            begin
              Append;
              FieldByName('IOTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
              FieldByName('StkCoNo').Value := '本厂仓库';
              FieldByName('IOType').Value := '打样入库';
              FieldByName('StkCoName').Value := '本厂仓库';
              FieldByName('FromCoName').Value := '恒益坯布仓库';
              FieldByName('C_COde').Value := Trim(CDS_Main.fieldbyname('SPCode').AsString);
              FieldByName('C_xinhao').Value := Trim(CDS_Main.fieldbyname('SPCode').AsString);
              FieldByName('NOTE').Value := Trim(CDS_Main.fieldbyname('note').AsString);
              FieldByName('C_NAME').Value := Trim(CDS_Main.fieldbyname('SPNAME').AsString);
              FieldByName('C_Color').Value := Trim(CDS_Main.fieldbyname('SPColor').AsString);
//              FieldByName('C_JITAI').Value := Trim(CDS_Main.fieldbyname('CarNo').AsString);
              FieldByName('C_ZHUANGKUANG').Value := Trim(CDS_Main.fieldbyname('ZHUANGKUANG').AsString);
              FieldByName('C_JIAJI').Value := Trim(CDS_Main.fieldbyname('JJZT').AsString);

              FieldByName('C_Width').Value := Trim(CDS_Main.fieldbyname('PBMF').AsString);
              FieldByName('C_GramWeight').Value := Trim(CDS_Main.fieldbyname('PBKZ').AsString);

              FieldByName('C_gongyi').Value := Trim(CDS_Main.fieldbyname('GONGYI2').AsString);
              FieldByName('LZCKORDNO').Value := Trim(CDS_Main.fieldbyname('SPID').AsString);
//              FieldByName('LZORDERNO').Value := Trim(CDS_Main.fieldbyname('ORDERNO').AsString);
//              FieldByName('LZSUBID').Value := Trim(CDS_Main.fieldbyname('SUBID').AsString);
              FieldByName('Piece').Value := CDS_Main.fieldbyname('PIQTY').ASFLOAT;
              //
              FieldByName('Qty').Value := 0;
            //  CDS_Main.fieldbyname('mjgw').ASFLOAT
              FieldByName('QtyUnit').Value := 'KG';
            end;
            frmGreyClothDYSel.CDS_Main.Delete;
          end;
        end;
        frmGreyClothDYSel.CDS_Main.EnableControls;
      end;
    end;
  finally
    frmGreyClothDYSel.Free;
  end;
end;

procedure TfrmDyeGreyClothInEdit.ToolButton6Click(Sender: TObject);
begin
  try
    frmGreyClothDJHXSel := TfrmGreyClothDJHXSel.Create(Application);
    with frmGreyClothDJHXSel do
    begin
      FstkName := Self.FstkName;
      if ShowModal = 1 then
      begin
        frmGreyClothDJHXSel.CDS_Main.DisableControls;
        with frmGreyClothDJHXSel.CDS_Main do
        begin
          First;
          while frmGreyClothDJHXSel.CDS_Main.Locate('SSel', True, []) do
          begin
            with Self.CDS_Sub do
            begin
              Append;
              FieldByName('IOTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
              FieldByName('StkCoNo').Value := '本厂仓库';
              FieldByName('IOType').Value := '返修入库';
              FieldByName('StkCoName').Value := '本厂仓库';
              FieldByName('FromCoName').Value := '恒益坯布仓库';
              FieldByName('C_COde').Value := Trim(CDS_Main.fieldbyname('SPCode').AsString);
              FieldByName('C_xinhao').Value := Trim(CDS_Main.fieldbyname('SPCode').AsString);
              FieldByName('NOTE').Value := Trim(CDS_Main.fieldbyname('note').AsString);
              FieldByName('C_NAME').Value := Trim(CDS_Main.fieldbyname('SPNAME').AsString);
              FieldByName('C_Color').Value := Trim(CDS_Main.fieldbyname('SPColor').AsString);
//              FieldByName('C_JITAI').Value := Trim(CDS_Main.fieldbyname('CarNo').AsString);
              FieldByName('C_ZHUANGKUANG').Value := Trim(CDS_Main.fieldbyname('ZHUANGKUANG').AsString);
              FieldByName('C_JIAJI').Value := Trim(CDS_Main.fieldbyname('JJZT').AsString);

              FieldByName('C_Width').Value := Trim(CDS_Main.fieldbyname('PBMF').AsString);
              FieldByName('C_GramWeight').Value := Trim(CDS_Main.fieldbyname('PBKZ').AsString);

              FieldByName('C_gongyi').Value := Trim(CDS_Main.fieldbyname('GONGYI2').AsString);
              FieldByName('LZCKORDNO').Value := Trim(CDS_Main.fieldbyname('SPID').AsString);
//              FieldByName('LZORDERNO').Value := Trim(CDS_Main.fieldbyname('ORDERNO').AsString);
//              FieldByName('LZSUBID').Value := Trim(CDS_Main.fieldbyname('SUBID').AsString);
              FieldByName('Piece').Value := CDS_Main.fieldbyname('PIQTY').ASFLOAT;
              //
              FieldByName('Qty').Value := 0;
            //  CDS_Main.fieldbyname('mjgw').ASFLOAT
              FieldByName('QtyUnit').Value := 'KG';
            end;
            frmGreyClothDJHXSel.CDS_Main.Delete;
          end;
        end;
        frmGreyClothDJHXSel.CDS_Main.EnableControls;
      end;
    end;
  finally
    frmGreyClothDJHXSel.Free;
  end;
end;

procedure TfrmDyeGreyClothInEdit.ToolButton7Click(Sender: TObject);
begin
  try
    frmGreyClothHXDJSel := TfrmGreyClothHXDJSel.Create(Application);
    with frmGreyClothHXDJSel do
    begin
      FstkName := Self.FstkName;
      if ShowModal = 1 then
      begin
        frmGreyClothHXDJSel.CDS_Main.DisableControls;
        with frmGreyClothHXDJSel.CDS_Main do
        begin
          First;
          while frmGreyClothHXDJSel.CDS_Main.Locate('SSel', True, []) do
          begin
            with Self.CDS_Sub do
            begin
              Append;
              FieldByName('IOTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
              FieldByName('StkCoNo').Value := '本厂仓库';
              FieldByName('IOType').Value := '回修入库';
              FieldByName('StkCoName').Value := '本厂仓库';
              FieldByName('FromCoName').Value := '恒益坯布仓库';
              FieldByName('C_COde').Value := Trim(CDS_Main.fieldbyname('SPCode').AsString);
              FieldByName('C_xinhao').Value := Trim(CDS_Main.fieldbyname('SPCode').AsString);
              FieldByName('NOTE').Value := Trim(CDS_Main.fieldbyname('note').AsString);
              FieldByName('C_NAME').Value := Trim(CDS_Main.fieldbyname('SPNAME').AsString);
              FieldByName('C_Color').Value := Trim(CDS_Main.fieldbyname('SPColor').AsString);
//              FieldByName('C_JITAI').Value := Trim(CDS_Main.fieldbyname('CarNo').AsString);
              FieldByName('C_ZHUANGKUANG').Value := Trim(CDS_Main.fieldbyname('ZHUANGKUANG').AsString);
              FieldByName('C_JIAJI').Value := Trim(CDS_Main.fieldbyname('JJZT').AsString);

              FieldByName('C_Width').Value := Trim(CDS_Main.fieldbyname('PBMF').AsString);
              FieldByName('C_GramWeight').Value := Trim(CDS_Main.fieldbyname('PBKZ').AsString);

              FieldByName('C_gongyi').Value := Trim(CDS_Main.fieldbyname('GONGYI2').AsString);
              FieldByName('LZCKORDNO').Value := Trim(CDS_Main.fieldbyname('SPID').AsString);
//              FieldByName('LZORDERNO').Value := Trim(CDS_Main.fieldbyname('ORDERNO').AsString);
//              FieldByName('LZSUBID').Value := Trim(CDS_Main.fieldbyname('SUBID').AsString);
              FieldByName('Piece').Value := CDS_Main.fieldbyname('PIQTY').ASFLOAT;
              //
              FieldByName('Qty').Value := 0;
            //  CDS_Main.fieldbyname('mjgw').ASFLOAT
              FieldByName('QtyUnit').Value := 'KG';
            end;
            frmGreyClothHXDJSel.CDS_Main.Delete;
          end;
        end;
        frmGreyClothHXDJSel.CDS_Main.EnableControls;
      end;
    end;
  finally
    frmGreyClothHXDJSel.Free;
  end;
end;

procedure TfrmDyeGreyClothInEdit.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 TfrmDyeGreyClothInEdit.Tv1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  inherited;
  try
    frmClothInfoSel := TfrmClothInfoSel.Create(Application);
    with frmClothInfoSel do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;

          FieldByName('C_Name').Value := Trim(CDS_1.fieldbyname('C_Name').AsString);
          FieldByName('F_Name').Value := Trim(CDS_1.fieldbyname('F_Name').AsString);

          FieldByName('C_Width').Value := Trim(CDS_1.fieldbyname('C_Width').AsString);

          FieldByName('F_Width').Value := Trim(CDS_1.fieldbyname('F_Width').AsString);
          FieldByName('Material').Value := Trim(CDS_1.fieldbyname('Material').AsString);

          FieldByName('finishDescription').Value := Trim(CDS_1.fieldbyname('FinishingStandards').AsString);
        end;
      end;
    end;
  finally
    frmClothInfoSel.Free;
  end;
end;

procedure TfrmDyeGreyClothInEdit.Tv1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  inherited;
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('FromCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
          FieldByName('FromCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
        end;
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;




procedure TfrmDyeGreyClothInEdit.Tv1OrderNoPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
//  with ADOQueryTemp do
//  begin
//    close;
//    sql.Clear;
//    sql.Add('exec P_OrderCR');
//
////    ShowMessage(sql.Text);
//    execsql;
//  end;

  frmProductOrderListSel := TfrmProductOrderListSel.create(self);
  with frmProductOrderListSel do
  begin
    FWorkshop := self.FWorkshop;
    if ShowModal = 1 then
    begin
      Self.CDS_Sub.Edit;
      Self.CDS_Sub.FieldByName('OrderNo').Value := Order_Main.fieldbyname('OrderNo').asstring;
      Self.CDS_Sub.FieldByName('C_Name').Value := Order_Main.fieldbyname('C_Name').asstring;
      Self.CDS_Sub.FieldByName('C_Code').Value := Order_Main.fieldbyname('C_Code').asstring;
      Self.CDS_Sub.FieldByName('F_Name').Value := Order_Main.fieldbyname('F_Name').asstring;

      Self.CDS_Sub.FieldByName('finishDescription').Value := Order_Main.fieldbyname('C_description').asstring;
      Self.CDS_Sub.FieldByName('F_description').Value := Order_Main.fieldbyname('F_description').asstring;

      //      showMessage(Order_Main.FieldByName('finishDescription').AsString);
      //      initGlide();
    end;
    free;
  end;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from BS_Cloth_Info where C_Name = ' + QuotedStr(Trim(CDS_Sub.FieldByName('C_Name').asString)));
    Open;
  end;
  with CDS_Sub do
  begin
    Edit;

    FieldByName('C_Width').Value := Trim(ADOQueryTemp.fieldbyname('C_Width').AsString);
    FieldByName('F_Width').Value := Trim(ADOQueryTemp.fieldbyname('F_Width').AsString);
    FieldByName('Material').Value := Trim(ADOQueryTemp.fieldbyname('Material').AsString);

//    FieldByName('finishDescription').Value := Trim(ADOQueryTemp.fieldbyname('FinishingStandards').AsString);
    Post;
  end;
end;

procedure TfrmDyeGreyClothInEdit.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 TfrmDyeGreyClothInEdit.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 TfrmDyeGreyClothInEdit.v2Column6PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FPS, FQty, FPiZhong: Double;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;

  with CDS_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := mvalue;
    Post;
    FPS := CDS_Sub.fieldbyname('Piece').AsFloat;
    FQty := CDS_Sub.fieldbyname('Qty').AsFloat;
    FPiZhong := CDS_Sub.fieldbyname('PieceWeight').AsFloat;
  end;

  if Trim(FFieldName) <> 'PieceWeight' then
  begin
    if FPS = 0 then
      FPiZhong := FQty / 1
    else
      FPiZhong := FQty / FPS;
  end;

  with CDS_Sub do
  begin
    Edit;
    FieldByName('PieceWeight').Value := FPiZhong;
    Post;
  end;

  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmDyeGreyClothInEdit.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 TfrmDyeGreyClothInEdit.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.

