unit U_YMTFHSQInPut;

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, dxSkinsCore, dxSkinDarkRoom,
  dxSkinOffice2013White, dxSkinSharpPlus, dxSkinSpringTime,
  dxSkinsDefaultPainters, dxSkinscxPCPainter, dxSkinBlack, dxSkinBlue,
  dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSilver,
  dxSkinStardust, dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinValentine,
  dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue;

type
  TfrmFHSQInPut = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    ToolBar2: TToolBar;
    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;
    CRType: TComboBox;
    Note: TEdit;
    Label12: TLabel;
    Label11: TLabel;
    SHDanWei: TBtnEditC;
    v1Column6: TcxGridDBColumn;
    Label2: TLabel;
    SQDate: TDateTimePicker;
    Label5: TLabel;
    Salesman: TBtnEditC;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    ToolButton3: TToolButton;
    Label3: TLabel;
    JGType: TEdit;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure SHDanWeiBtnUpClick(Sender: TObject);
    procedure SHDanWeiBtnDnClick(Sender: TObject);
    procedure ShouKuanKeyPress(Sender: TObject; var Key: Char);
    procedure SalesmanBtnUpClick(Sender: TObject);
    procedure SalesmanBtnDnClick(Sender: TObject);
    procedure v1Column10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton3Click(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_YMTOrderSel, U_ZdyAttachKH, U_ZdyAttachGYS;       //    U_YMTKHList, , U_YMTGYSList

{$R *.dfm}

procedure TfrmFHSQInPut.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('发货申请录入1', 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);
  end;
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*   from YMT_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.*  from YMT_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('发货申请录入1', 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, 'NK', 'YMT_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 YMT_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, 'YMT_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', 'YMT_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 YMT_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, 'YMT_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;
          FieldByName('FHPrice').Value := Order_Sub.fieldbyname('FHPrice').Value;
          FieldByName('BZType').Value := Order_Sub.fieldbyname('BZType').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 YMT_FHSQ_Main Set ');
      sql.Add('SQPSHZ=(select Sum(SQPS) from YMT_FHSQ_Sub A where A.FSID=YMT_FHSQ_Main.FSID)');
      sql.Add(',SQQtyHZM=(select Sum(SQQtyM) from YMT_FHSQ_Sub A where A.FSID=YMT_FHSQ_Main.FSID)');
      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 Order_Sub.IsEmpty 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.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 YMT_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 YMT_FHSQ_Sub where SSId=''' + Trim(Order_Sub.fieldbyname('SSId').AsString) + '''');
      ExecSQL;
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YMT_FHSQ_Main Set ');
      sql.Add('SQPSHZ=(select Sum(SQPS) from YMT_FHSQ_Sub A where A.FSID=YMT_FHSQ_Main.FSID)');
      sql.Add('where FSID=''' + Trim(FMainId) + '''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmFHSQInPut.SHDanWeiBtnUpClick(Sender: TObject);
begin

  if CRType.Text = '加工出库' then
  begin
    try
      frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
      with frmZdyAttachGYS do
      begin
        if ShowModal = 1 then
        begin
          Self.SHDanWei.Text := Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('KHNameJC').AsString);
          Self.SHDanWei.TxtCode := Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('KHNo').AsString);
          Self.Salesman.Text := Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('YWY').AsString);
        end;
      end;
    finally
      frmZdyAttachGYS.Free;
    end;
  end
  else
  begin
    try
      frmZdyAttachkh := TfrmZdyAttachkh.Create(Application);
      with frmZdyAttachkh do
      begin
//      if self.canshu1 <> '高权限' then
//        canshu1 := '业务员';
        if ShowModal = 1 then
        begin
          Self.SHDanWei.Text := Trim(frmZdyAttachkh.CDS_HZ.fieldbyname('KHNameJC').AsString);
          Self.SHDanWei.TxtCode := Trim(frmZdyAttachkh.CDS_HZ.fieldbyname('KHNo').AsString);
          Self.Salesman.Text := Trim(frmZdyAttachkh.CDS_HZ.fieldbyname('YWY').AsString);
        end;
      end;
    finally
      frmZdyAttachkh.Free;
    end;
  end;
end;

procedure TfrmFHSQInPut.SHDanWeiBtnDnClick(Sender: TObject);
begin
  SHDanWei.Text := '';
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.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;

procedure TfrmFHSQInPut.v1Column10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'FeeName';
      flagname := '费用名称';
      if ShowModal = 1 then
      begin
        with Order_Sub do
        begin
          Edit;
          FieldByName('FeeName').Value := Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmFHSQInPut.ToolButton3Click(Sender: TObject);
begin

  try
    frmOrderSel := TfrmOrderSel.Create(Application);
    with frmOrderSel do
    begin

      FKHNo := Trim(SHDanWei.TxtCode);
//      FTT := Trim(FHTaiTou.text);

      if ShowModal = 1 then
      begin
        while frmOrderSel.CDS_Main.Locate('SSel', true, []) do
        begin
          if Trim(Self.SHDanWei.Text) = '' then
          begin
            SHDanWei.TxtCode := Trim(frmOrderSel.CDS_Main.fieldbyname('KHNO').AsString);
            with ADOQueryTemp do
            begin
              Close;
              sql.Clear;
              sql.Add('select * from KH_Main where KHNO=''' + Trim(SHDanWei.TxtCode) + '''');
              open;
            end;
            SHDanWei.Text := Trim(ADOQueryTemp.fieldbyname('KHNameJC').AsString);
          end;

//          if Trim(Self.FHTaiTou.Text) = '' then
//          begin
//            FHTaiTou.Text := Trim(frmOrderSel.CDS_Main.fieldbyname('SYRName').AsString);
//          end;

          if Self.Order_Sub.Locate('SubId;CPType', VarArrayOf([Trim(frmOrderSel.CDS_Main.fieldbyname('SubId').AsString), Trim(frmOrderSel.CDS_Main.fieldbyname('CPType').AsString)]), []) = False then
          begin
            with Self.Order_Sub do
            begin
              Append;
              FieldByName('MainId').Value := frmOrderSel.CDS_Main.fieldbyname('MainId').Value;
              FieldByName('SubId').Value := frmOrderSel.CDS_Main.fieldbyname('SubId').Value;
              FieldByName('MXPrtName').Value := Trim(frmOrderSel.CDS_Main.fieldbyname('MLCodeName').AsString);
              FieldByName('MXPrtColor').Value := Trim(frmOrderSel.CDS_Main.fieldbyname('MLColor').AsString);
              FieldByName('MXPrtHX').Value := Trim(frmOrderSel.CDS_Main.fieldbyname('MLHX').AsString);
              FieldByName('SCConNo').Value := Trim(frmOrderSel.CDS_Main.fieldbyname('MLOrderNo').AsString);
//              CRType.Text := Trim(frmOrderSel.CDS_Main.fieldbyname('CRType').AsString);

              CRType.ItemIndex := CRType.Items.IndexOf(trim(frmOrderSel.CDS_Main.fieldbyname('CRType').AsString));
              FieldByName('CPType').Value := '成品';
              FieldByName('SQPS').Value := 0;
              FieldByName('SQQtyM').Value := 0;
              FieldByName('BZType').Value := frmOrderSel.CDS_Main.fieldbyname('BZType').Value;
              FieldByName('FHPrice').Value := frmOrderSel.CDS_Main.fieldbyname('MLPrice').Value;
              FieldByName('FeeName').Value := frmOrderSel.CDS_Main.fieldbyname('FeeName').Value;
              FieldByName('FeeMoney').Value := frmOrderSel.CDS_Main.fieldbyname('FeeMoney').Value;
              Post;
            end;
          end;
          frmOrderSel.CDS_Main.Delete;
        end;
      end;
    end;
  finally
    frmOrderSel.Free;
  end;
end;

end.

