unit U_FHOKSelList;

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, cxCalendar, cxButtonEdit, cxSplitter,
  RM_Common, RM_Class, RM_e_Xls, RM_Dataset, RM_System, RM_GridReport,
  cxTextEdit, cxPC, cxCheckBox, Menus;


type
  TfrmFHOKSelList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    ToolButton3: TToolButton;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Panel1: TPanel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label8: TLabel;
    OrderNo: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1ShipDate: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column16: TcxGridDBColumn;
    ComboBox2: TComboBox;
    Label3: TLabel;
    Label1: TLabel;
    ToolButton1: TToolButton;
    ToolButton4: TToolButton;
    Label5: TLabel;
    PRTCode: TEdit;
    v1Column1: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Label7: TLabel;
    PRTColor: TEdit;
    Label9: TLabel;
    PRTHX: TEdit;
    ADOQueryPrint: TADOQuery;
    v1Column15: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    v1Column14: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    v1Column8: TcxGridDBColumn;
    ToolButton2: TToolButton;
    v1Column17: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column22: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure WorkerChange(Sender: TObject);
    procedure v1Column15CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure v1Column9CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);

  private
    FInt,PFInt:Integer;
    canshu1,canshu2:string;
    FNowDate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function  DelData():Boolean;
    procedure InitGridWsql(fsj:string);
    function  SaveData():Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmFHOKSelList: TfrmFHOKSelList;

implementation
uses
  U_DataLink,U_RTFun,U_FHInPut,U_ModuleNote,U_ZDYHelp,U_FHJJInPut;

{$R *.dfm}

procedure TfrmFHOKSelList.FormDestroy(Sender: TObject);
begin
  frmFHOKSelList:=nil;
end;

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

procedure TfrmFHOKSelList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('发货记录',Tv1,'CI管理');
end;

procedure TfrmFHOKSelList.InitGrid();
var
  fsj:String;
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select AA.* from (');
      sql.Add(' select isnull(A.FHQty,A.SQQty) FHQty10,isnull(A.FHPS,A.SQPS) FHPS10  ');
      sql.Add(',A.*,C.OrderNo,D.PRTHX,PRTCode=isnull(C.MPRTCode,'''')+isnull(C.MPRTGY,'''')');
      sql.Add(',PRTColor=isnull(D.PRTColorNo,'''')+'' ''+isnull(D.PRTColor,'''')+'' ''+isnull(D.PRTColorEng,'''')');
      sql.Add(' ,E.PRTPrice,E.PriceUnit,F.ConNo,F.CustomerNoName,F.CustomerNo,PIMainId=E.Mainid,PISubId=E.SubId,FHMoney=E.PRTPrice*A.FHQty');
      SQL.Add(' from JYOrder_FH A');
      sql.Add(' inner join JYOrder_Main C on A.MainId=C.MainId');
      sql.Add(' inner join JYOrder_Sub D on A.SubId=D.SubId');
      sql.Add(' left join JYOrderCon_Sub E on D.PISubId=E.SubId');
      sql.Add(' left join JYorderCon_Main F on E.Mainid=F.MainId');
      sql.Add(' where not exists(select * from JYOrder_CI_Sub JCS where JCS.FHID=A.FHID)');
      sql.Add('  ) AA');
      sql.Add(' where 1=1 ');
      if cxTabControl1.TabIndex<>0 then
      begin
        fsj:=TA(ComboBox2.Items.Objects[ComboBox2.Items.IndexOf(Trim(ComboBox2.Text))]).S;
        sql.Add(' and AA.'+Trim(fsj)+'>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.Date))+'''');
        sql.Add(' and AA.'+Trim(fsj)+'<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.Date+1))+'''');
      end;
      sql.Add(' and isnull(FHStatus,'''')<>'''' ');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;

end;

procedure TfrmFHOKSelList.InitForm();
begin
  FNowDate:=SGetServerDate(ADOQueryTemp);
  EndDate.Date:=FNowDate;
  BegDate.Date:=EndDate.Date-7;
  ReadCxGrid('发货记录',Tv1,'CI管理');
  //InitGrid();
end;

procedure TfrmFHOKSelList.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  if cxTabControl1.TabIndex<>0 then Exit;
  if Trim(DName)<>Trim(Order_Main.fieldbyname('Filler').AsString) then
  begin
    Application.MessageBox('不能操作他人的信息!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if DelData() then
  begin
    Order_Main.Delete;
  end;
end;

function TfrmFHOKSelList.DelData():Boolean;
begin
  try
    Result:=false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete JYOrder_FH  where FHId='''+Trim(Order_Main.fieldbyname('FHId').AsString)+'''');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=False;
    Application.MessageBox('数据删除异常！','提示',0);
  end;
end;

procedure TfrmFHOKSelList.TBExportClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then Exit;
  TcxGridToExcel('实际发货列表',cxGrid1);
end;

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

procedure TfrmFHOKSelList.FormShow(Sender: TObject);
var
  i:Integer;
begin
  InitForm();
  SInitComBoxByTvColumns(ComboBox2,Tv1,999,True,True);
end;

procedure TfrmFHOKSelList.cxPageControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmFHOKSelList.ToolButton3Click(Sender: TObject);
begin
  try
    frmModuleNote:=TfrmModuleNote.Create(Application);
    with frmModuleNote do
    begin
      flag:='实际发货';
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmModuleNote.Free;
  end;
end;

procedure TfrmFHOKSelList.FormCreate(Sender: TObject);
begin
  canshu1:=Trim(DParameters1);
  canshu2:=Trim(DParameters2);
end;

procedure TfrmFHOKSelList.InitGridWsql(fsj:string);
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('select AA.* from (');
      sql.Add(' select   ');  //isnull(A.FHQty,A.SQQty) FHQty10,isnull(A.FHPS,A.SQPS) FHPS10
      sql.Add(' A.*,C.OrderNo,D.PRTHX,PRTCode=isnull(C.MPRTCode,'''')+isnull(C.MPRTGY,'''')');
      sql.Add(',PRTColor=isnull(D.PRTColorNo,'''')+'' ''+isnull(D.PRTColor,'''')+'' ''+isnull(D.PRTColorEng,'''')');
      sql.Add(' ,E.PRTPrice,E.PriceUnit,F.ConNo,F.CustomerNoName,PIMainId=E.Mainid,PISubId=E.SubId,FHMoney=E.PRTPrice*A.FHQty');
      SQL.Add(' from JYOrder_FH A');
      sql.Add(' inner join JYOrder_Main C on A.MainId=C.MainId');
      sql.Add(' inner join JYOrder_Sub D on A.SubId=D.SubId');
      sql.Add(' left join JYOrderCon_Sub E on D.PISubId=E.SubId');
      sql.Add(' left join JYorderCon_Main F on E.Mainid=F.MainId');
      sql.Add(' where not exists(select * from JYOrder_CI_Sub JCS where JCS.FHID=A.FHID)');
      sql.Add('  ) AA');
      sql.Add(' where 1=1 ');
      {if Trim(canshu1)<>'高权限' then
      begin
        sql.Add('and Filler in(select UserName from  SY_User where DPID in');
        sql.Add('              (select DPID from SY_User where UserName='''+Trim(DName)+'''))');
      end;}
      sql.Add(' and isnull(FHStatus,'''')<>'''' ');
      sql.Add(fsj);
      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;
function TfrmFHOKSelList.SaveData():Boolean;
var
  maxno,maxSubNo:String;
begin
  try
    Result:=False;
    ADOQueryCmd.Connection.BeginTrans;
    //保存主表
    if Trim(Order_Main.fieldbyname('MRID').AsString)='' then
    begin
      if GetLSNo(ADOQueryCmd,maxno,'MR','Module_Prompt_Result',4,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取工作子流水号失败!','提示',0);
        Exit;
      end;
    end else
    begin
      maxno:=Trim(Order_Main.fieldbyname('MRID').AsString);
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Module_Prompt_Result where MRId='''+Trim(Order_Main.fieldbyname('MRID').AsString)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(Order_Main.fieldbyname('MRID').AsString)='' then
      begin
        Append;
      end
      else begin
        Edit;
      end;
      FieldByName('MPId').Value:=Trim(Order_Main.fieldbyname('MPId').AsString);
      FieldByName('MRId').Value:=Trim(maxno);
      FieldByName('MRStatus').Value:='已处理';
      FieldByName('MRDate').Value:=SGetServerDateTime(ADOQueryTemp);
      if Trim(Order_Main.fieldbyname('MRId').AsString)='' then
      begin
        FieldByName('Filler').Value:=Trim(DName);
      end else
      begin
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
      end;
      Post;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;
procedure TfrmFHOKSelList.cxTabControl1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmFHOKSelList.WorkerChange(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 TfrmFHOKSelList.v1Column15CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  Id,Id10:Integer;
begin
  Id:=TV1.GetColumnByFieldName('YuJinagDate1').Index;//
  Id10:=TV1.GetColumnByFieldName('MoneyKC').Index;
  if Id<0 then Exit;
  //$008000FF 橙红色    橙色 $000D55FF   $00B9EE9F  浅绿
  if AViewInfo.GridRecord.Values[Id10]>0 then
  begin
    if AViewInfo.GridRecord.Values[Id]-FNowDate<=0 then
    begin
      ACanvas.Brush.Color:=clYellow;
    end  else
    if AViewInfo.GridRecord.Values[Id]=null then
    begin
     // ACanvas.Brush.Color:=clWhite;
    end;
  end else
  begin
    //ACanvas.Brush.Color:=clWhite;
  end;

end;

procedure TfrmFHOKSelList.v1Column9CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  Id,Id10:Integer;
begin
  Id:=TV1.GetColumnByFieldName('YuJinagDate2').Index;//
  Id10:=TV1.GetColumnByFieldName('MoneyKC').Index;
  if Id<0 then Exit;
  //$008000FF 橙红色    橙色 $000D55FF   $00B9EE9F  浅绿
  if AViewInfo.GridRecord.Values[Id10]>0 then
  begin
    if AViewInfo.GridRecord.Values[Id]-FNowDate<=0 then
    begin
      ACanvas.Brush.Color:=clRed;
    end  else
    if AViewInfo.GridRecord.Values[Id]=null then
    begin
      //ACanvas.Brush.Color:=clWhite;
    end;
  end else
  begin
    //ACanvas.Brush.Color:=clWhite;
  end;

end;

procedure TfrmFHOKSelList.ToolButton1Click(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 TfrmFHOKSelList.ToolButton4Click(Sender: TObject);
var
  fPrintFile:String;
begin
  if Order_Main.IsEmpty then Exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据！','提示',0);
    Exit;
  end;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\检验指示单标签.rmf' ;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean=True then
      begin
        {with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from JYOrder_Main_MD where MDID='''+Trim(Order_Main.fieldbyname('MDID').AsString)+'''');
          Open;
        end;
        if ADOQueryTemp.FieldByName('NotPrint').AsBoolean=True then
        begin
          Order_Main.EnableControls;
          Application.MessageBox('不能打印《不打印》的缸条码!','提示',0);
          Exit;
        end;  }
        if FileExists(fPrintFile) then
        begin
          with ADOQueryPrint do
          begin
            Close;
            sql.Clear;
            sql.Add('select B.OrderNo,B.MPRTCodeName,B.MPRTMF,B.MPRTKZ,C.PRTHX,C.PRTColorNo,C.PRTColor,GangNo=A.GangNo,A.PS,A.Qty,A.QtyUnit,A.MDID ');
            SQL.Add(',B.MPRTCode,B.MPRTGY,C.PRTColorEng,A.SelfGangNo');
            sql.Add('from JYOrder_Main_MD A');
            sql.Add('inner join JYOrder_Main B on A.OrdMainid=B.Mainid');
            sql.Add(' inner join JYOrder_Sub C on A.OrdSubid=C.SubId');
            sql.Add(' where A.MDID='''+Trim(Order_Main.fieldbyname('MDID').AsString)+'''');
            Open;
          end;
          RM1.LoadFromFile(fPrintFile);
          //RM1.ShowReport;
          RM1.PrintReport;
        end else
        begin
          Order_Main.DisableControls;
          Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+'Report\检验指示单标签.rmf'),'提示',0);
          Exit;
        end;
      end;

      Next;
    end;
  end;
  Order_Main.EnableControls;
end;

procedure TfrmFHOKSelList.OrderNoKeyPress(Sender: TObject; var Key: Char);
var
  fsj:String;
begin
  if Key=#13 then
  begin
    if Length(Trim(OrderNo.Text))<3 then Exit;
    fsj:=' and AA.OrderNo like '''+'%'+Trim(OrderNo.Text)+'%'+'''';
    InitGridWsql(fsj);
  end;

end;

procedure TfrmFHOKSelList.N1Click(Sender: TObject);
begin
  SelOKNo(Order_Main,True);
end;

procedure TfrmFHOKSelList.N2Click(Sender: TObject);
begin
  SelOKNo(Order_Main,false);
end;

procedure TfrmFHOKSelList.ToolButton2Click(Sender: TObject);
var
  FConNO:String;
begin
  if Order_Main.IsEmpty then exit;
  if Order_Main.Locate('SSel',True,[])=False then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    exit;
  end;
  if Order_Main.Locate('SSel;ConNo',VarArrayOf([True,Null]),[loPartialKey])=True then
  begin
    Application.MessageBox(PChar(Trim(Order_Main.fieldbyname('OrderNo').AsString)+
                                 Trim(Order_Main.fieldbyname('PRTColor').AsString)+
                                 Trim(Order_Main.fieldbyname('PRTHX').AsString)+
                                 '单价未指定!'),'提示',0);
    Exit;
  end;
  FConNO:='';
  Order_Main.DisableControls;
  with Order_Main do
  begin
    First;
    while not Eof do
    begin
      if Order_Main.FieldByName('SSel').AsBoolean=True then
      begin
        if Trim(FConNO)='' then
        begin
          FConNO:=Trim(Order_Main.fieldbyname('ConNo').AsString);
        end else
        begin
          if FConNO<>Trim(Order_Main.fieldbyname('ConNo').AsString) then
          begin
            Order_Main.EnableControls;
            Application.MessageBox('合同号不同!','提示',0);
            Exit;
          end;
        end;
      end;
      Next;
    end;
  end;
  Order_Main.EnableControls;
  ModalResult:=1;
end;

end.
