unit U_FHSQInPut;

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;

type
  TfrmFHSQInPut = 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;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Label1: TLabel;
    CRType: TComboBox;
    Note: TEdit;
    Label12: TLabel;
    Label11: TLabel;
    SHDanWei: TBtnEditC;
    Label13: TLabel;
    FHTaiTou: TBtnEditC;
    Label17: TLabel;
    TruckId: TEdit;
    v1Column3: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    Label2: TLabel;
    SQDate: TDateTimePicker;
    Label5: TLabel;
    Salesman: TBtnEditC;
    Label8: TLabel;
    ZZType: TEdit;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure SHDanWeiBtnUpClick(Sender: TObject);
    procedure SHDanWeiBtnDnClick(Sender: TObject);
    procedure FHTaiTouBtnDnClick(Sender: TObject);
    procedure FHTaiTouBtnUpClick(Sender: TObject);
    procedure ShouKuanKeyPress(Sender: TObject; var Key: Char);
    procedure v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure SalesmanBtnUpClick(Sender: TObject);
    procedure SalesmanBtnDnClick(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;
    FZZType: string; 
    { Public declarations }
  end;

var
  frmFHSQInPut: TfrmFHSQInPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_RTFun, U_SQKCHZSelList, U_ProductOrderSel,
  U_ZdyAttachment;

{$R *.dfm}

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

procedure TfrmFHSQInPut.InitData();
var
  fsj: string;
begin
//  fsj:='select Name=ZdyName from KH_Zdy where Type=''ClothType'' ';
//  SInitComBoxBySql(ADOTemp,ClothType,False,fsj);
  if PState = 0 then
  begin
    Salesman.Text := dname;
    SQDate.DateTime := SGetServerDate10(ADOTemp);
    ZZType.Text := trim(FZZType);
  end;
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*  ');
    sql.Add(' from FHSQ_Sub A');
    if PState = 1 then
    begin
      sql.Add('where FSId=''' + 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 FHSQ_Main A');
    sql.Add(' where FSId=''' + Trim(FMainId) + ''' ');
    Open;
  end;
  SCSHDataNew(ADOQuery1, ScrollBox1, 1);
end;

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

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

function TfrmFHSQInPut.SaveData(): Boolean;
var
  maxno, maxmnno, FSSId: string;
  fconNO, fmxType: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId) = '' then
    begin
      if GetLSNoHZ(ADOCmd, maxno, 'CK', 'FHSQ_Main', 3, 1, 3) = 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 FHSQ_Main where FSID=''' + Trim(FMainId) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId) = '' then
      begin
        Append;
      end
      else
      begin
        Edit;
      end;
      FieldByName('SQType').Value := '坯布';
      FieldByName('FSID').Value := Trim(maxno);
      RTSetsavedata(ADOCmd, 'FHSQ_Main', ScrollBox1, 1);

      if Trim(FMainId) = '' then
      begin
        FieldByName('Filler').Value := Trim(DName);

      end
      else
      begin
        FieldByName('Editer').Value := Trim(DName);
        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('SSId').AsString) = '' then
        begin
          if GetLSNo(ADOCmd, maxno, 'SS', 'FHSQ_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('SSId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from FHSQ_Sub ');
          sql.Add(' where SSId=''' + 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;
            FieldByName('Filler').Value := Trim(DName);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
          end;
          FieldByName('FSID').Value := Trim(FMainId);
          FieldByName('SSID').Value := Trim(maxno);
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'FHSQ_Sub', 0);
          FieldByName('MainId').Value := Order_Sub.fieldbyname('MainId').Value;
          FieldByName('SubId').Value := Order_Sub.fieldbyname('SubId').Value;
          FieldByName('SSConMainId').Value := Order_Sub.fieldbyname('SSConMainId').Value;
          FieldByName('SSConSubId').Value := Order_Sub.fieldbyname('SSConSubId').Value;
          Post;
        end;
        Order_Sub.Edit;
        Order_Sub.FieldByName('SSID').Value := Trim(maxno);
        Next;
      end;
    end;
    Order_Sub.EnableControls;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update FHSQ_Main Set ');
      sql.Add('SQPSHZ=(select Sum(SQPS) from FHSQ_Sub A where A.FSID=FHSQ_Main.FSID)');
      //sql.Add(',SQGCCode=dbo.F_Get_Order_SubStr_PB(FHSQ_Main.FSID,''SQGCCode'')');
      //sql.Add(',SQBatchNoHZ=dbo.F_Get_Order_SubStr_PB(FHSQ_Main.FSID,''SQBatchNoHZ'')');
      sql.Add('where FSID=''' + Trim(FMainId) + '''');
      ExecSQL;
    end;
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
    Result := False;
  end;
end;

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

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

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

procedure TfrmFHSQInPut.ToolButton1Click(Sender: TObject);
begin

  try
    frmSQKCHZSelList := TfrmSQKCHZSelList.Create(Application);
    with frmSQKCHZSelList do
    begin
      if ShowModal = 1 then
      begin
        while frmSQKCHZSelList.CDS_Main.Locate('SSel', true, []) do
        begin
          if Self.Order_Sub.Locate('SCConNo', Trim(frmSQKCHZSelList.CDS_Main.fieldbyname('ConNO').AsString), []) = False then
          begin
            with Self.Order_Sub do
            begin
              Append;
              FieldByName('PrtCode').Value := Trim(frmSQKCHZSelList.CDS_Main.fieldbyname('MPrtCode').AsString);
              FieldByName('PrtCodeName').Value := Trim(frmSQKCHZSelList.CDS_Main.fieldbyname('MPrtCodeName').AsString);
              FieldByName('prtmf').Value := Trim(frmSQKCHZSelList.CDS_Main.fieldbyname('MPRTMF').AsString);
              FieldByName('prtkz').Value := Trim(frmSQKCHZSelList.CDS_Main.fieldbyname('MPRTKZ').AsString);
              FieldByName('SSKHName').Value := Trim(frmSQKCHZSelList.CDS_Main.fieldbyname('CustomerNoName').AsString);
              FieldByName('SSConNo').Value := Trim(frmSQKCHZSelList.CDS_Main.fieldbyname('ConNO').AsString);
              FieldByName('SCConNo').Value := Trim(frmSQKCHZSelList.CDS_Main.fieldbyname('orderno').AsString);
              FieldByName('SQPS').Value := frmSQKCHZSelList.CDS_Main.fieldbyname('JQty').Value;
              FieldByName('MainId').Value := frmSQKCHZSelList.CDS_Main.fieldbyname('MainId').Value;
              FieldByName('SubId').Value := frmSQKCHZSelList.CDS_Main.fieldbyname('SubId').Value;
              Post;
            end;
          end;
          frmSQKCHZSelList.CDS_Main.Delete;
        end;
      end;
    end;
  finally
    frmSQKCHZSelList.Free;
  end;
end;

procedure TfrmFHSQInPut.ToolButton2Click(Sender: TObject);
begin
  if Order_Sub.IsEmpty then
    Exit;
  if Trim(Order_Sub.fieldbyname('SSId').AsString) <> '' then
  begin
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from FHSQ_Sub where SSId=''' + Trim(Order_Sub.fieldbyname('SSId').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 FHSQ_Sub where SSId=''' + Trim(Order_Sub.fieldbyname('SSId').AsString) + '''');
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update FHSQ_Main Set ');
      sql.Add('SQPSHZ=(select Sum(SQPS) from FHSQ_Sub A where A.FSID=FHSQ_Main.FSID)');
      sql.Add('where FSID=''' + Trim(FMainId) + '''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmFHSQInPut.SHDanWeiBtnUpClick(Sender: TObject);
begin
  try
    frmZdyAttachment := TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin

      if ShowModal = 1 then
      begin
        SHDanWei.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
//  try
//    frmZDYHelp:=TfrmZDYHelp.Create(Application);
//    with frmZDYHelp do
//    begin
//      flag:='SHDanWei';
//      flagname:='收货单位';
//      if ShowModal=1 then
//      begin
//        Self.SHDanWei.Text:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
//      end;
//    end;
//  finally
//    frmZDYHelp.Free;
//  end;
end;

procedure TfrmFHSQInPut.SHDanWeiBtnDnClick(Sender: TObject);
begin
  SHDanWei.Text := '';
end;

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

procedure TfrmFHSQInPut.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 TfrmFHSQInPut.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 TfrmFHSQInPut.v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
//  try
//    frmCPManageFMSel:=TfrmCPManageFMSel.Create(Application);
//    with frmCPManageFMSel do
//    begin
//      if ShowModal=1 then
//      begin
//        Self.PrtCodeName.Text:=Trim(frmCPManageFMSel.CDS_Main.fieldbyname('CYName').AsString);
//        Self.PrtCode.Text:=Trim(frmCPManageFMSel.CDS_Main.fieldbyname('CYNo').AsString);
////        Self.C_Spec.Text:=Trim(frmCPManageFMSel.CDS_Main.fieldbyname('CYSpec').AsString);
//      end;
//    end;
//  finally
//    frmCPManageFMSel.Free;
//  end;
end;

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

procedure TfrmFHSQInPut.SalesmanBtnDnClick(Sender: TObject);
begin
  Salesman.Text := '';
end;

end.

