unit U_BanCpCkSaoM;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, StdCtrls, ExtCtrls, ADODB, DBClient,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxClasses,
  cxControls, cxGridCustomView, cxGrid;

type
  TfrmBanCpCkSaoM = class(TForm)
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    DataSource1: TDataSource;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    Panel1: TPanel;
    XJID: TEdit;
    Label1: TLabel;
    Button1: TButton;
    v1Column5: TcxGridDBColumn;
    Button2: TButton;
    v1Column6: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure XJIDKeyPress(Sender: TObject; var Key: Char);
    procedure Button1Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
  public
    { Public declarations }
  end;

var
  frmBanCpCkSaoM: TfrmBanCpCkSaoM;

implementation
uses
U_DataLink,U_Fun ;

{$R *.dfm}

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

procedure TfrmBanCpCkSaoM.FormDestroy(Sender: TObject);
begin
  frmBanCpCkSaoM:=nil;
end;
procedure TfrmBanCpCkSaoM.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      SQL.Add('select A.*,B.OrderNo,B.MPRTCodeName,C.PRTColor,F.KCQty,F.KCKgQty,F.KCQtyUnit,KK.GangNo  ');
      sql.add('from CK_BanCP_CR A ');
      Sql.add(' inner join JYOrder_Main B on A.MainId=B.MainId');
      Sql.add(' inner join JYOrder_Sub C on A.SubId=C.SubId');
      Sql.add(' inner join WFB_MJJY D on A.MJId=D.MJId');
      sql.Add(' inner join JYOrder_Sub_AnPai KK on D.APID=KK.APID');
      sql.Add(' inner join CK_BanCP_KC F on A.CRID=F.CRID');
      sql.add('where 1<>1');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmBanCpCkSaoM.FormShow(Sender: TObject);
begin
  ReadCxGrid('成品出库',Tv1,'成品仓库');
  InitGrid();
end;

procedure TfrmBanCpCkSaoM.XJIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      SQL.Add('select A.*  ');
      sql.add('from CK_BanCP_CR A ');
      sql.add('where A.MJID='''+Trim(XJID.Text)+'''');
      Open;
    end;
    if ADOQueryTemp.IsEmpty then
    begin
      Application.MessageBox('条码错误！','提示',0);
      Exit;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      SQL.Add('select A.*,B.OrderNo,B.MPRTCodeName,C.PRTColor,F.KCQty,F.KCKgQty,F.KCQtyUnit,KK.GangNo  ');
      sql.add('from CK_BanCP_CR A ');
      Sql.add(' inner join JYOrder_Main B on A.MainId=B.MainId');
      Sql.add(' inner join JYOrder_Sub C on A.SubId=C.SubId');
      Sql.add(' inner join WFB_MJJY D on A.MJId=D.MJId');
      sql.Add(' inner join JYOrder_Sub_AnPai KK on D.APID=KK.APID');
      sql.Add(' inner join CK_BanCP_KC F on A.CRID=F.CRID');
      sql.add('where A.MJID='''+Trim(XJID.Text)+'''');
      sql.Add(' and KCQty>0 and A.CRType=''检验入库'' ');
      Open;
    end;
    if ADOQueryTemp.IsEmpty=False  then
    begin
      if CDS_Main.Locate('MJID',Trim(ADOQueryTemp.fieldbyname('MJID').AsString),[])=True then
      begin
        Application.MessageBox('已经扫描过，不能再次扫描！','提示',0);
        Exit;
      end;
      with CDS_Main do
      begin
        Append;
        FieldByName('OrderNo').Value:=ADOQueryTemp.fieldbyname('OrderNo').Value;
        FieldByName('MPRTCodeName').Value:=ADOQueryTemp.fieldbyname('MPRTCodeName').Value;
        FieldByName('PRTColor').Value:=ADOQueryTemp.fieldbyname('PRTColor').Value;
        FieldByName('GangNo').Value:=ADOQueryTemp.fieldbyname('GangNo').Value;
        FieldByName('CRID').Value:=ADOQueryTemp.fieldbyname('CRID').Value;
        FieldByName('CRTime').Value:=SGetServerDateTime(ADOQueryCmd);
        FieldByName('KGQty').Value:=ADOQueryTemp.fieldbyname('kCKGQty').Value;
        FieldByName('Qty').Value:=ADOQueryTemp.fieldbyname('KCQty').Value;
        FieldByName('QtyUnit').Value:=ADOQueryTemp.fieldbyname('KCQtyUnit').Value;
        FieldByName('MJID').Value:=ADOQueryTemp.fieldbyname('MJID').Value;
        FieldByName('APID').Value:=ADOQueryTemp.fieldbyname('APID').Value;
        Post;
      end;
    end else
    begin
      Application.MessageBox('此卷已经出库，不能再次出库！','提示',0);
      Exit;
    end;
    XJID.Text:='';
  end;
end;

procedure TfrmBanCpCkSaoM.Button1Click(Sender: TObject);
var
  maxno:string;
begin
  if CDS_Main.IsEmpty then Exit;
  XJID.SetFocus;
  if Application.MessageBox('确定要执行此操作吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_Main do
    begin
      First;
      while not Eof do
      begin
        if GetLSNo(ADOQueryCmd,maxno,'ZC','CK_BanCp_CR',4,1)=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取出库最大号失败！','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update CK_BanCp_CR Set NowOutFlag=1 where MJID='''+Trim(CDS_Main.fieldbyname('MJID').AsString)+'''');
          SQL.Add(' and CRType=''正常出库'' ');
          ExecSQL;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_BanCp_CR where 1<>1');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('BCID').Value:=Trim(maxno);
          FieldByName('CRID').Value:=CDS_Main.fieldbyname('CRID').Value;
          FieldByName('CRTime').Value:=CDS_Main.fieldbyname('CRTime').Value;
          FieldByName('KGQty').Value:=CDS_Main.fieldbyname('KGQty').Value;
          FieldByName('Qty').Value:=CDS_Main.fieldbyname('Qty').Value;
          FieldByName('QtyUnit').Value:=CDS_Main.fieldbyname('QtyUnit').Value;
          FieldByName('MJID').Value:=CDS_Main.fieldbyname('MJID').Value;
          FieldByName('APID').Value:=CDS_Main.fieldbyname('APID').Value;
          FieldByName('CPType').Value:=CDS_Main.fieldbyname('CPType').Value;
          FieldByName('FillTime').Value:=SGetServerDateTime(ADOQueryTemp);
          FieldByName('Filler').Value:=Trim(DName);
          FieldByName('CRFlag').Value:='出库';
          FieldByName('CRType').Value:='正常出库';
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update CK_BanCp_KC set KCKgQty=0,KCQty=0 where CRID='+CDS_Main.fieldbyname('CRID').AsString);
          ExecSQL;
        end;
        Next;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    InitGrid();
    Application.MessageBox('出库成功！','提示',0);
    Exit;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('出库异常！','提示',0);
  end;
end;

procedure TfrmBanCpCkSaoM.Tv1DblClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  CDS_Main.Delete;
end;

procedure TfrmBanCpCkSaoM.Button2Click(Sender: TObject);
begin
  Close;
  WriteCxGrid('成品出库',Tv1,'成品仓库');
end;

end.
