unit U_BPTPList;

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, cxCalendar, cxButtonEdit, cxSplitter,
  RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System, RM_GridReport,
  cxTextEdit, cxDropDownEdit, cxCheckBox;

type
  TfrmBPTPList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    BPCode: TEdit;
    Label5: TLabel;
    BPName: TEdit;
    Order_Main: TClientDataSet;
    Label4: TLabel;
    BPSpec: TEdit;
    DataSource2: TDataSource;
    ClientDataSet2: TClientDataSet;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2ComeDate: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2MxQty: TcxGridDBColumn;
    v2MxNote: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    ToolBar2: TToolBar;
    ToolButton8: TToolButton;
    ToolButton9: TToolButton;
    ToolButton10: TToolButton;
    cxGridPopupMenu2: TcxGridPopupMenu;
    v2Column1: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v1OrderNo: TcxGridDBColumn;
    v1PSName: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1GDPerson: TcxGridDBColumn;
    v1FactoryName: TcxGridDBColumn;
    v1ToFactoryName: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    CheckBox1: TCheckBox;
    v2Column4: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    v2Column11: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v2Column12: TcxGridDBColumn;
    v2Column13: TcxGridDBColumn;
    v2Column14: TcxGridDBColumn;
    v2Column15: TcxGridDBColumn;
    v2Column16: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v2Column17: TcxGridDBColumn;
    v2Column18: TcxGridDBColumn;
    v2Column19: TcxGridDBColumn;
    v2Column20: TcxGridDBColumn;
    v2Column21: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v2Column22: TcxGridDBColumn;
    Label6: TLabel;
    GangNoHZ: TEdit;
    Label7: TLabel;
    Taitou: TEdit;
    v1Column16: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v2Column23: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    Label2: TLabel;
    JHBatchNo: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure BPCodeChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure BPCodeKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton10Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure ToolButton9Click(Sender: TObject);
    procedure v2Column5PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column8PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column1PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column21PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure v1Column14CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
  private
    FInt,PFInt:Integer;
    procedure InitGrid();
    procedure InitForm();
    procedure InitSubGrid();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmBPTPList: TfrmBPTPList;

implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun;

{$R *.dfm}

procedure TfrmBPTPList.FormDestroy(Sender: TObject);
begin
  frmBPTPList:=nil;
end;

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

procedure TfrmBPTPList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('投坯汇总GY',Tv1,'跟单管理');
  WriteCxGrid('投坯明细GY',Tv2,'跟单管理');
end;

procedure TfrmBPTPList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select A.*  ');
      sql.Add(' ,CPZGKgQty=(select Top 1 CPZGKgQty from XH_Cloth_TP B where B.JHID=A.JHID)');
      sql.Add(' ,CPZGKgQtyBZ=(select Top 1 CPZGKgQtyBZ from XH_Cloth_TP B where B.JHID=A.JHID)');
      sql.Add('from XH_Cloth_JH A where BegFlag=1 and EndFlag=0');
      if CheckBox1.Checked=True then
      begin
        SQL.Add(' and JHDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
        SQL.Add(' and JHDate<'''+Trim(FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1))+'''');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmBPTPList.InitForm();
begin
  ReadCxGrid('投坯汇总GY',Tv1,'跟单管理');
  ReadCxGrid('投坯明细GY',Tv2,'跟单管理');
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-7;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  InitGrid();
end;

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

procedure TfrmBPTPList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

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

procedure TfrmBPTPList.FormShow(Sender: TObject);
begin
  InitForm();
end;
procedure TfrmBPTPList.BPCodeKeyPress(Sender: TObject;
  var Key: Char);
begin
  if Key=#13 then
  begin

    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('select * from XH_Cloth_JH where BegFlag=1 and EndFlag=0 ');
        if CheckBox1.Checked=True then
        begin
          SQL.Add(' and JHDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
          SQL.Add(' and JHDate>='''+Trim(FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1))+'''');
        end;
        sql.Add(' and BPCode like '''+'%'+Trim(BPCode.Text)+'%'+''' ' );
          Open;
        end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
    InitSubGrid();
  end;

end;
procedure TfrmBPTPList.InitSubGrid();
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.*');
    sql.Add(',TPJGSHQty=isnull(WCQty,0)-isnull(TPQty,0)-isnull(TPZGHSHZ,0)');//+isnull(WCQtyFP,0)
    sql.Add('from XH_Cloth_TP A');
    sql.Add(' where A.JHId='''+Trim(Order_Main.fieldbyname('JHId').AsString)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet2);
  SInitCDSData20(ADOQueryTemp,ClientDataSet2);
end;
procedure TfrmBPTPList.ToolButton10Click(Sender: TObject);
var
  maxno,MXMaxno,FMXID:string;
begin
  if ClientDataSet2.IsEmpty then Exit;
  if Trim(Order_Main.fieldbyname('JHID').AsString)='' then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('TPType',null,[]) then
  begin
    Application.MessageBox('类型不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('TPDate',null,[]) then
  begin
    Application.MessageBox('投坯日期不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('TPPS',null,[]) then
  begin
    Application.MessageBox('投坯匹数不能为空！','提示',0);
    Exit;
  end;
  if ClientDataSet2.Locate('TPQty',null,[]) then
  begin
    Application.MessageBox('投坯数量不能为空！','提示',0);
    Exit;
  end;
  BegDate.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    //保存明细表
    ClientDataSet2.DisableControls;
    with ClientDataSet2 do
    begin
      First;
      while not eof do
      begin
        with ADOQueryTemp do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from XH_Cloth_TP where TPID='''+Trim(ClientDataSet2.fieldbyname('TPID').AsString)+'''');
          Open;
        end;
        FMXID:=Trim(ADOQueryTemp.fieldbyname('TPID').AsString);
        if Trim(FMXID)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'TP','XH_Cloth_TP',4,1)=False then
          begin
            ClientDataSet2.EnableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取明细流水号失败！','提示',0);
            Exit;
          end;
        end  else
        begin
          maxno:=Trim(FMXID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from XH_Cloth_TP where TPID='''+Trim(FMXID)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(FMXID)='' then
          begin
            Append;
            FieldByName('Filler').Value:=Trim(DName);
            FieldByName('FillerCode').Value:=Trim(DCode);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditerCode').Value:=Trim(DCode);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          end;
          FieldByName('TPID').Value:=Trim(maxno);
          RTSetSaveDataCDS(ADOQueryCmd,Tv2,ClientDataSet2,'XH_Cloth_TP',2);
          FieldByName('JHId').Value:=Trim(Order_Main.fieldbyname('JHId').AsString);
          FieldByName('FromFactoryNo').Value:=Trim(Order_Main.fieldbyname('FactoryNo').AsString);
          FieldByName('FromFactoryName').Value:=Trim(Order_Main.fieldbyname('FactoryName').AsString);
          FieldByName('DQFactoryNo').Value:=Trim(Order_Main.fieldbyname('ToFactoryNo').AsString);
          FieldByName('DQFactoryName').Value:=Trim(Order_Main.fieldbyname('ToFactoryName').AsString);
          if ClientDataSet2.FieldByName('WCFlag').AsBoolean=True then
          begin
            FieldByName('WCFlag').Value:=1;
          end else
          begin
            FieldByName('WCFlag').Value:=0;
          end;
          if ClientDataSet2.FieldByName('FHFlag').AsBoolean=True then
          begin
            FieldByName('FHFlag').Value:=1;
          end else
          begin
            FieldByName('FHFlag').Value:=0;
          end;
          //FieldByName('GDCode').Value:=Trim(ClientDataSet2.fieldbyname('GDCode').AsString);
         // FieldByName('GDName').Value:=Trim(ClientDataSet2.fieldbyname('GDName').AsString);
          Post;
        end;
       { with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from XH_Cloth_TP where isnull(GangNo,'''')='''+Trim(ClientDataSet2.fieldbyname('GangNo').AsString)+'''');
          Open;
        end;
        if ADOQueryTemp.RecordCount>1 then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          ClientDataSet2.EnableControls;
          Application.MessageBox(PChar('缸号：'+Trim(ClientDataSet2.fieldbyname('GangNo').AsString)+' 重复录入，不允许保存!'),'提示',0);
          Exit;

        end; }
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('exec P_Update_JHTP :JHID');
          Parameters.ParamByName('JHID').Value:=Trim(Order_Main.fieldbyname('JHID').AsString);
          ExecSQL;
        end;
        with ClientDataSet2 do
        begin
          Edit;
          FieldByName('TPID').Value:=Trim(maxno);
          Post;
        end;
        Next;
      end;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from XH_Cloth_JH where JHID='''+Trim(Order_Main.fieldbyname('JHID').AsString)+'''');
      sql.Add(' and isnull(KCPSHZ,0)<0');
      Open;
    end;
    if ADOQueryTemp.IsEmpty=False then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      ClientDataSet2.EnableControls;
      Application.MessageBox('投坯数大于到货数不能保存数据!','提示',0);
      Exit;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    ClientDataSet2.EnableControls;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from XH_Cloth_JH where JHID='''+Trim(Order_Main.fieldbyname('JHID').AsString)+'''');
      Open;
    end;
    with Order_Main do
    begin
      Edit;
      FieldByName('WCPSHZ').Value:=ADOQueryTemp.fieldbyname('WCPSHZ').Value;
      FieldByName('WCQtyHZ').Value:=ADOQueryTemp.fieldbyname('WCQtyHZ').Value;
      FieldByName('FHPSHZ').Value:=ADOQueryTemp.fieldbyname('FHPSHZ').Value;
      FieldByName('FHQtyHZ').Value:=ADOQueryTemp.fieldbyname('FHQtyHZ').Value;
      FieldByName('TPPSHZ').Value:=ADOQueryTemp.fieldbyname('TPPSHZ').Value;
      FieldByName('TPQtyHZ').Value:=ADOQueryTemp.fieldbyname('TPQtyHZ').Value;
      Post;
    end;
    Application.MessageBox('操作成功！','提示',0);
  except
    ClientDataSet2.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常！','提示',0);
  end;
end;

procedure TfrmBPTPList.ToolButton8Click(Sender: TObject);
var
  TPPS,TPQty,colorName,FS,GangNo,MF,KZ,CPZGKgQty,CPZGKgQtyBZ,TPDate:String;
begin
  if Order_Main.IsEmpty then Exit;
  if Trim(Order_Main.fieldbyname('JHID').AsString)='' then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if ClientDataSet2.IsEmpty=False then
  begin
    FS:='1';
    TPPS:=Trim(ClientDataSet2.fieldbyname('TPPS').AsString);
    TPQty:=Trim(ClientDataSet2.fieldbyname('TPQty').AsString);
    colorName:=Trim(ClientDataSet2.fieldbyname('colorName').AsString);
    GangNo:=Trim(ClientDataSet2.fieldbyname('GangNo').AsString);
    TPDate:=Trim(ClientDataSet2.fieldbyname('TPDate').AsString);
    MF:=Trim(ClientDataSet2.fieldbyname('MF').AsString);
    KZ:=Trim(ClientDataSet2.fieldbyname('KZ').AsString);
    CPZGKgQty:=ClientDataSet2.fieldbyname('CPZGKgQty').AsString;
    CPZGKgQtyBZ:=ClientDataSet2.fieldbyname('CPZGKgQtyBZ').AsString;
  end else
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.* from XH_Cloth_TP A inner join XH_Cloth_JH B on A.JHID=B.JHID ');
      sql.Add(' and B.BPCode='''+Trim(Order_Main.fieldbyname('BPCode').AsString)+'''');
      sql.Add(' and isnull(CPZGKgQty,-999)<>-9999 order by A.TPID desc');
      Open;
    end;
    if ADOQueryTemp.IsEmpty=False then
    begin
      CPZGKgQty:=ADOQueryTemp.fieldbyname('CPZGKgQty').AsString;
      CPZGKgQtyBZ:=ADOQueryTemp.fieldbyname('CPZGKgQtyBZ').AsString;
    end else
    begin
      CPZGKgQty:='';CPZGKgQtyBZ:='';
    end;
  end;
  ClientDataSet2.Append;
  ClientDataSet2.FieldByName('TPUnit').Value:=Trim(Order_Main.fieldbyname('JHUnit').AsString);
  ClientDataSet2.FieldByName('TPType').Value:='正常';
  if Trim(CPZGKgQty)<>'' then
    ClientDataSet2.FieldByName('CPZGKgQty').Value:=CPZGKgQty;
  if Trim(CPZGKgQtyBZ)<>'' then
    ClientDataSet2.FieldByName('CPZGKgQtyBZ').Value:=CPZGKgQtyBZ;
  if FS='1' then
  begin
    ClientDataSet2.FieldByName('TPPS').Value:=TPPS;
    ClientDataSet2.FieldByName('TPQty').Value:=TPQty;
    ClientDataSet2.FieldByName('colorName').Value:=colorName;
    if Length(GangNo)>8 then
    begin
      ClientDataSet2.FieldByName('GangNo').Value:=Copy(GangNo,1,Length(GangNo)-2);
    end else
    begin
      ClientDataSet2.FieldByName('GangNo').Value:=GangNo;
    end;

    ClientDataSet2.FieldByName('MF').Value:=MF;
    ClientDataSet2.FieldByName('KZ').Value:=KZ;
  end;
  if Trim(TPDate)='' then
  begin
    ClientDataSet2.FieldByName('TPDate').Value:=SGetServerDate(ADOQueryTemp);
  end else
  begin
    ClientDataSet2.FieldByName('TPDate').Value:=TPDate;
  end;
  ClientDataSet2.Post;
end;

procedure TfrmBPTPList.ToolButton9Click(Sender: TObject);
begin
  if ClientDataSet2.IsEmpty then Exit;
  if Trim(ClientDataSet2.fieldbyname('TPID').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete XH_Cloth_TP where TPId='''+Trim(ClientDataSet2.fieldbyname('TPId').AsString)+'''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('exec P_Update_JHTP :JHID');
        Parameters.ParamByName('JHID').Value:=Trim(Order_Main.fieldbyname('JHID').AsString);
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!','提示',0);
    end;
  end;
  ClientDataSet2.Delete;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from XH_Cloth_JH where JHID='''+Trim(Order_Main.fieldbyname('JHID').AsString)+'''');
    Open;
  end;
  with Order_Main do
  begin
    Edit;
    FieldByName('WCPSHZ').Value:=ADOQueryTemp.fieldbyname('WCPSHZ').Value;
    FieldByName('WCQtyHZ').Value:=ADOQueryTemp.fieldbyname('WCQtyHZ').Value;
    FieldByName('FHPSHZ').Value:=ADOQueryTemp.fieldbyname('FHPSHZ').Value;
    FieldByName('FHQtyHZ').Value:=ADOQueryTemp.fieldbyname('FHQtyHZ').Value;
    FieldByName('TPPSHZ').Value:=ADOQueryTemp.fieldbyname('TPPSHZ').Value;
    FieldByName('TPQtyHZ').Value:=ADOQueryTemp.fieldbyname('TPQtyHZ').Value;
    Post;
  end;
end;

procedure TfrmBPTPList.v2Column5PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:Boolean;
begin
  mvalue:=TcxCheckBox(Sender).EditingValue;
  if mvalue=True then
  begin
    with ClientDataSet2 do
    begin
      Edit;
      FieldByName('WCFlag').Value:=True;
      FieldByName('WCDate').Value:=SGetServerDate(ADOQueryTemp);
      FieldByName('WCPS').Value:=FieldByName('TPPS').Value;
      FieldByName('WCQty').Value:=FieldByName('TPQty').Value;
    end;
  end else
  begin
    with ClientDataSet2 do
    begin
      Edit;
      FieldByName('WCFlag').Value:=False;
      FieldByName('WCDate').Value:=Null;
      FieldByName('WCPS').Value:=Null;
      FieldByName('WCQty').Value:=Null;
      FieldByName('WCPSFP').Value:=Null;
      FieldByName('WCQtyFP').Value:=Null;
    end;
  end;
end;

procedure TfrmBPTPList.v2Column8PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:Boolean;
begin
  mvalue:=TcxCheckBox(Sender).EditingValue;
  if mvalue=True then
  begin
    with ClientDataSet2 do
    begin
      Edit;
      FieldByName('FHFlag').Value:=True;
      FieldByName('FHDate').Value:=SGetServerDate(ADOQueryTemp);
      FieldByName('FHPS').Value:=FieldByName('WCPS').Value;
      FieldByName('FHQty').Value:=FieldByName('WCQty').Value;
      FieldByName('FHPSFP').Value:=FieldByName('WCPSFP').Value;
      FieldByName('FHQtyFP').Value:=FieldByName('WCQtyFP').Value;
    end;
  end else
  begin
    with ClientDataSet2 do
    begin
      Edit;
      FieldByName('FHFlag').Value:=False;
      FieldByName('FHDate').Value:=Null;
      FieldByName('FHPS').Value:=Null;
      FieldByName('FHQty').Value:=Null;
      FieldByName('FHPSFP').Value:=Null;
      FieldByName('FHQtyFP').Value:=Null;
    end;
  end;
end;

procedure TfrmBPTPList.v2Column1PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:string;
  FPS,FQty,QtyAvg,QtyAvgFP:Double;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    FPS:=0;
  end else
  begin
    FPS:=StrToFloat(mvalue);
  end;
  if Trim(Order_Main.fieldbyname('QtyAvg').AsString)<>'' then
  begin
    QtyAvg:=Order_Main.fieldbyname('QtyAvg').Value;
  end else
  begin
    QtyAvg:=0;
  end;
  if Trim(Order_Main.fieldbyname('QtyAvgFP').AsString)<>'' then
  begin
    QtyAvgFP:=Order_Main.fieldbyname('QtyAvgFP').Value;
  end else
  begin
    QtyAvgFP:=0;
  end;

  //QtyAvgFP:=Order_Main.fieldbyname('QtyAvgFP').Value;
  with ClientDataSet2 do
  begin
    Edit;
    if Trim(ClientDataSet2.fieldbyname('TPType').AsString)='正常' then
    begin
      FieldByName('TPQty').Value:=FPS*QtyAvg ;
    end else
    begin
      FieldByName('TPQty').Value:=FPS*QtyAvgFP;
    end;
    FieldByName('TPPS').Value:=FPS;
    if FPS=0 then
    begin
      FieldByName('TPPS').Value:=null;
      FieldByName('TPQty').Value:=null;
    end;

  end;
end;

procedure TfrmBPTPList.v2Column21PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,TPType:string;
  FPS,FQty,QtyAvg,QtyAvgFP:Double;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    TPType:='';
  end else
  begin
    TPType:=Trim(mvalue);
  end;
  with ClientDataSet2 do
  begin
    Edit;
    FieldByName('TPType').Value:=TPType;
    Post;
  end;
  if Trim(ClientDataSet2.fieldbyname('TPPS').AsString)<>'' then
  begin
    FPS:=ClientDataSet2.fieldbyname('TPPS').Value;
  end else
  begin
    FPS:=0;
    Exit;
  end;
  if Trim(Order_Main.fieldbyname('QtyAvg').AsString)<>'' then
  begin
    QtyAvg:=Order_Main.fieldbyname('QtyAvg').Value;
  end else
  begin
    QtyAvg:=0;
  end;
  if Trim(Order_Main.fieldbyname('QtyAvgFP').AsString)<>'' then
  begin
    QtyAvgFP:=Order_Main.fieldbyname('QtyAvgFP').Value;
  end else
  begin
    QtyAvgFP:=0;
  end;
  with ClientDataSet2 do
  begin
    Edit;
    if Trim(TPType)='正常' then
    begin
      FieldByName('TPQty').Value:=FPS*QtyAvg ;
    end else
    begin
      FieldByName('TPQty').Value:=FPS*QtyAvgFP;
    end;
    FieldByName('TPPS').Value:=FPS;
    if FPS=0 then
    begin
      FieldByName('TPPS').Value:=null;
      FieldByName('TPQty').Value:=null;
    end;

  end;
end;

procedure TfrmBPTPList.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  TcxGridToExcel(Self.Caption,cxGrid1);
end;

procedure TfrmBPTPList.v1Column14CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  Id,id10:Integer;
begin
  Id:=TV1.GetColumnByFieldName('TPPSHZ').Index;
  id10:=TV1.GetColumnByFieldName('DHPSHZ').Index;
  if Id<0 then Exit;
  if AViewInfo.GridRecord.Values[id]=Null then Exit;
  if AViewInfo.GridRecord.Values[id10]=Null then Exit;
  if AViewInfo.GridRecord.Values[id]=AViewInfo.GridRecord.Values[id10] then
  begin
    ACanvas.Brush.Color:=$0040FF00;  //clGreen
  end;
end;

procedure TfrmBPTPList.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
InitSubGrid();
end;

end.
