unit U_BaseIn;

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
  TfrmBaseIn = class(TForm)
    ToolBar1: TToolBar;
    BtnSave: TToolButton;
    BtnExit: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    v1PlanNo: TcxGridDBColumn;
    v1P_ChnName: TcxGridDBColumn;
    v1P_ChnName10: TcxGridDBColumn;
    v1ShortName: TcxGridDBColumn;
    v1DefStr3: TcxGridDBColumn;
    v1DefFlt1: TcxGridDBColumn;
    v1DefStr4: 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;
    v1DefStr2: TcxGridDBColumn;
    v1DefStr1: TcxGridDBColumn;
    v1P_Code: TcxGridDBColumn;
    v1RollUnit: TcxGridDBColumn;
    ToolButton1: TToolButton;
    v1P_SeqNo: TcxGridDBColumn;
    v1batchnum: TcxGridDBColumn;
    v1DepotClass: TcxGridDBColumn;
    v1defstr6: TcxGridDBColumn;
    v1defstr7: 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 v1P_ChnNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1P_ChnName10PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1ShortNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1UnitNamePropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1DefStr4PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1PlanNoPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure BtnSaveClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure v1DepotClassPropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column2PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1defstr7PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    procedure InitGrid();
    function  SaveData():Boolean;
    { Private declarations }
  public
    DepotNo:string;
    { Public declarations }
  end;

var
  frmBaseIn: TfrmBaseIn;

implementation
uses
  U_DataLink,U_Fun;

{$R *.dfm}

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

procedure TfrmBaseIn.BtnExitClick(Sender: TObject);
begin
  Close;
end;
procedure TfrmBaseIn.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Get_ClothInOutList :begdate,:enddate,:DepotNo,:DepotCode,:PState,:CRK');
      Parameters.ParamByName('DepotNo').Value:=Trim(DepotNo);
      Parameters.ParamByName('PState').Value:=2;
      Open;
    end;
    CreateCDS20(ADOQueryMain,CDS_Sub);
    InitCDSData20(ADOQueryMain,CDS_Sub);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmBaseIn.ToolButton2Click(Sender: TObject);
begin
  with CDS_Sub do
  begin
    Append;
    CDS_Sub.FieldByName('UnitName').Value:='米';
    CDS_Sub.FieldByName('RollUnit').Value:='JL004';
    CDS_Sub.FieldByName('DefStr4').Value:='一等';
    Post;
  end;
end;

procedure TfrmBaseIn.ToolButton3Click(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then Exit;
  CDS_Sub.Delete;
end;

procedure TfrmBaseIn.ToolButton4Click(Sender: TObject);
begin
  CopyAddRow(tv1,CDS_Sub);
  ///CDS_Sub.FieldByName('DepotNo').Value:='';
end;

procedure TfrmBaseIn.ToolButton1Click(Sender: TObject);
begin
  OneKeyPost(Tv1,CDS_Sub);
end;

function TfrmBaseIn.SaveData():Boolean;
var
  MaxNo,strSQL:string;
  KID,i,SubId,j:Integer;
begin
  try
    Result:=False;
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Sub.First;
    for i:=0 to CDS_Sub.RecordCount-1 do
    begin
      if Trim(CDS_Sub.fieldbyname('DepotNo').AsString)<>'' then
      begin
        MaxNo:=Trim(CDS_Sub.fieldbyname('DepotNo').AsString);
        KID:=CDS_Sub.fieldbyname('KcID').AsInteger;
      end else
      begin
        if GetMaxNo20(ADOQueryCmd,MaxNo,'WC_ClothInout','BR',1,4)=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取最大号失败！','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('UPDATE WC_ClothKcid SET KcID = KcID+1');
          SQL.Add('SELECT KcID FROM WC_ClothKcid');
          Open;
          KID:=fieldbyname('KcID').AsInteger;
        end;
      end;

      //保存主表
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add('select * from WC_ClothInout where DepotNO='''+Trim(DepotNo)+'''');
        Open;
      end;
      with ADOQueryCmd do
      begin
        if Trim(CDS_Sub.fieldbyname('DepotNo').AsString)<>'' then
          Edit
        else
          Append;
        FieldByName('DepotNo').Value:=Trim(MaxNo);
        FieldByName('DepotCode').Value:=Trim(gDef1);
        FieldByName('DepotType').Value:=1;
        FieldByName('Filler').Value:=Trim(gUserName);
        FieldByName('FillTime').Value:=Trim(CDS_Sub.fieldbyname('FillTime').AsString);
        FieldByName('DepotClass').Value:=Trim(CDS_Sub.fieldbyname('DepotClass').AsString);
        FieldByName('Status').Value:='I9';
        FieldByName('DepotNo').Value:=Trim(MaxNo);
        //SSetSaveDataCDS(ADOQueryCmd,Tv1,CDS_Sub,'WC_ClothInout',1);
        Post;
      end;
      //保存从表
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add('select * from WC_ClothInOut_Info where DepotNO='''+Trim(DepotNo)+'''');
        Open;
      end;
      with ADOQueryCmd do
      begin
        if Trim(CDS_Sub.fieldbyname('DepotNo').AsString)<>'' then
          Edit
        else
          Append;
        FieldByName('DepotNo').Value:=Trim(MaxNo);
        FieldByName('KCID').Value:=KID;
        FieldByName('ID').Value:=1;
        {if Trim(gdatabase)<>'期初' then
        begin
          if Trim(CDS_Sub.fieldbyname('DepotNo').AsString)='' then
          begin
            FieldByName('DefFlt2').Value:=SubId;
            FieldByName('DefFlt3').Value:=((SubId-1) div 10);
          end;

        end; }

        SSetSaveDataCDS(ADOQueryCmd,Tv1,CDS_Sub,'WC_ClothInOut_Info',2);
        Post;
      end;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        SQL.Add('select DepotNo,DefFlt2,DefFlt3 from WC_ClothInout_Info where DepotNo like ''BR%''  ');
        if Trim(CDS_Sub.fieldbyname('PlanNo').AsString)<>'' then
          SQL.Add(' and PlanNo='''+CDS_Sub.fieldbyname('PlanNo').AsString+'''');
        if Trim(CDS_Sub.fieldbyname('P_Code').AsString)<>'' then
          SQL.Add(' and P_Code='''+CDS_Sub.fieldbyname('P_Code').AsString+'''');
        if Trim(CDS_Sub.fieldbyname('DefStr1').AsString)<>'' then
          SQL.Add(' and  DefStr1='''+CDS_Sub.fieldbyname('DefStr1').AsString+'''');
        if Trim(CDS_Sub.fieldbyname('DefStr2').AsString)<>'' then
          SQL.Add(' and DefStr2='''+CDS_Sub.fieldbyname('DefStr2').AsString+'''');
        if Trim(CDS_Sub.fieldbyname('DefStr3').AsString)<>'' then
          SQL.Add(' and DefStr3='''+CDS_Sub.fieldbyname('DefStr3').AsString+'''');
        if Trim(CDS_Sub.fieldbyname('DefStr4').AsString)<>'' then
          SQL.Add(' and DefStr4='''+CDS_Sub.fieldbyname('DefStr4').AsString+'''');
        if Trim(CDS_Sub.fieldbyname('Defflt1').AsString)<>'' then
          SQL.Add(' and Defflt1='''+CDS_Sub.fieldbyname('Defflt1').AsString+'''');
        Open;
       { if ADOQueryTemp.fieldbyname('aa').AsInteger>0 then
          SubId:=ADOQueryTemp.fieldbyname('aa').AsInteger
        else}
          //SubId:=ADOQueryTemp.fieldbyname('aa').AsInteger+1;
      end;
      with ADOQueryTemp do
      begin
        First;
        j:=1;
        while not eof do
        begin
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update WC_ClothInout_Info Set defflt2='+inttostr(j));
            sql.Add(',defflt3='+inttostr((j-1) div 10) );
            sql.Add(' where DepotNo='''+Trim(ADOQueryTemp.fieldbyname('DepotNo').AsString)+'''');
            ExecSQL;
          end;
          j:=j+1;
          Next;
        end;
      end;
      //保存到库存表
      if Trim(CDS_Sub.fieldbyname('DepotNo').AsString)='' then
      begin
        strSQL := 'INSERT WC_ClothClothing ('
               + 'KcID,'
               + 'batchnum,'
               + 'P_Code,'
               + 'P_ChnName,'
               + 'Quantity,'
               + 'RollUnit,'
               + 'DepotNo'
               + ') VALUES ('
               + IntToStr(KID) + ','
               + QuotedStr(Trim(CDS_Sub.fieldbyname('batchnum').AsString)) + ','
               + QuotedStr(Trim(CDS_Sub.fieldbyname('P_Code').AsString)) + ','
               + QuotedStr(Trim(CDS_Sub.fieldbyname('P_ChnName').AsString)) + ','
               + CDS_Sub.fieldbyname('Quantity').AsString + ','
               + QuotedStr(Trim(CDS_Sub.fieldbyname('RollUnit').AsString)) + ','
               + QuotedStr(MaxNo)
               + ')';
      end else
      begin
        //更新到库存表
        strSQL := 'UPDATE WC_ClothClothing SET '
                + 'batchnum = ' + QuotedStr(Trim(CDS_Sub.fieldbyname('batchnum').AsString)) + ','
                + 'P_Code = ' + QuotedStr(Trim(CDS_Sub.fieldbyname('P_Code').AsString)) + ','
                + 'P_ChnName = ' + QuotedStr(Trim(CDS_Sub.fieldbyname('P_ChnName').AsString)) + ','
                + 'Quantity = ' + CDS_Sub.fieldbyname('Quantity').AsString+ ','
                + 'RollUnit = ' + QuotedStr(Trim(CDS_Sub.fieldbyname('RollUnit').AsString)) + ' '
                + 'WHERE KcID = ' + IntToStr(KID);
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add(strSQL);
        ExecSQL;
      end;
      //更新卷号
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update WC_ClothInout_Info set batchnum='''+Trim(CDS_Sub.fieldbyname('batchnum').AsString)+'''');
        sql.Add(' where KCID='+IntToStr(KID));
        ExecSQL;
      end;
      CDS_Sub.Next;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
    DepotNo:=MaxNo;
    ModalResult:=1;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!','提示',0);
  end;
end;

procedure TfrmBaseIn.v1P_ChnNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmProductHelp:=TfrmProductHelp.Create(Application);
    with frmProductHelp do
    begin
      fDipFlag:=3;
      cxTabControl1.Tabs[0].Visible:=False;
      cxTabControl1.Tabs[1].Visible:=False;
      cxTabControl1.Tabs[2].Visible:=False;
      if ShowModal=1 then
      begin
        Self.CDS_Sub.Edit;
        CDS_Sub.FieldByName('P_Code').Value:=ADOQueryHelp.fieldbyname('P_Code').AsString;
        CDS_Sub.FieldByName('P_ChnName').Value:=ADOQueryHelp.fieldbyname('P_ChnName').AsString;
      end;
    end;
  finally
    frmProductHelp.Free;
  end;
end;

procedure TfrmBaseIn.v1P_ChnName10PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmProductHelp:=TfrmProductHelp.Create(Application);
    with frmProductHelp do
    begin
      fDipFlag:=2;
      cxTabControl1.Tabs[0].Visible:=False;
      cxTabControl1.Tabs[1].Visible:=False;
      cxTabControl1.Tabs[3].Visible:=False;
      if ShowModal=1 then
      begin
        Self.CDS_Sub.Edit;
        CDS_Sub.FieldByName('DefStr1').Value:=ADOQueryHelp.fieldbyname('P_Code').AsString;
        CDS_Sub.FieldByName('P_ChnName10').Value:=ADOQueryHelp.fieldbyname('P_ChnName').AsString;
      end;
    end;
  finally
    frmProductHelp.Free;
  end;
end;

procedure TfrmBaseIn.v1ShortNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmSupplyHelp:=TfrmSupplyHelp.Create(Application);
    with frmSupplyHelp do
    begin
      FLType:='坯布';
      if ShowModal=1 then
      begin
        Self.CDS_Sub.Edit;
        self.CDS_Sub.FieldByName('ShortName').Value:=Trim(ADOQueryHelp.fieldbyname('ShortName').AsString);
        self.CDS_Sub.FieldByName('DefStr2').Value:=Trim(ADOQueryHelp.fieldbyname('CustomNo').AsString);
      end;
    end;
  finally
    frmSupplyHelp.Free;
  end;
end;

procedure TfrmBaseIn.v1UnitNamePropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    kf_frmJlUnitHelp:=Tkf_frmJlUnitHelp.Create(Application);
    with kf_frmJlUnitHelp do
    begin
      Fflag:='length';
      if ShowModal=1 then
      begin
        Self.CDS_Sub.Edit;
        Self.CDS_Sub.FieldByName('UnitName').Value:=Trim(ADOQueryHelp.fieldbyname('名称').AsString);
        Self.CDS_Sub.FieldByName('RollUnit').Value:=Trim(ADOQueryHelp.fieldbyname('编号').AsString);
      end;
    end;
  finally
    kf_frmJlUnitHelp.Free;
  end;
end;

procedure TfrmBaseIn.v1DefStr4PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmItemManageNew:=TfrmItemManageNew.Create(Application);
    with frmItemManageNew do
    begin
      flag:='DefStr4';
      flagname:='贝斯等级';
      if ShowModal=1 then
      begin
        Self.CDS_Sub.Edit;
        Self.CDS_Sub.FieldByName('DefStr4').Value:=Trim(ClientDataSet1.fieldbyname('name').AsString);
      end;
    end;
  finally
    frmItemManageNew.Free;
  end;
end;

procedure TfrmBaseIn.v1PlanNoPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmGetPlanListHelp:=TfrmGetPlanListHelp.Create(Application);
    with frmGetPlanListHelp do
    begin
      if ShowModal=1 then
      begin
        CDS_Sub.Edit;
        CDS_Sub.FieldByName('PlanNo').Value:=Trim(ADOQueryHelp.fieldbyname('PlanNo').AsString);
        CDS_Sub.FieldByName('P_SeqNo').Value:=Trim(ADOQueryHelp.fieldbyname('P_SeqNo').AsString);
        CDS_Sub.FieldByName('DefStr1').Value:=Trim(ADOQueryHelp.fieldbyname('P_BaseCode').AsString);
        CDS_Sub.FieldByName('P_ChnName10').Value:=Trim(ADOQueryHelp.fieldbyname('P_BaseChnName').AsString);
        CDS_Sub.FieldByName('DefStr2').Value:=Trim(ADOQueryHelp.fieldbyname('P_Factory').AsString);
        CDS_Sub.FieldByName('Shortname').Value:=Trim(ADOQueryHelp.fieldbyname('SupplierName').AsString);
        CDS_Sub.FieldByName('DefStr3').Value:=Trim(ADOQueryHelp.fieldbyname('SBtr2').AsString);
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('Select P_Code from MC_CatLog where P_ChnName='''+Trim(ADOQueryHelp.fieldbyname('LbStr6').AsString)+'''');
          Open;
          if not IsEmpty then
          begin
            CDS_Sub.FieldByName('P_Code').Value:=Trim(ADOQueryTemp.fieldbyname('P_Code').AsString);
            CDS_Sub.FieldByName('P_ChnName').Value:=Trim(ADOQueryHelp.fieldbyname('LbStr6').AsString);
          end;
        end;
      end;
    end;
  finally
    frmGetPlanListHelp.Free;
  end;
end;

procedure TfrmBaseIn.BtnSaveClick(Sender: TObject);
begin
  if CDS_Sub.Locate('FillTime',Null,[]) then
  begin
    Application.MessageBox('入库时间不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('PlanNo',Null,[]) then
  begin
    Application.MessageBox('批号不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('P_ChnName',Null,[]) then
  begin
    Application.MessageBox('品名不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('DepotClass',Null,[]) then
  begin
    Application.MessageBox('入库类型不能为空!','提示',0); 
    Exit;
  end;
  if CDS_Sub.Locate('Quantity',Null,[]) then
  begin
    Application.MessageBox('数量不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.IsEmpty then Exit;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
  end;
end;

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

procedure TfrmBaseIn.v1DepotClassPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmItemManageNew:=TfrmItemManageNew.Create(Application);
    with frmItemManageNew do
    begin
      flag:='BSInType';
      flagname:='贝斯入库类型';
      if ShowModal=1 then
      begin
        Self.CDS_Sub.Edit;
        Self.CDS_Sub.FieldByName('DepotClass').Value:=Trim(ClientDataSet1.fieldbyname('name').AsString);
      end;
    end;
  finally
    frmItemManageNew.Free;
  end;
end;

procedure TfrmBaseIn.v1Column2PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmItemManageNew:=TfrmItemManageNew.Create(Application);
    with frmItemManageNew do
    begin
      flag:='BSBC';
      flagname:='贝斯班次';
      if ShowModal=1 then
      begin
        Self.CDS_Sub.Edit;
        Self.CDS_Sub.FieldByName('defstr6').Value:=Trim(ClientDataSet1.fieldbyname('name').AsString);
      end;
    end;
  finally
    frmItemManageNew.Free;
  end;
end;

procedure TfrmBaseIn.v1defstr7PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmItemManageNew:=TfrmItemManageNew.Create(Application);
    with frmItemManageNew do
    begin
      flag:='BSBZ';
      flagname:='贝斯班组';
      if ShowModal=1 then
      begin
        Self.CDS_Sub.Edit;
        Self.CDS_Sub.FieldByName('defstr7').Value:=Trim(ClientDataSet1.fieldbyname('name').AsString);
      end;
    end;
  finally
    frmItemManageNew.Free;
  end;
end;

end.
