unit U_PurClothEnterInput;

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, dxDateRanges, dxBarBuiltInMenu, U_BaseInput, System.ImageList,
  Vcl.ImgList, cxContainer, cxMemo, dxScrollbarAnnotations, dxCore, cxDateUtils,
  cxCurrencyEdit, dxSkinsCore, dxSkinsDefaultPainters, cxImageList;

type
  TfrmPurClothEnterInput = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ToolButton3: TToolButton;
    ScrollBox1: TScrollBox;
    Label2: TLabel;
    Label7: TLabel;
    Label1: TLabel;
    BatchNo: TcxTextEdit;
    C_Code: TcxTextEdit;
    Label8: TLabel;
    Label5: TLabel;
    StkPosition: TcxButtonEdit;
    FromCoName: TcxButtonEdit;
    ToolButton1: TToolButton;
    Note: TcxMemo;
    lbl5: TLabel;
    IOTime: TcxDateEdit;
    Label10: TLabel;
    Label11: TLabel;
    Qty: TcxCurrencyEdit;
    Piece: TcxCurrencyEdit;
    Label4: TLabel;
    StkCoName: TcxButtonEdit;
    Label6: TLabel;
    C_Name: TcxTextEdit;
    QtyUnit: TcxTextEdit;
    Label3: TLabel;
    C_Color: TcxTextEdit;
    Label9: TLabel;
    FromPurNo: TcxTextEdit;
    FromPurSId: TcxTextEdit;
    FromPurMId: TcxTextEdit;
    Label12: TLabel;
    C_Composition: TcxTextEdit;
    Label13: TLabel;
    C_Spec: TcxTextEdit;
    C_Degree: TcxTextEdit;
    C_Width: TcxTextEdit;
    C_GramWeight: TcxTextEdit;
    Price: TcxTextEdit;
    GC_Name: TcxTextEdit;
    CraftCode: TcxTextEdit;
    CraftName: TcxTextEdit;
    FromConNo: TcxTextEdit;
    Label14: TLabel;
    FtyPCId: TcxTextEdit;
    Grade: TComboBox;
    Label15: TLabel;
    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 StkCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    function SaveData(MSaveType: string): Boolean;
  public
    { Public declarations }
    FBCIOID, FPurMId, FPurSId, FQtyUnit: string;
  end;

var
  frmPurClothEnterInput: TfrmPurClothEnterInput;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_CompanySel;

{$R *.dfm}

procedure TfrmPurClothEnterInput.InitGrid();
begin
  if trim(FBCIOID) <> '' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add(' select A.* ');
      sql.Add(' from BS_Cloth_IO A');
      sql.Add(' where BCIOID=''' + Trim(FBCIOID) + '''');
      Open;
    end;
    FromCoName.Properties.LookupItems.Text := ADOQueryTemp.FieldByName('FromCoNo').ASString;
    StkCoName.Properties.LookupItems.Text := ADOQueryTemp.FieldByName('StkCoNo').ASString;
    SCSHData(ADOQueryTemp, ScrollBox1, 0);
  end
  else
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add(' select A.*,B.* ');
      sql.Add('  from Pur_ClothPlan_Main A inner join Pur_ClothPlan_Sub B on A.PurMId=B.PurMId   ');
      sql.Add(' where PurSId=''' + Trim(FPurSId) + '''');
      Open;
    end;
    FromCoName.Properties.LookupItems.Text := trim(ADOQueryTemp.FieldByName('SellNo').ASString);
//    ShowMessage(ADOQueryTemp.FieldByName('SellNo').ASString + ';');
//    ShowMessage(FromCoName.Properties.LookupItems.Text + ';');

    FromCoName.Text := ADOQueryTemp.FieldByName('SellName').ASString;
    FromPurNo.Text := ADOQueryTemp.FieldByName('PurNo').ASString;
    FromPurMId.Text := ADOQueryTemp.FieldByName('PurMId').ASString;
    FromPurSId.Text := ADOQueryTemp.FieldByName('PurSId').ASString;

    FromConNo.Text := ADOQueryTemp.FieldByName('ConNo').ASString;

    C_Spec.Text := ADOQueryTemp.FieldByName('C_Spec').ASString;
    QtyUnit.Text := ADOQueryTemp.FieldByName('QtyUnit').ASString;
    C_Composition.Text := ADOQueryTemp.FieldByName('C_Composition').ASString;
    C_Code.Text := ADOQueryTemp.FieldByName('C_Code').ASString;
    C_Name.Text := ADOQueryTemp.FieldByName('C_Name').ASString;
    GC_Name.Text := ADOQueryTemp.FieldByName('GC_Name').ASString;
    CraftCode.Text := ADOQueryTemp.FieldByName('CraftCode').ASString;
    CraftName.Text := ADOQueryTemp.FieldByName('CraftName').ASString;
    C_Color.Text := ADOQueryTemp.FieldByName('C_Color').ASString;
    C_Degree.Text := ADOQueryTemp.FieldByName('C_Degree').ASString;
    C_GramWeight.Text := ADOQueryTemp.FieldByName('C_GramWeight').ASString;
    C_Width.Text := ADOQueryTemp.FieldByName('C_Width').ASString;
    Price.Text := ADOQueryTemp.FieldByName('Price').ASString;
  end;

end;

procedure TfrmPurClothEnterInput.FormDestroy(Sender: TObject);
begin
  inherited;
  frmPurClothEnterInput := nil;
end;

procedure TfrmPurClothEnterInput.BuyNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  fsj: string;
  FWZ: Integer;
begin
  fsj := Trim(TcxButtonEdit(Sender).Hint);
  FWZ := Pos('/', fsj);
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      MainType := 'Cust';
      flag := Copy(fsj, 1, FWZ - 1);
      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
      if ShowModal = 1 then
      begin
        TcxButtonEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

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

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

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

procedure TfrmPurClothEnterInput.TBCloseClick(Sender: TObject);
begin

  close;
end;

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

end;

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

function TfrmPurClothEnterInput.SaveData(MSaveType: string): Boolean;
var
  Maxno, MBCIOID, MBCIONo: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from BS_Cloth_IO where BCIOID=''' + Trim(FBCIOID) + '''');
      open;
    end;
    MBCIOID := Trim(ADOQueryTemp.fieldbyname('BCIOID').AsString);

    if Trim(MBCIOID) = '' then
    begin
      if not GetLSNo(ADOQueryCmd, Maxno, 'MR', 'BS_Cloth_IO', 4, 1) then
        raise Exception.Create('取染色坯布入库编号失败！');

      if not GetLSNo(ADOQueryCmd, MBCIONo, 'RZ', 'BS_Cloth_IO', 4, 1) then
        raise Exception.Create('取染色坯布入库编号失败！');
    end
    else
    begin
      Maxno := Trim(MBCIOID);
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from BS_Cloth_IO where BCIOID=''' + Trim(Maxno) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(MBCIOID) = '' then
      begin
        Append;
        FieldByName('Fillid').Value := Trim(Dcode);
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('BCIONo').Value := Trim(MBCIONo);
        FieldByName('BCIOID').Value := Trim(Maxno);
        FieldByName('STKID').Value := Trim(Maxno);
        FieldByName('CIID').Value := Trim(Maxno);
      end
      else
      begin
        Edit;
        FieldByName('Editid').Value := Trim(Dcode);
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditTime').Value := SGetServerDate(ADOQueryTemp);
      end;

      RTSetsavedata(ADOQueryCmd, 'BS_Cloth_IO', ScrollBox1, 0);
      FieldByName('STKName').Value := '贸易布匹';
      FieldByName('IOFlag').Value := '入库';
      FieldByName('IOType').Value := '采购入库';
      FieldByName('IOQtyFlag').Value := 1;
      FieldByName('Piece').Value := 1;
      FieldByName('FromPurMId').Value := FPurMId;
      FieldByName('FromPurSId').Value := FPurSId;
      FieldByName('QtyUnit').Value := FQtyUnit;
      Post;
    end;
    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('exec P_BS_Cloth_UpdateByPurchase ');
      Sql.Add(' @PurSId=' + quotedstr(Trim(FPurSId)));
      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;

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

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

procedure TfrmPurClothEnterInput.ToolButton3Click(Sender: TObject);
begin

  if Trim(BatchNo.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 Trim(StkCoName.Text) = '' then
  begin
    Application.MessageBox('存放单位不能为空!', '提示', 0);
    Exit;
  end;
  if SaveData('采购入库') then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    ModalResult := 1;
  end;
end;

end.

