unit U_ProductOrderListOLJ;

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, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP,ShellAPI
  ,IniFiles,jpeg, cxButtonEdit, cxGridBandedTableView,
  cxGridDBBandedTableView;

type
  TfrmProductOrderListOLJ = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    OrderNoM: TEdit;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    Label4: TLabel;
    CustomerNoName: TEdit;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    Label5: TLabel;
    OrdPerson1: TEdit;
    Label7: TLabel;
    PRTColor: TEdit;
    Label8: TLabel;
    PRTCodeName: TEdit;
    Label13: TLabel;
    PRTSpec: TEdit;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    ToolButton3: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBBandedTableView;
    v1Column1: TcxGridDBBandedColumn;
    v1Column2: TcxGridDBBandedColumn;
    v1Column3: TcxGridDBBandedColumn;
    v1Column4: TcxGridDBBandedColumn;
    v1Column5: TcxGridDBBandedColumn;
    v1Column6: TcxGridDBBandedColumn;
    v1Column7: TcxGridDBBandedColumn;
    v1Column8: TcxGridDBBandedColumn;
    v1Column9: TcxGridDBBandedColumn;
    v1Column10: TcxGridDBBandedColumn;
    v1Column11: TcxGridDBBandedColumn;
    v1Column12: TcxGridDBBandedColumn;
    v1Column13: TcxGridDBBandedColumn;
    v1Column14: TcxGridDBBandedColumn;
    v1Column15: TcxGridDBBandedColumn;
    v1Column16: TcxGridDBBandedColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column17: TcxGridDBBandedColumn;
    v1Column18: TcxGridDBBandedColumn;
    v1Column19: TcxGridDBBandedColumn;
    v1Column20: TcxGridDBBandedColumn;
    v1Column21: TcxGridDBBandedColumn;
    v1Column22: TcxGridDBBandedColumn;
    v1Column23: TcxGridDBBandedColumn;
    v1Column24: TcxGridDBBandedColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure OrderNoMChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBTPClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure Tv1StylesGetContentStyle(Sender: TcxCustomGridTableView;
      ARecord: TcxCustomGridRecord; AItem: TcxCustomGridTableItem;
      out AStyle: TcxStyle);
    procedure v1DeliveryDateCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure OrderNoMKeyPress(Sender: TObject; var Key: Char);
  private
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure InitGridFH();
    procedure ReadINIFile();
    procedure SaveImage();
    procedure SetSaveImage(ADOQuerycmd:TADOQuery;MyPic:string;Image1:TImage);
    procedure CreThumb(Width, Height: Integer;Image10,Image20:TImage);
    { Private declarations }
  public
    FFInt,FCloth:Integer;

    { Public declarations }
  end;

var
  frmProductOrderListOLJ: TfrmProductOrderListOLJ;

implementation
uses
  U_DataLink,U_Fun;

{$R *.dfm}

procedure TfrmProductOrderListOLJ.FormDestroy(Sender: TObject);
begin
  frmProductOrderListOLJ:=nil;
end;

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

procedure TfrmProductOrderListOLJ.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
  //BegDate.DateTime:=SGetServerDateTime(ADOQueryTemp)-7;
  //EndDate.DateTime:=SGetServerDateTime(ADOQueryTemp);
  DQdate:=SGetServerDate(ADOQueryTemp);
end;

procedure TfrmProductOrderListOLJ.TBCloseClick(Sender: TObject);
begin
  Close;
  if FCloth<>1 then
    WriteCxBandedGrid('工作指示单列表',Tv1,'生产指示单管理')
  else
    WriteCxBandedGrid('工作指示单列表选择',Tv1,'生产指示单管理');
end;

procedure TfrmProductOrderListOLJ.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select OrdDate=convert(varchar(10),OrdDate,120),DlyDate=convert(varchar(10),DlyDate,120)  ');
      sql.Add(',Case when SOrdFlag1=0 then '''' else ''√'' end as SOrdFlag1');
      sql.Add(',Case when SOrdFlag2=0 then '''' else ''√'' end as SOrdFlag2');
      sql.Add(',Case when SOrdFlag3=0 then '''' else ''√'' end as SOrdFlag3');
      sql.add(', A.*,B.*,A.OrderNo OrderNoM');
      sql.Add('from JYOrder_Main A left join JYOrder_Sub B on A.MainId=B.MainId ');

      SQL.Add('where OrdDate>=:begdate and OrdDate<:enddate');
      
      Parameters.ParamByName('begdate').Value:=FormatDateTime('yyyy-MM-dd',BegDate.DateTime);
      Parameters.ParamByName('enddate').Value:=FormatDateTime('yyyy-MM-dd',enddate.DateTime+1);
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;
procedure TfrmProductOrderListOLJ.InitGridFH();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('exec Order_QryList :MainId,:WSql');
      Parameters.ParamByName('WSql').Value:=' and FillTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+''''
                                           +' and FillTime<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))+'''';
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmProductOrderListOLJ.InitForm();
begin
 

  ReadCxBandedGrid('工作指示单列表选择',Tv1,'生产指示单管理');


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

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

function TfrmProductOrderListOLJ.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_Sub where SubId='''+Trim(Order_Main.fieldbyname('SubId').AsString)+'''');
      ExecSQL;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from JYOrder_Sub where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      Open;
      if IsEmpty then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete JYOrder_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
          ExecSQL;
        end;
      end;
    end;
    if Trim(Order_Main.fieldbyname('SubId').AsString)='' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('delete JYOrder_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
        ExecSQL;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmProductOrderListOLJ.SaveImage();
var
  JPGImg:TJPEGImage;
  ADOData:TADOBlobStream;
begin
   
end;
procedure TfrmProductOrderListOLJ.SetSaveImage(ADOQuerycmd:TADOQuery;MyPic:string;Image1:TImage);
var
  JPGImg:TJPEGImage;
  ADOData:TADOBlobStream;
begin

end;
procedure TfrmProductOrderListOLJ.CreThumb(Width, Height: Integer;Image10,Image20:TImage);
var
  Bitmap: TBitmap;
  Ratio: Double;
  ARect: TRect;
  AHeight, AHeightOffset: Integer;
  AWidth, AWidthOffset: Integer;
begin

end;

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

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

procedure TfrmProductOrderListOLJ.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmProductOrderListOLJ.TBTPClick(Sender: TObject);
 var
   FQty,FQty1,FMxQty,FPQty,FMxQtyS,FPQtyS:String;
begin
end;

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

procedure TfrmProductOrderListOLJ.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmProductOrderListOLJ.Tv1StylesGetContentStyle(
  Sender: TcxCustomGridTableView; ARecord: TcxCustomGridRecord;
  AItem: TcxCustomGridTableItem; out AStyle: TcxStyle);
var
  id,id10:Integer;
begin
  {try
    if Tv1.GroupedItemCount=0 then
    begin
      Id:=Tv1.GetColumnByFieldName('DeliveryDate').Index-tv1.GroupedItemCount;
      Id10:=Tv1.GetColumnByFieldName('SubStatus').Index-tv1.GroupedItemCount;
      if Trim(VarToStr(ARecord.Values[id]))='' then Exit;
      if Id<0 then Exit;
      if ARecord.Values[id10]='完成' then exit;
      if (ARecord.Values[id]-DQdate)>=4 then Exit;
      if ((ARecord.Values[id]-DQdate)>=0) and ((ARecord.Values[id]-DQdate)<4) then
        AStyle:=DataLink_.QHuangSe
      else
      if ARecord.Values[id]-DQdate<0 then
      begin
        AStyle:=DataLink_OrderManage.FenHongS;
      end;
    end else
    begin

    end;
  except
  end; }
end;

procedure TfrmProductOrderListOLJ.v1DeliveryDateCustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin
 { Id:=TV1.GetColumnByFieldName('DeliveryDate').Index;//;-TV1.GroupedItemCount;
  Id10:=TV1.GetColumnByFieldName('SubStatus').Index;
  if Id<0 then Exit;
  if AViewInfo.GridRecord.Values[Id10]='完成' then Exit;
  if AViewInfo.GridRecord.Values[Id]-SGetServerDate(ADOQueryTemp)>=4 then Exit;
  if ((AViewInfo.GridRecord.Values[id]-SGetServerDate10(ADOQueryTemp))>=0) and ((AViewInfo.GridRecord.Values[id]-SGetServerDate(ADOQueryTemp))<4) then
    ACanvas.Brush.Color:=clYellow
  else
  if (AViewInfo.GridRecord.Values[id])-(SGetServerDate10(ADOQueryTemp)<0) then
  begin
    ACanvas.Brush.Color:=clRed;
  end;
  begin
    ACanvas.Brush.Color:=clRed;
  end else
  if AViewInfo.GridRecord.Values[Id]='Purple' then
  begin
    ACanvas.Brush.Color:=clPurple;
  end else
  if AViewInfo.GridRecord.Values[Id]='Olive' then
  begin
    ACanvas.Brush.Color:=clOlive;
  end else
  if AViewInfo.GridRecord.Values[Id]='Teal' then
  begin
    ACanvas.Brush.Color:=clTeal;
  end else
  if AViewInfo.GridRecord.Values[Id]='Background' then
  begin
    ACanvas.Brush.Color:=clBackground;
  end;   }
end;

procedure TfrmProductOrderListOLJ.N1Click(Sender: TObject);
var
   fPrintFile:string;
   Porderno:string;
begin
  if Order_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\生产指示单10.rmf' ;
  SDofilter(ADOQueryMain,' OrderNoM='''+Trim(Order_Main.fieldbyname('OrderNoM').AsString)+'''');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Porderno:=Trim(Order_Main.fieldbyname('OrderNoM').AsString);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\生产指示单10.rmf'),'提示',0);
  end;
  SDofilter(ADOQueryMain,'');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Order_Main.Locate('ordernoM',Porderno,[]);
end;

procedure TfrmProductOrderListOLJ.N2Click(Sender: TObject);
var
   fPrintFile:string;
   Porderno:string;
begin
  if Order_Main.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\生产指示单.rmf' ;
  SDofilter(ADOQueryMain,' OrderNoM='''+Trim(Order_Main.fieldbyname('OrderNoM').AsString)+'''');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Porderno:=Trim(Order_Main.fieldbyname('OrderNoM').AsString);
  if FileExists(fPrintFile) then
  begin
    //RMVariables['begindate']:=begindate.DateTime;
    //RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\生产指示单.rmf'),'提示',0);
  end;
  SDofilter(ADOQueryMain,'');
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
  Order_Main.Locate('ordernoM',Porderno,[]);
end;

procedure TfrmProductOrderListOLJ.ToolButton3Click(Sender: TObject);
begin
  ModalResult:=1;
end;

procedure TfrmProductOrderListOLJ.ReadINIFile();
var
  programIni:Tinifile;  //配置文件名
  FileName:string;
begin
  FileName:=ExtractFilePath(Paramstr(0))+'SYSTEMSET.INI';
  programIni:=Tinifile.create(FileName);
  server:=programIni.ReadString('SERVER','服务器地址','127.0.0.1');
  programIni.Free;
end;

procedure TfrmProductOrderListOLJ.OrderNoMKeyPress(Sender: TObject;
  var Key: Char);
var
  fsj:string;
begin
  if Key=#13 then
  begin
    if Length(Trim(OrderNoM.Text))<4 then Exit;
    fsj:='%'+Trim(OrderNoM.Text)+'%';
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add('select OrderNoM=A.OrderNo,OrdDate=convert(varchar(10),OrdDate,120),DlyDate=convert(varchar(10),DlyDate,120)  ');
        sql.Add(',Case when SOrdFlag1=0 then '''' else ''√'' end as SOrdFlag1');
        sql.Add(',Case when SOrdFlag2=0 then '''' else ''√'' end as SOrdFlag2');
        sql.Add(',Case when SOrdFlag3=0 then '''' else ''√'' end as SOrdFlag3');
        sql.add(', A.*,B.* ');
        sql.Add('from JYOrder_Main A left join JYOrder_Sub B on A.MainId=B.MainId ');

        SQL.Add('where A.orderNo like '''+Trim(fsj)+'''');
        
        //ShowMessage(ADOQueryMain.SQL.Text);
        Open;
      end;
      SCreateCDS20(ADOQueryMain,Order_Main);
      SInitCDSData20(ADOQueryMain,Order_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;

end;

end.
