unit U_BSPrtDyeejInEdit;

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, StdCtrls, ExtCtrls,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, 
   dxDateRanges, dxBarBuiltInMenu, U_BaseInput,
  System.ImageList, Vcl.ImgList, cxContainer, cxMaskEdit, 
  dxScrollbarAnnotations, dxSkinsCore, dxSkinsDefaultPainters, cxImageList,
  dxSkinWXI;

type
  TfrmBSPrtDyeejInEdit = class(TfrmBaseInput)
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column2: TcxGridDBColumn;
    v1SPName: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ToolBar1: TToolBar;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBSave: TToolButton;
    TBClose: TToolButton;
    DS_1: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    GPM_1: TcxGridPopupMenu;
    v1Column12: TcxGridDBColumn;
    v1FactoryName: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1SPSpec: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Panel1: TPanel;
    Label5: TLabel;
    ZIONO: TcxTextEdit;
    Label12: TLabel;
    IOType: TComboBox;
    FromCoName: TcxButtonEdit;
    Label1: TLabel;
    FromCono: TcxButtonEdit;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBAddClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure v1Column14PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure FormDestroy(Sender: TObject);
    procedure Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1Column3PropertiesEditValueChanged(Sender: TObject);
    procedure FromCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column6PropertiesEditValueChanged(Sender: TObject);
  private
    { Private declarations }
    function SaveCKData(): Boolean;
  public
    { Public declarations }
    FBCId, canshu3, FSTKName, FKHName: string;
  end;

var
  frmBSPrtDyeejInEdit: TfrmBSPrtDyeejInEdit;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_ProductInfoSel, U_CompanySel;

{$R *.dfm}

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

procedure TfrmBSPrtDyeejInEdit.TBAddClick(Sender: TObject);
begin

  try
    frmProductInfoSel := TfrmProductInfoSel.Create(Application);
    with frmProductInfoSel do
    begin
      FSTKName := '染化料';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Append;
          FieldByName('IOTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
          FieldByName('StkCoNo').Value := '本厂仓库';
          FieldByName('StkCoName').Value := '本厂仓库';
          FieldByName('IOType').Value := '采购入库';
          FieldByName('shuilv').Value := 13;
          FieldByName('P_Code').Value := frmProductInfoSel.CDS_1.fieldbyname('C_SCCODE').value;
          FieldByName('P_Name').Value := frmProductInfoSel.CDS_1.fieldbyname('P_Name').value;
          FieldByName('P_Spec').Value := frmProductInfoSel.CDS_1.fieldbyname('P_Spec').value;
          FieldByName('QtyUnit').Value := frmProductInfoSel.CDS_1.fieldbyname('QtyUnit').value;
          FieldByName('Price').Value := frmProductInfoSel.CDS_1.fieldbyname('Price').value;
        end;
      end;
    end;
  finally
    frmProductInfoSel.Free;
  end;


//  if CDS_Sub.IsEmpty = False then
//  begin
//    CopyAddRowCDS(CDS_Sub);
//    with CDS_Sub do
//    begin
//      Edit;
//      FieldByName('BPIOID').Value := null;
//      FieldByName('StkCoNo').Value := '本厂仓库';
//      FieldByName('StkCoName').Value := '本厂仓库';
//      post;
//    end;
//  end
//  else
//  begin
//    with CDS_Sub do
//    begin
//      Append;
//      FieldByName('IOTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
//      FieldByName('StkCoNo').Value := '本厂仓库';
//      FieldByName('StkCoName').Value := '本厂仓库';
//      FieldByName('QtyUnit').Value := '件';
//      Post;
//    end;
//  end;

end;

function TfrmBSPrtDyeejInEdit.SaveCKData(): Boolean;
var
  Maxno, MBPIOID: string;
  fprice: double;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not eof do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from BS_Product_IO where BPIOID=''' + Trim(CDS_Sub.fieldbyname('BPIOID').AsString) + '''');
          open;
        end;
        MBPIOID := Trim(ADOQueryTemp.fieldbyname('BPIOID').AsString);
        if Trim(MBPIOID) = '' then
        begin
          if not GetLSNo(ADOQueryCmd, Maxno, 'PR', 'BS_Product_IO', 3, 1) then
            raise Exception.Create('取入库编号失败！');
        end
        else
        begin
          Maxno := Trim(MBPIOID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from BS_Product_IO where BPIOID=''' + Trim(Maxno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(MBPIOID) = '' then
          begin
            Append;
            FieldByName('Fillid').Value := Trim(Dcode);
            FieldByName('Filler').Value := Trim(DName);
          end
          else
          begin
            Edit;
            FieldByName('Editid').Value := Trim(Dcode);
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDate(ADOQueryTemp);
          end;
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'BS_Product_IO', 0);
//          FieldByName('FromCoNo').Value := CDS_Sub.fieldbyname('FromCoNo').Value;

          FieldByName('iotype').Value := Trim(iotype.Text);
          FieldByName('FromCoNo').Value := Trim(FromCoNo.Text);
          FieldByName('FromConame').Value := Trim(FromConame.Text);

          FieldByName('StkCoNo').Value := CDS_Sub.fieldbyname('StkCoNo').Value;
          FieldByName('BPIOID').Value := Trim(Maxno);
          FieldByName('STKID').Value := Trim(Maxno);
          FieldByName('ZIONO').Value := Trim(ZIONO.Text);
          FieldByName('STKName').Value := FSTKName;
          FieldByName('IOFlag').Value := '入库';
          FieldByName('IOQtyFlag').Value := 1;
          Post;
        end;
        with ADOQueryCmd 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(Maxno) + '''');
          ExecSQL;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select stkQty   from BS_Product_IO where BPIOID=''' + Trim(Maxno) + '''');
          Open;
        end;
        if ADOQueryTemp.FieldByName('stkQty').Value < 0 then
          raise Exception.Create('入库数量小于出库数量不能保存!');

        fprice := strtofloatdef(CDS_Sub.fieldbyname('price').AsString, 0);
        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(Maxno));
          sql.Add(',''入库''   ');
          sql.Add(',' + quotedstr(DName));
          sql.Add(',' + quotedstr(trim(self.Caption)));
          sql.Add(',' + quotedstr(Maxno));
          sql.Add(')  ');
          execsql;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update BS_Product_Info Set  Price=' + FloatToStr(fprice));
          sql.Add(' where P_Code=''' + Trim(CDS_Sub.fieldbyname('P_Code').AsString) + '''');
          sql.Add('and stkname=''染化料''');
          sql.Add('insert into SY_SysLog(operor,opertime,Model,acction,opevent,result) values( ');
          sql.Add(' ' + quotedstr(trim(DName)));
          sql.Add(',getdate() ');
          sql.Add(',' + quotedstr(trim(self.Caption)));
          sql.Add(',' + quotedstr(trim('染化料单价修改')));
          sql.Add(',' + quotedstr(trim('编号：' + trim(CDS_Sub.FieldByName('P_Code').AsString) + '改为' + trim(FloatToStr(fprice)))));
          sql.Add(',' + quotedstr(trim('成功')));
          sql.Add(')');
          ExecSQL;
        end;
        Edit;
        FieldByName('BPIOID').Value := Trim(Maxno);
        Post;
        Next;
      end;
    end;

    CDS_Sub.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;

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

end;

procedure TfrmBSPrtDyeejInEdit.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmBSPrtDyeejInEdit.FormDestroy(Sender: TObject);
begin
  inherited;
  frmBSPrtDyeEJInEdit := nil;
end;

procedure TfrmBSPrtDyeejInEdit.FormShow(Sender: TObject);
var
  fsj, maxId: string;
begin
  inherited;
  ReadCxGrid(FSTKName + '入库登记', Tv1, '机物料仓库');
  with ADOQueryTemp 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(ADOQueryTemp, CDS_Sub);
  SInitCDSData(ADOQueryTemp, CDS_Sub);
  if ADOQueryTemp.FieldByName('ZIONO').AsString = '' then
  begin
    if GetLSNo(ADOQueryCmd, maxId, 'RI', 'BS_Product_IO', 3, 1) = False then
    begin
      Application.MessageBox('取最大号失败!', '提示', 0);
      Exit;
    end;
    ZIONO.Text := maxId;
  end
  else
  begin
    ZIONO.Text := ADOQueryTemp.FieldByName('ZIONO').AsString;
    IOType.ItemIndex := IOType.Items.IndexOf(trim(ADOQueryTemp.fieldbyname('IOType').AsString));
    FromCoName.Text := ADOQueryTemp.FieldByName('FromCoName').AsString;
    FromCono.Text := ADOQueryTemp.FieldByName('FromCono').AsString;
  end;
//  fsj := 'select name=CJName ,Code=null from CheJian';
//  SInitCxGridComboBoxBySql(ADOQueryTemp, v1Column7, fsj, 0, True, '');
end;

procedure TfrmBSPrtDyeejInEdit.FromCoNamePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  inherited;
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        FromCoNo.Text := Trim(CDS_1.fieldbyname('CoCode').AsString);
        FromCoName.Text := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
//        with Self.CDS_Sub do
//        begin
//          Edit;
//          FieldByName('FromCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
//          FieldByName('FromCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
//        end;
      end;
    end;
  finally
    frmCompanySel.Free;
  end;
end;

procedure TfrmBSPrtDyeejInEdit.TBDelClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if Trim(CDS_Sub.fieldbyname('BPIOID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Fin_Flow_Judge ');
      Sql.Add(' @FFIDS=' + quotedstr(Trim(CDS_Sub.fieldbyname('BPIOID').AsString)));
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));
      Open;
    end;
    if ADOQueryTemp.FieldByName('intReturn').AsInteger = -1 then
    begin
      Application.MessageBox(PChar(ADOQueryTemp.fieldbyname('ShowMsg').AsString), '提示', 0);
      exit;
    end;
    try
      ADOQueryCmd.Connection.BeginTrans;
      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(CDS_Sub.fieldbyname('BPIOID').AsString));
        sql.Add(',''撤销入库''   ');
        sql.Add(',' + quotedstr(DName));
        sql.Add(',' + quotedstr(trim(self.Caption)));
        sql.Add(',' + quotedstr(CDS_Sub.fieldbyname('BPIOID').AsString));
        sql.Add(')  ');
        execsql;
      end;
      with ADOQueryCmd do
      begin
        Close;
        Sql.Clear;
        Sql.Add('exec P_BS_Product_In_Del ');
        Sql.Add(' @BPIOIDS=' + quotedstr(Trim(CDS_Sub.fieldbyname('BPIOID').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)));
      ADOQueryCmd.Connection.CommitTrans;
      CDS_Sub.Delete;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    end;
  end
  else
  begin
    CDS_Sub.Delete;
  end;

end;

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

  if CDS_Sub.Locate('Qty', null, []) = True then
  begin
    Application.MessageBox('数量不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('QtyUnit', null, []) = True then
  begin
    Application.MessageBox('数量单位不能为空!', '提示', 0);
    Exit;
  end;

  if SaveCKData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    ModalResult := 1;
    Exit;
  end;
end;

procedure TfrmBSPrtDyeejInEdit.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(FSTKName + '入库登记', Tv1, '机物料仓库');

end;

procedure TfrmBSPrtDyeejInEdit.Tv1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '客户';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('StkCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
          FieldByName('StkCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
        end;
      end;
    end;
  finally
    frmCompanySel.Free;
  end;

end;

procedure TfrmBSPrtDyeejInEdit.Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmProductInfoSel := TfrmProductInfoSel.Create(Application);
    with frmProductInfoSel do
    begin
      FSTKName := Self.FSTKName;
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('P_Code').Value := frmProductInfoSel.CDS_1.fieldbyname('P_Code').value;
          FieldByName('P_Name').Value := frmProductInfoSel.CDS_1.fieldbyname('P_Name').value;
          FieldByName('P_Spec').Value := frmProductInfoSel.CDS_1.fieldbyname('P_Spec').value;
          FieldByName('QtyUnit').Value := frmProductInfoSel.CDS_1.fieldbyname('QtyUnit').value;
          FieldByName('Price').Value := frmProductInfoSel.CDS_1.fieldbyname('Price').value;
        end;
      end;
    end;
  finally
    frmProductInfoSel.Free;
  end;
end;

procedure TfrmBSPrtDyeejInEdit.Tv1Column3PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQty, FPrice, FMxMoney: double;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  with CDS_SUB do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
  end;
  with CDS_SUB do
  begin
    Edit;
    FQty := FieldByName('Qty').AsFloat;
    FPrice := FieldByName('Price').AsFloat;
    Post;
  end;
  FMxMoney := ROUNDFLOAT(FQty * FPrice, 2);
  with CDS_SUB do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    FieldbyName('sjMoney').Value := FMxMoney;
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmBSPrtDyeejInEdit.Tv1Column6PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FQty, FPrice, FMxMoney: double;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  with CDS_SUB do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
  end;

  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmBSPrtDyeejInEdit.v1Column14PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin

  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('FromCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
          FieldByName('FromCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
        end;
      end;
    end;
  finally
    frmCompanySel.Free;
  end;

end;

procedure TfrmBSPrtDyeejInEdit.v1Column5PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'StkPosition';
      flagname := '库位';
      MainType := FSTKName;
      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;

end.

