unit U_DyePlanCardListSEL;

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, cxCheckBox, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, dxBarBuiltInMenu, dxSkinsCore, dxSkinsDefaultPainters,
  dxDateRanges, IdExplicitTLSClientServerBase, U_Basehelp, U_BaseList;

type
  TfrmDyePlanCardListsel = class(TfrmBasehelp)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label1: TLabel;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    GPM_1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_1: TDataSource;
    Label3: TLabel;
    OrderNo: TEdit;
    TBExport: TToolButton;
    v1OrderNo: TcxGridDBColumn;
    v1OrdDate: TcxGridDBColumn;
    v1DeliveryDate: TcxGridDBColumn;
    v1ConNo: TcxGridDBColumn;
    v1MPRTSpec: TcxGridDBColumn;
    CDS_1: TClientDataSet;
    RM1: TRMGridReport;
    RMDB_1: TRMDBDataSet;
    v1CustomerNoName: TcxGridDBColumn;
    Label4: TLabel;
    CustName: TEdit;
    v1MPRTCodeName: TcxGridDBColumn;
    v1MPRTMF: TcxGridDBColumn;
    Label8: TLabel;
    C_Name: TEdit;
    Label9: TLabel;
    ConNo: TEdit;
    v1MPRTKZ: TcxGridDBColumn;
    Label10: TLabel;
    C_Spec: TEdit;
    Label12: TLabel;
    MPRTKZ: TEdit;
    ADOQueryPrint: TADOQuery;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    ThreeColorBase: TcxStyleRepository;
    SHuangSe: TcxStyle;
    SkyBlue: TcxStyle;
    Default: TcxStyle;
    QHuangSe: TcxStyle;
    Red: TcxStyle;
    FontBlue: TcxStyle;
    TextSHuangSe: TcxStyle;
    FonePurple: TcxStyle;
    FoneClMaroon: TcxStyle;
    FoneRed: TcxStyle;
    RowColor: TcxStyle;
    handBlack: TcxStyle;
    cxBlue: TcxStyle;
    cxStyle1: TcxStyle;
    v1Column12: TcxGridDBColumn;
    PCID: TEdit;
    Label2: TLabel;
    v1Column13: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    C_ColorNo: TEdit;
    Label5: TLabel;
    C_Color: TEdit;
    Label6: TLabel;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    Tv1Column20: TcxGridDBColumn;
    ToolButton6: TToolButton;
    cxTabControl1: TcxTabControl;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    ToolButton1: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBPrintClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TchkClick(Sender: TObject);
    procedure TnochkClick(Sender: TObject);
    procedure Label2DblClick(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure tbeditClick(Sender: TObject);
    procedure btnWCClick(Sender: TObject);
    procedure btnnowcClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    DQdate: TDateTime;
    procedure InitGrid();
    procedure SetStatus();
    procedure InitForm();
//    procedure SetStatus();
    procedure PrintCar(MPCID: string);
    { Private declarations }
  public
    FFInt, FCloth: Integer;
    canshu1, canshu2, FWorkshop: string;
    fFlileFlag: string;
    { Public declarations }
  end;

var
  frmDyePlanCardListsel: TfrmDyePlanCardListsel;

implementation

uses
  U_DataLink, U_RTFun;
{$R *.dfm}

procedure TfrmDyePlanCardListsel.PrintCar(MPCID: string);
var
  fPrintFile: string;
begin
  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    sql.Add('exec P_Dye_Card_Prt ');
    sql.Add(' @PCID=' + quotedstr(trim(MPCID)));
    Open;
  end;

  ExportFtErpFile('染色流程卡.rmf', ADOQueryTemp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\染色流程卡.rmf';

  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.PrintReport;
    sleep(200);
    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  Dye_Plan_Card SET status=''9'' ');
      sql.Add('where PCID=' + quotedstr(trim(CDS_1.fieldbyname('PCID').AsString)));
      execsql;
    end;
  end
  else
    Application.MessageBox(PChar('没有找到' + trim(fPrintFile)), '提示信息', 0);
end;

procedure TfrmDyePlanCardListsel.SetStatus();
begin

end;

procedure TfrmDyePlanCardListsel.FormDestroy(Sender: TObject);
begin
  inherited;
  frmDyePlanCardListsel := nil;
end;

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

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

end;

procedure TfrmDyePlanCardListsel.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmDyePlanCardListsel.InitGrid();
var
  strwhere: string;
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      Filtered := False;
      sql.Clear;
      sql.Add('select A.*,ConNo,OrdType,Salestype ,OrdDate,DlyDate');
      sql.Add(',GlideList=[dbo].[F_Dye_Card_GlideList](A.PCID) ');
      sql.Add('from Dye_Plan_Card A ');
      sql.Add('left join Dye_Plan_sub B on B.subID=A.subID ');
      sql.Add('left join Dye_Plan_Main C on C.mainID=A.mainID ');
      sql.Add('where A.PCTime>=''' + formatdateTime('yyyy-MM-dd', begdate.Date) + ''' ');
      sql.Add('and A.PCTime<''' + formatdateTime('yyyy-MM-dd', enddate.Date + 1) + ''' ');
      case cxTabControl1.TabIndex of
        0:
          begin
            sql.Add('  and  isnull(A.status,''0'')= ''0'' ');
          end;
        1:
          begin
            sql.Add('  and  isnull(A.status,''0'')=''9'' ');
          end;
        2:
          begin
            sql.Add('  and  isnull(A.status,''0'')=''10'' ');
          end;

      end;
//      if trim(FWorkshop) <> '' then
//        sql.Add(' and C.Workshop=''' + trim(FWorkshop) + ''' ');
//      if Trim(canshu1) = '' then
//        sql.Add(' and A.Filler=''' + trim(DName) + ''' ');

      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
    TBFind.Click;
  end;
end;

procedure TfrmDyePlanCardListsel.Label2DblClick(Sender: TObject);
var
  I: Integer;
  Column: TcxGridDBColumn;
  FieldNames: TStringList;
begin
  FieldNames := TStringList.Create;
  try
    for I := 0 to Tv1.ColumnCount - 1 do
    begin
      Column := Tv1.Columns[I];
      FieldNames.Add(Column.Caption + ':' + Column.DataBinding.FieldName);
    end;
    ShowMessage(FieldNames.Text);
  finally
    FieldNames.Free;
  end;
end;

procedure TfrmDyePlanCardListsel.InitForm();
begin
  ReadCxGrid(self.Caption + 'TV1', Tv1, '生产指示单管理');
  BegDate.DateTime := SGetServerDate(ADOQueryTemp) - 3;
  EndDate.DateTime := SGetServerDate(ADOQueryTemp);

  SetStatus();

  InitGrid()
end;

procedure TfrmDyePlanCardListsel.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
end;

procedure TfrmDyePlanCardListsel.TBDelClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  if (Trim(CDS_1.fieldbyname('Filler').AsString) <> Trim(DName)) and (dname <> 'ADMIN') then
  begin
    Application.MessageBox('不能操作他人的数据!', '提示', 0);
    Exit;
  end;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    with CDS_1 do
    begin
      DisableControls;
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          try
            ADOQueryCmd.Connection.BeginTrans;
            with ADOQueryCmd do
            begin
              Close;
              Sql.Clear;
              Sql.Add('exec P_Dye_Card_Del ');
              Sql.Add('@PCIDS=' + quotedstr(Trim(CDS_1.fieldbyname('PCID').AsString)));
              Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
              Sql.Add(',@DName=' + quotedstr(Trim(DName)));
              Open;
            end;
            if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
              raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));

            if (Trim(CDS_1.fieldbyname('PCType').AsString) = '正常卡') or (Trim(CDS_1.fieldbyname('PCType').AsString) = '前道卡') then
            begin
//              with ADOQueryCmd do
//              begin
//                Close;
//                Sql.Clear;
//                Sql.Add('exec P_Dye_GreyCloth_Out_Del ');
//                Sql.Add('@PCID=' + quotedstr(Trim(CDS_1.fieldbyname('PCID').AsString)));
//                Open;
//              end;
//              if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
//                raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
            end
            else
            begin
              with ADOQueryCmd do
              begin
                Close;
                Sql.Clear;
                Sql.Add('exec P_Dye_Card_Move_Del ');
                Sql.Add('@PCID=' + quotedstr(Trim(CDS_1.fieldbyname('PCID').AsString)));
                Open;
              end;
              if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
                raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
            end;

            ADOQueryCmd.Connection.CommitTrans;
          except
            ADOQueryCmd.Connection.RollbackTrans;
            raise Exception.Create(PChar(Exception(ExceptObject).Message));
          end;
        end;
        Next;
      end;
      EnableControls;
    end;
    TBRafresh.Click;
  except
    CDS_1.EnableControls;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

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

procedure TfrmDyePlanCardListsel.TBPrintClick(Sender: TObject);
var
  i: integer;
  MPCID: string;
begin
  if CDS_1.IsEmpty then
    Exit;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  i := 0;
  with CDS_1 do
  begin
    DisableControls;
    First;
    while not Eof do
    begin
      if Fieldbyname('Ssel').AsBoolean then
      begin
        if i = 0 then
          MPCID := trim(CDS_1.fieldbyname('PCID').AsString);
        PrintCar(trim(CDS_1.fieldbyname('PCID').AsString));
        i := i + 1;
      end;
      Next;
    end;
    EnableControls;
  end;
  SelOKNoFiler(Tv1, False);
  CDS_1.Locate('PCID', MPCID, []);
end;

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

procedure TfrmDyePlanCardListsel.OrderNoChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
end;

procedure TfrmDyePlanCardListsel.FormShow(Sender: TObject);
begin
  inherited;
  fFlileFlag := UserDataFlag + 'HX';

  InitForm();
end;

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

procedure TfrmDyePlanCardListsel.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_1, true);
end;

procedure TfrmDyePlanCardListsel.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_1, False);
end;

procedure TfrmDyePlanCardListsel.tbeditClick(Sender: TObject);
var
  MPCIDS: string;
begin

  with CDS_1 do
  begin
    DisableControls;
    First;
    while not Eof do
    begin
      if Fieldbyname('Ssel').AsBoolean then
      begin
        MPCIDS := MPCIDS + trim(CDS_1.FieldByName('PCID').AsString) + ';';
      end;
      Next;
    end;
    EnableControls;
  end;

end;

procedure TfrmDyePlanCardListsel.btnWCClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    exit;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_1 do
    begin
      DisableControls;
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  Dye_Plan_Card SET status=''10'' ');
            sql.Add('where PCID=' + quotedstr(trim(CDS_1.fieldbyname('PCID').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(CDS_1.FieldByName('PCID').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            execsql;
          end;
        end;
        Next;
      end;
      EnableControls;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('操作成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmDyePlanCardListsel.btnnowcClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    exit;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_1 do
    begin
      DisableControls;
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  Dye_Plan_Card SET status=''9'' ');
            sql.Add('where PCID=' + quotedstr(trim(CDS_1.fieldbyname('PCID').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(CDS_1.FieldByName('PCID').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            execsql;
          end;
        end;
        Next;
      end;
      EnableControls;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('操作成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('操作失败！', '提示信息', 0);
  end;
end;

procedure TfrmDyePlanCardListsel.ToolButton1Click(Sender: TObject);
begin
  inherited;
  ModalResult := 1;
end;

procedure TfrmDyePlanCardListsel.ToolButton6Click(Sender: TObject);
begin
  WriteCxGrid(self.Caption + 'TV1', Tv1, '生产指示单管理');

end;

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

procedure TfrmDyePlanCardListsel.TchkClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    exit;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_1 do
    begin
      DisableControls;
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  Dye_Plan_Card SET status=''9'' ');
            sql.Add('where PCID=' + quotedstr(trim(CDS_1.fieldbyname('PCID').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(CDS_1.FieldByName('PCID').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            execsql;
          end;
        end;
        Next;
      end;
      EnableControls;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('审核成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('审核失败！', '提示信息', 0);
  end;
end;

procedure TfrmDyePlanCardListsel.TnochkClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    exit;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_1 do
    begin
      DisableControls;
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('update  Dye_Plan_Card SET status=''0'' ');
            sql.Add('where PCID=' + quotedstr(trim(CDS_1.fieldbyname('PCID').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(CDS_1.FieldByName('PCID').AsString))));
            sql.Add(',' + quotedstr(trim('成功')));
            sql.Add(')');
            execsql;
          end;
        end;
        Next;
      end;
      EnableControls;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    application.MessageBox('撤销审核成功！', '提示信息');
    TBRafresh.Click;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('撤销审核失败！', '提示信息', 0);
  end;
end;

end.

