unit U_OrdCostQtyList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, ExtCtrls, ToolWin, cxPC, cxControls,
  cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxCalendar, cxDropDownEdit, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxGridCustomView, cxGrid, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB,
  DBClient, cxButtonEdit, cxGridBandedTableView, cxGridDBBandedTableView,
  cxCheckBox, cxTextEdit, cxSplitter;

type
  TfrmOrdCostQtyList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label3: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label5: TLabel;
    MPRTCodeName: TEdit;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    orderNo: TEdit;
    Label7: TLabel;
    MPRTCode: TEdit;
    DataSource3: TDataSource;
    ClientDataSet3: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQuery1: TADOQuery;
    cxGridPopupMenu3: TcxGridPopupMenu;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQuery3: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DataSource2: TDataSource;
    ClientDataSet2: TClientDataSet;
    cxGridPopupMenu5: TcxGridPopupMenu;
    ClientDataSet5: TClientDataSet;
    DataSource5: TDataSource;
    Panel2: TPanel;
    ScrollBox1: TScrollBox;
    cxGrid3: TcxGrid;
    Tv1: TcxGridDBTableView;
    v3Column3: TcxGridDBColumn;
    v3Column6: TcxGridDBColumn;
    v3Column7: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    v3Column8: TcxGridDBColumn;
    v3Column9: TcxGridDBColumn;
    v3Column10: TcxGridDBColumn;
    v3Column11: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGrid2: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridDBColumn22: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridDBColumn27: TcxGridDBColumn;
    cxGridDBColumn28: TcxGridDBColumn;
    cxGridDBTableView2Column1: TcxGridDBColumn;
    cxGridDBTableView2Column2: TcxGridDBColumn;
    cxGridDBTableView2Column3: TcxGridDBColumn;
    v3Column4: TcxGridDBColumn;
    v3Column5: TcxGridDBColumn;
    v3Column12: TcxGridDBColumn;
    v3Column13: TcxGridDBColumn;
    v3Column14: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    cxGrid1: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    Tv3Column1: TcxGridDBColumn;
    Tv3Column2: TcxGridDBColumn;
    Tv3Column3: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    Tv2Column1: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    cxGrid5: TcxGrid;
    Tv5: TcxGridDBTableView;
    cxGridDBColumn23: TcxGridDBColumn;
    cxGridDBColumn26: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v5Column1: TcxGridDBColumn;
    v5Column2: TcxGridDBColumn;
    v5Column5: TcxGridDBColumn;
    cxGridLevel4: TcxGridLevel;
    Label4: TLabel;
    Label6: TLabel;
    Label8: TLabel;
    Label10: TLabel;
    cxGrid7: TcxGrid;
    Tv7: TcxGridDBTableView;
    cxGridDBColumn34: TcxGridDBColumn;
    cxGridDBColumn30: TcxGridDBColumn;
    cxGridDBColumn31: TcxGridDBColumn;
    cxGridDBColumn32: TcxGridDBColumn;
    cxGridDBColumn33: TcxGridDBColumn;
    cxGridDBColumn36: TcxGridDBColumn;
    cxGridLevel5: TcxGridLevel;
    ClientDataSet7: TClientDataSet;
    cxGridPopupMenu7: TcxGridPopupMenu;
    DataSource7: TDataSource;
    v5Column6: TcxGridDBColumn;
    v5Column7: TcxGridDBColumn;
    Label11: TLabel;
    CZZSH: TLabel;
    OrdCost: TLabel;
    QTFee: TLabel;
    HZQty: TLabel;
    AVGPrice: TLabel;
    ToolButton1: TToolButton;
    Panel3: TPanel;
    cxGrid4: TcxGrid;
    Tv0: TcxGridDBTableView;
    cxGridDBColumn29: TcxGridDBColumn;
    cxGridDBColumn35: TcxGridDBColumn;
    cxGridDBColumn37: TcxGridDBColumn;
    cxGridLevel6: TcxGridLevel;
    cxSplitter1: TcxSplitter;
    cxSplitter2: TcxSplitter;
    ClientDataSet0: TClientDataSet;
    cxGridPopupMenu0: TcxGridPopupMenu;
    DataSource0: TDataSource;
    ADOQueryMain: TADOQuery;
    Label9: TLabel;
    SGJSSH: TLabel;
    Label12: TLabel;
    HZSHQty: TLabel;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure orderNoKeyPress(Sender: TObject; var Key: Char);
    procedure TBExportClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure v5Column1PropertiesEditValueChanged(Sender: TObject);
    procedure v5Column5PropertiesEditValueChanged(Sender: TObject);
    procedure v5Column2PropertiesEditValueChanged(Sender: TObject);
    procedure v5Column3PropertiesEditValueChanged(Sender: TObject);
    procedure v5Column4PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv0CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
  private
    { Private declarations }
    canshu1,canshu2:string;
    procedure InitSubGrid();
  public
    { Public declarations }
  end;

var
  frmOrdCostQtyList: TfrmOrdCostQtyList;

implementation
uses
  U_DataLink,U_RTFun, U_ModuleNote;

{$R *.dfm}

procedure TfrmOrdCostQtyList.FormDestroy(Sender: TObject);
begin
  frmOrdCostQtyList:=nil;
end;

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

procedure TfrmOrdCostQtyList.TBRafreshClick(Sender: TObject);
var
  fsj:string;
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.MainId,MPRTCode=RTrim(A.MPRTCode)+RTrim(A.MPRTGY),A.MPRTCodeName,A.OrderNo,A.SGJSSH from JYOrder_Main A where OrdDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
      sql.Add(' and OrdDate<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,ClientDataSet0);
    SInitCDSData20(ADOQueryMain,ClientDataSet0);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmOrdCostQtyList.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
  canshu2:=Trim(DParameters2);
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  BegDate.Date:=EndDate.Date-60;
end;

procedure TfrmOrdCostQtyList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('订单列表Qty',Tv1,'订单核算');
  WriteCxGrid('正常码单Qty',Tv2,'订单核算');
  WriteCxGrid('调节仓库Qty',Tv3,'订单核算');
  WriteCxGrid('其他费用Qty',Tv7,'订单核算');
  WriteCxGrid('订单号Qty',Tv7,'订单核算');
  Close;
end;

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

procedure TfrmOrdCostQtyList.orderNoKeyPress(Sender: TObject; var Key: Char);
var
  fsj:string;
begin
  if Key=#13 then
  begin
    if Trim(orderNo.Text)='' then Exit;
    if Length(Trim(orderNo.Text))<4 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('select A.MainId,MPRTCode=RTrim(A.MPRTCode)+RTrim(A.MPRTGY),A.MPRTCodeName,A.OrderNo,A.SGJSSH from JYOrder_Main A where ');
        sql.Add(' A.orderno like '''+'%'+Trim(orderNo.Text)+'%'+'''');
        Open;
      end;
      SCreateCDS20(ADOQueryMain,ClientDataSet0);
      SInitCDSData20(ADOQueryMain,ClientDataSet0);
    finally
      ADOQueryMain.EnableControls;
    end;

  end;

end;

procedure TfrmOrdCostQtyList.TBExportClick(Sender: TObject);
begin
  if ClientDataSet3.IsEmpty then Exit;
end;

procedure TfrmOrdCostQtyList.FormShow(Sender: TObject);
begin
  READCxGrid('订单列表Qty',Tv1,'订单核算');
  READCxGrid('正常码单Qty',Tv2,'订单核算');
  READCxGrid('调节仓库Qty',Tv3,'订单核算');
  READCxGrid('其他费用Qty',Tv7,'订单核算');
  READCxGrid('订单号Qty',Tv7,'订单核算');
end;

procedure TfrmOrdCostQtyList.Tv1CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var
  fsj,MDMoney,OrdCC,ZZFty,QQFee,FHZQty:string;
begin
  if ClientDataSet1.IsEmpty then Exit;
  with  ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.PRTHX,B.PRTColorNo ');
    sql.Add('from JYOrder_Main_MD A');
    sql.Add(' left join JYOrder_Sub B on A.ORDSubId=B.SubId');
    sql.Add(' where A.ORDMainId='''+Trim(ClientDataSet1.fieldbyname('ORDMainId').AsString)+'''');
    sql.Add(' and A.FactoryNo='''+Trim(ClientDataSet1.fieldbyname('FactoryNo').AsString)+'''');
    sql.Add(' and A.ConSubId='''+Trim(ClientDataSet1.fieldbyname('ConSubId').AsString)+'''');
    sql.Add(' and isnull(A.CKName,'''')='''' ');
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet2);
  SInitCDSData20(ADOQuery1,ClientDataSet2);
  {ClientDataSet5.Locate('PSName','检验包装',[]);
  FHZQty:=Trim(ClientDataSet5.fieldbyname('SJQty').AsString);
  if FHZQty='' then FHZQty:='0';
  MDMoney:=Tv1.DataController.Summary.FooterSummaryTexts[2];
  if Trim(MDMoney)='' then MDMoney:='0';
  OrdCC:=Tv3.DataController.Summary.FooterSummaryTexts[0];
  if Trim(OrdCC)='' then OrdCC:='0';
  ZZFty:=Tv3.DataController.Summary.FooterSummaryTexts[4];
  if Trim(ZZFty)='' then ZZFty:='0';
  QQFee:=Tv7.DataController.Summary.FooterSummaryTexts[2];
  if Trim(QQFee)='' then QQFee:='0';
  OrdCost.Visible:=True;
  QTFee.Visible:=True;
  HZQty.Visible:=True;
  OrdCost.Caption:=FloatToStr(StrToFloat(MDMoney)-StrToFloat(OrdCC)-StrToFloat(ZZFty));
  QTFee.Caption:=QQFee;
  HZQty.Caption:=FHZQty;
  if StrToFloat(FHZQty)>0 then
  begin

    AVGPrice.Caption:=FloatToStr((StrToFloat(OrdCost.Caption)+StrToFloat(QTFee.Caption))*1.00/StrToFloat(FHZQty));
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select AA=Cast(:AVGPrice as decimal(18,2))');
      Parameters.ParamByName('AVGPrice').Value:=AVGPrice.Caption;
      Open;
    end;

    AVGPrice.Caption:=ADOQueryTemp.fieldbyname('AA').AsString;
    AVGPrice.Visible:=True;
  end;   }
end;
procedure TfrmOrdCostQtyList.InitSubGrid();
begin
  with  ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.PRTHX,B.PRTColorNo, ');
    sql.Add('KKZZR=(select sum(ZZPonMoney) from JYOrder_Main_MD_KK KK where KK.MDID=A.MDID and KK.KKTYpe=''结果'' ),');
    sql.Add('KKZZC=(select sum(ZZFtyMoney) from JYOrder_Main_MD_KK KK where KK.MDID=A.MDID and KK.KKTYpe=''结果'' )');
    sql.Add('from JYOrder_Main_MD A');
    sql.Add(' left join JYOrder_Sub B on A.ORDSubId=B.SubId');
    sql.Add(' where A.ORDMainId='''+Trim(ClientDataSet0.fieldbyname('MainId').AsString)+'''');
    sql.Add(' and isnull(A.CKName,'''')<>'''' ');
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet3);
  SInitCDSData20(ADOQuery1,ClientDataSet3);
  with  ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add(' exec P_View_PSNameSH :MainId');
    Parameters.ParamByName('MainId').Value:=Trim(ClientDataSet0.fieldbyname('MainId').AsString);
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet5);
  SInitCDSData20(ADOQuery1,ClientDataSet5);
  CZZSH.Visible:=True;
  CZZSH.Caption:=Trim(ClientDataSet5.fieldbyname('HZSH').AsString)+'%';
  HZSHQty.Visible:=True;
  HZSHQty.Caption:=Trim(ClientDataSet5.fieldbyname('HZSHQty').AsString)+'KG';
  with  ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from JYOrder_Main_Fee where MainId='''+Trim(ClientDataSet0.fieldbyname('MainId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet7);
  SInitCDSData20(ADOQuery1,ClientDataSet7);
end;

procedure TfrmOrdCostQtyList.v5Column1PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
    mvalue:='0';
  with ClientDataSet5 do
  begin
    Edit;
    FieldByName('PSSJSH').Value:=mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Process Set PSSJSH='+Trim(mvalue));
    sql.Add(' where PSID='''+Trim(ClientDataSet5.fieldbyname('PSID').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmOrdCostQtyList.v5Column5PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  with ClientDataSet5 do
  begin
    Edit;
    FieldByName('PSSJNote').Value:=mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Process Set PSSJNote='''+Trim(mvalue)+'''');
    sql.Add(' where PSID='''+Trim(ClientDataSet5.fieldbyname('PSID').AsString)+'''');
    ExecSQL;
  end;
end;

procedure TfrmOrdCostQtyList.v5Column2PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FQty,FMoney:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
    mvalue:='0';
  with ClientDataSet5 do
  begin
    Edit;
    FieldByName('PSSJQty').Value:=mvalue;
    Post;
  end;
  FQty:=ClientDataSet5.fieldbyname('PSSJQty').AsString;
  FMoney:=ClientDataSet5.fieldbyname('PSSJMoney').AsString;
  if Trim(FMoney)='' then
    FMoney:='0';
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Process Set PSSJQty='+Trim(FQty));
    sql.Add(' where PSID='''+Trim(ClientDataSet5.fieldbyname('PSID').AsString)+'''');
    ExecSQL;
  end;
  if ClientDataSet5.fieldbyname('PSSJQty').Value<>0 then
  begin
    with ClientDataSet5 do
    begin
      Edit;
      FieldByName('PSSJPrice').Value:=StrToFloat(FMoney)/ClientDataSet5.fieldbyname('PSSJQty').Value;
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_Process Set PSSJPrice='+Trim(ClientDataSet5.fieldbyname('PSSJPrice').AsString));
      sql.Add(' where PSID='''+Trim(ClientDataSet5.fieldbyname('PSID').AsString)+'''');
      ExecSQL;
    end;
  end;

end;

procedure TfrmOrdCostQtyList.v5Column3PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FQty,FMoney:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
    mvalue:='0';
  with ClientDataSet5 do
  begin
    Edit;
    FieldByName('PSSJMoney').Value:=mvalue;
    Post;
  end;
  FQty:=ClientDataSet5.fieldbyname('PSSJQty').AsString;
  FMoney:=ClientDataSet5.fieldbyname('PSSJMoney').AsString;
  if Trim(FQty)='' then
    FQty:='0';
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Process Set PSSJMoney='+Trim(FMoney));
    sql.Add(' where PSID='''+Trim(ClientDataSet5.fieldbyname('PSID').AsString)+'''');
    ExecSQL;
  end;
  if ClientDataSet5.fieldbyname('PSSJQty').Value<>0 then
  begin
    with ClientDataSet5 do
    begin
      Edit;
      FieldByName('PSSJPrice').Value:=StrToFloat(FMoney)/strtofloat(FQty);
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update JYOrder_Process Set PSSJPrice='+Trim(ClientDataSet5.fieldbyname('PSSJPrice').AsString));
      sql.Add(' where PSID='''+Trim(ClientDataSet5.fieldbyname('PSID').AsString)+'''');
      ExecSQL;
    end;
  end;

end;

procedure TfrmOrdCostQtyList.v5Column4PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
    mvalue:='0';
  with ClientDataSet5 do
  begin
    Edit;
    FieldByName('PSSJPrice').Value:=mvalue;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update JYOrder_Process Set PSSJPrice='+Trim(mvalue));
    sql.Add(' where PSID='''+Trim(ClientDataSet5.fieldbyname('PSID').AsString)+'''');
    ExecSQL;
  end;
end;


procedure TfrmOrdCostQtyList.ToolButton1Click(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 TfrmOrdCostQtyList.Tv0CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var
  fsj,MDMoney,OrdCC,ZZFty,QQFee,FHZQty:string;
begin
  if ClientDataSet0.IsEmpty then Exit;
  SGJSSH.Caption:=Trim(ClientDataSet0.fieldbyname('SGJSSH').AsString)+'%';
  SGJSSH.Visible:=True;
  try
    ADOQuery3.DisableControls;
    with ADOQuery3 do
    begin
      Close;
      sql.Clear;
      SQL.Add('exec P_View_OrderCostMainId :MainId');
      Parameters.ParamByName('MainId').Value:=Trim(ClientDataSet0.fieldbyname('MainId').AsString);
      Open;
    end;
    SCreateCDS20(ADOQuery3,ClientDataSet1);
    SInitCDSData20(ADOQuery3,ClientDataSet1);
  finally
    ADOQuery3.EnableControls;
  end;
  with  ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.*,B.PRTHX,B.PRTColorNo ');
    sql.Add('from JYOrder_Main_MD A');
    sql.Add(' left join JYOrder_Sub B on A.ORDSubId=B.SubId');
    sql.Add(' where 1<>1');
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet2);
  SInitCDSData20(ADOQuery1,ClientDataSet2);
  InitSubGrid();
  ClientDataSet5.Locate('PSName','检验包装',[]);
  FHZQty:=Trim(ClientDataSet5.fieldbyname('SJQty').AsString);
  if FHZQty='' then FHZQty:='0';
  MDMoney:=Tv1.DataController.Summary.FooterSummaryTexts[2];
  if Trim(MDMoney)='' then MDMoney:='0';
  OrdCC:=Tv3.DataController.Summary.FooterSummaryTexts[0];
  if Trim(OrdCC)='' then OrdCC:='0';
  ZZFty:=Tv3.DataController.Summary.FooterSummaryTexts[4];
  if Trim(ZZFty)='' then ZZFty:='0';
  QQFee:=Tv7.DataController.Summary.FooterSummaryTexts[2];
  if Trim(QQFee)='' then QQFee:='0';
  OrdCost.Visible:=True;
  QTFee.Visible:=True;
  HZQty.Visible:=True;
  OrdCost.Caption:=FloatToStr(StrToFloat(MDMoney)-StrToFloat(OrdCC)-StrToFloat(ZZFty));
  QTFee.Caption:=QQFee;
  HZQty.Caption:=FHZQty;
  if StrToFloat(FHZQty)>0 then
  begin

    AVGPrice.Caption:=FloatToStr((StrToFloat(OrdCost.Caption)+StrToFloat(QTFee.Caption))*1.00/StrToFloat(FHZQty));
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select AA=Cast(:AVGPrice as decimal(18,2))');
      Parameters.ParamByName('AVGPrice').Value:=AVGPrice.Caption;
      Open;
    end;

    AVGPrice.Caption:=ADOQueryTemp.fieldbyname('AA').AsString;
    AVGPrice.Visible:=True;
  end else
  begin
    AVGPrice.Caption:='';
  end;
  if ClientDataSet1.IsEmpty then
  begin
    AVGPrice.Caption:='';
  end;
end;

end.
