unit U_TradeClothProcessNoteInput;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxCalendar, cxDropDownEdit, ComCtrls, ToolWin,
  cxGridLevel, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxClasses, cxControls, cxGridCustomView, cxGrid, cxGridCustomPopupMenu,
  cxGridPopupMenu, ADODB, DBClient, cxButtonEdit, cxTextEdit, StdCtrls, ExtCtrls,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxDateRanges,
  dxBarBuiltInMenu, U_BaseInput, System.ImageList, Vcl.ImgList, cxCheckBox, cxPC,
  dxScrollbarAnnotations, dxSkinsCore, dxSkinsDefaultPainters, cxImageList;

type
  TfrmTradeClothProcessNoteInput = class(TfrmBaseInput)
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBSave: TToolButton;
    TBClose: TToolButton;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    GPM_1: TcxGridPopupMenu;
    v1Column6: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBAddClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    { Private declarations }
    function SaveData(MSaveType: string): Boolean;
    function CheckData(): Boolean;
  public
    { Public declarations }
    FMasterOrderNo: string;
  end;

var
  frmTradeClothProcessNoteInput: TfrmTradeClothProcessNoteInput;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

function TfrmTradeClothProcessNoteInput.CheckData(): Boolean;
var
  mvalue, FReal: double;
begin
  try
    if CDS_1.IsEmpty then
      raise Exception.Create('明细不能为空！');

    if CDS_1.Locate('TPPNDate', null, []) = True then
      raise Exception.Create('入库时间不能为空！');

    Result := True;
  except
    Result := false;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;
end;

procedure TfrmTradeClothProcessNoteInput.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := caFree;
end;

procedure TfrmTradeClothProcessNoteInput.TBAddClick(Sender: TObject);
begin

  with CDS_1 do
  begin
    Append;
    FieldByName('TPPNDate').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
    Post;
  end;

end;

function TfrmTradeClothProcessNoteInput.SaveData(MSaveType: string): Boolean;
var
  Maxno, MBCIOID: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_1.DisableControls;
    with CDS_1 do
    begin
      First;
      while not eof do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Trade_Plan_Process_Note where TPPNId=''' + Trim(CDS_1.fieldbyname('TPPNId').AsString) + '''');
          open;
        end;
        MBCIOID := Trim(ADOQueryTemp.fieldbyname('TPPNId').AsString);
        if Trim(MBCIOID) = '' then
        begin
          if not GetLSNo(ADOQueryCmd, Maxno, 'MR', 'Trade_Plan_Process_Note', 4, 1) then
            raise Exception.Create('取染色坯布入库编号失败！');
        end
        else
        begin
          Maxno := Trim(MBCIOID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Trade_Plan_Process_Note where TPPNId=''' + Trim(Maxno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(MBCIOID) = '' 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 := SGetServerDate(ADOQueryTemp);
          end;
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_1, 'Trade_Plan_Process_Note', 0);
          FieldByName('TPPNId').Value := Trim(Maxno);
          FieldByName('MasterOrderNo').Value := Trim(FMasterOrderNo);

          Post;
        end;

        Edit;
        FieldByName('TPPNId').Value := Trim(Maxno);
        Post;
        Next;
      end;
    end;

    CDS_1.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;

    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure TfrmTradeClothProcessNoteInput.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmTradeClothProcessNoteInput.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(self.caption + 'TV1', Tv1, '贸易汇总仓库');

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from Trade_Plan_Process_Note A');
    sql.Add(' where  MasterOrderNo=''' + Trim(FMasterOrderNo) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_1);
  SInitCDSData(ADOQueryTemp, CDS_1);
end;

procedure TfrmTradeClothProcessNoteInput.TBDelClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  if Trim(CDS_1.fieldbyname('TPPNId').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;

    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        Sql.Clear;

        Sql.Add('delete Trade_Plan_Process_Note where TPPNId=' + quotedstr(Trim(CDS_1.fieldbyname('TPPNId').AsString)));

        Open;
      end;
      ADOQueryCmd.Connection.CommitTrans;
      CDS_1.Delete;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    end;
  end
  else
  begin
    CDS_1.Delete;
  end;

end;

procedure TfrmTradeClothProcessNoteInput.TBSaveClick(Sender: TObject);
begin

  if CheckData() = false then
    exit;

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

procedure TfrmTradeClothProcessNoteInput.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(self.caption + 'TV1', Tv1, '贸易汇总仓库');

end;

end.

