unit U_MDChkFLZhengYiCLChk;

interface

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

type
  TfrmMDChkFLZhengYiCLChk = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    ToolButton2: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label2: TLabel;
    Label5: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    orderNo: TEdit;
    Panel2: TPanel;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column7: TcxGridDBColumn;
    v1GenDan: TcxGridDBColumn;
    v1SOrddefstr1: TcxGridDBColumn;
    V1OrderNo: TcxGridDBColumn;
    cxGridDBColumn28: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1FactoryName: TcxGridDBColumn;
    v1ToFactoryName: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v72Column1: TcxGridDBColumn;
    v72Column2: TcxGridDBColumn;
    v72Column3: TcxGridDBColumn;
    v72Column4: TcxGridDBColumn;
    v72Column5: TcxGridDBColumn;
    v72Column6: TcxGridDBColumn;
    v72Column7: TcxGridDBColumn;
    v72Column8: TcxGridDBColumn;
    v72Column9: TcxGridDBColumn;
    Label7: TLabel;
    FactoryName: TEdit;
    DataSource1: TDataSource;
    ClientDataSet1: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryMain: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQuery1: TADOQuery;
    v1Column1: TcxGridDBColumn;
    v1PSName: TcxGridDBColumn;
    cxTabControl2: TcxTabControl;
    ToolButton4: TToolButton;
    ToolButton7: TToolButton;
    ComboBox4: TComboBox;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Panel5: TPanel;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    v3Column20: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v3Column12: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    v3Column17: TcxGridDBColumn;
    v3Column18: TcxGridDBColumn;
    v3Column16: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    v3Column19: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    Tv3Column1: TcxGridDBColumn;
    v3Column24: TcxGridDBColumn;
    v3Column3: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v3Column13: TcxGridDBColumn;
    v3Column21: TcxGridDBColumn;
    v3Column22: TcxGridDBColumn;
    v3Column23: TcxGridDBColumn;
    v3Column4: TcxGridDBColumn;
    v3Column5: TcxGridDBColumn;
    Tv3Column2: TcxGridDBColumn;
    Tv3Column3: TcxGridDBColumn;
    v3Column6: TcxGridDBColumn;
    v3Column7: TcxGridDBColumn;
    v3Column8: TcxGridDBColumn;
    v3Column9: TcxGridDBColumn;
    v3Column10: TcxGridDBColumn;
    v3Column11: TcxGridDBColumn;
    v3Column15: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxSplitter2: TcxSplitter;
    DataSource3: TDataSource;
    ClientDataSet3: TClientDataSet;
    cxGridPopupMenu3: TcxGridPopupMenu;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton7Click(Sender: TObject);
    procedure cxTabControl2Change(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure orderNoChange(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure v1Column9PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
  private
    { Private declarations }
    FMHId:String;
    procedure InitGrid();
  public
    { Public declarations }
  end;

var
  frmMDChkFLZhengYiCLChk: TfrmMDChkFLZhengYiCLChk;

implementation
uses
  U_DataLink,U_RTFun, U_GYSList,U_ZDYHelp, U_ModuleNote;

{$R *.dfm}

procedure TfrmMDChkFLZhengYiCLChk.FormDestroy(Sender: TObject);
begin
  frmMDChkFLZhengYiCLChk:=nil;
end;

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

procedure TfrmMDChkFLZhengYiCLChk.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('Tv1',Tv1,'争议码单审核');
  WriteCxGrid('Tv3',Tv3,'争议码单审核');
  close;
end;

procedure TfrmMDChkFLZhengYiCLChk.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Main_MD_HZ where 1=1 ');
      if cxTabControl2.TabIndex<>0 then
      begin
        if ComboBox4.ItemIndex=0 then
        begin
          sql.Add(' and MHDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
          sql.Add(' and MHDate<'''+Trim(FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1))+'''');
        end else
        if ComboBox4.ItemIndex=1 then
        begin
          sql.Add(' and FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
          sql.Add(' and FillTime<'''+Trim(FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1))+'''');
        end else
        if ComboBox4.ItemIndex=2 then
        begin
          sql.Add(' and YFDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
          sql.Add(' and YFDate<'''+Trim(FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1))+'''');
        end;
      end;
      sql.Add(' and isnull(ChkStatus,'''')<>'''' and isnull(ChkStatus,'''')<>''待处理'' ');
      if cxTabControl2.TabIndex=0 then
      begin
        sql.Add(' and isnull(ChkStatus,'''') in(''已处理'') ');
      end else
      if cxTabControl2.TabIndex=1 then
      begin
        sql.Add(' and isnull(ChkStatus,'''') in(''审核不通过'',''审核通过'')');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,ClientDataSet1);
    SInitCDSData20(ADOQueryMain,ClientDataSet1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;



procedure TfrmMDChkFLZhengYiCLChk.FormShow(Sender: TObject);
begin
  cxTabControl2.TabIndex:=0;
  EndDate.DateTime:=SGetServerDate(ADOQueryTemp);
  BegDate.Date:=EndDate.Date-30;
  ReadCxGrid('Tv1',Tv1,'争议码单审核');
  ReadCxGrid('Tv3',Tv3,'争议码单审核');
  InitGrid();
end;

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

procedure TfrmMDChkFLZhengYiCLChk.TBExportClick(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  TcxGridToExcel('争议码单处理',cxGrid1);
end;

procedure TfrmMDChkFLZhengYiCLChk.ToolButton2Click(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 TfrmMDChkFLZhengYiCLChk.ToolButton4Click(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  if cxTabControl2.TabIndex<>0 then Exit;
  BegDate.SetFocus;
  if ClientDataSet1.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    ClientDataSet1.DisableControls;
    with ClientDataSet1 do
    begin
      First;
      while ClientDataSet1.Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' Update JYOrder_Main_MD_HZ Set ChkTime=Getdate(),');
          SQL.Add(' Chker='''+Trim(DName)+''',ChkStatus=''审核通过'' ' );
          sql.Add(' where MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
          sql.Add(' Update YF_Money_CR Set Valid=''Y'' ');
          sql.Add(' ,Qty='+ClientDataSet1.fieldbyname('CLQty').AsString);
          sql.Add(' ,Price='+ClientDataSet1.fieldbyname('CLPrice').AsString);
          sql.Add(' ,Money='+ClientDataSet1.fieldbyname('CLMoney').AsString);
          sql.Add(' ,BBMoney='+ClientDataSet1.fieldbyname('CLMoney').AsString);
          sql.Add(' where MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
          sql.Add(' Update JYOrder_Main_MD_HZ Set Qty=CLQty,Price=CLPrice,Money=CLMoney');
          sql.Add(' where MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update YF_Money_KC Set ');
          sql.Add(' KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID and A.Valid=''Y'')');
          sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID and A.Valid=''Y'' )');
          sql.Add(' where CRID=(select CRID from YF_Money_CR AA where AA.MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+''')');
          ExecSQL;
        end;
        ClientDataSet1.Delete;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    ClientDataSet1.EnableControls;
  except
    ClientDataSet1.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

procedure TfrmMDChkFLZhengYiCLChk.ToolButton7Click(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  if cxTabControl2.TabIndex<>1 then Exit;
  BegDate.SetFocus;
  if ClientDataSet1.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    ClientDataSet1.DisableControls;
    with ClientDataSet1 do
    begin
      First;
      while ClientDataSet1.Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update JYOrder_Main_MD_HZ Set ChkTime=Null,');
          SQL.Add('Chker=Null,ChkStatus=''已处理'' ' );
          sql.Add(' where MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
          sql.Add(' Update YF_Money_CR Set Valid=''N'' ');
          sql.Add(' where MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update YF_Money_KC Set ');
          sql.Add(' KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID and A.Valid=''Y'')');
          sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID and A.Valid=''Y'' )');
          sql.Add(' where CRID=(select CRID from YF_Money_CR AA where AA.MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+''')');
          ExecSQL;
        end;
        ClientDataSet1.Delete;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    ClientDataSet1.EnableControls;
  except
    ClientDataSet1.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

procedure TfrmMDChkFLZhengYiCLChk.cxTabControl2Change(Sender: TObject);
begin
  InitGrid();
end;

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

procedure TfrmMDChkFLZhengYiCLChk.orderNoChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmMDChkFLZhengYiCLChk.FactoryNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmMDChkFLZhengYiCLChk.v1Column9PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue,FName,Qty,Price:string;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  FName:=Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName(FName).Value:=mvalue;
    Post;
  end;
  Qty:=Trim(ClientDataSet1.fieldbyname('CLQty').AsString);
  if Trim(Qty)='' then
  begin
    Qty:='0';
  end;
  Price:=Trim(ClientDataSet1.fieldbyname('CLPrice').AsString);
  if Trim(Price)='' then
  begin
    Price:='0';
  end;
  with ClientDataSet1 do
  begin
    Edit;
    FieldByName('CLMoney').Value:=StrToFloat(Qty)*StrToFloat(Price);
    Post;
  end;
end;

procedure TfrmMDChkFLZhengYiCLChk.ToolButton1Click(Sender: TObject);
begin
  if ClientDataSet1.IsEmpty then Exit;
  if cxTabControl2.TabIndex<>0 then Exit;
  BegDate.SetFocus;
  if ClientDataSet1.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    ClientDataSet1.DisableControls;
    with ClientDataSet1 do
    begin
      First;
      while ClientDataSet1.Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' Update JYOrder_Main_MD_HZ Set ChkTime=Getdate(),');
          SQL.Add(' Chker='''+Trim(DName)+''',ChkStatus=''审核不通过'' ' );
          sql.Add(' where MHID='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''');
          ExecSQL;
        end;
        ClientDataSet1.Delete;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    ClientDataSet1.EnableControls;
  except
    ClientDataSet1.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!','提示',0);
  end;
end;

procedure TfrmMDChkFLZhengYiCLChk.Tv1CellClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var
  fsj:String;
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_View_MDTJ_JS :begdate,:enddate,:orderno,:MDType,:WSql');
    Parameters.ParamByName('orderno').Value:=Trim(ClientDataSet1.fieldbyname('OrderNo').AsString);
    fsj:=' and isnull(MHID,'''')='''+Trim(ClientDataSet1.fieldbyname('MHID').AsString)+'''';
    Parameters.ParamByName('WSql').Value:=fsj;
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,ClientDataSet3);
  SInitCDSData20(ADOQueryTemp,ClientDataSet3);
end;


end.
