unit U_DcgGzList;

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, cxCurrencyEdit, cxDropDownEdit;

type
  TfrmDcgGzList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    Label1: TLabel;
    Label2: TLabel;
    Label6: TLabel;
    Name: TEdit;
    OrderNo: TEdit;
    DCarNo: TEdit;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    V2Column1: TcxGridDBColumn;
    V2Column2: TcxGridDBColumn;
    V2Column3: TcxGridDBColumn;
    V2Column10: TcxGridDBColumn;
    enddate: TDateTimePicker;
    BegDate: TDateTimePicker;
    Label3: TLabel;
    Label4: TLabel;
    ToolButton3: TToolButton;
    V2Column8: TcxGridDBColumn;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolButton6: TToolButton;
    V2Column4: TcxGridDBColumn;
    V2Column6: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ZdyNameChange(Sender: TObject);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure TSselClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure V2Column6PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure V2Column8PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure V2Column2PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure V2Column3PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure ToolButton5Click(Sender: TObject);
    procedure V2Column11PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure V2Column1PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    function SaveData():Boolean;
  public

  end;

var
  frmDcgGzList: TfrmDcgGzList;

implementation
uses
  U_DataLink,U_Fun10,U_ZDYHelp,U_JFSCList;//U_ClothSCListSel

{$R *.dfm}
function TfrmDcgGzList.SaveData():Boolean;
var
  MaxId:String;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    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());
      Post;
    end;
    with CDS_HZ do
    begin
      Append;
      FieldByName('GZID').Value:=Trim(maxId);
      FieldByName('JYDate').Value:=formatdateTIme('yyyy-MM-dd',now());
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('增行失败!','提示',0);
  end;
end;
procedure TfrmDcgGzList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      Filtered:=False;
      SQL.Clear;
      sql.Add(' select *,DName Name from DcgGz where JYDate>='+quotedstr(Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))));
      sql.Add(' and JYDate< '+quotedstr(Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))));
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmDcgGzList.FormDestroy(Sender: TObject);
begin
  frmDcgGzList:=nil;
end;

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

procedure TfrmDcgGzList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.caption),Tv2,'挡车工工资');
  Close;
end;
procedure TfrmDcgGzList.FormShow(Sender: TObject);
begin
  ReadCxGrid(trim(self.caption),Tv2,'挡车工工资');

  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime:=EndDate.DateTime;
  InitGrid();
end;

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

procedure TfrmDcgGzList.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 TfrmDcgGzList.ZdyNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;


procedure TfrmDcgGzList.Tv2CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_HZ.IsEmpty then exit;
  ModalResult:=1;
end;

procedure TfrmDcgGzList.TSselClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then exit;
  ModalResult:=1;
end;

procedure TfrmDcgGzList.ToolButton1Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
   TcxGridToExcel('次布统计',cxGrid2);
end;

procedure TfrmDcgGzList.V2Column6PropertiesEditValueChanged(
  Sender: TObject);
var
  FFieldName,mvalue:String;
  FZSSJ,FZSJB1,FZSJB2:integer;
  FPTMoney,FGZMoney,FDcgPrice:double;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value:=mvalue;
    Post;
    FZSJB1:=FieldByName('ZSJB1').AsInteger;
    FZSJB2:=FieldByName('ZSJB2').AsInteger;
    FDcgPrice:=FieldByName('GJPrice').AsFloat;
  end;
  //产量工资=（交班总转数-接班总转数）/10000*工价
  FZSSJ:=FZSJB2-FZSJB1;
  FGZMoney:=(FZSJB2-FZSJB1)/10000* FDcgPrice;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update DcgGz set '+FFieldName+'='+mvalue);
    sql.Add(' , ZSSJ='+inttostr(FZSSJ));
    sql.Add(' , GZMoney='+floattostr(FGZMoney));
    sql.Add(' where GZID='+quotedstr(trim(Self.CDS_HZ.FieldByName('GZID').AsString)));
    EXECSQL;
  end;
  with CDS_HZ do
  begin
    Edit;
    FieldByName('ZSSJ').Value:=FZSSJ;
    FieldByName('GZMoney').Value:=FGZMoney;
    Post;
  end;
  tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmDcgGzList.ToolButton3Click(Sender: TObject);
var
 fPrintFile:String;
begin
  if CDS_HZ.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\挡车工工资.rmf' ;
  if FileExists(fPrintFile) then
  begin

    RM1.LoadFromFile(fPrintFile);
  //        RMVariables['QRBARCODE']:=fImagePath;
    RM1.ShowReport;
//    RM1.PrintReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\挡车工工资.rmf'),'提示',0);
    Exit;
  end;
end;

procedure TfrmDcgGzList.ToolButton4Click(Sender: TObject);
begin
  SaveData();
end;

procedure TfrmDcgGzList.V2Column8PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
//  try
//    frmClothSCListSel:=TfrmClothSCListSel.Create(Application);
//    with frmClothSCListSel do
//    begin
//      if ShowModal=1 then
//      begin
//        with Self.CDS_HZ do
//        begin
//          edit;
//          FieldByName('OrderNo').Value:=Trim(frmClothSCListSel.Order_Main.fieldbyname('ConNo').AsString);
//          FieldByName('MainId').Value:=Trim(frmClothSCListSel.Order_Main.fieldbyname('MainId').AsString);
//          FieldByName('SubId').Value:=Trim(frmClothSCListSel.Order_Main.fieldbyname('SubId').AsString);
//          FieldByName('GJPrice').Value:=frmClothSCListSel.Order_Main.fieldbyname('WzPrice').AsInteger;
//          Post;
//        end;
//        with ADOQueryCmd do
//        begin
//          Close;
//          sql.Clear;
//          sql.Add('update DcgGz set OrderNo='+quotedstr(trim(Self.CDS_HZ.FieldByName('OrderNo').AsString)));
//          sql.Add(', MainId='+quotedstr(trim(Self.CDS_HZ.FieldByName('MainId').AsString)));
//          sql.Add(', SubId='+quotedstr(trim(Self.CDS_HZ.FieldByName('SubId').AsString)));
//          sql.Add(', GJPrice='+inttostr(Self.CDS_HZ.FieldByName('GJPrice').AsInteger));
//          sql.Add(' where GZID='+quotedstr(trim(Self.CDS_HZ.FieldByName('GZID').AsString)));
//          execsql;
//        end;
//      end;
//    end;
//  finally
//    frmClothSCListSel.Free;
//  end;
//  tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmDcgGzList.V2Column2PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      TBAdd.Visible:=false;
      TBDel.Visible:=false;
      TBEdit.Visible:=false;
      TBSave.Visible:=false;
      flag:='SCPerson';
      flagname:='挡车工';
      fnote:=True;
      V1Note.Caption:='编号';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
//          FieldByName('YFCode').Value:=Trim(ClientDataSet1.fieldbyname('ZdyNo').AsString);
          FieldByName('DName').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
          FieldByName('Name').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('update DcgGz set DName='+quotedstr(trim(Self.CDS_HZ.FieldByName('DName').AsString)));
          sql.Add(' where GZID='+quotedstr(trim(Self.CDS_HZ.FieldByName('GZID').AsString)));
          execsql;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmDcgGzList.V2Column3PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      TBAdd.Visible:=false;
      TBDel.Visible:=false;
      TBEdit.Visible:=false;
      TBSave.Visible:=false;
      flag:='CarNo';
      flagname:='机台';
      fnote:=True;
      V1Note.Caption:='车间';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('DCarNo').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('update DcgGz set DCarNo='+quotedstr(trim(Self.CDS_HZ.FieldByName('DCarNo').AsString)));
          sql.Add(' where GZID='+quotedstr(trim(Self.CDS_HZ.FieldByName('GZID').AsString)));
          execsql;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmDcgGzList.ToolButton5Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty   then exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('delete DcgGz where gzid='''+Trim(CDS_HZ.fieldbyname('gzid').AsString)+'''');
    execsql;
  end;
  CDS_HZ.Delete;
end;

procedure TfrmDcgGzList.V2Column11PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin

      flag:='BanCi';
      flagname:='班别';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('BanCi').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('update DcgGz set BanCi='+quotedstr(trim(Self.CDS_HZ.FieldByName('BanCi').AsString)));
          sql.Add(' where GZID='+quotedstr(trim(Self.CDS_HZ.FieldByName('GZID').AsString)));
          execsql;
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmDcgGzList.V2Column1PropertiesEditValueChanged(
  Sender: TObject);
var
  FFieldName,mvalue:String;
begin
  mvalue:=TcxTextEdit(Sender).EditingText;
  if Trim(mvalue)='' then
  begin
    mvalue:='0';
  end;
  FFieldName:=Trim(Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_HZ do
  begin
    Edit;
    FieldByName(FFieldName).Value:=trim(mvalue);
    Post;
  end;
  
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('update DcgGz set '+FFieldName+'='+quotedstr(trim(mvalue)));
    sql.Add(' where GZID='+quotedstr(trim(Self.CDS_HZ.FieldByName('GZID').AsString)));
    EXECSQL;
  end;
  tv2.Controller.EditingController.ShowEdit();
end;

procedure TfrmDcgGzList.ToolButton6Click(Sender: TObject);
begin
  frmJFSCList:=TfrmJFSCList.create(self) ;
  with frmJFSCList do
  begin
    show;
  end;
    InitGrid();
end;

end.


