unit U_JFSCList;

interface

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

type
  TfrmJFSCList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Label5: TLabel;
    ZZPerson: TEdit;
    ToolButton1: TToolButton;
    cxTabControl1: TcxTabControl;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    v1Column2: TcxGridDBColumn;
    Label3: TLabel;
    Reason: TEdit;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure SPSpecChange(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure ZZPersonChange(Sender: TObject);
  private
    canshu1,canshu2:string;
    procedure InitGrid();
    procedure SetFrom();
    function  YFData():Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmJFSCList: TfrmJFSCList;

implementation
uses
  U_DataLink,U_Fun;

{$R *.dfm}
procedure TfrmJFSCList.SetFrom();
begin
  case cxTabControl1.TabIndex of
  0:begin
      ToolButton1.Visible:=true;
    end;
  1:begin
      ToolButton1.Visible:=false;
    end;
  end;
end;
function TfrmJFSCList.YFData():Boolean;
var
  MaxId:String;
begin
  Result:=False;
  if GetLSNo(ADOQueryCmd,MaxId,'GZ','DcgGz',4,1)=False then
  begin
    Result:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('取最大号失败！','提示',0);
    Exit;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from DcgGz where 1=2');
    Open;
  end;
  with ADOQueryCmd do
  begin
    Append;
    FieldByName('GZID').Value:=Trim(MaxId);
    FieldByName('Filler').Value:=Trim(DName);
    FieldByName('JYDate').Value:=formatdateTIme('yyyy-MM-dd',now());
    FieldByName('GZMoney').Value:=CDS_Main.FieldByName('AddMoney').AsFloat-CDS_Main.FieldByName('DelMoney').AsFloat;
    FieldByName('MFID').Value:=trim(CDS_Main.FieldByName('MFID').AsString);
    FieldByName('DName').Value:=trim(CDS_Main.FieldByName('ZZPerson').AsString);
    FieldByName('OrderNo').Value:=trim(CDS_Main.FieldByName('ConNo').AsString);
    FieldByName('MainID').Value:=trim(CDS_Main.FieldByName('MainID').AsString);
    FieldByName('SubId').Value:=trim(CDS_Main.FieldByName('SubId').AsString);
    FieldByName('GGNote').Value:=trim(CDS_Main.FieldByName('Reason').AsString);
    FieldByName('JLMoney').Value:=CDS_Main.FieldByName('AddMoney').Value;
    FieldByName('CFMoney').Value:=CDS_Main.FieldByName('DelMoney').Value;
    FieldByName('DCarNo').Value:=CDS_Main.FieldByName('CarNo').Value;
    FieldByName('APXH').Value:=CDS_Main.FieldByName('APXH').Value;
    Post;
  end;                          
  Result:=True;
end;
procedure TfrmJFSCList.FormDestroy(Sender: TObject);
begin
  frmJFSCList:=nil;
end;

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

procedure TfrmJFSCList.FormCreate(Sender: TObject);
begin
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime:=EndDate.DateTime-30;
  canshu1:=Trim(DParameters1);
  canshu2:=Trim(DParameters2);
end;

procedure TfrmJFSCList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add(' select A.*,B.Mainid,B.Subid,B.CarNo,B.APXH,C.ConNo from WFB_MJJY_JiangFa A ');
      sql.Add(' inner join WFB_MJJY_PB B on A.MJID=B.MJID ');
      sql.Add(' inner join Cloth_Main C on B.MainId=C.MainId ');
//      sql.Add(' inner join Cloth_Sub D on B.SubId=D.SubId ');
      sql.add(' where A.FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
      sql.Add(' and A.FillTime<='''+Trim(FormatDateTime('yyyy-MM-dd',enddate.DateTime+1))+'''');
      IF cxTabControl1.TabIndex=0 then
        SQL.Add(' and not exists(select MFID from DcgGz  X where X.MFID=A.MFID ) ')
      else
        SQL.Add(' and  exists(select MFID from DcgGz X where X.MFID=A.MFID )');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmJFSCList.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  InitGrid();
end;

procedure TfrmJFSCList.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  end;
end;

procedure TfrmJFSCList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption,Tv1,'坯布仓库');
  Close;
end;

procedure TfrmJFSCList.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption,Tv1,'坯布仓库');
  SetFrom();
  InitGrid();
end;

procedure TfrmJFSCList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then exit;
  TcxGridToExcel('坯布入库列表',cxGrid2);
end;

procedure TfrmJFSCList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmJFSCList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,True);
end;

procedure TfrmJFSCList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,False);
end;

procedure TfrmJFSCList.SPSpecChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

procedure TfrmJFSCList.ToolButton1Click(Sender: TObject);
begin
  IF CDS_Main.IsEmpty then exit;
  IF not CDS_Main.Locate('ssel',true,[]) then
  begin
    application.MessageBox('没有选择数据！','提示信息',0);
    exit;
  end;
  ADOQueryCmd.Connection.BeginTrans;
  try

    with CDS_Main do
    begin
      DisableControls;
      first;
      while not eof do
      begin
        IF fieldbyname('ssel').AsBoolean then
        begin
           IF not YFData() then
           begin
             ADOQueryCmd.Connection.RollbackTrans;
             application.MessageBox('生成扣款失败！','提示信息',0);
           end;
        end;
        next;
      end;
      first;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;

     application.MessageBox('生成扣款成功！','提示信息');
     initGrid();
  except
     ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('生成扣款失败！','提示信息',0);

  end;
end;

procedure TfrmJFSCList.cxTabControl1Change(Sender: TObject);
begin
  SetFrom();
  INITGrid();
end;

procedure TfrmJFSCList.Tv1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if button=mbright then
end;

procedure TfrmJFSCList.ZZPersonChange(Sender: TObject);
begin
  TBFind.Click;
end;

end.
