unit U_CKProductBCPInList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridBandedTableView, cxGridDBBandedTableView, cxGridLevel, cxClasses,
  cxControls, cxGridCustomView, cxGridDBTableView, cxGrid, StdCtrls, ComCtrls,
  ExtCtrls, ToolWin, cxGridCustomPopupMenu, cxGridPopupMenu, ADODB, DBClient,
  cxDropDownEdit, cxCheckBox, RM_Common, RM_Class, RM_e_Xls, RM_Dataset,
  RM_System, RM_GridReport, Menus, cxContainer, cxTextEdit, cxMaskEdit,
  cxCalendar, Clipbrd, cxLookAndFeels, cxLookAndFeelPainters, dxCore,
  cxDateUtils, cxNavigator, 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
  TfrmCKProductBCPInListPB = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    Label3: TLabel;
    Label4: TLabel;
    C_CodeName: TEdit;
    C_Color: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    CDS_Main: TClientDataSet;
    Label5: TLabel;
    ConNo: TEdit;
    Label6: TLabel;
    MJID: TEdit;
    Label7: TLabel;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    TBRKCX: TToolButton;
    v1Sel: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ADOQueryPrint: TADOQuery;
    v1Column4: TcxGridDBColumn;
    Label8: TLabel;
    C_Code: TEdit;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Label12: TLabel;
    CheJian: TComboBox;
    CPType: TComboBox;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1Column10: TcxGridDBColumn;
    BegDate: TcxDateEdit;
    EndDate: TcxDateEdit;
    v1Column11: TcxGridDBColumn;
    cxgrdbclmnv1Column12: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Label9: TLabel;
    KHMC: TEdit;
    Tv1Column2: TcxGridDBColumn;
    Label10: TLabel;
    C_Spec: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure C_CodeNameChange(Sender: TObject);
    procedure ConNoChange(Sender: TObject);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBRKCXClick(Sender: TObject);
    procedure CheJianChange(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    { Public declarations }
  end;

//var
  //frmCKProductBCPInListPB: TfrmCKProductBCPInListPB;

implementation

uses
  U_DataLink, U_Fun;

{$R *.dfm}

procedure TfrmCKProductBCPInListPB.FormDestroy(Sender: TObject);
begin
  //frmCKProductBCPInListPB:=nil;
end;

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

procedure TfrmCKProductBCPInListPB.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  EndDate.Text := FormatDateTime('yyyy-MM-dd', SGetServerDate10(ADOQueryTemp)) + ' 08:00:00';
  BegDate.Text := FormatDateTime('yyyy-MM-dd', SGetServerDate10(ADOQueryTemp) - 1) + ' 08:00:00';
end;

procedure TfrmCKProductBCPInListPB.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      SQL.Add('select KHMC=B.KHName,D.CheJian,D.CarNo,A.*,B.ConNo,C.C_CodeName,C.C_Code,C.C_Color,C.gongyi,D.APXH,C.C_Spec ');
      sql.add('from CK_PBCP_CR A ');
      Sql.add(' inner join Cloth_Main B on A.MainId=B.MainId');
      Sql.add(' inner join Cloth_Sub C on A.MainId=C.MainId');
      Sql.add(' inner join WFB_MJJY_PB D on A.MJId=D.MJId');
      sql.add('where A.CRTime>=''' + Trim(BegDate.Text) + ''' ');
      sql.Add(' and A.CRTime<''' + Trim(enddate.Text) + '''');
      SQL.Add(' and CRFlag=''入库'' ');
      Open;
//      ShowMessage(SQL.Text);
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCKProductBCPInListPB.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  InitGrid();
end;

procedure TfrmCKProductBCPInListPB.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  end;
end;

procedure TfrmCKProductBCPInListPB.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Self.Caption, Tv1, '坯布仓库');
  Close;
end;

procedure TfrmCKProductBCPInListPB.FormShow(Sender: TObject);
var
  fsj: string;
begin
  fsj := 'select distinct Code=Null,Name=MCCJ from Machine order by MCCJ ';
  SInitComBoxBySql(ADOQueryTemp, CheJian, False, fsj);
  ReadCxGrid(Self.Caption, Tv1, '坯布仓库');
  if Trim(Self.Caption) = '坯布入库查询' then
  begin
    TBRKCX.Visible := False;
    v1Sel.Visible := False;
  end
  else if Trim(Self.Caption) = '坯布入库管理' then
  begin
    TBRKCX.Visible := True;
    v1Sel.Visible := True;
  end;
  //InitGrid();
end;

procedure TfrmCKProductBCPInListPB.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel('坯布入库列表', cxGrid2);
end;

procedure TfrmCKProductBCPInListPB.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  end;
end;

procedure TfrmCKProductBCPInListPB.C_CodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductBCPInListPB.ConNoChange(Sender: TObject);
begin
  if Length(Trim(ConNo.Text)) < 4 then
    Exit;
  TBFind.Click;
end;

procedure TfrmCKProductBCPInListPB.ConNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if Length(Trim(ConNo.Text)) < 4 then
      Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered := False;
        Close;
        sql.Clear;
        SQL.Add('select A.*,B.ConNo,C.C_CodeName,C.C_Color,D.MJXH,C.ConDefStr9');
        sql.add('from CK_PBCP_CR A ');
        Sql.add(' inner join Cloth_Main B on A.MainId=B.MainId');
        Sql.add(' inner join Cloth_Sub C on A.SubId=C.SubId');
        Sql.add(' inner join WFB_MJJY_PB D on A.MJId=D.MJId');
        sql.add('where B.ConNo like :ConNo');
        SQL.Add(' and CRType=''检验入库'' ');
        Parameters.ParamByName('ConNo').Value := '%' + Trim(ConNo.Text) + '%';

        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain, CDS_Main);
      SInitCDSData20(ADOQueryMain, CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
end;

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

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

procedure TfrmCKProductBCPInListPB.TBRKCXClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  if CDS_Main.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要执行操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  BegDate.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      while Locate('SSel', True, []) do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_PBCP_CR where  CRFlag=''出库'' and MJID=' + quotedstr(Trim(CDS_Main.fieldbyname('MJID').AsString)));
          Open;
        end;
        if ADOQueryTemp.RecordCount > 0 then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('已出库不能撤销入库!', '提示', 0);
          Exit;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_PBCP_CR where  CRFlag=''预出库'' and MJID=' + quotedstr(Trim(CDS_Main.fieldbyname('MJID').AsString)));
          Open;
        end;
        if ADOQueryTemp.RecordCount > 0 then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('已预出库不能撤销入库!', '提示', 0);
          Exit;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('exec P_Do_PBRK @CRFlag=''未入库'',@KuWei='''',@MJID=' + quotedstr(Trim(CDS_Main.fieldbyname('MJID').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_Main.FieldByName('ConNo').AsString) + ' 条码：' + trim(CDS_Main.FieldByName('MJID').AsString))));
          sql.Add(',' + quotedstr(trim('成功')));
          sql.Add(')');
          ExecSQL;
        end;

        CDS_Main.Delete;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;

  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销失败!', '提示', 0);
  end;
end;

procedure TfrmCKProductBCPInListPB.CheJianChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCKProductBCPInListPB.Tv1DblClick(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

end.

