unit U_TatCardInPut1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls, ToolWin, cxStyles, cxCustomData,
  cxGraphics, cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ADODB,
  cxGridCustomPopupMenu, cxGridPopupMenu, cxGridLevel, cxClasses, cxControls,
  cxGridCustomView, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxGrid, DBClient, cxCalendar, cxButtonEdit, cxSplitter, RM_Common, RM_Class,
  RM_e_Xls, RM_Dataset, RM_System, RM_GridReport, cxTextEdit, cxCheckBox,
  BtnEdit, cxPC, MovePanel, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxBarBuiltInMenu, dxDateRanges, U_BaseList, Vcl.Menus, U_BaseInput,
  System.ImageList, Vcl.ImgList, dxScrollbarAnnotations, cxContainer, dxCore,
  cxDateUtils, cxMaskEdit, cxDropDownEdit, dxSkinsCore, dxSkinsDefaultPainters,
  cxImageList, dxSkinWXI, dxCoreGraphics;

type
  TfrmTatCardInPut1 = class(TfrmBaseInput)
    GPM_1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADO_1: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    DS_2: TDataSource;
    CDS_2: TClientDataSet;
    GPM_2: TcxGridPopupMenu;
    PM_1: TPopupMenu;
    PM_2: TPopupMenu;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Pnl_WBC: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    ConNo: TcxTextEdit;
    MCNO: TcxTextEdit;
    C_Name: TcxTextEdit;
    C_Code: TcxTextEdit;
    OrderNo: TcxTextEdit;
    C_FinishLatitudeBatchNo: TcxTextEdit;
    Panel2: TPanel;
    Label10: TLabel;
    Label11: TLabel;
    Label7: TLabel;
    Label4: TLabel;
    Button3: TButton;
    edtCardQty: TcxTextEdit;
    BatchNo: TcxTextEdit;
    isDY: TComboBox;
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    ToolButton12: TToolButton;
    TBClose: TToolButton;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    v3Column8: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    v3Column6: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    v3Column16: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxTabControl1: TcxTabControl;
    Tv1Column13: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    Label3: TLabel;
    YarnBatchNo: TcxTextEdit;
    Label9: TLabel;
    Label15: TLabel;
    BegDate: TcxDateEdit;
    EndDate: TcxDateEdit;
    Repairer: TcxButtonEdit;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Label8: TLabel;
    Note: TcxTextEdit;
    TV2Column3: TcxGridDBColumn;
    ToolButton2: TToolButton;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    Tv1WB_YarnLen: TcxGridDBColumn;
    Tv1WBCID: TcxGridDBColumn;
    Tv1WBNo: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure ToolButton12Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CustNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure RepairerDblClick(Sender: TObject);
    procedure Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton2Click(Sender: TObject);
    procedure ConNoPropertiesChange(Sender: TObject);
  private
    FPCIDS: string;
    procedure InitGrid1();
    procedure InitGrid2();
    { Private declarations }
  public
//    FMainId, FSubId, FWBCID: string;
    { Public declarations }
  end;

var
  frmTatCardInPut1: TfrmTatCardInPut1;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_LabelPrint, U_TatPlanSel;
  {$R *.dfm}

procedure TfrmTatCardInPut1.InitGrid1();
begin
  with ADO_1 do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.*,B.*,E.WBNo,E.Qty,CONVERT(VARCHAR(10),A.FillTime,120) AS APDate ');
    sql.Add(' ,YAPPS=ISNULL((select count(*) from Tat_Plan_Card X where X.PMID=A.PMID   ),0)');      //任务安排匹数
    sql.Add(' ,YAPPS2=ISNULL((select count(*) from Tat_Plan_Card X where X.MainID=A.MainID   ),0)'); //织造单安排匹数
    sql.Add(' from Tat_Plan_Mach A');
    sql.Add(' inner join Tat_plan_Sub B on B.Subid=A.Subid ');
    sql.Add(' inner join Tat_plan_Main D on B.MainID=D.MainID ');
    sql.Add(' left join Tat_WB_Card C on C.WBCID=A.WBCID ');
    sql.Add(' left join Tat_Plan_OutPut E on C.WBCID=E.PCID ');
    sql.Add(' where E.GlideName in(''上轴'',''接头'')');

    case cxTabControl1.TabIndex of
      0:
        begin
          sql.Add('  and not EXISTS (SELECT * from Tat_Plan_Card X where X.PMID =A.PMID) ');
          sql.Add(' and isnull(D.Status,''0'') in (''0'',''9'') ');
        end;
      1:
        begin
          sql.Add('  and  EXISTS (SELECT * from Tat_Plan_Card X where X.PMID =A.PMID) ');
          sql.Add(' and isnull(D.Status,''0'') in (''0'',''9'') ');
          sql.Add(' and CONVERT(VARCHAR(10),A.FillTime,120)  >=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
          sql.Add(' and CONVERT(VARCHAR(10),A.FillTime,120)  <''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
        end;
    end;
//    ShowMessage(sql.Text);
    Open;
  end;

  SCreateCDS(ADO_1, CDS_1);
  SInitCDSData(ADO_1, CDS_1);

end;

procedure TfrmTatCardInPut1.InitGrid2();
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select * from Tat_Plan_Card A  ');
    sql.Add(' inner join Tat_plan_Sub B on B.Subid=A.Subid ');
    sql.Add(' inner join Tat_Plan_Main C on A.MainId=C.MainId');
    sql.Add(' where PMID=  ' + QuotedStr(Trim(CDS_1.fieldbyname('PMID').AsString)));

//    sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(FPCIDS) + ','','') X where A.PCID=X.RTValue )  ');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_2);
  SInitCDSData(ADOQueryTemp, CDS_2);
end;

procedure TfrmTatCardInPut1.FormDestroy(Sender: TObject);
begin
  inherited;
  frmTatCardInPut1 := nil;
end;

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

procedure TfrmTatCardInPut1.TBCloseClick(Sender: TObject);
begin
  Close;

end;

procedure TfrmTatCardInPut1.FormShow(Sender: TObject);
begin
  inherited;

  ReadCxGrid(trim(self.Caption) + 'Tv11', Tv1, '指示单管理');
  ReadCxGrid(trim(self.Caption) + 'Tv22', Tv2, '指示单管理');
  EndDate.Date := SGetServerDate(ADOQueryTemp);
  BegDate.Date := EndDate.Date - 1;
  InitGrid1();
  InitGrid2();
end;

procedure TfrmTatCardInPut1.RepairerDblClick(Sender: TObject);
begin
  Repairer.Text := '';
end;

procedure TfrmTatCardInPut1.ConNoPropertiesChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmTatCardInPut1.CustNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'Repairer';
      flagname := '机修工';
      if ShowModal = 1 then
      begin
        Repairer.Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTatCardInPut1.cxTabControl1Change(Sender: TObject);
begin
  InitGrid1();
end;

procedure TfrmTatCardInPut1.Button3Click(Sender: TObject);
var
  MaxNo, MaxPCId, MPrtPCIDS: string;
  i, j, MPCNo: integer;
begin

  if StrToIntdef(edtCardQty.Text, 0) = 0 then
    Exit;
  MPrtPCIDS := '';
  try

    CDS_1.DisableControls;

    with CDS_1 do
    begin
      First;
      i := 0;
      while not Eof do
      begin
        if CDS_1.fieldbyname('SSel').AsBoolean then
        begin
          try
            ADOQueryCmd.Connection.BeginTrans;

            with ADOQueryTemp do
            begin
              Close;
              sql.Clear;
              sql.Add('select PCNo=isnull(Max(PCNo),0),APPS=COUNT(PCNo),OrdPiece=MAX(OrdPiece)  ');
              sql.Add('from Tat_Plan_Card A right join Tat_Plan_Sub B on A.SubId=B.SubId');
              sql.Add('where  B.MainID=' + Quotedstr(Trim(CDS_1.FieldByName('MainID').AsString)));
//              ShowMessage(sql.Text);
              Open;
            end;
//            if ADOQueryTemp.FieldByName('APPS').AsInteger + StrToIntdef(edtCardQty.Text, 0) > ADOQueryTemp.FieldByName('OrdPiece').AsInteger then
//              raise Exception.Create('计划单安排匹数超出上限!');

            if ADOQueryTemp.IsEmpty then
              MPCNo := 1
            else
              MPCNo := ADOQueryTemp.FieldByName('PCNo').AsInteger + 1;
//////////////////            插入布票               //////////////////////
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add(' insert into Tat_Plan_Card(MainId,SubId,WBCID,PMID,Loom,PCId,PCNo,PCDate,Filler,BatchNo,Repairer,YarnBatchNo,PieceLen,Note) values');
              for j := 1 to StrToIntdef(edtCardQty.Text, 0) do
              begin
                if GetLSNo(ADOQueryTemp, MaxPCId, 'TPC', 'Tat_Plan_Card', 3, 1) = False then
                begin
                  raise Exception.Create('取布票流水号失败!');
                end;
                if j > 1 then
                  sql.Add(',');
                sql.Add(' (' + Quotedstr(Trim(CDS_1.FieldByName('MainID').AsString)));
                sql.Add(',' + Quotedstr(Trim(CDS_1.FieldByName('SubId').AsString)));
                sql.Add(',' + Quotedstr(Trim(CDS_1.FieldByName('WBCID').AsString)));
                sql.Add(',' + Quotedstr(Trim(CDS_1.FieldByName('PMID').AsString)));
                sql.Add(',' + Quotedstr(Trim(CDS_1.FieldByName('MCNO').AsString)));
                sql.Add(',' + Quotedstr(MaxPCId));
                sql.Add(',' + Quotedstr(IntToStr(MPCNo)));
                sql.Add(',getdate() ');
                sql.Add(',' + Quotedstr(Trim(DName)));
                sql.Add(',' + Quotedstr(Trim(BatchNo.Text)));
                sql.Add(',' + Quotedstr(Trim(Repairer.Text)));
                sql.Add(',' + Quotedstr(Trim(YarnBatchNo.Text)));
                sql.Add(',' + Quotedstr(Trim(CDS_1.FieldByName('OrdPieceLen').AsString)));
                sql.Add(',' + Quotedstr(Trim(Note.Text)));
                sql.Add(' )');
                MPCNo := MPCNo + 1;

                MPrtPCIDS := MPrtPCIDS + Trim(MaxPCId) + ',';

              end;
//             ShowMessage(sql.text);
              ExecSQL;
            end;
            ADOQueryCmd.Connection.CommitTrans;
          except
            ADOQueryCmd.Connection.RollbackTrans;
            CDS_1.EnableControls;
            raise Exception.Create(PChar(Exception(ExceptObject).Message));
          end;
//////////////////            插入布票               //////////////////////
        end;
        Next;
        i := i + 1;
      end;
    end;
    CDS_1.Locate('SSel', True, []);
    CDS_1.EnableControls;
    FPCIDS := FPCIDS + MPrtPCIDS;

    if trim(isDY.text) = '是' then
    begin
      try
        frmLabelPrint := TfrmLabelPrint.Create(Application);
        with frmLabelPrint do
        begin
          FPreviewPrint := False;
          FLMType := 'TatPlanCard';
          FFiltration1 := MPrtPCIDS;
          if ShowModal = 1 then
          begin
            with ADOQueryCmd do
            begin
              Close;
              sql.Clear;
              sql.Add('update Tat_Plan_Card         ');
              sql.Add('set PRTer=' + quotedstr(DName));
              sql.Add(', PRTCount=PRTCount+1');
              sql.Add(', PRTDate=getdate()');
              sql.Add('where EXISTS(select X.RTValue from [dbo].[F_Tool_SplitString](' + QuotedStr(MPrtPCIDS) + ','','') X where Tat_Plan_Card.PCID=X.RTValue )  ');
              ExecSQL;
            end;
          end;
        end;
      finally
        frmLabelPrint.Free;
      end;
    end;
    InitGrid1();
    InitGrid2();
  except

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

end;

procedure TfrmTatCardInPut1.ToolButton12Click(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + 'Tv11', Tv1, '指示单管理');
  WriteCxGrid(trim(self.Caption) + 'Tv22', Tv2, '指示单管理');
end;

procedure TfrmTatCardInPut1.ToolButton1Click(Sender: TObject);
begin
  ToolBar1.SetFocus;
  InitGrid1();
end;

procedure TfrmTatCardInPut1.ToolButton2Click(Sender: TObject);
begin
  if ADO_1.Active = False then
    Exit;
  SDofilter(ADO_1, SGetFilters(Pnl_WBC, 1, 2));
  SCreateCDS(ADO_1, CDS_1);
  SInitCDSData(ADO_1, CDS_1);
end;

procedure TfrmTatCardInPut1.Tv1FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitGrid2();
end;

end.

