unit U_TradePlanColorLogInput;

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,   
  cxImageList;

type
  TfrmTradePlanColorLogInput = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    DS_1: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    GPM_1: TcxGridPopupMenu;
    ToolButton1: TToolButton;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridLevel2: TcxGridLevel;
    TV1Column1: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    { Private declarations }
    function SaveData(MSaveType: string): Boolean;
    function CheckData(): Boolean;
  public
    { Public declarations }
    FSubId, FmainID: string;
  end;

var
  frmTradePlanColorLogInput: TfrmTradePlanColorLogInput;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

function TfrmTradePlanColorLogInput.CheckData(): Boolean;
var
  mvalue, FReal: double;
begin
  try
//    if CDS_Sub.IsEmpty then
//      raise Exception.Create('明细不能为空！');
//
//    if CDS_Sub.Locate('IOTime', null, []) = True then
//      raise Exception.Create('入库时间不能为空！');
//
//    if CDS_Sub.Locate('IOType', null, []) = True then
//      raise Exception.Create('入库类型不能为空！');
//
//    if CDS_Sub.Locate('FromCoName', null, []) = True then
//      raise Exception.Create('来自单位不能为空！');
//
//    if CDS_Sub.Locate('C_Name', null, []) = True then
//      raise Exception.Create('品名不能为空！');
//
//    if CDS_Sub.Locate('Piece', null, []) = True then
//      raise Exception.Create('匹数不能为空！');
//
//    if CDS_Sub.Locate('Qty', null, []) = True then
//      raise Exception.Create('数量不能为空！');
//
//    if CDS_Sub.Locate('QtyUnit', null, []) = True then
//      raise Exception.Create('数量单位不能为空！');

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

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

function TfrmTradePlanColorLogInput.SaveData(MSaveType: string): Boolean;
var
  Maxno, MBCIOID: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not eof do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from Trade_Plan_ColorLog where TPCID=''' + Trim(CDS_Sub.fieldbyname('TPCID').AsString) + '''');
          open;
        end;
        MBCIOID := Trim(ADOQueryTemp.fieldbyname('TPCID').AsString);
        if Trim(MBCIOID) = '' then
        begin
          if not GetLSNo(ADOQueryCmd, Maxno, 'Y', 'Trade_Plan_ColorLog', 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_ColorLog where TPCID=''' + 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_Sub, 'Trade_Plan_ColorLog', 0);
          FieldByName('SubId').Value := FSubId;
          FieldByName('TPCID').Value := Trim(Maxno);
          Post;
        end;

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

    CDS_Sub.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;

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

end;

procedure TfrmTradePlanColorLogInput.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmTradePlanColorLogInput.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_ColorLog A  ');
    sql.Add(' where A.Subid =' + quotedstr(Trim(FSubId)));
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_Sub);
  SInitCDSData(ADOQueryTemp, CDS_Sub);

  if CDS_Sub.IsEmpty then
  begin
    with CDS_Sub do
    begin
      Append;
      FieldByName('TPCIdx').Value := 1;
      Append;
      FieldByName('TPCIdx').Value := 2;
      Append;
      FieldByName('TPCIdx').Value := 3;
      Append;
      FieldByName('TPCIdx').Value := 4;
      Append;
      FieldByName('TPCIdx').Value := 5;
      Append;
      FieldByName('TPCIdx').Value := 6;
      Post;
    end;
  end;
end;

procedure TfrmTradePlanColorLogInput.TBSaveClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if CheckData() = false then
    exit;

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

procedure TfrmTradePlanColorLogInput.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(Self.Caption + 'Tv1', Tv1, '贸易汇总仓库');
end;

end.

