unit U_TradeClothProcessOutInput;

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

type
  TfrmTradeClothProcessOutInput = 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;
    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;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column12: TcxGridDBColumn;
    ScrollBox1: TScrollBox;
    Label3: TLabel;
    Label14: TLabel;
    Label15: TLabel;
    ioTime: TcxDateEdit;
    CustName: TcxButtonEdit;
    IOType: TcxComboBox;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CustNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure CustNameDblClick(Sender: TObject);
  private
    { Private declarations }
    function SaveData(): Boolean;
    procedure InitGrid1();
  public
    { Public declarations }
    FMasterOrderNo, FSubid: string;
  end;

var
  frmTradeClothProcessOutInput: TfrmTradeClothProcessOutInput;

implementation

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

{$R *.dfm}

procedure TfrmTradeClothProcessOutInput.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(',KYPiece=StkPiece,KYQty=StkQty  ');
    sql.Add(' from BS_Cloth_IO A    ');
    SQL.Add(' where A.IOFlag=''入库'' and (isnull(StkQty,0)>0 or isnull(StkPiece,0)>0) ');
    sql.Add(' and isnull(stkName,'''')=''贸易布匹''  ');
    sql.Add(' and FromMasterOrdNo= ' + QuotedStr(TRIM(FMasterOrderNo)));
    Open;
  end;
  SCreateCDS(ADO_1, CDS_1);
  SInitCDSData(ADO_1, CDS_1);

end;

procedure TfrmTradeClothProcessOutInput.CustNameDblClick(Sender: TObject);
begin
  TcxButtonEdit(Sender).Text := '';
  TcxButtonEdit(Sender).Properties.LookupItems.Text := '';
end;

procedure TfrmTradeClothProcessOutInput.CustNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '客户';
      if ShowModal = 1 then
      begin
        self.CustName.Properties.LookupItems.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoCode').AsString);
        self.CustName.Text := Trim(frmCompanySel.CDS_1.fieldbyname('CoAbbrName').AsString);
      end;
    end;

  finally
    frmCompanySel.Free;
  end;
end;

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

function TfrmTradeClothProcessOutInput.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
        if not GetLSNo(ADOQueryCmd, Maxno, 'Z', 'BS_Cloth_IO', 4, 1) then
          raise Exception.Create('取染色坯布出库编号失败！');
        if (CDS_1.fieldbyname('OutPiece').AsFloat > 0) and (CDS_1.fieldbyname('OutQty').AsFloat > 0) then
        begin
          with ADOQueryCmd do
          begin
            Close;
            Sql.Clear;
            Sql.Add('exec P_BS_Cloth_Out ');
            Sql.Add('  @StkID=' + quotedstr(Trim(CDS_1.fieldbyname('StkID').AsString)));
            Sql.Add(' ,@BCIOID=' + quotedstr(Trim(Maxno)));
            Sql.Add(' ,@IOTime=' + quotedstr(Trim(FormatDateTime('yyyy-MM-dd', IOTime.Date))));
            Sql.Add(' ,@IOType=' + quotedstr(Trim(IOType.Text)));
            Sql.Add(' ,@ToCoNo=' + quotedstr(Trim(self.CustName.Properties.LookupItems.Text)));
            Sql.Add(' ,@ToCoName=' + quotedstr(Trim(self.CustName.Text)));
            Sql.Add(' ,@Piece=' + Trim(CDS_1.fieldbyname('OutPiece').AsString));
            Sql.Add(' ,@Qty=' + Trim(CDS_1.fieldbyname('OutQty').AsString));
            Sql.Add(' ,@Dcode=' + quotedstr(Trim(Dcode)));
            Sql.Add(' ,@DName=' + quotedstr(Trim(DName)));
            Open;
          end;
          if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
            raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
        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 TfrmTradeClothProcessOutInput.TBCloseClick(Sender: TObject);
begin

  Close;
end;

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

procedure TfrmTradeClothProcessOutInput.TBSaveClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  ToolBar1.SetFocus;
  if SaveData() then
  begin
    ModalResult := 1;
  end;
end;

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

end.

