unit U_CPSMCK;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters,
  cxStyles, 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, cxCustomData, cxFilter, cxData,
  cxDataStorage, cxEdit, cxNavigator, DB, cxDBData, cxCheckBox, ComCtrls,
  StdCtrls, ExtCtrls, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxGridCustomView, cxGrid, DBClient,
  cxGridCustomPopupMenu, cxGridPopupMenu, Menus, ToolWin, ADODB, MovePanel,
  RM_Common, RM_Class, RM_GridReport, RM_System, RM_Dataset;

type
  TFRMCPSMCK = class(TForm)
    DS_MainSel: TDataSource;
    CDS_MainSel: TClientDataSet;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    v3Column5: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    v3Column3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    v3MJID: TcxGridDBColumn;
    v3BaoNo: TcxGridDBColumn;
    v3BAOID: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    v3Column6: TcxGridDBColumn;
    v3Column7: TcxGridDBColumn;
    v3Column4: TcxGridDBColumn;
    Tv3Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel1: TPanel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label9: TLabel;
    Label12: TLabel;
    Label1: TLabel;
    BaoID: TEdit;
    Button1: TButton;
    CRTime: TDateTimePicker;
    CKOrdNo: TEdit;
    RKOrdID: TEdit;
    mjid: TEdit;
    gangno: TEdit;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    ToolButton3: TToolButton;
    TBClose: TToolButton;
    PopupMenu2: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    cxGridPopupMenu4: TcxGridPopupMenu;
    ADOQueryMain: TADOQuery;
    Button2: TButton;
    MovePanel2: TMovePanel;
    ADOQueryTemp: TADOQuery;
    ADOQueryPrint: TADOQuery;
    CDS_HZ: TClientDataSet;
    RMDBHZ: TRMDBDataSet;
    RM2: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    ADOQueryCmd: TADOQuery;
    ToolButton2: TToolButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure mjidKeyPress(Sender: TObject; var Key: Char);
    procedure Button1Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FRMCPSMCK: TFRMCPSMCK;

implementation

uses
  U_DataLink, U_Fun, MMSystem;

{$R *.dfm}

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

procedure TFRMCPSMCK.FormDestroy(Sender: TObject);
begin
  FRMCPSMCK := nil;
end;

procedure TFRMCPSMCK.FormShow(Sender: TObject);
begin

  ReadCxGrid('成品出库Sels', Tv3, '成品仓库');
//  InitSubGrid();
  InitGrid();
  CRTime.DateTime := SGetServerDate(ADOQueryTemp);
end;

procedure TFRMCPSMCK.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      SQL.Add('select A.*,OrderNo=(select OrderNo from JYOrder_Main where MainId=A.MainId) ');
      sql.Add(',KHName=(select isnull(customerNoName,OrderNo) from JYOrder_Main where MainId=A.MainId)');
      SQL.Add(',PRTColor=(select PRTColor from JYOrder_Sub where SubId=A.SubId)');
      SQL.Add(',SOrddefstr1=(select SOrddefstr1 from JYOrder_Sub where SubId=A.SubId)');
      SQL.Add(',SOrddefstr5=(select SOrddefstr5 from JYOrder_Sub where SubId=A.SubId)');
      sql.Add(',AOrdDefstr1=(select AOrdDefstr1 from JYOrder_Sub_AnPai where ApId=A.ApId)');
      sql.Add(',MJXH=(select MJXH from WFB_MJJY where MJID=A.MJID)');
      sql.Add('from CK_BanCP_CR A');
      sql.Add('where CKOrdNo=''' + trim(CKOrdNo.text) + ''' and CRflag=''出库''');
//      sql.add('where 1=2');
//      ShowMessage(SQL.text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_MainSel);
    SInitCDSData20(ADOQueryMain, CDS_MainSel);
  finally
    ADOQueryMain.EnableControls;
  end;
  if trim(CKOrdNo.Text) <> '' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select count(distinct baoid) as CKbao from WFB_MJJY where ckno=''' + Trim(CKOrdNo.Text) + ''' ');
      Open;
    end;
    Label12.caption := '已扫描包数；' + Trim(ADOQueryTemp.fieldbyname('CKbao').AsString);
  end;
end;

procedure TFRMCPSMCK.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('成品出库Sels', Tv3, '成品仓库');
end;

procedure TFRMCPSMCK.Button2Click(Sender: TObject);
var
  maxno: string;
begin
  if GetLSNo(ADOQueryCmd, maxno, 'CK', 'CK_BanCP_CR', 3, 1) = False then
  begin
    Application.MessageBox('取出库单号失败!', '提示', 0);
    Exit;
  end;
  CKOrdNo.Text := Trim(maxno);
end;

procedure TFRMCPSMCK.mjidKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if trim(CKOrdNo.Text) = '' then
    begin
      PlaySound('wav\错误.wav', 0, SND_FILENAME or SND_ASYNC);
      application.MessageBox('出库单号不能为空', '提示');
      exit;
    end;
    if trim(mjid.Text) = '' then
      exit;
    with ADOQueryCmd do
    begin
      close;
      sql.clear;
      sql.add('select count(distinct CKFlag) as baos,baoid,CKFlag');
      sql.add('from WFB_MJJY A  ');
      sql.add(' where A.baoid=''' + trim(mjid.Text) + '''  GROUP BY A.baoid,CKFlag	 ');
//      showmessage(sql.Text);
      open;
    end;
    if ADOQueryCmd.isempty then
    begin
      if FileExists(PChar(ExtractFilePath(Application.ExeName) + 'wav\包号错误.wav')) then
        PlaySound(PChar(ExtractFilePath(Application.ExeName) + 'wav\包号错误.wav'), 0, SND_ASYNC);
      mjid.Text := '';
      exit;
    end
    else
    begin
      if StrToFloat(ADOQueryCmd.fieldbyname('baos').AsString) > 1 then
      begin
        PlaySound(PChar(ExtractFilePath(Application.ExeName) + 'wav\此包已出库.wav'), 0, SND_ASYNC);
        mjid.Text := '';
        exit;
      end;
    end;
    if Trim(ADOQueryCmd.fieldbyname('CKFlag').AsString) = '已出库' then
    begin
      PlaySound(PChar(ExtractFilePath(Application.ExeName) + 'wav\重复扫描.wav'), 0, SND_ASYNC);
      mjid.Text := '';
      exit;
    end
    else
    begin
      try
            // 出库
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('exec P_Do_JYSMCK @MJID=''' + trim(mjid.Text) + ''',@CKOrdNo=''' + trim(CKOrdNo.Text) + '''');
          execsql;
        end;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('update WFB_MJJY set MJStr2=''已出库'',CKFlag=''已出库'',CKNO=''' + trim(CKOrdNo.Text) + ''' where baoid=''' + Trim(mjid.text) + '''');
          ExecSQL;
        end;
        PlaySound('wav\正确.wav', 0, SND_FILENAME or SND_ASYNC);
      except
        PlaySound('wav\扫描错误.wav', 0, SND_FILENAME or SND_ASYNC);
      end;
    end;

    mjid.Text := '';
    InitGrid();
  end;
//  if trim(CKOrdNo.Text) <> '' then
//  begin
//    with ADOQueryTemp do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('select count(distinct baoid) as CKbao from WFB_MJJY where ckno=''' + Trim(CKOrdNo.Text) + ''' ');
//      Open;
//    end;
//    Label12.caption := '已扫描包数；' + Trim(ADOQueryTemp.fieldbyname('CKbao').AsString);
//  end;

end;

procedure TFRMCPSMCK.Button1Click(Sender: TObject);
begin
  if CDS_MainSel.IsEmpty then
    Exit;
  if CDS_MainSel.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要执行此操作吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;

  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_MainSel.DisableControls;
    with CDS_MainSel do
    begin
      while CDS_MainSel.Locate('SSel', True, []) = True do
      begin
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
//            sql.Add('delete CK_BanCP_CR where BCID=''' + Trim(CDS_MainSel.fieldbyname('BCID').AsString) + '''');
//            sql.Add('Update WFB_MJJY Set MJStr2=''已入库'',CKFlag=''未出库'',CKNO=null where MJID=''' + Trim(CDS_MainSel.fieldbyname('MJID').AsString) + '''');

            sql.Add('UPdate CK_BanCP_KC Set KCKgQty=(select KgQty from CK_BanCP_CR A where  A.MJID=CK_BanCP_KC.MJID and A.CRType=''检验入库'') ');
            sql.Add(',KCQty=(select Qty from CK_BanCP_CR A where  A.MJID=CK_BanCP_KC.MJID and A.CRType=''检验入库'') ');
            SQL.Add(' where MJID=' + CDS_MainSel.fieldbyname('MJID').AsString);

            sql.Add('delete CK_BanCP_CR where baoid=''' + Trim(CDS_MainSel.fieldbyname('baoid').AsString) + ''' and CRFlag=''出库'' ');
            sql.Add('Update WFB_MJJY Set MJStr2=''已入库'',CKFlag=''未出库'',CKNO=null where baoid=''' + Trim(CDS_MainSel.fieldbyname('baoid').AsString) + '''');
            ShowMessage(SQL.text);
            ExecSQL;
          end;

          CDS_MainSel.Delete;
        end;
      end;
    end;
    CDS_MainSel.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    MovePanel2.Visible := False;
    Exit;
  except
    MovePanel2.Visible := False;
    CDS_MainSel.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('撤销异常！', '提示', 0);
  end;
  initgrid();
end;

procedure TFRMCPSMCK.ToolButton1Click(Sender: TObject);
var
  fPrintFile, fPrintFile10, FMainID: string;
  sqlStr: string;
begin
  if CDS_MainSel.IsEmpty then
    Exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\出库英文品名码单.rmf';

  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Do_PrintMd10_PMYW  ');  //''' + CDS_Main.fieldbyname('CKNO').AsString + '''
      sql.add('@mainID=''' +  Trim(CKOrdNo.Text) + '''');
      sql.add(',@orderNo=''' + CDS_MainSel.fieldbyname('mainid').AsString + '''');
      sql.add(',@Sorddefstr5=''' +  CDS_MainSel.fieldbyname('Sorddefstr5').AsString + '''');
     ShowMessage(SQL.text);
      Open;
//      ShowMessage(SQL.text);                          
    end;
    SCreateCDS20(ADOQueryTemp, CDS_HZ);
    SInitCDSData20(ADOQueryTemp, CDS_HZ);

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Do_PrintMd_HZ ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      sql.add(',@flag=''2'' ');
      Open;
    end;
  end;
  if FileExists(fPrintFile) then
  begin
    RM2.LoadFromFile(fPrintFile);
    RM2.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

procedure TFRMCPSMCK.ToolButton3Click(Sender: TObject);
var
  fPrintFile, fPrintFile10, FMainID: string;
  sqlStr: string;
begin
  if CDS_MainSel.IsEmpty then
    Exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\出库码单.rmf';
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
//      sql.Add('exec P_Do_PrintMd10 ''' + CDS_Main.fieldbyname('CKNO').AsString + ''' ');
      sql.Add('exec P_Do_PrintMd10  ''' + trim(CKOrdNo.Text) + ''' ');
      Open;
//      ShowMessage(SQL.text);
    end;
    SCreateCDS20(ADOQueryTemp, CDS_HZ);
    SInitCDSData20(ADOQueryTemp, CDS_HZ);

    with ADOQueryPrint do
    begin
      Close;
      sql.Clear;
      sql.add('exec P_Do_PrintMd_HZ ');
      sql.add('@mainID=' + quotedstr(Trim('')));
      sql.add(',@DName=' + quotedstr(Trim(DCode)));
      sql.add(',@flag=''2'' ');
      Open;
    end;
  end;
  if FileExists(fPrintFile) then
  begin
    RM2.LoadFromFile(fPrintFile);
    RM2.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

procedure TFRMCPSMCK.ToolButton2Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TFRMCPSMCK.MenuItem1Click(Sender: TObject);
begin
  SelOKNo(CDS_MainSel, True);
end;

procedure TFRMCPSMCK.MenuItem2Click(Sender: TObject);
begin
  SelOKNo(CDS_MainSel, False);
end;

end.

