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,
      
     
     
     
     
    
    
    
    
    
     
     
     
    
    
    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;
    ToolButton2: TToolButton;
    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);
    procedure ToolButton2Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    function SaveData(MSaveType: string): Boolean;
  public
    { Public declarations }
    FBCIOID, FPurSId: 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: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FBCIOID) = '' then
    begin
      if not GetLSNo(ADOQueryCmd, Maxno, 'MR', 'BS_Cloth_IO', 4, 1) then
        raise Exception.Create('取入库编号失败！');
    end
    else
    begin
      Maxno := Trim(FBCIOID);
    end;

    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from BS_Cloth_IO where BCIOID=''' + Trim(FBCIOID) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FBCIOID) = '' then
      begin
        Append;
        FieldByName('FillId').Value := Trim(DCode);
        FieldByName('Filler').Value := Trim(DName);

      end
      else
      begin
        Edit;
        FieldByName('EditId').Value := Trim(DCode);
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('Edittime').Value := SGetServerDateTime(ADOQueryTemp);
      end;
      RTSetsavedata(ADOQueryCmd, 'BS_Cloth_IO', ScrollBox1, 0);

      FieldByName('FromCoNo').Value := Trim(FromCoName.Properties.LookupItems.Text);
      FieldByName('StkCoNo').Value := Trim(StkCoName.Properties.LookupItems.Text);
      FieldByName('BCIOID').Value := Trim(Maxno);
      FieldByName('STKID').Value := Trim(Maxno);
      FieldByName('STKName').Value := '贸易布匹';
      FieldByName('IOFlag').Value := '入库';
      FieldByName('IOType').Value := '采购入库';
      FieldByName('CostPrice').Value := StrToFloatDef(Price.Text, 0);
      FieldByName('IOQtyFlag').Value := 1;
      Post;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update BS_Cloth_IO Set StkPiece=(select Sum(isnull(Piece,0)*IOQtyFlag) from BS_Cloth_IO A where A.STKID=BS_Cloth_IO.BCIOID )');
      sql.Add(',StkQty=(select Sum(isnull(Qty,0)*IOQtyFlag) from BS_Cloth_IO A where A.STKID=BS_Cloth_IO.BCIOID )');
      sql.Add(' where BCIOID=''' + Trim(Maxno) + '''');
      ExecSQL;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select stkQty,StkPiece from BS_Cloth_IO where BCIOID=''' + Trim(Maxno) + '''');
      Open;
    end;
    if ADOQueryTemp.FieldByName('stkQty').Value < 0 then
      raise Exception.Create('入库数量小于出库数量不能保存!');
    if ADOQueryTemp.FieldByName('StkPiece').Value < 0 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(Maxno));
      sql.Add(',''保存''   ');
      sql.Add(',' + quotedstr(DName));
      sql.Add(',' + quotedstr(trim(self.Caption)));
      sql.Add(',' + quotedstr(Maxno));
      sql.Add(')  ');
      ExecSQL;
    end;
    if (MSaveType = '检验出库') or (MSaveType = '直接发货') then
    begin
      with ADOQueryCmd do
      begin
        Close;
        Sql.Clear;
        Sql.Add('exec P_BS_Cloth_OutBYIn ');
        Sql.Add(' @BCIOID=' + quotedstr(Trim(Maxno)));
        Sql.Add(' ,@OutType=' + quotedstr(MSaveType));
        Open;
      end;
      if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
        raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    end;
    ADOQueryCmd.Connection.CommitTrans;
    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.ToolButton2Click(Sender: TObject);
begin


//  if Trim(CoAbbrName.Text) = '' then
//  begin
//    Application.MessageBox('简称不能为空!', '提示', 0);
//    Exit;
//  end;
//  if Trim(CoName.Text) = '' then
//  begin
//    Application.MessageBox('名称不能为空!', '提示', 0);
//    Exit;
//  end;
  if SaveData('检验出库') then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    ModalResult := 1;
  end;
end;

procedure TfrmPurClothEnterInput.ToolButton3Click(Sender: TObject);
begin

//  if Trim(CoAbbrName.Text) = '' then
//  begin
//    Application.MessageBox('简称不能为空!', '提示', 0);
//    Exit;
//  end;
//  if Trim(CoName.Text) = '' then
//  begin
//    Application.MessageBox('名称不能为空!', '提示', 0);
//    Exit;
//  end;
  if SaveData('保存') then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    ModalResult := 1;
  end;
end;

end.

