unit U_PBCGRKList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGrid, DBClient, cxCheckBox, cxCalendar, cxSplitter, RM_Dataset, RM_System,
  RM_Common, RM_Class, RM_GridReport, RM_e_Xls, Menus, cxButtonEdit,
  cxDropDownEdit, cxPC, Buttons, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, BtnEdit, dxSkinsCore, dxSkinDarkRoom, dxSkinOffice2013White,
  dxSkinSharpPlus, dxSkinSpringTime, dxSkinsDefaultPainters, dxSkinscxPCPainter,
  dxSkinBlack, dxSkinBlue, dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee,
  dxSkinDarkSide, dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSilver,
  dxSkinStardust, dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinValentine,
  dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue;

type
  TfrmPBCGRKList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    ToolButton1: TToolButton;
    Panel1: TPanel;
    v1Column1: TcxGridDBColumn;
    Label34: TLabel;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    ToolButton4: TToolButton;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport2: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    v1Column17: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Label2: TLabel;
    v1Column3: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    Label1: TLabel;
    Note: TEdit;
    labMYType: TLabel;
    v1MYType: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    CheckBox1: TCheckBox;
    v1MYMDPrice: TcxGridDBColumn;
    v1MYMDMoney: TcxGridDBColumn;
    v1JCDanWeiJC: TcxGridDBColumn;
    Label3: TLabel;
    v1filler: TcxGridDBColumn;
    Label4: TLabel;
    filler: TEdit;
    JCKHNo: TEdit;
    v1RKNo: TcxGridDBColumn;
    Label5: TLabel;
    CGName: TEdit;
    v1Column4: TcxGridDBColumn;
    N3: TMenuItem;
    v1Column6: TcxGridDBColumn;
    Label6: TLabel;
    PBBatchNo: TEdit;
    Tv1Column1: TcxGridDBColumn;
    Label7: TLabel;
    MYCode: TEdit;
    N4: TMenuItem;
    Panel3: TPanel;
    SpeedButton2: TSpeedButton;
    Label8: TLabel;
    PBBatchNo1: TEdit;
    Label9: TLabel;
    RCName1: TEdit;
    Label10: TLabel;
    ToRCName: TBtnEditA;
    Label32: TLabel;
    Label45: TLabel;
    Label50: TLabel;
    PS: TEdit;
    Qty: TEdit;
    KgQty: TEdit;
    SpeedButton3: TSpeedButton;
    Label11: TLabel;
    ADOCmd: TADOQuery;
    Label12: TLabel;
    CRTime: TDateTimePicker;
    Label13: TLabel;
    YGSunHao: TEdit;
    N5: TMenuItem;
    Panel4: TPanel;
    SpeedButton4: TSpeedButton;
    Label14: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    SpeedButton5: TSpeedButton;
    Label20: TLabel;
    Label21: TLabel;
    Label22: TLabel;
    PBBatchNo2: TEdit;
    RCName2: TEdit;
    KHName: TBtnEditA;
    PS2: TEdit;
    Qty2: TEdit;
    KgQty2: TEdit;
    CRTime2: TDateTimePicker;
    YGSunHao2: TEdit;
    Tv1Column2: TcxGridDBColumn;
    MYName: TComboBox;
    KHNameJC: TComboBox;
    ADOQuery1: TADOQuery;
    ADOQuery2: TADOQuery;
    KHNo: TEdit;
    JCDanWeiJC: TBtnEditA;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ZJStatusChange(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure ToRCNameBtnClick(Sender: TObject);
    procedure N5Click(Sender: TObject);
    procedure KHNameJCChange(Sender: TObject);
    procedure JCDanWeiJCBtnClick(Sender: TObject);
  private
    canshu1: string;
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function DelData(): Boolean;
    function SaveData(): Boolean;
    function SaveTHData(): Boolean;
    function YFData(FMainid: string): Boolean;
    { Private declarations }
  public
    FFInt, FCloth: Integer;

    { Public declarations }
  end;

var
  frmPBCGRKList: TfrmPBCGRKList;

implementation

uses
  U_DataLink, U_RTFun, U_QCRKInPut, U_ZDYHelp, U_LLRKInPut, U_GYSList,
  U_BPZdy_LRPB, U_LLRKInPutMH, U_LLRKInPutX, U_LLRKInPut_CG, U_LLRKInPutMH_CG,
  U_LLRKInPutMH_PBCG;

{$R *.dfm}

procedure TfrmPBCGRKList.FormDestroy(Sender: TObject);
begin
  frmPBCGRKList := nil;
end;

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

procedure TfrmPBCGRKList.FormCreate(Sender: TObject);
begin
  cxgrid1.Align := alClient;
  canshu1 := Trim(DParameters1);
end;

procedure TfrmPBCGRKList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('采购入库2', Tv1, '贸易生产管理');
  Close;
end;

procedure TfrmPBCGRKList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select A.* ');
      sql.Add(',KHNameJC=(select Top 1 B.KHNameJC from KH_Main B ');
      sql.Add(' where isnull(B.KHFlag,'''')=''GYS'' and isnull(B.Valid,'''')=''Y'' and B.KHName=A.KHName)');
      sql.Add(',JCDanWeiJC=(select Top 1 B.KHNameJC from KH_Main B ');
      sql.Add(' where isnull(B.KHFlag,'''')=''GYS'' and isnull(B.Valid,'''')=''Y'' and B.KHName=A.JCDanWei)');
      sql.Add(',JCKHNo=(select Top 1 B.KHNo from KH_Main B ');
      sql.Add(' where isnull(B.KHFlag,'''')=''GYS'' and isnull(B.Valid,'''')=''Y'' and B.KHName=A.JCDanWei)');
      sql.Add(' from CK_MYSC_CR A  ');
      sql.Add(' where 1=1 ');
      if CheckBox1.Checked = False then
      begin
        sql.Add(' and A.CRTime>=''' + Trim(FormatDateTime('yyyy-MM', now) + '-01') + '''');
        sql.Add(' and A.CRTime<''' + Trim(FormatDateTime('yyyy-MM-dd', now + 1)) + '''');
        BegDate.Visible := false;
        EndDate.Visible := false;
      end
      else
      begin
        sql.Add(' and A.CRTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
        sql.Add(' and A.CRTime<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
        BegDate.Visible := true;
        EndDate.Visible := true;
      end;
      sql.Add(' and isnull(CRType,'''') in(''采购入库'',''坯布退货'',''坯布转厂'') ');
      SQL.Add('and isnull(MYType,'''')=''采购坯布''');
      if canshu1 <> '高权限' then
      begin
        sql.add(' and filler=''' + trim(DName) + '''');
      end;
//      ShowMessage(sql.text);
      Open;
    end;
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;

  with ADOQuery1 do
  begin
    Filtered := False;
    Close;
    sql.Clear;
    sql.Add(' select distinct(isnull(MYName,'''')) AS name ');
    sql.Add(' from CK_MYSC_CR A  ');
    sql.Add(' where 1=1 ');
    if CheckBox1.Checked = False then
    begin
      sql.Add(' and A.CRTime>=''' + Trim(FormatDateTime('yyyy-MM', now) + '-01') + '''');
      sql.Add(' and A.CRTime<''' + Trim(FormatDateTime('yyyy-MM-dd', now + 1)) + '''');
      BegDate.Visible := false;
      EndDate.Visible := false;
    end
    else
    begin
      sql.Add(' and A.CRTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
      sql.Add(' and A.CRTime<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
      BegDate.Visible := true;
      EndDate.Visible := true;
    end;
    sql.Add(' and isnull(CRType,'''') in(''采购入库'',''坯布退货'',''坯布转厂'') ');
    SQL.Add('and isnull(MYType,'''')=''采购坯布''');
    if canshu1 <> '高权限' then
    begin
      sql.add(' and filler=''' + trim(DName) + '''');
    end;
//    ShowMessage(sql.text);
    Open;
  end;

  MYName.Items.Clear; //清空
  MYName.Items.Add(Trim(''));
  while not ADOQuery1.eof do
  begin
    MYName.Items.Add(ADOQuery1.fieldbyname('name').AsString);
    ADOQuery1.next;
  end;

  with ADOQuery1 do
  begin
    Filtered := False;
    Close;
    sql.Clear;
    sql.Add(' select distinct(isnull(KHNameJC,'''')) AS name from (');
    sql.Add(' select KHNameJC=(select Top 1 B.KHNameJC from KH_Main B ');
    sql.Add(' where isnull(B.KHFlag,'''')=''GYS'' and isnull(B.Valid,'''')=''Y'' and B.KHName=A.KHName)');
    sql.Add(' from CK_MYSC_CR A  ');
    sql.Add(' where 1=1 ');
    if CheckBox1.Checked = False then
    begin
      sql.Add(' and A.CRTime>=''' + Trim(FormatDateTime('yyyy-MM', now) + '-01') + '''');
      sql.Add(' and A.CRTime<''' + Trim(FormatDateTime('yyyy-MM-dd', now + 1)) + '''');
      BegDate.Visible := false;
      EndDate.Visible := false;
    end
    else
    begin
      sql.Add(' and A.CRTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
      sql.Add(' and A.CRTime<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
      BegDate.Visible := true;
      EndDate.Visible := true;
    end;
    sql.Add(' and isnull(CRType,'''') in(''采购入库'',''坯布退货'',''坯布转厂'') ');
    SQL.Add('and isnull(MYType,'''')=''采购坯布''');
    if canshu1 <> '高权限' then
    begin
      sql.add(' and filler=''' + trim(DName) + '''');
    end;
    sql.Add(')AA ');
//    ShowMessage(sql.text);
    Open;
  end;

  KHNameJC.Items.Clear; //清空
  KHNameJC.Items.Add(Trim(''));
  while not ADOQuery1.eof do
  begin
    KHNameJC.Items.Add(ADOQuery1.fieldbyname('name').AsString);
    ADOQuery1.next;
  end;
end;

procedure TfrmPBCGRKList.InitForm();
begin
//  BegDate.Date := StrToDate(formatdatetime('yyyy-MM', SGetServerDate(ADOQueryTemp)) + '-01');
  BegDate.Date := SGetServerDate(ADOQueryTemp);
  EndDate.Date := SGetServerDate(ADOQueryTemp);
  ReadCxGrid('采购入库2', Tv1, '贸易生产管理');
  if canshu1 = '高权限' then
  begin
    N3.Visible := true;
  end
  else
  begin
    N3.Visible := false;
  end;
  InitGrid();
end;

procedure TfrmPBCGRKList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
end;

function TfrmPBCGRKList.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      //First;
     // while Order_Main.Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' delete YS_Money_CR where YFTypeId=''' + Trim(Order_Main.fieldbyname('MYID').AsString) + '''');
          sql.Add(' and isnull(YFTypeId,'''')<>'''' ');
          sql.Add(' insert into CK_MYSC_CR_Log  ');
          sql.Add(' select * from CK_MYSC_CR where MYID=''' + Trim(Order_Main.fieldbyname('MYID').AsString) + '''');
          sql.Add(' UPdate CK_MYSC_CR_Log Set DelTime=getdate(),DelerCode=''' + Trim(DCode) + ''',Deler=''' + Trim(DName) + '''');
          sql.Add(' where MYID=''' + Trim(Order_Main.fieldbyname('MYID').AsString) + '''');
          sql.Add(' Delete CK_MYSC_CR  where MYId=''' + Trim(Order_Main.fieldbyname('MYId').AsString) + '''');
          sql.Add(' delete from TP_File where WBID=''' + trim(Order_Main.fieldbyname('MYId').AsString) + ''' ');
          sql.Add(' and TFType=''采购坯布''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' exec P_Update_PBKC :PBBatchNo,:JGCNO');
          Parameters.ParamByName('PBBatchNo').Value := Trim(Order_Main.fieldbyname('PBBatchNo').AsString);
          Parameters.ParamByName('JGCNO').Value := Trim(Order_Main.fieldbyname('JCNO').AsString);
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' exec P_Update_PBKC :PBBatchNo,:JGCNO');
          Parameters.ParamByName('PBBatchNo').Value := Trim(Order_Main.fieldbyname('PBBatchNo').AsString);
          Parameters.ParamByName('JGCNO').Value := Trim(Order_Main.fieldbyname('FromJCNO').AsString);
          ExecSQL;
        end;
        Order_Main.Delete;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result := False;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

procedure TfrmPBCGRKList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(Self.Caption, cxGrid1);
end;

procedure TfrmPBCGRKList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
  TBFind.Click;
end;

procedure TfrmPBCGRKList.TBAddClick(Sender: TObject);
begin

  try
    frmLLRKInPutMH_PBCG := TfrmLLRKInPutMH_PBCG.Create(Application);
    with frmLLRKInPutMH_PBCG do
    begin
      PState := 0;
      FMainId := '';
      FMYType := '采购坯布';
      FMYTypeFlag := 'CGP';
      labMYType.Caption := '(坯布)采购';
      FML := '坯布';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPutMH_PBCG.Free;
  end;
end;

procedure TfrmPBCGRKList.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmPBCGRKList.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmPBCGRKList.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmPBCGRKList.ToolButton1Click(Sender: TObject);
var
  FFMYType: string;
begin
  FFMYType := Trim(Order_Main.fieldbyname('MYType').AsString);
  try
    frmLLRKInPutMH_PBCG := TfrmLLRKInPutMH_PBCG.Create(Application);
    with frmLLRKInPutMH_PBCG do
    begin
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('MYId').AsString);
      FMYType := Trim(Self.Order_Main.fieldbyname('MYType').AsString);
      FMYTypeFlag := Trim(Self.Order_Main.fieldbyname('MYTypeFlag').AsString);
      labMYType.Caption := '(坯布)采购';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmLLRKInPutMH_PBCG.Free;
  end;
end;

procedure TfrmPBCGRKList.CustomerNoNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);
end;

procedure TfrmPBCGRKList.N2Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR where YFTypeId=''' + Trim(Self.Order_Main.fieldbyname('MYId').AsString) + '''');
    sql.Add(' and ChkStatusP=''已审核''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = false then
  begin
    Application.MessageBox('应付款已审核不能操作', '提示');
    exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_MYSC_CR_MX where MYID=''' + Trim(Order_Main.fieldbyname('MYID').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已产生明细数据，不能删除!', '提示', 0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR where YFTypeId=''' + trim(Order_Main.fieldbyname('MYID').AsString) + '''');
    sql.Add(' and (isnull(ChkerP,'''')<>'''' ');
    sql.Add(' or isnull(ChkerQ,'''')<>'''') ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = false then
  begin
    Application.MessageBox('应付款已审核,不能删除', '提示');
    exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if DelData() then
  begin

  end;
end;

procedure TfrmPBCGRKList.N1Click(Sender: TObject);
var
  FFMYType: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Trim(canshu1) <> '高权限' then
  begin
    if Order_Main.FieldByName('filltime').AsDateTime + 1 < Now then
    begin
      application.MessageBox('超过一天不能修改', '提示');
      Exit;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR where YFTypeId=''' + Trim(Self.Order_Main.fieldbyname('MYId').AsString) + '''');
    sql.Add(' and ChkStatusP=''已审核''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = false then
  begin
    Application.MessageBox('应付款已审核不能操作', '提示');
    exit;
  end;
  FFMYType := Trim(Order_Main.fieldbyname('MYType').AsString); //加限制财务ChkerP=''已审核''
  if Trim(Order_Main.fieldbyname('CRType').AsString) = '采购入库' then
  begin
    try
      frmLLRKInPutMH_PBCG := TfrmLLRKInPutMH_PBCG.Create(Application);
      with frmLLRKInPutMH_PBCG do
      begin
        PState := 1;
        FMainId := Trim(Self.Order_Main.fieldbyname('MYId').AsString);
        FMYType := Trim(Self.Order_Main.fieldbyname('MYType').AsString);
        FMYTypeFlag := Trim(Self.Order_Main.fieldbyname('MYTypeFlag').AsString);
        labMYType.Caption := '(坯布)采购';
        if ShowModal = 1 then
        begin
          InitGrid();
        end;
      end;
    finally
      frmLLRKInPutMH_PBCG.Free;
    end;
  end
  else if Trim(Order_Main.fieldbyname('CRType').AsString) = '坯布转厂' then
  begin
    Panel3.Visible := True;
    PBBatchNo1.Text := Trim(Order_Main.fieldbyname('PBBatchNo').AsString);
    PBBatchNo1.Hint := Trim(Order_Main.fieldbyname('MYId').AsString);
    RCName1.Text := Trim(Order_Main.fieldbyname('FromJCName').AsString);
    RCName1.Hint := Trim(Order_Main.fieldbyname('FromJCNo').AsString);
    YGSunHao.Text := Trim(Order_Main.fieldbyname('YGSunHao').AsString);
    CRTime.Date := Order_Main.fieldbyname('YGSunHao').AsDateTime;
    ToRCName.Text := Trim(Order_Main.fieldbyname('JCDanWei').AsString);
    ToRCName.TxtCode := Trim(Order_Main.fieldbyname('JCNo').AsString);
    PS.Text := Trim(Order_Main.fieldbyname('PS').AsString);
    Qty.Text := Trim(Order_Main.fieldbyname('Qty').AsString);
    KgQty.Text := Trim(Order_Main.fieldbyname('KgQty').AsString);
  end
  else if Trim(Order_Main.fieldbyname('CRType').AsString) = '坯布退货' then
  begin
    Panel4.Visible := True;
    PBBatchNo2.Text := Trim(Order_Main.fieldbyname('PBBatchNo').AsString);
    PBBatchNo2.Hint := Trim(Order_Main.fieldbyname('MYId').AsString);
    RCName2.Text := Trim(Order_Main.fieldbyname('FromJCName').AsString);
    RCName2.Hint := Trim(Order_Main.fieldbyname('FromJCNo').AsString);
    YGSunHao2.Text := Trim(Order_Main.fieldbyname('YGSunHao').AsString);
    CRTime2.Date := Order_Main.fieldbyname('YGSunHao').AsDateTime;
    KHName.Text := Trim(Order_Main.fieldbyname('KHName').AsString);
    KHName.TxtCode := Trim(Order_Main.fieldbyname('KHNo').AsString);
    PS2.Text := Trim(Order_Main.fieldbyname('PS').AsString);
    Qty2.Text := Trim(Order_Main.fieldbyname('Qty').AsString);
    KgQty2.Text := Trim(Order_Main.fieldbyname('KgQty').AsString);
  end;

end;

procedure TfrmPBCGRKList.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmPBCGRKList.ZJStatusChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmPBCGRKList.ToolButton4Click(Sender: TObject);
var
  fPrintFile: string;
begin
  if Order_Main.IsEmpty then
    Exit;
  if Trim(Order_Main.fieldbyname('Chker').AsString) = '' then
    exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\检测报告.rmf';
  RMXLSExport2 := TRMXLSExport.Create(RMXLSExport2);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Test_Report where TRId=''' + Trim(Order_Main.fieldbyname('TRId').AsString) + '''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_PRT);
  SInitCDSData20(ADOQueryTemp, CDS_PRT);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['OrderUnit']:=Order_Main.fieldbyname('OrderUnit').Value;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\检测报告.rmf'), '提示', 0);
  end;
end;

procedure TfrmPBCGRKList.Tv1DblClick(Sender: TObject);
begin
  ToolButton1.Click;
end;

procedure TfrmPBCGRKList.SpeedButton4Click(Sender: TObject);
var
  FReal: Double;
  FInt: Integer;
begin
  if Trim(PBBatchNo2.Text) = '' then
  begin
    Application.MessageBox('坯布联系号不能为空！', '提示', 0);
    Exit;
  end;
  if Trim(RCName2.Text) = '' then
  begin
    Application.MessageBox('染厂不能为空！', '提示', 0);
    Exit;
  end;
  if Trim(KHName.Text) = '' then
  begin
    Application.MessageBox('退给供应商不能为空！', '提示', 0);
    Exit;
  end;
  if Trim(PS2.Text) = '' then
  begin
    Application.MessageBox('匹数不能为空!', '提示', 0);
    Exit;
  end;
  if TryStrToInt(PS2.Text, FInt) = False then
  begin
    Application.MessageBox('匹数非法数字!', '提示', 0);
    exit;
  end;
  if (Trim(Qty2.Text) = '') and (Trim(KgQty2.text) = '') then
  begin
    Application.MessageBox('米数和公斤数不能同时为空!', '提示', 0);
    exit;
  end;
  if Trim(Qty2.Text) <> '' then
  begin
    if TryStrToFloat(Qty2.Text, FReal) = False then
    begin
      Application.MessageBox('米数非法数字!', '提示', 0);
      exit;
    end;
  end;
  if Trim(KgQty2.Text) <> '' then
  begin
    if TryStrToFloat(KgQty2.Text, FReal) = False then
    begin
      Application.MessageBox('公斤数非法数字!', '提示', 0);
      exit;
    end;
  end;
  if SaveTHData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    Panel3.Visible := False;
  end;

end;

procedure TfrmPBCGRKList.SpeedButton3Click(Sender: TObject);
begin
  Panel3.Visible := False;
end;

procedure TfrmPBCGRKList.Button1Click(Sender: TObject);
begin
  try
    frmGYSList := TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      KHType.Text := '染厂';
      KHType.TxtCode := '染厂';
      if ShowModal = 1 then
      begin
        Self.JCDanWeiJC.Text := Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
        Self.JCKHNo.Text := Trim(frmGYSList.Order_Main.fieldbyname('KHNo').AsString);
        TBFind.Click;
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmPBCGRKList.N3Click(Sender: TObject);
begin
  frmZDYHelp := TfrmZDYHelp.Create(self);
  with frmZDYHelp do
  begin
    flag := 'filler';
    flagname := '操作人';
    if ShowModal = 1 then
    begin
      with ADOQueryTemp do
      begin
        Close;
        SQL.Clear;
        sql.Add('update CK_MYSC_CR set filler=''' + trim(ClientDataSet1.fieldbyname('filler').AsString) + ''' ');
        sql.Add(' where MYId=''' + trim(Order_Main.fieldbyname('MYID').AsString) + '''');
        ExecSQL;
      end;
      with Order_Main do
      begin
        edit;
        FieldByName('filler').AsString := Trim(ClientDataSet1.fieldbyname('filler').AsString);
      end;
    end;
    free;
  end;
end;

procedure TfrmPBCGRKList.N4Click(Sender: TObject);
begin
  Panel3.Visible := True;
  PBBatchNo1.Text := Trim(Order_Main.fieldbyname('PBBatchNo').AsString);
  PBBatchNo1.Hint := '';
  RCName1.Text := Trim(Order_Main.fieldbyname('JCDanWeiJC').AsString);
  RCName1.Hint := Trim(Order_Main.fieldbyname('JCNO').AsString);
  YGSunHao.Text := Trim(Order_Main.fieldbyname('YGSunHao').AsString);
  CRTime.Date := SGetServerDate(ADOQueryTemp);
  ToRCName.Text := '';
  ToRCName.TxtCode := '';
  PS.Text := '';
  Qty.Text := '';
  KgQty.Text := '';
end;

function TfrmPBCGRKList.SaveData(): Boolean;
var
  maxno, maxmxno, maxnoflag, fsj, FMYID: string;
begin
  Result := False;
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    FMYID := Trim(PBBatchNo1.Hint);
    if Trim(FMYID) = '' then
    begin
      if GetLSNo(ADOCmd, maxno, 'ZC', 'CK_MYSC_CR', 3, 1) = False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!', '提示', 0);
        Exit;
      end;
    end
    else
    begin
      maxno := Trim(FMYID);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from CK_MYSC_CR where MYId=''' + Trim(maxno) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if isempty then
      begin
        Append;
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('FillerCode').Value := Trim(DCode);
      end
      else
      begin
        Edit;
      end;
      FieldByName('MYId').Value := Trim(maxno);
      FieldByName('CRType').Value := '坯布转厂';
      FieldByName('CRFlag').Value := '出库';
      FieldByName('CRQtyFlag').Value := -1;
      if Trim(FMYID) = '' then
      begin

      end
      else
      begin
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditerCode').Value := Trim(DCode);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTemp);
      end;
      fieldbyname('KHName').value := Order_Main.fieldbyname('KHName').Value;
      fieldbyname('KHCode').value := Order_Main.fieldbyname('KHNo').Value;
      fieldbyname('MYName').value := Order_Main.fieldbyname('MYName').Value;
      fieldbyname('MYCode').value := Order_Main.fieldbyname('MYCode').Value;
      fieldbyname('CGName').value := Order_Main.fieldbyname('CGName').Value;
      fieldbyname('PBBatchNo').value := Order_Main.fieldbyname('PBBatchNo').Value;
      fieldbyname('MYMF').value := Order_Main.fieldbyname('MYMF').Value;
      fieldbyname('MYKZ').value := Order_Main.fieldbyname('MYKZ').Value;
      fieldbyname('MYCF').value := Order_Main.fieldbyname('MYCF').Value;
      fieldbyname('MYJWS').value := Order_Main.fieldbyname('MYJWS').Value;
      FieldByName('RKNo').Value := Trim(maxno);
      FieldByName('MYType').Value := Trim('采购坯布');
      FieldByName('MYTypeFlag').Value := Trim('CGP');
      RTSetsavedata(ADOCmd, 'CK_MYSC_CR', Panel3, 2);
      fieldbyname('JCDanWei').value := Trim(ToRCName.Text);
      fieldbyname('JCNo').value := Trim(ToRCName.TxtCode);
      fieldbyname('FromJCNo').value := Trim(RCName1.Hint);
      fieldbyname('FromJCName').value := Trim(RCName1.Text);
      Post;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Update_PBKC :PBBatchNo,:JGCNO');
      Parameters.ParamByName('PBBatchNo').Value := Trim(PBBatchNo.Text);
      Parameters.ParamByName('JGCNO').Value := Trim(RCName1.Hint);
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Update_PBKC :PBBatchNo,:JGCNO');
      Parameters.ParamByName('PBBatchNo').Value := Trim(PBBatchNo.Text);
      Parameters.ParamByName('JGCNO').Value := Trim(ToRCName.TxtCode);
      ExecSQL;
    end;
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

function TfrmPBCGRKList.SaveTHData(): Boolean;
var
  maxno, maxmxno, maxnoflag, fsj, FMYID: string;
begin
  Result := False;
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    FMYID := Trim(PBBatchNo2.Hint);
    if Trim(FMYID) = '' then
    begin
      if GetLSNo(ADOCmd, maxno, 'TH', 'CK_MYSC_CR', 3, 1) = False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!', '提示', 0);
        Exit;
      end;
    end
    else
    begin
      maxno := Trim(FMYID);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from CK_MYSC_CR where MYId=''' + Trim(maxno) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if isempty then
      begin
        Append;
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('FillerCode').Value := Trim(DCode);
      end
      else
      begin
        Edit;
      end;
      FieldByName('MYId').Value := Trim(maxno);
      FieldByName('CRType').Value := '坯布退货';
      FieldByName('CRFlag').Value := '出库';
      FieldByName('CRQtyFlag').Value := -1;
      if Trim(FMYID) = '' then
      begin

      end
      else
      begin
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditerCode').Value := Trim(DCode);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTemp);
      end;
      fieldbyname('KHName').value := Order_Main.fieldbyname('KHName').Value;
      fieldbyname('KHCode').value := Order_Main.fieldbyname('KHNo').Value;
      fieldbyname('MYName').value := Order_Main.fieldbyname('MYName').Value;
      fieldbyname('MYCode').value := Order_Main.fieldbyname('MYCode').Value;
      fieldbyname('CGName').value := Order_Main.fieldbyname('CGName').Value;
      fieldbyname('PBBatchNo').value := Order_Main.fieldbyname('PBBatchNo').Value;
      fieldbyname('MYMF').value := Order_Main.fieldbyname('MYMF').Value;
      fieldbyname('MYKZ').value := Order_Main.fieldbyname('MYKZ').Value;
      fieldbyname('MYCF').value := Order_Main.fieldbyname('MYCF').Value;
      fieldbyname('MYJWS').value := Order_Main.fieldbyname('MYJWS').Value;
      FieldByName('RKNo').Value := Trim(maxno);
      FieldByName('MYType').Value := Trim('采购坯布');
      FieldByName('MYTypeFlag').Value := Trim('CGP');
      FieldByName('CRTime').Value := CRTime2.Date;
      if Trim(PS2.Text) <> '' then
      begin
        FieldByName('PS').Value := PS2.Text;
      end
      else
      begin
        FieldByName('PS').Value := NUll;
      end;
      if Trim(Qty2.Text) <> '' then
      begin
        FieldByName('Qty').Value := Qty2.Text;
      end
      else
      begin
        FieldByName('Qty').Value := NUll;
      end;
      if Trim(KgQty2.Text) <> '' then
      begin
        FieldByName('KgQty').Value := KgQty2.Text;
      end
      else
      begin
        FieldByName('KgQty').Value := NUll;
      end;
      fieldbyname('JCDanWei').value := Order_Main.fieldbyname('JCDanWei').Value;
      fieldbyname('JCNo').value := Order_Main.fieldbyname('JCNo').Value;
      FieldByName('MDMflag').Value := Order_Main.fieldbyname('MDMflag').Value;
      FieldByName('MDKGflag').Value := Order_Main.fieldbyname('MDKGflag').Value;
      FieldByName('HSFlag').AsBoolean := Order_Main.fieldbyname('HSFlag').Value;
      FieldByName('BHSFlag').AsBoolean := Order_Main.fieldbyname('BHSFlag').Value;
      fieldbyname('MYMDPrice').Value := Order_Main.fieldbyname('MYMDPrice').Value;
      Post;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      if Trim(Qty2.Text) <> '' then
      begin
        sql.Add('Update CK_MYSC_CR Set MYMDMoney=Qty*MYMDPrice*(-1) where MYId=''' + Trim(maxno) + '''');
      end
      else if Trim(KgQty2.Text) <> '' then
      begin
        sql.Add('Update CK_MYSC_CR Set MYMDMoney=KgQty*MYMDPrice*(-1) where MYId=''' + Trim(maxno) + '''');
      end;
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' exec P_Update_PBKC :PBBatchNo,:JGCNO');
      Parameters.ParamByName('PBBatchNo').Value := Trim(PBBatchNo.Text);
      Parameters.ParamByName('JGCNO').Value := Trim(Order_Main.fieldbyname('JCNo').AsString);
      ExecSQL;
    end;
    YFData(maxno);
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

function TfrmPBCGRKList.YFData(FMainId: string): Boolean;
var
  CRID, OrdMainId, YFID, FComTaiTou, FCRID, FFactoryName, FFactoryNo: string;
begin
  Result := False;
  FFactoryName := Trim(KHName.Text);
  FFactoryNo := Trim(KHNo.Text);
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YS_Money_KC where FactoryName=' + quotedstr(Trim(FFactoryName)));
    sql.Add(' and KCType=''KH'' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    CRID := ADOQueryTemp.fieldbyname('CRID').AsString;
  end
  else
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YS_Money_CRID');
      Open;
    end;
    if ADOQueryTemp.IsEmpty = False then
    begin
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YS_Money_CRID set CRID=CRID+1');
        sql.Add('select * from YS_Money_CRID ');
        Open;
      end;
    end
    else
    begin
      with ADOCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('insert into YS_Money_CRID select 0 ');
        sql.Add('select * from YS_Money_CRID ');
        Open;
      end;
    end;

    CRID := ADOCmd.fieldbyname('CRID').AsString;
    with ADOCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from YS_Money_KC where 1=2');
      Open;
    end;
    with ADOCmd do
    begin
      Append;
      FieldByName('CRID').Value := StrToInt(CRID);
      FieldByName('FactoryName').Value := Trim(FFactoryName);
      FieldByName('FactoryNo').Value := Trim(FFactoryNo);
      FieldByName('ZdyStr1').Value := '应付付';
      FieldByName('KCType').Value := 'KH';
      Post;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR where FactoryNo=' + quotedstr(Trim(FFactoryNo)));
    sql.Add(' and YFTypeId=''' + Trim(FMainId) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    if GetLSNo(ADOCmd, YFID, 'PB', 'YS_Money_CR', 3, 1) = False then
    begin
      Application.MessageBox('取应付最大号失败!', '提示', 0);
      Exit;
    end;
  end
  else
  begin
    YFID := Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
  end;
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YS_Money_CR where YFID=''' + Trim(YFID) + '''');
    Open;
  end;
  with ADOCmd do
  begin
    if ADOQueryTemp.IsEmpty then
    begin
      Append;
      FieldByName('SCQty').Value := strtofloatdef(Qty2.text, 0) * (-1);
      FieldByName('SCPS').Value := strtofloatdef(PS2.text, 0) * (-1);
    end
    else
      Edit;
    FieldByName('YFID').Value := Trim(YFID);
    FieldByName('YFTypeId').Value := Trim(FMainId);
    FieldByName('CRID').Value := StrToInt(CRID);
    FieldByName('YFName').Value := '坯布退货';
    FieldByName('Filler').Value := Trim(DName);
    FieldByName('CRType').Value := '应付款登记';
    FieldByName('CRFlag').Value := '应付付';
    if Trim(PBBatchNo2.Hint) = '' then
    begin
      FieldByName('ChkStatusP').Value := '未审核';
      FieldByName('ChkStatusQ').Value := '未审核';
    end;
    FieldByName('QtyFlag').Value := 1;
    FieldByName('FactoryName').Value := Trim(FFactoryName);
    FieldByName('FactoryNo').Value := Trim(FFactoryNo);
    FieldByName('CRTime').Value := CRTime.datetime;
    if Order_Main.FieldByName('MDMFlag').AsBoolean = True then
    begin
      FieldByName('Qty').Value := Strtofloatdef(Qty2.text, 0) * (-1);
      FieldByName('QtyUnit').Value := 'M';
    end
    else
    begin
      FieldByName('Qty').Value := Strtofloatdef(KGQty2.text, 0) * (-1);
      FieldByName('QtyUnit').Value := 'KG';
    end;
    FieldByName('PS').Value := Strtofloatdef(PS2.text, 0) * (-1);
    FieldByName('YFType').Value := '自动生成';
    FieldByName('Price').Value := Order_Main.fieldbyname('MYMDPrice').Value;
    if order_Main.fieldbyname('HSFlag').AsBoolean = true then
    begin
      FieldByName('HanShui').Value := '含税';
    end
    else
    begin
      FieldByName('HanShui').Value := '不含税';
    end;
    FieldByName('CKNO').Value := trim(FMainId);
    FieldByName('SJQty').Value := Strtofloatdef(Qty2.text, 0);
    FieldByName('MiQty').Value := Strtofloatdef(Qty2.text, 0);
    FieldByName('KGQty').Value := Strtofloatdef(KGQty2.text, 0);

    FieldByName('P_CodeName').Value := Trim(Order_Main.FieldByName('MYName').ASstring);
    FieldByName('P_MF').Value := Trim(Order_Main.FieldByName('MYMF').ASstring);
    FieldByName('P_KZ').Value := Trim(Order_Main.FieldByName('MYKZ').ASstring);
    FieldByName('P_Code').Value := Trim(Order_Main.FieldByName('MYCode').ASstring);
    FieldByName('P_Spec').Value := Trim(Order_Main.FieldByName('MYCF').ASstring);
    FieldByName('BZType').Value := '￥';
    FieldByName('status').Value := '0';
    Post;
  end;
  with ADOCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update YS_Money_CR Set money=Qty*Price,JSmoney=Qty*Price,HSmoney=Qty*Price where YFID=''' + Trim(YFID) + '''');
    ExecSQL;
  end;
  with ADOCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YS_Money_KC Set KCMoney=(select isnull(Sum(HSMoney*QtyFlag),0) from YS_Money_CR A where A.CRID=YS_Money_KC.CRID)');
    sql.Add(' where CRID=' + CRID);
    ExecSQL;
  end;
  Result := True;
end;

procedure TfrmPBCGRKList.SpeedButton2Click(Sender: TObject);
var
  FReal: Double;
  FInt: Integer;
begin
  if Trim(PBBatchNo1.Text) = '' then
  begin
    Application.MessageBox('坯布联系号不能为空！', '提示', 0);
    Exit;
  end;
  if Trim(RCName1.Text) = '' then
  begin
    Application.MessageBox('染厂不能为空！', '提示', 0);
    Exit;
  end;
  if Trim(ToRCName.Text) = '' then
  begin
    Application.MessageBox('转到染厂不能为空！', '提示', 0);
    Exit;
  end;
  if Trim(PS.Text) = '' then
  begin
    Application.MessageBox('匹数不能为空!', '提示', 0);
    Exit;
  end;
  if TryStrToInt(PS.Text, FInt) = False then
  begin
    Application.MessageBox('匹数非法数字!', '提示', 0);
    exit;
  end;
  if (Trim(Qty.Text) = '') and (Trim(KgQty.text) = '') then
  begin
    Application.MessageBox('米数和公斤数不能同时为空!', '提示', 0);
    exit;
  end;
  if Trim(Qty.Text) <> '' then
  begin
    if TryStrToFloat(Qty.Text, FReal) = False then
    begin
      Application.MessageBox('米数非法数字!', '提示', 0);
      exit;
    end;
  end;
  if Trim(KgQty.Text) <> '' then
  begin
    if TryStrToFloat(KgQty.Text, FReal) = False then
    begin
      Application.MessageBox('公斤数非法数字!', '提示', 0);
      exit;
    end;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    Panel3.Visible := False;
  end;

end;

procedure TfrmPBCGRKList.ToRCNameBtnClick(Sender: TObject);
begin
  try
    frmGYSList := TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      KHType.Text := '染厂';
      KHType.TxtCode := '染厂';
      if ShowModal = 1 then
      begin
        Self.ToRCName.Text := Trim(frmGYSList.Order_Main.fieldbyname('KHName').AsString);
        Self.ToRCName.TxtCode := Trim(frmGYSList.Order_Main.fieldbyname('KHNo').AsString);
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

procedure TfrmPBCGRKList.N5Click(Sender: TObject);
begin
  Panel4.Visible := True;
  PBBatchNo2.Text := Trim(Order_Main.fieldbyname('PBBatchNo').AsString);
  PBBatchNo2.Hint := '';
  RCName2.Text := Trim(Order_Main.fieldbyname('JCDanWeiJC').AsString);
  RCName2.Hint := Trim(Order_Main.fieldbyname('JCNO').AsString);
  YGSunHao2.Text := Trim(Order_Main.fieldbyname('YGSunHao').AsString);
  CRTime2.Date := SGetServerDate(ADOQueryTemp);
  KHName.Text := Trim(Order_Main.fieldbyname('KHName').AsString);
  KHName.TxtCode := Trim(Order_Main.fieldbyname('KHNo').AsString);
  PS.Text := '';
  Qty.Text := '';
  KgQty.Text := '';
end;

procedure TfrmPBCGRKList.KHNameJCChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, Order_Main);
  SInitCDSData20(ADOQueryMain, Order_Main);

  with ADOQuery2 do
  begin
    Filtered := False;
    Close;
    sql.Clear;
    sql.Add(' select Top 1 B.KHNo from KH_Main B ');
    sql.Add(' where isnull(B.KHFlag,'''')=''GYS'' and isnull(B.Valid,'''')=''Y'' ');
    sql.Add(' and KHNameJC=''' + trim(KHNameJC.text) + '''');
//    ShowMessage(sql.text);
    Open;
  end;
  KHNo.Text := Trim(ADOQuery2.fieldbyname('KHNo').AsString);
end;

procedure TfrmPBCGRKList.JCDanWeiJCBtnClick(Sender: TObject);
begin
  try
    frmGYSList := TfrmGYSList.Create(Application);
    with frmGYSList do
    begin
      KHType.Text := '染厂';
      KHType.TxtCode := '染厂';
      if ShowModal = 1 then
      begin
        Self.JCDanWeiJC.Text := Trim(frmGYSList.Order_Main.fieldbyname('KHNameJC').AsString);
        Self.JCKHNo.Text := Trim(frmGYSList.Order_Main.fieldbyname('KHNo').AsString);
        TBFind.Click;
      end;
    end;
  finally
    frmGYSList.Free;
  end;
end;

end.

