unit U_DyePlanCardInput1;

interface

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

type
  TfrmDyePlanCardInput1 = class(TfrmBaseInput)
    CDS_1: TClientDataSet;
    DS_1: TDataSource;
    ADO_Car: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    GPM_1: TcxGridPopupMenu;
    Panel3: TPanel;
    Panel5: TPanel;
    Panel4: TPanel;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    V1SSel: TcxGridDBColumn;
    V1Column1: TcxGridDBColumn;
    V1Column5: TcxGridDBColumn;
    TV1Column1: TcxGridDBColumn;
    TV1Column5: TcxGridDBColumn;
    TV1Column6: TcxGridDBColumn;
    TV1Column7: TcxGridDBColumn;
    V1Column2: TcxGridDBColumn;
    V1Column3: TcxGridDBColumn;
    TV1Column4: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    V1Column6: TcxGridDBColumn;
    V1Column4: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolBar1: TToolBar;
    ToolButton4: TToolButton;
    ADOQueryMain: TADOQuery;
    DS_2: TDataSource;
    CDS_2: TClientDataSet;
    PM_2: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    PM_1: TPopupMenu;
    MenuItem1: TMenuItem;
    MenuItem2: TMenuItem;
    GPM_2: TcxGridPopupMenu;
    TV1Column10: TcxGridDBColumn;
    ToolButton5: TToolButton;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    ToolButton3: TToolButton;
    cxPageControl1: TcxPageControl;
    cxTabSheet3: TcxTabSheet;
    cxTabSheet4: TcxTabSheet;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Panel6: TPanel;
    Label20: TLabel;
    PCID: TEdit;
    GPM_3: TcxGridPopupMenu;
    CDS_3: TClientDataSet;
    DS_3: TDataSource;
    TV3Column1: TcxGridDBColumn;
    TV3Column2: TcxGridDBColumn;
    Button1: TButton;
    TV1Column12: TcxGridDBColumn;
    TV1Column13: TcxGridDBColumn;
    TV1Column14: TcxGridDBColumn;
    TV1Column15: TcxGridDBColumn;
    TV1Column16: TcxGridDBColumn;
    TV1Column17: TcxGridDBColumn;
    TV3Column3: TcxGridDBColumn;
    TV3Column4: TcxGridDBColumn;
    TV3Column5: TcxGridDBColumn;
    TV3Column6: TcxGridDBColumn;
    TV3Column7: TcxGridDBColumn;
    TV3Column8: TcxGridDBColumn;
    Label13: TLabel;
    Label14: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Re_Width: TEdit;
    Re_GramWeight: TEdit;
    Re_Color: TEdit;
    Re_ColorNo: TEdit;
    Label21: TLabel;
    REUnit: TComboBox;
    Label22: TLabel;
    RePCType: TComboBox;
    TV1Column18: TcxGridDBColumn;
    TV1Column19: TcxGridDBColumn;
    ADO_Order: TADOQuery;
    CDS_Color: TClientDataSet;
    pnlkpkk: TPanel;
    Label1: TLabel;
    Label7: TLabel;
    Label15: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    PCTime: TDateTimePicker;
    carnum: TcxCurrencyEdit;
    PCPiece: TcxCurrencyEdit;
    PCType: TComboBox;
    PCQty: TEdit;
    Button2: TButton;
    pnlOrder: TPanel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    v1Column14: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label23: TLabel;
    OrderNo: TBtnEditA;
    Label4: TLabel;
    Label9: TLabel;
    Label11: TLabel;
    Label10: TLabel;
    C_Width: TEdit;
    C_GramWeight: TEdit;
    C_COLORNO: TEdit;
    C_Color: TComboBox;
    Label24: TLabel;
    OrdQty: TEdit;
    OrdUnit: TEdit;
    TV3Column9: TcxGridDBColumn;
    Label2: TLabel;
    OrdPiece: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure MenuItem1Click(Sender: TObject);
    procedure MenuItem2Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure PCIDKeyPress(Sender: TObject; var Key: Char);
    procedure Button1Click(Sender: TObject);
    procedure TV3DblClick(Sender: TObject);
    procedure OrderNoBtnClick(Sender: TObject);
    procedure C_ColorChange(Sender: TObject);
    procedure TV3Column1PropertiesEditValueChanged(Sender: TObject);
  private
    FMainID: string;
    FRollNum: integer;
    FGC_BatchNo, FGC_Piece, FGC_STKID, FGC_Position: string;
    FFromCoNo, FFromCoName, FSaleser, FC_Name: string;
    procedure InitCard();
    procedure InitOrder();
    procedure InitGreyClothStk();
    function GetCard(): Boolean;
    procedure InitRepairCard(PPCID: string);
    { Private declarations }
  public
    FWorkshop, FPCIDS: string;



    { Public declarations }
  end;

var
  frmDyePlanCardInput1: TfrmDyePlanCardInput1;

implementation

uses
  U_DataLink, U_ZDYHelp, U_RTFun, U_DyePlanForCardSel, U_DyeProcessSel,
  U_DyeTechnicsSel, U_DyePlanCardSel, U_DyeGreyClothStkSel, U_DyeCardGlideEdit;
{$R *.dfm}

function TfrmDyePlanCardInput1.GetCard(): Boolean;
var
  MCustNo, MC_Name: string;
begin

  try
    CDS_3.DisableControls;
    if CDS_3.IsEmpty then
      raise Exception.Create('请选择流程卡！');
    CDS_3.First;
    while not CDS_3.eof do
    begin
      if Trim(MCustNo) = '' then
      begin
        MCustNo := trim(CDS_3.FieldByName('CustNo').AsString);
        MC_Name := trim(CDS_3.FieldByName('C_Name').AsString);
      end
      else
      begin
        if MCustNo <> trim(CDS_3.FieldByName('CustNo').AsString) then
          raise Exception.Create('不能选择多个客户！');
        if MC_Name <> trim(CDS_3.FieldByName('C_Name').AsString) then
          raise Exception.Create('不能选择多个产品！');
      end;
      if CDS_3.FieldByName('MvPiece').AsFloat > CDS_3.FieldByName('PSC_StkPiece').AsFloat then
        raise Exception.Create('调用匹数超限！');
      if CDS_3.FieldByName('MvQty').AsFloat > CDS_3.FieldByName('PSC_StkQty').AsFloat then
        raise Exception.Create('调用数量超限！');

      CDS_3.Next;
    end;
    CDS_3.EnableControls;

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

end;

procedure TfrmDyePlanCardInput1.InitOrder();
var
  fsj: string;
begin
  with Ado_Order do
  begin
    close;
    sql.Clear;
    sql.Add('select A.*,B.*');
    sql.Add('from Dye_Plan_sub A ');
    sql.Add('inner join Dye_Plan_Main B on B.MainID=A.mainID ');
    sql.Add('where A.MainID=''' + trim(FMainID) + ''' ');
    open;
  end;
  SCreateCDS(Ado_Order, CDS_Color);
  SInitCDSData(Ado_Order, CDS_Color);

  if not Ado_Order.IsEmpty then
  begin
    fsj := 'select distinct(C_Color) name from Dye_Plan_sub  where mainid= ' + QuotedStr(Trim(FMainID));
    SInitComBoxBySql(ADOQueryTmp, C_COLOR, True, fsj);
    SCSHData(Ado_Order, pnlOrder, 2);
    FC_Name := Trim(Ado_Order.FieldByName('C_Name').AsString);
    FFromCoNo := Trim(Ado_Order.FieldByName('CustNo').AsString);
    FFromCoName := Trim(Ado_Order.FieldByName('CustName').AsString);
    FGC_BatchNo := Trim(Ado_Order.FieldByName('GC_BatchNo').AsString);
    FGC_Piece := Trim(Ado_Order.FieldByName('GC_Piece').AsString);
    FGC_STKID := Trim(Ado_Order.FieldByName('GC_STKID').AsString);
    FGC_Position := Trim(Ado_Order.FieldByName('GC_Position').AsString);
//    FRollNum :=Trim(Ado_Order.FieldByName('').AsString);
  end;
end;

procedure TfrmDyePlanCardInput1.InitRepairCard(PPCID: string);
begin

  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select A.*');
    sql.Add(',GlideList=[dbo].[F_Dye_Card_GlideList](A.PCID) ');
    sql.Add(',MvPiece=PSC_StkPiece,MvQty=PSC_StkQty ');
    sql.Add('from Dye_Plan_Card A ');
    if TRIM(PPCID) = '' then
      sql.Add('where 1=2 ')
    else
      sql.Add('where PCID= ' + Quotedstr(trim(PPCID)));

    open;
  end;
  if TRIM(PPCID) = '' then
  begin
    SCreateCDS(ADOQueryTmp, CDS_3);
  end
  else
  begin
    SInitCDSData(ADOQueryTmp, CDS_3);
  end;

end;

procedure TfrmDyePlanCardInput1.InitGreyClothStk();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select A.* ');
      sql.Add(' from BS_Cloth_IO A where isnull(STKName,'''')=''染色坯布'' ');
      SQL.Add(' and A.IOFlag=''入库''   ');
      SQL.Add(' and EXISTS(select * from [dbo].[F_Tool_SplitString](' + quotedstr(FGC_STKID) + ','';'' ) X where X.RTValue=A.StkId)  ');
//      ShowMessage(sql.Text);
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_2);
    SInitCDSData(ADOQueryMain, CDS_2);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmDyePlanCardInput1.MenuItem1Click(Sender: TObject);
begin
  SelOKNo(CDS_1, True);

end;

procedure TfrmDyePlanCardInput1.MenuItem2Click(Sender: TObject);
begin
  SelOKNo(CDS_1, False);

end;

procedure TfrmDyePlanCardInput1.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_2, True);

end;

procedure TfrmDyePlanCardInput1.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_2, False);

end;

procedure TfrmDyePlanCardInput1.OrderNoBtnClick(Sender: TObject);
begin
  frmProductOrderListSel := TfrmProductOrderListSel.create(self);
  with frmProductOrderListSel do
  begin
    FWorkshop := self.FWorkshop;
    if ShowModal = 1 then
    begin
      fmainID := Order_Main.fieldbyname('mainID').asstring;
      InitOrder();
      InitGreyClothStk();
//      InitCard();
//      initGlide();
    end;
    free;
  end;

end;

procedure TfrmDyePlanCardInput1.PCIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    if Trim(PCID.Text) = '' then
      exit;
    InitRepairCard(PCID.Text);
    PCID.Text := '';
  end;

end;

procedure TfrmDyePlanCardInput1.InitCard();
begin

  with Ado_car do
  begin
    close;
    sql.Clear;
    sql.Add('select A.*');
    sql.Add(',GlideList=[dbo].[F_Dye_Card_GlideList](A.PCID) ');
    sql.Add('from Dye_Plan_Card A ');
    sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + Quotedstr(trim(FPCIDS)) + ', '';'' ) X where X.RTValue=A.PCID ) ');
    open;
  end;
  SCreateCDS(Ado_car, CDS_1);
  SInitCDSData(Ado_car, CDS_1);

end;

procedure TfrmDyePlanCardInput1.FormDestroy(Sender: TObject);
begin
  inherited;
  frmDyePlanCardInput1 := nil;
end;

procedure TfrmDyePlanCardInput1.Button1Click(Sender: TObject);
var
  maxId: string;
begin
  PCID.SetFocus;
  if not GetCard() then
    Exit;

  if GetLSNo(ADOQueryCmd, maxId, 'CZ', 'Dye_Plan_Card', 3, 1) = False then
  begin
    Application.MessageBox('取最大号失败!', '提示', 0);
    Exit;
  end;
  FPCIDS := FPCIDS + maxId + ';';
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_3.DisableConstraints;

    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('select * from Dye_Plan_Card A where 1=2');
      Open;
      Append;
      FieldByName('Filler').Value := trim(DName);
      FieldByName('Workshop').Value := trim(FWorkshop);
      FieldByName('PCType').Value := trim(RePCType.Text);
      FieldByName('PCID').Value := trim(maxId);
      FieldByName('PCTime').Value := formatdateTime('yyyy-MM-dd', now());
      FieldByName('GC_BatchNo').Value := '';
      FieldByName('GC_Piece').Value := '';
      FieldByName('GC_STKID').Value := '';
      FieldByName('GC_Position').Value := '';
      FieldByName('C_Name').Value := trim(CDS_3.FieldByName('C_Name').AsString);
      FieldByName('CustNo').Value := trim(CDS_3.FieldByName('CustNo').AsString);
      FieldByName('CustName').Value := trim(CDS_3.FieldByName('CustName').AsString);
      FieldByName('MainId').Value := trim(CDS_3.FieldByName('MainId').AsString);
      FieldByName('SubId').Value := trim(CDS_3.FieldByName('SubId').AsString);
      FieldByName('C_GramWeight').Value := trim(Re_GramWeight.Text);
      FieldByName('C_Width').Value := trim(Re_Width.Text);
      FieldByName('C_Color').Value := trim(Re_Color.Text);
      FieldByName('PCUnit').Value := trim(ReUnit.Text);
      FieldByName('C_ColorNo').Value := trim(Re_ColorNo.Text);
      FieldByName('PCPiece').Value := 0;
      FieldByName('PCQty').Value := 0;
      post;
    end;

    with CDS_3 do
    begin
      first;
      while not Eof do
      begin
        with ADOQueryCmd do
        begin
          Close;
          Sql.Clear;
          Sql.Add('select * from Dye_Plan_Card_Move A where 1=2');
          Open;
          Append;
          FieldByName('Filler').Value := trim(DName);
          FieldByName('FromID').Value := trim(CDS_3.FieldByName('PCID').AsString);
          FieldByName('ToID').Value := maxId;
          FieldByName('MvPiece').Value := CDS_3.FieldByName('MvPiece').AsFloat;
          FieldByName('MvQty').Value := CDS_3.FieldByName('MvQty').AsFloat;
          post;
        end;
        Next;
      end;
    end;

    with ADOQueryCmd do
    begin
      Close;
      Sql.Clear;
      Sql.Add('exec P_Dye_Card_Move ');
      Sql.Add('@PCID=' + quotedstr(trim(maxId)));
      Open;
    end;
    if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
    begin
      raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_3.EnableConstraints;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    CDS_3.EnableConstraints;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

  InitCard();
  InitRepairCard('');
end;

procedure TfrmDyePlanCardInput1.Button2Click(Sender: TObject);
var
  i, k, fhxInt: integer;
  maxId: string;
begin
  if trim(C_Color.Text) = '' then
  begin
    application.MessageBox('颜色不能为空！', '提示信息', MB_ICONERROR);
    exit;
  end;
  if trim(C_Width.Text) = '' then
  begin
    application.MessageBox('门幅不能为空！', '提示信息', MB_ICONERROR);
    exit;
  end;
  if trim(C_GramWeight.Text) = '' then
  begin
    application.MessageBox('克重不能为空！', '提示信息', MB_ICONERROR);
    exit;
  end;
  if (StrToFloatDef(PCPiece.Text, 0) + StrToFloatDef(PCQty.Text, 0)) = 0 then
  begin
    application.MessageBox('开卡匹数、数量不能都为零！', '提示信息', MB_ICONERROR);
    exit;
  end;

  if strtointdef(trim(carNum.text), 0) <= 1 then
    k := 1
  else
    k := strtointdef(trim(carNum.text), 0);
  CDS_Color.Locate('C_Color', C_Color.Text, []);
  CDS_1.Last;
  for i := 1 to k do
  begin
    if GetLSNo(ADOQueryCmd, maxId, 'CZ', 'Dye_Plan_Card', 3, 1) = False then
    begin
      Application.MessageBox('取最大号失败!', '提示', 0);
      Exit;
    end;
    FPCIDS := FPCIDS + maxId + ';';
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        Sql.Clear;
        Sql.Add('select * from Dye_Plan_Card A where 1=2');
        Open;
        Append;
        FieldByName('OrderNo').Value := OrderNo.Text;
        FieldByName('SubId').Value := CDS_Color.FieldByName('SubId').Value;
        FieldByName('Mainid').Value := FMainid;
        FieldByName('Filler').Value := trim(DName);
        FieldByName('Workshop').Value := trim(FWorkshop);
        FieldByName('PCType').Value := trim(PCType.Text);
        FieldByName('PCID').Value := trim(maxId);
        FieldByName('PCTime').Value := formatdateTime('yyyy-MM-dd', PCTime.DateTime);
        FieldByName('GC_BatchNo').Value := trim(FGC_BatchNo);
        FieldByName('GC_Piece').Value := trim(FGC_Piece);
        FieldByName('GC_STKID').Value := trim(FGC_STKID);
        FieldByName('GC_Position').Value := trim(FGC_Position);
        FieldByName('CustNo').Value := trim(FFromCoNo);
        FieldByName('CustName').Value := trim(FFromCoName);
        FieldByName('Saleser').Value := trim(FSaleser);
        FieldByName('C_Name').Value := trim(FC_Name);
        FieldByName('C_GramWeight').Value := trim(C_GramWeight.Text);
        FieldByName('C_Width').Value := trim(C_Width.Text);
        FieldByName('C_Color').Value := trim(C_Color.Text);
        FieldByName('PCUnit').Value := trim(OrdUnit.Text);
        FieldByName('C_ColorNo').Value := trim(C_ColorNo.Text);
        FieldByName('PCPiece').Value := StrtoIntDef(PCPiece.Text, 0);
        FieldByName('PCQty').Value := StrtoFloatDef(PCQty.Text, 0);
        post;
      end;
      with ADOQueryCmd do
      begin
        Close;
        Sql.Clear;
        Sql.Add('exec P_Dye_GreyCloth_Out ');
        Sql.Add('@PCID=' + quotedstr(trim(maxId)));
        Sql.Add(',@Filler=' + quotedstr(trim(DName)));
        Open;
      end;
      if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
      begin
        raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
      end;
      ADOQueryCmd.Connection.CommitTrans;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    end;
  end;
  InitCard();
  InitGreyClothStk();
end;

procedure TfrmDyePlanCardInput1.C_ColorChange(Sender: TObject);
begin
  CDS_Color.Locate('C_Color', C_Color.Text, []);
  C_COLOR.Text := CDS_Color.FieldByName('C_COLOR').AsString;
  OrdQty.Text := CDS_Color.FieldByName('OrdQty').AsString;
  OrdUnit.Text := CDS_Color.FieldByName('OrdUnit').AsString;
  C_COLORNO.Text := CDS_Color.FieldByName('C_COLORNO').AsString;
end;

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

procedure TfrmDyePlanCardInput1.FormCreate(Sender: TObject);
begin
  inherited;
  PCTime.Date := DServerDate;
end;

procedure TfrmDyePlanCardInput1.FormShow(Sender: TObject);
begin
  inherited;
  ReadCxGrid(self.Caption + 'TV1', Tv1, '生产指示单管理');
  ReadCxGrid(self.Caption + 'TV2', Tv2, '生产指示单管理');
  InitCard();
  InitGreyClothStk();
  InitRepairCard('');
end;

procedure TfrmDyePlanCardInput1.ToolButton1Click(Sender: TObject);
var
  MPCIDS: string;
begin
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  with CDS_1 do
  begin
    DisableControls;
    First;
    while not Eof do
    begin
      if Fieldbyname('Ssel').AsBoolean then
      begin
        MPCIDS := MPCIDS + trim(CDS_1.FieldByName('PCID').AsString) + ';';
      end;
      Next;
    end;
    EnableControls;
  end;

  with ADOQueryTmp do
  begin
    close;
    sql.Clear;
    sql.Add('select distinct GX=[dbo].[F_Dye_Card_GlideList](A.RTValue) from [dbo].[F_Tool_SplitString](' + quotedstr(MPCIDS) + ','';'')  A ');
    open;
  end;

  if ADOQueryTmp.RecordCount > 1 then
  begin
    Application.MessageBox('请选择相同工序的卡编辑!', '提示', 0);
    Exit;
  end;

  try
    frmDyeCardGlideEdit := TfrmDyeCardGlideEdit.Create(Application);
    with frmDyeCardGlideEdit do
    begin
      FPCIDS := MPCIDS;
      FWorkshop := self.FWorkshop;
      if ShowModal = 1 then
      begin
        InitCard();
      end;
    end;
  finally
    frmDyeCardGlideEdit.Free;
  end;

end;

procedure TfrmDyePlanCardInput1.ToolButton2Click(Sender: TObject);
begin
  WriteCxGrid(self.Caption + 'TV1', Tv1, '生产指示单管理');
  WriteCxGrid(self.Caption + 'TV2', Tv2, '生产指示单管理');

end;

procedure TfrmDyePlanCardInput1.ToolButton3Click(Sender: TObject);
begin
  close;
end;

procedure TfrmDyePlanCardInput1.ToolButton4Click(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    exit;
  if CDS_1.Locate('SSel', True, []) = False then
  begin
    Application.MessageBox('没有选择数据!', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;

  try
    with CDS_1 do
    begin
      DisableControls;
      First;
      while not Eof do
      begin
        if Fieldbyname('Ssel').AsBoolean then
        begin
          try
            ADOQueryCmd.Connection.BeginTrans;

            with ADOQueryCmd do
            begin
              Close;
              Sql.Clear;
              Sql.Add('exec P_Dye_Card_Del ');
              Sql.Add('@PCIDS=' + quotedstr(Trim(CDS_1.fieldbyname('PCID').AsString)));
              Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
              Sql.Add(',@DName=' + quotedstr(Trim(DName)));
              Open;
            end;
            if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
              raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));

            if (Trim(CDS_1.fieldbyname('PCType').AsString) = '正常卡') or (Trim(CDS_1.fieldbyname('PCType').AsString) = '前道卡') then
            begin
              with ADOQueryCmd do
              begin
                Close;
                Sql.Clear;
                Sql.Add('exec P_Dye_GreyCloth_Out_Del ');
                Sql.Add('@PCID=' + quotedstr(Trim(CDS_1.fieldbyname('PCID').AsString)));
                Open;
              end;
              if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
                raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
            end
            else
            begin
              with ADOQueryCmd do
              begin
                Close;
                Sql.Clear;
                Sql.Add('exec P_Dye_Card_Move_Del ');
                Sql.Add('@PCID=' + quotedstr(Trim(CDS_1.fieldbyname('PCID').AsString)));
                Open;
              end;
              if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
                raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
            end;

            ADOQueryCmd.Connection.CommitTrans;
          except
            ADOQueryCmd.Connection.RollbackTrans;
            raise Exception.Create(PChar(Exception(ExceptObject).Message));
          end;
        end;
        Next;
        EnableControls;
      end;
      InitCard();
      InitGreyClothStk();
    end;
  except
    CDS_1.EnableControls;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure TfrmDyePlanCardInput1.ToolButton5Click(Sender: TObject);
begin

  try
    CDS_1.DisableControls;
    CDS_1.First;
    while not CDS_1.Eof do
    begin
      if CDS_1.Fieldbyname('Ssel').AsBoolean then
      begin
        ADOQueryCmd.Connection.BeginTrans;
        try
          with ADOQueryCmd do
          begin
            close;
            sql.Clear;
            sql.Add('select * from Dye_Plan_Card A');
            sql.Add('where PCID=''' + trim(CDS_1.fieldbyname('PCID').AsString) + '''');
            open;
            edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDateTime(ADOQueryTmp);
            RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_1, 'Dye_Plan_Card', 0);
            post;
          end;

          if (Trim(CDS_1.fieldbyname('PCType').AsString) = '正常卡') or (Trim(CDS_1.fieldbyname('PCType').AsString) = '前道卡') then
          begin
            with ADOQueryCmd do
            begin
              Close;
              Sql.Clear;
              Sql.Add('exec P_Dye_GreyCloth_Out_Del ');
              Sql.Add('@PCID=' + quotedstr(Trim(CDS_1.fieldbyname('PCID').AsString)));
              Open;
            end;
            if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
              raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
            with ADOQueryCmd do
            begin
              Close;
              Sql.Clear;
              Sql.Add('exec P_Dye_GreyCloth_Out ');
              Sql.Add('@PCID=' + quotedstr(Trim(CDS_1.fieldbyname('PCID').AsString)));
              Sql.Add(',@Filler=' + quotedstr(trim(DName)));
              Open;
            end;
            if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
            begin
              raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
            end;
          end
          else
          begin
            with ADOQueryCmd do
            begin
              Close;
              Sql.Clear;
              Sql.Add('exec P_Dye_Card_Move ');
              Sql.Add('@PCID=' + quotedstr(Trim(CDS_1.fieldbyname('PCID').AsString)));
              Open;
            end;
            if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
            begin
              raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
            end;
          end;

          ADOQueryCmd.Connection.CommitTrans;
        except
          ADOQueryCmd.Connection.RollbackTrans;
          raise Exception.Create(PChar(Exception(ExceptObject).Message));
        end;
      end;
      CDS_1.Next;
    end;
    CDS_1.EnableControls;
    InitCard();
    InitGreyClothStk();
  except
    CDS_1.EnableControls;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure TfrmDyePlanCardInput1.TV3Column1PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin

  mvalue := TcxTextEdit(Sender).EditingText;
  with CDS_3 do
  begin
    Edit;
    FieldByName('MvPiece').Value := STRTOFLOATDEF(mvalue, 0);
    if FieldByName('PSC_StkPiece').AsFloat > 0 then
      FieldByName('MvQty').Value := STRTOFLOATDEF(mvalue, 0) * (FieldByName('PSC_StkQty').AsFloat / FieldByName('PSC_StkPiece').AsFloat)
    else
      FieldByName('MvQty').Value := 0;
    Post;

  end;
  Tv3.Controller.EditingController.ShowEdit();
end;

procedure TfrmDyePlanCardInput1.TV3DblClick(Sender: TObject);
begin
  CDS_3.Delete;

end;

end.

