unit U_PBRk;

interface

uses
  Windows, Messages, SysUtils, StrUtils, Variants, Classes, Graphics, Controls,
  Forms, Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, MMSystem,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView, cxGridTableView,
  cxGridDBTableView, cxGridLevel, cxClasses, cxControls, cxGridCustomView,
  cxGrid, StdCtrls, ComCtrls, ExtCtrls, ToolWin, cxButtonEdit, cxDropDownEdit,
  DBClient, ADODB, cxGridCustomPopupMenu, cxGridPopupMenu, cxTextEdit, BtnEdit;

type
  TfrmPBRk = class(TForm)
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    CRTime: TDateTimePicker;
    Label2: TLabel;
    CRType: TComboBox;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1CodeName: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    v1QtyUnit: TcxGridDBColumn;
    v1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    ADOQueryCmd: TADOQuery;
    ADOQueryTmp: TADOQuery;
    DataSource1: TDataSource;
    CDS_Main: TClientDataSet;
    ToolBar2: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    v1Code: TcxGridDBColumn;
    CRNO: TEdit;
    Label8: TLabel;
    v1Price: TcxGridDBColumn;
    v1Money: TcxGridDBColumn;
    Label6: TLabel;
    Label9: TLabel;
    fromFactoryName: TBtnEditA;
    Label3: TLabel;
    kuwei: TBtnEditA;
    Label4: TLabel;
    FactoryName: TBtnEditA;
    v1MF: TcxGridDBColumn;
    v1KZ: TcxGridDBColumn;
    ToolButton3: TToolButton;
    v1CloOrderNo: TcxGridDBColumn;
    v1BatchNo: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure v1P_ColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1P_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1QtyPropertiesEditValueChanged(Sender: TObject);
    procedure fromFactoryNameBtnClick(Sender: TObject);
    procedure kuweiBtnClick(Sender: TObject);
    procedure v1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1NotePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure v1PRTSpecPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure FactoryNameBtnClick(Sender: TObject);
    procedure v1QtyUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure ToolButton3Click(Sender: TObject);
  private
    procedure InitGrid();
    procedure SetFrom();
    function savedate(): Boolean;
    function CheckData(): Boolean;
    { Private declarations }
  public
    fkeyNO: string;
    FType: integer;
    fCRType, FZZType: string;
    { Public declarations }
  end;

var
  frmPBRk: TfrmPBRk;

implementation

uses
  U_DataLink, U_Fun10, U_ZDYHelp, U_ZdyAttachment, U_ZdyAttachGYS,
  U_ClothGMListSel, U_ClothSCListSel, U_CPManageSel;
{$R *.dfm}

function TfrmPBRk.CheckData(): Boolean;
begin
  try
    if Trim(CRType.Text) = '' then
      raise Exception.Create('入库类型不能为空！');
    if trim(CRType.Text) <> '期初入库' then
    begin
      if Trim(fromFactoryName.Text) = '' then
        raise Exception.Create('供应商不能为空！');
      if Trim(FactoryName.Text) = '' then
        raise Exception.Create('存放工厂不能为空！');
    end;
    if CDS_Main.IsEmpty then
      raise Exception.Create('明细不能为空！');
    if CDS_Main.Locate('Code', null, []) then
      raise Exception.Create('产品编号不能为空！');
    if CDS_Main.Locate('Code', '', []) then
      raise Exception.Create('产品编号不能为空！');
    if CDS_Main.Locate('CodeName', null, []) then
      raise Exception.Create('品名不能为空！');
    if CDS_Main.Locate('CodeName', '', []) then
      raise Exception.Create('品名不能为空！');

    if CDS_Main.Locate('PS', null, []) then
      raise Exception.Create('匹数不能为空！');
//    if CDS_Main.Locate('PS',0,[]) then raise Exception.Create('数量不能为0！');
    if CDS_Main.Locate('Qty', null, []) then
      raise Exception.Create('数量不能为空！');
    if CDS_Main.Locate('Qty', 0, []) then
      raise Exception.Create('数量不能为0！');
    if CDS_Main.Locate('qtyUnit', null, []) then
      raise Exception.Create('数量单位不能为空！');
    if CDS_Main.Locate('qtyUnit', '', []) then
      raise Exception.Create('数量单位不能为空！');

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

procedure TfrmPBRk.SetFrom();
var
  i: integer;
begin
  if fType = 0 then
  begin
    if GetLSNo(ADOQueryCmd, fkeyNo, 'PRK', 'CK_PB_CR', 3, 1) = False then
    begin
      Application.MessageBox('取最大号失败!', '提示', 0);
      Exit;
    end;
    CRNO.Text := Trim(fkeyNo);
  end
  else
  begin
//    kuwei.Enabled := false;
    fromFactoryName.Enabled := false;
    FactoryName.Enabled := false;
  end;
end;

function TfrmPBRk.savedate(): Boolean;
var
  MaxNo: string;
begin
  result := false;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Main.DisableControls;
    CDS_Main.First;
    with CDS_Main do
    begin
      while not eof do
      begin
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('select * from CK_PB_CR');
          sql.Add('where CRID=' + quotedstr(Trim(CDS_Main.FieldByName('CRID').AsString)));
          open;
        end;
        if ADOQueryCmd.IsEmpty then
        begin
          if GetLSNo(ADOQueryTmp, MaxNo, 'PR', 'CK_PB_CR', 3, 1) = False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取最大号失败!', '提示', 0);
            Exit;
          end;
          ADOQueryCmd.Append;
          ADOQueryCmd.FieldByName('Filler').Value := trim(DName);
          ADOQueryCmd.FieldByName('FillTime').Value := dateTimetostr(SGetServerDateTime(adoqueryTmp));
        end
        else
        begin
          MaxNo := Trim(CDS_Main.FieldByName('CRID').AsString);
          ADOQueryCmd.Edit;
          ADOQueryCmd.FieldByName('editer').Value := trim(dName);
          ADOQueryCmd.FieldByName('editTime').Value := SGetServerDateTime(adoqueryTmp);
        end;
        ADOQueryCmd.FieldByName('CRNO').Value := trim(CRNO.Text);
        //ADOQueryCmd.FieldByName('hcartype').Value := trim(hcartype.Text);
        //ADOQueryCmd.FieldByName('hcarno').Value := trim(hcarno.Text);
        ADOQueryCmd.FieldByName('FactoryName').Value := trim(FactoryName.Text);
        ADOQueryCmd.FieldByName('fromFactoryName').Value := trim(fromFactoryName.Text);
        ADOQueryCmd.FieldByName('kuwei').Value := trim(kuwei.Text);
        ADOQueryCmd.FieldByName('CRID').Value := trim(MaxNo);
        ADOQueryCmd.FieldByName('CRTime').Value := formatdateTime('yyyy-MM-dd', CRTIme.Date);
        ADOQueryCmd.FieldByName('CKName').Value := trim('坯布仓库');
        ADOQueryCmd.FieldByName('CRType').Value := trim(CRType.Text);
        ADOQueryCmd.FieldByName('CRFlag').Value := '入库';
        ADOQueryCmd.FieldByName('CRQtyFlag').Value := 1;

        RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Main, 'CK_PB_CR', 0);

        ADOQueryCmd.FieldByName('CloMainId').Value := CDS_Main.FieldByName('CloMainId').Value;
        ADOQueryCmd.FieldByName('CloSubId').Value := CDS_Main.FieldByName('CloSubId').Value;

        ADOQueryCmd.Post;

        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          SQL.Add('exec P_Up_PBKc ' + quotedstr(trim(MaxNo)));
          Open;
        end;
        if Trim(ADOQueryCmd.FieldByName('Msg').AsString) = '操作失败' then
        begin
          CDS_Main.EnableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('入库数量少于出库数量!', '提示', 0);
          Exit;
        end;
        CDS_Main.Edit;
        CDS_Main.FieldByName('CRID').Value := Trim(MaxNo);
        CDS_Main.Post;
        Next;
      end;
    end;
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    result := true;
  except
    CDS_Main.EnableControls;
    ADOQueryCmd.Connection.RollbackTrans;
  end;
end;

procedure TfrmPBRk.InitGrid();
var
  i: Integer;
begin
  try
    with adoqueryTmp do
    begin
      close;
      sql.Clear;
      sql.Add('select A.*  from CK_PB_CR A');
      sql.Add('where  A.CRID=' + quotedstr(trim(fkeyNO)));
      open;
    end;
    SCreateCDS20(adoqueryTmp, CDS_Main);
    SInitCDSData20(adoqueryTmp, CDS_Main);
    if not CDS_Main.IsEmpty then
    begin
      CRTime.Date := CDS_Main.fieldbyname('CRTime').AsDateTime;
      CRType.ItemIndex := CRType.Items.IndexOf(trim(CDS_Main.fieldbyname('CRType').AsString));
      CRNO.Text := CDS_Main.fieldbyname('CRNO').asstring;
      fromFactoryName.Text := trim(CDS_Main.fieldbyname('fromFactoryName').AsString);
      FactoryName.Text := trim(CDS_Main.fieldbyname('FactoryName').AsString);
      kuwei.Text := trim(CDS_Main.fieldbyname('kuwei').AsString);
    end;
  finally
  end;
end;

procedure TfrmPBRk.FormDestroy(Sender: TObject);
begin
  frmPBRk := nil;
end;

procedure TfrmPBRk.FormCreate(Sender: TObject);
begin
  CRTime.Date := SGetServerDate10(ADOQueryTmp);
end;

procedure TfrmPBRk.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  writeCxGrid(self.Caption + tv1.Name + '1', Tv1, '面料仓库');
  Action := cahide;
end;

procedure TfrmPBRk.FormShow(Sender: TObject);
begin
  readCxGrid(self.Caption + tv1.Name + '1', Tv1, '面料仓库');
  InitGrid();
  SetFrom();

end;

procedure TfrmPBRk.TBSaveClick(Sender: TObject);
begin

  CRTime.SetFocus;
  if CheckData() = false then
    exit;

  if savedate() then
  begin
    application.MessageBox('数据保存成功！', '提示信息');
    ModalResult := 1;
  end
  else
  begin
    application.MessageBox('数据保存失败！', '提示信息', MB_ICONERROR);
  end;
end;

procedure TfrmPBRk.ToolButton1Click(Sender: TObject);
begin
  with CDS_Main do
  begin
    append;
    if trim(FZZType) <> '全部' then
      FieldbyName('ZZType').Value := trim(FZZType);
    FieldbyName('QtyUnit').Value := 'Kg';
    FieldbyName('Price').Value := 0;
    FieldbyName('Money').Value := 0;
    post;
  end;
end;

procedure TfrmPBRk.ToolButton2Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  if Trim(CDS_Main.FieldByName('CRID').AsString) = '' then
  begin
    CDS_Main.Delete;
  end
  else
  begin
    ADOQueryCmd.Connection.BeginTrans;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('update CK_ML_CR set Valid=''N'' where CRID=' + quotedstr(Trim(CDS_Main.FieldByName('CRID').AsString)));
      ExecSQL;
    end;

    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      SQL.Add('exec P_Up_PBKc ' + quotedstr(Trim(CDS_Main.FieldByName('CRID').AsString)));
      Open;
    end;

    if Trim(ADOQueryCmd.FieldByName('Msg').AsString) = '操作失败' then
    begin
      CDS_Main.EnableControls;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('出库数量超出库存数量!', '提示', 0);
      Exit;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    CDS_Main.Delete;
  end;
end;

procedure TfrmPBRk.TBCloseClick(Sender: TObject);
begin
  close;
end;

procedure TfrmPBRk.v1P_ColorPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'OrdColor';
      flagname := '颜色';
      V1Name.Caption := '中文名称';
      V1Note.Caption := '英文名称';
      MainType := Trim(DName);
      fnote := True;
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('P_Color').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.CDS_Main.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmPBRk.v1P_CodePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmCPManageSel := TfrmCPManageSel.Create(self);
  with frmCPManageSel do
  begin
    if showmodal = 1 then
    begin
      Self.CDS_Main.Edit;
      Self.CDS_Main.fieldbyname('Code').Value := CDS_Main.fieldbyname('CYNo').asstring;
      Self.CDS_Main.fieldbyname('CodeName').Value := CDS_Main.fieldbyname('CYName').asstring;
      Self.CDS_Main.fieldbyname('SPEC').Value := CDS_Main.fieldbyname('CYSpec').asstring;
      Self.CDS_Main.fieldbyname('MF').Value := CDS_Main.fieldbyname('CYMF').asstring;
      Self.CDS_Main.fieldbyname('KZ').Value := CDS_Main.fieldbyname('CYKZ').asstring;
      Self.CDS_Main.Post;
    end;
    free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmPBRk.v1QtyPropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  with CDS_Main do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    FieldbyName('Money').Value := format('%.2f', [FieldByName('Qty').AsFloat * FieldByName('Price').AsFloat]);
    Post;
  end;
  tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmPBRk.fromFactoryNameBtnClick(Sender: TObject);
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        fromFactoryName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmPBRk.kuweiBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'kuwei';
      flagname := '库位';
      if ShowModal = 1 then
      begin
        TEdit(Sender).Text := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmPBRk.v1Column1PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  frmClothSCListSel := TfrmClothSCListSel.Create(self);
  with frmClothSCListSel do
  begin
    if showmodal = 1 then
    begin
      Self.CDS_Main.Edit;
      Self.CDS_Main.fieldbyname('CloOrderno').Value := Order_Main.fieldbyname('ConNo').asstring;
      Self.CDS_Main.fieldbyname('CloMainId').Value := Order_Main.fieldbyname('MainId').asstring;
      Self.CDS_Main.fieldbyname('CloSubId').Value := Order_Main.fieldbyname('SubId').asstring;
      Self.CDS_Main.fieldbyname('Code').Value := Order_Main.fieldbyname('C_Code').asstring;
      Self.CDS_Main.fieldbyname('CodeName').Value := Order_Main.fieldbyname('C_CodeName').asstring;
      Self.CDS_Main.fieldbyname('SPEC').Value := Order_Main.fieldbyname('C_Spec').asstring;
      Self.CDS_Main.fieldbyname('MF').Value := Order_Main.fieldbyname('MF').asstring;
      Self.CDS_Main.fieldbyname('KZ').Value := Order_Main.fieldbyname('KZ').asstring;
      Self.CDS_Main.Post;
    end;
    free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmPBRk.v1NotePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'WJGRKNote';
      flagname := '备注';

      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('Note').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
        Self.CDS_Main.Post;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
  self.tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmPBRk.v1PRTSpecPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'flSPEC';
      flagname := '辅料规格';
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('SPEC').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmPBRk.FactoryNameBtnClick(Sender: TObject);
begin
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        FactoryName.Text := Trim(CDS_HZ.fieldbyname('CoName').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmPBRk.v1QtyUnitPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmZDYHelp := TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag := 'FLqtyUnit';
      flagname := '单位';
      if ShowModal = 1 then
      begin
        Self.CDS_Main.Edit;
        Self.CDS_Main.FieldByName('qtyUnit').Value := Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);

      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmPBRk.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin

  if trim(CDS_Main.FieldByName('CRID').AsString) <> '' then
  begin
    v1CloOrderNo.Options.Editing := false;
    v1BatchNo.Options.Editing := false;
    v1Note.Options.Editing := false;
    v1Code.Options.Editing := false;
    v1CodeName.Options.Editing := false;
    v1Column1.Options.Editing := false;
    v1MF.Options.Editing := false;
    v1KZ.Options.Editing := false;

    v1QtyUnit.Options.Editing := false;
  end;
end;

procedure TfrmPBRk.ToolButton3Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  CopyAddRowCDS(CDS_Main);
  with CDS_Main do
  begin
    edit;
    FieldbyName('CRID').Value := '';
    post;
  end;

end;

end.

