unit U_MeetingAPNew;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxDropDownEdit, cxSplitter,
  cxGridLevel, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxClasses, cxControls, cxGridCustomView, cxGrid, StdCtrls, ComCtrls,
  ExtCtrls, cxCalendar, DBClient, ADODB, cxButtonEdit, cxTextEdit,
  RM_Dataset, RM_Common, RM_Class, RM_e_Xls, RM_System, RM_GridReport;

type
  TfrmMeetingAPNew = class(TForm)
    Panel1: TPanel;
    Label3: TLabel;
    OrderNo: TEdit;
    Button9: TButton;
    Button10: TButton;
    Panel4: TPanel;
    Panel6: TPanel;
    Label12: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label25: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    DLYDate: TDateTimePicker;
    MPRTCodeName: TEdit;
    MPRTSpec: TEdit;
    MPRTCF: TEdit;
    MPRTMF: TEdit;
    MPRTKZ: TEdit;
    MPRTKZNote: TEdit;
    LiDanPerson: TEdit;
    CustomerNoName: TEdit;
    MPRTCode: TEdit;
    MPRTGY: TEdit;
    MPRTGYHZ: TEdit;
    Label5: TLabel;
    ZLLevel: TEdit;
    ScrollBox8: TScrollBox;
    Panel9: TPanel;
    ScrollBox1: TScrollBox;
    Panel10: TPanel;
    Panel5: TPanel;
    Label9: TLabel;
    Panel7: TPanel;
    Label23: TLabel;
    Panel11: TPanel;
    Button1: TButton;
    Button2: TButton;
    Panel12: TPanel;
    PingDingNote: TRichEdit;
    Panel13: TPanel;
    Panel15: TPanel;
    Label13: TLabel;
    Panel14: TPanel;
    Label14: TLabel;
    Button3: TButton;
    DS_OrderNo: TDataSource;
    CDS_OrderNo: TClientDataSet;
    cxGrid6: TcxGrid;
    TvOrdeNo: TcxGridDBTableView;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGrid4Level1: TcxGridLevel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQuery1: TADOQuery;
    ADOQuery2: TADOQuery;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    ClientDataSet2: TClientDataSet;
    DataSource2: TDataSource;
    ClientDataSet5: TClientDataSet;
    DataSource5: TDataSource;
    Button5: TButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Label11: TLabel;
    Label31: TLabel;
    SCYaoSu: TRichEdit;
    Panel2: TPanel;
    Label4: TLabel;
    Button4: TButton;
    OtherNote: TRichEdit;
    Panel16: TPanel;
    Panel3: TPanel;
    Label19: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    Label22: TLabel;
    Label24: TLabel;
    Label26: TLabel;
    Label27: TLabel;
    Label28: TLabel;
    Label29: TLabel;
    Label30: TLabel;
    ZPQty: TEdit;
    CPQty: TEdit;
    TJQty: TEdit;
    CKQty: TEdit;
    GDKK: TEdit;
    KHMoney: TEdit;
    SGJSSH: TEdit;
    HZSH: TEdit;
    cxGrid5: TcxGrid;
    Tv5: TcxGridDBTableView;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridLevel5: TcxGridLevel;
    cxGrid4: TcxGrid;
    Tv4: TcxGridDBTableView;
    cxGridDBColumn15: TcxGridDBColumn;
    Tv4Column1: TcxGridDBColumn;
    Tv4Column2: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    Button6: TButton;
    Button7: TButton;
    ClientDataSet4: TClientDataSet;
    DataSource4: TDataSource;
    cxGrid2: TcxGrid;
    TvOrderNoHZ: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    DS_OrderNoHZ: TDataSource;
    CDS_OrderNoHZ: TClientDataSet;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label10: TLabel;
    Label32: TLabel;
    Label33: TLabel;
    MPRTCodeHZ: TEdit;
    Button8: TButton;
    ADOQuery3: TADOQuery;
    Button11: TButton;
    RM1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    RMDBMain: TRMDBDataSet;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button9Click(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure TvOrdeNoDblClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure cxGridDBColumn14PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure cxGridDBColumn15PropertiesEditValueChanged(Sender: TObject);
    procedure Tv4Column1PropertiesEditValueChanged(Sender: TObject);
    procedure Tv4Column2PropertiesEditValueChanged(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Button10Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TvOrderNoHZCellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Button8Click(Sender: TObject);
    procedure MPRTCodeHZChange(Sender: TObject);
    procedure Button11Click(Sender: TObject);
  private
    { Private declarations }

  public
    { Public declarations }
    FMainId:String;
    PState:Integer;
  end;

var
  frmMeetingAPNew: TfrmMeetingAPNew;

implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_ZDYHelpSel, U_ModuleNote,U_OrderInPutPrice;
{$R *.dfm}

procedure TfrmMeetingAPNew.FormDestroy(Sender: TObject);
begin
  frmMeetingAPNew:=nil;
end;

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

procedure TfrmMeetingAPNew.Button9Click(Sender: TObject);
begin
  Close;
  WriteCxGrid('1',Tv1,'生产会议');
  WriteCxGrid('4',Tv4,'生产会议');
  WriteCxGrid('5',Tv5,'生产会议');
  WriteCxGrid('2',TvOrderNoHZ,'生产会议');
end;

procedure TfrmMeetingAPNew.OrderNoChange(Sender: TObject);
var
  mvalue:String;
begin
  mvalue:=Trim(OrderNo.Text);
  if Length(Trim(mvalue))<2 then
  begin
    cxGrid6.Visible:=False;
    Exit;
  end;
  mvalue:='%'+Trim(mvalue)+'%';
  with ADOQuery1 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(ADOQuery1,CDS_OrderNo);
  SInitCDSData20(ADOQuery1,CDS_OrderNo);
  if CDS_OrderNo.IsEmpty then cxGrid6.Visible:=False else cxGrid6.Visible:=True;
end;

procedure TfrmMeetingAPNew.TvOrdeNoDblClick(Sender: TObject);
var
  fsj:String;
begin
  OrderNo.Text:=CDS_OrderNo.fieldbyname('OrderNo').AsString;
  cxGrid6.Visible:=False;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* ');
    sql.Add(' from JYOrder_Main A');
    sql.Add(' where Orderno='''+Trim(OrderNo.Text)+'''');
    Open;
  end;
  SCSHData(ADOQueryTemp,Panel6,2);
  MPRTCode.Text:=Trim(ADOQueryTemp.fieldbyname('MPRTCode').AsString);
  FMainId:=Trim(ADOQueryTemp.fieldbyname('MainId').AsString);
  OtherNote.Text:=Trim(ADOQueryTemp.fieldbyname('OtherNote').AsString);
  PingDingNote.Text:=Trim(ADOQueryTemp.fieldbyname('PingDingNote').AsString);
  SCYaoSu.Text:=Trim(ADOQueryTemp.fieldbyname('SCYaoSu').AsString);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* ');
    sql.Add(',DlyDate=(select Min(PFDlyDate) from JYOrder_PCS_Sub B where B.PSID=A.PSID)');
    sql.Add(',APPS=(select sum(PFTPPS) from JYOrder_PCS_Sub B where B.PSID=A.PSID)');
    sql.Add(',APQty=(select Min(PFTPQty) from JYOrder_PCS_Sub B where B.PSID=A.PSID)');
    sql.Add(',APUnit=(select Min(PFTPUnit) from JYOrder_PCS_Sub B where B.PSID=A.PSID)');
    sql.Add(',MDDate=(select Max(MDDate) from JYOrder_Main_MD B where B.PSID=A.PSID and B.MDNO like ''MD%'')');
    sql.Add(',MDPS=(select sum(PS) from JYOrder_Main_MD B where B.PSID=A.PSID and B.MDNO like ''MD%'')');
    sql.Add(',MDQty=(select sum(Qty) from JYOrder_Main_MD B where B.PSID=A.PSID and B.MDNO like ''MD%'')');
    sql.Add(',BDQty=(select sum(Qty) from JYOrder_Main_MD B where B.PSID=A.PSID and B.MDNO like ''TJ%'' and OtherOrdFlag=0)');
    sql.Add(',FBDQty=(select sum(Qty) from JYOrder_Main_MD B where B.PSID=A.PSID and B.MDNO like ''TJ%'' and OtherOrdFlag=1)');
    sql.Add(',GDPersent=dbo.F_Get_Order_SubStr(A.PSID,''GDPersent'')');
    sql.Add(' from JYOrder_Process A where MainId='''+Trim(FMainId)+'''');
    SQL.Add(' and A.PSName not in(''检验包装'',''出货'')');
    sql.Add(' order by A.PSXH');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet1);
  SInitCDSData20(ADOQueryTemp,ClientDataSet1);
  with ClientDataSet1 do
  begin
    ClientDataSet1.Last;
  end;
  fsj:=Trim(ClientDataSet1.fieldbyname('APUnit').AsString);
  with  ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.HZSH,A.SGJSSH');
    if Trim(fsj)='M'  then
    begin
      sql.Add(' ,ZPQty=(select sum(MJLen) from WFB_MJJY B where B.MainId=A.MainId and isnull(MJType,'''')=''正品'' )');
      sql.Add(' ,CPQty=(select sum(MJLen) from WFB_MJJY B where B.MainId=A.MainId and isnull(MJType,'''')=''次品'' )');
    end else
    begin
      sql.Add(' ,ZPQty=(select sum(MJQty4) from WFB_MJJY B where B.MainId=A.MainId and isnull(MJType,'''')=''正品'')');
      sql.Add(' ,CPQty=(select sum(MJQty4) from WFB_MJJY B where B.MainId=A.MainId and isnull(MJType,'''')=''次品'')');
    end;
    sql.Add(' ,TJQty=(select Sum(Qty-CKQty) from JYOrder_Main_MD B where B.ORDMainId=A.MainId and B.MDNO like ''TJ%'')');
    sql.Add(' ,CKQty=(select Sum(FHQty) from JYOrder_FH B where B.MainId=A.MainId)');
    sql.Add(' ,GDKK=(select Sum(ZZPonMoney) from JYOrder_Main_MD_KK B ');
    sql.Add('        inner join JYOrder_Main_MD C on B.MDID=C.MDID where C.ORDMainId=A.MainId)');
    sql.Add(' from JYOrder_Main A where MainId='''+Trim(FMainId)+'''');
    //ShowMessage(sql.Text);
    Open;
  end;
  SCSHDataNew(ADOQueryTemp,Panel3,2);
  //Button3.Click;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Process Set');
    if Trim(ZPQty.Text)<>'' then
      SQL.Add('ZPQty='+ZPQty.Text)
    else
      SQL.Add('ZPQty=0');
    sql.Add(' where MainId='''+Trim(FMainId)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Process Set ');
    sql.Add('PJFenShu=(select Avg(FenShu) from JYOrder_GD_FenShu A where A.PSID=JYOrder_Process.PSID) ');
    sql.Add(' where MainId='''+Trim(FMainId)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Process Set ');
    sql.Add(' LDMoney=ZPQty*LDPrice*PJFenShu*1.00/10');
    sql.Add(',SHMoney=Cast(ZPQty*SHPrice*');
    sql.Add('(PSSH-(select SGJSSH from JYOrder_Main A where A.MainId=JYOrder_Process.MainId))*1.00/100 as decimal(18,2))');
    sql.Add(' where MainId='''+Trim(FMainId)+'''');
    ExecSQL;
  end;
  {with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Process Set ');
    sql.Add(' SHMoney=0');
    sql.Add(' where MainId='''+Trim(FMainId)+'''');
    sql.Add(' and PSName=''坯布'' and PSXH=1 ');
    ExecSQL;
  end; }
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update JYOrder_Process Set SHMoney=SHMoney*PJFenShu*1.00/10');
    sql.Add(' where MainId='''+Trim(FMainId)+'''');
    sql.Add(' and isnull(SHMoney,0)>0');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' Update JYOrder_PCS_GD Set ');
    sql.Add(' LDMoney=(select LDMoney from JYOrder_Process A where A.PSID=JYOrder_PCS_GD.PSID)*QtyPersent*1.00/100');
    sql.Add(' ,SHMoney=(select SHMoney from JYOrder_Process A where A.PSID=JYOrder_PCS_GD.PSID)*QtyPersent*1.00/100');
    sql.Add(' where MainId='''+Trim(FMainId)+'''');
    ExecSQL;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select AA.*,HZMoney=isnull(LDMoney,0)+isnull(SHMoney,0)-isnull(KKMoney,0) from (');
    sql.Add('select GenDanPerson,LDMoney=sum(LDMoney),SHMoney=sum(SHMoney)');
    sql.Add(' ,KKMoney=(select Sum(ZZPonMoney) from JYOrder_Main_MD_KK B ');
    sql.Add('        inner join JYOrder_Main_MD C on B.MDID=C.MDID ');
    sql.Add('        where C.ORDMainId=A.MainId and B.ZZPerson=A.GenDanPerson and isnull(B.KKTYpe,'''')=''结果'') ');
    sql.Add(' from JYOrder_PCS_GD A');
    sql.Add(' where MainId='''+Trim(FMainId)+'''');
    sql.Add(' group by GenDanPerson,MainId)AA');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet5);
  SInitCDSData20(ADOQueryTemp,ClientDataSet5);
  if ClientDataSet5.IsEmpty=False then
  begin
    KHMoney.Text:=Tv5.DataController.Summary.FooterSummaryTexts[5];
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* ');
    sql.Add(' from JYOrder_Main A');
    sql.Add(' where MainId='''+Trim(FMainId)+'''');
    Open;
  end;
  SCSHData(ADOQueryTemp,Panel6,2);
  MPRTCode.Text:=Trim(ADOQueryTemp.fieldbyname('MPRTCode').AsString);
  //FMainId:=Trim(ADOQueryTemp.fieldbyname('MainId').AsString);
  OtherNote.Text:=Trim(ADOQueryTemp.fieldbyname('OtherNote').AsString);
  PingDingNote.Text:=Trim(ADOQueryTemp.fieldbyname('PingDingNote').AsString);
  SCYaoSu.Text:=Trim(ADOQueryTemp.fieldbyname('SCYaoSu').AsString);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* ');
    sql.Add(',DlyDate=(select Min(PFDlyDate) from JYOrder_PCS_Sub B where B.PSID=A.PSID)');
    sql.Add(',APPS=(select sum(PFTPPS) from JYOrder_PCS_Sub B where B.PSID=A.PSID)');
    sql.Add(',APQty=(select Min(PFTPQty) from JYOrder_PCS_Sub B where B.PSID=A.PSID)');
    sql.Add(',APUnit=(select Min(PFTPUnit) from JYOrder_PCS_Sub B where B.PSID=A.PSID)');
    sql.Add(',MDDate=(select Max(MDDate) from JYOrder_Main_MD B where B.PSID=A.PSID and B.MDNO like ''MD%'')');
    sql.Add(',MDPS=(select sum(PS) from JYOrder_Main_MD B where B.PSID=A.PSID and B.MDNO like ''MD%'')');
    sql.Add(',MDQty=(select sum(Qty) from JYOrder_Main_MD B where B.PSID=A.PSID and B.MDNO like ''MD%'')');
    sql.Add(',BDQty=(select sum(Qty) from JYOrder_Main_MD B where B.PSID=A.PSID and B.MDNO like ''TJ%'' and OtherOrdFlag=0)');
    sql.Add(',FBDQty=(select sum(Qty) from JYOrder_Main_MD B where B.PSID=A.PSID and B.MDNO like ''TJ%'' and OtherOrdFlag=1)');
    sql.Add(',GDPersent=dbo.F_Get_Order_SubStr(A.PSID,''GDPersent'')');
    sql.Add(' from JYOrder_Process A where MainId='''+Trim(FMainId)+'''');
    SQL.Add(' and A.PSName not in(''检验包装'',''出货'')');
    sql.Add(' order by A.PSXH');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet1);
  SInitCDSData20(ADOQueryTemp,ClientDataSet1);
  with ClientDataSet1 do
  begin
    ClientDataSet1.Last;
  end;
  fsj:=Trim(ClientDataSet1.fieldbyname('APUnit').AsString);
  with  ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.HZSH,A.SGJSSH');
    if Trim(fsj)='M'  then
    begin
      sql.Add(' ,ZPQty=(select sum(MJLen) from WFB_MJJY B where B.MainId=A.MainId and isnull(MJType,'''')=''正品'' )');
      sql.Add(' ,CPQty=(select sum(MJLen) from WFB_MJJY B where B.MainId=A.MainId and isnull(MJType,'''')=''次品'' )');
    end else
    begin
      sql.Add(' ,ZPQty=(select sum(MJQty4) from WFB_MJJY B where B.MainId=A.MainId and isnull(MJType,'''')=''正品'')');
      sql.Add(' ,CPQty=(select sum(MJQty4) from WFB_MJJY B where B.MainId=A.MainId and isnull(MJType,'''')=''次品'')');
    end;
    sql.Add(' ,TJQty=(select Sum(Qty-CKQty) from JYOrder_Main_MD B where B.ORDMainId=A.MainId and B.MDNO like ''TJ%'')');
    sql.Add(' ,CKQty=(select Sum(FHQty) from JYOrder_FH B where B.MainId=A.MainId)');
    sql.Add(' ,GDKK=(select Sum(ZZPonMoney) from JYOrder_Main_MD_KK B ');
    sql.Add('        inner join JYOrder_Main_MD C on B.MDID=C.MDID where C.ORDMainId=A.MainId)');
    sql.Add(' from JYOrder_Main A where MainId='''+Trim(FMainId)+'''');
    //ShowMessage(sql.Text);
    Open;
  end;
  SCSHDataNew(ADOQueryTemp,Panel3,2);
  //InitGrid();
end;

procedure TfrmMeetingAPNew.Button1Click(Sender: TObject);
var
  maxno:String;
begin
  if Trim(OrderNo.Text)='' then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if GetLSNo(ADOQueryCmd,maxno,'GF','JYOrder_GD_FenShu',3,1)=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取跟单评分最大号失败!','提示',0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' select * from JYOrder_GD_FenShu where 1<>1');
      Open;
    end;
    with ADOQueryCmd do
    begin
      Append;
      FieldByName('MainId').Value:=Trim(FMainId);
      FieldByName('PSID').Value:=Trim(ClientDataSet1.fieldbyname('PSID').AsString);
      FieldByName('GFId').Value:=Trim(maxno);
      FieldByName('GFDate').Value:=Trim(FormatDateTime('yyyy-MM-dd',SGetServerDate(ADOQueryTemp)));
      Post;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    with ClientDataSet4 do
    begin
      Append;
      FieldByName('GFId').Value:=Trim(maxno);
      FieldByName('GFDate').Value:=Trim(FormatDateTime('yyyy-MM-dd',SGetServerDate(ADOQueryTemp)));
      Post;
    end;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('增行异常!','提示',0);
  end;

end;

procedure TfrmMeetingAPNew.cxGridDBColumn14PropertiesButtonClick(
  Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='PFGenDanPerson';
      flagname:='跟单员';
      fnote:=True;
      V1Note.Caption:='电话';
      if ShowModal=1 then
      begin
        with Self.ClientDataSet4 do
        begin
          Edit;
          FieldByName('GenDanPerson').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        with Self.ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('UPdate JYOrder_GD_FenShu Set GenDanPerson='''+Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString)+'''');
          sql.Add(' where GFID='''+Trim(Self.ClientDataSet4.fieldbyname('GFID').AsString)+'''');
          ExecSQL;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmMeetingAPNew.cxGridDBColumn15PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:String;
begin
  mvalue:=TcxDateEdit(Sender).EditText;
  if Trim(mvalue)='' then
  begin
    mvalue:=null;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    if Trim(mvalue)<>'' then
      sql.Add('UPdate JYOrder_GD_FenShu Set GFDate='''+Trim(mvalue)+'''')
    else
      sql.Add('UPdate JYOrder_GD_FenShu Set GFDate=Null');
    sql.Add(' where GFID='''+Trim(ClientDataSet4.fieldbyname('GFID').AsString)+'''');
    ExecSQL;
  end;
  with ClientDataSet4 do
  begin
    Edit;
    FieldByName('GFDate').Value:=mvalue;
    Post;
  end;
end;

procedure TfrmMeetingAPNew.Tv4Column1PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:String;
begin
  mvalue:=TcxComboBox(Sender).EditText;
  if Trim(mvalue)='' then
  begin
    mvalue:=null;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    if Trim(mvalue)<>'' then
      sql.Add('UPdate JYOrder_GD_FenShu Set FenShu='+Trim(mvalue))
    else
      sql.Add('UPdate JYOrder_GD_FenShu Set FenShu=Null');
    sql.Add(' where GFID='''+Trim(ClientDataSet4.fieldbyname('GFID').AsString)+'''');
    ExecSQL;
  end;
  with ClientDataSet4 do
  begin
    Edit;
    FieldByName('FenShu').Value:=mvalue;
    Post;
  end;
end;

procedure TfrmMeetingAPNew.Tv4Column2PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:=null;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    if Trim(mvalue)<>'' then
      sql.Add('UPdate JYOrder_GD_FenShu Set GFNote='''+Trim(mvalue)+'''')
    else
      sql.Add('UPdate JYOrder_GD_FenShu Set GFNote=Null');
    sql.Add(' where GFID='''+Trim(ClientDataSet4.fieldbyname('GFID').AsString)+'''');
    ExecSQL;
  end;
  with ClientDataSet4 do
  begin
    Edit;
    FieldByName('GFNote').Value:=mvalue;
    Post;
  end;
end;

procedure TfrmMeetingAPNew.Button2Click(Sender: TObject);
begin
  if ClientDataSet4.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('delete JYOrder_GD_FenShu where GFID='''+Trim(ClientDataSet4.fieldbyname('GFID').AsString)+'''');
    ExecSQL;
  end;
  ClientDataSet4.Delete;
end;

procedure TfrmMeetingAPNew.Button4Click(Sender: TObject);
begin
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' Update JYOrder_Main Set OtherNote='''+Trim(OtherNote.Text)+'''');
    SQL.Add(' where Mainid='''+Trim(FMainId)+'''');
    ExecSQL;
  end;
  Application.MessageBox('操作成功!','提示',0);
  Exit;
end;

procedure TfrmMeetingAPNew.Button5Click(Sender: TObject);
begin
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' Update JYOrder_Main Set PingDingNote='''+Trim(PingDingNote.Text)+'''');
    SQL.Add(' where Mainid='''+Trim(FMainId)+'''');
    ExecSQL;
  end;
  Application.MessageBox('操作成功!','提示',0);
  Exit;
end;

procedure TfrmMeetingAPNew.Button3Click(Sender: TObject);
var
  fsj:string;
begin
  with ADOQuery3 do
  begin
    Close;
    sql.Clear;
    sql.Add('select MainId,OrderNo,MPRTCodeHZ=MPRTCode from JYOrder_Main where OrdDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
    sql.Add(' and OrdDate<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery3,CDS_OrderNoHZ);
  SInitCDSData20(ADOQuery3,CDS_OrderNoHZ);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Process Set');
    if Trim(ZPQty.Text)<>'' then
      SQL.Add('ZPQty='+ZPQty.Text)
    else
      SQL.Add('ZPQty=0');
    sql.Add(' where MainId='''+Trim(FMainId)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Process Set ');
    sql.Add('PJFenShu=(select Avg(FenShu) from JYOrder_GD_FenShu A where A.PSID=JYOrder_Process.PSID) ');
    sql.Add(' where MainId='''+Trim(FMainId)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Process Set ');
    sql.Add(' LDMoney=ZPQty*LDPrice*PJFenShu*1.00/10');
    sql.Add(',SHMoney=Cast(ZPQty*SHPrice*');
    sql.Add('(PSSH-(select SGJSSH from JYOrder_Main A where A.MainId=JYOrder_Process.MainId))*1.00/100 as decimal(18,2))');
    sql.Add(' where MainId='''+Trim(FMainId)+'''');
    ExecSQL;
  end;
  {with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Process Set ');
    sql.Add(' SHMoney=0');
    sql.Add(' where MainId='''+Trim(FMainId)+'''');
    sql.Add(' and PSName=''坯布'' and PSXH=1 ');
    ExecSQL;
  end; }
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update JYOrder_Process Set SHMoney=SHMoney*PJFenShu*1.00/10');
    sql.Add(' where MainId='''+Trim(FMainId)+'''');
    sql.Add(' and isnull(SHMoney,0)>0');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' Update JYOrder_PCS_GD Set ');
    sql.Add(' LDMoney=(select LDMoney from JYOrder_Process A where A.PSID=JYOrder_PCS_GD.PSID)*QtyPersent*1.00/100');
    sql.Add(' ,SHMoney=(select SHMoney from JYOrder_Process A where A.PSID=JYOrder_PCS_GD.PSID)*QtyPersent*1.00/100');
    sql.Add(' where MainId='''+Trim(FMainId)+'''');
    ExecSQL;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select AA.*,HZMoney=isnull(LDMoney,0)+isnull(SHMoney,0)-isnull(KKMoney,0) from (');
    sql.Add('select GenDanPerson,LDMoney=sum(LDMoney),SHMoney=sum(SHMoney)');
    sql.Add(' ,KKMoney=(select Sum(ZZPonMoney) from JYOrder_Main_MD_KK B ');
    sql.Add('        inner join JYOrder_Main_MD C on B.MDID=C.MDID ');
    sql.Add('        where C.ORDMainId=A.MainId and B.ZZPerson=A.GenDanPerson and isnull(B.KKTYpe,'''')=''结果'') ');
    sql.Add(' from JYOrder_PCS_GD A');
    sql.Add(' where MainId='''+Trim(FMainId)+'''');
    sql.Add(' group by GenDanPerson,MainId)AA');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet5);
  SInitCDSData20(ADOQueryTemp,ClientDataSet5);
  if ClientDataSet5.IsEmpty=False then
  begin
    KHMoney.Text:=Tv5.DataController.Summary.FooterSummaryTexts[5];
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* ');
    sql.Add(' from JYOrder_Main A');
    sql.Add(' where MainId='''+Trim(FMainId)+'''');
    Open;
  end;
  SCSHData(ADOQueryTemp,Panel6,2);
  MPRTCode.Text:=Trim(ADOQueryTemp.fieldbyname('MPRTCode').AsString);
  //FMainId:=Trim(ADOQueryTemp.fieldbyname('MainId').AsString);
  OtherNote.Text:=Trim(ADOQueryTemp.fieldbyname('OtherNote').AsString);
  PingDingNote.Text:=Trim(ADOQueryTemp.fieldbyname('PingDingNote').AsString);
  SCYaoSu.Text:=Trim(ADOQueryTemp.fieldbyname('SCYaoSu').AsString);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* ');
    sql.Add(',DlyDate=(select Min(PFDlyDate) from JYOrder_PCS_Sub B where B.PSID=A.PSID)');
    sql.Add(',APPS=(select sum(PFTPPS) from JYOrder_PCS_Sub B where B.PSID=A.PSID)');
    sql.Add(',APQty=(select Min(PFTPQty) from JYOrder_PCS_Sub B where B.PSID=A.PSID)');
    sql.Add(',APUnit=(select Min(PFTPUnit) from JYOrder_PCS_Sub B where B.PSID=A.PSID)');
    sql.Add(',MDDate=(select Max(MDDate) from JYOrder_Main_MD B where B.PSID=A.PSID and B.MDNO like ''MD%'')');
    sql.Add(',MDPS=(select sum(PS) from JYOrder_Main_MD B where B.PSID=A.PSID and B.MDNO like ''MD%'')');
    sql.Add(',MDQty=(select sum(Qty) from JYOrder_Main_MD B where B.PSID=A.PSID and B.MDNO like ''MD%'')');
    sql.Add(',BDQty=(select sum(Qty) from JYOrder_Main_MD B where B.PSID=A.PSID and B.MDNO like ''TJ%'' and OtherOrdFlag=0)');
    sql.Add(',FBDQty=(select sum(Qty) from JYOrder_Main_MD B where B.PSID=A.PSID and B.MDNO like ''TJ%'' and OtherOrdFlag=1)');
    sql.Add(',GDPersent=dbo.F_Get_Order_SubStr(A.PSID,''GDPersent'')');
    sql.Add(' from JYOrder_Process A where MainId='''+Trim(FMainId)+'''');
    SQL.Add(' and A.PSName not in(''检验包装'',''出货'')');
    sql.Add(' order by A.PSXH');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet1);
  SInitCDSData20(ADOQueryTemp,ClientDataSet1);
  with ClientDataSet1 do
  begin
    ClientDataSet1.Last;
  end;
  fsj:=Trim(ClientDataSet1.fieldbyname('APUnit').AsString);
  with  ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.HZSH,A.SGJSSH');
    if Trim(fsj)='M'  then
    begin
      sql.Add(' ,ZPQty=(select sum(MJLen) from WFB_MJJY B where B.MainId=A.MainId and isnull(MJType,'''')=''正品'' )');
      sql.Add(' ,CPQty=(select sum(MJLen) from WFB_MJJY B where B.MainId=A.MainId and isnull(MJType,'''')=''次品'' )');
    end else
    begin
      sql.Add(' ,ZPQty=(select sum(MJQty4) from WFB_MJJY B where B.MainId=A.MainId and isnull(MJType,'''')=''正品'')');
      sql.Add(' ,CPQty=(select sum(MJQty4) from WFB_MJJY B where B.MainId=A.MainId and isnull(MJType,'''')=''次品'')');
    end;
    sql.Add(' ,TJQty=(select Sum(Qty-CKQty) from JYOrder_Main_MD B where B.ORDMainId=A.MainId and B.MDNO like ''TJ%'')');
    sql.Add(' ,CKQty=(select Sum(FHQty) from JYOrder_FH B where B.MainId=A.MainId)');
    sql.Add(' ,GDKK=(select Sum(ZZPonMoney) from JYOrder_Main_MD_KK B ');
    sql.Add('        inner join JYOrder_Main_MD C on B.MDID=C.MDID where C.ORDMainId=A.MainId)');
    sql.Add(' from JYOrder_Main A where MainId='''+Trim(FMainId)+'''');
    //ShowMessage(sql.Text);
    Open;
  end;
  SCSHDataNew(ADOQueryTemp,Panel3,2);
  //TvOrdeNoDblClick(Sender);
end;

procedure TfrmMeetingAPNew.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_GD_FenShu where PSID='''+Trim(ClientDataSet1.fieldbyname('PSID').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet4);
  SInitCDSData20(ADOQueryTemp,ClientDataSet4);
end;

procedure TfrmMeetingAPNew.Button10Click(Sender: TObject);
begin
  try
    frmModuleNote:=TfrmModuleNote.Create(Application);
    with frmModuleNote do
    begin
      flag:='生产会议';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmModuleNote.Free;
  end;
end;

procedure TfrmMeetingAPNew.FormShow(Sender: TObject);
begin
  ReadCxGrid('1',Tv1,'生产会议');
  ReadCxGrid('4',Tv4,'生产会议');
  ReadCxGrid('5',Tv5,'生产会议');
  ReadCxGrid('2',TvOrderNoHZ,'生产会议');
  if Trim(FMainId)<>'' then
  begin
    cxGrid6.Visible:=False;
    Button3.Click;
  end;
  if PState=2 then
  begin
    Button1.Visible:=False;
    Button2.Visible:=False;
    Button4.Visible:=False;
    Button6.Visible:=False;
    Button5.Visible:=False;
    Button7.Visible:=False;
  end;
end;

procedure TfrmMeetingAPNew.FormCreate(Sender: TObject);
begin
  EndDate.DateTime:=SGetServerDate(ADOQueryTemp);
  BegDate.DateTime:=EndDate.DateTime-30;
end;

procedure TfrmMeetingAPNew.TvOrderNoHZCellClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var
  fsj:String;
begin
  OrderNo.Text:=CDS_OrderNoHZ.fieldbyname('OrderNo').AsString;
  cxGrid6.Visible:=False;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* ');
    sql.Add(' from JYOrder_Main A');
    sql.Add(' where Orderno='''+Trim(OrderNo.Text)+'''');
    Open;
  end;
  SCSHData(ADOQueryTemp,Panel6,2);
  MPRTCode.Text:=Trim(ADOQueryTemp.fieldbyname('MPRTCode').AsString);
  FMainId:=Trim(ADOQueryTemp.fieldbyname('MainId').AsString);
  OtherNote.Text:=Trim(ADOQueryTemp.fieldbyname('OtherNote').AsString);
  PingDingNote.Text:=Trim(ADOQueryTemp.fieldbyname('PingDingNote').AsString);
  SCYaoSu.Text:=Trim(ADOQueryTemp.fieldbyname('SCYaoSu').AsString);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* ');
    sql.Add(',DlyDate=(select Min(PFDlyDate) from JYOrder_PCS_Sub B where B.PSID=A.PSID)');
    sql.Add(',APPS=(select sum(PFTPPS) from JYOrder_PCS_Sub B where B.PSID=A.PSID)');
    sql.Add(',APQty=(select Min(PFTPQty) from JYOrder_PCS_Sub B where B.PSID=A.PSID)');
    sql.Add(',APUnit=(select Min(PFTPUnit) from JYOrder_PCS_Sub B where B.PSID=A.PSID)');
    sql.Add(',MDDate=(select Max(MDDate) from JYOrder_Main_MD B where B.PSID=A.PSID and B.MDNO like ''MD%'')');
    sql.Add(',MDPS=(select sum(PS) from JYOrder_Main_MD B where B.PSID=A.PSID and B.MDNO like ''MD%'')');
    sql.Add(',MDQty=(select sum(Qty) from JYOrder_Main_MD B where B.PSID=A.PSID and B.MDNO like ''MD%'')');
    sql.Add(',BDQty=(select sum(Qty) from JYOrder_Main_MD B where B.PSID=A.PSID and B.MDNO like ''TJ%'' and OtherOrdFlag=0)');
    sql.Add(',FBDQty=(select sum(Qty) from JYOrder_Main_MD B where B.PSID=A.PSID and B.MDNO like ''TJ%'' and OtherOrdFlag=1)');
    sql.Add(',GDPersent=dbo.F_Get_Order_SubStr(A.PSID,''GDPersent'')');
    sql.Add(' from JYOrder_Process A where MainId='''+Trim(FMainId)+'''');
    SQL.Add(' and A.PSName not in(''检验包装'',''出货'')');
    sql.Add(' order by A.PSXH');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet1);
  SInitCDSData20(ADOQueryTemp,ClientDataSet1);
  with ClientDataSet1 do
  begin
    ClientDataSet1.Last;
  end;
  fsj:=Trim(ClientDataSet1.fieldbyname('APUnit').AsString);
  with  ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.HZSH,A.SGJSSH');
    if Trim(fsj)='M'  then
    begin
      sql.Add(' ,ZPQty=(select sum(MJLen) from WFB_MJJY B where B.MainId=A.MainId and isnull(MJType,'''')=''正品'' )');
      sql.Add(' ,CPQty=(select sum(MJLen) from WFB_MJJY B where B.MainId=A.MainId and isnull(MJType,'''')=''次品'' )');
    end else
    begin
      sql.Add(' ,ZPQty=(select sum(MJQty4) from WFB_MJJY B where B.MainId=A.MainId and isnull(MJType,'''')=''正品'')');
      sql.Add(' ,CPQty=(select sum(MJQty4) from WFB_MJJY B where B.MainId=A.MainId and isnull(MJType,'''')=''次品'')');
    end;
    sql.Add(' ,TJQty=(select Sum(Qty-CKQty) from JYOrder_Main_MD B where B.ORDMainId=A.MainId and B.MDNO like ''TJ%'')');
    sql.Add(' ,CKQty=(select Sum(FHQty) from JYOrder_FH B where B.MainId=A.MainId)');
    sql.Add(' ,GDKK=(select Sum(ZZPonMoney) from JYOrder_Main_MD_KK B ');
    sql.Add('        inner join JYOrder_Main_MD C on B.MDID=C.MDID where C.ORDMainId=A.MainId)');
    sql.Add(' from JYOrder_Main A where MainId='''+Trim(FMainId)+'''');
    //ShowMessage(sql.Text);
    Open;
  end;
  SCSHDataNew(ADOQueryTemp,Panel3,2);
  //Button3.Click;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Process Set');
    if Trim(ZPQty.Text)<>'' then
      SQL.Add('ZPQty='+ZPQty.Text)
    else
      SQL.Add('ZPQty=0');
    sql.Add(' where MainId='''+Trim(FMainId)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Process Set ');
    sql.Add('PJFenShu=(select Avg(FenShu) from JYOrder_GD_FenShu A where A.PSID=JYOrder_Process.PSID) ');
    sql.Add(' where MainId='''+Trim(FMainId)+'''');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Process Set ');
    sql.Add(' LDMoney=ZPQty*LDPrice*PJFenShu*1.00/10');
    sql.Add(',SHMoney=Cast(ZPQty*SHPrice*');
    sql.Add('(PSSH-(select SGJSSH from JYOrder_Main A where A.MainId=JYOrder_Process.MainId))*1.00/100 as decimal(18,2))');
    sql.Add(' where MainId='''+Trim(FMainId)+'''');
    ExecSQL;
  end;
  {with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Process Set ');
    sql.Add(' SHMoney=0');
    sql.Add(' where MainId='''+Trim(FMainId)+'''');
    sql.Add(' and PSName=''坯布'' and PSXH=1 ');
    ExecSQL;
  end; }
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update JYOrder_Process Set SHMoney=SHMoney*PJFenShu*1.00/10');
    sql.Add(' where MainId='''+Trim(FMainId)+'''');
    sql.Add(' and isnull(SHMoney,0)>0');
    ExecSQL;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' Update JYOrder_PCS_GD Set ');
    sql.Add(' LDMoney=(select LDMoney from JYOrder_Process A where A.PSID=JYOrder_PCS_GD.PSID)*QtyPersent*1.00/100');
    sql.Add(' ,SHMoney=(select SHMoney from JYOrder_Process A where A.PSID=JYOrder_PCS_GD.PSID)*QtyPersent*1.00/100');
    sql.Add(' where MainId='''+Trim(FMainId)+'''');
    ExecSQL;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select AA.*,HZMoney=isnull(LDMoney,0)+isnull(SHMoney,0)-isnull(KKMoney,0) from (');
    sql.Add('select GenDanPerson,LDMoney=sum(LDMoney),SHMoney=sum(SHMoney)');
    sql.Add(' ,KKMoney=(select Sum(ZZPonMoney) from JYOrder_Main_MD_KK B ');
    sql.Add('        inner join JYOrder_Main_MD C on B.MDID=C.MDID ');
    sql.Add('        where C.ORDMainId=A.MainId and B.ZZPerson=A.GenDanPerson and isnull(B.KKTYpe,'''')=''结果'') ');
    sql.Add(' from JYOrder_PCS_GD A');
    sql.Add(' where MainId='''+Trim(FMainId)+'''');
    sql.Add(' group by GenDanPerson,MainId)AA');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet5);
  SInitCDSData20(ADOQueryTemp,ClientDataSet5);
  if ClientDataSet5.IsEmpty=False then
  begin
    KHMoney.Text:=Tv5.DataController.Summary.FooterSummaryTexts[5];
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* ');
    sql.Add(' from JYOrder_Main A');
    sql.Add(' where MainId='''+Trim(FMainId)+'''');
    Open;
  end;
  SCSHData(ADOQueryTemp,Panel6,2);
  MPRTCode.Text:=Trim(ADOQueryTemp.fieldbyname('MPRTCode').AsString);
  //FMainId:=Trim(ADOQueryTemp.fieldbyname('MainId').AsString);
  OtherNote.Text:=Trim(ADOQueryTemp.fieldbyname('OtherNote').AsString);
  PingDingNote.Text:=Trim(ADOQueryTemp.fieldbyname('PingDingNote').AsString);
  SCYaoSu.Text:=Trim(ADOQueryTemp.fieldbyname('SCYaoSu').AsString);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* ');
    sql.Add(',DlyDate=(select Min(PFDlyDate) from JYOrder_PCS_Sub B where B.PSID=A.PSID)');
    sql.Add(',APPS=(select sum(PFTPPS) from JYOrder_PCS_Sub B where B.PSID=A.PSID)');
    sql.Add(',APQty=(select Min(PFTPQty) from JYOrder_PCS_Sub B where B.PSID=A.PSID)');
    sql.Add(',APUnit=(select Min(PFTPUnit) from JYOrder_PCS_Sub B where B.PSID=A.PSID)');
    sql.Add(',MDDate=(select Max(MDDate) from JYOrder_Main_MD B where B.PSID=A.PSID and B.MDNO like ''MD%'')');
    sql.Add(',MDPS=(select sum(PS) from JYOrder_Main_MD B where B.PSID=A.PSID and B.MDNO like ''MD%'')');
    sql.Add(',MDQty=(select sum(Qty) from JYOrder_Main_MD B where B.PSID=A.PSID and B.MDNO like ''MD%'')');
    sql.Add(',BDQty=(select sum(Qty) from JYOrder_Main_MD B where B.PSID=A.PSID and B.MDNO like ''TJ%'' and OtherOrdFlag=0)');
    sql.Add(',FBDQty=(select sum(Qty) from JYOrder_Main_MD B where B.PSID=A.PSID and B.MDNO like ''TJ%'' and OtherOrdFlag=1)');
    sql.Add(',GDPersent=dbo.F_Get_Order_SubStr(A.PSID,''GDPersent'')');
    sql.Add(' from JYOrder_Process A where MainId='''+Trim(FMainId)+'''');
    SQL.Add(' and A.PSName not in(''检验包装'',''出货'')');
    sql.Add(' order by A.PSXH');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet1);
  SInitCDSData20(ADOQueryTemp,ClientDataSet1);
  with ClientDataSet1 do
  begin
    ClientDataSet1.Last;
  end;
  fsj:=Trim(ClientDataSet1.fieldbyname('APUnit').AsString);
  with  ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.HZSH,A.SGJSSH');
    if Trim(fsj)='M'  then
    begin
      sql.Add(' ,ZPQty=(select sum(MJLen) from WFB_MJJY B where B.MainId=A.MainId and isnull(MJType,'''')=''正品'' )');
      sql.Add(' ,CPQty=(select sum(MJLen) from WFB_MJJY B where B.MainId=A.MainId and isnull(MJType,'''')=''次品'' )');
    end else
    begin
      sql.Add(' ,ZPQty=(select sum(MJQty4) from WFB_MJJY B where B.MainId=A.MainId and isnull(MJType,'''')=''正品'')');
      sql.Add(' ,CPQty=(select sum(MJQty4) from WFB_MJJY B where B.MainId=A.MainId and isnull(MJType,'''')=''次品'')');
    end;
    sql.Add(' ,TJQty=(select Sum(Qty-CKQty) from JYOrder_Main_MD B where B.ORDMainId=A.MainId and B.MDNO like ''TJ%'')');
    sql.Add(' ,CKQty=(select Sum(FHQty) from JYOrder_FH B where B.MainId=A.MainId)');
    sql.Add(' ,GDKK=(select Sum(ZZPonMoney) from JYOrder_Main_MD_KK B ');
    sql.Add('        inner join JYOrder_Main_MD C on B.MDID=C.MDID where C.ORDMainId=A.MainId)');
    sql.Add(' from JYOrder_Main A where MainId='''+Trim(FMainId)+'''');
    //ShowMessage(sql.Text);
    Open;
  end;
  SCSHDataNew(ADOQueryTemp,Panel3,2);
  //InitGrid();
end;

procedure TfrmMeetingAPNew.Button8Click(Sender: TObject);
var
   FFFF:String;
begin
  if Trim(FMainId)='' then Exit;
  FFFF:=FMainId;
  try
    frmOrderInPutPrice:=TfrmOrderInPutPrice.Create(Application);
    with frmOrderInPutPrice do
    begin
      PState:=1;
      frmOrderInPutPrice.FMainId:=Trim(FFFF);
      ToolBar2.Visible:=False;
      //ToolBar3.Visible:=False;
      ToolBar4.Visible:=False;
      ToolBar5.Visible:=False;
      TBSave.Visible:=False;
      ToolButton4.Visible:=False;
      ToolButton5.Visible:=False;
      ToolButton8.Visible:=False;
      ToolButton10.Visible:=False;
      ToolButton11.Visible:=False;
      //ScrollBox1.Enabled:=False;
      Tv1.OptionsSelection.CellSelect:=False;
      ToolButton20.Visible:=False;
      ToolButton21.Visible:=False;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmOrderInPutPrice.Free;
  end;
end;

procedure TfrmMeetingAPNew.MPRTCodeHZChange(Sender: TObject);
begin
  if ADOQuery3.Active=False then Exit;
  SDofilter(ADOQuery3,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQuery3,CDS_OrderNoHZ);
  SInitCDSData20(ADOQuery3,CDS_OrderNoHZ);
end;

procedure TfrmMeetingAPNew.Button11Click(Sender: TObject);
var
   fPrintFile:String;
begin
  fPrintFile:=ExtractFilePath(Application.ExeName)+'Report\生产会议.rmf' ;
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\生产会议'),'提示',0);
  end;

end;


end.
