unit U_WJSJInPut;

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, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator;

type
  TfrmWJSJInPut = class(TForm)
    ToolBar1: TToolBar;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBSave: TToolButton;
    TBClose: TToolButton;
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column13: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Tv1Column1: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Tv1Column2: TcxGridDBColumn;
    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 v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column14PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column11PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column16PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column13PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure v1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure v1Column1PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
  private
    { Private declarations }
    function SaveCKData(): Boolean;
    function YSData(): Boolean;
  public
    { Public declarations }
    FBCId, canshu3: string;
  end;

var
  frmWJSJInPut: TfrmWJSJInPut;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_CKProductBCPOutListHZSEL, U_ZdyAttachment,
  U_ZdyAttachGYS, U_ProductOrderListSel;

{$R *.dfm}
function TfrmWJSJInPut.YSData(): Boolean;
var
  CRID, YFID, Price, PriceUnit, OrderUnit, FComTaiTou, cust: string;
begin
  Result := False;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryName=''' + Trim(CDS_Sub.fieldbyname('FactoryName').AsString) + ''' ');
    Open;
  end;

  if not ADOQueryTemp.IsEmpty then
  begin
    CRID := ADOQueryTemp.fieldbyname('CRID').AsString;
  end
  else
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CRID set CRID=CRID+1');
      sql.Add('select * from YF_Money_CRID ');
      Open;
    end;
    CRID := ADOQueryCmd.fieldbyname('CRID').AsString;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YF_Money_KC where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('CRID').Value := StrToInt(CRID);
      FieldByName('FactoryName').Value := Trim(CDS_Sub.fieldbyname('FactoryName').AsString);
      FieldByName('ZdyStr1').Value := '应收收';
      Post;
    end;
  end;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR ');
    sql.Add(' where mainID=''' + Trim(CDS_Sub.fieldbyname('SPID').AsString) + '''');
     //   sql.Add(' and subID='''+Trim(CDS_Main.fieldbyname('CRID').AsString)+'''');
    sql.Add(' and YFName=''坯布销售退回'' ');
    Open;
  end;

  if ADOQueryTemp.IsEmpty then
  begin
    if GetLSNo(ADOQueryCmd, YFID, 'CS', 'YF_Money_CR', 3, 1) = False then
    begin
      Application.MessageBox('取应收最大号失败!', '提示', 0);
      Exit;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YF_Money_CR where 1<>1');
      Open;
    end;

    with ADOQueryCmd do
    begin
      Append;
      FieldByName('YFID').Value := Trim(YFID);
      FieldByName('YFTypeId').Value := Trim(CDS_Sub.fieldbyname('SPID').AsString);
      FieldByName('CRID').Value := StrToInt(CRID);
      FieldByName('Filler').Value := Trim(DName);
      FieldByName('CRType').Value := '应收款登记';
      FieldByName('CRFlag').Value := '应收收';
      FieldByName('QtyFlag').Value := 1;
      FieldByName('FactoryName').Value := Trim(CDS_Sub.fieldbyname('ToFactoryName').AsString);
      FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', CDS_Sub.fieldbyname('CRTime').AsDateTime));
      FieldByName('YFType').Value := '自动生成';
      FieldByName('ps').Value := -1 * CDS_Sub.fieldbyname('PiQty').AsFloat;
      FieldByName('Qty').Value := -1 * CDS_Sub.fieldbyname('Qty').AsFloat;
      FieldByName('Price').Value := 0;
      FieldByName('Money').Value := 0;
      FieldByName('BBMoney').Value := 0;
      FieldByName('HuiLv').Value := 1;
      FieldByName('BZType').Value := 'RMB';
      FieldByName('QtyUnit').Value := Trim(CDS_Sub.fieldbyname('QtyUnit').AsString);
      FieldByName('ComTaiTou').Value := Trim(CDS_Sub.fieldbyname('ToFactoryName').AsString);
      FieldByName('YFName').Value := '坯布销售退回';
      FieldByName('MainId').Value := Trim(CDS_Sub.fieldbyname('SPID').AsString);
      FieldByName('subID').Value := '';
      FieldByName('P_CodeName').Value := Trim(CDS_Sub.fieldbyname('SPName').AsString);
      FieldByName('P_Spec').Value := Trim(CDS_Sub.fieldbyname('SPSpec').AsString);
      FieldByName('P_Color').Value := Trim(CDS_Sub.fieldbyname('PRTColor').AsString);
      FieldByName('status').Value := '0';
      Post;
    end;
  end
  else
  begin
    YFID := Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
  end;

  Result := True;
end;

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

procedure TfrmWJSJInPut.TBAddClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty = False then
  begin
    CopyAddRowCDS(CDS_Sub);
    with CDS_Sub do
    begin
      Edit;
      FieldByName('SPID').Value := null;
      post;
    end;
  end
  else
  begin
    with CDS_Sub do
    begin
      Append;
      FieldByName('SHQTY').Value := 0;
      FieldByName('QTY').Value := 0;
      FieldByName('AQTY').Value := 0;
      FieldByName('BQTY').Value := 0;
      FieldByName('DDQTY').Value := 0;
      FieldByName('DDBILI').Value := 0;

      Post;
    end;
  end;

end;

function TfrmWJSJInPut.SaveCKData(): Boolean;
var
  FCRID, Maxno, FFSPID: 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 WJSJ where SPID=''' + Trim(CDS_Sub.fieldbyname('SPID').AsString) + '''');
          open;
        end;
        FFSPID := Trim(ADOQueryTemp.fieldbyname('SPID').AsString);
        if Trim(FFSPID) = '' then
        begin
          if GetLSNo(ADOQueryCmd, Maxno, 'WJ', 'WJSJ', 3, 1) = False then
          begin
            Result := False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取流水号失败!', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          Maxno := Trim(FFSPID);
        end;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from WJSJ where SPID=''' + Trim(Maxno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(FFSPID) = '' then
          begin
            Append;
          end
          else
          begin
            Edit;
          end;
          FieldByName('SPID').Value := Trim(Maxno);
          FieldByName('FZSPID').Value := Trim(Maxno);
          FieldByName('CKName').Value := '坯布';

          FieldByName('ORDMainIdRK').Value := Trim(CDS_Sub.fieldbyname('ORDMainIdRK').AsString);
          FieldByName('ORDSubIdRK').Value := Trim(CDS_Sub.fieldbyname('ORDSubIdRK').AsString);

          FieldByName('CRFlag').Value := '入库';
          FieldByName('CRQtyFlag').Value := 1;
          //FieldByName('CRID').Value:=FCRID;
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'WJSJ', 2);
          if Trim(FFSPID) = '' then
          begin
            FieldByName('Filler').Value := Trim(DName);
            FieldByName('Filltime').Value := SGetServerDate(ADOQueryTemp);
          end
          else
          begin
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDate(ADOQueryTemp);
          end;

          Post;
        end;

        Edit;
        FieldByName('SPID').Value := Trim(Maxno);
        Post;

        Next;
      end;
    end;
    CDS_Sub.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!', '提示', 0);
  end;

end;

procedure TfrmWJSJInPut.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Self.Caption, Tv1, '坯布仓库');
  Close;
end;

procedure TfrmWJSJInPut.FormShow(Sender: TObject);
var
  fsj: string;
begin
  ReadCxGrid(Self.Caption, Tv1, '坯布仓库');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');

    sql.Add(' from WJSJ A');
    sql.Add(' where SPID=''' + Trim(FBCId) + '''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_Sub);
  SInitCDSData20(ADOQueryTemp, CDS_Sub);

end;

procedure TfrmWJSJInPut.TBDelClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if Trim(CDS_Sub.fieldbyname('SPID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;

    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' delete wjsj where SPID=''' + Trim(CDS_Sub.fieldbyname('SPID').AsString) + '''');

        ExecSQL;

      end;
      ADOQueryCmd.Connection.CommitTrans;
      CDS_Sub.Delete;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!', '提示', 0);
    end;

  end
  else
  begin
    CDS_Sub.Delete;
  end;

end;

procedure TfrmWJSJInPut.TBSaveClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if CDS_Sub.Locate('orderno', 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('SPName', null, []) = True then
  begin
    Application.MessageBox('品名不能为空!', '提示', 0);
    Exit;
  end;
//  if CDS_Sub.Locate('QtyUnit', null, []) = True then
//  begin
//    Application.MessageBox('数量单位不能为空!', '提示', 0);
//    Exit;
//  end;

  if SaveCKData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    //ModalResult:=1;
    Exit;
  end;
end;

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

procedure TfrmWJSJInPut.v1Column14PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  if CDS_Sub.FieldByName('CRType').AsString = '销售退回' then
  begin
    try
      frmZdyAttachment := TfrmZdyAttachment.Create(Application);
      with frmZdyAttachment do
      begin
        if ShowModal = 1 then
        begin
          with Self.CDS_Sub do
          begin
            edit;
            FieldByName('FactoryNo').Value := Trim(CDS_HZ.fieldbyname('coCode').AsString);
            FieldByName('FactoryName').Value := Trim(CDS_HZ.fieldbyname('coName').AsString);
          //Post;
          end;
        end;
      end;
    finally
      frmZdyAttachment.Free;
    end;
  end
  else
  begin
    try
      frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
      with frmZdyAttachGYS do
      begin
        if ShowModal = 1 then
        begin
          with Self.CDS_Sub do
          begin
            edit;
            FieldByName('FactoryNo').Value := Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('coCode').AsString);
            FieldByName('FactoryName').Value := Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('coName').AsString);
          //Post;
          end;
        end;
      end;
    finally
      frmZdyAttachGYS.Free;
    end;
  end;

end;

procedure TfrmWJSJInPut.v2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PBName';
      flagname := '坯布名称';
      fnote := True;
      V1Name.Caption := '中文品名';
      V1Note.Caption := '英文品名';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('SPName').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
//  if Trim(canshu3)='' then
//  begin
//
//  end else
//  begin
//    try
//      frmZdyAttachCP:=TfrmZdyAttachCP.Create(Application);
//      with frmZdyAttachCP do
//      begin
//        if ShowModal=1 then
//        begin
//          with Self.CDS_Sub do
//          begin
//            Edit;
//            FieldByName('SPName').Value:=Trim(frmZdyAttachCP.CDS_HZ.fieldbyname('ZdyName').AsString);
//            FieldByName('SPSpec').Value:=Trim(frmZdyAttachCP.CDS_HZ.fieldbyname('DEFstr1').AsString);
//            FieldByName('SPCF').Value:=Trim(frmZdyAttachCP.CDS_HZ.fieldbyname('DEFstr2').AsString);
//            FieldByName('SPMF').Value:=Trim(frmZdyAttachCP.CDS_HZ.fieldbyname('DEFstr7').AsString);
//            FieldByName('SPKZ').Value:=Trim(frmZdyAttachCP.CDS_HZ.fieldbyname('DEFstr8').AsString);
//            FieldByName('SPKZ').Value:=Trim(frmZdyAttachCP.CDS_HZ.fieldbyname('DEFstr8').AsString);
//            FieldByName('SPCode').Value:=Trim(frmZdyAttachCP.CDS_HZ.fieldbyname('ZdyCode').AsString);
//            FieldByName('CPID').Value:=Trim(frmZdyAttachCP.CDS_HZ.fieldbyname('ATID').AsString);
//          end;
//        end;
//      end;
//    finally
//      frmZdyAttachCP.Free;
//    end;
//  end;

end;

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

procedure TfrmWJSJInPut.v1Column11PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          edit;
          FieldByName('ToFactoryNo').Value := Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('coCode').AsString);
          FieldByName('ToFactoryName').Value := Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('coName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmWJSJInPut.v1Column16PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          edit;
          FieldByName('FromFactoryNo').Value := Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('coCode').AsString);
          FieldByName('FromFactoryName').Value := Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('coName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmWJSJInPut.v1Column13PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCKProductBCPOutListHZSEL := tfrmCKProductBCPOutListHZSEL.Create(Application);
    with frmCKProductBCPOutListHZSEL do
    begin
      if ShowModal = 1 then
      begin
        with frmCKProductBCPOutListHZSEL.CDS_Main do
        begin

          with Self.CDS_Sub do
          begin
            Edit;
            FieldByName('orderno').Value := CDS_Main.FieldByName('orderno').Value;
            FieldByName('CONNO').Value := CDS_Main.FieldByName('khconno').Value;
            FieldByName('CustomerNoName').Value := CDS_Main.FieldByName('CustomerNoName').Value;
            FieldByName('SPName').Value := CDS_Main.FieldByName('MPRTCodeName').Value;
            FieldByName('SPCOLOR').Value := CDS_Main.FieldByName('PRTCOLOR').AsString + '/' + CDS_Main.FieldByName('PRThx').AsString;
            FieldByName('ORDERQTY').Value := CDS_Main.FieldByName('PRTORDERQTY').Value;
            FieldByName('SHQTY').Value := CDS_Main.FieldByName('Qty').Value;

            FieldByName('ORDMainIdRK').Value := Trim(CDS_Main.fieldbyname('MainId').AsString);
            FieldByName('ORDSubIdRK').Value := Trim(CDS_Main.fieldbyname('SubId').AsString);
            Post;
          end;

          EnableControls;
        end;
      end;
    end;
  finally
    frmCKProductBCPOutListHZSEL.Free;
  end;
end;

procedure TfrmWJSJInPut.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if Trim(canshu3) = '' then
    exit;
  v1Column17.Options.Editing := False;
  v1Column8.Options.Editing := False;
  v1Column7.Options.Editing := False;
  (v2Column1.Properties as TcxButtonEditProperties).ReadOnly := True;
end;

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

procedure TfrmWJSJInPut.v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PBKF';
      flagname := '坯布称呼';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('SPCF').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmWJSJInPut.v1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PBMF';
      flagname := '坯布门幅';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('SPMF').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmWJSJInPut.v1Column10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PBKZ';
      flagname := '坯布克重';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('SPKZ').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmWJSJInPut.ToolButton1Click(Sender: TObject);
var
  MMaxno: string;
begin
  try
    frmCKProductBCPOutListHZSEL := tfrmCKProductBCPOutListHZSEL.Create(Application);
    with frmCKProductBCPOutListHZSEL do
    begin
      if ShowModal = 1 then
      begin
        with frmCKProductBCPOutListHZSEL.CDS_Main do
        begin
          first;
          DisableControls;
          while not eof do
          begin
            if fieldbyname('ssel').AsBoolean then
            begin
              if GetLSNo(ADOQueryCmd, MMaxno, 'WJ', 'WJSJ', 3, 1) = False then
              begin
                Application.MessageBox('取编号失败!', '提示', 0);
                Exit;
              end;
              with Self.CDS_Sub do
              begin
                Append;
                FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
                FieldByName('SPID').Value := Trim(MMaxno);
                FieldByName('orderno').Value := CDS_Main.FieldByName('orderno').Value;
                FieldByName('CONNO').Value := CDS_Main.FieldByName('khconno').Value;
                FieldByName('CustomerNoName').Value := CDS_Main.FieldByName('CustomerNoName').Value;
                FieldByName('SPName').Value := CDS_Main.FieldByName('MPRTCodeName').Value;
                FieldByName('SPCOLOR').Value := CDS_Main.FieldByName('PRTCOLOR').AsString + '/' + CDS_Main.FieldByName('PRThx').AsString;
                FieldByName('ORDERQTY').Value := CDS_Main.FieldByName('PRTORDERQTY').Value;
                FieldByName('SHQTY').Value := CDS_Main.FieldByName('Qty').Value;
                FieldByName('SHQTY').Value := CDS_Main.FieldByName('Qty').Value;

                FieldByName('ORDMainIdRK').Value := Trim(CDS_Main.fieldbyname('MainId').AsString);
                FieldByName('ORDSubIdRK').Value := Trim(CDS_Main.fieldbyname('SubId').AsString);
                Post;
              end;
            end;
            next;
          end;
          first;
          EnableControls;
        end;
      end;
    end;
  finally
    frmCKProductBCPOutListHZSEL.Free;
  end;
end;

procedure TfrmWJSJInPut.v1Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FName, FAQTY, FBQty, fddqty, FSHQTY: string;
  FQTY: Double;
begin
  FName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  with CDS_Sub do
  begin
    Edit;
    FieldByName(FName).Value := mvalue;
    Post;
  end;
  FAQTY := Trim(CDS_Sub.fieldbyname('AQTY').AsString);
  FBQty := Trim(CDS_Sub.fieldbyname('BQTY').AsString);
  fddqty := Trim(CDS_Sub.fieldbyname('DDQTY').AsString);
  FSHQTY := Trim(CDS_Sub.fieldbyname('SHQTY').AsString);
  if Trim(FAQTY) = '' then
  begin
    FAQTY := '0';
  end;
  if Trim(FBQty) = '' then
  begin
    FBQty := '0';
  end;
  if Trim(fddqty) = '' then
  begin
    fddqty := '0';
  end;
  if Trim(FSHQTY) = '' then
  begin
    FSHQTY := '0';
  end;
  FQTY := StrToFloat(FAQTY) + StrToFloat(FBQty) ;      //- StrToFloat(fddqty)
  with CDS_Sub do
  begin
    Edit;
    FieldByName('QTY').Value := FQTY;
    if FQTY <> 0 then
    begin
      FieldByName('DDBILI').value := (StrToFloat(fddqty) / StrToFloat(FSHQTY)) * 100;
    end;
    Post;
  end;
end;

procedure TfrmWJSJInPut.ToolButton2Click(Sender: TObject);
var
  FValue, FFValue, FColumn, FFColumn: string;
begin
  ToolBar1.SetFocus;
  if CDS_Sub.IsEmpty then
    Exit;
  if Application.MessageBox('确定要一键替换吗', '提示', 1) = 2 then
    exit;
  FColumn := Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  FFColumn := Tv1.Controller.FocusedColumn.Summary.GroupFormat;
  FValue := Trim(CDS_Sub.fieldbyname(FColumn).AsString);
  if Trim(FFColumn) <> '' then
  begin
    FFValue := Trim(CDS_Sub.fieldbyname(FFColumn).AsString);
  end;
  with CDS_Sub do
  begin
    DisableControls;
    First;
    while not Eof do
    begin
      Edit;
      if FValue = '' then
      begin
        CDS_Sub.FieldByName(FColumn).Value := null;
      end
      else
      begin
        CDS_Sub.FieldByName(FColumn).Value := FValue;
      end;

      if Trim(FFColumn) <> '' then
      begin
        if FFValue = '' then
        begin
          CDS_Sub.FieldByName(FFColumn).Value := null;
        end
        else
        begin
          CDS_Sub.FieldByName(FFColumn).Value := FFValue;
        end;
      end;
      if (FColumn = 'AQTY') or (FColumn = 'BQTY') or (FColumn = 'DDQTY') or (FColumn = 'SHQTY') then
      begin
        CDS_Sub.FieldByName('QTY').AsFloat := StrToFloatDef(CDS_Sub.FieldByName('aQty').AsString, 0) + StrToFloatDef(CDS_Sub.FieldByName('bqty').AsSTRING, 0);  // - StrToFloatDef(CDS_Sub.FieldByName('DDqty').AsSTRING, 0)
        CDS_Sub.FieldByName('DDBILI').AsFloat := StrToFloatDef(CDS_Sub.FieldByName('DDQTY').AsSTRING, 0) / StrToFloatDef(CDS_Sub.FieldByName('SHqty').AsSTRING, 0) * 100;

      end;
      Post;

      Next;
    end;
    EnableControls;
  end;

end;

end.

