unit U_TradeClothCardCompleteList;

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
  TfrmTradeClothCardCompleteList = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    DS_1: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADO_1: TADOQuery;
    ADOQueryTemp: TADOQuery;
    GPM_1: TcxGridPopupMenu;
    ToolButton1: TToolButton;
    ToolButton3: TToolButton;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridDBColumn22: TcxGridDBColumn;
    cxGridDBColumn23: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    TV1Column1: TcxGridDBColumn;
    TV1Column2: TcxGridDBColumn;
    TV1Column3: TcxGridDBColumn;
    TV1Column4: TcxGridDBColumn;
    TV1Column5: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    TV1Column6: TcxGridDBColumn;
    TV1Column7: TcxGridDBColumn;
    TV1Column8: TcxGridDBColumn;
    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 TV4Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TV4Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure cxGridDBColumn23PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    { Private declarations }
    function SaveData(): Boolean;
    procedure InitGrid1();
  public
    { Public declarations }
    FSubId: string;
  end;

var
  frmTradeClothCardCompleteList: TfrmTradeClothCardCompleteList;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp;

{$R *.dfm}
procedure TfrmTradeClothCardCompleteList.InitGrid1();
var
  SqlStr: string;
begin
  SqlStr := ' select A.*';
  SqlStr := SqlStr + ' from BS_Cloth_IO A    ';
  SqlStr := SqlStr + 'where A.STKName = ''贸易布匹'' and A.IOFlag = ''入库'' and A.IOType = ''加工完成''  ';
  SqlStr := SqlStr + 'and A.FromSubId =  ' + quotedstr(Trim(FSubId));
  InitCDSData(ADO_1, CDS_Sub, Tv1, SqlStr, '', '');

end;

procedure TfrmTradeClothCardCompleteList.cxGridDBColumn23PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      MainType := 'Cust';
      flag := 'StkPosition';
      flagname := '库位';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('StkPosition').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

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

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

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

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

end;

procedure TfrmTradeClothCardCompleteList.ToolButton3Click(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if SaveData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    ModalResult := 1;
  end;
end;

function TfrmTradeClothCardCompleteList.SaveData(): Boolean;
var
  Maxno, MBCIOID: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not eof do
      begin

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update BS_Cloth_IO Set Editid=' + quotedstr(Trim(Dcode)) + ',Editer=' + quotedstr(Trim(DName)) + ',EditTime=getdate()');
          sql.Add(', IOTime=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', CDS_Sub.fieldbyname('IOTime').AsDateTime))));
          sql.Add(', Downgrade=' + quotedstr(Trim(CDS_Sub.fieldbyname('Downgrade').AsString)));
          sql.Add(', SettlementType=' + quotedstr(Trim(CDS_Sub.fieldbyname('SettlementType').AsString)));
          sql.Add(', StkPosition=' + quotedstr(Trim(CDS_Sub.fieldbyname('StkPosition').AsString)));
          sql.Add(', Note=' + quotedstr(Trim(CDS_Sub.fieldbyname('Note').AsString)));
          sql.Add(' where stkid=''' + Trim(CDS_Sub.fieldbyname('stkid').AsString) + '''');
          ExecSQL;
        end;

        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update BS_Cloth_IO Set StkPiece=(select Sum(isnull(Piece,0)*IOQtyFlag) from BS_Cloth_IO A where A.STKID=BS_Cloth_IO.BCIOID )');
          sql.Add(',StkQty=(select Sum(isnull(Qty,0)*IOQtyFlag) from BS_Cloth_IO A where A.STKID=BS_Cloth_IO.BCIOID )');
          sql.Add(' where BCIOID=''' + Trim(CDS_Sub.fieldbyname('BCIOID').AsString) + '''');
          ExecSQL;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select stkQty,StkPiece from BS_Cloth_IO where BCIOID=''' + Trim(CDS_Sub.fieldbyname('BCIOID').AsString) + '''');
          Open;
        end;
        if ADOQueryTemp.FieldByName('stkQty').Value < 0 then
          raise Exception.Create('入库数量小于出库数量不能保存!');
        if ADOQueryTemp.FieldByName('StkPiece').Value < 0 then
          raise Exception.Create('入库匹数小于出库匹数不能保存!');
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('insert into Finance_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)       ');
          sql.Add('values(''贸易待检布入库''    ');
          sql.Add(',' + quotedstr(Trim(CDS_Sub.fieldbyname('BCIOID').AsString)));
          sql.Add(',''保存''   ');
          sql.Add(',' + quotedstr(DName));
          sql.Add(',' + quotedstr(trim(self.Caption)));
          sql.Add(',' + quotedstr(Trim(CDS_Sub.fieldbyname('BCIOID').AsString)));
          sql.Add(')  ');
          ExecSQL;
        end;

        Next;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    CDS_Sub.EnableControls;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

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

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

procedure TfrmTradeClothCardCompleteList.TV4Column3PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'Downgrade';
      flagname := '降级原因';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('Downgrade').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmTradeClothCardCompleteList.TV4Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'SettlementType';
      flagname := '结案类型';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('SettlementType').Value := Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.

