unit U_YPCRIn;

interface

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

type
  TfrmYPCRIn = class(TForm)
    ToolBar1: TToolBar;
    BtnSave: TToolButton;
    BtnExit: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    v1P_ChnName: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    v1Quantity: TcxGridDBColumn;
    v1UnitName: TcxGridDBColumn;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    DataSource1: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryMain: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ToolButton4: TToolButton;
    v1Column1: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v1Column3: TcxGridDBColumn;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    procedure FormCreate(Sender: TObject);
    procedure BtnExitClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure BtnSaveClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure v1P_ChnNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column18PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column4PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    FCRID:String;
    procedure InitGrid();
    function  SaveData():Boolean;
    procedure SaveKCData();
    { Private declarations }
  public
    FMainID:string;
    FCopyInt:Integer;
    { Public declarations }
  end;

var
  frmYPCRIn: TfrmYPCRIn;

implementation
uses
  U_DataLink,U_RTFun,U_ZDYHelp,U_CPSel;

{$R *.dfm}

procedure TfrmYPCRIn.FormCreate(Sender: TObject);
begin
  cxGrid1.Align:=alClient;
end;

procedure TfrmYPCRIn.BtnExitClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('样品入库STInput',Tv1,'样品仓库');
end;
procedure TfrmYPCRIn.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      SQL.Add('select  A.CRTime,A.CRID,A.BCID,A.Qty,A.QtyUnit,A.CYColor,A.Note,A.CYType,A.BCID ');
      sql.Add(' ,A.CRPrice,A.BZTYpe,A.PBFactory,A.RCFactory,A.HJGFactory,A.CYID,A.WeiZhi,A.MiLenNote');
      SQL.Add(',B.CYNO,B.CYName,B.CYMF,B.CYKZ,B.CYSpec,B.CYCF,B.CYHX');
      sql.Add(' from CK_YP_CR A');
      sql.Add(' inner join CP_YDang B on A.CYID=B.CYID');
      SQL.Add(' where BCId='''+Trim(FMainID)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Sub);
    SInitCDSData20(ADOQueryMain,CDS_Sub);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmYPCRIn.ToolButton2Click(Sender: TObject);
var
  FDate:TDateTime;
begin
  FDate:=SGetServerDate(ADOQueryTemp);
  try
    frmCPSel:=TfrmCPSel.Create(self);
    with frmCPSel do
    begin
      frmCPSel.Align:=alClient;
      if ShowModal=1 then
      begin
        with CDS_Sub do
        begin
          Append;
          FieldByName('CRTime').Value:=FDate;
          FieldByName('CYID').Value:=Trim(CDS_Main.fieldbyname('CYID').AsString);
          FieldByName('CYNo').Value:=Trim(CDS_Main.fieldbyname('CYNo').AsString);
          FieldByName('CYName').Value:=Trim(CDS_Main.fieldbyname('CYName').AsString);
          FieldByName('CYSpec').Value:=Trim(CDS_Main.fieldbyname('CYSpec').AsString);
          FieldByName('CYCF').Value:=Trim(CDS_Main.fieldbyname('CYCF').AsString);
          FieldByName('CYMF').Value:=Trim(CDS_Main.fieldbyname('CYMF').AsString);
          FieldByName('CYKZ').Value:=Trim(CDS_Main.fieldbyname('CYKZ').AsString);
          FieldByName('CYHX').Value:=Trim(CDS_Main.fieldbyname('CYHX').AsString);
          FieldByName('CYColor').Value:=Trim(CDS_Main.fieldbyname('CYColor').AsString);
          Post;
        end;
      end;
    end;
  finally
    frmCPSel.Free;
  end;

end;

procedure TfrmYPCRIn.ToolButton3Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then Exit;
  if Trim(CDS_Sub.fieldbyname('BCId').AsString)<>'' then
  begin
    Application.MessageBox('不能在此删除已经保存的数据！','提示',0);
    Exit;
  end;
  CDS_Sub.Delete;
end;

procedure TfrmYPCRIn.ToolButton4Click(Sender: TObject);
var
  YCLCode,GYS:String;
begin
  if CDS_Sub.IsEmpty then Exit;
  CopyAddRow(tv1,CDS_Sub);
  with CDS_Sub do
  begin
    Edit;
    FieldByName('BCID').Value:='';
    FieldByName('CRID').Value:=NULL;
    FieldByName('CYColor').Value:='';
    FieldByName('Note').Value:='';
    Post;
  end;
end;

procedure TfrmYPCRIn.ToolButton1Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then Exit;
  ToolBar1.SetFocus;
  OneKeyPost(Tv1,CDS_Sub);
end;

function TfrmYPCRIn.SaveData():Boolean;
var
  MaxNo,SubId,strSQL,LQuantity,LQty,YLQty:string;
  KID,i,PStatus,KID1,KID2:Integer;
begin
  try
    Result:=False;
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not Eof do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select Top 1* from CK_YP_CR where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
          sql.Add(' and CYType='''+Trim(CDS_Sub.fieldbyname('CYType').AsString)+'''');
          Open;
        end;
        if ADOQueryTemp.IsEmpty=False then
        begin
          if Trim(ADOQueryTemp.FieldByName('QtyUnit').AsString)<>Trim(CDS_Sub.fieldbyname('QtyUnit').AsString) then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            CDS_Sub.EnableControls;
            Application.MessageBox('单位错误!','提示',0);
            Exit;
          end;
        end;
        if Trim(CDS_Sub.FieldByName('Qty').AsString)='' then
        begin
          CDS_Sub.Edit;
          CDS_Sub.FieldByName('Qty').Value:='0';
          CDS_Sub.Post;
        end;


       { with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_YP_KC where WeiZhi='''+Trim(CDS_Sub.fieldbyname('WeiZhi').AsString)+'''');
          sql.Add(' and KCQty>0');
          Open;
        end;
        if ADOQueryTemp.RecordCount>1 then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          CDS_Sub.EnableControls;
          Application.MessageBox('此位置已经被占用!','提示',0);
          Exit;
        end; }
        SaveKCData();
        //保存
        if Trim(CDS_Sub.fieldbyname('BCId').AsString)<>'' then
        begin
          SubId:=Trim(CDS_Sub.fieldbyname('BCId').AsString);
        end else
        begin
          if GetLSNo(ADOQueryCmd,SubId,'BC','CK_YP_CR',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            CDS_Sub.EnableControls;
            Application.MessageBox('取最大号失败！','提示',0);
            Exit;
          end;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from CK_YP_CR where BCId='''+Trim(SubId)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(CDS_Sub.fieldbyname('BCId').AsString)<>'' then
            Edit
          else
            Append;
          FieldByName('BCId').Value:=Trim(SubId);
          //SSetSaveDataCDSNew(ADOQueryCmd,Tv1,CDS_Sub,'CK_YP_CR',2);
          RTSetSaveDataCDS(ADOQueryCmd,Tv1,CDS_Sub,'CK_YP_CR',2);
          FieldByName('CRTime').Value:=CDS_Sub.fieldbyname('CRtime').Value;
          FieldByName('CYID').Value:=CDS_Sub.fieldbyname('CYID').Value;
          FieldByName('CYColor').Value:=CDS_Sub.fieldbyname('CYColor').Value;
          FieldByName('CYType').Value:=CDS_Sub.fieldbyname('CYType').Value;
          FieldByName('QtyUnit').Value:=CDS_Sub.fieldbyname('QtyUnit').Value;
          FieldByName('Note').Value:=CDS_Sub.fieldbyname('Note').Value;
          FieldByName('Qty').Value:=CDS_Sub.fieldbyname('Qty').Value;
          FieldByName('QtyFlag').Value:=1;
          FieldByName('CRID').Value:=StrToInt(FCRID);
          FieldByName('CRFlag').Value:='入库';
          FieldByName('CRType').Value:='正常入库';
          if Trim(CDS_Sub.fieldbyname('BCId').AsString)='' then
          begin
            FieldByName('Filler').Value:=Trim(DName);
          end
          else
          begin
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          end;
          FieldByName('Note').Value:=Trim(CDS_Sub.fieldbyname('Note').AsString);
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update CK_YP_KC Set KCQty=(select sum(Qty*QtyFlag) from CK_YP_CR A where A.CRID=CK_YP_KC.CRID)');
          sql.Add(' where CRID='+Trim(FCRID));
          ExecSQL;
        end;
        Next;
      end;
    end;
    CDS_Sub.EnableControls;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete CK_YP_KC where CRID in(select CRID from CK_YP_KC ');
      sql.Add('  where not exists(select * from CK_YP_CR A where A.CRID=CK_YP_KC.CRID))');
      ExecSQL;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
    FMainID:=SubId;
    ModalResult:=1;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    CDS_Sub.EnableControls;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;
procedure TfrmYPCRIn.SaveKCData();
var
  LQty:String;
  PState:Integer;
begin
  PState:=0;
  if Trim(CDS_Sub.fieldbyname('BCID').AsString)<>'' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_YP_CR where BCID='''+Trim(CDS_Sub.fieldbyname('BCID').AsString)+'''');
      Open;
    end;
    LQty:=Trim(ADOQueryTemp.fieldbyname('Qty').AsString);
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('UPdate CK_YP_KC Set KCQty=KCQty-'+LQty);
      SQL.Add(' where CRID='+Trim(ADOQueryTemp.fieldbyname('CRID').AsString));
      ExecSQL;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from CK_YP_KC');
    sql.Add(' where CYID='''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
    sql.Add(' and isnull(CYType,'''')='''+Trim(CDS_Sub.fieldbyname('CYType').AsString)+'''');
    sql.Add(' and isnull(CYColor,'''')='''+Trim(CDS_Sub.fieldbyname('CYColor').AsString)+'''');
    sql.Add(' and isnull(PBFactory,'''')='''+Trim(CDS_Sub.fieldbyname('PBFactory').AsString)+'''');
    sql.Add(' and isnull(RCFactory,'''')='''+Trim(CDS_Sub.fieldbyname('RCFactory').AsString)+'''');
    sql.Add(' and isnull(HJGFactory,'''')='''+Trim(CDS_Sub.fieldbyname('HJGFactory').AsString)+'''');
    sql.Add(' and isnull(KCQtyUnit,'''')='''+Trim(CDS_Sub.fieldbyname('QtyUnit').AsString)+'''');
    sql.Add(' and isnull(WeiZhi,'''')='''+Trim(CDS_Sub.fieldbyname('WeiZhi').AsString)+'''');
    sql.Add(' and isnull(KCNote,'''')='''+Trim(CDS_Sub.fieldbyname('Note').AsString)+'''');
    //sql.Add(' and isnull(MiLenNote,'''')='''+Trim(CDS_Sub.fieldbyname('MiLenNote').AsString)+'''');
    // sql.Add(' and isnull(BZType,'''')='''+Trim(CDS_Sub.fieldbyname('BZType').AsString)+'''');
    {if Trim(CDS_Sub.fieldbyname('CRPrice').AsString)<>'' then
    begin
      sql.Add(' and CRPrice='+Trim(CDS_Sub.fieldbyname('CRPrice').AsString));
    end else
    begin
      sql.Add(' and CRPrice=0');
    end; }

    Open;
  end;
  if ADOQueryTemp.IsEmpty then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('update CK_YP_CRID Set CRID=CRID+1 select * from CK_YP_CRID');
      ExecSQL;
      Open;
    end;
    FCRID:=Trim(ADOQueryCmd.fieldbyname('CRID').AsString);
  end else
  begin
    FCRID:=Trim(ADOQueryTemp.fieldbyname('CRID').AsString);
    PState:=1;
  end;
  if PState=0 then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('insert into CK_YP_KC(CRID,CYID,CYColor,PBFactory,KCQtyUnit,KCValid,CYType,KCNote,RCFactory,HJGFactory,WeiZhi)');
      sql.Add(' select '+FCRID);
      sql.Add(','''+Trim(CDS_Sub.fieldbyname('CYID').AsString)+'''');
      sql.Add(','''+Trim(CDS_Sub.fieldbyname('CYColor').AsString)+'''');
      sql.Add(','''+Trim(CDS_Sub.fieldbyname('PBFactory').AsString)+'''');
      sql.Add(','''+Trim(CDS_Sub.fieldbyname('QtyUnit').AsString)+'''');
      sql.Add(',''Y'' ');
      sql.Add(','''+Trim(CDS_Sub.fieldbyname('CYType').AsString)+'''');
      sql.Add(','''+Trim(CDS_Sub.fieldbyname('Note').AsString)+'''');
     // sql.Add(','''+Trim(CDS_Sub.fieldbyname('BZType').AsString)+'''');
      sql.Add(','''+Trim(CDS_Sub.fieldbyname('RCFactory').AsString)+'''');
      sql.Add(','''+Trim(CDS_Sub.fieldbyname('HJGFactory').AsString)+'''');
      {if  Trim(CDS_Sub.fieldbyname('CRPrice').AsString)<>'' then
        sql.Add(','+Trim(CDS_Sub.fieldbyname('CRPrice').AsString))
      else
        sql.Add(',0'); }
      sql.Add(','''+Trim(CDS_Sub.fieldbyname('WeiZhi').AsString)+'''');
      //sql.Add(','''+Trim(CDS_Sub.fieldbyname('MiLenNote').AsString)+'''');
      ExecSQL;
    end;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    sql.Add('Update CK_YP_KC Set KCQty=KCQty+'+Trim(CDS_Sub.fieldbyname('Qty').AsString));
    SQL.Add(' where CRID='+FCRID);
    ExecSQL;
  end;
end;

procedure TfrmYPCRIn.BtnSaveClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then Exit;
  if CDS_Sub.Locate('CRTime',Null,[]) then
  begin
    Application.MessageBox('入库时间不能为空!','提示',0);
    Exit;
  end;
 { if CDS_Sub.Locate('CYNO',Null,[]) then
  begin
    Application.MessageBox('样品编号不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('CYColor',Null,[]) then
  begin
    Application.MessageBox('颜色不能为空!','提示',0);
    Exit;
  end;  }
  if CDS_Sub.Locate('Qty',Null,[]) then
  begin
    Application.MessageBox('数量不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('QtyUnit',Null,[]) then
  begin
    Application.MessageBox('单位不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('CYType',Null,[]) then
  begin
    Application.MessageBox('样品类型不能为空!','提示',0);
    Exit;
  end;
 { if CDS_Sub.Locate('CRPrice',Null,[]) then
  begin
    Application.MessageBox('单价不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('BZType',Null,[]) then
  begin
    Application.MessageBox('币种不能为空!','提示',0);
    Exit;
  end;   }
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
  end;
end;

procedure TfrmYPCRIn.FormShow(Sender: TObject);
var
  i:Integer;
begin
  readCxGrid('样品入库STInput',Tv1,'样品仓库');
  InitGrid();
  if FCopyInt=99 then
  begin
    FMainID:='';
    with CDS_Sub do
    begin
      Edit;
      FieldByName('BCID').Value:='';
      FieldByName('WeiZhi').Value:='';
      FieldByName('CRID').Value:=Null;
      Post;
    end;
  end;
  if Trim(FMainID)<>'' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_YP_CR where CRID='+Trim(CDS_Sub.fieldbyname('CRID').AsString));
      sql.Add(' and CRFlag=''出库'' ');
      Open;
    end;
    if ADOQueryTemp.IsEmpty=False then
    begin
      for i:=0 to Tv1.ColumnCount-1 do
      begin
        Tv1.Columns[i].Options.Focusing:=False;
      end;
      v1Column1.Options.Focusing:=True;
      v1Quantity.Options.Focusing:=True;
    end;
  end;
  {FDate:=SGetServerDate(ADOQueryTemp);
  if Trim(FMainID)='' then
  begin
    with CDS_Sub do
    begin
      Append;
      FieldByName('CRTime').Value:=FDate;
      Post;
    end;
  end; }
end;

procedure TfrmYPCRIn.v1P_ChnNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='ordcolor';
      flagname:='颜色';
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('CYColor').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYPCRIn.v1Column18PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='WeiZhi';
      flagname:='存放位置';
      if ShowModal=1 then
      begin
        CDS_Sub.Edit;
        CDS_Sub.FieldByName('WeiZhi').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmYPCRIn.v1Column4PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='CYType';
      flagname:='样品类型';
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('CYType').Value:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZDYName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.
