unit U_ProductOrderNewList_CY_Sel;

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, cxButtonEdit, cxTextEdit, cxPC;

type
  TfrmProductOrderNewList_CY_Sel = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    OrderNo: TEdit;
    TBExport: TToolButton;
    v1OrderNo: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1CustomerNoName: TcxGridDBColumn;
    Label4: TLabel;
    CustomerNoName: TEdit;
    ToolButton1: TToolButton;
    Label8: TLabel;
    MPRTCodeName: TEdit;
    Label9: TLabel;
    ConNo: TEdit;
    Label10: TLabel;
    MPRTSpec: TEdit;
    Label11: TLabel;
    MPRTCode: TEdit;
    Label12: TLabel;
    MPRTKZ: TEdit;
    Label13: TLabel;
    MPRTMF: TEdit;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    ToolButton3: TToolButton;
    ComboBox1: TComboBox;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    cxSplitter1: TcxSplitter;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    v1Column8: TcxGridDBColumn;
    PMFJ: TPopupMenu;
    NFJ: TMenuItem;
    v1OrdPerson2: TcxGridDBColumn;
    Label2: TLabel;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v1XHNo: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v1Column14: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    CDS_Sub: TClientDataSet;
    v1Column15: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure OrderNoChange(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 ToolButton1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure Panel10MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView;
      APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord;
      ANewItemRecordFocusingChanged: Boolean);
    procedure Tv2DblClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
  private
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;

    procedure InitSub();
    procedure SetStatus();
    { Private declarations }
  public
    FFInt,FCloth:Integer;

    { Public declarations }
  end;

var
  frmProductOrderNewList_CY_Sel: TfrmProductOrderNewList_CY_Sel;

implementation
uses
  U_DataLink,U_OrderInPut,U_Fun,  U_OrderInPut_CY;

{$R *.dfm}
procedure TfrmProductOrderNewList_CY_Sel.SetStatus();
begin

end;
procedure TfrmProductOrderNewList_CY_Sel.InitSub();
begin
  ADOQuerySub.Close;
  IF Order_Main.IsEmpty then exit;
  with ADOQuerySub do
  begin
    close;
    sql.Clear;
    sql.Add('select * from JYOrderCY_sub ');
    sql.Add('where mainID ='+quotedstr((Order_Main.fieldbyname('mainID').AsString)));
    open;
  end;
   SCreateCDS20(ADOQuerySub,CDS_Sub);
   SInitCDSData20(ADOQuerySub,CDS_Sub);
end;

procedure TfrmProductOrderNewList_CY_Sel.FormDestroy(Sender: TObject);
begin
  frmProductOrderNewList_CY_Sel:=nil;
end;

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

procedure TfrmProductOrderNewList_CY_Sel.FormCreate(Sender: TObject);
begin
  //BegDate.DateTime:=SGetServerDateTime(ADOQueryTemp)-7;
  //EndDate.DateTime:=SGetServerDateTime(ADOQueryTemp);
  DQdate:=SGetServerDate(ADOQueryTemp);
 /// SetStatus();
end;

procedure TfrmProductOrderNewList_CY_Sel.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid(self.Caption+tv1.Name,Tv1,'生产指示单管理');
  WriteCxGrid(self.Caption+tv2.Name,Tv2,'生产指示单管理');
end;

procedure TfrmProductOrderNewList_CY_Sel.InitGrid();
var
  fwsql:string;
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.add('select OrderQty=(select Sum(PRTOrderQty) from JYOrderCY_Sub B where B.MainId=A.MainId group by B.MainId),') ;
      sql.add(' ZQty=(select Sum(PRTOrderQty) from JYOrderCY_Sub B where B.MainId=A.MainId group by B.MainId),') ;
      sql.add(' OrderUnit=(select Top 1 OrderUnit  from JYOrderCY_Sub B where B.MainId=A.MainId),') ;
      sql.add(' ZSordQty1=(select Sum(SordQty1) from JYOrderCY_Sub B where B.MainId=A.MainId group by B.MainId), ') ;
      sql.add(' A.*,A.OrderNo OrderNoM') ;
      sql.add(' from JYOrderCY_Main A') ;
      sql.add(' where A.OrdDate>='''+FormatDateTime('yyyy-MM-dd',BegDate.DateTime)+''' and  A.OrdDate<'''+FormatDateTime('yyyy-MM-dd',enddate.DateTime+1)+'''');
      SQL.Add(' and Status=''1''');
      ExecSQL;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmProductOrderNewList_CY_Sel.InitForm();
begin
  ReadCxGrid(self.Caption+tv1.Name,Tv1,'生产指示单管理');
  readCxGrid(self.Caption+tv2.Name,Tv2,'生产指示单管理');
  BegDate.DateTime:=SGetServerDate10(ADOQueryTemp)-30;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  InitGrid();
end;

procedure TfrmProductOrderNewList_CY_Sel.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 TfrmProductOrderNewList_CY_Sel.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrderCY_Main where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      sql.Add('delete JYOrderCY_Sub where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
      sql.Add('delete JY_CY_money where MainId='''+Trim(Order_Main.fieldbyname('MainId').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(trim('出运单删除')));
      sql.Add(','+quotedstr(trim('指出运单：'+trim(Order_Main.FieldByName('OrderNO').AsString))));
      sql.Add(','+quotedstr(trim('成功')));
      sql.Add(')');
      ExecSQL;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmProductOrderNewList_CY_Sel.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv1,ADOQueryMain,self.Caption);
end;

procedure TfrmProductOrderNewList_CY_Sel.TBRafreshClick(Sender: TObject);
begin
  if FFInt=1 then
  begin

  end else
  begin
    InitGrid();
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select Top 10* from JYOrder_Main Order by FillTime desc');
      Open;
    end;
    ComboBox1.Clear;
    with ADOQueryTemp do
    begin
      First;
      while not Eof do
      begin
        ComboBox1.Items.Add(Trim(ADOQueryTemp.fieldbyname('OrderNO').AsString));
        Next;
      end;
    end;
  end;
end;

procedure TfrmProductOrderNewList_CY_Sel.OrderNoChange(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 TfrmProductOrderNewList_CY_Sel.FormShow(Sender: TObject);
begin
  InitForm();
end;

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

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

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

procedure TfrmProductOrderNewList_CY_Sel.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 TfrmProductOrderNewList_CY_Sel.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 TfrmProductOrderNewList_CY_Sel.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 TfrmProductOrderNewList_CY_Sel.ToolButton1Click(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  try
    frmOrderInPut_CY:=TfrmOrderInPut_CY.Create(Application);
    with frmOrderInPut_CY do
    begin
      PState:=1;
      FMainId:=Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      ToolBar2.Visible:=False;
      TBSave.Visible:=False;
      Panel1.Enabled:=False;
      Tv1.OptionsSelection.CellSelect:=False;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmOrderInPut_CY.Free;
  end;
end;

procedure TfrmProductOrderNewList_CY_Sel.ToolButton3Click(Sender: TObject);
begin
  IF Order_Main.IsEmpty then exit;
  ModalResult:=1;
end;

procedure TfrmProductOrderNewList_CY_Sel.Panel10MouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  ReleaseCapture;
//  TWinControl(Panel4).Perform(WM_SYSCOMMAND,$F012,0);
end;

procedure TfrmProductOrderNewList_CY_Sel.Tv1FocusedRecordChanged(
  Sender: TcxCustomGridTableView; APrevFocusedRecord,
  AFocusedRecord: TcxCustomGridRecord;
  ANewItemRecordFocusingChanged: Boolean);
begin
  InitSub();
end;

procedure TfrmProductOrderNewList_CY_Sel.Tv2DblClick(
  Sender: TObject);
var
  fNO:string;
begin
  IF ADOQuerySub.IsEmpty then exit;
//  ToolButton2.Click;
 { with  ADOQueryCmd do
  begin
    close;
    sql.Clear;
    sql.Add('select ATID from KH_Zdy_Attachment A');
    sql.Add('where zdyCode='+quotedstr(trim(ADOQuerySub.fieldbyname('prtCode').AsString)));
    sql.Add('and Type='+quotedstr(trim('CP')));
    open;
    if not IsEmpty then
    begin
      fNO:=trim(fieldbyname('ATID').AsString);
    end;
  end;

  try
    frmZdyAttInputCP10:=TfrmZdyAttInputCP10.Create(Application);
    with frmZdyAttInputCP10 do
    begin
      FATID:=Trim(fNO);
      frmZdyAttInputCP10.Tsave.Enabled:=false;
      if ShowModal=1 then
      begin
      //  TBRafresh.Click;
      end;
    end;
  finally
    frmZdyAttInputCP10.Free;
  end;
   }
end;

procedure TfrmProductOrderNewList_CY_Sel.cxTabControl1Change(Sender: TObject);
begin
//  SetStatus();
  TBRafresh.Click;
end;

end.
