unit U_ShaRKInPut;

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,
  RM_Dataset, RM_System, RM_Common, RM_Class, RM_GridReport, BtnEdit,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmShaRKInPut = class(TForm)
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBDel: TToolButton;
    TBSave: TToolButton;
    TBClose: TToolButton;
    v1Column4: TcxGridDBColumn;
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    v1Note: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Price: TcxGridDBColumn;
    v1ComTaiTou: TcxGridDBColumn;
    RM1: TRMGridReport;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    ToolButton1: TToolButton;
    Panel3: TPanel;
    Label2: TLabel;
    Label4: TLabel;
    ConNo: TBtnEditC;
    Label1: TLabel;
    FactoryName: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    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 Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure v1Column10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column15PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1ComTaiTouPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure ConNoBtnUpClick(Sender: TObject);
    procedure ConNoBtnDnClick(Sender: TObject);
    procedure v1Column8PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure FactoryNameLPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v2Column6PropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }
    function SaveCKData(): Boolean;
    procedure prindata();
  public
    { Public declarations }
    FBCId, canshu3, FTxtCode: string;
  end;

var
  frmShaRKInPut: TfrmShaRKInPut;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_ZdyAttachGYS, U_CaiGouListSel, U_YarnListSel,
  U_ContractListCGSel;

{$R *.dfm}

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

procedure TfrmShaRKInput.prindata();
var
  filepath: string;
  mBegdate, mEnddate: string;
  FName, FNameSX, FNameJX: string;
  i: Integer;
  maxno, fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  with ADOQueryCmd do
  begin
    Filtered := False;
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(',FillNo=(select B.Userid From SY_User B where B.UserName=A.Filler)');
    sql.Add(' from CK_SXPB_CR A');
    sql.add(' where A.Y_ID=''' + Trim(CDS_Sub.Fieldbyname('Y_ID').asstring) + '''');
    sql.Add(' and isnull(CKName,'''')=''纱线''');
    SQL.Add(' and CRFlag=''入库''  ');
    Sql.Add(' and ZFFlag=0 ');
    Open;
  end;
  SCreateCDS20(ADOQueryCmd, CDS_HZ);
  SInitCDSData20(ADOQueryCmd, CDS_HZ);
  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := Trim(CDS_Sub.Fieldbyname('Y_ID').asstring);
    fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
    if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
      CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
    if FileExists(fImagePath) then
      DeleteFile(fImagePath);
    Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);
  except
    application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
    exit;
  end;
  filepath := ExtractFilePath(Application.ExeName) + 'report\纱线入库标签.rmf';
  if not FileExists(Pchar(filepath)) then
  begin
    application.MessageBox(pchar('文件[' + filepath + ']不存在！'), '提示信息', MB_IConError);
    exit;
  end;
  RMVariables['QRBARCODE'] := fImagePath;
  RM1.LoadFromFile(filepath);
  RM1.PrintReport;
end;

function TfrmShaRKInPut.SaveCKData(): Boolean;
var
  FCRID, Maxno, FFY_ID: 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 CK_Yarn_CR where Y_ID=''' + Trim(CDS_Sub.fieldbyname('Y_ID').AsString) + '''');
          open;
        end;
        FFY_ID := Trim(ADOQueryTemp.fieldbyname('Y_ID').AsString);
        if Trim(FFY_ID) = '' then
        begin
          if GetLSNo(ADOQueryCmd, Maxno, 'YR', 'CK_Yarn_CR', 3, 1) = False then
          begin
            Result := False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取纱线入库编号失败!', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          Maxno := Trim(FFY_ID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_Yarn_CR where Y_ID=''' + Trim(Maxno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(FFY_ID) = '' then
          begin
            Append;
          end
          else
          begin
            Edit;
          end;
          FieldByName('Y_ID').Value := Trim(Maxno);
          FieldByName('KCID').Value := Trim(Maxno);
          FieldByName('CKName').Value := '纱线';
          FieldByName('Conno').Value := ConNo.Text;
          FieldByName('FromFactoryNo').Value := Trim(CDS_Sub.fieldbyname('FromFactoryNo').AsString);
          FieldByName('ToFactoryNo').Value := Trim(CDS_Sub.fieldbyname('ToFactoryNo').AsString);
          FieldByName('CPID').Value := Trim(CDS_Sub.fieldbyname('CPID').AsString);
          FieldByName('CRFlag').Value := '入库';
          FieldByName('CRQtyFlag').Value := 1;
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'CK_SXPB_CR', 2);
          if Trim(FFY_ID) = '' then
          begin
            FieldByName('Filler').Value := Trim(DName);
            FieldByName('FillCode').Value := Trim(DCode);
            FieldByName('Filltime').Value := SGetServerDate(ADOQueryTemp);
          end
          else
          begin
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditCode').Value := Trim(DCode);
            FieldByName('EditTime').Value := SGetServerDate(ADOQueryTemp);
          end;
          fieldbyname('Price').Value := CDS_Sub.fieldbyname('Price').AsFloat;
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update CK_Yarn_CR Set MXKCQty=(select sum(isnull(Qty,0)*CRQtyFlag) from CK_Yarn_CR A where A.KCID=CK_Yarn_CR.Y_ID )');
          sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_Yarn_CR A where A.KCID=CK_Yarn_CR.Y_ID )');
          sql.Add(' where Y_ID=''' + Trim(Maxno) + '''');
          ExecSQL;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_Yarn_CR where Y_ID=''' + Trim(Maxno) + '''');
          Open;
        end;
        if ADOQueryTemp.FieldByName('MXKCQty').Value < 0 then
        begin
          CDS_Sub.DisableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('入库数小于出库数不能保存!', '提示', 0);
          Exit;
        end;
        Edit;
        FieldByName('Y_ID').Value := Trim(Maxno);
        //FieldByName('CRID').Value:=Trim(FCRID);
        Post;
        //prindata();
        Next;
      end;
    end;
    CDS_Sub.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!', '提示', 0);
  end;
end;

procedure TfrmShaRKInPut.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('纱线入库登记22', Tv1, '纱线仓库');
  Close;
end;

procedure TfrmShaRKInPut.FormShow(Sender: TObject);
var
  fsj: string;
begin
  ReadCxGrid('纱线入库登记22', Tv1, '纱线仓库');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from CK_Yarn_CR A');
    sql.Add(' where Y_ID=''' + Trim(FBCId) + '''');
    Open;
  end;
  ConNo.Text := ADOQueryTemp.fieldbyname('ConNo').AsString;
  FTxtCode := ADOQueryTemp.fieldbyname('FactoryNo').AsString;
  SCreateCDS20(ADOQueryTemp, CDS_Sub);
  SInitCDSData20(ADOQueryTemp, CDS_Sub);
  if CDS_Sub.IsEmpty then
    Exit;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_Yarn_CR where KCID=''' + Trim(CDS_Sub.fieldbyname('Y_ID').AsString) + '''');
    sql.Add(' and CRQtyFlag=-1');
    Open;
  end;

end;

procedure TfrmShaRKInPut.TBDelClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if Trim(CDS_Sub.fieldbyname('Y_ID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_SXPB_CR where KCID=''' + Trim(CDS_Sub.fieldbyname('Y_ID').AsString) + '''');
      sql.Add(' and CRQtyFlag=-1');
      Open;
    end;
    if ADOQueryTemp.IsEmpty = False then
    begin
      Application.MessageBox('已有出库记录，不能删除!', '提示', 0);
      Exit;
    end;
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' delete CK_SXPB_CR where Y_ID=''' + Trim(CDS_Sub.fieldbyname('Y_ID').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 TfrmShaRKInPut.TBSaveClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if CDS_Sub.Locate('CRTime', null, []) = True then
  begin
    Application.MessageBox('入库时间不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('CRType', null, []) = True then
  begin
    Application.MessageBox('入库类型不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('Y_CodeName', null, []) = True then
  begin
    Application.MessageBox('品名不能为空!', '提示', 0);
    Exit;
  end;
 // if CDS_Sub.Locate('ToFactoryName', null, []) = True then
//  begin
//    Application.MessageBox('存放工厂不能为空!', '提示', 0);
//    Exit;
//  end;
  if CDS_Sub.Locate('FactoryName', null, []) = True then
  begin
    Application.MessageBox('纱线供应商（产地）不能为空!', '提示', 0);
    Exit;
  end;
//  if CDS_Sub.Locate('FactType', 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('FactoryNo', null, []) = True then
//  begin
//    Application.MessageBox('供应商编号不能为空!', '提示', 0);
//    Exit;
//  end;
  if SaveCKData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    ModalResult := 1;
    Exit;
  end;
end;

procedure TfrmShaRKInPut.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 TfrmShaRKInPut.v1Column14PropertiesButtonClick(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('FactoryNo').Value := Trim(CDS_HZ.fieldbyname('ATID').AsString);
          FieldByName('FactoryName').Value := Trim(CDS_HZ.fieldbyname('ZdyName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;

end;

procedure TfrmShaRKInPut.v2Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SXYCLName';
      flagname := '纱线名称';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('Y_CodeName').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
//
//  try
//    frmYarnListSel := TfrmYarnListSel.Create(Application);
//    with frmYarnListSel do
//    begin
//      if ShowModal = 1 then
//      begin
//        with Self.CDS_Sub do
//        begin
//          Edit;
//          FieldByName('Y_CodeName').Value := Trim(frmYarnListSel.CDS_HZ.fieldbyname('Y_CodeName').AsString);
//          FieldByName('Y_Spec').Value := Trim(frmYarnListSel.CDS_HZ.fieldbyname('Y_Spec').AsString);
//          FieldByName('Y_CF').Value := Trim(frmYarnListSel.CDS_HZ.fieldbyname('Y_CF').AsString);
//        end;
//      end;
//    end;
//  finally
//    frmYarnListSel.Free;
//  end;

end;

procedure TfrmShaRKInPut.v1Column5PropertiesButtonClick(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 TfrmShaRKInPut.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(CDS_HZ.fieldbyname('Coid').AsString);
          FieldByName('ToFactoryName').Value := Trim(CDS_HZ.fieldbyname('Name').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;

end;

procedure TfrmShaRKInPut.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(CDS_HZ.fieldbyname('CoID').AsString);
          FieldByName('FromFactoryName').Value := Trim(CDS_HZ.fieldbyname('Name').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;

end;

procedure TfrmShaRKInPut.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_Yarn_CR where Y_ID=''' + Trim(CDS_Sub.fieldbyname('Y_ID').AsString) + '''');
    sql.Add(' and CRQtyFlag=-1');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    v1Column4.Options.Editing := False;
    v2Column1.Options.Editing := False;
    v1Column8.Options.Editing := False;
    v1Column7.Options.Editing := False;
    v1Column11.Options.Editing := False;
    v1Column3.Options.Editing := False;
    v1Column16.Options.Editing := False;

  end
  else
  begin
    v1Column4.Options.Editing := True;
    v1Column8.Options.Editing := True;
    v1Column7.Options.Editing := True;
    v2Column1.Options.Editing := True;
    v1Column11.Options.Editing := True;
    v1Column3.Options.Editing := True;
    v1Column16.Options.Editing := True;

  end;
end;

procedure TfrmShaRKInPut.v1Column10PropertiesButtonClick(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('FactoryNo').Value := Trim(CDS_HZ.fieldbyname('Coid').AsString);
          FieldByName('FactoryName').Value := Trim(CDS_HZ.fieldbyname('coName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmShaRKInPut.v1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SXBATCHNO';
      flagname := '纱线批号';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('BatchNo').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
//  try
//    frmCaiGouListSel := TfrmCaiGouListSel.Create(Application);
//    with frmCaiGouListSel do
//    begin
//      if ShowModal = 1 then
//      begin
//        frmCaiGouListSel.Order_Main.DisableControls;
//        with frmCaiGouListSel.Order_Main do
//        begin
//          First;
//          while frmCaiGouListSel.Order_Main.Locate('SSel', True, []) do
//          begin
//            with Self.CDS_Sub do
//            begin
//              Edit;
//              FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
//              FieldByName('SQID').Value := Trim(frmCaiGouListSel.Order_Main.fieldbyname('SQID').AsString);
//              FieldByName('ToFactoryName').Value := frmCaiGouListSel.Order_Main.fieldbyname('CFFactoryName').Value;
//              FieldByName('FactoryName').Value := frmCaiGouListSel.Order_Main.fieldbyname('FactoryName').Value;
//              FieldByName('FactoryNo').Value := frmCaiGouListSel.Order_Main.fieldbyname('FactoryNo').Value;
//              FieldByName('Y_CodeName').Value := frmCaiGouListSel.Order_Main.fieldbyname('SPName').Value;
//              FieldByName('Y_Spec').Value := frmCaiGouListSel.Order_Main.fieldbyname('SPSpec').Value;
//              FieldByName('Y_CF').Value := frmCaiGouListSel.Order_Main.fieldbyname('SPCF').Value;
//              FieldByName('PiQty').Value := frmCaiGouListSel.Order_Main.fieldbyname('SQPS').Value;
//              FieldByName('Qty').Value := frmCaiGouListSel.Order_Main.fieldbyname('SQQty').Value;
//              FieldByName('Price').Value := frmCaiGouListSel.Order_Main.fieldbyname('SQPrice').Value;
//              FieldByName('QtyUnit').Value := frmCaiGouListSel.Order_Main.fieldbyname('SQQtyUnit').Value;
//              FieldByName('Price').Value := frmCaiGouListSel.Order_Main.fieldbyname('SQPrice').Value;
//
//            end;
//            frmCaiGouListSel.Order_Main.Delete;
//          end;
//        end;
//        frmCaiGouListSel.Order_Main.EnableControls;
//      end;
//    end;
//  finally
//    frmCaiGouListSel.Hide;
//  end;
end;

procedure TfrmShaRKInPut.v1Column15PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SXPinPai';
      flagname := '纱线品牌';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('SXPinPai').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmShaRKInPut.v1ComTaiTouPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'GSTTPB';
      flagname := '抬头';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('SyrName').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmShaRKInPut.ToolButton1Click(Sender: TObject);
begin

  CopyAddRow(Tv1, CDS_Sub);

  with Self.CDS_Sub do
  begin
    Edit;
    FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
    FieldByName('PiQty').Value := 0;
    FieldByName('Qty').Value := 0;
    FieldByName('Price').Value := 0;
    FieldByName('QtyUnit').Value := 'Kg';
    FieldByName('Price').Value := 0;
    Post;
  end;
end;

procedure TfrmShaRKInPut.ConNoBtnUpClick(Sender: TObject);
begin
  try
    frmContractListCGSel := TfrmContractListCGSel.Create(Application);
    with frmContractListCGSel do
    begin
      if ShowModal = 1 then
      begin
        Self.ConNo.Text := Trim(CDS_Main.fieldbyname('ConNo').AsString);
        CDS_Main.DisableControls;
        CDS_Main.first;
        while not CDS_Sub.Eof do
        begin

          with self.CDS_Sub do
          begin
            Append;
            self.CDS_Sub.Edit;
            self.CDS_Sub.FieldbyName('CRTime').Value := SGetServerDate10(ADOQueryTemp);
            self.CDS_Sub.FieldbyName('CRType').Value := '采购入库';
            self.CDS_Sub.FieldByName('Y_CodeName').Value := Trim(frmContractListCGSel.CDS_Sub.fieldbyname('P_Name').AsString);
            self.CDS_Sub.FieldByName('Y_Spec').Value := Trim(frmContractListCGSel.CDS_Sub.fieldbyname('P_Spec').AsString);
            self.CDS_Sub.FieldByName('Y_CF').Value := Trim(frmContractListCGSel.CDS_Sub.fieldbyname('P_CF').AsString);
            self.CDS_Sub.FieldByName('PiQty').Value := frmContractListCGSel.CDS_Sub.fieldbyname('PiQty').Value;
            self.CDS_Sub.FieldByName('Price').Value := frmContractListCGSel.CDS_Sub.fieldbyname('Price').Value;
            self.CDS_Sub.FieldByName('Qty').Value := frmContractListCGSel.CDS_Sub.fieldbyname('Qty').Value;
            self.CDS_Sub.FieldByName('QtyUnit').Value := Trim(frmContractListCGSel.CDS_Sub.fieldbyname('qtyUnit').AsString);
           // self.CDS_Sub.FieldByName('FactoryName').Value := Trim(frmContractListCGSel.CDS_Main.fieldbyname('qtyUnit').AsString);
            self.CDS_Sub.Post;
          end;

          CDS_Sub.next;
        end;
        CDS_Main.EnableControls;
      end;
    end;
  finally
    frmContractListCGSel.Hide;
  end;
end;

procedure TfrmShaRKInPut.ConNoBtnDnClick(Sender: TObject);
begin
  conno.Text := '';
end;

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

procedure TfrmShaRKInPut.v1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SXSPCF';
      flagname := '纱线成分';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('Y_CF').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmShaRKInPut.FactoryNameLPropertiesButtonClick(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;
          Self.CDS_Sub.FieldByName('FactoryName').Value := CDS_HZ.fieldbyname('Name').AsString;
          Self.CDS_Sub.FieldByName('FactType').Value := CDS_HZ.fieldbyname('FactType').AsString;
          Self.CDS_Sub.FieldByName('FactoryNo').Value := CDS_HZ.fieldbyname('Code').AsString;

        end;

      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmShaRKInPut.v2Column6PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQty, FPrice, FMxMoney: 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 := Trim(mvalue);
    Post;
  end;
  with CDS_Sub do
  begin
    Edit;
    FQty := FieldByName('Qty').AsFloat;
    FPrice := FieldByName('Price').AsFloat;
    Post;
  end;
  FMxMoney := strtofloat(format('%.2f', [FQty * FPrice]));
  with CDS_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    FieldbyName('MONEY').Value := FMxMoney;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();

end;

end.

