unit U_Tat_WB_InPut;

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,
  cxCurrencyEdit, cxGridCustomPopupMenu, cxGridPopupMenu, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, cxDropDownEdit, dxSkinsCore,
  dxSkinsDefaultPainters, dxDateRanges, dxBarBuiltInMenu, cxPC,
  dxScrollbarAnnotations;

type
  TfrmTat_WB_InPut = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    F_description: TEdit;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    Label2: TLabel;
    C_description: TMemo;
    Label23: TLabel;
    GPM_1: TcxGridPopupMenu;
    ToolButton4: TToolButton;
    cxPageControl1: TcxPageControl;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    Tv1Column1: TcxGridDBColumn;
    v1SOrddefstr1: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    v1PRTORDQTY: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Label6: TLabel;
    F_Name: TEdit;
    djUnit: TComboBox;
    Label8: TLabel;
    Label11: TLabel;
    C_PlanQty: TEdit;
    OrdDate: TDateTimePicker;
    DlyDate: TDateTimePicker;
    Label3: TLabel;
    Label4: TLabel;
    Tv1OrdPiece: TcxGridDBColumn;
    Label7: TLabel;
    Label15: TLabel;
    OrderNo: TEdit;
    Tv1Column2: TcxGridDBColumn;
    QtyOrder: TEdit;
    C_Name: TBtnEditA;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1PRTColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure CustNameBtnDnClick(Sender: TObject);
//    procedure C_CodeBtnUpClick(Sender: TObject);
    procedure v1OrderUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1PriceUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton3Click(Sender: TObject);
//    procedure Need_ProductionDblClick(Sender: TObject);
    procedure GC_BatchNoMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure ToolButton4Click(Sender: TObject);

    procedure C_NameDblClick(Sender: TObject);
    procedure C_NameBtnClick(Sender: TObject);
  private
    procedure InitData();
    procedure ZDYHelp(FButn: TcxButtonEdit; LType: string);
    function SaveData(): Boolean;
    { Private declarations }
  public
    PState, CopyInt, PriceFlag: Integer;
    FMainId, FFMainId, FOrderNo: string;
    FXS: Integer;
    canshu1, canshu2, FWorkshop: string;
    fFlileFlag: string;
    { Public declarations }
  end;

var
  frmTat_WB_InPut: TfrmTat_WB_InPut;
  newh: hwnd;

implementation

uses
  U_DataLink, U_ZDYHelp, U_RTFun, U_CompanySel, U_ClothInfoSel;

{$R *.dfm}

procedure TfrmTat_WB_InPut.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmTat_WB_InPut.InitData();
begin

  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Dye_Plan_Main where MainId=''' + Trim(FMainId) + '''');
    Open;
  end;
  SCSHData(ADOTemp, ScrollBox1, 2);


  with ADOTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select *  ');
    sql.Add('from Dye_Plan_Sub A where 1=1 ');
    sql.Add(' and MainId=''' + Trim(FMainId) + '''');
    Open;
  end;

  SCreateCDS(ADOTemp, CDS_1);
  SInitCDSData(ADOTemp, CDS_1);

  if CopyInt = 99 then
  begin
    PState := 0;
    FMainId := '';
    OrderNo.Text := '自动生成';
//    GC_Piece.Text := '';
//    GC_BatchNo.Text := '';
//    GC_Position.Text := '';
//    GC_STKID.Text := '';
//    OrdDate.DateTime := SGetServerDateTime(ADOTemp);
//    DlyDate.DateTime := OrdDate.DateTime;
    with CDS_1 do
    begin
      First;
      while not Eof do
      begin
        Edit;
        FieldByName('MainId').Value := '';
        FieldByName('SubId').Value := '';
        FieldByName('carNum').Value := 0;
        Post;
        Next;
      end;
    end;
  end;

end;

procedure TfrmTat_WB_InPut.ZDYHelp(FButn: TcxButtonEdit; LType: string);
var
  FType, ZDYName, FText: string;
begin
end;

procedure TfrmTat_WB_InPut.C_NameBtnClick(Sender: TObject);
begin

  frmClothInfoSel := TfrmClothInfoSel.create(self);
  with frmClothInfoSel do
  begin
    if showmodal = 1 then
    begin
      CDS_1.DisableControls;

      Self.C_Name.Text := trim(CDS_1.FieldByName('C_Name').AsString);

    end;
    CDS_1.EnableControls;
  end;
  frmClothInfoSel.free;
end;


procedure TfrmTat_WB_InPut.C_NameDblClick(Sender: TObject);
begin
  C_Name.Text := '';
end;

procedure TfrmTat_WB_InPut.FormShow(Sender: TObject);
var
  forderNo: string;
begin
  readCxGrid(Trim(Self.Caption), Tv1, '染整计划管理');
  OrdDate.DateTime := SGetServerDateTime(ADOTemp);
  DlyDate.DateTime := OrdDate.DateTime;
//  Workshop.text := trim(FWorkshop);
  fFlileFlag := UserDataFlag + 'HX';
  InitData();

end;

function TfrmTat_WB_InPut.SaveData(): Boolean;
var
  maxno, forderNo: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
///////////////       保存主表       ///////////////
    if Trim(FMainId) = '' then
    begin
      if not GetLSNo(ADOCmd, maxno, 'JM', 'Dye_Plan_Main', 3, 1) then
        raise Exception.Create('取最大号失败！');

      if GetLSNo(ADOCmd, forderNo, 'JH', 'Dye_Plan_Main', 3, 1) = False then
        raise Exception.Create('取计划单号失败！');
      OrderNo.Text := trim(forderNo);

    end
    else
    begin
      maxno := Trim(FMainId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from Dye_Plan_Main where MainId=''' + Trim(FMainId) + '''');
      Open;
    end;
    with ADOCmd do
    begin
      if ADOCmd.IsEmpty 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 := SGetServerDateTime(ADOTemp);
      end;

      RTSetsavedata(ADOCmd, 'Dye_Plan_Main', ScrollBox1, 2);
      FieldByName('MainId').Value := Trim(maxno);
      FieldByName('Workshop').Value := Trim(FWorkshop);
      FieldByName('status').Value := '9';
      Post;
    end;

    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from Dye_Plan_Main where orderno=''' + Trim(OrderNo.Text) + '''');
      Open;
    end;
    if ADOCmd.RecordCount > 1 then
      raise Exception.Create('计划单号重复！');

    FMainId := Trim(maxno);
///////////////       保存主表       ///////////////
///
///////////////       保存子表       ///////////////
    with CDS_1 do
    begin
      First;
      while not Eof do
      begin
        if Trim(CDS_1.fieldbyname('SubId').AsString) = '' then
        begin
          if not GetLSNo(ADOCmd, maxno, 'JS', 'Dye_Plan_Sub', 4, 1) then
            raise Exception.Create('计划单号重复！');
        end
        else
        begin
          maxno := Trim(CDS_1.fieldbyname('SubId').AsString);
        end;
        with ADOCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from Dye_Plan_Sub A   ');
          sql.Add(' where MainId=''' + Trim(FMainId) + '''');
          sql.Add(' and SubId=''' + Trim(maxno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if ADOCmd.IsEmpty then
          begin
            Append;
          end
          else
            Edit;
          RTSetSaveDataCDS(ADOCmd, Tv1, CDS_1, 'Dye_Plan_Sub', 0);
          FieldByName('MainId').Value := Trim(FMainId);
          FieldByName('SubId').Value := Trim(maxno);
          Post;
        end;
        CDS_1.Edit;
        CDS_1.FieldByName('SubId').Value := Trim(maxno);
        Next;
      end;
    end;
    with ADOTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add('UPDATE Dye_Plan_Main SET GC_STKID=   ');
      SQL.Add(' STUFF(( SELECT '','' +CAST(STKID AS VARCHAR)  FROM Dye_Plan_Sub c where c.MAINID = Dye_Plan_Main.MAINID FOR XML PATH('''')), 1, 1, '''')');
      sql.Add(' where MainId=''' + Trim(FMainId) + '''');
//      ShowMessage(sql.Text);
      EXECSQL;
    end;
///////////////       保存子表       ///////////////
    ADOCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOCmd.Connection.RollbackTrans;
    Result := False;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure TfrmTat_WB_InPut.TBSaveClick(Sender: TObject);
begin
//  OrdDate.SetFocus;
  try

//    if Trim(OrderNo.Text) = '' then
//      raise Exception.Create('计划单号不能为空！');

//    if Trim(OrdType.Text) = '' then
//      raise Exception.Create('订单类型不能为空！');


//    if Trim(C_Name.Text) = '' then
//      raise Exception.Create('产品名称不能为空！');

//    if CDS_1.IsEmpty then
//      raise Exception.Create('明细不能为空！');

  except
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    exit;
  end;


//
//  if PState = 1 then
//  begin
//    if Trim(OrderNo.Text) <> Trim(forderNo) then
//    begin
//      with ADOTemp do
//      begin
//        Close;
//        sql.Clear;
//        sql.Add('select * from YF_Money_CR where MainId=''' + Trim(FMainId) + '''');
//        Open;
//      end;
//      if ADOTemp.IsEmpty = False then
//      begin
//        Application.MessageBox('已产生数据，不能更改订单号!', '提示', 0);
//        Exit;
//      end;
//    end;
//  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！', '提示', 0);
    TBClose.Click();
  end;
end;

procedure TfrmTat_WB_InPut.v1PRTColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin

  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'C_Color';
      flagname := '颜色';
      V1Name.Caption := '中文名称';
//      V1Note.Caption := '英文名称';
//      fnote := True;
      if ShowModal = 1 then
      begin
        Self.CDS_1.Edit;
        Self.CDS_1.FieldByName('C_Color').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
//        Self.CDS_1.FieldByName('ORDNOTE').Value := Trim(ClientDataSet1.fieldbyname('ORDNOTE').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTat_WB_InPut.ToolButton1Click(Sender: TObject);
var
  i: Integer;
begin
//  if Trim(OrderNo.Text) = '' then
//  begin
//    Application.MessageBox('计划单号不能为空!', '提示', 0);
//    Exit;
//  end;
  i := CDS_1.RecordCount;
  i := i + 1;
  CDS_1.Last;
  CopyAddRow(Tv1, CDS_1);
  with CDS_1 do
  begin
    Edit;
    FieldByName('SerialNo').Value := IntToStr(i);
    FieldByName('OrdQty').Value := 0;
    FieldByName('AntcpPiece').Value := 0;
    FieldByName('AntcpQty').Value := 0;
    FieldByName('OrdUnit').Value := 'M';
    FieldByName('AntcpUnit').Value := 'M';
    Post;
  end;

end;

procedure TfrmTat_WB_InPut.ToolButton2Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from Dye_Plan_Card where SubId=''' + Trim(CDS_1.fieldbyname('SubId').AsString) + '''');
    Open;
  end;
  if ADOTemp.IsEmpty = False then
  begin
    Application.MessageBox('已开卡不能删除数据!', '提示', 0);
    Exit;
  end;
  if Trim(CDS_1.fieldbyname('SubId').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete Dye_Plan_Sub where SubId=''' + Trim(CDS_1.fieldbyname('SubId').AsString) + '''');
      ExecSQL;
    end;
  end;
  CDS_1.Delete;
end;

procedure TfrmTat_WB_InPut.CustNameBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

//procedure TfrmDyePlanInPut.C_CodeBtnUpClick(Sender: TObject);
//begin
//  if trim(CustName.Text) = '' then
//  begin
//    Application.MessageBox('客户不能为空!', '提示', 0);
//    Exit;
//  end;
//
//  frmDyeGreyClothStkSel := TfrmDyeGreyClothStkSel.create(self);
//  with frmDyeGreyClothStkSel do
//  begin
//    FromCoNo.Text := trim(self.CustName.TxtCode);
//    FromCoNo.Enabled := False;
//    if showmodal = 1 then
//    begin
//      CDS_Main.DisableControls;
//
//      Self.C_Code.Text := trim(CDS_Main.FieldByName('C_Code').AsString);
//      Self.C_Name.Text := trim(CDS_Main.FieldByName('C_Name').AsString);
////          Self.C_Spec.Text := trim(CDS_Main.FieldByName('C_spec').AsString);
//      Self.C_Width.Text := trim(CDS_Main.FieldByName('C_Width').AsString);
////          Self.C_GramWeight.Text := trim(CDS_Main.FieldByName('C_GramWeight').AsString);
//      Self.Saleser.Text := trim(CDS_Main.FieldByName('Saleser').AsString);
//
//      with CDS_1 do
//      begin
//        if CDS_1.IsEmpty then
//        begin
//          Append;
//        end
//        else
//        begin
//          Edit;
//        end;
//        FieldByName('stkid').Value := trim(CDS_Main.FieldByName('stkid').AsString);
//        FieldByName('OrdQty').Value := trim(CDS_Main.FieldByName('StkPiece').AsString);
//        FieldByName('OrdPiece').Value := trim(CDS_Main.FieldByName('STKQty').AsString);
//        FieldByName('OrdUnit').Value := 'M';
//      end;
//
//    end;
//    CDS_Main.EnableControls;
//  end;
//  frmDyeGreyClothStkSel.free;
//end;

procedure TfrmTat_WB_InPut.v1OrderUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
  mvalues: string;
begin
  mvalues := tv1.Columns[tv1.Controller.FocusedColumn.index].DataBinding.FieldName;
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'OrderUnit';
      flagname := '数量单位';
      if ShowModal = 1 then
      begin
        Self.CDS_1.Edit;
        Self.CDS_1.FieldByName(mvalues).Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTat_WB_InPut.v1PriceUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'PriceUnit';
      flagname := '价格单位';
      if Trim(DParameters1) <> '高权限' then
      begin
        TBAdd.Visible := False;
        TBEdit.Visible := False;
        TBDel.Visible := False;
      end;

      if ShowModal = 1 then
      begin
        Self.CDS_1.Edit;
        Self.CDS_1.FieldByName('PriceUnit').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTat_WB_InPut.ToolButton3Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  OneKeyPost(Tv1, CDS_1);
end;

procedure TfrmTat_WB_InPut.ToolButton4Click(Sender: TObject);
begin
  WriteCxGrid(Trim(Self.Caption), Tv1, '染整计划管理');
end;

//procedure TfrmTat_WB_InPut.Need_ProductionDblClick(Sender: TObject);
//var
//  fsj: string;
//  FWZ: Integer;
//  i: integer;
//begin
//  fsj := Trim(TMemo(Sender).Hint);
//  FWZ := Pos('/', fsj);
//  i := 0;
//  try
//    frmZDYHelpSel := TfrmZDYHelpSel.Create(Application);
//    with frmZDYHelpSel do
//    begin
//      flag := Copy(fsj, 1, FWZ - 1);
//      flagname := Copy(fsj, FWZ + 1, Length(fsj) - FWZ);
//      if ShowModal = 1 then
//      begin
//        TMemo(Sender).Lines.Clear;
//        with ClientDataSet1 do
//        begin
//          First;
//          while not Eof do
//          begin
//            if FieldByName('SSel').AsBoolean = True then
//            begin
//              i := i + 1;
//              TMemo(Sender).Lines.Add(inttostr(i) + '.' + FieldByName('ZDYName').AsString)
//            end;
//            Next;
//          end;
//        end;
//      end;
//    end;
//  finally
//    frmZDYHelpSel.Free;
//  end;
//end;

procedure TfrmTat_WB_InPut.GC_BatchNoMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  Application.Hint := TEdit(Sender).Name;
end;



end.

