unit U_TradeClothProcessInput;

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, cxSpinEdit,
  StdCtrls, ExtCtrls, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxDateRanges, dxBarBuiltInMenu, U_BaseInput, System.ImageList, Vcl.ImgList,
  cxPC, dxScrollbarAnnotations, cxCheckBox, dxSkinsCore, dxSkinsDefaultPainters,
  cxImageList;

type
  TfrmTradeClothProcessInput = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ToolButton1: TToolButton;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    v1PRTColor: TcxGridDBColumn;
    TV2Column3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    TV2Column1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    VC_PRTPs: TcxGridDBColumn;
    v1PRTOrderQty: TcxGridDBColumn;
    v1OrderUnit: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    TV2Column4: TcxGridDBColumn;
    TV2Column5: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    TV2Column8: TcxGridDBColumn;
    TV2Column9: TcxGridDBColumn;
    GPM_2: TcxGridPopupMenu;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    ADO_2: TADOQuery;
    ToolButton3: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    GPM_1: TcxGridPopupMenu;
    ADO_1: TADOQuery;
    TV2Column6: TcxGridDBColumn;
    TV2Column7: TcxGridDBColumn;
    TV2Column10: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure TV2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure TV2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
  private
    { Private declarations }                                                       procedure InitGrid1();
    procedure InitGrid2();
  public
    { Public declarations }
    FBCId, FStkName, FMasterOrderNo: string;
  end;

var
  frmTradeClothProcessInput: TfrmTradeClothProcessInput;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_TradeFinishClothStkSel, U_CompanySel,
  U_TradePlanSel, U_TradeClothProcessCastingInput;

{$R *.dfm}
procedure TfrmTradeClothProcessInput.InitGrid1();
begin
  with ADO_1 do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from BS_Cloth_IO A    ');
    SQL.Add(' where A.IOFlag=''出库'' and A.IOType=''投产出库''   ');
    sql.Add(' and isnull(stkName,'''')=''贸易布匹'' ');
    sql.Add(' and ToSubId= ' + QuotedStr(TRIM(CDS_2.FieldByName('SubId').AsString)));

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

end;

procedure TfrmTradeClothProcessInput.InitGrid2();
begin
  with ADO_2 do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.*,B.* ');
    sql.Add(',OutPiece=cast(0 as DECIMAL(18,2)),OutQty=cast(0 as DECIMAL(18,2)),StkID=cast(null as varchar(60))   ');
    sql.Add(',LJOutPiece=isnull( (select SUM(X.Piece) from BS_Cloth_IO X where X.ToSubid = B.Subid and IOFlag=''出库'' and IOType=''投产出库'' ) ,0)  ');
    sql.Add(',LJOutQty= isnull((select SUM(X.Qty)   from BS_Cloth_IO X where X.ToSubid = B.Subid and IOFlag=''出库'' and IOType=''投产出库'')   ,0)     ');
    sql.Add(' from Trade_Plan_Main A inner join Trade_Plan_Sub B on A.MainId=B.MainId');
    sql.Add(' where A.MasterOrderNo= ' + QuotedStr(TRIM(FMasterOrderNo)));
    Open;
  end;
  SCreateCDS(ADO_2, CDS_2);
  SInitCDSData(ADO_2, CDS_2);
end;

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

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

procedure TfrmTradeClothProcessInput.FormShow(Sender: TObject);
var
  fsj: string;
begin
  inherited;
  ReadCxGrid(trim(Self.Caption) + 'TV1', Tv1, '贸易汇总仓库');
  ReadCxGrid(trim(Self.Caption) + 'TV2', Tv2, '贸易汇总仓库');

  InitGrid2();
end;

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

procedure TfrmTradeClothProcessInput.ToolButton3Click(Sender: TObject);
begin
  try
    frmTradeClothProcessCastingInput := TfrmTradeClothProcessCastingInput.Create(Application);
    with frmTradeClothProcessCastingInput do
    begin
      FSubid := Trim(Self.CDS_2.fieldbyname('Subid').AsString);
      FMasterOrderNo := Trim(Self.CDS_2.fieldbyname('MasterOrderNo').AsString);
      if ShowModal = 1 then
      begin
        Self.InitGrid2();
      end;
    end;
  finally
    frmTradeClothProcessCastingInput.Free;
  end;

end;

procedure TfrmTradeClothProcessInput.TV2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  ToolButton3.Click;
end;

procedure TfrmTradeClothProcessInput.TV2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  InitGrid1();
end;

end.

