unit U_BanCpHCSaoM;

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
  TfrmBanCpHCSaoM = class(TForm)
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: 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;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    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 Button2Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
  public
    { Public declarations }
  end;

var
  frmBanCpHCSaoM: TfrmBanCpHCSaoM;

implementation
uses
U_DataLink,U_Fun ;

{$R *.dfm}

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

procedure TfrmBanCpHCSaoM.FormDestroy(Sender: TObject);
begin
  frmBanCpHCSaoM:=nil;
end;
procedure TfrmBanCpHCSaoM.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      SQL.Add('select A.*,B.OrderNo,B.MPRTCodeName,C.PRTColor,B.MPRTMF,B.MPRTKZ  ');
      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('where 1<>1');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

procedure TfrmBanCpHCSaoM.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,B.MPRTMF,B.MPRTKZ,F.KCQty,F.KCKgQty  ');
      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 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('MPRTMF').Value:=ADOQueryTemp.fieldbyname('MPRTMF').Value;
        FieldByName('MPRTKZ').Value:=ADOQueryTemp.fieldbyname('MPRTKZ').Value;
        FieldByName('CRID').Value:=ADOQueryTemp.fieldbyname('CRID').Value;
        FieldByName('CRTime').Value:=SGetServerDateTime(ADOQueryCmd);
        FieldByName('KGQty').Value:=ADOQueryTemp.fieldbyname('KGQty').Value;
        FieldByName('Qty').Value:=ADOQueryTemp.fieldbyname('Qty').Value;
        FieldByName('QtyUnit').Value:=ADOQueryTemp.fieldbyname('QtyUnit').Value;
        FieldByName('MainID').Value:=ADOQueryTemp.fieldbyname('MainID').Value;
        FieldByName('SubID').Value:=ADOQueryTemp.fieldbyname('SubID').Value;
        FieldByName('APID').Value:=ADOQueryTemp.fieldbyname('APID').Value;
        FieldByName('CPType').Value:=ADOQueryTemp.fieldbyname('CPType').Value;
        FieldByName('MJID').Value:=ADOQueryTemp.fieldbyname('MJID').Value;
        Post;
      end;
    end else
    begin
      Application.MessageBox('此卷已在仓库中，无需回仓！','提示',0);
      Exit;
    end;
    XJID.Text:='';
  end;
end;

procedure TfrmBanCpHCSaoM.Button1Click(Sender: TObject);
var
  maxno:string;
begin
  if CDS_Main.IsEmpty then Exit;
  if CDS_Main.Locate('KgQty',0,[]) then
  begin
    Application.MessageBox('回仓公斤数不能为0！','提示',0);
    Exit;
  end;
  if CDS_Main.Locate('Qty',0,[]) then
  begin
    Application.MessageBox('回仓长度不能为0！','提示',0);
    Exit;
  end;
  if CDS_Main.Locate('KgQty',null,[]) then
  begin
    Application.MessageBox('回仓公斤数不能为空！','提示',0);
    Exit;
  end;
  if CDS_Main.Locate('Qty',null,[]) then
  begin
    Application.MessageBox('回仓长度不能为空！','提示',0);
    Exit;
  end;
  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,'HC','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('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('MainID').Value:=CDS_Main.fieldbyname('MainID').Value;
          FieldByName('SubID').Value:=CDS_Main.fieldbyname('SubID').Value;
          FieldByName('APID').Value:=CDS_Main.fieldbyname('APID').Value;
          FieldByName('MJID').Value:=CDS_Main.fieldbyname('MJID').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='+cds_main.fieldbyname('KgQty').AsString);
          SQL.Add(',KCQty='+cds_main.fieldbyname('Qty').AsString);
          sql.Add(' 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 TfrmBanCpHCSaoM.Button2Click(Sender: TObject);
begin
  Close;
  WriteCxGrid('成品回仓',Tv1,'成品仓库');
end;

end.
