unit U_YMTRKList;

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, cxCalendar, cxButtonEdit, cxTextEdit,
  cxContainer, cxImage, cxDBEdit, cxLookAndFeels, cxLookAndFeelPainters,
  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, cxNavigator, cxCurrencyEdit;

type
  TfrmYMTRKList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    CDS_PRT: TClientDataSet;
    TBDel: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label8: TLabel;
    SPName: TEdit;
    FactoryName: TEdit;
    SPID: TEdit;
    SPSpec: TEdit;
    Label6: TLabel;
    SPCF: TEdit;
    Label7: TLabel;
    OrderNo: TEdit;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label9: TLabel;
    ToFactoryName: TEdit;
    Label12: TLabel;
    CRType: TComboBox;
    v1Column18: TcxGridDBColumn;
    Label10: TLabel;
    KuWei: TEdit;
    v1Column19: TcxGridDBColumn;
    Label11: TLabel;
    RCGangNo: TEdit;
    Label13: TLabel;
    PRTColor: TEdit;
    Label14: TLabel;
    SOrddefstr1: TEdit;
    Label15: TLabel;
    PRTHX: TEdit;
    ADOQueryImage: TADOQuery;
    v1Column21: TcxGridDBColumn;
    Label16: TLabel;
    CustomerNoName: TEdit;
    cxGrid1: TcxGrid;
    TvMX: TcxGridDBTableView;
    TvMXMXQty: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    CDS_MX: TClientDataSet;
    DS_MX: TDataSource;
    cxGridPopupMenu2: TcxGridPopupMenu;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    TvMXMXID: TcxGridDBColumn;
    Panel2: TPanel;
    ToolBar2: TToolBar;
    TbAddRow: TToolButton;
    TbDeleteRow: TToolButton;
    Panel3: TPanel;
    AddNum: TEdit;
    Label17: TLabel;
    Button1: TButton;
    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 N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure SPIDChange(Sender: TObject);
    procedure SPSpecChange(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure TvMXCustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure TbAddRowClick(Sender: TObject);
    procedure TbDeleteRowClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    canshu1, canshu2: string;
    procedure InitGrid();
    procedure InitImage(fsubID: string);
    procedure InitMXGrid(SPID: string);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmYMTRKList: TfrmYMTRKList;

implementation

uses
  U_DataLink, U_RTFun, U_YMTRKInPut, U_ZdyAttachGYS, U_LabelPrintFun;

{$R *.dfm}
procedure TfrmYMTRKList.InitMXGrid(SPID: string);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from YMT_CK_MX where 1 = 1 and SPID=' + quotedstr(Trim(SPID)));
    sql.Add(' order by MXID');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_MX);
  SInitCDSData20(ADOQueryTemp, CDS_MX);
end;

procedure TfrmYMTRKList.InitImage(fsubID: string);
begin
  ADOQueryImage.close;
 // IF fwbid='' then exit;
  with ADOQueryImage do
  begin
    close;
    sql.Clear;
    sql.Add('select * from TP_File A');
    sql.Add('inner join JYOrder_Sub B on B.HXFile=A.WBID');
    sql.Add('where B.SubID=' + quotedstr(trim(fsubID)));
    open;
  end;
end;

procedure TfrmYMTRKList.FormDestroy(Sender: TObject);
begin
  frmYMTRKList := nil;
end;

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

procedure TfrmYMTRKList.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  canshu1 := Trim(DParameters1);
  canshu2 := Trim(DParameters2);
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime;
end;

procedure TfrmYMTRKList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select A.* ');
      sql.Add(' from YMT_CK_CR A');
      sql.add(' where A.CRTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''');
      sql.Add(' and A.CRTime<=''' + Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)) + '''');
      sql.Add(' and isnull(CKName,'''')=''待检布''');
      SQL.Add(' and CRFlag=''入库''  ');
      Open;
      //ShowMessage(SQL.Text);
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);

    InitMXGrid(Trim(CDS_Main.fieldbyname('SPID').AsString));
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

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

procedure TfrmYMTRKList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('待检布入库列表', Tv1, '待检布仓库');
  WriteCxGrid('待检布入库列表2', TvMX, '待检布仓库');
  Close;
end;

procedure TfrmYMTRKList.FormShow(Sender: TObject);
begin
  if Trim(canshu2) = '查看' then
  begin
    TBAdd.Visible := False;
    TBDel.Visible := False;
    TBEdit.Visible := False;
  end
  else
  begin
    TBAdd.Visible := True;
    TBDel.Visible := True;
    TBEdit.Visible := True;
  end;
  ReadCxGrid('待检布入库列表', Tv1, '待检布仓库');
  ReadCxGrid('待检布入库列表2', TvMX, '待检布仓库');
  InitGrid();
end;

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

procedure TfrmYMTRKList.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 TfrmYMTRKList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, True);
end;

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

procedure TfrmYMTRKList.TBDelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  if Trim(CDS_Main.fieldbyname('SPID').AsString) <> '' then
  begin
    if Trim(CDS_Main.fieldbyname('CRType').AsString) = '平移入库' then
    begin
      Application.MessageBox('平移入库的数据为自动生成，不能删除!', '提示', 0);
      Exit;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YMT_CK_CR where FZSPID=''' + Trim(CDS_Main.fieldbyname('SPID').AsString) + '''');
      sql.Add(' and CRQtyFlag=-1');
      Open;
    end;
    if ADOQueryTemp.IsEmpty = False then
    begin
      Application.MessageBox('已有出库记录，不能删除!', '提示', 0);
      Exit;
    end;
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    try
      ADOQueryCmd.Connection.BeginTrans;

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' delete YMT_CK_CR where SPID=''' + Trim(CDS_Main.fieldbyname('SPID').AsString) + '''');
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      CDS_Main.Delete;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!', '提示', 0);
    end;
  end
  else
  begin
    CDS_Main.Delete;
  end;
end;

procedure TfrmYMTRKList.TBAddClick(Sender: TObject);
begin
  try
    frmYMTRKInPut := TfrmYMTRKInPut.Create(Application);
    with frmYMTRKInPut do
    begin
      FBCId := '';
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmYMTRKInPut.Free;
  end;
end;

procedure TfrmYMTRKList.TBEditClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    Exit;
  if Trim(CDS_Main.fieldbyname('CRType').AsString) = '平移入库' then
  begin
    Application.MessageBox('平移入库的数据为自动生成，不能修改!', '提示', 0);
    Exit;
  end;
  try
    frmYMTRKInPut := TfrmYMTRKInPut.Create(Application);
    with frmYMTRKInPut do
    begin
      FBCId := Trim(CDS_Main.fieldbyname('SPID').AsString);
      TBDel.Visible := False;
      TBAdd.Visible := False;
      if ShowModal = 1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmYMTRKInPut.Free;
  end;
end;

procedure TfrmYMTRKList.SPIDChange(Sender: TObject);
begin
  if Length(Trim(SPID.Text)) < 4 then
  begin
    if Trim(SPID.Text) <> '' then
      Exit;
  end;
  TBFind.Click;
end;

procedure TfrmYMTRKList.SPSpecChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmYMTRKList.FactoryNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmYMTRKList.TvMXCustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
var
  FValue: string;
  FBounds: TRect;
begin
  FBounds := AViewInfo.Bounds;
  if (AViewInfo is TcxGridIndicatorRowItemViewInfo) then
  begin
    ACanvas.FillRect(FBounds);
    ACanvas.DrawComplexFrame(FBounds, clBtnHighlight, clBtnShadow, [bBottom, bLeft, bRight], 1);
    FValue := IntToStr(TcxGridIndicatorRowItemViewInfo(AViewInfo).GridRecord.Index + 1);
    InflateRect(FBounds, -1, -1);    //Platform   specific.   May   not   work   on   Linux.
    ACanvas.Font.Color := clBlack;
    ACanvas.Brush.Style := bsClear;
    ACanvas.DrawText(FValue, FBounds, cxAlignCenter or cxAlignTop);
    ADone := True;
  end;
end;

procedure TfrmYMTRKList.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitMXGrid(Trim(CDS_Main.fieldbyname('SPID').AsString));
end;

procedure TfrmYMTRKList.ToolButton1Click(Sender: TObject);
var
  FPrintJson, JsonResult, MFiltration: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  MFiltration := Trim(CDS_Main.fieldbyname('SPID').AsString);
  FPrintJson := '{ "LMType": "LMType","PreviewPrint": true,"DConString": "' + DConString + '","DCode": "' + DCode + '","DName": "' + DName + '", "PrtArgs": [ { "IsSql": true, "Filtration": " ' + MFiltration + '" }] }';

  FormPrint(Application, PChar(FPrintJson));
end;

procedure TfrmYMTRKList.ToolButton2Click(Sender: TObject);
var
  MPrintJson, JsonResult, MFiltration: string;
begin
  if CDS_Main.IsEmpty then
    Exit;
  MFiltration := Trim(CDS_Main.fieldbyname('SPID').AsString);
  MPrintJson := '{ "LBName": "标签打印","DConString": "' + DConString + '","DCode": "' + DCode + '","DName": "' + DName + '","IsPreview": true,"printerIndex": 0,"ExportFileType": "", "PrtArgs": [ {"SqlStr": "EXEC P_YMT_CK_MX_Prt1 '' ' + MFiltration + ''' " }] }';
  FunPrint(Application, PChar(MPrintJson));

end;

procedure TfrmYMTRKList.TbAddRowClick(Sender: TObject);
var
  MaxNo, MMXID, SPID: string;
begin
//  with CDS_MX do
//  begin
//    Append;
//    FieldByName('MXID').Value := '';
//    FieldByName('MXQty').Value := 0;
//    Post;
//  end;
  if GetLSNo(ADOQueryTemp, MaxNo, 'MCM', 'YMT_CK_MX', 3, 1) = False then
  begin
    Application.MessageBox('取最大号失败!', '提示', 0);
    Exit;
  end;
  MMXID := Trim(MaxNo);
  SPID := Trim(CDS_Main.fieldbyname('SPID').AsString);
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('insert into  YMT_CK_MX (MXID,SPID,MXQty)');
    sql.Add('values(' + quotedstr(MMXID));
    sql.Add(',' + quotedstr(Trim(SPID)));
    sql.Add(',0');
    sql.Add(')');
    ExecSQL;
  end;
  InitMXGrid(SPID);
end;

procedure TfrmYMTRKList.TbDeleteRowClick(Sender: TObject);
var
  Bookmark: TBookmark;
begin
  if CDS_MX.IsEmpty then
  begin
    Application.MessageBox('当前没有可删除的记录！', '提示', MB_ICONWARNING);
    Exit;
  end;

  if Application.MessageBox('确定要删除当前行吗？', '确认删除', MB_ICONQUESTION + MB_YESNO) <> IDYES then
    Exit;
    
  // 记录当前行，删除后尝试回到附近位置
  Bookmark := CDS_MX.GetBookmark;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('delete YMT_CK_MX   where MXID=' + quotedstr(Trim(CDS_MX.FieldByName('MXID').AsString)));
    ExecSQL;
  end;
  try
    CDS_MX.Delete;
  finally
    if CDS_MX.BookmarkValid(Bookmark) then
      CDS_MX.GotoBookmark(Bookmark);
    CDS_MX.FreeBookmark(Bookmark);
  end;

end;

procedure TfrmYMTRKList.Button1Click(Sender: TObject);
var
  Num, i: Integer;
  MaxNo, MMXID, SPID: string;
begin
  Num := StrToInt(Trim(AddNum.Text));
  SPID := Trim(CDS_Main.fieldbyname('SPID').AsString);

  try
    ADOQueryCmd.Connection.BeginTrans;
    for i := 1 to Num do
    begin
      if GetLSNo(ADOQueryTemp, MaxNo, 'MCM', 'YMT_CK_MX', 3, 1) = False then
      begin
        Application.MessageBox('取最大号失败!', '提示', 0);
        Exit;
      end;
      MMXID := Trim(MaxNo);
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('insert into  YMT_CK_MX (MXID,SPID,MXQty)');
        sql.Add('values(' + quotedstr(MMXID));
        sql.Add(',' + quotedstr(Trim(SPID)));
        sql.Add(',0');
        sql.Add(')');
        ExecSQL;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitMXGrid(SPID);
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('添加失败!', '提示', 0);
  end;
end;

end.

