unit U_CaiGouSQInPut;

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, cxDropDownEdit,
  cxCalendar, cxGridCustomPopupMenu, cxGridPopupMenu;

type
  TfrmCaiGouSQInPut = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DataSource1: TDataSource;
    Order_Sub: TClientDataSet;
    ADOQuery1: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ToolButton4: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column7: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ADOQueryTemp: TADOQuery;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    TBTP: TToolButton;
    v1Column16: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure v1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column12PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column14PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TBTPClick(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure v1Column23PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    procedure InitData();
    function SaveData(): Boolean;
    { Private declarations }
  public
    PState: Integer;
    FMainId, FSQType, Canshu1, ZFormName: string;

    { Public declarations }
  end;

var
  frmCaiGouSQInPut: TfrmCaiGouSQInPut;

implementation

uses
  U_DataLink, U_ZDYHelp, U_RTFun, U_ZdyAttachGYS, U_YarnListSel;

{$R *.dfm}

procedure TfrmCaiGouSQInPut.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('采购申请登记', Tv1, '采购申请管理');
end;

procedure TfrmCaiGouSQInPut.InitData();
begin
  with ADOQuery1 do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select A.*');
    SQL.Add(' from SQ_CG A ');
    sql.Add(' where A.SQId=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCreateCDS20(ADOQuery1, Order_Sub);
  SInitCDSData20(ADOQuery1, Order_Sub);

end;

procedure TfrmCaiGouSQInPut.FormShow(Sender: TObject);
begin
  ReadCxGrid('采购申请登记', Tv1, '采购申请管理');
  InitData();
  if Trim(Canshu1) = '高权限' then
  begin
    TBTP.Visible := True;
  end;
end;

function TfrmCaiGouSQInPut.SaveData(): Boolean;
var
  maxno, FSKID: string;
  FYLQty, FYLMXQty, FSQQty, FCGQtyUp, FCGQtyZJUp: Double;
begin

  try
    Result := False;
    ADOCmd.Connection.BeginTrans;
    //保存主表
    Order_Sub.DisableControls;
    with Order_Sub do
    begin
      First;
      while not Eof do
      begin
        with ADOTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from SQ_CG where SQID=''' + Trim(Order_Sub.fieldbyname('SQID').AsString) + '''');
          Open;
        end;
        FSKID := Trim(ADOTemp.fieldbyname('SQID').AsString);
        if Trim(FSKID) = '' then
        begin
          if GetLSNoHZ(ADOCmd, maxno, 'SQ', 'SQ_CG', 4, 1, 3) = False then
          begin
            Order_Sub.EnableControls;
            ADOCmd.Connection.RollbackTrans;
            Application.MessageBox('取采购申请登记表流水号失败!', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          maxno := Trim(FSKID);
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from SQ_CG where SQID=''' + Trim(FSKID) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(FSKID) = '' then
          begin
            Append;
          end
          else
          begin
            Edit;
          end;
          FieldByName('SQID').Value := Trim(maxno);
          RTSetSaveDataCDS(ADOCmd, Tv1, Order_Sub, 'SQ_CG', 0);
          FieldByName('FactoryNo').Value := Trim(Order_Sub.fieldbyname('FactoryNo').AsString);
          FieldByName('CFFactoryNo').Value := Trim(Order_Sub.fieldbyname('CFFactoryNo').AsString);
          FieldByName('PBID').Value := Trim(Order_Sub.fieldbyname('PBID').AsString);
          FieldByName('PXID').Value := Trim(Order_Sub.fieldbyname('PXID').AsString);
          FieldByName('MXPBID').Value := Trim(Order_Sub.fieldbyname('MXPBID').AsString);
          FieldByName('SQType').Value := '纱线';
          FieldByName('DataType').Value := Trim(Order_Sub.fieldbyname('DataType').AsString);
          if Trim(FSKID) = '' then
          begin
            FieldByName('Filler').Value := Trim(DName);
          end
          else
          begin
            if Trim(ZFormName) = '可修改' then
            begin
              FieldByName('Editer').Value := Trim(DName) + '高权限修改';
            end
            else
            begin
              FieldByName('Editer').Value := Trim(DName);
            end;
            FieldByName('EditTime').Value := SGetServerDateTime(ADOTemp);
          end;
          Post;
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('Update SQ_CG Set SQMoney=SQQty*SQPrice where SQID=''' + Trim(maxno) + '''');
          ExecSQL;
        end;
        if Trim(Order_Sub.fieldbyname('SQQtyUpHZ').AsString) <> '' then
        begin
          with ADOCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update SQ_CG Set QtySXStr=''汇总'' where SQID=''' + Trim(maxno) + '''');
            ExecSQL;
          end;
        end;

        Edit;
        FieldByName('SQID').Value := Trim(maxno);
        Next;
      end;
    end;
    Order_Sub.EnableControls;
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

procedure TfrmCaiGouSQInPut.TBSaveClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if Order_Sub.IsEmpty then
  begin
    Application.MessageBox('明细不能为空！', '提示', 0);
    exit;
  end;
  if Order_Sub.Locate('SQDate', null, []) then
  begin
    Application.MessageBox('申请日期不能为空!', '提示', 0);
    Exit;
  end;
  if Order_Sub.Locate('CFFactoryName', null, []) then
  begin
    Application.MessageBox('入库单位不能为空!', '提示', 0);
    Exit;
  end;
  if Order_Sub.Locate('SQPerson', null, []) then
  begin
    Application.MessageBox('申请人不能为空!', '提示', 0);
    Exit;
  end;
  if Order_Sub.Locate('pingzhi', null, []) then
  begin
    Application.MessageBox('品质不能为空!', '提示', 0);
    Exit;
  end;
  if Order_Sub.Locate('SPType', null, []) then
  begin
    Application.MessageBox('类型不能为空!', '提示', 0);
    Exit;
  end;
  if Order_Sub.Locate('SPName', null, []) then
  begin
    Application.MessageBox('品名不能为空!', '提示', 0);
    Exit;
  end;
  if Order_Sub.Locate('FactoryName', null, []) then
  begin
    Application.MessageBox('供应商不能为空!', '提示', 0);
    Exit;
  end;

  if Order_Sub.Locate('SQPrice', null, []) then
  begin
    Application.MessageBox('申请单价不能为空!', '提示', 0);
    Exit;
  end;
  if Order_Sub.Locate('SQQtyUp;SQQtyUpHZ', VarArrayOf([null, null]), [loPartialKey]) then
  begin
    if Order_Sub.FieldByName('SQQtyHZ').AsString <> '' then
    begin
      Application.MessageBox('汇总数量上限%不能为空!', '提示', 0);
      Exit;
    end
    else
    begin
      Application.MessageBox('数量上限%不能为空!', '提示', 0);
      Exit;
    end;
    Application.MessageBox('数量上限不能为空!', '提示', 0);
    Exit;
  end;
  if Order_Sub.Locate('SQQty;SQQtyHZ', VarArrayOf([null, null]), [loPartialKey]) then
  begin
    if Order_Sub.FieldByName('SQQtyUpHZ').AsString <> '' then
    begin
      Application.MessageBox('汇总数量不能为空!', '提示', 0);
      Exit;
    end
    else
    begin
      Application.MessageBox('数量不能为空!', '提示', 0);
      Exit;
    end;

  end;

  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
   // ModalResult:=1;
  end;

end;

procedure TfrmCaiGouSQInPut.ToolButton4Click(Sender: TObject);
var
  FWorker, FDate: string;
begin
  if Order_Sub.IsEmpty then
    Exit;
  if Trim(Order_Sub.fieldbyname('SQID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete CaiGou_SQ where SQId=''' + Trim(Order_Sub.fieldbyname('SQId').AsString) + '''');
      ExecSQL;
    end;
  end;
  Order_Sub.Delete;
end;

procedure TfrmCaiGouSQInPut.v1Column6PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SXSPSpec';
      flagname := '纱线规格';
      if ShowModal = 1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('SPSpec').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCaiGouSQInPut.v1Column10PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SXSPCF';
      flagname := '纱线成分';
      if ShowModal = 1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('SPCF').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCaiGouSQInPut.v1Column12PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SXPinPai';
      flagname := '纱线品牌';
      if ShowModal = 1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('PinPai').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCaiGouSQInPut.v1Column14PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('FactoryNo').Value := Trim(CDS_HZ.fieldbyname('Cocode').AsString);
          FieldByName('FactoryName').Value := Trim(CDS_HZ.fieldbyname('cohrName').AsString);
//          FieldByName('ZDYNameTj').Value := Trim(CDS_HZ.fieldbyname('ZDYNameTj').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmCaiGouSQInPut.v1Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin

  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SXSQPerson';
      flagname := '申请人';
      if ShowModal = 1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('SQPerson').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmCaiGouSQInPut.TBTPClick(Sender: TObject);
begin
  with Self.Order_Sub do
  begin
    Append;
    FieldByName('SQQtyUnit').Value := 'Kg';
    FieldByName('SQPerson').Value := Trim(DName);
    FieldByName('SQDate').Value := SGetServerDate(ADOQueryTemp);
    FieldByName('DataType').Value := '入库';
    //Post;
  end;
end;

procedure TfrmCaiGouSQInPut.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if Trim(ZFormName) <> '可修改' then
  begin
    if Trim(Order_Sub.fieldbyname('ConNo').AsString) = '' then
    begin
      v1Column4.Options.Editing := True;
      v1Column6.Options.Editing := True;
    end
    else
    begin
      v1Column4.Options.Editing := False;
      v1Column6.Options.Editing := False;
    end;
  end
  else
  begin
    v1Column4.Options.Editing := True;
  end;

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from CK_SXPB_CR A where A.ZFFlag=0 and A.SQID= ''' + Trim(Order_Sub.fieldbyname('SQID').AsString) + '''');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    v1Column14.Options.Editing := True;
  end;

end;

procedure TfrmCaiGouSQInPut.v1Column23PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('CFFactoryNo').Value := Trim(CDS_HZ.fieldbyname('cocode').AsString);
          FieldByName('CFFactoryName').Value := Trim(CDS_HZ.fieldbyname('cohrname').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmCaiGouSQInPut.v1Column4PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmYarnListSel := TfrmYarnListSel.Create(Application);
    with frmYarnListSel do
    begin
      if ShowModal = 1 then
      begin
        with Self.Order_Sub do
        begin
          Edit;
          FieldByName('SPName').Value := Trim(CDS_HZ.fieldbyname('Y_CodeName').AsString);
          FieldByName('SPSpec').Value := Trim(CDS_HZ.fieldbyname('Y_Spec').AsString);
          FieldByName('SPCF').Value := Trim(CDS_HZ.fieldbyname('Y_CF').AsString);
        end;
      end;
    end;
  finally
    frmYarnListSel.Free;
  end;
end;

end.

