unit U_JWLOrderList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid, cxMemo,
  cxRichEdit, ComCtrls, cxContainer, cxTextEdit, cxMaskEdit, cxButtonEdit,
  StdCtrls, ToolWin, DBClient, ADODB, ExtCtrls, BtnEdit, cxCalendar, StrUtils,
  cxCurrencyEdit, cxImage, cxDBEdit, Menus, RM_Common, RM_Class, RM_e_Xls,
  RM_Dataset, RM_System, RM_GridReport, cxGridCustomPopupMenu, cxGridPopupMenu,
  cxPC, cxSplitter, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdFTP, ShellAPI, cxDropDownEdit, cxCheckBox, RM_e_Graphic, RM_e_Jpeg,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu, Math,
  dxSkinsCore, dxSkinsDefaultPainters, dxSkinscxPCPainter, dxSkinBlack,
  dxSkinBlue, dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkRoom,
  dxSkinDarkSide, dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle,
  dxSkinFoggy, dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary,
  dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin,
  dxSkinMetropolis, dxSkinMetropolisDark, dxSkinMoneyTwins,
  dxSkinOffice2007Black, dxSkinOffice2007Blue, dxSkinOffice2007Green,
  dxSkinOffice2007Pink, dxSkinOffice2007Silver, dxSkinOffice2010Black,
  dxSkinOffice2010Blue, dxSkinOffice2010Silver, dxSkinOffice2013DarkGray,
  dxSkinOffice2013LightGray, dxSkinOffice2013White, dxSkinPumpkin,
  dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSharpPlus,
  dxSkinSilver, dxSkinSpringTime, dxSkinStardust, dxSkinSummer2008,
  dxSkinTheAsphaltWorld, dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint,
  dxSkinXmas2008Blue;

type
  TfrmJWLOrderList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    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;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    v1MPRTCodeName: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N2: TMenuItem;
    TBCK: TToolButton;
    Label8: TLabel;
    MPRTCodeName: TEdit;
    Label11: TLabel;
    MPRTCode: TEdit;
    Label12: TLabel;
    MPRTKZ: TEdit;
    Label13: TLabel;
    KHConNo: TEdit;
    btnCopy: TToolButton;
    ADOQueryPrint: TADOQuery;
    CDS_Print: TClientDataSet;
    v1Column2: TcxGridDBColumn;
    cxSplitter1: TcxSplitter;
    ADOQuerySub: TADOQuery;
    DataSource2: TDataSource;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    Panel2: TPanel;
    ADOQueryImage: TADOQuery;
    DSImage: TDataSource;
    IdFTP1: TIdFTP;
    RMDBMX: TRMDBDataSet;
    CDSPRTMX: TClientDataSet;
    CDSPRTHZ: TClientDataSet;
    RMDBHZ: TRMDBDataSet;
    RM2: TRMGridReport;
    v1Column14: TcxGridDBColumn;
    N1: TMenuItem;
    v1Column16: TcxGridDBColumn;
    RMJPEGExport1: TRMJPEGExport;
    RMDB_Main2: TRMDBDataSet;
    CDS_Main2: TClientDataSet;
    CDS_Main3: TClientDataSet;
    RMDB_Main3: TRMDBDataSet;
    ADOQuery1: TADOQuery;
    v1Column17: TcxGridDBColumn;
    Label6: TLabel;
    YWY: TEdit;
    ORDER_SUB: TClientDataSet;
    PopupMenu2: TPopupMenu;
    N3: TMenuItem;
    N4: TMenuItem;
    RM3: TRMGridReport;
    yin1: TMenuItem;
    N5: TMenuItem;
    N6: TMenuItem;
    ADOQuery2: TADOQuery;
    Label1: TLabel;
    FILLER: TEdit;
    Tv1Column6: TcxGridDBColumn;
    Label5: TLabel;
    ToolButton1: TToolButton;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: 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 TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure OrderNoMChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBTPClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure TBCKClick(Sender: TObject);
    procedure btnCopyClick(Sender: TObject);
    procedure OrderNoKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure TRZClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure N4Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
    procedure CustomerNoNameKeyPress(Sender: TObject; var Key: Char);
  private
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function DelData(): Boolean;
    procedure InitGridFH();
    procedure InitSub();
    procedure SetStatus();
   
    { Private declarations }
  public
    FFInt, FCloth: Integer;
    canshu1, canshu2: string;
    fFlileFlag: string;
    FNOTE: string;
    { Public declarations }
  end;

//var   TV2
//  frmProductOrderList: TfrmProductOrderList;

implementation

uses
  U_DataLink, U_JWLORDERInPut, U_Fun, U_SysLogHelp, U_ZDYHelp;

{$R *.dfm}

procedure TfrmJWLOrderList.SetStatus();
begin
//  btnCopy.Visible := false;
//  tbadd.Visible := false;
//  TBEdit.Visible := False;
//
//  TBDel.Visible := False;
//  if Trim(canshu1) = '查询' then
//    exit;

end;

procedure TfrmJWLOrderList.InitSub();
begin
  ADOQuerySub.Close;
  if Order_Main.IsEmpty then
    exit;
  ADOQuerySub.DisableControls;
  with ADOQuerySub do
  begin
    close;
    sql.Clear;
    sql.Add('select *  ');
    sql.Add(' from FLOrder_sub A');
    sql.Add('where mainid =' + quotedstr((Order_Main.fieldbyname('mainID').AsString)));
    open;
  end;
  SCreateCDS20(ADOQuerySub, ORDER_SUB);
  SInitCDSData20(ADOQuerySub, ORDER_SUB);

  ADOQuerySub.EnableControls;
end;

procedure TfrmJWLOrderList.FormDestroy(Sender: TObject);
begin
//  frmProductOrderList:=nil;
end;

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

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

end;

procedure TfrmJWLOrderList.TBCloseClick(Sender: TObject);
begin

  WriteCxGrid('指示单列表FF1', Tv1, '生产指示单管理');
  WriteCxGrid('指示单列表FFTv21', Tv2, '生产指示单管理6');
  Close;

end;

procedure TfrmJWLOrderList.InitGrid();
var
  STRwhere, Pwhere: string;
begin
  STRwhere := '';
  //if Trim(canshu1) = '录入' then
  Pwhere := SGetFilters(Panel1, 1, 2);

  begin
    if trim(Pwhere) <> '' then
      STRwhere := STRwhere + ' and ' + trim(Pwhere);
  end;

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.add('select *    ');
      SQL.Add(',ZMS=(select Sum(ms) from florder_SUB B where B.MainId=A.MainId group by B.MainId)');
      SQL.Add(' ,YSSL=(select SUM(1)    from florder_SUB B where B.MainId=A.MainId)');
      SQL.Add(' ,ZJS=(select  Sum(PRTOrderQty) from florder_SUB B where B.MainId=A.MainId group by B.MainId)');
      SQL.Add('from florder_main A');
      SQL.Add('where  A.OrdDate>=''' + FormatDateTime('yyyy-MM-dd', BegDate.DateTime) + '''');
      SQL.Add('and  A.OrdDate<''' + FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1) + '''');
      SQL.Add(STRwhere);
//        ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmJWLOrderList.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 TfrmJWLOrderList.InitForm();
begin
  ReadCxGrid('指示单列表FF1', Tv1, '生产指示单管理');

  ReadCxGrid('指示单列表FFTv21', Tv2, '生产指示单管理6');
  BegDate.DateTime := SGetServerDate10(ADOQueryTemp) - 180;
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);

  SetStatus();
  InitGrid();
end;

procedure TfrmJWLOrderList.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;

procedure TfrmJWLOrderList.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;


  { with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    SQL.Add('select * from CK_SXPB_CR A  ');
    SQL.Add('where ORDMainIdCK='''+Trim(Order_Main.fieldbyname('MainId').AsString)+''' ');
    SQL.Add('and  ckName<>''坯布'' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('已产生数据不能修改!','提示',0);
    Exit;
  end; }
  try
    frmjwlInPut := TfrmjwlInPut.Create(Application);
    with frmjwlInPut do
    begin

      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      FOrderNo := Trim(Self.Order_Main.fieldbyname('OrderNo').AsString);
      canshu1 := self.canshu1;
//      conNo.Enabled := false;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmjwlInPut.Free;
  end;
//  self.TBRafresh.Click;
end;

procedure TfrmJWLOrderList.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;

//  with ADOQueryTemp do
//  begin
//    Close;
//    sql.Clear;
//    SQL.Add('select * from CK_SXPB_CR A  ');
//    SQL.Add('where ORDMainIdCK='''+Trim(Order_Main.fieldbyname('MainId').AsString)+''' ');
//    SQL.Add('or ORDMainIdRK='''+Trim(Order_Main.fieldbyname('MainId').AsString)+''' ');
//    Open;
//  end;
//  if ADOQueryTemp.IsEmpty=False then
//  begin
//    Application.MessageBox('已产生数据不能删除!','提示',0);
//    Exit;
//  end;
//  with ADOQueryTemp do
//  begin
//    Close;
//    sql.Clear;
//    SQL.Add('select * from CK_PBMX_CR A  ');
//    SQL.Add('where ToMainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+''' ');
//    SQL.Add('and CKName=''坯布'' ');
//    Open;
//  end;
//  if ADOQueryTemp.IsEmpty=False then
//  begin
//    Application.MessageBox('已产生数据不能删除!','提示',0);
//    Exit;
//  end;
//  with ADOQueryTemp do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add('select * from YF_Money_CR where MainId='''+Trim(Order_Main.fieldbyname('MainId').AsString)+'''');
//    Open;
//  end;
//  if ADOQueryTemp.IsEmpty=False 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 TfrmJWLOrderList.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete FLOrder_Main where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
      sql.Add('delete FLOrder_Sub 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 TfrmJWLOrderList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv1, ADOQueryMain, '生产指示单列表');
end;

procedure TfrmJWLOrderList.TBPrintClick(Sender: TObject);
begin

  if Order_Main.IsEmpty then
    Exit;

end;

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

procedure TfrmJWLOrderList.TBAddClick(Sender: TObject);
var
  maxno: string;
begin
  try
    frmjwlInPut := TfrmJWLInPut.Create(Application);
    with frmjwlInPut do
    begin
      PState := 0;

      FMainId := '';
      canshu1 := self.canshu1;
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmjwlInPut.Free;
  end;
end;

procedure TfrmJWLOrderList.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 TfrmJWLOrderList.FormShow(Sender: TObject);
begin

  InitForm();
end;

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

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

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

procedure TfrmJWLOrderList.TBCKClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmjwlInPut := TfrmjwlInPut.Create(Application);
    with frmjwlInPut do
    begin
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      ToolBar2.Visible := False;
      TBSave.Visible := False;
      ScrollBox1.Enabled := False;
      cxGrid1.Enabled := False;

      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmjwlInPut.Free;
  end;
end;

procedure TfrmJWLOrderList.btnCopyClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmjwlInPut := TfrmJWLInPut.Create(Application);
    with frmjwlInPut do
    begin
      PState := 1;
      CopyInt := 99;
      FMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      canshu1 := self.canshu1;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmjwlInPut.Free;
  end;
end;

procedure TfrmJWLOrderList.OrderNoKeyPress(Sender: TObject; var Key: Char);
var
  STRwhere, Pwhere: string;
begin
  if Key = #13 then
  begin
    INITGRID();
  end;
end;

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

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

procedure TfrmJWLOrderList.ToolButton1Click(Sender: TObject);
var
  fPrintFile: string;
  Porderno, LBName, SYRName: string;
  i, j: Integer;
  OrderKg: Double;
begin
  if Order_Main.IsEmpty then
    Exit;

  LBName := '辅料指示单';
  ExportFtErpFile(Trim(LBName) + '.rmf', ADOQueryTemp);
  with ADOQueryPrint do
  begin
    Close;
    sql.Clear;
    sql.add('select *    ');
    SQL.Add(',ZMS=(select Sum(ms) from florder_SUB B where B.MainId=A.MainId group by B.MainId)');
    SQL.Add(' ,YSSL=(select SUM(1)    from florder_SUB B where B.MainId=A.MainId)');
    SQL.Add(' ,ZJS=(select  Sum(PRTOrderQty) from florder_SUB B where B.MainId=A.MainId group by B.MainId)');
    SQL.Add('from florder_main A inner join florder_sub B ON A.MAINID=B.MAINID');
    SQL.Add('WHERE A.MAINID= ' + quotedstr(trim(Order_Main.fieldbyname('MainID').AsString)));
    sql.Add('order by PRTColorNoKH');

    Open;
  end;

  SCreateCDS20(ADOQueryPrint, CDS_Print);
  SInitCDSData20(ADOQueryPrint, CDS_Print);

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RMVariables['SYRName'] := Trim(SYRName);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\' + Trim(LBName) + '.rmf'), '提示', 0);
  end;

end;

procedure TfrmJWLOrderList.N2Click(Sender: TObject);
var
  maxno: string;
begin
  if Order_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('请选择数据!', '提示', 0);
    Exit;
  end;

end;

procedure TfrmJWLOrderList.N3Click(Sender: TObject);
begin
  SelOKNo(ORDER_SUB, True);
end;

procedure TfrmJWLOrderList.N4Click(Sender: TObject);
begin
  SelOKNo(ORDER_SUB, False);
end;

procedure TfrmJWLOrderList.ComboBox1Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmJWLOrderList.CustomerNoNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    INITGRID();
  end;
end;

end.

