unit U_YarnPurOutReturn;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin, StdCtrls,
  cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP, ShellAPI, IniFiles, cxCheckBox, cxCalendar, cxButtonEdit,
  cxTextEdit, cxDropDownEdit, BtnEdit, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, U_BaseInput, cxContainer, dxSkinsCore, dxSkinsDefaultPainters,
  dxSkinWXI, dxCore, cxDateUtils, cxCurrencyEdit, cxMemo, System.ImageList,
  Vcl.ImgList;

type
  TfrmYarnPurOutReturn = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ToolButton3: TToolButton;
    ScrollBox1: TScrollBox;
    Label2: TLabel;
    Y_Code: TcxTextEdit;
    Label8: TLabel;
    StkCoName: TcxButtonEdit;
    lbl5: TLabel;
    IOTime: TcxDateEdit;
    Label10: TLabel;
    Label11: TLabel;
    Qty: TcxCurrencyEdit;
    Piece: TcxCurrencyEdit;
    Label6: TLabel;
    Y_Name: TcxTextEdit;
    Label9: TLabel;
    FromCoName: TcxTextEdit;
    Label4: TLabel;
    Y_Color: TcxTextEdit;
    Label5: TLabel;
    Y_ColorNo: TcxTextEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure BuyNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure CoBusinessTypeDblClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    function SaveData(): Boolean;
  public
    { Public declarations }
    FBYIOID: string;
  end;

var
  frmYarnPurOutReturn: TfrmYarnPurOutReturn;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_CompanySel;

{$R *.dfm}

procedure TfrmYarnPurOutReturn.InitGrid();
begin

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from BS_Yarn_IO A    ');
    sql.Add(' where A.BYIOID=''' + Trim(FBYIOID) + '''');
    Open;
  end;
  FromCoName.Text := ADOQueryTemp.FieldByName('ToCoName').ASString;
  Y_Code.Text := ADOQueryTemp.FieldByName('Y_Code').ASString;
  Y_Name.Text := ADOQueryTemp.FieldByName('Y_Name').ASString;
  Y_ColorNo.Text := ADOQueryTemp.FieldByName('Y_ColorNo').ASString;
  Y_Color.Text := ADOQueryTemp.FieldByName('Y_Color').ASString;
end;

procedure TfrmYarnPurOutReturn.FormDestroy(Sender: TObject);
begin
  inherited;
  frmYarnPurOutReturn := nil;
end;

procedure TfrmYarnPurOutReturn.BuyNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        self.StkCoName.Properties.LookupItems.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.StkCoName.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;

  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmYarnPurOutReturn.CoBusinessTypeDblClick(Sender: TObject);
begin
  TcxButtonEdit(Sender).Text := '';
  TcxButtonEdit(Sender).Properties.LookupItems.Text := '';
end;

procedure TfrmYarnPurOutReturn.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := caFree;
end;

procedure TfrmYarnPurOutReturn.FormCreate(Sender: TObject);
begin
  inherited;
  IOTime.Date := SGetServerDate(ADOQueryTemp);
end;

procedure TfrmYarnPurOutReturn.TBCloseClick(Sender: TObject);
begin

  close;
end;

procedure TfrmYarnPurOutReturn.FormShow(Sender: TObject);
var
  i: integer;
  a: string;
begin
  inherited;
  InitGrid();
end;

procedure TfrmYarnPurOutReturn.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

function TfrmYarnPurOutReturn.SaveData(): Boolean;
var
  MBCIOID, Maxno: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;

    if not GetLSNo(ADOQueryCmd, Maxno, 'Z', 'BS_Cloth_IO', 4, 1) then
      raise Exception.Create('取染色坯布出库编号失败！');

    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      sql.Add('insert into Finance_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)       ');
      sql.Add('values(''纱线入库''    ');
      sql.Add(',' + quotedstr(Trim(Maxno)));
      sql.Add(',''入库''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(Trim(Maxno)));
      sql.Add(')  ');

      Sql.Add('exec P_BS_Yarn_OutReturn ');
      Sql.Add(' @BYIOID=' + quotedstr(Trim(FBYIOID)));
      Sql.Add(',@Maxno=' + quotedstr(Trim(Maxno)));
      Sql.Add(',@IOTime=' + quotedstr(Trim(Trim(FormatDateTime('yyyy-MM-dd', IOTime.Date)))));
      Sql.Add(',@StkCoNo=' + quotedstr(Trim(StkCoName.Properties.LookupItems.Text)));
      Sql.Add(',@StkCoName=' + quotedstr(Trim(StkCoName.Text)));
      Sql.Add(',@Y_Color=' + quotedstr(Trim(Y_Color.Text)));
      Sql.Add(',@Y_ColorNo=' + quotedstr(Trim(Y_ColorNo.Text)));
      Sql.Add(',@Piece=' + Piece.Text);
      Sql.Add(',@Qty=' + Qty.Text);
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));

    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure TfrmYarnPurOutReturn.ToolButton3Click(Sender: TObject);
begin

  if Trim(StkCoName.Text) = '' then
  begin
    Application.MessageBox('存放单位不能为空!', '提示', 0);
    Exit;
  end;
  if Trim(Piece.Text) = '' then
  begin
    Application.MessageBox('件数不能为空!', '提示', 0);
    Exit;
  end;
  if Trim(Qty.Text) = '' then
  begin
    Application.MessageBox('数量不能为空!', '提示', 0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    ModalResult := 1;
  end;
end;

end.

