unit U_WarpRollerCklist;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, StdCtrls, ExtCtrls, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData,
  cxButtonEdit, cxDropDownEdit, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls, cxGridCustomView,
  cxGrid, DBClient, ADODB, cxGridCustomPopupMenu, cxGridPopupMenu, RM_Common,
  RM_Class, RM_e_Xls, RM_GridReport, RM_System, RM_Dataset, cxTextEdit, cxPC,
  cxCheckBox, Menus, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  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
  TfrmWarpRollerCklist = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    TBDel: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Label2: TLabel;
    CRType: TComboBox;
    Label5: TLabel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOPrint: TADOQuery;
    RMXLSExport1: TRMXLSExport;
    JZRKNo: TEdit;
    Label7: TLabel;
    RMDB_Main: TRMDBDataSet;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxStyle_gridRow: TcxStyle;
    cxStyle_gridFoot: TcxStyle;
    cxStyle_gridHead: TcxStyle;
    cxStyle_gridGroupBox: TcxStyle;
    cxStyle_yellow: TcxStyle;
    cxStyle_Red: TcxStyle;
    cxStyle_fontBlack: TcxStyle;
    cxStyle_fontclFuchsia: TcxStyle;
    cxStyle_fontclPurple: TcxStyle;
    cxStyle_fontclGreen: TcxStyle;
    cxStyle_fontclBlue: TcxStyle;
    cxStyle_fontclTeal: TcxStyle;
    cxStyle_fontclOlive: TcxStyle;
    Label8: TLabel;
    ToFactoryName: TEdit;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    PCID: TEdit;
    Label14: TLabel;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Ssel: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1CRTime: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Label3: TLabel;
    Label4: TLabel;
    P_CODE: TEdit;
    edtKaNo: TEdit;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure OrderNoChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure v1Column6CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
  private
    procedure InitGrid();
    procedure InitForm();
    { Private declarations }
  public
    canshu1, canshu2: string;
    { Public declarations }
  end;

var
  frmWarpRollerCklist: TfrmWarpRollerCklist;

implementation

uses
  U_DataLink, U_Fun10, U_ZDYHelp, U_WarpRollerCk;
{$R *.dfm}

procedure TfrmWarpRollerCklist.InitForm();
var
  fsj: string;
begin
  readCxGrid(self.Caption + 'TV1', Tv1, '仓储管理');
  fsj := 'select distinct(CRType) name from CK_WarpBeam_CR where  CRFlag=''出库''   ';
  SInitComBoxBySql(ADOQueryCmd, CRType, False, fsj);
end;

procedure TfrmWarpRollerCklist.InitGrid();
begin
  try
    with adoqueryMain do
    begin
      close;
      sql.Clear;
      sql.Add(' select A.* ,edzdfNo,edtKaNo,C_TouWen,ZJPiLen,C_ShangPai,C_PiE,C_Len,APMachineNo ,edZDFNo ');
      SQL.ADD(',CarNo2=(SELECT   MACHNO FROM BS_Machine k where k.machid= (SELECT top 1 CARNO FROM CK_YgCl E WHERE E.MJID=A.PCID ANd CLTYPE=''上轴''))');
      SQL.Add(',P_CODE=(select TOP 1 P_CODE from PD_Plan_SUB e where e.SUBID=(select TOP 1 SUBID from PD_Plan_Card_MS f where f.pcid=A.pcid))');
      SQL.Add(',P_codeNAME=(select TOP 1 P_codeNAME from PD_Plan_SUB e where e.SUBID=(select TOP 1 SUBID from PD_Plan_Card_MS f where f.pcid=A.pcid))');
      SQL.Add('from CK_WarpBeam_CR A LEFT JOIN PD_PLAN_cARD B ON A.PCID=B.PCID   ');
      sql.Add(' where  A.CRFlag=''出库'' ');
      SQL.Add(' and A.CRTime>=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime))));
      SQL.Add(' and A.CRTime<' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', EndDate.DateTime + 1))));
      SQL.Add(' and A.Valid =''Y'' ');
     // ShowMessage(SQL.Text);
      open;
    end;
    SCreateCDS20(adoqueryMain, CDS_Main);
    SInitCDSData20(adoqueryMain, CDS_Main);
  finally
  end;
end;

procedure TfrmWarpRollerCklist.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := cafree;
end;

procedure TfrmWarpRollerCklist.FormDestroy(Sender: TObject);
begin
  frmWarpRollerCklist := nil;
end;

procedure TfrmWarpRollerCklist.TBAddClick(Sender: TObject);
begin
  frmWarpRollerCk := TfrmWarpRollerCk.create(self);
  with frmWarpRollerCk do
  begin
    fkeyNo := '';
    fType := 0;
    if showmodal = 1 then
    begin
      TBRafresh.Click;
      self.CDS_Main.Locate('crNo', trim(fkeyNo), []);
    end;
    free;
  end;
end;

procedure TfrmWarpRollerCklist.OrderNoChange(Sender: TObject);
begin
  if ADOQuerymain.Active = False then
    Exit;
  SDofilter(ADOQuerymain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQuerymain, CDS_Main);
  SInitCDSData20(ADOQuerymain, CDS_Main);
end;

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

procedure TfrmWarpRollerCklist.FormCreate(Sender: TObject);
begin
  EndDate.Date := SGetServerDate10(ADOQueryTmp);
  BegDate.Date := EndDate.Date;
  cxGrid1.Align := alclient;
  canshu1 := trim(DParameters1);
end;

procedure TfrmWarpRollerCklist.TBCloseClick(Sender: TObject);
begin
  writeCxGrid(self.Caption + 'TV1', Tv1, '仓储管理');
  close;
end;

procedure TfrmWarpRollerCklist.TBEditClick(Sender: TObject);
var
  WSql: string;
begin
  if cds_main.IsEmpty then
    exit;

  WSql := '';
  CDS_Main.DisableControls;
  with CDS_Main do
  begin
    First;
    while CDS_Main.Locate('SSel', True, []) do
    begin
      if WSql <> '' then
        WSql := WSql + '' + ',' + QuotedStr(Trim(CDS_Main.fieldbyname('JZRKID').AsString))
      else
        WSql := ' in (' + QuotedStr(Trim(CDS_Main.fieldbyname('JZRKID').AsString));
      Edit;
      FieldByName('SSel').Value := False;
      Post;
    end;
  end;
  CDS_Main.EnableControls;
  WSql := WSql + ')';

  frmWarpRollerCk := TfrmWarpRollerCk.create(self);
  with frmWarpRollerCk do
  begin
    fkeyNo := WSql;
    fType := 1;
    JZRKNo.Enabled := false;
    if showmodal = 1 then
    begin
      TBRafresh.Click;
      self.CDS_Main.Locate('JZRKID', trim(fkeyNo), []);
    end;
    free;
  end;
end;

procedure TfrmWarpRollerCklist.TBDelClick(Sender: TObject);
var
  YFID, CRID, note: string;
begin
  if CDS_Main.IsEmpty then
    Exit;

  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  try
    if Trim(CDS_Main.FieldByName('JZRKID').AsString) <> '' then
    begin
      ADOQueryCmd.Connection.BeginTrans;

      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('update CK_WarpBeam_CR set Valid=''N''  where JZRKID=' + quotedstr(Trim(CDS_Main.FieldByName('JZRKID').AsString)));
        if Trim(CDS_Main.FieldByName('crtype').AsString) = '织造出库' then
        begin
          sql.Add('DELETE CK_YGCL    where MJID=' + quotedstr(Trim(CDS_Main.FieldByName('PCID').AsString)));
          SQL.Add('AND CLTYPE=''上轴''  ');
        end;

        ExecSQL;
      end;
//      with ADOQueryCmd do
//      begin
//        Close;
//        SQL.Clear;
//        SQL.Add('exec P_Up_WarpRoller ' + quotedstr(Trim(CDS_Main.FieldByName('kcID').AsString)));
//        Open;
//      end;
//      if Trim(ADOQueryCmd.FieldByName('Msg').AsString) = '操作失败' then
//      begin
//        CDS_Main.EnableControls;
//        ADOQueryCmd.Connection.RollbackTrans;
//        Application.MessageBox('出库数量超出库存数量!', '提示', 0);
//        Exit;
//      end;
      ADOQueryCmd.Connection.CommitTrans;
      CDS_Main.Delete;
    end;
  except
    Application.MessageBox('数据删除失败！', '提示信息', 0);
  end;
end;

procedure TfrmWarpRollerCklist.TBRafreshClick(Sender: TObject);
begin
  initGrid();
end;

procedure TfrmWarpRollerCklist.TBExportClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid1);
end;

procedure TfrmWarpRollerCklist.v1Column6CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
var
  colIdx0, colIdx1: integer;
begin
  colIdx0 := tv1.GetColumnByFieldName('CRNO').Index;
  if (ARow1.Values[colIdx0] = ARow2.Values[colIdx0]) then
    AAreEqual := True
  else
    AAreEqual := False;

end;

procedure TfrmWarpRollerCklist.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

procedure TfrmWarpRollerCklist.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, False);
end;

end.

