unit U_CPDBAOList;

interface

uses
  Windows, Messages, strutils, SysUtils, Variants, Classes, Graphics, Controls,
  Forms, Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls, cxGridCustomView,
  cxGrid, StdCtrls, cxContainer, cxTextEdit, cxCurrencyEdit, BtnEdit, ExtCtrls,
  ComCtrls, ToolWin, DBClient, ADODB, MovePanel, Buttons, Menus,
  cxLookAndFeelPainters, cxButtons, RM_Common, RM_Class, RM_GridReport,
  RM_System, RM_Dataset, cxCheckBox, MMSystem, Math, cxGridCustomPopupMenu,
  cxGridPopupMenu, cxLookAndFeels, cxNavigator, RM_e_Xls, 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
  TfrmCPDBAOList = class(TForm)
    Panel1: TPanel;
    Label1: TLabel;
    Panel2: TPanel;
    ADOQueryMain: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    RMDB_Main: TRMDBDataSet;
    RM2: TRMGridReport;
    ADOQueryPrint: TADOQuery;
    CDS_YDB: TClientDataSet;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    DS_YDB: TDataSource;
    V4Column2: TcxGridDBColumn;
    PM_YDB: TPopupMenu;
    btnClose: TcxButton;
    N3: TMenuItem;
    CDS_DDY: TClientDataSet;
    cxGridPopupMenu3: TcxGridPopupMenu;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    CDS_PRT: TClientDataSet;
    RMDBPRT: TRMDBDataSet;
    RM3: TRMGridReport;
    ComboBox1: TComboBox;
    cxButton1: TcxButton;
    V1Column1: TcxGridDBColumn;
    V1Column2: TcxGridDBColumn;
    edtFHSQ: TBtnEditA;
    BaoLab: TEdit;
    cbbFS: TComboBox;
    Label10: TLabel;
    V1Column3: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column5: TcxGridDBColumn;
    v2Column12: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ClientDataSet2: TClientDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DataSource2: TDataSource;
    cxButton2: TcxButton;
    edtStatus: TEdit;
    v2Column10: TcxGridDBColumn;
    V1Column4: TcxGridDBColumn;
    RMXLSExport1: TRMXLSExport;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure btnCloseClick(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure cxButton1Click(Sender: TObject);
    procedure edtFHSQBtnClick(Sender: TObject);
    procedure edtFHSQKeyPress(Sender: TObject; var Key: Char);
    procedure cxButton2Click(Sender: TObject);
    procedure TV1CustomDrawIndicatorCell(Sender: TcxGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxCustomGridIndicatorItemViewInfo; var ADone: Boolean);
  private
    FFSID, NLBName: string;
    procedure InitGridYDB();
    procedure PrintBao(PBaoId: string);
    procedure InitGridFHZB();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCPDBAOList: TfrmCPDBAOList;
  newh: hwnd;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_CPFHSQListSel;
{$R *.dfm}

procedure TfrmCPDBAOList.InitGridFHZB();
begin
  with ADOQueryTmp do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* from FHSQ_Sub_CP A ');
    sql.Add(' where A.FSId=''' + Trim(FFSID) + '''');
    Open;
  end;
  SCreateCDS20(ADOQueryTmp, ClientDataSet2);
  SInitCDSData20(ADOQueryTmp, ClientDataSet2);
end;

procedure TfrmCPDBAOList.PrintBao(PBaoId: string);
var
  fPrintFile: string;
  Txt, fImagePath: string;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin

  with ADOQueryPrint do
  begin
    Close;
    SQL.Clear;
    SQL.Add('exec P_Do_PrintDaBao ' + quotedstr(PBaoId));
    Open;
  end;
  if Trim(BaoLab.Text) <> '' then
  begin
    NLBName := Trim(BaoLab.Text);
  end
  else
  begin
    NLBName := '包标签';
  end;
  try
    Moudle := LoadLibrary('MakeQRBarcode.dll');
    @Makebar := GetProcAddress(Moudle, 'Make');
    @Mixtext := GetProcAddress(Moudle, 'MixText');
    Txt := PBaoId;
    fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
    if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
      CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
    if FileExists(fImagePath) then
      DeleteFile(fImagePath);
    Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);
  except
    application.MessageBox('条形码生成失败！', '提示信息', MB_ICONERROR);
    exit;
  end;

  begin
    ExportFtErpFile(Trim(NLBName) + '.rmf', ADOQueryTmp);
    fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(NLBName) + '.rmf';
    if not FileExists(fPrintFile) then
    begin
      Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
      Exit;
    end;
    if FileExists(fPrintFile) then
    begin
      RMVariables['QRBARCODE'] := fImagePath;
      RM2.LoadFromFile(fPrintFile);
      RM2.DefaultCopies := strtointdef(trim(cbbFS.Text), 1);
      RM2.printReport;
    end;
  end;
end;

procedure TfrmCPDBAOList.InitGridYDB();
begin
  with ADOQueryMain do
  begin
    close;
    sql.Clear;
    sql.Add('exec P_DB_View_YDBHZ ' + QUOTEDSTR(FFSID));  //P_DB_View_YDB
    open;
  end;
  SCreateCDS20(ADOQueryMain, CDS_YDB);
  SInitCDSData20(ADOQueryMain, CDS_YDB);
end;

procedure TfrmCPDBAOList.FormDestroy(Sender: TObject);
begin
  frmCPDBAOList := nil;
end;

procedure TfrmCPDBAOList.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  writeCxGrid(trim(Self.Caption), Tv1, '成品仓库');
  Action := cafree;
end;

procedure TfrmCPDBAOList.FormCreate(Sender: TObject);
begin
  readCxGrid(trim(Self.Caption), Tv1, '成品仓库');

end;

procedure TfrmCPDBAOList.btnCloseClick(Sender: TObject);
begin

  close;
end;

procedure TfrmCPDBAOList.N3Click(Sender: TObject);
begin
  PrintBao(Trim(CDS_YDB.FieldByName('baoid').AsString));
end;

procedure TfrmCPDBAOList.cxButton1Click(Sender: TObject);
var
  fPrintFile, fPrintFile10, FMainID, FKHName, FFPNo: string;
begin
  if Trim(FFSID) = '' then
    Exit;

  if trim(ComboBox1.Text) = '仓库打包码单' then
  begin
    with ADOQueryTmp do
    begin

      Close;
      sql.Clear;
      sql.add('exec P_Print_CKBMD1 ');
      sql.add('@CKOrdNo=' + quotedstr(Trim(FFSID)));
      Open;
    end;
    SCreateCDS20(ADOQueryTmp, CDS_PRT);
    SInitCDSData20(ADOQueryTmp, CDS_PRT);
  end;
  if trim(ComboBox1.Text) = '单证打包码单' then
  begin
    with ADOQueryTmp do
    begin

      Close;
      sql.Clear;
      sql.add('exec P_Print_CKBMD2 ');
      sql.add('@CKOrdNo=' + quotedstr(Trim(FFSID)));
      Open;
    end;
    SCreateCDS20(ADOQueryTmp, CDS_PRT);
    SInitCDSData20(ADOQueryTmp, CDS_PRT);

    with ADOQueryTmp do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Print_CKBMD3 ');
      sql.add('@CKOrdNo=' + quotedstr(Trim(FFSID)));
      Open;
    end;
    SCreateCDS20(ADOQueryTmp, CDS_HZ);
    SInitCDSData20(ADOQueryTmp, CDS_HZ);
  end;

  if trim(ComboBox1.Text) = '打包总结单' then
  begin
    with ADOQueryTmp do
    begin

      Close;
      sql.Clear;
      sql.add('exec P_Print_CKBMD4 ');
      sql.add('@CKOrdNo=' + quotedstr(Trim(FFSID)));
      Open;
    end;
    SCreateCDS20(ADOQueryTmp, CDS_PRT);
    SInitCDSData20(ADOQueryTmp, CDS_PRT);
  end;

  if trim(ComboBox1.Text) = '随车细码单' then
  begin
    with ADOQueryTmp do
    begin

      Close;
      sql.Clear;
      sql.add('exec P_Print_CKBMD5 ');
      sql.add('@CKOrdNo=' + quotedstr(Trim(FFSID)));
      Open;
    end;
    SCreateCDS20(ADOQueryTmp, CDS_PRT);
    SInitCDSData20(ADOQueryTmp, CDS_PRT);
  end;
  RMXLSExport1:= TRMXLSExport.Create(RMXLSExport1);
  ExportFtErpFile(trim(ComboBox1.Text) + '.rmf', ADOQueryTmp);
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\' + Trim(ComboBox1.Text) + '.rmf';
  if FileExists(fPrintFile) then
  begin
    RM3.LoadFromFile(fPrintFile);
    RMVariables['ZDR'] := trim(DName);
    RM3.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找到' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmCPDBAOList.edtFHSQBtnClick(Sender: TObject);
var
  FBaoID: string;
  FMainID: string;
begin
  try
    frmFHSQListSel := TfrmFHSQListSel.Create(self);
    with frmFHSQListSel do
    begin
      FSQStatus := '已打包';
      if ShowModal = 1 then
      begin
        FFSID := Trim(Order_Main.fieldbyname('FSID').AsString);
        BaoLab.Text := Trim(Order_Main.fieldbyname('BaoLab').AsString);
        edtStatus.Text := Trim(Order_Main.fieldbyname('Status').AsString);
      end;
    end;
  finally
    frmFHSQListSel.Free;
  end;
  edtFHSQ.SetFocus;

  InitGridYDB();
  InitGridFHZB();
end;

procedure TfrmCPDBAOList.edtFHSQKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    FFSID := Trim(edtFHSQ.Text);
    with ADOQueryTmp do
    begin
      Close;
      sql.Clear;
      Sql.Add(' select A.* ');
      sql.Add(' from FHSQ_Main_CP A');
      SQL.Add(' where isnull(A.Chker,'''')= ' + quotedstr(Trim(FFSID)));
      Open;
    end;
    BaoLab.Text := Trim(ADOQueryTmp.fieldbyname('BaoLab').AsString);
    edtStatus.Text := Trim(ADOQueryTmp.fieldbyname('Status').AsString);
    InitGridFHZB();
    InitGridYDB();
    edtFHSQ.Text := '';
  end;
end;

procedure TfrmCPDBAOList.cxButton2Click(Sender: TObject);
begin
  if FFSID = '' then
    Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      close;
      sql.Clear;
      sql.Add('update  FHSQ_Main_CP SET Status=''已确认'' ');
      sql.Add('where FSID=' + quotedstr(Trim(FFSID)));
      sql.Add('and  Status<>''已发货''');
      execsql;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    edtStatus.Text := '已确认';
  except
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox('出库失败！', '提示信息', 0);
  end;
end;

procedure TfrmCPDBAOList.TV1CustomDrawIndicatorCell(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;

end.

