unit U_ShuttleLSSCList;

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, cxDropDownEdit, 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
  TfrmShuttleLSSCList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_Main: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMXLSExport1: TRMXLSExport;
    TBCopy: TToolButton;
    TBView: TToolButton;
    RMDB_Main: TRMDBDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column18: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxTabControl1: TcxTabControl;
    v1Column1: TcxGridDBColumn;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    Panel1: TPanel;
    v1Column4: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure LSNOChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBViewClick(Sender: TObject);
    procedure TBCopyClick(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure machnoChange(Sender: TObject);
    procedure v1Column4PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column11PropertiesEditValueChanged(Sender: TObject);
    procedure v1Column17PropertiesEditValueChanged(Sender: TObject);
    procedure Y_TypeChange(Sender: TObject);
    procedure machstatusChange(Sender: TObject);
    procedure v1Column18PropertiesEditValueChanged(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
  private
    FInt, PFInt: Integer;
    procedure InitGrid();
    procedure SetStatus();
    procedure InitForm();
    function DelData(): Boolean;
    procedure InitButton();
    { Private declarations }
  public
    canshu1, FCheJian: string;
    { Public declarations }
  end;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_ShuttleLSSCInPut, U_JTSel;

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

end;

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

procedure TfrmShuttleLSSCList.InitButton();
begin
  TBAdd.Visible := True;
  TBCopy.Visible := True;
  TBEdit.Visible := True;
  TBDel.Visible := True;
  TBView.Visible := True;

  if Trim(canshu1) = '查询' then
  begin
    TBAdd.Visible := False;
    TBCopy.Visible := False;
    TBEdit.Visible := False;
    TBDel.Visible := False;
  end;

end;

procedure TfrmShuttleLSSCList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('品名管理', Tv1, '加弹丝');
  Close;
end;

procedure TfrmShuttleLSSCList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;

    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select A.* ');
      Sql.Add(' ,ZGYSHZ=CAST((select  distinct(RName) +  ''  '' from YT_Plan_Rule X where X.MainID=A.MainID and RType=''颜色'' and isnull(RName,'''')<>'''' for xml path('''') ) AS VARCHAR(100))');
      Sql.Add(' ,SCJTHZ=CAST((select  distinct(RName) +  ''  '' from YT_Plan_Rule X where X.MainID=A.MainID and RType=''机台'' and isnull(RName,'''')<>'''' for xml path('''') ) AS VARCHAR(100))');
      sql.Add(' from  YT_Plan_Main A');
      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add(' where isnull(Status,''0'')=''0'' ');
          end;
        1:
          begin
            sql.Add(' where isnull(Status,''0'')=''1'' ');
          end;
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain, Order_Main);
    SInitCDSData20(ADOQueryMain, Order_Main);

  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmShuttleLSSCList.InitForm();
begin
  InitButton();
  ReadCxGrid('品名管理', Tv1, '加弹丝');
  InitGrid();
end;

procedure TfrmShuttleLSSCList.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 TfrmShuttleLSSCList.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmShuttleLSSCInPut := TfrmShuttleLSSCInPut.Create(Application);
    with frmShuttleLSSCInPut do
    begin
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
//      FSubId := Trim(Self.Order_Main.fieldbyname('SubId').AsString);
      if ShowModal = 1 then
      begin
        InitGrid();
        Self.Order_Main.Locate('MainId', frmShuttleLSSCInPut.FMainId, []);
      end;
    end;
  finally
    frmShuttleLSSCInPut.Free;
  end;
end;

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

  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if DelData() then
  begin
//    Order_Main.Delete;
    initgrid();
  end;
end;

function TfrmShuttleLSSCList.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete YT_Plan_Main where MainId=''' + Trim(Order_Main.fieldbyname('MainId').AsString) + '''');
      ExecSQL;
    end;

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

procedure TfrmShuttleLSSCList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel('拉丝单', cxGrid1);
end;

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

procedure TfrmShuttleLSSCList.TBAddClick(Sender: TObject);
begin
  try
    frmShuttleLSSCInPut := TfrmShuttleLSSCInPut.Create(Application);
    with frmShuttleLSSCInPut do
    begin
      PState := 0;
      FMainId := '';
      FSubId := '';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmShuttleLSSCInPut.Free;
  end;
end;

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

procedure TfrmShuttleLSSCList.TBViewClick(Sender: TObject);
begin
  try
    frmShuttleLSSCInPut := TfrmShuttleLSSCInPut.Create(Application);
    with frmShuttleLSSCInPut do
    begin
      PState := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
//      FSubId := Trim(Self.Order_Main.fieldbyname('SubId').AsString);
      TBSave.Visible := False;

      ScrollBox1.Enabled := False;
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmShuttleLSSCInPut.Free;
  end;
end;

procedure TfrmShuttleLSSCList.TBCopyClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmShuttleLSSCInPut := TfrmShuttleLSSCInPut.Create(Application);
    with frmShuttleLSSCInPut do
    begin
      PState := 1;
      FCopyInt := 1;
      FMainId := Trim(Self.Order_Main.fieldbyname('MainId').AsString);
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmShuttleLSSCInPut.Free;
  end;
end;

procedure TfrmShuttleLSSCList.cxPageControl1Change(Sender: TObject);
begin
  InitButton();
end;

procedure TfrmShuttleLSSCList.machnoChange(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 TfrmShuttleLSSCList.v1Column4PropertiesEditValueChanged(Sender: TObject);
var
  mvalues, yjdate: string;
begin
  mvalues := TCXTextEdit(Sender).Text;

  with order_main do
  begin
    edit;
    fieldbyname('MachSpeed').Value := mvalues;
    post;
  end;
  tv1.Controller.EditingController.ShowEdit();
  if (order_main.FieldByName('SCQty').AsString <> '') and (order_main.FieldByName('machspeed').AsString <> '') then
  begin
    yjdate := format('%.1f', [(order_main.FieldByName('SCQty').AsFloat * 10000) / order_main.FieldByName('machspeed').AsFloat / 60 / 24]);
    with order_main do
    begin
      edit;
      fieldbyname('YJDate').Value := yjdate;
      post;
    end;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YN_Plan_Mach Set MachSpeed=''' + trim(mvalues) + ''', YJDate=''' + trim(yjdate) + ''' ');
    sql.Add('where PMID=''' + Trim(order_main.fieldbyname('pmid').AsString) + '''');
    ExecSQL;
  end;

end;

procedure TfrmShuttleLSSCList.v1Column11PropertiesEditValueChanged(Sender: TObject);
var
  mvalues: string;
begin
  mvalues := TCXTextEdit(Sender).Text;

  with Order_Main do
  begin
    edit;
    fieldbyname('Machstatus').Value := mvalues;
    post;
  end;
  tv1.Controller.EditingController.ShowEdit();

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YN_Plan_Mach Set Machstatus=''' + trim(mvalues) + ''' ');
    sql.Add('where PMID=''' + Trim(Order_Main.fieldbyname('pmid').AsString) + '''');
    ExecSQL;
  end;

end;

procedure TfrmShuttleLSSCList.v1Column17PropertiesEditValueChanged(Sender: TObject);
var
  mvalues, YJDate: string;
begin
  mvalues := TCXTextEdit(Sender).Text;

  with order_main do
  begin
    edit;
    fieldbyname('SCQty').Value := mvalues;
    post;
  end;
  tv1.Controller.EditingController.ShowEdit();

  if (order_main.FieldByName('SCQty').AsString <> '') and (order_main.FieldByName('machspeed').AsString <> '') then
  begin
    YJDate := format('%.1f', [(order_main.FieldByName('SCQty').AsFloat * 10000) / order_main.FieldByName('machspeed').AsFloat / 60 / 24]);
    with order_main do
    begin
      edit;
      fieldbyname('YJDate').Value := YJDate;
      post;
    end;
  end;

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YN_Plan_Mach Set SCQty=''' + trim(mvalues) + ''' , YJDate=''' + trim(YJDate) + ''' ');
    sql.Add('where PMID=''' + Trim(order_main.fieldbyname('pmid').AsString) + '''');
    ExecSQL;
  end;

end;

procedure TfrmShuttleLSSCList.Y_TypeChange(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 TfrmShuttleLSSCList.machstatusChange(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 TfrmShuttleLSSCList.v1Column18PropertiesEditValueChanged(Sender: TObject);
var
  mvalues: string;
begin
  mvalues := TCXTextEdit(Sender).Text;

  with order_main do
  begin
    edit;
    fieldbyname('Y_PC').Value := mvalues;
    post;
  end;
  tv1.Controller.EditingController.ShowEdit();

  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update YN_Plan_Mach Set Y_PC=''' + trim(mvalues) + ''' ');
    sql.Add('where PMID=''' + Trim(order_main.fieldbyname('pmid').AsString) + '''');
    ExecSQL;
  end;

end;

procedure TfrmShuttleLSSCList.ToolButton1Click(Sender: TObject);
begin

  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update YT_Plan_Main set Status=''1'' where MainId=' + quotedstr(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('BatchNo').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    application.MessageBox('操作成功！', '提示信息');

    Order_Main.Delete
  except
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmShuttleLSSCList.ToolButton2Click(Sender: TObject);
begin
  try
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update YT_Plan_Main set Status=''0'' where MainId=' + quotedstr(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('BatchNo').AsString))));
      sql.Add(',' + quotedstr(trim('成功')));
      sql.Add(')');
      execsql;
    end;
    application.MessageBox('操作成功！', '提示信息');

    Order_Main.Delete
  except
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

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

end.

