unit U_TatCardInPut;

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, dxSkinsCore, dxSkinsDefaultPainters, dxDateRanges,
  U_BaseList, Vcl.Menus, U_BaseInput, System.ImageList, Vcl.ImgList,
  dxScrollbarAnnotations, cxImageList;

type
  TfrmTatCardInPut = class(TfrmBaseInput)
    GPM_1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    DS_2: TDataSource;
    CDS_2: TClientDataSet;
    GPM_2: TcxGridPopupMenu;
    Panel4: TPanel;
    PM_1: TPopupMenu;
    PM_2: TPopupMenu;
    Panel5: TPanel;
    Panel2: TPanel;
    Label10: TLabel;
    Label11: TLabel;
    Button3: TButton;
    edtMachNo: TEdit;
    edtCardQty: TEdit;
    Repairer: TBtnEditC;
    Panel1: TPanel;
    N2: TMenuItem;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    TV2Column4: TcxGridDBColumn;
    TV2Column5: TcxGridDBColumn;
    TV2Column6: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label3: TLabel;
    MCNO: TEdit;
    ToolBar1: TToolBar;
    ToolButton12: TToolButton;
    TBClose: TToolButton;
    Label7: TLabel;
    BatchNo: TEdit;
    TV2Column7: TcxGridDBColumn;
    Panel3: TPanel;
    Label1: TLabel;
    Label12: TLabel;
    Label13: TLabel;
    Label21: TLabel;
    edtName: TEdit;
    edtCode: TEdit;
    edtConNo: TEdit;
    edtWAPPS: TEdit;
    Label8: TLabel;
    Label9: TLabel;
    edtYAPPS: TEdit;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Label4: TLabel;
    isDY: TComboBox;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Button1: TButton;
    ToolButton1: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure MCNOChange(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure ToolButton12Click(Sender: TObject);
    procedure Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure RepairerBtnDnClick(Sender: TObject);
    procedure RepairerBtnUpClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    procedure InitOrd();
    procedure InitGrid1();
    procedure InitGrid2();
    { Private declarations }
  public
    FMainId, FSubId, FWBCID: string;
    { Public declarations }
  end;

var
  frmTatCardInPut: TfrmTatCardInPut;

implementation

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

procedure TfrmTatCardInPut.InitOrd();
begin

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');  //,B.*
//    sql.Add(' ,YAPPS=ISNULL((select count(*) from Tat_Plan_Card X where X.MainId=A.MainId and isnull(Status,''0'') <>''-1'' ),0)');
//    sql.Add(' ,WAPPS=OrdPiece-ISNULL((select count(*) from Tat_Plan_Card X where X.MainId=A.MainId and isnull(Status,''0'') <>''-1''),0)');
    sql.Add(' from Tat_WB_Card A');
//    sql.Add(' inner join Tat_Plan_Sub B on A.MainId=B.MainId ');
    sql.Add(' where A.WBCID=' + QuotedStr(FWBCID));
    Open;
  end;
  edtConNo.Text := ADOQueryTemp.FieldByName('WBCID').AsString;
  edtCode.Text := ADOQueryTemp.FieldByName('WB_Clothcode').AsString;
  edtName.Text := ADOQueryTemp.FieldByName('WB_ClothName').AsString;
//  edtYAPPS.Text := ADOQueryTemp.FieldByName('YAPPS').AsString;
//  edtWAPPS.Text := ADOQueryTemp.FieldByName('WAPPS').AsString;
end;

procedure TfrmTatCardInPut.RepairerBtnDnClick(Sender: TObject);
begin
  Repairer.Text := '';
end;

procedure TfrmTatCardInPut.RepairerBtnUpClick(Sender: TObject);
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 TfrmTatCardInPut.InitGrid1();
var
  fwhere: string;
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add(' select * ');
      sql.Add(' ,ZWJYPS=(select count(X.Loom) from Tat_Plan_Card X where X.Loom =AA.MCNO and isnull(Status,''0'')=''0'') ');
      sql.Add(' ,BWJYPS=(select count(X.Loom) from Tat_Plan_Card X where X.Loom =AA.MCNO and isnull(Status,''0'')=''0'' and MainID=''' + trim(FMainID) + ''')');
      sql.Add(' from (select A.MCNO,ISAP=''0'' from BS_Machine A where EXISTS (select Loom from Tat_Plan_Card X where X.Loom = A.MCNO and X.WBCID=''' + trim(FWBCID) + ''')');
      sql.Add(' UNION   ');
      sql.Add(' select A.MCNO,ISAP=''1'' from BS_Machine A where not EXISTS (select Loom from Tat_Plan_Card X where X.Loom = A.MCNO and X.WBCID=''' + trim(FWBCID) + ''')');
      sql.Add(' ) AA Order BY ISAP,MCNO');
//      ShowMessage(sql.text);
      Open;
    end;
    SCreateCDS(ADOQueryMain, CDS_1);
    SInitCDSData(ADOQueryMain, CDS_1);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmTatCardInPut.InitGrid2();
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select COUNT(*) AS APPS,Loom,isnull(Repairer,'''') AS Repairer,isnull(BatchNo,'''') AS BatchNo ');
    sql.Add(',PCDate=(select top 1 CONVERT(VARCHAR(10),X.PCDate,120) from Tat_Plan_Card X where X.Loom=A.Loom   ');
    sql.Add(' and isnull(X.Repairer,'''') = isnull(A.Repairer,'''') and isnull(X.BatchNo,'''') = isnull(A.BatchNo,'''') and isnull(Status,''0'') <>''-1'' ORDER BY PCDate ) ');
    sql.Add(' ,DYPS=(select count(*) from Tat_Plan_Card X where X.Loom=A.Loom and PrtCount > 0 and isnull(Status,''0'') <>''-1''  ');
    sql.Add(' and isnull(X.Repairer,'''') = isnull(A.Repairer,'''') and isnull(X.BatchNo,'''') = isnull(A.BatchNo,'''') )');
    sql.Add(' ,JYPS=(select count(*) from Tat_Plan_Card X where X.Loom=A.Loom and isnull(X.Status,''0'') = ''1''    ');
    sql.Add(' and isnull(X.Repairer,'''') = isnull(A.Repairer,'''') and isnull(X.BatchNo,'''') = isnull(A.BatchNo,'''') )');
    sql.Add(' from Tat_Plan_Card A where WBCID =''' + trim(FWBCID) + '''');
    sql.Add(' and Deler is null');
    sql.Add(' GROUP BY Loom,isnull(Repairer,''''),isnull(BatchNo,'''') ');
//    ShowMessage(sql.text);
    Open;
  end;

  SCreateCDS(ADOQueryTemp, CDS_2);
  SInitCDSData(ADOQueryTemp, CDS_2);
end;

procedure TfrmTatCardInPut.FormDestroy(Sender: TObject);
begin
  inherited;
  frmTatCardInPut := nil;
end;

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

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

end;

procedure TfrmTatCardInPut.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
end;

procedure TfrmTatCardInPut.TBRafreshClick(Sender: TObject);
begin
  InitGrid1();
  InitGrid2();

end;

procedure TfrmTatCardInPut.MCNOChange(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS(ADOQueryMain, CDS_1);
  SInitCDSData(ADOQueryMain, CDS_1);
end;

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

  ReadCxGrid(trim(self.Caption) + 'Tv1', Tv1, '指示单管理');
  ReadCxGrid(trim(self.Caption) + 'Tv2', Tv2, '指示单管理');

  InitOrd();
  InitGrid1();
  InitGrid2();
end;

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

procedure TfrmTatCardInPut.Button1Click(Sender: TObject);
begin
  edtMachNo.text := '';
end;

procedure TfrmTatCardInPut.Button3Click(Sender: TObject);
var
  maxno, maxPCId, WSql: string;
  FStrs: TStringList;
  i, j, MPCNo: integer;
begin
  if edtMachNo.Text = '' then
    Exit;
  if StrToIntdef(edtCardQty.Text, 0) = 0 then
    Exit;
  try
    FStrs := TStringList.Create();
    ExtractStrings([';'], [' '], PChar(edtMachNo.Text), FStrs);
    ADOQueryCmd.Connection.BeginTrans;
//    with ADOQueryTemp do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add(' select CardQty=OrdPiece-ISNULL((select sum(CardQty) from Tat_Plan_Mach AA where AA.SubId=B.SubId),0)');
//      sql.Add(' from   Tat_Plan_Sub B ');
//      sql.Add(' where B.SubId=' + QuotedStr(Trim(FSubId)));
//      Open;
//    end;
    ///////////////判断是否超出限制//////////////////
//    if FStrs.Count * StrToIntdef(edtCardQty.Text, 0) > StrToIntdef(edtWAPPS.text, 0) then
//    begin
//      raise Exception.Create('安排数量过多!');
//    end;
    ///////////////判断是否超出限制//////////////////


    for i := 0 to FStrs.Count - 1 do
    begin

      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select PCNo=isnull(Max(PCNo),0)  ');
        sql.Add('from Tat_Plan_Card A ');
        sql.Add('where isnull(Status,''0'') <>''-1'' and A.MainID=' + Quotedstr(Trim(FMainID)));
        sql.Add('and A.Loom=' + Quotedstr(Trim(FStrs[i])));
        Open;
      end;
      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,Loom,PCId,PCNo,PCDate,Filler,BatchNo,Repairer) 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(FMainId)));
          sql.Add(',' + Quotedstr(Trim(FSubId)));
          sql.Add(',' + Quotedstr(Trim(edtConNo.text)));
          sql.Add(',' + Quotedstr(Trim(FStrs[i])));
          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(' )');
          MPCNo := MPCNo + 1;

          if WSql <> '' then
          begin
            WSql := WSql + ',' + QuotedStr(Trim(maxPCId));
          end
          else
          begin
            WSql := QuotedStr(Trim(maxPCId));
          end;
        end;
        ExecSQL;
      end;

//////////////////            插入布票               //////////////////////
    end;
    ADOQueryCmd.Connection.CommitTrans;
    FStrs.Free;
  except
    FStrs.Free;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

  if trim(isDY.text) = '是' then
  begin
    try
      frmLabelPrint := TfrmLabelPrint.Create(Application);
      with frmLabelPrint do
      begin
        FPreviewPrint := False;
        FLMType := 'TatPlanCard';
        FFiltration1 := WSql;
        if ShowModal = 1 then
        begin
//        Self.InitGrid();
          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 PCID in (' + WSql + ')');
            ExecSQL;
          end;
        end;
      end;
    finally
      frmLabelPrint.Free;
    end;
  end;
  InitOrd();
  InitGrid1();
  InitGrid2();
  edtMachNo.text := '';
end;

procedure TfrmTatCardInPut.Tv1CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
begin

  if '0' = AViewInfo.GridRecord.Values[Tv1Column2.Index] then
    ACanvas.Brush.Color := $66ff66;
end;

procedure TfrmTatCardInPut.Tv1DblClick(Sender: TObject);
var
  FStrs: TStringList;
begin

  try
    FStrs := TStringList.Create();
    ExtractStrings([';'], [' '], PChar(edtMachNo.Text), FStrs);
    if FStrs.IndexOf(Trim(CDS_1.FieldByName('MCNO').AsString)) < 0 then
      edtMachNo.Text := edtMachNo.Text + TRIM(CDS_1.FieldByName('MCNO').AsString) + ';';
  finally
    FStrs.Free
  end;

end;

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

procedure TfrmTatCardInPut.ToolButton1Click(Sender: TObject);
var
  KMainId: string;
  KWBCID: string;
begin
  try
    frmTatPlan_ZOUKA_Sel := TfrmTatPlan_ZOUKA_Sel.Create(Application);
    with frmTatPlan_ZOUKA_Sel do
    begin
      if ShowModal = 1 then
      begin
        self.FMainId := Trim(frmTatPlan_ZOUKA_Sel.CDS_1.fieldbyname('MainId').AsString);
        self.FSubId := Trim(frmTatPlan_ZOUKA_Sel.CDS_1.fieldbyname('SubId').AsString);
        self.edtConNo.text := Trim(frmTatPlan_ZOUKA_Sel.CDS_1.fieldbyname('WBCID').AsString);
        self.edtCode.text := Trim(frmTatPlan_ZOUKA_Sel.CDS_1.fieldbyname('WB_Clothcode').AsString);
        self.edtName.text := Trim(frmTatPlan_ZOUKA_Sel.CDS_1.fieldbyname('WB_ClothName').AsString);
      end;
    end;
  finally
    frmTatPlan_ZOUKA_Sel.Free;
  end;

  begin
    begin
        KMainId:=self.FMainId;
        KWBCID:=self.edtConNo.text;
    end;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Close;
        sql.Clear;
        sql.Add(' select * ');
        sql.Add(' ,ZWJYPS=(select count(X.Loom) from Tat_Plan_Card X where X.Loom =AA.MCNO and isnull(Status,''0'')=''0'') ');
        sql.Add(' ,BWJYPS=(select count(X.Loom) from Tat_Plan_Card X where X.Loom =AA.MCNO and isnull(Status,''0'')=''0'' and MainID=''' + trim(KMainId) + ''')');
        sql.Add(' from (select A.MCNO,ISAP=''0'' from BS_Machine A where EXISTS (select Loom from Tat_Plan_Card X where X.Loom = A.MCNO and X.WBCID=''' + trim(KWBCID) + ''')');
        sql.Add(' UNION   ');
        sql.Add(' select A.MCNO,ISAP=''1'' from BS_Machine A where not EXISTS (select Loom from Tat_Plan_Card X where X.Loom = A.MCNO and X.WBCID=''' + trim(KWBCID) + ''')');
        sql.Add(' ) AA Order BY ISAP,MCNO');
//      ShowMessage(sql.text);
        Open;
      end;
      SCreateCDS(ADOQueryMain, CDS_1);
      SInitCDSData(ADOQueryMain, CDS_1);
    finally
      ADOQueryMain.EnableControls;
    end;
  end;
  begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select COUNT(*) AS APPS,Loom,isnull(Repairer,'''') AS Repairer,isnull(BatchNo,'''') AS BatchNo ');
    sql.Add(',PCDate=(select top 1 CONVERT(VARCHAR(10),X.PCDate,120) from Tat_Plan_Card X where X.Loom=A.Loom   ');
    sql.Add(' and isnull(X.Repairer,'''') = isnull(A.Repairer,'''') and isnull(X.BatchNo,'''') = isnull(A.BatchNo,'''') and isnull(Status,''0'') <>''-1'' ORDER BY PCDate ) ');
    sql.Add(' ,DYPS=(select count(*) from Tat_Plan_Card X where X.Loom=A.Loom and PrtCount > 0 and isnull(Status,''0'') <>''-1''  ');
    sql.Add(' and isnull(X.Repairer,'''') = isnull(A.Repairer,'''') and isnull(X.BatchNo,'''') = isnull(A.BatchNo,'''') )');
    sql.Add(' ,JYPS=(select count(*) from Tat_Plan_Card X where X.Loom=A.Loom and isnull(X.Status,''0'') = ''1''    ');
    sql.Add(' and isnull(X.Repairer,'''') = isnull(A.Repairer,'''') and isnull(X.BatchNo,'''') = isnull(A.BatchNo,'''') )');
    sql.Add(' from Tat_Plan_Card A where WBCID =''' + trim(KWBCID) + '''');
    sql.Add(' and Deler is null');
    sql.Add(' GROUP BY Loom,isnull(Repairer,''''),isnull(BatchNo,'''') ');
//    ShowMessage(sql.text);
    Open;
  end;

  SCreateCDS(ADOQueryTemp, CDS_2);
  SInitCDSData(ADOQueryTemp, CDS_2);
end;

end;

end.

