unit U_TradeClothCompleteInput;

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, cxImageList, dxSkinsCore,
  dxSkinsDefaultPainters;

type
  TfrmTradeClothCompleteInput = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    DS_1: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADO_1: TADOQuery;
    ADOQueryTemp: TADOQuery;
    GPM_1: TcxGridPopupMenu;
    ToolButton1: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    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;
    ADO_2: TADOQuery;
    DS_2: TDataSource;
    TV2Column10: TcxGridDBColumn;
    TV2Column11: TcxGridDBColumn;
    TV2Column12: TcxGridDBColumn;
    ToolButton3: TToolButton;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    ToolButton2: TToolButton;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure TV2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure ToolButton3Click(Sender: TObject);
    procedure TV2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton2Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid1();
    procedure InitGrid2();
  public
    { Public declarations }
    FBCId, FStkName, FMasterOrderNo, FTPFId: string;
  end;

var
  frmTradeClothCompleteInput: TfrmTradeClothCompleteInput;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_TradeFinishClothStkSel, U_CompanySel,
  U_TradePlanSel, U_TradePlanClothEnterInput, U_TradeCardInPut;

{$R *.dfm}
procedure TfrmTradeClothCompleteInput.InitGrid1();
begin
  with ADO_1 do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(',OutPiece=cast(0 as DECIMAL(18,2)),OutQty=cast(0 as DECIMAL(18,2))   ');
    sql.Add(' from BS_Cloth_IO A    ');
    SQL.Add(' where A.IOFlag=''入库'' and A.IOType=''加工完成''   ');
    sql.Add(' and isnull(stkName,'''')=''贸易布匹'' ');
    sql.Add(' and FromSubId= ' + QuotedStr(TRIM(CDS_2.FieldByName('SubId').AsString)));

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

end;

procedure TfrmTradeClothCompleteInput.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= (select SUM(X.Piece) from BS_Cloth_IO X where X.FromSubId = B.SubId and IOFlag=''入库'' and IOType=''加工完成'' )   ');
    sql.Add(',LJOutQty= (select SUM(X.Qty)   from BS_Cloth_IO X where X.FromSubId = B.SubId and IOFlag=''入库'' and IOType=''加工完成'')      ');
    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 TfrmTradeClothCompleteInput.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := caFree;
end;

procedure TfrmTradeClothCompleteInput.TBCloseClick(Sender: TObject);
begin
  ModalResult := 1;
  Close;
end;

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

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

end;

procedure TfrmTradeClothCompleteInput.ToolButton2Click(Sender: TObject);
begin
  try
    frmTradeCardInPut := TfrmTradeCardInPut.Create(Application);
    with frmTradeCardInPut do
    begin
      FPursId := '';
      FMainId := Trim(Self.CDS_2.fieldbyname('MainId').AsString);
      FSubId := Trim(Self.CDS_2.fieldbyname('SubId').AsString);
      if ShowModal = 1 then
      begin
        InitGrid2();
      end;
    end;
  finally
    frmTradeCardInPut.Free;
  end;
end;

procedure TfrmTradeClothCompleteInput.ToolButton3Click(Sender: TObject);
begin
  try
    frmTradePlanClothEnterInput := TfrmTradePlanClothEnterInput.Create(Application);
    with frmTradePlanClothEnterInput do
    begin
      FSubId := Trim(self.CDS_2.fieldbyname('SubId').AsString);

      FTPFId := self.FTPFId;

      if ShowModal = 1 then
      begin
        Self.InitGrid1();
      end;
    end;
  finally
    frmTradePlanClothEnterInput.Free;
  end;
end;

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

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

end.

