unit U_JWLIn;

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;

type
  TfrmJWLIn = class(TForm)
    ToolBar1: TToolBar;
    BtnSave: TToolButton;
    BtnExit: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    v1P_ChnName: TcxGridDBColumn;
    v1ShortName: 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;
    v1DepotClass: TcxGridDBColumn;
    v1Column2: 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 v1ShortNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1UnitNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    procedure InitGrid();
    function  SaveData():Boolean;
    { Private declarations }
  public
    FMainID:string;
    { Public declarations }
  end;

var
  frmJWLIn: TfrmJWLIn;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp;

{$R *.dfm}

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

procedure TfrmJWLIn.BtnExitClick(Sender: TObject);
begin
  Close;
end;
procedure TfrmJWLIn.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select A.*,B.*,GYSName=C.ZDYName from CK_YCL_Main A inner join CK_YCL_Sub B on A.MainId=B.MainId ');
      sql.Add(' left join KH_ZDY C on A.GYS=C.ZDYNO');
      SQL.Add(' where A.MainId='''+Trim(FMainID)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Sub);
    SInitCDSData20(ADOQueryMain,CDS_Sub);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmJWLIn.ToolButton2Click(Sender: TObject);
begin
  with CDS_Sub do
  begin
    Append;
    Post;
  end;
end;

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

procedure TfrmJWLIn.ToolButton4Click(Sender: TObject);
begin
  CopyAddRow(tv1,CDS_Sub);
end;

procedure TfrmJWLIn.ToolButton1Click(Sender: TObject);
begin
  ToolBar1.SetFocus;
  OneKeyPost(Tv1,CDS_Sub);
end;

function TfrmJWLIn.SaveData():Boolean;
var
  MaxNo,SubId,strSQL,LQuantity,LQty:string;
  KID,i,PStatus,KID1:Integer;
begin
  try
    Result:=False;
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Sub.First;
    while not CDS_Sub.Eof  do
    begin
      if Trim(CDS_Sub.FieldByName('CRQty').AsString)='' then
      begin
        CDS_Sub.Edit;
        CDS_Sub.FieldByName('CRQty').Value:='0';
        CDS_Sub.Post;
      end;
      if Trim(CDS_Sub.fieldbyname('MainId').AsString)<>'' then
      begin
        MaxNo:=Trim(CDS_Sub.fieldbyname('MainId').AsString);
        SubId:=Trim(CDS_Sub.fieldbyname('SubId').AsString);
        KID:=CDS_Sub.fieldbyname('CRID').AsInteger;
      end else
      begin
        if GetLSNo(ADOQueryCmd,MaxNo,'MR','CK_YCL_Main',4,1)=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取最大号失败！','提示',0);
          Exit;
        end;
        if GetLSNo(ADOQueryCmd,SubId,'SR','CK_YCL_Sub',4,1)=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取最大号失败！','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('UPDATE CK_CRID SET CRID = CRID+1');
          SQL.Add('SELECT CRID FROM CK_CRID');
          Open;
          KID:=fieldbyname('CRID').AsInteger;
        end;
      end;
      
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from CK_YCL_Sub where MainId='''+Trim(MaxNo)+'''');
        SQL.Add(' and CRID='+Inttostr(KID));
        Open;
        if not ADOQueryTemp.IsEmpty then
        begin
          LQuantity:=ADOQueryTemp.fieldbyname('CRQty').AsString;
        end else
        begin
          LQuantity:='0.00';
        end;
      end;
      //保存主表
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add('select * from CK_YCL_Main where MainId='''+Trim(MaxNo)+'''');
        Open;
      end;
      with ADOQueryCmd do
      begin
        if Trim(CDS_Sub.fieldbyname('MainId').AsString)<>'' then
          Edit
        else
          Append;
        FieldByName('MainId').Value:=Trim(MaxNo);
        FieldByName('CRFlag').Value:='入库';
        FieldByName('Filler').Value:=Trim(DName);
        FieldByName('FillTime').Value:=SGetServerDateTime(ADOQueryTemp);
        FieldByName('CRTime').Value:=Trim(CDS_Sub.fieldbyname('CRTime').AsString);
        FieldByName('CRType').Value:=Trim(CDS_Sub.fieldbyname('CRType').AsString);
        FieldByName('GYS').Value:=Trim(CDS_Sub.fieldbyname('GYS').AsString);
        FieldByName('CKName').Value:=Trim(DParameters1);
        Post;
      end;
      //保存从表
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add('select * from CK_YCL_Sub where SubId='''+Trim(SubId)+'''');
        Open;
      end;
      with ADOQueryCmd do
      begin
        if Trim(CDS_Sub.fieldbyname('SubId').AsString)<>'' then
          Edit
        else
          Append;
        FieldByName('MainId').Value:=Trim(MaxNo);
        FieldByName('SubId').Value:=Trim(SubId);
        FieldByName('CRID').Value:=KID;
        SSetSaveDataCDSNew(ADOQueryCmd,Tv1,CDS_Sub,'CK_YCL_Sub',2);
        Post;
      end;
      //保存到库存表
      if Trim(CDS_Sub.fieldbyname('MainId').AsString)='' then
      begin
        strSQL := 'INSERT CK_YCL_KC ('
               + 'CRID,'
               + 'RKNo,'
               + 'KCQty'
               + ') VALUES ('
               + IntToStr(KID) + ','
               + QuotedStr(Trim(SubId)) + ','
               + QuotedStr(Trim(CDS_Sub.fieldbyname('CRQty').AsString)) + ''
               + ')';


      end else
      begin
        //更新到库存表
        strSQL := 'UPDATE CK_YCL_KC SET '
                + 'RKNo = ' + QuotedStr(Trim(SubId)) + ','
                + 'KCQty =KCQty-'+LQuantity+'+'+ CDS_Sub.fieldbyname('CRQty').AsString+ ' '
                + 'WHERE CRID = ' + IntToStr(KID);
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add(strSQL);
        ExecSQL;
      end;
      CDS_Sub.Next;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
    FMainID:=MaxNo;
    ModalResult:=1;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

procedure TfrmJWLIn.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('YCLName',Null,[]) then
  begin
    Application.MessageBox('物料名称不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('CRType',Null,[]) then
  begin
    Application.MessageBox('入库类型不能为空!','提示',0); 
    Exit;
  end;
  if CDS_Sub.Locate('CRQty',Null,[]) then
  begin
    Application.MessageBox('数量不能为空!','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
  end;
end;

procedure TfrmJWLIn.FormShow(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmJWLIn.v1P_ChnNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='YCL';
      flagname:='原材料名称';
      if ShowModal=1 then
      begin
        Self.CDS_Sub.Edit;
        Self.CDS_Sub.FieldByName('YCLCode').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        Self.CDS_Sub.FieldByName('YCLName').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmJWLIn.v1ShortNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='GYS';
      flagname:='供应商名称';
      if ShowModal=1 then
      begin
        Self.CDS_Sub.Edit;
        Self.CDS_Sub.FieldByName('GYS').Value:=Trim(ClientDataSet1.fieldbyname('ZDYNo').AsString);
        Self.CDS_Sub.FieldByName('GYSName').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmJWLIn.v1UnitNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='DW';
      flagname:='单位';
      if ShowModal=1 then
      begin
        Self.CDS_Sub.Edit;
        Self.CDS_Sub.FieldByName('CRUnit').Value:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.
