unit U_LSFHSQInPut;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid, cxMemo,
  cxRichEdit, ComCtrls, cxContainer, cxTextEdit, cxMaskEdit, cxButtonEdit,
  StdCtrls, ToolWin, DBClient, ADODB, ExtCtrls, BtnEdit, cxCalendar, StrUtils,
  cxDropDownEdit, cxGridCustomPopupMenu, cxGridPopupMenu, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator;

type
  TfrmLSFHSQInPut = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    v1PRTColor: TcxGridDBColumn;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    v1Column14: TcxGridDBColumn;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Label1: TLabel;
    SQType: TComboBox;
    Note: TEdit;
    Label12: TLabel;
    v1Column6: TcxGridDBColumn;
    Label2: TLabel;
    SQDate: TDateTimePicker;
    Label5: TLabel;
    YWYName: TBtnEditC;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Label3: TLabel;
    KHName: TBtnEditC;
    Label4: TLabel;
    KHNameDZ: TEdit;
    Tv1Column1: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure FHTaiTouBtnDnClick(Sender: TObject);
    procedure FHTaiTouBtnUpClick(Sender: TObject);
    procedure ShouKuanKeyPress(Sender: TObject; var Key: Char);
    procedure YWYNameBtnUpClick(Sender: TObject);
    procedure YWYNameBtnDnClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure KHNameBtnUpClick(Sender: TObject);
    procedure KHNameBtnDnClick(Sender: TObject);
  private
    fuserName: string;
    procedure InitData();
    procedure ZDYHelp(FButn: TcxButtonEdit; LType: string);
    function SaveData(): Boolean;
    { Private declarations }
  public
    PState, CopyInt: Integer;
    FMainId, FFMainId, ChkStr, ConPriceStr: string;
    FXS: Integer;
    FClothType: string; 
    { Public declarations }
  end;

var
  frmLSFHSQInPut: TfrmLSFHSQInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_RTFun, U_LSSQKCHZSel;

{$R *.dfm}

procedure TfrmLSFHSQInPut.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('发货申请录入', Tv1, 'JTS发货管理');
end;

procedure TfrmLSFHSQInPut.InitData();
var
  fsj: string;
begin
  fsj := 'select Name=ZdyName from KH_Zdy where Type=''ClothType'' ';
  if PState = 0 then
  begin
    YWYName.Text := dname;
    SQDate.DateTime := SGetServerDate10(ADOTemp);

  end;
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*  ');
    sql.Add(' from SQ_FHYT_Sub A');
    if PState = 1 then
    begin
      sql.Add('where FHMainId=''' + Trim(FMainId) + '''');
    end;
    if PState = 0 then
    begin
      sql.Add(' where 1<>1');

    end;
    Open;
  end;
  SCreateCDS20(ADOQuery1, Order_Sub);
  SInitCDSData20(ADOQuery1, Order_Sub);

  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select A.* ');
    sql.Add(' from SQ_FHYT_Main A');
    sql.Add(' where FHMainId=''' + Trim(FMainId) + ''' ');
    Open;
  end;
  SCSHDataNew(ADOQuery1, ScrollBox1, 1);
end;

procedure TfrmLSFHSQInPut.ZDYHelp(FButn: TcxButtonEdit; LType: string);
var
  FType, ZDYName, FText: string;
begin
end;

procedure TfrmLSFHSQInPut.FormShow(Sender: TObject);
begin
  readCxGrid('发货申请录入', Tv1, 'JTS发货管理');
  InitData();
end;

function TfrmLSFHSQInPut.SaveData(): Boolean;
var
  maxno, maxmnno, FSSId: string;
  fconNO, fmxType: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId) = '' then
    begin
      if GetLSNo(ADOCmd, maxno, 'FC', 'SQ_FHYT_Main', 3, 1) = False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!', '提示', 0);
        Exit;
      end;
    end
    else
    begin
      maxno := Trim(FMainId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from SQ_FHYT_Main where FHmainid=''' + Trim(FMainId) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId) = '' then
      begin
        Append;
      end
      else
      begin
        Edit;
      end;
      FieldByName('FHmainid').Value := Trim(maxno);
      RTSetsavedata(ADOCmd, 'SQ_FHYT_Main', ScrollBox1, 1);

      if Trim(FMainId) = '' then
      begin
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('FillCode').Value := Trim(DCode);
        FieldByName('FillTime').Value := SGetServerDateTime(ADOTemp);
      end
      else
      begin
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('EditCode').Value := Trim(DCode);
        FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
      end;
      Post;
    end;

    FMainId := Trim(maxno);
    ///保存子表
    Order_Sub.DisableControls;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        if Trim(Order_Sub.fieldbyname('FHSubId').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, maxno, 'SS', 'SQ_FHYT_Sub', 4, 1) = False then
          begin
            Order_Sub.EnableControls;
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取子流水号失败！', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxno := Trim(Order_Sub.fieldbyname('FHSubId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from SQ_FHYT_Sub ');
          sql.Add(' where FHSubId=''' + Trim(maxno) + '''');
          Open;
        end;
        if ADOCmd.IsEmpty then
        begin
          FSSId := '';
        end
        else
        begin
          FSSId := Trim(maxno);
        end;
        with ADOCmd do
        begin
          if Trim(FSSId) = '' then
          begin
            Append;
          end
          else
          begin
            Edit;
          end;
          FieldByName('FHMainId').Value := Trim(FMainId);
          FieldByName('FHSubId').Value := Trim(maxno);
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'SQ_FHYT_Sub', 0);
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('FHSubId').Value := Trim(maxno);
        Next;
      end;
    end;
    Order_Sub.EnableControls;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update SQ_FHYT_Main Set ');
      sql.Add('SQPiQtyHZ=(select Sum(SQPiQty) from SQ_FHYT_Sub A where A.FHMainId=SQ_FHYT_Main.FHMainId)');
      sql.Add('where SQ_FHYT_Main.FHMainId=''' + Trim(FMainId) + '''');
      ExecSQL;
    end;
    if Trim(SQType.Text)='本厂出库' then
    begin
      with ADOCmd do
      begin
        close;
        sql.Clear;
        sql.Add('update  SQ_FHYT_Main SET Chker=''' + Trim(DName) + ''',ChkTime=getdate(),ChkStatus=''审核通过'' ');
        sql.Add('where FHMainId=''' + Trim(FMainId) + '''');
        execsql;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
    Result := False;
  end;
end;

procedure TfrmLSFHSQInPut.TBSaveClick(Sender: TObject);
var
  Freal: Double;
begin
  ToolBar1.SetFocus;

  if Trim(SQType.Text) = '' then
  begin
    Application.MessageBox('出库类型不能为空！', '提示', 0);
    Exit;
  end;

  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空！', '提示', 0);
    Exit;
  end;
  if Order_Sub.Locate('SQPiQty', null, []) = True then
  begin
    Application.MessageBox('申请件数不能为空！', '提示', 0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
  end;
end;

procedure TfrmLSFHSQInPut.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    Exit;
  if Trim(Order_Sub.fieldbyname('FHSubId').AsString) <> '' then
  begin
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from SQ_FHYT_Sub where FHSubId=''' + Trim(Order_Sub.fieldbyname('FHSubId').AsString) + '''');
      sql.add(' and isnull(FHPS,0)>0');
      Open;
    end;
    if ADOTemp.IsEmpty = False then
    begin
      Application.MessageBox('已发货不能删除数据!', '提示', 0);
      exit;
    end;
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete SQ_FHYT_Sub where FHSubId=''' + Trim(Order_Sub.fieldbyname('FHSubId').AsString) + '''');
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update SQ_FHYT_Main Set ');
      sql.Add('SQPiQtyHZ=(select Sum(SQPiQty) from SQ_FHYT_Sub A where A.FHMainId=SQ_FHYT_Main.FHMainId)');
      sql.Add('where FHMainId=''' + Trim(FMainId) + '''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmLSFHSQInPut.FHTaiTouBtnDnClick(Sender: TObject);
begin
//  FHTaiTou.Text := '';
end;

procedure TfrmLSFHSQInPut.FHTaiTouBtnUpClick(Sender: TObject);
begin
//  try
//    frmZDYHelp := TfrmZDYHelp.Create(Application);
//    with frmZDYHelp do
//    begin
//      flag := 'FHTaiTou';
//      flagname := '发货抬头';
//      if ShowModal = 1 then
//      begin
//        Self.FHTaiTou.Text := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
//      end;
//    end;
//  finally
//    frmZDYHelp.Free;
//  end;
end;

procedure TfrmLSFHSQInPut.ShouKuanKeyPress(Sender: TObject; var Key: Char);
begin
  if not (((Key >= '0') and (Key <= '9')) or (Key = '.') or (Key = #8)) then
  begin
    Key := #0;
  end
  else if (pos('.', TEdit(Sender).text) <> 0) then
  begin
    if Key = '.' then
    begin
      Key := #0;
    end;
  end;
end;

procedure TfrmLSFHSQInPut.YWYNameBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'JTSYWYName';
      flagname := '业务员';
      if ShowModal = 1 then
      begin
        Self.YWYName.Text := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmLSFHSQInPut.YWYNameBtnDnClick(Sender: TObject);
begin
  ywyname.Text := '';
end;

procedure TfrmLSFHSQInPut.ToolButton1Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty=False then Exit;
  try
    frmLSSQKCHZSel := TfrmLSSQKCHZSel.Create(Application);
    with frmLSSQKCHZSel do
    begin
      if ShowModal = 1 then
      begin
        //while frmLSSQKCHZSel.CDS_Main.Locate('SSel', true, []) do
        begin

          with Self.Order_Sub do
          begin
            Append;
            FieldByName('BatchNo').Value := Trim(frmLSSQKCHZSel.CDS_Main.fieldbyname('BatchNo').AsString);
            FieldByName('Y_CodeName').Value := Trim(frmLSSQKCHZSel.CDS_Main.fieldbyname('Y_CodeName').AsString);
            FieldByName('FactoryName').Value := Trim(frmLSSQKCHZSel.CDS_Main.fieldbyname('FactoryName').AsString);
            FieldByName('Y_Spec').Value := frmLSSQKCHZSel.CDS_Main.fieldbyname('Y_Spec').Value;
            FieldByName('Y_CodeNameCP').Value := frmLSSQKCHZSel.CDS_Main.fieldbyname('Y_CodeNameCP').Value;
            FieldByName('Y_Type').Value := frmLSSQKCHZSel.CDS_Main.fieldbyname('Y_Type').Value;
            FieldByName('ZGColor').Value := frmLSSQKCHZSel.CDS_Main.fieldbyname('ZGColor').Value;
            FieldByName('SQPiQty').Value := frmLSSQKCHZSel.CDS_Main.fieldbyname('JQty').Value;
            FieldByName('SQQty').Value := frmLSSQKCHZSel.CDS_Main.fieldbyname('JYNW').Value;
            Post;
          end;
          //frmLSSQKCHZSel.CDS_Main.Delete;
        end;
      end;
    end;
  finally
    frmLSSQKCHZSel.Free;
  end;
end;

procedure TfrmLSFHSQInPut.KHNameBtnUpClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Self);
    with frmZDYHelp do
    begin
      Flag:='JTSKH';
      flagname:='客户';
      if ShowModal=1 then
      begin
        KHName.Text:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select Top 1 * from SQ_FHYT_Main where KHName='''+Trim(KHName.Text)+'''');
    sql.Add(' and isnull(KHNameDZ,'''')<>'''' order by FillTime desc');
    Open;
  end;
  KHNameDZ.Text:=Trim(ADOTemp.fieldbyname('KHNameDZ').AsString);
end;

procedure TfrmLSFHSQInPut.KHNameBtnDnClick(Sender: TObject);
begin
  KHName.Text:='';
end;

end.

