unit U_BaseOut;

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
  TfrmBaseOut = 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;
    v1Column1: TcxGridDBColumn;
    v1DefStr2: TcxGridDBColumn;
    v1DefStr1: TcxGridDBColumn;
    v1P_Code: TcxGridDBColumn;
    v1RollUnit: TcxGridDBColumn;
    v1P_SeqNo: TcxGridDBColumn;
    v1DepotClass: TcxGridDBColumn;
    v1batchnum: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    procedure FormCreate(Sender: TObject);
    procedure BtnExitClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(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);
  private
    procedure InitGrid();
    function  SaveData():Boolean;
    { Private declarations }
  public
    DepotNo:string;
    QuantityCK:string;
    { Public declarations }
  end;

var
  frmBaseOut: TfrmBaseOut;

implementation
uses
  U_adodbmd,U_global,U_FormPas,U_SelfForm,U_ProductHelp,
  U_SupplyHelp,U_JlUnitHelp,U_ItemManageNew,U_GetPlanListHelp
  ,U_GetBaseInList;

{$R *.dfm}

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

procedure TfrmBaseOut.BtnExitClick(Sender: TObject);
begin
  Close;
end;
procedure TfrmBaseOut.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 TfrmBaseOut.ToolButton2Click(Sender: TObject);
begin
  try
    frmGetBaseInList:=TfrmGetBaseInList.Create(Application);
    with frmGetBaseInList do
    begin
      if ShowModal=1 then
      begin
        with CDS_Sub do
        begin
          Append;
          CDS_Sub.FieldByName('PlanNo').Value:=Trim(ADOQueryMain.fieldbyname('PlanNo').AsString);
          CDS_Sub.FieldByName('P_SeqNO').Value:=Trim(ADOQueryMain.fieldbyname('P_SeqNO').AsString);
          CDS_Sub.FieldByName('P_Code').Value:=Trim(ADOQueryMain.fieldbyname('P_Code').AsString);
          CDS_Sub.FieldByName('P_ChnName').Value:=Trim(ADOQueryMain.fieldbyname('P_ChnName').AsString);
          CDS_Sub.FieldByName('DefStr1').Value:=Trim(ADOQueryMain.fieldbyname('DefStr1').AsString);
          CDS_Sub.FieldByName('P_ChnName10').Value:=Trim(ADOQueryMain.fieldbyname('P_ChnName10').AsString);
          CDS_Sub.FieldByName('DefStr2').Value:=Trim(ADOQueryMain.fieldbyname('DefStr2').AsString);
          CDS_Sub.FieldByName('ShortName').Value:=Trim(ADOQueryMain.fieldbyname('ShortName').AsString);
          CDS_Sub.FieldByName('DefStr3').Value:=Trim(ADOQueryMain.fieldbyname('DefStr3').AsString);
          CDS_Sub.FieldByName('DefFlt1').Value:=Trim(ADOQueryMain.fieldbyname('DefFlt1').AsString);
          CDS_Sub.FieldByName('DefStr6').Value:=Trim(ADOQueryMain.fieldbyname('DefStr6').AsString);
          CDS_Sub.FieldByName('DefStr7').Value:=Trim(ADOQueryMain.fieldbyname('DefStr7').AsString);
          CDS_Sub.FieldByName('Quantity').Value:=Trim(ADOQueryMain.fieldbyname('QuantityKC').AsString);
          CDS_Sub.FieldByName('UnitName').Value:=Trim(ADOQueryMain.fieldbyname('UnitName').AsString);
          CDS_Sub.FieldByName('RollUnit').Value:=Trim(ADOQueryMain.fieldbyname('RollUnit').AsString);
          CDS_Sub.FieldByName('DefStr4').Value:=Trim(ADOQueryMain.fieldbyname('DefStr4').AsString);
          CDS_Sub.FieldByName('KCID').Value:=Trim(ADOQueryMain.fieldbyname('KCID').AsString);
          CDS_Sub.FieldByName('batchnum').Value:=Trim(ADOQueryMain.fieldbyname('batchnum').AsString);
          Post;
        end;
      end;
    end;
  finally
    frmGetBaseInList.Free;
  end;
end;

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

function TfrmBaseOut.SaveData():Boolean;
var
  MaxNo,strSQL:string;
  KID,i: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);
      end else
      begin
        if GetMaxNo20(ADOQueryCmd,MaxNo,'WC_ClothInout','BC',1,4)=False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取最大号失败！','提示',0);
          Exit;
        end;
      end;
      KID:=CDS_Sub.fieldbyname('KcID').AsInteger;
      //保存主表
      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:=0;
        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;
        SSetSaveDataCDS(ADOQueryCmd,Tv1,CDS_Sub,'WC_ClothInOut_Info',2);
        Post;
      end;
      //保存到库存表
      if Trim(CDS_Sub.fieldbyname('DepotNo').AsString)='' then
      begin
        //更新到库存表
        strSQL := 'UPDATE WC_ClothClothing SET '
                + 'Quantity =Quantity-' + CDS_Sub.fieldbyname('Quantity').AsString+ ' '
                + 'WHERE KcID = ' + IntToStr(KID);
      end else
      begin
        //更新到库存表
        strSQL := 'UPDATE WC_ClothClothing SET '
                + 'Quantity =Quantity+'+QuantityCK+'-'+CDS_Sub.fieldbyname('Quantity').AsString+ ' '
                + 'WHERE KcID = ' + IntToStr(KID);
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add(strSQL);
        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 TfrmBaseOut.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 TfrmBaseOut.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 TfrmBaseOut.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 TfrmBaseOut.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 TfrmBaseOut.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 TfrmBaseOut.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 TfrmBaseOut.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('batchnum',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('P_ChnName',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 TfrmBaseOut.FormShow(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmBaseOut.v1DepotClassPropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmItemManageNew:=TfrmItemManageNew.Create(Application);
    with frmItemManageNew do
    begin
      flag:='BSOutType';
      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;

end.
