unit U_TradeClothCostPrice;

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

type
  TfrmTradeClothCostPrice = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADO_1: TADOQuery;
    ADOQueryTemp: TADOQuery;
    GPM_1: TcxGridPopupMenu;
    ToolButton1: TToolButton;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    Tv1Column13: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    Tv1Column11: TcxGridDBColumn;
    Tv1Column20: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    Tv1Column19: TcxGridDBColumn;
    v1Column19: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column20: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column21: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column10: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column14: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolButton2: TToolButton;
    Tv1Column15: TcxGridDBColumn;
    Tv1Column16: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    Tv1Column17: TcxGridDBColumn;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    v1Column18: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    Tv1Column18: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridDBColumn22: TcxGridDBColumn;
    cxGridDBColumn23: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridDBColumn26: TcxGridDBColumn;
    Tv1Column22: TcxGridDBColumn;
    cxGridDBColumn27: TcxGridDBColumn;
    cxGridDBColumn28: TcxGridDBColumn;
    cxGridDBColumn29: TcxGridDBColumn;
    cxGridDBColumn30: TcxGridDBColumn;
    cxGridDBColumn31: TcxGridDBColumn;
    cxGridDBColumn32: TcxGridDBColumn;
    cxGridDBColumn33: TcxGridDBColumn;
    cxGridDBColumn34: TcxGridDBColumn;
    Tv1Column24: TcxGridDBColumn;
    Tv1Column25: TcxGridDBColumn;
    Tv1Column26: TcxGridDBColumn;
    Tv1Column27: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    TV2Column1: TcxGridDBColumn;
    TV2Column2: TcxGridDBColumn;
    ToolButton3: TToolButton;
    ADO_2: TADOQuery;
    GPM_2: TcxGridPopupMenu;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid1();
    procedure InitGrid2();
    function SaveData(): Boolean;
  public
    { Public declarations }
    FBCId, FStkName, FMasterOrderNo: string;
  end;

var
  frmTradeClothCostPrice: TfrmTradeClothCostPrice;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}
procedure TfrmTradeClothCostPrice.InitGrid1();
var
  Msql, mbegdate, menddate: string;
  SqlStr: string;
begin

  SqlStr := ' select A.* ';
  SqlStr := SqlStr + ',OutPiece=cast(0 as DECIMAL(18,2)),OutQty=cast(0 as DECIMAL(18,2))   ';
  SqlStr := SqlStr + ' from BS_Cloth_IO A    ';
  SqlStr := SqlStr + ' where A.IOFlag=''入库'' and (isnull(StkQty,0)>0 or isnull(StkPiece,0)>0) ';
  SqlStr := SqlStr + ' and isnull(stkName,'''')=''贸易布匹'' ';
  SqlStr := SqlStr + 'and A.FromMasterOrdNo =  ' + quotedstr(FMasterOrderNo);
  InitCDSData(ADO_1, CDS_1, Tv1, SqlStr, '', 'STKID');

end;

procedure TfrmTradeClothCostPrice.InitGrid2();
var
  Msql, mbegdate, menddate: string;
  SqlStr: string;
begin

  SqlStr := ' select A.*,CostMoney=Qty*CostPrice';
  SqlStr := SqlStr + ' from BS_Cloth_IO A    ';
  SqlStr := SqlStr + 'where A.STKName = ''贸易布匹'' and A.IOFlag = ''出库'' and A.IOType = ''投产出库''  ';
  SqlStr := SqlStr + 'and A.ToMasterOrdNo =  ' + quotedstr(FMasterOrderNo);
  InitCDSData(ADO_2, CDS_2, Tv2, SqlStr, '', 'STKID');

end;

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

function TfrmTradeClothCostPrice.SaveData(): Boolean;
var
  MBCIOID, Maxno: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_1.DisableControls;
    with CDS_1 do
    begin
      First;
      while not eof do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from BS_Cloth_IO where BCIOID=''' + Trim(CDS_1.fieldbyname('BCIOID').AsString) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin

          Edit;
          FieldByName('Editid').Value := Trim(Dcode);
          FieldByName('Editer').Value := Trim(DName);
          FieldByName('EditTime').Value := SGetServerDate(ADOQueryTemp);

//          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'BS_Cloth_IO', 0);

          FieldByName('CostPrice').Value := CDS_1.fieldbyname('CostPrice').Value;

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

end;

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

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

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

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

end.

