unit U_QryBSPrtDyeOutEdit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxClasses, cxControls, cxGridCustomView, cxGrid, cxMemo,
  cxRichEdit, ComCtrls, cxContainer, cxTextEdit, cxMaskEdit, cxButtonEdit,
  StdCtrls, ToolWin, DBClient, ADODB, ExtCtrls, BtnEdit, cxCalendar, StrUtils,
  cxCurrencyEdit, cxGridCustomPopupMenu, cxGridPopupMenu, cxDropDownEdit,
  RM_Common, RM_Preview, RM_System, RM_Class, RM_GridReport, RM_Dataset, Menus,
  cxSplitter, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, cxCheckBox,
  dxDateRanges, dxBarBuiltInMenu, cxPC, System.ImageList, Vcl.ImgList,
  U_BaseInput, cxImage, cxDBEdit, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdExplicitTLSClientServerBase, IdFTP, ShellAPI,
  dxScrollbarAnnotations, cxGeometry, dxFramedControl, dxLayoutContainer,
  dxLayoutControl, dxPanel, cxGroupBox, dxLayoutcxEditAdapters, dxCore,
  cxDateUtils, cxImageList, dxSkinsCore, dxSkinsDefaultPainters, cxSpinEdit,
  dxLayoutControlAdapters, dxSkinWXI;

type
  TfrmQryBSPrtDyeOutEdit = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    GPM_1: TcxGridPopupMenu;
    Label51: TLabel;
    cxgrdpmn2: TcxGridPopupMenu;
    Qry_ImgHX: TADOQuery;
    DS_ImgHX: TDataSource;
    Tbcgs: TToolButton;
    IdFTP1: TIdFTP;
    dxPanel1: TdxPanel;
    dxLayoutControl_bzplan: TdxLayoutControl;
    dxLayoutControl_bzplanGroup_Root: TdxLayoutGroup;
    Tsj: TToolButton;
    ZIONO: TcxTextEdit;
    dxLayoutItem1: TdxLayoutItem;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1CRType: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    BegDate: TcxDateEdit;
    dxLayoutItem4: TdxLayoutItem;
    todept: TcxComboBox;
    dxLayoutItem2: TdxLayoutItem;
    Tv1Column1: TcxGridDBColumn;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure TcxButtonEditDel(Sender: TObject);
    procedure TbcgsClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TsjClick(Sender: TObject);
    procedure Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure TBAddClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
  private
    function SaveData(): Boolean;
    { Private declarations }
  public
    FBCId, FStkName, FKHName: string;

    { Public declarations }
  end;

var
  frmQryBSPrtDyeOutEdit: TfrmQryBSPrtDyeOutEdit;
  newh: hwnd;

implementation

uses
  U_DataLink, U_RTFun, U_CompanySel, U_ZDYHelp, U_cxGridCustomSet,
  U_FormLayOutDesign, U_globalVar, U_BSPrtDyeStkSel;


{$R *.dfm}

procedure TfrmQryBSPrtDyeOutEdit.TBCloseClick(Sender: TObject);
begin

  close;

end;

procedure TfrmQryBSPrtDyeOutEdit.TBDelClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  if Trim(CDS_1.fieldbyname('BPIOID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;

    try
      ADOCmd.Connection.BeginTrans;
      with ADOCmd 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_1.fieldbyname('BPIOID').AsString)));
        sql.Add(',''删除''   ');
        sql.Add(',' + quotedstr(DName));
        sql.Add(',' + quotedstr(trim(self.Caption)));
        sql.Add(',' + quotedstr(Trim(CDS_1.fieldbyname('BPIOID').AsString)));
        sql.Add(')  ');
        Sql.Add('exec P_BS_Product_Out_Del ');
        Sql.Add(' @BPIOIDS=' + quotedstr(Trim(CDS_1.fieldbyname('BPIOID').AsString)));
        Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
        Sql.Add(',@DName=' + quotedstr(Trim(DName)));
        Open;
      end;
      if ADOCmd.FieldByName('intReturn').AsInteger = -1 then
        raise Exception.Create(pchar(trim(ADOCmd.FieldByName('ShowMsg').AsString)));

      ADOCmd.Connection.CommitTrans;
      CDS_1.Delete;
    except
      ADOCmd.Connection.RollbackTrans;
      application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    end;
  end
  else
    CDS_1.Delete;
end;

procedure TfrmQryBSPrtDyeOutEdit.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := cahide;
end;

procedure TfrmQryBSPrtDyeOutEdit.FormCreate(Sender: TObject);
begin
  inherited;
  cxGrid2.Align := alClient;
end;

procedure TfrmQryBSPrtDyeOutEdit.FormDestroy(Sender: TObject);
begin
  inherited;
  frmQryBSPrtDyeOutEdit := nil;
end;

procedure TfrmQryBSPrtDyeOutEdit.FormShow(Sender: TObject);
var
  fsj, maxId: string;
begin
  inherited;
  ReadCxGrid(FStkName + '出库登记', Tv1, '机物料仓库');
  fsj := 'select name=DPNAME,code=DPNAME from SY_Dept where DPLEVEL=''2''  order by DPID ';

  SInitTcxComBoxBySql(ADOTemp, todept, false, fsj);
  with ADOTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from BS_Product_IO A');
    sql.Add(' where BPIOID=''' + Trim(FBCId) + '''');
    Open;
  end;
  SCreateCDS(ADOTemp, CDS_1);
  SInitCDSData(ADOTemp, CDS_1);
  if ADOTemp.FieldByName('ZIONO').AsString = '' then
  begin
    if GetLSNo(ADOCmd, maxId, 'RO', 'BS_Product_IO', 3, 1) = False then
    begin
      Application.MessageBox('取最大号失败!', '提示', 0);
      Exit;
    end;
    ZIONO.Text := maxId;
    begDate.Date := SGetServerDate(ADOTemp);

  end
  else
  begin
    ZIONO.Text := ADOTemp.FieldByName('ZIONO').AsString;
    begDate.Date := ADOTemp.FieldByName('IOTime').ASDATETIME;
    todept.ItemIndex := todept.Properties.Items.IndexOf(trim(ADOTemp.fieldbyname('todept').AsString));
  end;
end;

function TfrmQryBSPrtDyeOutEdit.SaveData(): Boolean;
var
  MBPIOID, Maxno: string;
begin
  try
    ADOCmd.Connection.BeginTrans;
    CDS_1.DisableControls;
    with CDS_1 do
    begin
      First;
      while not eof do
      begin
        with ADOTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from BS_Product_IO where BPIOID=''' + Trim(CDS_1.fieldbyname('BPIOID').AsString) + '''');
          Open;
        end;
        MBPIOID := Trim(ADOTemp.fieldbyname('BPIOID').AsString);
        if Trim(MBPIOID) = '' then
        begin
          if not GetLSNo(ADOCmd, Maxno, 'PC', 'BS_Product_IO', 3, 1) then
            raise Exception.Create('取染色坯布出库编号失败！');
        end
        else
        begin
          Maxno := Trim(MBPIOID);
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from BS_Product_IO where BPIOID=''' + Trim(Maxno) + '''');
          Open;
        end;
        with ADOCmd do
        begin
          if Trim(MBPIOID) = '' then
          begin
            Append;
            FieldByName('Filler').Value := Trim(DName)
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDate(ADOTemp);
          end;
          RTSetSaveDataCDS(ADOCmd, Tv1, CDS_1, 'BS_Product_IO', 0);
          FieldByName('STKName').Value := FStkName;
          FieldByName('IOTIME').Value := formatdateTime('yyyy-MM-dd', BEGDATE.Date);
          FieldByName('todept').Value := Trim(todept.Text);
          FieldByName('BPIOID').Value := Trim(Maxno);
          FieldByName('IOFlag').Value := '出库';
          FieldByName('IOQtyFlag').Value := -1;
          FieldByName('STKID').Value := CDS_1.fieldbyname('STKID').Value;
          FieldByName('FromCoNo').Value := CDS_1.fieldbyname('FromCoNo').Value;
          FieldByName('FromCoName').Value := CDS_1.fieldbyname('FromCoName').Value;
          FieldByName('StkCoNo').Value := CDS_1.fieldbyname('StkCoNo').Value;
          FieldByName('ToCoNo').Value := CDS_1.fieldbyname('ToCoNo').Value;
          FieldByName('ZIONO').Value := Trim(ZIONO.Text);
          Post;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update BS_Product_IO Set  STKQty=(select Sum(isnull(Qty,0)*IOQtyFlag) from BS_Product_IO A where A.STKID=BS_Product_IO.BPIOID )');
          sql.Add('where BPIOID=''' + Trim(CDS_1.fieldbyname('STKID').AsString) + '''');
          ExecSQL;
        end;
        with ADOTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from BS_Product_IO where BPIOID=''' + Trim(CDS_1.fieldbyname('STKID').AsString) + '''');
          Open;
        end;

        if ADOTemp.FieldByName('STKQty').Value < 0 then
          raise Exception.Create('出库数量大于入库数量不能保存！');

        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('exec P_Dye_Formulalarge_CLOTHOUT');
          sql.Add('  @BPIOID=''' + Trim(Maxno) + '''');
          ExecSQL;
        end;
        with ADOCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('insert into Finance_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)       ');
          sql.Add('values(''染化料出库''    ');
          sql.Add(',' + quotedstr(Maxno));
          sql.Add(',''保存''   ');
          sql.Add(',' + quotedstr(DName));
          sql.Add(',' + quotedstr(trim(self.Caption)));
          sql.Add(',' + quotedstr(Maxno));
          sql.Add(')  ');
          ExecSQL;
        end;
        Edit;
        FieldByName('BPIOID').Value := Trim(Maxno);
        Post;
        Next;
      end;
    end;
    CDS_1.EnableControls;

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

end;

procedure TfrmQryBSPrtDyeOutEdit.TBSaveClick(Sender: TObject);
begin
  if CDS_1.IsEmpty then
    Exit;
  if CDS_1.Locate('IOTime', null, []) = True then
  begin
    Application.MessageBox('出库时间不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_1.Locate('IOType', null, []) = True then
  begin
    Application.MessageBox('出库类型不能为空!', '提示', 0);
    Exit;
  end;

  if CDS_1.Locate('Qty', null, []) = True then
  begin
    Application.MessageBox('数量不能为空!', '提示', 0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    ModalResult := 1;
    Exit;
  end;
end;

procedure TfrmQryBSPrtDyeOutEdit.TBAddClick(Sender: TObject);
begin
  try
    frmBSPrtDyeStkSel := TfrmBSPrtDyeStkSel.Create(Application);
    with frmBSPrtDyeStkSel do
    begin
      FStkName := Self.FStkName;
      if ShowModal = 1 then
      begin
        frmBSPrtDyeStkSel.CDS_Main.DisableControls;
        with frmBSPrtDyeStkSel.CDS_Main do
        begin
          First;
          while frmBSPrtDyeStkSel.CDS_Main.Locate('SSel', True, []) do
          begin
            with Self.CDS_1 do
            begin
              Append;
              FieldByName('IOTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
              FieldByName('STKID').Value := frmBSPrtDyeStkSel.CDS_Main.fieldbyname('BPIOID').Value;
              FieldByName('IOType').Value := '车间领用';
              FieldByName('FromCoNo').Value := frmBSPrtDyeStkSel.CDS_Main.fieldbyname('FromCoNo').Value;
              FieldByName('FromCoName').Value := frmBSPrtDyeStkSel.CDS_Main.fieldbyname('FromCoName').Value;
              FieldByName('StkCoNo').Value := frmBSPrtDyeStkSel.CDS_Main.fieldbyname('StkCoNo').Value;
              FieldByName('StkCoName').Value := frmBSPrtDyeStkSel.CDS_Main.fieldbyname('StkCoName').Value;
              FieldByName('ToCoNo').Value := frmBSPrtDyeStkSel.CDS_Main.fieldbyname('StkCoNo').Value;
              FieldByName('ToCoName').Value := frmBSPrtDyeStkSel.CDS_Main.fieldbyname('StkCoName').Value;
              FieldByName('P_Name').Value := frmBSPrtDyeStkSel.CDS_Main.fieldbyname('P_Name').Value;
              FieldByName('P_Code').Value := frmBSPrtDyeStkSel.CDS_Main.fieldbyname('P_Code').Value;
              FieldByName('XH').Value := frmBSPrtDyeStkSel.CDS_Main.fieldbyname('XH').Value;
              FieldByName('P_Spec').Value := frmBSPrtDyeStkSel.CDS_Main.fieldbyname('P_Spec').Value;
              FieldByName('QtyUnit').Value := frmBSPrtDyeStkSel.CDS_Main.fieldbyname('QtyUnit').Value;
              FieldByName('Qty').Value := 0;
              FieldByName('BatchNo').Value := frmBSPrtDyeStkSel.CDS_Main.fieldbyname('BatchNo').Value;
              FieldByName('FromOrdNo').Value := frmBSPrtDyeStkSel.CDS_Main.fieldbyname('FromOrdNo').Value;
              FieldByName('Workshop').Value := frmBSPrtDyeStkSel.CDS_Main.fieldbyname('Workshop').Value;
            end;
            frmBSPrtDyeStkSel.CDS_Main.Delete;
          end;
        end;
        frmBSPrtDyeStkSel.CDS_Main.EnableControls;
      end;
    end;
  finally
    frmBSPrtDyeStkSel.Free;
  end;

end;

procedure TfrmQryBSPrtDyeOutEdit.TbcgsClick(Sender: TObject);
begin
  WriteCxGrid(trim(SELF.Caption) + '-录入窗口', Tv1, gDllFileCaption);

end;

procedure TfrmQryBSPrtDyeOutEdit.TsjClick(Sender: TObject);
begin
  saveLayOut(application, dxLayoutControl_bzplan, ADOQueryBaseCmd, PWideChar(fDllFileName + '|' + SELF.Name + '|' + dxLayoutControl_bzplan.Name + '.ini'));
end;

procedure TfrmQryBSPrtDyeOutEdit.Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        with Self.CDS_1 do
        begin
          Edit;
          FieldByName('ToCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
          FieldByName('ToCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
        end;
      end;
    end;
  finally
    frmCompanySel.Free;
  end;

end;

/// //////////////////////////////////
///
procedure TfrmQryBSPrtDyeOutEdit.TcxButtonEditDel(Sender: TObject);
begin
  TcxButtonEdit(Sender).Text := '';
  TcxButtonEdit(Sender).Properties.LookupItems.Text := '';
end;

end.

