unit U_TradePlanColorResultInput;

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,    
     
    
     
     
    
    
    
    
    
    
     
      
     
   
    
   Vcl.Menus;

type
  TfrmTradePlanColorResultInput = 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;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    TV2Column6: TcxGridDBColumn;
    TV1Column1: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    TV1Column4: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    TV1Column5: TcxGridDBColumn;
    TV1Column6: TcxGridDBColumn;
    TV1Column7: TcxGridDBColumn;
    TV1Column8: TcxGridDBColumn;
    TV1Column9: TcxGridDBColumn;
    TV1Column10: TcxGridDBColumn;
    TV1Column11: TcxGridDBColumn;
    TV1Column12: TcxGridDBColumn;
    TV1Column13: TcxGridDBColumn;
    TV1Column14: TcxGridDBColumn;
    TV1Column15: TcxGridDBColumn;
    TV1Column16: TcxGridDBColumn;
    TV1Column17: TcxGridDBColumn;
    TV1Column18: TcxGridDBColumn;
    TV1Column19: TcxGridDBColumn;
    TV1Column20: TcxGridDBColumn;
    TV1Column21: TcxGridDBColumn;
    TV1Column22: TcxGridDBColumn;
    TV1Column23: TcxGridDBColumn;
    TV1Column24: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    { Private declarations }
    function SaveData(MSaveType: string): Boolean;
    function CheckData(): Boolean;
  public
    { Public declarations }
    FSubId, FmainID: string;
  end;

var
  frmTradePlanColorResultInput: TfrmTradePlanColorResultInput;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

function TfrmTradePlanColorResultInput.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 TfrmTradePlanColorResultInput.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := caFree;
end;

function TfrmTradePlanColorResultInput.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_Technical_Results where TPTRID=''' + Trim(CDS_Sub.fieldbyname('TPTRID').AsString) + '''');
          open;
        end;
        MBCIOID := Trim(ADOQueryTemp.fieldbyname('TPTRID').AsString);
        if Trim(MBCIOID) = '' then
        begin
          if not GetLSNo(ADOQueryCmd, Maxno, 'Y', 'Trade_Plan_Technical_Results', 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_Technical_Results where TPTRID=''' + 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_Technical_Results', 2);
          FieldByName('SubId').Value := FSubId;
          FieldByName('TPTID').Value := Trim(CDS_Sub.fieldbyname('TPTID').AsString);
          FieldByName('TPTRID').Value := Trim(Maxno);
          Post;
        end;

        Edit;
        FieldByName('TPTRID').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 TfrmTradePlanColorResultInput.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmTradePlanColorResultInput.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(Self.Caption + 'Tv1', Tv1, '贸易汇总仓库');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.*,B.TPTRID ');
    sql.Add(' ,B.Result1,B.Result2,B.Result3,B.Result4,B.Result5,B.Result6,B.Result7,B.Result8,B.Result9,B.Result10,B.Result11,B.Result12 ');
    sql.Add(' ,B.Result13,B.Result14,B.Result15,B.Result16,B.Result17,B.Result18,B.Result19,B.Result20,B.Result21,B.Result22,B.Result23,B.Result24 ');
    sql.Add(' from Trade_Plan_Technical A left join Trade_Plan_Technical_Results B on A.TPTID=B.TPTID');
    sql.Add(' and B.Subid =' + quotedstr(Trim(FSubId)));
    sql.Add(' where A.mainID =' + quotedstr(Trim(FmainID)));
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_Sub);
  SInitCDSData(ADOQueryTemp, CDS_Sub);
end;

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

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

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

procedure TfrmTradePlanColorResultInput.ToolButton2Click(Sender: TObject);
var
  mvalue, FFieldName: string;
  MDqgs: integer;
  I, J: Integer;
begin
  ToolBar1.SetFocus;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  MDqgs := StrToIntdef(Copy(FFieldName, 7, 3), 0);

  if MDqgs mod 4 = 0 then
    J := 4
  else
    J := MDqgs mod 4;

  with CDS_Sub do
  begin
    DisableControls;
    First;
    while not EOF do
    begin
      mvalue := CDS_Sub.FieldByName(FFieldName).AsString;
      for I := 1 to 4 - MDqgs mod 4 do
      begin
        with CDS_Sub do
        begin
          Edit;
          FieldByName('Result' + IntToStr(I + MDqgs)).Value := mvalue;
          Post;
        end;
      end;
      Next;
    end;
    EnableControls;
  end;

end;

procedure TfrmTradePlanColorResultInput.ToolButton3Click(Sender: TObject);
var
  mvalue, FFieldName: string;
  MDqgs: integer;
begin
  ToolBar1.SetFocus;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  MDqgs := StrToIntdef(Copy(FFieldName, 7, 3), 0);

  with CDS_Sub do
  begin
    DisableControls;
    First;
    while not EOF do
    begin
      mvalue := CDS_Sub.FieldByName(FFieldName).AsString;
      with CDS_Sub do
      begin
        Edit;
        FieldByName('Result' + IntToStr(1 + MDqgs)).Value := mvalue;
        Post;
      end;

      Next;
    end;
    EnableControls;
  end;

end;

end.

