unit U_SXFHList;

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, cxPC, cxButtonEdit,
  RM_E_llPDF, cxDropDownEdit, cxTextEdit, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, dxBarBuiltInMenu, dxSkinsCore, 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, dxSkinsDefaultPainters,
  dxSkinValentine, dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue,
  dxSkinscxPCPainter;

type
  TfrmSXFHList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    Panel1: TPanel;
    Label1: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    MPRTCodeName: TEdit;
    OrderNo: TEdit;
    MPRTKZ: TEdit;
    MPRTMF: TEdit;
    Label2: TLabel;
    Label7: TLabel;
    Label10: TLabel;
    CPName: TEdit;
    DQFactoryName: TEdit;
    Label3: TLabel;
    SPName: TEdit;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Tv1Column4: TcxGridDBColumn;
    Label4: TLabel;
    filler: TEdit;
    Tv1Column5: TcxGridDBColumn;
    Label5: TLabel;
    FromFactoryName: TEdit;
    cxTabControl1: TcxTabControl;
    Thd: TToolButton;
    Tnohd: TToolButton;
    Tv1Column6: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    //RMllPDFExport1: TRMllPDFExport;
    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 FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ThdClick(Sender: TObject);
    procedure TnohdClick(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
  private
    procedure InitGrid();
    procedure InitForm();
    procedure SetStatus();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmSXFHList: TfrmSXFHList;

implementation

uses
  U_DataLink, U_Fun;

{$R *.dfm}

procedure TfrmSXFHList.FormDestroy(Sender: TObject);
begin
  frmSXFHList := nil;
end;

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

procedure TfrmSXFHList.FormCreate(Sender: TObject);
begin
  cxgrid1.Align := alClient;
end;

procedure TfrmSXFHList.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('纱线外发查询', Tv1, '发货管理');
end;

procedure TfrmSXFHList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      Sql.Add(' select A.*,B.SPName,B.PiQty,FHQty=B.Qty,B.FromFactoryName ');
      sql.Add(' from Ck_YarnDeliver A');
      sql.Add(' left join CK_SXPB_CR B on A.FHID=B.CKorderNo');
      sql.Add(' where 1=1');
      begin
        sql.Add(' and FHDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
        sql.Add(' and FHDate<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
        SQL.Add(' and isnull(A.FHType,'''')=''纱线'' ');
      end;
      case cxTabControl1.TabIndex of
        0:
          begin
            Sql.Add(' and isnull(A.status,''0'')=''0'' ');
          end;
        1:
          begin
            Sql.Add(' and isnull(A.status,''0'')=''1'' ');
          end;

      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmSXFHList.InitForm();
begin
  ReadCxGrid('纱线外发查询', Tv1, '发货管理');

  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 60;
  if Trim(DParameters1) = '高权限' then
  begin
    cxTabControl1.Visible := True;
  end;
  InitGrid();

end;

procedure TfrmSXFHList.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 TfrmSXFHList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv1, ADOQueryMain, '纱线外发');
end;

procedure TfrmSXFHList.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
  TBFind.Click;
end;

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

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

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

procedure TfrmSXFHList.CustomerNoNameChange(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 TfrmSXFHList.SetStatus();
begin
  Tnohd.Visible := false;
  Thd.Visible := false;

  case cxTabControl1.TabIndex of
    0:
      begin
        Thd.Visible := true;
        Tnohd.Visible := false;
      end;
    1:
      begin
        Tnohd.Visible := true;
        Thd.Visible := false;
      end;
  end;
end;

procedure TfrmSXFHList.cxTabControl1Change(Sender: TObject);
begin
  SetStatus();
  InitGrid();
end;

procedure TfrmSXFHList.ThdClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    exit;
  if Application.MessageBox('确定要进行数据核对吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      First;
      while Order_Main.Locate('SSel', True, []) do
      begin
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('update  Ck_YarnDeliver SET status=''1'' ');
          sql.Add('where FHId=' + quotedstr(trim(Order_Main.fieldbyname('FHId').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('FHId').AsString))));
          sql.Add(',' + quotedstr(trim('成功')));
          sql.Add(')');
          execsql;
        end;
        Order_Main.Delete;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;

end;

procedure TfrmSXFHList.TnohdClick(Sender: TObject);
begin
//  if Order_Main.IsEmpty then
//    exit;
//  try
//    with ADOQueryCmd do
//    begin
//      close;
//      sql.Clear;
//      sql.Add('update  Ck_YarnDeliver SET status=''0'' ');
//      sql.Add('where FHId=' + quotedstr(trim(Order_Main.fieldbyname('FHId').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('FHId').AsString))));
//      sql.Add(',' + quotedstr(trim('成功')));
//      sql.Add(')');
//      execsql;
//    end;
//    application.MessageBox('纱线外发撤销核对成功！', '提示信息');
//    TBRafresh.Click;
//  except                             
//    application.MessageBox('纱线外发撤销核对失败！', '提示信息', 0);
//  end;
  if Order_Main.IsEmpty then                            
    exit;
  if Application.MessageBox('确定要进行撤销核对吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      First;                                           
      while Order_Main.Locate('SSel', True, []) do
      begin
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('update  Ck_YarnDeliver SET status=''0'' ');
          sql.Add('where FHId=' + quotedstr(trim(Order_Main.fieldbyname('FHId').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('FHId').AsString))));
          sql.Add(',' + quotedstr(trim('成功')));
          sql.Add(')');
          execsql;
        end;
        Order_Main.Delete;
      end;
    end;
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    Order_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作异常!', '提示', 0);
  end;
end;

procedure TfrmSXFHList.N3Click(Sender: TObject);
begin
  with Order_Main do
  begin
    first;
    while not Eof do
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('update  Ck_YarnDeliver SET status=''1'' ');
        sql.Add('where FHId=' + quotedstr(trim(Order_Main.fieldbyname('FHId').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('FHId').AsString))));
        sql.Add(',' + quotedstr(trim('成功')));
        sql.Add(')');
        execsql;
      end;
      Next;
    end;
  end;
end;

procedure TfrmSXFHList.N1Click(Sender: TObject);
var
  i: integer;
begin
  Screen.Cursor := crHourGlass;
  tv1.DataController.GotoFirst;
  for i := 0 to tv1.DataController.FilteredRecordCount - 1 do
  begin
    tv1.DataController.GetItemByFieldName('ssel').EditValue := True;
    tv1.DataController.GotoNext;
  end;
  Screen.Cursor := crDefault;
end;

procedure TfrmSXFHList.N2Click(Sender: TObject);
var
  i: integer;
begin
  Screen.Cursor := crHourGlass;
  tv1.DataController.GotoFirst;
  for i := 0 to tv1.DataController.FilteredRecordCount - 1 do
  begin
    tv1.DataController.GetItemByFieldName('ssel').EditValue := False;
    tv1.DataController.GotoNext;
  end;
  Screen.Cursor := crDefault;
end;

end.

