unit U_ProductOrderAnPaiGQX;

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, cxPC, BtnEdit, cxTextEdit, cxButtonEdit, cxDropDownEdit, MovePanel;

type
  TfrmProductOrderAnPaiGQX = class(TForm)
    ToolBar1: TToolBar;
    TBPrint: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    ToolButton1: TToolButton;
    DataSource2: TDataSource;
    CDS_Sub: TClientDataSet;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    V2Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ToolBar2: TToolBar;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    ADOQueryPrint: TADOQuery;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    v1OrdDefStr1: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v1OrdPerson1: TcxGridDBColumn;
    v1JGFactoryName: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    v1MPRTCodeName: TcxGridDBColumn;
    v1MPRTSpec: TcxGridDBColumn;
    v1MPRTCF: TcxGridDBColumn;
    v1MPRTMF: TcxGridDBColumn;
    v1MPRTKZ: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    V2Column2: TcxGridDBColumn;
    V2Column3: TcxGridDBColumn;
    V2Column4: TcxGridDBColumn;
    Panel1: TPanel;
    Label3: TLabel;
    OrderNoM: TEdit;
    cxGrid4: TcxGrid;
    Tv4: TcxGridDBTableView;
    v4Column1: TcxGridDBColumn;
    v4Column2: TcxGridDBColumn;
    cxGrid4Level1: TcxGridLevel;
    CDS_OrderNo: TClientDataSet;
    DS_OrderNo: TDataSource;
    V2Column5: TcxGridDBColumn;
    V2Column6: TcxGridDBColumn;
    V2Column7: TcxGridDBColumn;
    V2Column8: TcxGridDBColumn;
    V2Column9: TcxGridDBColumn;
    ToolButton4: TToolButton;
    ADOQueryHC: TADOQuery;
    Panel3: TPanel;
    DateTimePicker1: TDateTimePicker;
    BtnEditA1: TBtnEditA;
    ComboBox1: TComboBox;
    BtnEditA2: TBtnEditA;
    Label1: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    BtnEditA3: TBtnEditA;
    Label6: TLabel;
    V2Column10: TcxGridDBColumn;
    ToolButton7: TToolButton;
    ToolButton8: TToolButton;
    V2Column11: TcxGridDBColumn;
    BtnEditA4: TBtnEditA;
    Label7: TLabel;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    cxGridPopupMenu2: TcxGridPopupMenu;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    ToolButton9: TToolButton;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    ToolButton10: TToolButton;
    V2Column12: TcxGridDBColumn;
    MovePanel2: TMovePanel;
    ToolButton11: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBTPClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView;
      ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem;
      out AStyle: TcxStyle);
    procedure v1DeliveryDateCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure AOrdDefNote12DblClick(Sender: TObject);
    procedure AOrdDefNote7BtnDnClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure OrderNoMChange(Sender: TObject);
    procedure Tv4DblClick(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure V2Column1PropertiesEditValueChanged(Sender: TObject);
    procedure cxGridDBColumn1PropertiesEditValueChanged(Sender: TObject);
    procedure V2Column4PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure V2Column5PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure V2Column6PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure V2Column8PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton4Click(Sender: TObject);
    procedure BtnEditA1BtnClick(Sender: TObject);
    procedure BtnEditA2BtnClick(Sender: TObject);
    procedure BtnEditA3BtnClick(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure BtnEditA4BtnClick(Sender: TObject);
    procedure V2Column11PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton9Click(Sender: TObject);
    procedure ToolButton10Click(Sender: TObject);
    procedure ToolButton11Click(Sender: TObject);
  private
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure InitGridFH();
    function  SaveData():Boolean;
    procedure UpdateHC(FFirstNo:String);
    function  YFData():Boolean;
    function  DELYFData():Boolean;
    { Private declarations }
  public
    FFInt:Integer;
    { Public declarations }
  end;

var
  frmProductOrderAnPaiGQX: TfrmProductOrderAnPaiGQX;

implementation
uses
  U_DataLink,U_OrderInPut,U_Fun, U_ZDYHelp,U_iniParam, U_ZDYHelpSel,U_BefChkHX;

{$R *.dfm}

procedure TfrmProductOrderAnPaiGQX.FormDestroy(Sender: TObject);
begin
  frmProductOrderAnPaiGQX:=nil;
end;

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

procedure TfrmProductOrderAnPaiGQX.FormCreate(Sender: TObject);
begin
  
  //BegDate.DateTime:=SGetServerDateTime(ADOQueryTemp)-7;
  //EndDate.DateTime:=SGetServerDateTime(ADOQueryTemp);
  DQdate:=SGetServerDate(ADOQueryTemp);
end;

procedure TfrmProductOrderAnPaiGQX.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('指示单信息',Tv1,'回仓管理');
  WriteCxGrid('回仓信息',Tv2,'回仓管理');
end;

procedure TfrmProductOrderAnPaiGQX.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Sub_AnPai where LLId='''+Trim(Order_Main.fieldbyname('LLId').AsString)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Sub);
    SInitCDSData20(ADOQueryMain,CDS_Sub);
  finally
    ADOQueryMain.EnableControls;
  end;
end;
procedure TfrmProductOrderAnPaiGQX.InitGridFH();
begin
end;

procedure TfrmProductOrderAnPaiGQX.InitForm();
var
  i:Integer;
begin
  DateTimePicker1.DateTime:=SGetServerDateTime(ADOQueryTemp);
  ReadCxGrid('指示单信息',Tv1,'回仓管理');
  ReadCxGrid('回仓信息',Tv2,'回仓管理');
end;

function TfrmProductOrderAnPaiGQX.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_Sub where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Sub where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      Open;
      if IsEmpty then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete JYOrder_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
          ExecSQL;
        end;
      end;
    end;
    if Trim(Order_Main.fieldbyname('SubId').AsString)='' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete JYOrder_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
        ExecSQL;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmProductOrderAnPaiGQX.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv1,ADOQueryMain,'采购单列表');
end;

procedure TfrmProductOrderAnPaiGQX.TBPrintClick(Sender: TObject);
var
   fPrintFile:string;
   Porderno:string;
begin
  if Order_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\采购单.rmf' ;
  SDofilter(ADOQueryMain,' OrderNoM='''+Trim(Order_Main.fieldbyname('OrderNoM').AsString)+'''');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Porderno:=Trim(Order_Main.fieldbyname('OrderNoM').AsString);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\生产指示单10.rmf'),'提示',0);
  end;
  SDofilter(ADOQueryMain,'');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Order_Main.Locate('ordernoM',Porderno,[]);
  //SelPrintData(TV4,ADOQueryMain,'合同查询报表');
end;

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

procedure TfrmProductOrderAnPaiGQX.TBTPClick(Sender: TObject);
 var
   FQty,FQty1,FMxQty,FPQty,FMxQtyS,FPQtyS:String;
begin
end;

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

procedure TfrmProductOrderAnPaiGQX.Tv1StylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  id,id10:Integer;
begin
  {try
    if Tv1.GroupedItemCount=0 then
    begin
      Id:=Tv1.GetColumnByFieldName('DeliveryDate').Index-tv1.GroupedItemCount;
      Id10:=Tv1.GetColumnByFieldName('SubStatus').Index-tv1.GroupedItemCount;
      if Trim(VarToStr(ARecord.Values[id]))='' then Exit;
      if Id<0 then Exit;
      if ARecord.Values[id10]='完成' then exit;
      if (ARecord.Values[id]-DQdate)>=4 then Exit;
      if ((ARecord.Values[id]-DQdate)>=0) and ((ARecord.Values[id]-DQdate)<4) then
        AStyle:=DataLink_.QHuangSe
      else
      if ARecord.Values[id]-DQdate<0 then
      begin
        AStyle:=DataLink_OrderManage.FenHongS;
      end;
    end else
    begin

    end;
  except
  end; }
end;

procedure TfrmProductOrderAnPaiGQX.v1DeliveryDateCustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
 { Id:=TV1.GetColumnByFieldName('DeliveryDate').Index;//;-TV1.GroupedItemCount;
  Id10:=TV1.GetColumnByFieldName('SubStatus').Index;
  if Id<0 then Exit;
  if AViewInfo.GridRecord.Values[Id10]='完成' then Exit;
  if AViewInfo.GridRecord.Values[Id]-SGetServerDate(ADOQueryTemp)>=4 then Exit;
  if ((AViewInfo.GridRecord.Values[id]-SGetServerDate10(ADOQueryTemp))>=0) and ((AViewInfo.GridRecord.Values[id]-SGetServerDate(ADOQueryTemp))<4) then
    ACanvas.Brush.Color:=clYellow
  else
  if (AViewInfo.GridRecord.Values[id])-(SGetServerDate10(ADOQueryTemp)<0) then
  begin
    ACanvas.Brush.Color:=clRed;
  end;
  begin
    ACanvas.Brush.Color:=clRed;
  end else
  if AViewInfo.GridRecord.Values[Id]='Purple' then
  begin
    ACanvas.Brush.Color:=clPurple;
  end else
  if AViewInfo.GridRecord.Values[Id]='Olive' then
  begin
    ACanvas.Brush.Color:=clOlive;
  end else
  if AViewInfo.GridRecord.Values[Id]='Teal' then
  begin
    ACanvas.Brush.Color:=clTeal;
  end else
  if AViewInfo.GridRecord.Values[Id]='Background' then
  begin
    ACanvas.Brush.Color:=clBackground;
  end;   }
end;

procedure TfrmProductOrderAnPaiGQX.N1Click(Sender: TObject);
var
   fPrintFile:string;
   Porderno:string;
begin
  if Order_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\生产指示单10.rmf' ;
  SDofilter(ADOQueryMain,' OrderNoM='''+Trim(Order_Main.fieldbyname('OrderNoM').AsString)+'''');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Porderno:=Trim(Order_Main.fieldbyname('OrderNoM').AsString);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\生产指示单10.rmf'),'提示',0);
  end;
  SDofilter(ADOQueryMain,'');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Order_Main.Locate('ordernoM',Porderno,[]);
end;

procedure TfrmProductOrderAnPaiGQX.N2Click(Sender: TObject);
var
   fPrintFile:string;
   Porderno:string;
begin
  if Order_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\生产指示单.rmf' ;
  SDofilter(ADOQueryMain,' OrderNoM='''+Trim(Order_Main.fieldbyname('OrderNoM').AsString)+'''');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Porderno:=Trim(Order_Main.fieldbyname('OrderNoM').AsString);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\生产指示单.rmf'),'提示',0);
  end;
  SDofilter(ADOQueryMain,'');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Order_Main.Locate('ordernoM',Porderno,[]);
end;

procedure TfrmProductOrderAnPaiGQX.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmOrderInPut:=TfrmOrderInPut.Create(Application);
    with frmOrderInPut do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      ToolBar2.Visible:=False;
      TBSave.Visible:=False;
      Tv1.OptionsSelection.CellSelect:=False;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmOrderInPut.Free;
  end;
end;

procedure TfrmProductOrderAnPaiGQX.ToolButton2Click(Sender: TObject);
begin
  with CDS_Sub do
  begin
    Append;
    FieldByName('SubId').Value:=Trim(Order_Main.fieldbyname('SubId').AsString);
    FieldByName('ADefDate1').Value:=DateTimePicker1.Date;
    FieldByName('AOrddefstr4').Value:=Trim(BtnEditA1.Text);
    FieldByName('AOrddefstr5').Value:=Trim(BtnEditA1.TxtCode);
    FieldByName('AOrddefstr2').Value:=Trim(ComboBox1.Text);
    FieldByName('AOrddefstr3').Value:=Trim(BtnEditA2.Text);
    FieldByName('AOrdDefNote1').Value:=Trim(BtnEditA3.Text);
    FieldByName('AOrddefstr6').Value:=Trim(BtnEditA4.Text);
    Post;
  end;
  
end;

procedure TfrmProductOrderAnPaiGQX.ToolButton3Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then Exit;
  if Trim(CDS_Sub.fieldbyname('APID').AsString)<>'' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select Top 1* from WFB_MJJY where APID='''+Trim(CDS_Sub.fieldbyname('APID').AsString)+'''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty=False then
    begin
      Application.MessageBox('已产生检验数据不能删除!','提示',0);
      Exit;
    end;
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete JYOrder_Sub_AnPai where APID='''+Trim(CDS_Sub.fieldbyname('APID').AsString)+'''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPdate Contract_Cloth_LL Set HCPS=(select isnull(sum(AOrdQty2),0) from JYOrder_Sub_AnPai A where A.LLID=Contract_Cloth_LL.LLID )');
        sql.Add(',HCMQty=(select isnull(sum(HCMQty),0) from JYOrder_Sub_AnPai A where A.LLID=Contract_Cloth_LL.LLID )');
        sql.Add(' where LLID='''+Trim(Order_Main.fieldbyname('LLID').AsString)+'''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update Contract_Cloth_LL Set HCQty=HCMQty*1.00/TPMQty*TPQty ');
        sql.Add(' where LLID='''+Trim(Order_Main.fieldbyname('LLID').AsString)+'''');
        ExecSQL;
      end;
      if DELYFData()=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('更新应付款失败!','提示',0);
      end;
      ADOQueryCmd.Connection.CommitTrans;
    except
      ADOQueryCmd.Connection.RollbackTrans;
    end;
  end;
  with CDS_Sub do
  begin
    Delete;
  end;
  if CDS_Sub.IsEmpty then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate JYOrder_Sub Set SOrdFlag20=0 where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end;
  end;
  {with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('UPdate Contract_Cloth_LL Set HCPS=0,HCQty=0,HCMQty=0 where OrdSubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
    ExecSQL;
  end;
  with ADOQueryHC do
  begin
    Close;
    sql.Clear;
    sql.Add('select distinct(AOrddefstr5) AOrddefstr5 from JYOrder_Sub_AnPai where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
    Open;
  end;
  if ADOQueryHC.IsEmpty then
    UpdateHC(Trim(ADOQueryHC.fieldbyname('AOrddefstr5').AsString))
  else
  begin
    with ADOQueryHC do
    begin
      First;
      while not Eof do
      begin
        UpdateHC(Trim(ADOQueryHC.fieldbyname('AOrddefstr5').AsString));
        Next;
      end;
    end;
  end; }

end;

function TfrmProductOrderAnPaiGQX.SaveData():Boolean;
var
  maxno:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    ///保存子表
    with CDS_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_Sub.fieldbyname('APId').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'','JYOrder_Sub_AnPai',3,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(CDS_Sub.fieldbyname('APId').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from JYOrder_Sub_AnPai where APId='''+Trim(CDS_Sub.fieldbyname('APId').AsString)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(CDS_Sub.fieldbyname('APId').AsString)='' then
            Append
          else
            Edit;
          FieldByName('MainId').Value:=Trim(Order_Main.fieldbyname('MainId').AsString);
          FieldByName('SubId').Value:=Trim(Order_Main.fieldbyname('SubId').AsString);
          FieldByName('ApId').Value:=Trim(maxno);
          FieldByName('ADefDate1').Value:=CDS_Sub.fieldbyname('ADefDate1').Value;
          FieldByName('AOrddefstr4').Value:=Trim(Order_Main.fieldbyname('FirstName').AsString);
          FieldByName('AOrddefstr5').Value:=Trim(Order_Main.fieldbyname('FirstNo').AsString);
          FieldByName('GangNo').Value:=CDS_Sub.fieldbyname('GangNo').Value;
          FieldByName('AOrddefstr1').Value:=CDS_Sub.fieldbyname('AOrddefstr1').Value;
          FieldByName('AOrddefstr2').Value:=CDS_Sub.fieldbyname('AOrddefstr2').Value;
          if Trim(CDS_Sub.fieldbyname('ZSXS').AsString)<>'' then
            FieldByName('ZSXS').Value:=CDS_Sub.fieldbyname('ZSXS').Value
          else
            FieldByName('ZSXS').Value:=1;
          FieldByName('AOrdQty2').Value:=CDS_Sub.fieldbyname('AOrdQty2').Value;
          FieldByName('AOrdQty1').Value:=CDS_Sub.fieldbyname('AOrdQty1').Value;
          FieldByName('AOrddefstr3').Value:=CDS_Sub.fieldbyname('AOrddefstr3').Value;
          FieldByName('AOrdDefNote1').Value:=CDS_Sub.fieldbyname('AOrdDefNote1').Value;
          FieldByName('LLID').Value:=Order_Main.fieldbyname('LLID').Value;
          FieldByName('AOrddefstr6').Value:=CDS_Sub.fieldbyname('AOrddefstr6').Value;
          if CDS_Sub.fieldbyname('AOrdFlag1').AsBoolean=True then
            FieldByName('AOrdFlag1').Value:=1
          else
            FieldByName('AOrdFlag1').Value:=0;

          //RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_Sub,'JYOrder_Sub_AnPai',0);
          {if Trim(CDS_Sub.fieldbyname('APID').AsString)<>'' then
          begin
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          end else
          begin

          end; }
          if Trim(CDS_Sub.fieldbyname('APId').AsString)='' then
            FieldByName('Filler').Value:=Trim(DName)
          else
          begin
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          end;
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select Top 1* from Contract_Cloth_LL where LLId='''+Trim(Order_Main.fieldbyname('LLId').AsString)+'''');
            Open;
          end;
          if Trim(ADOQueryTemp.fieldbyname('TPUnit').AsString)=Trim(CDS_Sub.fieldbyname('AOrddefstr2').AsString) then
          begin
            FieldByName('HCYZQty').Value:=CDS_Sub.fieldbyname('AOrdQty1').Value;
          end else
          begin
            if Trim(CDS_Sub.fieldbyname('AOrddefstr2').AsString)='M' then
            begin
              FieldByName('HCYZQty').Value:=(CDS_Sub.fieldbyname('AOrdQty1').Value*1.00/ADOQueryTemp.fieldbyname('TPMQty').Value)
                                            *ADOQueryTemp.fieldbyname('TPQty').Value ;
            end;
          end;
          if Trim(CDS_Sub.fieldbyname('AOrddefstr2').AsString)='M' then
          begin
            FieldByName('HCMQty').Value:=CDS_Sub.fieldbyname('AOrdQty1').Value;
          end else
          begin
            FieldByName('HCMQty').Value:=CDS_Sub.fieldbyname('AOrdQty1').Value*CDS_Sub.fieldbyname('ZSXS').Value;
          end;
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate JYOrder_Sub Set SOrdFlag20=1 where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
          ExecSQL;
        end;
        CDS_Sub.Edit;
        CDS_Sub.FieldByName('APId').Value:=Trim(maxno);
        CDS_Sub.FieldByName('SubId').Value:=Trim(Order_Main.fieldbyname('SubId').AsString);

        //Order_Sub.Post;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' UPdate WFB_MJJY Set MJTypeOther='''+Trim(CDS_Sub.fieldbyname('AOrddefstr3').AsString)+'''');
          SQL.Add(' where APId='''+Trim(maxno)+'''');
          sql.Add(' UPdate CK_BanCP_KC Set KCQtyUnit='''+Trim(CDS_Sub.fieldbyname('AOrddefstr3').AsString)+'''');
          SQL.Add(' where MJId in (select MJID from WFB_MJJY where APID='''+Trim(maxno)+''')');
          sql.Add(' UPdate CK_BanCP_CR Set QtyUnit='''+Trim(CDS_Sub.fieldbyname('AOrddefstr3').AsString)+'''');
          SQL.Add(' where MJId in (select MJID from WFB_MJJY where APID='''+Trim(maxno)+''')');
          ExecSQL;
        end;

        if YFData()=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('更新财务应付款失败!','提示',0);
          Exit;
        end;
        Next;
      end;
    end;
   { with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('UPdate Contract_Cloth_LL Set HCPS=0,HCQty=0,HCMQty=0 where OrdSubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryHC do
    begin
      Close;
      sql.Clear;
      sql.Add('select distinct(AOrddefstr5) AOrddefstr5 from JYOrder_Sub_AnPai where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      Open;
    end;
    with ADOQueryHC do
    begin
      First;
      while not Eof do
      begin
        UpdateHC(Trim(ADOQueryHC.fieldbyname('AOrddefstr5').AsString));
        Next;
      end;
    end;  }
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate Contract_Cloth_LL Set HCPS=(select isnull(sum(AOrdQty2),0) from JYOrder_Sub_AnPai A where A.LLID=Contract_Cloth_LL.LLID )');
      sql.Add(',HCMQty=(select isnull(sum(HCMQty),0) from JYOrder_Sub_AnPai A where A.LLID=Contract_Cloth_LL.LLID )');
      sql.Add(' where LLID='''+Trim(Order_Main.fieldbyname('LLID').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      if (Trim(CDS_Sub.fieldbyname('AOrddefstr2').AsString)=Trim(Order_Main.fieldbyname('TPUnit').AsString)) then
        sql.Add('Update Contract_Cloth_LL Set HCQty=(select isnull(Sum(AOrdQty1),0) from JYOrder_Sub_AnPai A where A.LLID=Contract_Cloth_LL.LLID) ')
      else
        sql.Add('Update Contract_Cloth_LL Set HCQty=HCMQty*1.00/TPMQty*TPQty ');
      sql.Add(' where LLID='''+Trim(Order_Main.fieldbyname('LLID').AsString)+'''');
      ExecSQL;
    end;
    {if YFData()=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('更新财务应付款失败!','提示',0);
      Exit;
    end;  }
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    MovePanel2.Visible:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常！','提示',0);
  end;
end;

procedure TfrmProductOrderAnPaiGQX.AOrdDefNote12DblClick(Sender: TObject);
var
  fsj:string;
  FWZ:Integer;
begin
  fsj:=Trim(TEdit(Sender).Hint);
  FWZ:=Pos('/',fsj);
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Copy(fsj,1,FWZ-1);
      flagname:=Copy(fsj,FWZ+1,Length(fsj)-fwz);
      if ShowModal=1 then
      begin
        TEdit(Sender).Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmProductOrderAnPaiGQX.AOrdDefNote7BtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text:='';
  TBtnEditC(Sender).TxtCode:='';
end;

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

procedure TfrmProductOrderAnPaiGQX.ToolButton5Click(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='WFBCD';
      flagname:='疵点';
      fnote:=True;
      V1Note.Caption:='英文名称';
      if ShowModal=1 then
      begin
        
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmProductOrderAnPaiGQX.ToolButton6Click(Sender: TObject);
var
   fPrintFile:string;
begin
  if CDS_Sub.IsEmpty then Exit;
  if CDS_Sub.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('未选择要打印的数据!','提示',0);
    Exit;
  end;
  CDS_Sub.DisableControls;
  with CDS_Sub do
  begin
    First;
    while not Eof do
    begin
      if CDS_Sub.FieldByName('SSel').AsBoolean=True then
      begin
        if  Trim(CDS_Sub.fieldbyname('APID').AsString) ='' then
        begin
          CDS_Sub.EnableControls;
          Application.MessageBox('数据未保存不能打印!','提示',0);
          Exit;
        end;

      end;
      Next;
    end;
  end;
  CDS_Sub.EnableControls;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\检验指示单标签.rmf' ;
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    //RM1.LoadFromFile(fPrintFile);
    //RM1.ShowReport;
    //RM1.PrintReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\检验指示单标签.rmf'),'提示',0);
    Exit;
  end;
  CDS_Sub.DisableControls;
  with CDS_Sub do
  begin
    First;
    while not Eof do
    begin
      if CDS_Sub.FieldByName('SSel').AsBoolean=True then
      begin
        with ADOQueryPrint do
        begin
          Close;
          sql.Clear;
          sql.Add('select A.*,OrderNoM=B.OrderNo ,B.MPRTCodeName,C.PRTColor from JYOrder_Sub_AnPai A ');
          sql.Add('inner join JYOrder_Main B on A.MainId=B.MainId');
          sql.Add('inner join JYOrder_Sub C on A.SubId=C.SubId where A.APID='''+Trim(CDS_Sub.fieldbyname('APID').AsString)+'''');
          Open;
        end;
        RM1.LoadFromFile(fPrintFile);
        RM1.PrintReport;
      end;
      Next;
    end;
  end;
  CDS_Sub.EnableControls;



end;

procedure TfrmProductOrderAnPaiGQX.OrderNoMChange(Sender: TObject);
var
  mvalue:String;
begin
  mvalue:=Trim(OrderNoM.Text);
  if Length(Trim(mvalue))<4 then
  begin
    cxGrid4.Visible:=False;
    Exit;
  end;
  mvalue:='%'+Trim(mvalue)+'%';
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select OrderNo,MPRTCodeName,A.MainId from JYOrder_Main A');
    sql.Add(' where A.orderno like :orderno ');
    sql.Add(' and A.MainId in(select MainId from JYOrder_Sub B where B.Mainid=A.MainId and B.subId in(select OrdSubId from Contract_Cloth_LL))');
    Parameters.ParamByName('orderno').Value:=mvalue;
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_OrderNo);
  SInitCDSData20(ADOQueryTemp,CDS_OrderNo);
  if CDS_OrderNo.IsEmpty then cxGrid4.Visible:=False else cxGrid4.Visible:=True;
end;

procedure TfrmProductOrderAnPaiGQX.Tv4DblClick(Sender: TObject);
begin
  cxGrid4.Visible:=False;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.*,C.FirstName,C.FirstNo,C.TPPS,C.TPMQty,C.LLID,C.HXPS,C.HXQty,C.HXUnit,C.DHIDHelp,C.DHID,C.TPUnit ');
    sql.Add(',PBFactory=(select Top 1 AA.factoryNoName from Contract_Main AA inner join Contract_Cloth_DH BB on AA.MainId=BB.MainId');
    sql.Add(' where BB.DHID=C.DHID)');
    sql.Add(',HCUnit=(select Top 1 AOrddefstr2  from JYOrder_Sub_AnPai AA where AA.SubId=B.SubId)');
    sql.Add('from JYOrder_Main A inner join JYOrder_Sub B on A.MainId=B.MainId ');
    sql.Add(' inner join Contract_Cloth_LL C on C.OrdSubId=B.SubId');
    sql.Add('where A.MainId='''+Trim(CDS_OrderNo.fieldbyname('MainId').AsString)+'''');
    sql.Add(' and C.JXJGFlag=0');
    sql.Add(' and exists(select * from Contract_Cloth_LLMX LM where LM.OrdSubId=B.SubId and LM.OrdSubId=C.OrdSubId )');
    //ShowMessage(SQL.Text);
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,Order_Main);
  SInitCDSData20(ADOQueryTemp,Order_Main);
end;

procedure TfrmProductOrderAnPaiGQX.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  InitGrid();
  BtnEditA1.TxtCode:=Trim(Order_Main.fieldbyname('FirstNo').AsString);
  BtnEditA1.Text:=Trim(Order_Main.fieldbyname('FirstName').AsString);
  BtnEditA4.Text:=Trim(Order_Main.fieldbyname('PBFactory').AsString);
end;

procedure TfrmProductOrderAnPaiGQX.V2Column1PropertiesEditValueChanged(
  Sender: TObject);
//var
  //mvalue,FFieldName,mvalue10,mvalue20:String;
  //FHCPS,FHCQty,FHCMQty:Double;
begin
  {if Trim(CDS_Sub.fieldbyname('AOrddefstr4').AsString)='' then
  begin
    Application.MessageBox('染厂不能为空!','提示',0);
    Exit;
  end;
  with Self.ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Contract_Cloth_LL A ');
    sql.Add(' where A.FirstNo='''+Trim(CDS_Sub.fieldbyname('AOrddefstr5').AsString)+'''');
    sql.Add(' and A.OrdSubId='''+Trim(CDS_Sub.fieldbyname('SubId').AsString)+'''');
    Open;
  end;
  if Self.ADOQueryTemp.IsEmpty then
  begin
    Application.MessageBox('此染厂没有相对应的数据!','提示',0);
    Exit;
  end;
  mvalue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if  FFieldName='AOrdQty1' then
  begin
    if Trim(CDS_Sub.fieldbyname('AOrddefstr2').AsString)='' then
    begin
      Application.MessageBox('数量单位不能为空!','提示',0);
      Exit;
    end;
  end;
  if Trim(mvalue)='' then
  begin
    if FFieldName='ZSXS' then
      mvalue:='1'
    else
    if FFieldName='AOrddefstr2' then
      mvalue:=''
    else
      mvalue:='0';
  end;

  with CDS_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate JYOrder_Sub_AnPai ');
    if Trim(FFieldName)='AOrddefstr2' then
    begin
      sql.Add(' Set '+FFieldName+'='''+Trim(mvalue)+'''');
    end else
    begin
      sql.Add(' Set '+FFieldName+'='+Trim(mvalue));
    end;
    SQL.Add(',Editer='''+Trim(DName)+'''');
    SQL.Add(',Edittime=getdate() ');
    SQL.Add(' where APId='''+Trim(CDS_Sub.fieldbyname('APId').AsString)+'''');
    ExecSQL;
  end;
  if Trim(FFieldName)='AOrdQty2' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add('select isnull(Sum(AOrdQty2),0) HCPS,isnull(Sum(AOrdQty1),0) HCQty from JYOrder_Sub_AnPai  ');
      sql.Add(' where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      sql.Add(' and AOrddefstr5='''+Trim(CDS_Sub.fieldbyname('AOrddefstr5').AsString)+'''');
      Open;
    end;
    FHCPS:=ADOQueryTemp.FieldByName('HCPS').Value;
    if FHCPS>0 then
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from Contract_Cloth_LL where OrdSubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
        sql.Add(' and FirstNo='''+Trim(CDS_Sub.fieldbyname('AOrddefstr5').AsString)+'''');
        SQL.Add(' order by LLIdx');
        Open;
      end;
      with ADOQueryTemp do
      begin
        First;
        while (not eof) and (FHCPS>0) do
        begin
          if FHCPS>=FieldByName('TPPS').Value then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('Update Contract_Cloth_LL Set HCPS=TPPS ');
              sql.Add(' where LLID='''+Trim(ADOQueryTemp.fieldbyname('LLID').AsString)+'''');
              ExecSQL;
            end;
            FHCPS:=FHCPS-FieldByName('TPPS').Value;
          end else
          begin

            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('Update Contract_Cloth_LL Set HCPS='+FloatToStr(FHCPS));
              sql.Add(' where LLID='''+Trim(ADOQueryTemp.fieldbyname('LLID').AsString)+'''');
              ExecSQL;
            end;
            FHCPS:=0;
          end;
          Next;
        end;
      end;
    end;
  end;
  if Trim(FFieldName)='AOrdQty1' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add('select isnull(Sum(AOrdQty2),0) HCPS,isnull(Sum(AOrdQty1),0) HCQty from JYOrder_Sub_AnPai  ');
      sql.Add(' where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      sql.Add(' and AOrddefstr5='''+Trim(CDS_Sub.fieldbyname('AOrddefstr5').AsString)+'''');
      Open;
    end;
    FHCQty:=ADOQueryTemp.FieldByName('HCQty').Value;
    if FHCQty>0 then
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from Contract_Cloth_LL where OrdSubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
        sql.Add(' and FirstNo='''+Trim(CDS_Sub.fieldbyname('AOrddefstr5').AsString)+'''');
        SQL.Add(' order by LLIdx');
        Open;
      end;
      with ADOQueryTemp do
      begin
        First;
        while (not eof) and (FHCQty>0) do
        begin
          if FHCQty>=FieldByName('BCPQty').Value then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('Update Contract_Cloth_LL Set HCQty=BCPQty ');
              sql.Add(' where LLID='''+Trim(ADOQueryTemp.fieldbyname('LLID').AsString)+'''');
              ExecSQL;
            end;
            FHCQty:=FHCQty-FieldByName('BCPQty').Value;
          end else
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('Update Contract_Cloth_LL Set HCQty='+FloatToStr(FHCQty));
              sql.Add(' where LLID='''+Trim(ADOQueryTemp.fieldbyname('LLID').AsString)+'''');
              ExecSQL;
            end;
            FHCQty:=0;
          end;
          Next;
        end;
      end;
    end;
  end;}
end;
procedure TfrmProductOrderAnPaiGQX.UpdateHC(FFirstNo:String);
var
  FHCPS,FHCYZQty,FHCMQty:Double;
begin
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add('select isnull(Sum(AOrdQty2),0) HCPS from JYOrder_Sub_AnPai  ');
      sql.Add(' where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      sql.Add(' and AOrddefstr5='''+Trim(FFirstNo)+'''');
      Open;
    end;
    FHCPS:=ADOQueryTemp.FieldByName('HCPS').Value;
    if FHCPS=0 then
    begin
    
    end;
    if FHCPS>0 then
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from Contract_Cloth_LL where OrdSubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
        sql.Add(' and FirstNo='''+Trim(FFirstNo)+'''');
        SQL.Add(' order by LLIdx');
        Open;
      end;
      with ADOQueryTemp do
      begin
        First;
        while (not eof) and (FHCPS>0) do
        begin
          if FHCPS>=FieldByName('TPPS').Value then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('Update Contract_Cloth_LL Set HCPS=TPPS ');
              sql.Add(' where LLID='''+Trim(ADOQueryTemp.fieldbyname('LLID').AsString)+'''');
              ExecSQL;
            end;
            FHCPS:=FHCPS-FieldByName('TPPS').Value;
          end else
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('Update Contract_Cloth_LL Set HCPS='+FloatToStr(FHCPS));
              sql.Add(' where LLID='''+Trim(ADOQueryTemp.fieldbyname('LLID').AsString)+'''');
              ExecSQL;
            end;
            FHCPS:=0;
          end;
          Next;
        end;
      end;
    end;
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add('select isnull(Sum(HCYZQty),0) HCYZQty,isnull(Sum(HCMQty),0) HCMQty from JYOrder_Sub_AnPai  ');
      sql.Add(' where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      sql.Add(' and AOrddefstr5='''+Trim(FFirstNo)+'''');
      Open;
    end;
    FHCYZQty:=ADOQueryTemp.FieldByName('HCYZQty').Value;
    FHCMQty:=ADOQueryTemp.FieldByName('HCMQty').Value;
    if FHCYZQty>=0 then
    begin
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from Contract_Cloth_LL where OrdSubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
        sql.Add(' and FirstNo='''+Trim(FFirstNo)+'''');
        SQL.Add(' order by LLIdx');
        Open;
      end;
      with ADOQueryTemp do
      begin
        First;
        while (not eof) and (FHCYZQty>0) do
        begin
          if FHCYZQty>=FieldByName('BCPQty').Value then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('Update Contract_Cloth_LL Set HCQty=BCPQty ');
              sql.Add(' where LLID='''+Trim(ADOQueryTemp.fieldbyname('LLID').AsString)+'''');
              ExecSQL;
            end;
            FHCYZQty:=FHCYZQty-FieldByName('BCPQty').Value;
          end else
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('Update Contract_Cloth_LL Set HCQty='+FloatToStr(FHCYZQty));
              sql.Add(' where LLID='''+Trim(ADOQueryTemp.fieldbyname('LLID').AsString)+'''');
              ExecSQL;
            end;
            FHCYZQty:=0;
          end;
          Next;
        end;
      end;
      with ADOQueryTemp do
      begin
        First;
        while (not eof) and (FHCMQty>0) do
        begin
          if FHCMQty>=FieldByName('BCPMQty').Value then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('Update Contract_Cloth_LL Set HCMQty=BCPMQty ');
              sql.Add(' where LLID='''+Trim(ADOQueryTemp.fieldbyname('LLID').AsString)+'''');
              ExecSQL;
            end;
            FHCMQty:=FHCMQty-FieldByName('BCPMQty').Value;
          end else
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              SQL.Add('Update Contract_Cloth_LL Set HCMQty='+FloatToStr(FHCMQty));
              sql.Add(' where LLID='''+Trim(ADOQueryTemp.fieldbyname('LLID').AsString)+'''');
              ExecSQL;
            end;
            FHCMQty:=0;
          end;
          Next;
        end;
      end;
    end;
end;

procedure TfrmProductOrderAnPaiGQX.cxGridDBColumn1PropertiesEditValueChanged(
  Sender: TObject);
//var
  //mvalue,FFieldName:String;
begin
  {mvalue:=TcxTextEdit(Sender).EditingText;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_Sub do
  begin
    Edit;
    FieldByName(FFieldName).Value:=Trim(mvalue);
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('UPdate JYOrder_Sub_AnPai ');
    if Trim(mvalue)<>'' then
    begin
      sql.Add(' Set '+FFieldName+'='''+Trim(mvalue)+'''');
    end else
    begin
      sql.Add(' Set '+FFieldName+'=NULL');
    end;
    SQL.Add(',Editer='''+Trim(DName)+'''');
    SQL.Add(',Edittime=getdate() ');
    SQL.Add(' where APId='''+Trim(CDS_Sub.fieldbyname('APId').AsString)+'''');
    ExecSQL;
  end;  }
end;


procedure TfrmProductOrderAnPaiGQX.V2Column4PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrderUnit';
      flagname:='数量单位';
      if ShowModal=1 then
      begin
        Self.CDS_Sub.Edit;
        Self.CDS_Sub.FieldByName('AOrddefstr2').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate JYOrder_Sub_AnPai ');
          sql.Add(' Set AOrddefstr2='''+Trim(ClientDataSet1.fieldbyname('ZDYName').AsString)+'''');
          SQL.Add(',Editer='''+Trim(DName)+'''');
          SQL.Add(',Edittime=getdate() ');
          SQL.Add(' where APId='''+Trim(CDS_Sub.fieldbyname('APId').AsString)+'''');
          sql.Add(' UPdate WFB_MJJY Set MJStr1='''+Trim(ClientDataSet1.fieldbyname('ZDYName').AsString)+'''');
          SQL.Add(' where APId='''+Trim(CDS_Sub.fieldbyname('APId').AsString)+'''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmProductOrderAnPaiGQX.V2Column5PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrderUnit';
      flagname:='数量单位';
      if ShowModal=1 then
      begin
        Self.CDS_Sub.Edit;
        Self.CDS_Sub.FieldByName('AOrddefstr3').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmProductOrderAnPaiGQX.V2Column6PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
var
  mvalue:string;
begin
  mvalue:=Trim(CDS_Sub.FieldByName('AOrdDefNote1').AsString);
  try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:='JYYQ';
      flagname:='检验要求';
      if ShowModal=1 then
      begin
        Self.CDS_Sub.Edit;
        Self.CDS_Sub.FieldByName('AOrdDefNote1').Value:=mvalue+frmZDYHelpSel.ReturnStr;
        {with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate JYOrder_Sub_AnPai ');
          sql.Add(' Set AOrdDefNote1='''+Trim(mvalue+frmZDYHelpSel.ReturnStr)+'''');
          SQL.Add(',Editer='''+Trim(DName)+'''');
          SQL.Add(',Edittime=getdate() ');
          SQL.Add(' where APId='''+Trim(CDS_Sub.fieldbyname('APId').AsString)+'''');
          ExecSQL;
        end; }
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmProductOrderAnPaiGQX.V2Column8PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      //flag:='RKPlace';
      flag:='FactoryNo1Name';
      flagname:='染厂';
      MainType:='染厂';
      if ShowModal=1 then
      begin

        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Contract_Cloth_LL A ');
          sql.Add(' where A.FirstNo='''+Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYNo').AsString)+'''');
          sql.Add(' and A.OrdSubId='''+Trim(CDS_Sub.fieldbyname('SubId').AsString)+'''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty then
        begin
          Application.MessageBox('此染厂没有相对应的数据!','提示',0);

        end else
        begin
          with CDS_Sub do
          begin
            Edit;
            FieldByName('AOrddefstr4').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString);
            FieldByName('AOrddefstr5').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYNo').AsString);
          end;
          {with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('UPdate JYOrder_Sub_AnPai ');
            sql.Add(' Set AOrddefstr5='''+Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString)+'''');
            sql.Add(' ,AOrddefstr4='''+Trim(ClientDataSet1.fieldbyname('ZDYName').AsString)+'''');
            SQL.Add(',Editer='''+Trim(DName)+'''');
            SQL.Add(',Edittime=getdate() ');
            SQL.Add(' where APId='''+Trim(CDS_Sub.fieldbyname('APId').AsString)+'''');
            ExecSQL;
          end;}
        end;

      end;
    end;
  finally
    //frmZDYHelp.Free;
  end;
end;

procedure TfrmProductOrderAnPaiGQX.ToolButton4Click(Sender: TObject);
var
  FDW:String;
begin
  if CDS_Sub.Locate('ADefDate1',null,[])=True then
  begin
    Application.MessageBox('回仓时间不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('AOrddefstr4',null,[])=True then
  begin
    Application.MessageBox('染厂不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('AOrddefstr4','',[])=True then
  begin
    Application.MessageBox('染厂不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('GangNo',null,[])=True then
  begin
    Application.MessageBox('本厂缸号不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('AOrddefstr2',null,[])=True then
  begin
    Application.MessageBox('数量单位不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('AOrddefstr2','',[])=True then
  begin
    Application.MessageBox('数量单位不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('AOrddefstr2','Kg',[])=True then
  begin
    if Trim(CDS_Sub.FieldByName('ZSXS').AsString)='' then
    begin
      Application.MessageBox('折算系数不能为空!','提示',0);
      Exit;
    end;
  end;
  if CDS_Sub.Locate('AOrdQty2',null,[])=True then
  begin
    Application.MessageBox('匹数量不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('AOrdQty1',null,[])=True then
  begin
    Application.MessageBox('数量不能为空!','提示',0);
    Exit;
  end;
 { if CDS_Sub.Locate('AOrdQty1','',[])=True then
  begin
    Application.MessageBox('数量不能为空!','提示',0);
    Exit;
  end;}
  FDW:=Trim(CDS_Sub.fieldbyname('AOrddefstr2').AsString);
  if CDS_Sub.Locate('AOrddefstr2',FDW,[])=False then
  begin
    Application.MessageBox('数量单位不一致!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('AOrddefstr3',null,[]) then
  begin
    Application.MessageBox('检验数量单位不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('AOrddefstr3','',[]) then
  begin
    Application.MessageBox('检验数量单位不能为空!','提示',0);
    Exit;
  end;
  FDW:=Trim(CDS_Sub.fieldbyname('AOrddefstr3').AsString);
  if CDS_Sub.Locate('AOrddefstr3',FDW,[])=False then
  begin
    Application.MessageBox('检验数量单位不一致!','提示',0);
    Exit;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select Top 1* from Contract_Cloth_LL where OrdSubId='''+Trim(Order_Main.fieldbyname('Subid').AsString)+'''');
    Open;
  end;
  if Trim(ADOQueryTemp.fieldbyname('TPUnit').AsString)='M' then
  begin
    if CDS_Sub.Locate('AOrddefstr2','Kg',[]) then
    begin
      Application.MessageBox('领料数量单位为M,回仓数量单位不能为Kg!','提示',0);
      Exit;
    end;
  end;
  OrderNoM.SetFocus;
  MovePanel2.Visible:=True;
  MovePanel2.Refresh;
  if  SaveData() then
  begin
    MovePanel2.Visible:=False;
    Application.MessageBox('保存成功!','提示',0);
    Exit;
  end;
  MovePanel2.Visible:=False;
end;

procedure TfrmProductOrderAnPaiGQX.BtnEditA1BtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='FactoryNo1Name';
      flagname:='染厂';
      MainType:='染厂';
      if ShowModal=1 then
      begin

        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Contract_Cloth_LL A ');
          sql.Add(' where A.FirstNo='''+Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYNo').AsString)+'''');
          sql.Add(' and A.OrdSubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty then
        begin
          Application.MessageBox('此染厂没有相对应的数据!','提示',0);

        end else
        begin
          BtnEditA1.Text:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString);
          BtnEditA1.TxtCode:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYNo').AsString);
        end;

      end;
    end;
  finally
    //frmZDYHelp.Free;
  end;
end;
function TfrmProductOrderAnPaiGQX.YFData():Boolean;
var
  CRID,OrdMainId,YFID,Price,LLID:String;
begin
  Result:=False;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete YF_Money_CR where YFTypeID='''+Trim(CDS_Sub.fieldbyname('APID').AsString)+'''');
    ExecSQL;
  end;
  OrdMainId:=Trim(Order_Main.fieldbyname('MainId').AsString);
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryName='''+Trim(Order_Main.fieldbyname('FirstName').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False 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(Order_Main.fieldbyname('FirstName').AsString);
      FieldByName('ZdyStr1').Value:='应付付';
      Post;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    if Trim(Order_Main.fieldbyname('DHIDHelp').AsString)='' then
      sql.Add('select Top 1 * from Contract_Cloth_LLMX where DHID='''+Trim(Order_Main.fieldbyname('DHID').AsString)+'''')
    else
      sql.Add('select Top 1 * from Contract_Cloth_LLMX where DHID='''+Trim(Order_Main.fieldbyname('DHIDHelp').AsString)+'''') ;
    Open;
  end;
  Price:=ADOQueryTemp.fieldbyname('JGPrice').AsString;
  if Trim(Price)='' then Price:='0';
  {with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YF_Money_CR where FactoryName='''+Trim(Order_Main.fieldbyname('FirstName').AsString)+'''');
    sql.Add(' and YFTypeId='''+Trim(Order_Main.fieldbyname('LLid').AsString)+'''');
    if Trim(Order_Main.fieldbyname('DHIDHelp').AsString)='' then
      sql.Add(' and YFName=''染费'' ')
    else
      sql.Add(' and YFName=''加工费'' ');
    Open;
  end;}
  if CDS_Sub.FieldByName('AOrdFlag1').AsBoolean=False then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YF_Money_CR where FactoryName='''+Trim(Order_Main.fieldbyname('FirstName').AsString)+'''');
      sql.Add(' and YFTypeId='''+Trim(CDS_Sub.fieldbyname('APId').AsString)+'''');
      if Trim(Order_Main.fieldbyname('DHIDHelp').AsString)='' then
        sql.Add(' and YFName=''染费'' ')
      else
        sql.Add(' and YFName=''加工费'' ');
      Open;
    end;

    if ADOQueryTemp.IsEmpty then
    begin

      if GetLSNo(ADOQueryCmd,YFID,'RJ','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('APId').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(Order_Main.fieldbyname('FirstName').AsString);
        FieldByName('CRTime').Value:=CDS_Sub.fieldbyname('ADefDate1').Value;
        FieldByName('Qty').Value:=CDS_Sub.fieldbyname('AOrdQty1').Value;
        FieldByName('YFType').Value:='自动生成';
        FieldByName('Price').Value:=Price;
        FieldByName('HuiLv').Value:=1;
        FieldByName('BZType').Value:='人民币';
        FieldByName('ComTaiTou').Value:=Trim(Order_Main.fieldbyname('OrdDefStr2').AsString);
        FieldByName('QtyUnit').Value:=Trim(Order_Main.fieldbyname('TPUnit').AsString);
        if Trim(Order_Main.fieldbyname('DHIDHelp').AsString)='' then
          FieldByName('YFName').Value:='染费'
        else
          FieldByName('YFName').Value:='加工费';
        FieldByName('MainId').Value:=Trim(OrdMainId);
        Post;
      end;
    end else
    begin
      YFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPdate YF_Money_CR Set Price='+Price);
        sql.Add(',Qty='+CDS_Sub.fieldbyname('AOrdQty1').AsString);
        SQL.Add(',CRTime='''+Trim(CDS_Sub.fieldbyname('ADefDate1').AsString)+'''');
        sql.Add(' where YFID='''+Trim(YFID)+'''');
        ExecSQL;
      end;
    end;
  end;


  //if Trim(Order_Main.fieldbyname('DHIDHelp').AsString)='' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      SQL.Add('select Case when HXQty>0 then HCQty-HXQty else HCQty end as HCQty');
      SQL.Add(',Case when HXPS>0 then HCPS-HXPS else HCPS end as HCPS');
      SQL.Add(' from Contract_Cloth_LL where LLID='''+Trim(Order_Main.fieldbyname('LLID').AsString)+'''');
      Open;
    end;
   { with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update YF_Money_CR Set Qty='+ADOQueryTemp.fieldbyname('HCQty').AsString);
      sql.Add(',PS='+ADOQueryTemp.fieldbyname('HCPS').AsString);
      sql.Add(' where YFTypeId='''+Trim(Order_Main.fieldbyname('LLid').AsString)+'''');
      ExecSQL;
    end; }
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_CR Set Money=Price*Qty,BBMoney=Price*Qty');
    sql.Add(' where YFID='''+Trim(YFID)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(' where CRID='+CRID);
    ExecSQL;
  end;
  Result:=True;
end;
function TfrmProductOrderAnPaiGQX.DELYFData():Boolean;
var
  CRID,OrdMainId,YFID,Price,LLID:String;
begin
  Result:=False;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('delete YF_Money_CR where YFTypeID='''+Trim(CDS_Sub.fieldbyname('APID').AsString)+'''');
    ExecSQL;
  end;
  OrdMainId:=Trim(Order_Main.fieldbyname('MainId').AsString);
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from YF_Money_KC where FactoryName='''+Trim(Order_Main.fieldbyname('FirstName').AsString)+'''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False 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(Order_Main.fieldbyname('FirstName').AsString);
      FieldByName('ZdyStr1').Value:='应付付';
      Post;
    end;
  end;
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      SQL.Add('select Case when HXQty>0 then HCQty-HXQty else HCQty end as HCQty');
      SQL.Add(',Case when HXPS>0 then HCPS-HXPS else HCPS end as HCPS');
      SQL.Add(' from Contract_Cloth_LL where LLID='''+Trim(Order_Main.fieldbyname('LLID').AsString)+'''');
      Open;
    end;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
    sql.Add(' where CRID='+CRID);
    ExecSQL;
  end;
  Result:=True;
end;

procedure TfrmProductOrderAnPaiGQX.BtnEditA2BtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='OrderUnit';
      flagname:='数量单位';
      if ShowModal=1 then
      begin
        BtnEditA2.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmProductOrderAnPaiGQX.BtnEditA3BtnClick(Sender: TObject);
begin
  try
    frmZDYHelpSel:=TfrmZDYHelpSel.Create(Application);
    with frmZDYHelpSel do
    begin
      flag:='JYYQ';
      flagname:='检验要求';
      if ShowModal=1 then
      begin
        BtnEditA3.Text:=Trim(BtnEditA3.Text)+frmZDYHelpSel.ReturnStr;
      end;
    end;
  finally
    frmZDYHelpSel.Free;
  end;
end;

procedure TfrmProductOrderAnPaiGQX.ToolButton8Click(Sender: TObject);
begin
  CDS_Sub.DisableControls;
  with CDS_Sub do
  begin
    First;
    while not Eof do
    begin
      Edit;
      FieldByName('SSel').Value:=True;
      Post;
      Next;
    end;
  end;
  CDS_Sub.EnableControls;
end;

procedure TfrmProductOrderAnPaiGQX.ToolButton7Click(Sender: TObject);
begin
  CDS_Sub.DisableControls;
  with CDS_Sub do
  begin
    First;
    while not Eof do
    begin
      Edit;
      FieldByName('SSel').Value:=False;
      Post;
      Next;
    end;
  end;
  CDS_Sub.EnableControls;
end;

procedure TfrmProductOrderAnPaiGQX.BtnEditA4BtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='FactoryNo1Name';
      flagname:='坯布厂';
      MainType:='坯布厂';
      if ShowModal=1 then
      begin
        BtnEditA4.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmProductOrderAnPaiGQX.V2Column11PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='FactoryNo1Name';
      flagname:='坯布厂';
      MainType:='坯布厂';
      if ShowModal=1 then
      begin
        begin
          with CDS_Sub do
          begin
            Edit;
            FieldByName('AOrddefstr6').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString);
          end;
        end;

      end;
    end;
  finally
    //frmZDYHelp.Free;
  end;
end;

procedure TfrmProductOrderAnPaiGQX.ToolButton9Click(Sender: TObject);
begin
  try
    frmBefChkHX:=TfrmBefChkHX.Create(Application);
    with frmBefChkHX do
    begin
      orderno.Caption:=Trim(Self.Order_Main.fieldbyname('OrderNo').AsString);
      PRTColor.Caption:=Trim(Self.Order_Main.fieldbyname('PRTColor').AsString);
      FirstName.Caption:=Trim(Self.Order_Main.fieldbyname('FirstName').AsString);
      PBFactory.Caption:=Trim(Self.Order_Main.fieldbyname('PBFactory').AsString);
      FLLID:=Trim(Self.Order_Main.fieldbyname('LLID').AsString);
      with ADOQuery1 do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from Contract_Cloth_BefChkHX where LLID='''+Trim(Order_Main.fieldbyname('LLID').AsString)+'''');
        Open;
      end;
      SCreateCDS20(ADOQuery1,ClientDataSet1);
      SInitCDSData20(ADOQuery1,ClientDataSet1);
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmBefChkHX.Free;
  end;
end;

procedure TfrmProductOrderAnPaiGQX.ToolButton10Click(Sender: TObject);
begin
  if Order_Main.IsEmpty=False then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,B.*,C.FirstName,C.FirstNo,C.TPPS,C.TPMQty,C.LLID,C.HXPS,C.HXQty,C.HXUnit ');
      sql.Add(',PBFactory=(select Top 1 AA.factoryNoName from Contract_Main AA inner join Contract_Cloth_DH BB on AA.MainId=BB.MainId');
      sql.Add(' where BB.DHID=C.DHID)');
      sql.Add(',HCQty=(select sum(AOrdQty1) from JYOrder_Sub_AnPai AA where AA.SubId=B.SubId)');
      sql.Add(',HCPS=(select sum(AOrdQty2) from JYOrder_Sub_AnPai AA where AA.SubId=B.SubId)');
      sql.Add(',HCUnit=(select Top 1 AOrddefstr2  from JYOrder_Sub_AnPai AA where AA.SubId=B.SubId)');
      sql.Add('from JYOrder_Main A inner join JYOrder_Sub B on A.MainId=B.MainId ');
      sql.Add(' inner join Contract_Cloth_LL C on C.OrdSubId=B.SubId');
      sql.Add('where A.MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      sql.Add(' and C.JXJGFlag=0');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp,Order_Main);
    SInitCDSData20(ADOQueryTemp,Order_Main);
  end;
end;

procedure TfrmProductOrderAnPaiGQX.ToolButton11Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  MovePanel2.Visible:=True;
  MovePanel2.Refresh;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      First;
      while not Eof do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrder_Sub_AnPai where LLId='''+Trim(Order_Main.fieldbyname('LLId').AsString)+'''');
          Open;
        end;
        SCreateCDS20(ADOQueryTemp,CDS_Sub);
        SInitCDSData20(ADOQueryTemp,CDS_Sub);
        with CDS_Sub do
        begin
          First;
          while not Eof do
          begin
            if YFData()=False then
            begin
              MovePanel2.Visible:=False;
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('更新财务应付款失败!','提示',0);
              Exit;
            end;
            Next;
          end;
        end;
        Next;
      end;
    end;
    Order_Main.EnableControls;
    MovePanel2.Visible:=False;
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('操作成功!','提示',0);
    Exit;
  except
    MovePanel2.Visible:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

end.
