unit U_RKSMEdit_Other;

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;

type
  TfrmRKSMEdit_other = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    PHYG: TBtnEditA;
    defstr2: TComboBox;
    Label16: TLabel;
    Label15: TLabel;
    packNo: TcxCurrencyEdit;
    Label14: TLabel;
    Label1: TLabel;
    Panel2: TPanel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    ADOQueryMain: TADOQuery;
    ADOQueryCmd: TADOQuery;
    CDSMJID: TClientDataSet;
    DataSource2: TDataSource;
    SmNO: TEdit;
    ADOQueryTmp: TADOQuery;
    V2Column1: TcxGridDBColumn;
    kcKw: TBtnEditA;
    Label5: TLabel;
    MovePanel1: TMovePanel;
    Edit2: TEdit;
    Button4: TButton;
    Label2: TLabel;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxStyle_gridRow: TcxStyle;
    cxStyle_gridFoot: TcxStyle;
    cxStyle_gridHead: TcxStyle;
    cxStyle_gridGroupBox: TcxStyle;
    cxStyle_yellow: TcxStyle;
    cxStyle_Red: TcxStyle;
    cxStyle_fontBlack: TcxStyle;
    cxStyle_fontclFuchsia: TcxStyle;
    cxStyle_fontclPurple: TcxStyle;
    cxStyle_fontclGreen: TcxStyle;
    cxStyle_fontclBlue: TcxStyle;
    cxStyle_fontclTeal: TcxStyle;
    cxStyle_fontclOlive: TcxStyle;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    ADOQuery1: TADOQuery;
    DataSource1: TDataSource;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridLevel2: TcxGridLevel;
    Tv3Column1: TcxGridDBColumn;
    ADOQueryTm: TADOQuery;
    DSTm: TDataSource;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);

    procedure FormCreate(Sender: TObject);
    procedure SmNOKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure TV2CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure kcKwBtnClick(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private
    procedure InitGrid();
    procedure InitMJID();
    function  SaveRK():Boolean;
    Procedure QueryRk();
    Procedure QueryTM();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmRKSMEdit_other: TfrmRKSMEdit_other;

implementation
uses
  U_DataLink,U_Fun, U_UserHelp, U_ZDYHelp;
{$R *.dfm}
Procedure TfrmRKSMEdit_other.QueryTm();
begin
  with ADOQueryTM do
  begin
    close;
    sql.Clear;
    sql.Add('select TOP 50 MJID');
    sql.Add('from WFB_MJJY1 A1   ');
  //  SQL.Add('WHERE exists(select MJID from WFB_MJJY1 X where X.APID=A.APID and X.MJID='+quotedstr(trim(smNO.Text)));
    sql.Add('where A1.mjstr2=''未入库'' ');
    open;
  end;
end;
Procedure TfrmRKSMEdit_other.QueryRk();
begin
{  with ADOQuery1 do
  begin
    close;
    sql.Clear;
    sql.Add('select A.*,hC_ps=A.AordQty2,JY_ps=(select count(MainID) from WFB_MJJY X where X.APID=A.APID),');
    sql.Add('JY_Qty=(select sum(MJLen) from WFB_MJJY X where X.APID=A.APID), ');
    sql.Add('RK_RollNum=(select sum(RollNum) from CK_BanCP_CR X where X.APID=A.APID and X.CRType=''检验入库''),');
    sql.Add('RK_Qty=(select sum(Qty) from CK_BanCP_CR X where X.APID=A.APID and X.CRType=''检验入库'') ');
    sql.Add('from JYOrder_sub_anPai A   ');
    SQL.Add('WHERE exists(select MJID from WFB_MJJY X where X.APID=A.APID and X.MJID='+quotedstr(trim(smNO.Text)));
    sql.Add(')');
    open;
  end;  }
end;
function TfrmRKSMEdit_other.SaveRK():Boolean;
var
  maxno:String;
  CRID:Integer;
  MaxCkNo,MaxCkSubNo:String;
  MhcTime:TdateTime;
begin
  Result:=false;
  ADOQueryCmd.Connection.BeginTrans;

  try
    with ADOQueryTmp  do
    begin
      Close;
      sql.Clear;
      SQL.add('Update CK_BanCP_CRID1 Set CRID=CRID+1');
      sql.Add('select * from CK_BanCP_CRID1');
      Open;
    end;
    CRID:=ADOQueryTmp.fieldbyname('CRID').AsInteger;
    if GetLSNo(ADOQueryTmp,MaxCkNo,'JT','CK_BanCP_CR1',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_CR1 where 1<>1');
            Open;
          end;

          mhcTime:=SGetServerDateTime(ADOQueryTmp);

          with ADOQueryCmd do
          begin
            Append;
            FieldByName('MainId').Value:=Trim(ADOQueryMain.fieldbyname('MainId').AsString);
            FieldByName('SubId').Value:=Trim(ADOQueryMain.fieldbyname('SubId').AsString);
            FieldByName('C_Code').Value:=Trim(ADOQueryMain.fieldbyname('C_Code').AsString);
            FieldByName('C_CodeName').Value:=Trim(ADOQueryMain.fieldbyname('C_CodeName').AsString);
            FieldByName('C_SPec').Value:=Trim(ADOQueryMain.fieldbyname('C_SPec').AsString);
            FieldByName('C_Color').Value:=Trim(ADOQueryMain.fieldbyname('C_Color').AsString);
            FieldByName('APID').Value:=Trim(ADOQueryMain.fieldbyname('APID').AsString);
            FieldByName('MJID').Value:=Trim(ADOQueryMain.fieldbyname('MJID').AsString);
            FieldByName('mjxh').Value:=Trim(ADOQueryMain.fieldbyname('mjxh').AsString);
            FieldByName('BCGangNO').Value:=Trim(ADOQueryMain.fieldbyname('BCGangNO').AsString);
            FieldByName('RCGangNO').Value:=Trim(ADOQueryMain.fieldbyname('RCGangNO').AsString);
            FieldByName('BCID').Value:=Trim(MaxCkNo);
            FieldByName('CRTime').Value:=FormatDateTime('yyyy-MM-dd',mhcTime);
            FieldByName('CRFlag').Value:='入库';
            FieldByName('CRType').Value:='检验入库';
            FieldByName('CRID').Value:=CRID;
            FieldByName('KGQty').Value:=0;
            FieldByName('Qty').Value:=ADOQueryMain.fieldbyname('MJLen').Value;
            FieldByName('RollNum').Value:=1;
            FieldByName('QtyUnit').Value:=ADOQueryMain.fieldbyname('MJTypeOther').Value;
            FieldByName('CPType').Value:=ADOQueryMain.fieldbyname('MJType').Value;
            FieldByName('Note').Value:=ADOQueryMain.fieldbyname('Note').Value;
            FieldByName('Filler').Value:=Trim(DName);
            FieldByName('FillTime').Value:=SGetServerDateTime(ADOQueryTmp);
         //   FieldByName('kcKw').Value:=Trim(kcKw.Text);
         //   fieldbyname('kcKw').Value:=GetKw(ADOQueryTmp,ADOQueryMain.fieldbyname('C_CodeName').AsString,ADOQueryMain.fieldbyname('C_Color').AsString);
            Post;
          end;

          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_BanCP_KC1 where 1<>1');
            Open;
          end;
          with ADOQueryCmd do
          begin
            Append;
            FieldByName('CRID').Value:=CRID;
            FieldByName('BCID').Value:=Trim(MaxCkNo);
            FieldByName('CRTime').Value:=FormatDateTime('yyyy-MM-dd',mhcTime);
            FieldByName('CRType').Value:='检验入库';
            FieldByName('CPType').Value:=ADOQueryMain.fieldbyname('MJType').Value;
            FieldByName('C_Code').Value:=Trim(ADOQueryMain.fieldbyname('C_Code').AsString);
            FieldByName('C_CodeName').Value:=Trim(ADOQueryMain.fieldbyname('C_CodeName').AsString);
            FieldByName('C_SPec').Value:=Trim(ADOQueryMain.fieldbyname('C_SPec').AsString);
            FieldByName('C_Color').Value:=Trim(ADOQueryMain.fieldbyname('C_Color').AsString);
            FieldByName('BCGangNO').Value:=Trim(ADOQueryMain.fieldbyname('BCGangNO').AsString);
            FieldByName('RCGangNO').Value:=Trim(ADOQueryMain.fieldbyname('RCGangNO').AsString);
            FieldByName('MJID').Value:=Trim(ADOQueryMain.fieldbyname('MJID').AsString);
            FieldByName('KCKGQty').Value:=0;
            FieldByName('KCQty').Value:=ADOQueryMain.fieldbyname('MJlen').Value;
            FieldByName('KCRollNum').Value:=1;
            FieldByName('KCQtyUnit').Value:=ADOQueryMain.fieldbyname('MJTypeOther').Value;
            FieldByName('mjxh').Value:=Trim(ADOQueryMain.fieldbyname('mjxh').AsString);
            FieldByName('KCNote').Value:=ADOQueryMain.fieldbyname('Note').Value;
          //  FieldByName('kcKw').Value:=Trim(kcKw.Text);
          //  fieldbyname('kcKw').Value:=GetKw(ADOQueryTmp,ADOQueryMain.fieldbyname('C_CodeName').AsString,ADOQueryMain.fieldbyname('C_Color').AsString);
            Post;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update WFB_MJJY1 Set MJStr2=''已入库'' where MJID='''+Trim(ADOQueryMain.fieldbyname('MJID').AsString)+'''');
            ExecSQL;
          end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=true;
  except
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;

procedure TfrmRKSMEdit_other.InitGrid();
begin
  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.* from CK_BanCP_CR1 A ');
    SQL.Add('where 1=2 ');
    Open;
  end;
  SCreateCDS20(ADOQueryMain,CDSMJID);
  SInitCDSData20(ADOQueryMain,CDSMJID);
end;

procedure TfrmRKSMEdit_other.InitMJID();
begin
  MovePanel1.Visible:=false;
  with ADOQueryMain do
  begin
    Close;
    sql.Clear;
    SQL.Add('select A.* from WFB_MJJY1 A');
    sql.Add('where MJID='+quotedstr(trim(SmNO.Text)));
    Open;
    if not IsEmpty  then
    begin
      IF trim(Fieldbyname('MJstr2').AsString)='已入库' then
      begin
        CDSMJID.Append;
        CDSMJID.FieldByName('SDefNote').Value:='此条码已入库';
        CDSMJID.FieldByName('MJID').Value:=trim(SmNO.Text);
        CDSMJID.Post;
      end
      else
      begin
        IF saveRk() then
        begin
            CDSMJID.Append;
            CDSMJID.FieldByName('SDefNote').Value:='入库成功';
            CDSMJID.FieldByName('MJID').Value:=trim(ADOQueryMain.Fieldbyname('MJID').AsString);
//            CDSMJID.FieldByName('CRID').Value:=trim(ADOQueryMain.Fieldbyname('CRID').AsString);
            CDSMJID.FieldByName('C_CodeName').Value:=trim(ADOQueryMain.Fieldbyname('C_CodeName').AsString);
            CDSMJID.FieldByName('C_Color').Value:=trim(ADOQueryMain.Fieldbyname('C_Color').AsString);
            CDSMJID.FieldByName('CPType').Value:=trim(ADOQueryMain.Fieldbyname('MJType').AsString);
            CDSMJID.FieldByName('RollNum').Value:=1;
            CDSMJID.FieldByName('Qty').Value:=ADOQueryMain.Fieldbyname('MJLen').AsFloat;
            CDSMJID.FieldByName('QtyUnit').Value:=trim(ADOQueryMain.Fieldbyname('MJTypeOther').AsString);
            CDSMJID.FieldByName('mainID').Value:=trim(ADOQueryMain.Fieldbyname('mainID').AsString);
            CDSMJID.FieldByName('subID').Value:=trim(ADOQueryMain.Fieldbyname('subID').AsString);
            CDSMJID.FieldByName('RCGangNO').Value:=trim(ADOQueryMain.Fieldbyname('RCGangNO').AsString);
            CDSMJID.FieldByName('BCgangNO').Value:=trim(ADOQueryMain.Fieldbyname('BCgangNO').AsString);
            CDSMJID.Post;
            QueryRk();
            QueryTm();
        end
        else
        begin
          CDSMJID.Append;
          CDSMJID.FieldByName('SDefNote').Value:='此条码入库失败';
          CDSMJID.FieldByName('MJID').Value:=trim(SmNO.Text);
          CDSMJID.Post;
        end;
      end;
    end
    else
    begin
      CDSMJID.Append;
      CDSMJID.FieldByName('SDefNote').Value:='此条码不存在';
      CDSMJID.FieldByName('MJID').Value:=trim(SmNO.Text);
      CDSMJID.Post;
    end;
  end;
  IF trim(CDSMJID.FieldByName('SDefNote').AsString)<>'入库成功' then
  begin
    MovePanel1.Visible:=true;
    Edit2.Text:=SmNO.Text;
    Label2.Caption:=trim(CDSMJID.FieldByName('SDefNote').AsString);
  end;
end;

procedure TfrmRKSMEdit_other.FormDestroy(Sender: TObject);
begin
  frmRKSMEdit_other:=nil;
end;

procedure TfrmRKSMEdit_other.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  writeCxGrid(self.Caption+tv1.Name,Tv1,'成品仓库');
  writeCxGrid(self.Caption+Tv2.Name,Tv2,'成品仓库');
  writeCxGrid(self.Caption+Tv3.Name,Tv3,'成品仓库');
  Action:=cahide;
end;

procedure TfrmRKSMEdit_other.TBCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmRKSMEdit_other.FormCreate(Sender: TObject);
begin
  Panel2.Align:=alClient;
  readCxGrid(self.Caption+tv1.Name,Tv1,'成品仓库');
  readCxGrid(self.Caption+Tv2.Name,Tv2,'成品仓库');
  readCxGrid(self.Caption+Tv3.Name,Tv3,'成品仓库');
end;

procedure TfrmRKSMEdit_other.SmNOKeyPress(Sender: TObject; var Key: Char);
begin
  IF key=#13 then
  begin
    if trim(smno.Text)='' then exit;
    initMJID();
    smno.Text:='';
    smno.SetFocus;
  end;
end;

procedure TfrmRKSMEdit_other.FormShow(Sender: TObject);
begin
  MovePanel1.Left:=(Width-MovePanel1.Width) div 2;
  MovePanel1.top:=(Height-MovePanel1.Height-200) div 2;
  InitGrid();
  queryTm();
end;

procedure TfrmRKSMEdit_other.TV2CustomDrawCell(Sender: TcxCustomGridTableView;
  ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
  var ADone: Boolean);
var
  i:integer;
begin
  i:=tv2.GetColumnByFieldName('SDefNote').Index;
  if (AViewInfo.GridRecord.Values[i]<>'入库成功')  then
  ACanvas.Brush.Color:=clred;
end;

procedure TfrmRKSMEdit_other.kcKwBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='CPKWFlag';
      flagname:='库位信息';
      V1Name.Caption:='库位名称';
      V1Note.Caption:='英文名称';
//      MainType:=Trim(DName);
      fnote:=FALSE;
      if ShowModal=1 then
      begin
        kcKw.Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmRKSMEdit_other.Button4Click(Sender: TObject);
begin
  MovePanel1.Visible:=False;
  SmNO.SetFocus;
end;

end.
