unit U_JYDayListGang;

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, cxCheckBox, cxCalendar, cxSplitter, RM_Dataset, RM_System,
  RM_Common, RM_Class, RM_GridReport, RM_e_Xls, Menus, MovePanel, cxTextEdit,
  cxContainer, cxCurrencyEdit, BtnEdit, RM_e_main, RM_e_htm, RM_e_Graphic,
  RM_e_Jpeg, RM_e_txt, RM_e_csv, RM_E_llPDF, Math, Clipbrd, cxPC,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu;

type
  TfrmJYDayListGang = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    v1Column24: TcxGridDBColumn;
    MovePanel2: TMovePanel;
    v1Column27: TcxGridDBColumn;
    v1Column28: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1MKXS: TcxGridDBColumn;
    v1Column31: TcxGridDBColumn;
    v1Column32: TcxGridDBColumn;
    v1Column33: TcxGridDBColumn;
    v1Column34: TcxGridDBColumn;
    v1Column36: TcxGridDBColumn;
    v1Column39: TcxGridDBColumn;
    v1Column42: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column25: TcxGridDBColumn;
    v1Column26: TcxGridDBColumn;
    v1Column29: TcxGridDBColumn;
    v1Column30: TcxGridDBColumn;
    v1Column35: TcxGridDBColumn;
    v1Column37: TcxGridDBColumn;
    v1Column38: TcxGridDBColumn;
    v1Column40: TcxGridDBColumn;
    v1Column41: TcxGridDBColumn;
    v1Column43: TcxGridDBColumn;
    v1Column44: TcxGridDBColumn;
    v1Column45: TcxGridDBColumn;
    v1Column46: TcxGridDBColumn;
    v1Column47: TcxGridDBColumn;
    v1Column48: TcxGridDBColumn;
    v1Column49: TcxGridDBColumn;
    v1Column50: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Panel2: TPanel;
    Panel3: TPanel;
    Tv1Column3: TcxGridDBColumn;
    ToolButton1: TToolButton;
    RM2: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    ComboBox1: TComboBox;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    Panel4: TPanel;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure TCBNORChange(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure Panel2Click(Sender: TObject);
    procedure Panel3Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure Panel4Click(Sender: TObject);
  private
    DQdate: TDateTime;
    FLeft, FTop: Integer;
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid(GJFW: string);
    procedure InitForm();
    procedure PrintCKMDG(MRKOrdID: string);
    { Private declarations }
  public
    FAPID: string;
    { Public declarations }
  end;

var
  frmJYDayListGang: TfrmJYDayListGang;

implementation

uses
  U_DataLink, U_RTFun,
  U_SysLogHelp,U_Fun;

{$R *.dfm}


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

procedure TfrmJYDayListGang.FormCreate(Sender: TObject);
begin
  cxgrid1.Align := alClient;
  DQdate := SGetServerDate(ADOQueryTemp);
end;

procedure TfrmJYDayListGang.TBCloseClick(Sender: TObject);
begin
  //WriteCxGrid('检验报告JF1', Tv1, '检验管理');
  if Trim(DName)='ADMIN' then
  begin
    WriteCxGridAll(trim(self.Caption), Tv1,ADOQueryCmd,ADOQueryTemp);
  end;
  Close;
end;



procedure TfrmJYDayListGang.InitGrid(GJFW: string);
var
  fwhere, Pwhere,FCDList,FCD,CDWhere: string;
begin
  MovePanel2.Left := (self.Width - MovePanel2.Width) div 2;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  fwhere :=' and APID='''+Trim(FAPID)+'''';

  if GJFW = '高损耗' then
  begin
    fwhere:=fwhere+' and  ABS(PCBFB)>=15';
  end;
  if GJFW = '低损耗' then
  begin
    fwhere:=fwhere+' and  ABS(PCBFB)>=10 and  ABS(PCBFB)<15';
  end;
  if GJFW = '已处理' then
  begin
    fwhere:=fwhere+' and  ChuLiFlag=1 ';
  end;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add('exec P_View_JD_KK  ');
      sql.Add('@criteria= ' + quotedstr(fwhere));
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
    MovePanel2.Visible := false;
  finally
    ADOQueryMain.EnableControls;
    MovePanel2.Visible := false;
  end;
end;



procedure TfrmJYDayListGang.InitForm();
var
  fsj: string;
begin
  ReadCxGridAll(trim(self.Caption), Tv1);
end;

procedure TfrmJYDayListGang.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv1, ADOQueryMain, '检验报告');
end;

procedure TfrmJYDayListGang.TBRafreshClick(Sender: TObject);
begin

  CurrentPage := 1;
  InitGrid('');

end;

procedure TfrmJYDayListGang.FormShow(Sender: TObject);
begin
  InitForm();
  InitGrid('');
end;

procedure TfrmJYDayListGang.Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FLeft := X;
  FTop := Y;
end;



procedure TfrmJYDayListGang.TCBNORChange(Sender: TObject);
begin
  InitGrid('');
end;

procedure TfrmJYDayListGang.FormDestroy(Sender: TObject);
begin
  frmJYDayListGang:=nil;
end;

procedure TfrmJYDayListGang.Tv1CustomDrawCell(Sender: TcxCustomGridTableView;
  ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
  var ADone: Boolean);
var
  Fsunhao:Double;
begin
  Fsunhao := AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('PCBFB').Index];

  if Fsunhao < 0 then
    Fsunhao := -Fsunhao;
  if Fsunhao >=15 then
    ACanvas.Brush.Color := $4763FF;
  if (Fsunhao >=10) and (Fsunhao <15) then
    ACanvas.Brush.Color := $00FFFF;
  if AViewInfo.GridRecord.Values[TcxGridDBTableView(Sender).GetColumnByFieldName('ChuLiFlag').Index]=True then
    ACanvas.Brush.Color :=clSkyBlue;
end;

procedure TfrmJYDayListGang.Panel2Click(Sender: TObject);
begin
  InitGrid('高损耗');
end;

procedure TfrmJYDayListGang.Panel3Click(Sender: TObject);
begin
  InitGrid('低损耗');
end;

procedure TfrmJYDayListGang.ToolButton1Click(Sender: TObject);
begin
  if Trim(Order_Main.fieldbyname('RKOrdID').AsString) = '' then
    exit;
  PrintCKMDG(Trim(Order_Main.fieldbyname('RKOrdID').AsString));
end;
procedure TfrmJYDayListGang.PrintCKMDG(MRKOrdID: string);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if Order_Main.IsEmpty then
    Exit;
  ExportFtErpFile('仓库码单.rmf', ADOQueryTemp);
  with Order_Main do
  begin

    with ADOQueryPrint do
    begin
      Close;
      SQL.Clear;
      sql.Add(' exec P_Print_RKMDGangNo ');
      SQL.Add(' @RKOrdID=''' + Trim(MRKOrdID) + '''');
      Open;
    end;
    try
      Moudle := LoadLibrary('MakeQRBarcode.dll');
      @Makebar := GetProcAddress(Moudle, 'Make');
      @Mixtext := GetProcAddress(Moudle, 'MixText');
      Txt := Trim(ADOQueryPrint.fieldbyname('RKOrdID').AsString);
      fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
      if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
        CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
      if FileExists(fImagePath) then
        DeleteFile(fImagePath);
      Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);
    except
      application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
      exit;
    end;
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\仓库码单.rmf';
    if FileExists(fPrintFile) then
    begin
      RMVariables['QRBARCODE'] := fImagePath;
      RM2.LoadFromFile(fPrintFile);
      Rm2.DefaultCopies := strtointdef(trim(ComboBox1.Text), 1);
//      RM2.PrintReport;
      RM2.ShowReport;
    end
    else
    begin
      Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
      Exit;
    end;

  end;

end;


procedure TfrmJYDayListGang.ToolButton2Click(Sender: TObject);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if Order_Main.IsEmpty then
    Exit;
  ExportFtErpFile('三联仓库码单.rmf', ADOQueryTemp);
  with Order_Main do
  begin

    with ADOQueryPrint do
    begin
      Close;
      SQL.Clear;
      sql.Add(' exec P_Print_RKMDGangNo2 ');
      SQL.Add(' @RKOrdID=''' + Trim(Order_Main.fieldbyname('RKOrdID').AsString) + '''');
      Open;
    end;
    try
      Moudle := LoadLibrary('MakeQRBarcode.dll');
      @Makebar := GetProcAddress(Moudle, 'Make');
      @Mixtext := GetProcAddress(Moudle, 'MixText');
      Txt := Trim(ADOQueryPrint.fieldbyname('RKOrdID').AsString);
      fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
      if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
        CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
      if FileExists(fImagePath) then
        DeleteFile(fImagePath);
      Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);
    except
      application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
      exit;
    end;
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\三联仓库码单.rmf';
    if FileExists(fPrintFile) then
    begin
      RMVariables['QRBARCODE'] := fImagePath;
      RM2.LoadFromFile(fPrintFile);
      //Rm2.DefaultCopies := strtointdef(trim(ComboBox1.Text), 1);
//      RM2.PrintReport;
      RM2.ShowReport;
    end
    else
    begin
      Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
      Exit;
    end;

  end;

end;

procedure TfrmJYDayListGang.ToolButton3Click(Sender: TObject);
begin
  frmSysLogHelp := TfrmSysLogHelp.create(self);
  with frmSysLogHelp do
  begin
    fModel := self.caption;
  //  facction:='生产指示单删除';
    showmodal;
    free;
  end;
end;

procedure TfrmJYDayListGang.ToolButton4Click(Sender: TObject);
var
  strsql, FSPId: string;
begin
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('请选择数据！', '提示', 0);
    Exit;
  end;
  while Order_Main.Locate('SSel', True, []) do
  begin
    FSPId := Trim(Order_Main.fieldbyname('MJId').AsString);
    strsql := '卷号：' + Trim(Order_Main.fieldbyname('MJId').AsString) + ' 订单号:' + Trim(Order_Main.fieldbyname('conNO').AsString);
    if Application.MessageBox(PChar('请确定：' + strsql), '提示', 32 + 4) <> IDYES then
      Exit;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' update WFB_MJJY set ChuLiFlag=1,ChuLiTime=getdate(),ChuLiPerson='''+Trim(DName)+'''');
      sql.Add(' where MJID=' + quotedstr(Trim(Order_Main.fieldbyname('MJID').AsString)));

      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr('偏差处理'));
      sql.Add(',' + quotedstr(trim(strsql)));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    Order_Main.Edit;
    Order_Main.FieldByName('SSel').Value:=False;
    Order_Main.FieldByName('ChuLiFlag').Value:=True;
    Order_Main.Post;
  end;

end;

procedure TfrmJYDayListGang.ToolButton5Click(Sender: TObject);
var
  strsql, FSPId: string;
begin
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('请选择数据！', '提示', 0);
    Exit;
  end;
  while Order_Main.Locate('SSel', True, []) do
  begin
    FSPId := Trim(Order_Main.fieldbyname('MJId').AsString);
    strsql := '卷号：' + Trim(Order_Main.fieldbyname('MJId').AsString) + '订单号:' + Trim(Order_Main.fieldbyname('conNO').AsString);
    if Application.MessageBox(PChar('请确定：' + strsql), '提示', 32 + 4) <> IDYES then
      Exit;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add(' update WFB_MJJY set ChuLiFlag=0,ChuLiTime=getdate(),ChuLiPerson='''+Trim(DName)+'''');
      sql.Add(' where MJID=' + quotedstr(Trim(Order_Main.fieldbyname('MJID').AsString)));

      sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
      sql.Add(' ' + quotedstr(trim(DName)));
      sql.Add(',getdate() ');
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr('偏差处理'));
      sql.Add(',' + quotedstr(trim(strsql)));
      sql.Add(',' + quotedstr(trim('撤销')));
      sql.Add(')');
      execsql;
    end;
    Order_Main.Edit;
    Order_Main.FieldByName('SSel').Value:=False;
    Order_Main.FieldByName('ChuLiFlag').Value:=False;
    Order_Main.Post;
  end;

end;

procedure TfrmJYDayListGang.Panel4Click(Sender: TObject);
begin
  InitGrid('已处理');
end;

end.

