unit U_MRJCCRClist;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin, StdCtrls,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, cxCalendar, cxButtonEdit,
  cxTextEdit, cxDropDownEdit, RM_e_Xls, cxGridBandedTableView,
  cxGridDBBandedTableView, BtnEdit, cxPC, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator;

type
  TfrmMRJCCRClist = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    OrderNo: TEdit;
    cxGrid2: TcxGrid;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    Label5: TLabel;
    RMGridReport1: TRMGridReport;
    RMDBDataSet1: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    ADOQueryPrint: TADOQuery;
    Panetime: TPanel;
    TV1: TcxGridDBBandedTableView;
    V1Column1: TcxGridDBBandedColumn;
    V1Column7: TcxGridDBBandedColumn;
    V1Column9: TcxGridDBBandedColumn;
    V1Column12: TcxGridDBBandedColumn;
    V1Column15: TcxGridDBBandedColumn;
    V1Column3: TcxGridDBBandedColumn;
    V1Column11: TcxGridDBBandedColumn;
    V1Column13: TcxGridDBBandedColumn;
    V1Column14: TcxGridDBBandedColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    V1Column16: TcxGridDBBandedColumn;
    lbl1: TLabel;
    CPName: TEdit;
    cxgrdbndclmnV1Column17: TcxGridDBBandedColumn;
    V1Column2: TcxGridDBBandedColumn;
    V1Column10: TcxGridDBBandedColumn;
    DQFactoryName: TEdit;
    ToolButton2: TToolButton;
    tbKCDC: TToolButton;
    tbDCZL: TToolButton;
    Label7: TLabel;
    CheckBox1: TCheckBox;
    Panel3: TPanel;
    Button3: TButton;
    Button4: TButton;
    Label1: TLabel;
    edtBQKCPS: TEdit;
    Label3: TLabel;
    edtBQKCQty: TEdit;
    TV1Column1: TcxGridDBBandedColumn;
    Label4: TLabel;
    CPCODE: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure DQFactoryNameChange(Sender: TObject);
    procedure CPNameChange(Sender: TObject);
    procedure tbKCDCClick(Sender: TObject);
    procedure EnddateChange(Sender: TObject);
    procedure begdateChange(Sender: TObject);
    procedure tbDCZLClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    { Private declarations }
    procedure SetStatus();
    procedure InitGrid();
  public
    { Public declarations }
    RKFlag, FCYID, canshu1: string;
    fType: string;
  end;
//
//var
//  frmMRJCCRClist: TfrmMRJCCRClist;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_ZdyAttachGYS, U_KCDCList;

{$R *.dfm}
procedure TfrmMRJCCRClist.SetStatus();
begin
  tbKCDC.Visible := False;
  tbDCZL.Visible := False;
  if canshu1 = '管理' then
  begin
    tbKCDC.Visible := true;
    tbDCZL.Visible := true;
  end;
end;

procedure TfrmMRJCCRClist.InitGrid();
begin

  screen.Cursor := crsqlwait;
  Panetime.Visible := true;
  Panetime.Left := (Width - Panetime.Width) div 2;
  Panetime.top := (Height - Panetime.Height - 200) div 2;
  application.ProcessMessages;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      Filtered := False;
      sql.Add(' exec  P_View_JCCRClistNew ');
      sql.Add(' @begdate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', begdate.DateTime))));
      sql.Add(',@Enddate=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1))));
      if CheckBox1.Checked then
        sql.Add(',@KZFlag  =''为空''')
      else
        sql.Add(',@KZFlag  =''不为空''');
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    Panetime.Visible := false;
    screen.Cursor := crdefault;
  end;
end;

procedure TfrmMRJCCRClist.FormDestroy(Sender: TObject);
begin
//  frmYFMXlist:=nil;
end;

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

procedure TfrmMRJCCRClist.TBCloseClick(Sender: TObject);
begin
  WriteCxBandedGrid(trim(self.Caption), Tv1, '财务管理');
  Close;
end;

procedure TfrmMRJCCRClist.FormShow(Sender: TObject);
begin
  readCxBandedGrid(trim(self.Caption), Tv1, '财务管理');
  SetStatus();
  InitGrid();
end;

procedure TfrmMRJCCRClist.TBRafreshClick(Sender: TObject);
begin
  begdate.SetFocus;
  InitGrid();
end;

procedure TfrmMRJCCRClist.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid2);
end;

procedure TfrmMRJCCRClist.FormCreate(Sender: TObject);
begin
  cxGrid2.Align := AlClient;
  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
  begdate.DateTime := Enddate.DateTime;
end;

procedure TfrmMRJCCRClist.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  end;
end;

procedure TfrmMRJCCRClist.DQFactoryNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmMRJCCRClist.CPNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmMRJCCRClist.tbKCDCClick(Sender: TObject);
begin
  cxGrid2.Enabled := False;
  edtBQKCPS.Text := CDS_HZ.FieldByName('BQKCPS').AsString;
  edtBQKCQty.Text := CDS_HZ.FieldByName('BQKCQty').AsString;
  Panel3.Visible := True;
end;

procedure TfrmMRJCCRClist.EnddateChange(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmMRJCCRClist.begdateChange(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmMRJCCRClist.tbDCZLClick(Sender: TObject);
begin
  try
    frmKCDCList := TfrmKCDCList.Create(Application);
    with frmKCDCList do
    begin
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmKCDCList.Free;
  end;
end;

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

procedure TfrmMRJCCRClist.Button3Click(Sender: TObject);
var
  Maxno: string;
  FBQKCPS: Integer;
  FBQKCQty: Double;
begin

  if GetLSNo(ADOQueryCmd, Maxno, 'CX', 'XH_Cloth_DH', 3, 1) = False then
  begin
    Application.MessageBox('取库存对冲编号失败!', '提示', 0);
    Exit;
  end;

  FBQKCPS := StrToIntDef(edtBQKCPS.Text, 0);
  FBQKCQty := StrToFloatDef(edtBQKCQty.Text, 0);
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add(' insert into XH_Cloth_DH(OrderNo,DQFactoryName,CPName,cpcode,DHUnit,CRQtyFlag,DHPS,DHQty,DHType,DHDate,Filler,FillerCode,FillTime,DHID)  ');
    sql.Add(' values(' + quotedstr(Trim(CDS_HZ.FieldByName('OrderNo').AsString)));
    sql.Add(',' + quotedstr(Trim(CDS_HZ.FieldByName('DQFactoryName').AsString)));
    sql.Add(',' + quotedstr(Trim(CDS_HZ.FieldByName('CPName').AsString)));
    sql.Add(',' + quotedstr(Trim(CDS_HZ.FieldByName('CPcode').AsString)));
    sql.Add(',' + quotedstr(Trim(CDS_HZ.FieldByName('DHUnit').AsString)));
    sql.Add(',-1');
    sql.Add(',' + IntToStr(FBQKCPS));
    sql.Add(',' + FloatToStr(FBQKCQty));
    sql.Add(',''库存对冲'' ');
    sql.Add(',' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime))));
    sql.Add(',' + quotedstr(Trim(DName)));
    sql.Add(',' + quotedstr(Trim(DCode)));
    sql.Add(',getdate()');
    sql.Add(',' + quotedstr(Trim(Maxno)));
    sql.Add(')');
//    ShowMessage(SQL.Text);
    EXECSQL;
  end;
  Application.MessageBox('库存对冲成功!', '提示', 0);
  Panel3.Visible := False;
  cxGrid2.Enabled := True;
  InitGrid();
end;

procedure TfrmMRJCCRClist.Button4Click(Sender: TObject);
begin
  Panel3.Visible := False;
  cxGrid2.Enabled := True;
end;

end.

