unit U_Kalc_DM;

interface

uses
  SysUtils, Classes, DB, kbmMemTable, U_BaseConstants,
  U_BaseCommon, siComp;

type
  TKalc_DM = class(TDataModule)
    Cable_MemTable: TkbmMemTable;
    ElemForCabcanal_MemTable: TkbmMemTable;
    Cable_Source: TDataSource;
    ElemForCabcanal_Source: TDataSource;
    CableCanalMemTable: TkbmMemTable;
    TypeOfElementCC_MemTable: TkbmMemTable;
    TypeUseCable_MemTable: TkbmMemTable;
    TypeUseCable_Source: TDataSource;
    TypeOfElementCC_Source: TDataSource;
    CableCanal_Source: TDataSource;
    AllComponent_MemTable: TkbmMemTable;
    AllComponent_DataSource: TDataSource;
    CostRM_MemTable: TkbmMemTable;
    CostRM_Source: TDataSource;
    CostPK_MemTable: TkbmMemTable;
    CostPK_Source: TDataSource;
    CostPP__Source: TDataSource;
    CostPP_MemTable: TkbmMemTable;
    CostOrg_Source: TDataSource;
    CostOrg_MemTable: TkbmMemTable;
    CostShelf_Source: TDataSource;
    CostShelf_MemTable: TkbmMemTable;
    ShelfForFloor_Source: TDataSource;
    ShelfForFloor_MemTable: TkbmMemTable;
    DopEq_MemTable: TkbmMemTable;
    DopEq_DataSource: TDataSource;
    MissingPK_MemTable: TkbmMemTable;
    MissingPK_Source: TDataSource;
    CCFloor_MemTable: TkbmMemTable;
    CCFloor_Source: TDataSource;
    CCUnderCeil_MemTable: TkbmMemTable;
    CCUnderCeil_Source: TDataSource;
    ElPointFix_MemTable: TkbmMemTable;
    ElPointFix_Source: TDataSource;
    HaracterProject_MemTable: TkbmMemTable;
    HaracterProject_Source: TDataSource;
    PriceWorks_MemTable: TkbmMemTable;
    PriceWorks_DataSource: TDataSource;
    siLang1: TsiLang;
    procedure CostShelf_MemTableAfterPost(DataSet: TDataSet);
  private
    { Private declarations }
    Procedure InitTypeOfElementCC_MemTable;
    Procedure InitTypeUseCable_MemTable;
    Procedure InitMissingPK_MemTable;
  public
    { Public declarations }
    Constructor Create(AOwner: TComponent);
    procedure AddShelfForFloor(AFloor: string);
    function CmpPerFromCostPk_MemTable(var AOstPer: integer):integer;
    function CmpPerFromCCFLoor_MemTable: integer;
    function CmpPerFromCCUnderCeil_MemTable: integer;
    function CmpPerFromElPointFix_MemTable: integer;
    procedure EnableMasterDetail;
    procedure DisebleMasterDetail;
    procedure InitPriceWorks_MemTable;
  end;

const //тип кабеля
  tcuRaspred = 0;
  tcuMagistral = 1;
    //предназначение патчкордов
  mpkRM = 1;
  mpkCross = 2;

Procedure AddRecordInMemTable(ATable: TkbmMemTable; AName : string = ''; AArticul: string = '';
                              APrice: double = 0; AKolvo: integer = -1; APercent: integer = -1; AId_Missing: integer = -1);



implementation



uses U_Kalc, u_Main;
{$R *.dfm}
{ TKalc_DM }


Procedure TKalc_DM.InitMissingPK_MemTable;
const
  MasOfTypes: array [1..2] of string = (
    cKalc_DM_Msg1,
    cKalc_DM_Msg2);
var
  i: integer;
begin
  with MissingPK_MemTable do
  begin
    FieldDefs.Add('Id', ftInteger);
    FieldDefs.Add('Name', ftString, 255);
    Active := true;
    for i := 1 to 2 do
    begin
      Append;
      FieldByName('ID').AsInteger := i;
      FieldByName('Name').AsString := MasOfTypes[i];
      Post;
    end;
  end
end;
//****************************************************

Procedure TKalc_DM.InitTypeOfElementCC_MemTable;
const
  MasOfTypes: array [1..5] of string = (
    cKalc_DM_Msg3,
    cKalc_DM_Msg4,
    cKalc_DM_Msg5,
    cKalc_DM_Msg6,
    cKalc_DM_Msg7);
var
  i: integer;
begin
  with TypeOfElementCC_MemTable do
  begin
    FieldDefs.Add('Id',ftInteger);
    FieldDefs.Add('Name',ftString, 255);
    Active := true;
    for i := 1 to 5 do
    begin
      Append;
      FieldByName('ID').AsInteger := i;
      FieldByName('Name').AsString := MasOfTypes[i];
      Post;
    end;
  end
end;

//*****************************************************************
Procedure TKalc_DM.InitTypeUseCable_MemTable;
const
  MasOfTypes: array [1..2] of string = (
    cKalc_DM_Msg8,
    cKalc_DM_Msg9);
var
  i: integer;
begin

  with TypeUseCable_MemTable do
  begin
    FieldDefs.Add('Id',ftAutoInc);
    FieldDefs.Add('TypeUse',ftString, 255);
    Active := true;
    for i := 1 to 2 do
    begin
      Append;
      FieldByName('TypeUse').AsString := MasOfTypes[i];
      Post;
    end;
    TypeUseCable_Source.Enabled := True;
  end
end;

constructor TKalc_DM.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

  CostRM_MemTable.FieldDefs.Add('Id',ftAutoInc);
  CostRM_MemTable.FieldDefs.Add('Name',ftString, 255);
  CostRM_MemTable.FieldDefs.Add('Articul',ftString, 50);
  CostRM_MemTable.FieldDefs.Add('Kolvo',ftInteger);
  CostRM_MemTable.FieldDefs.Add('Price',ftFloat);
  CostRM_MemTable.Active := true;

  InitMissingPK_MemTable;

  CostPK_MemTable.FieldDefs.Add('Id',ftAutoInc);
  CostPK_MemTable.FieldDefs.Add('Name',ftString, 255);
  CostPK_MemTable.FieldDefs.Add('Articul',ftString, 50);
  CostPK_MemTable.FieldDefs.Add('Price',ftFloat);
  CostPK_MemTable.FieldDefs.Add('Id_Missing',ftInteger);
  CostPK_MemTable.FieldDefs.Add('Percent',ftFloat);
  CostPK_MemTable.Active := true;

  CostPP_MemTable.FieldDefs.Add('Id',ftAutoInc);
  CostPP_MemTable.FieldDefs.Add('Name',ftString, 255);
  CostPP_MemTable.FieldDefs.Add('Articul',ftString, 50);
  CostPP_MemTable.FieldDefs.Add('Price',ftFloat);
  CostPP_MemTable.Active := true;

  CostOrg_MemTable.FieldDefs.Add('Id',ftAutoInc);
  CostOrg_MemTable.FieldDefs.Add('Name',ftString, 255);
  CostOrg_MemTable.FieldDefs.Add('Articul',ftString, 50);
  CostOrg_MemTable.FieldDefs.Add('Price',ftFloat);
  CostOrg_MemTable.Active := true;

  CostShelf_MemTable.FieldDefs.Add('Id',ftAutoInc);
  CostShelf_MemTable.FieldDefs.Add('Name',ftString, 255);
  CostShelf_MemTable.FieldDefs.Add('Articul',ftString, 50);
  CostShelf_MemTable.FieldDefs.Add('Price',ftFloat);
  CostShelf_MemTable.Active := true;

  ShelfForFloor_MemTable.FieldDefs.Add('Id',ftAutoInc);
  ShelfForFloor_MemTable.FieldDefs.Add('Floor',ftString, 20);
  ShelfForFloor_MemTable.FieldDefs.Add('Id_Shelf',ftInteger);
  ShelfForFloor_MemTable.FieldDefs.Add('Price',ftFloat);
  ShelfForFloor_MemTable.Active := true;
  AddShelfForFloor('1 этаж');

  DopEq_MemTable.FieldDefs.Add('Id',ftAutoInc);
  DopEq_MemTable.FieldDefs.Add('Name',ftString, 255);
  DopEq_MemTable.FieldDefs.Add('Articul',ftString, 50);
  DopEq_MemTable.FieldDefs.Add('Kolvo',ftInteger);
  DopEq_MemTable.FieldDefs.Add('Price',ftFloat);
  DopEq_MemTable.Active := true;

  CableCanalMemtable.FieldDefs.Add('Npp',ftAutoInc);
  CableCanalMemtable.FieldDefs.Add('Name',ftString, 255);
  CableCanalMemTable.FieldDefs.Add('Articul',ftString, 50);
  CableCanalMemtable.FieldDefs.Add('Percent',ftInteger);
  //CableCanalMemtable.Fields[CableCanalMemTable.FieldDefs.Count-1].OnChange := CableCanalMemTableChange;
  //TIntegerField(CableCanalMemTable.FieldDefs.Items[CableCanalMemTable.FieldDefs.Count-1].FieldClass).OnChange := CableCanalMemTableChange;
  CableCanalMemtable.FieldDefs.Add('Price',ftFloat);
  CableCanalMemtable.Active := true;

  CCFLoor_MemTable.FieldDefs.Add('Id',ftAutoInc);
  CCFLoor_MemTable.FieldDefs.Add('Name',ftString, 255);
  CCFLoor_MemTable.FieldDefs.Add('Articul',ftString, 50);
  CCFLoor_MemTable.FieldDefs.Add('Price',ftFloat);
  CCFLoor_MemTable.FieldDefs.Add('Percent',ftFloat);
  CCFLoor_MemTable.Active := true;

  
  CCUnderCeil_MemTable.FieldDefs.Add('Id',ftAutoInc);
  CCUnderCeil_MemTable.FieldDefs.Add('Name',ftString, 255);
  CCUnderCeil_MemTable.FieldDefs.Add('Articul',ftString, 50);
  CCUnderCeil_MemTable.FieldDefs.Add('Price',ftFloat);
  CCUnderCeil_MemTable.FieldDefs.Add('Percent',ftFloat);
  CCUnderCeil_MemTable.Active := true;

  ElPointFix_MemTable.FieldDefs.Add('Id',ftAutoInc);
  ElPointFix_MemTable.FieldDefs.Add('Name',ftString, 255);
  ElPointFix_MemTable.FieldDefs.Add('Articul',ftString, 50);
  ElPointFix_MemTable.FieldDefs.Add('Price',ftFloat);
  ElPointFix_MemTable.FieldDefs.Add('Percent',ftFloat);
  ElPointFix_MemTable.Active := true;


  InitTypeOfElementCC_MemTable;

  ElemForCabcanal_MemTable.FieldDefs.Add('ID', ftAutoInc);
  ElemForCabcanal_MemTable.FieldDefs.Add('Npp',ftInteger);
  ElemForCabcanal_MemTable.FieldDefs.Add('Npp_CC',ftInteger);
  ElemForCabcanal_MemTable.FieldDefs.Add('Articul',ftString, 50);
  ElemForCabcanal_MemTable.FieldDefs.Add('ID_Type',ftString, 255);
  ElemForCabcanal_MemTable.FieldDefs.Add('Price',ftFloat);
  ElemForCabcanal_MemTable.FieldDefs.Add('Rashod',ftFloat);
  ElemForCabcanal_MemTable.MasterSource := CableCanal_Source;
  ElemForCabcanal_MemTable.MasterFields := 'Npp';
  ElemForCabcanal_MemTable.DetailFields := 'Npp_CC';
  ElemForCabcanal_MemTable.Active := true;

  InitTypeUseCable_MemTable;

  Cable_MemTable.FieldDefs.Add('Id',ftAutoInc);
  Cable_MemTable.FieldDefs.Add('Name',ftString, 255);
  Cable_MemTable.FieldDefs.Add('Articul',ftString, 50);
  Cable_MemTable.FieldDefs.Add('Price',ftFloat);
  Cable_MemTable.FieldDefs.Add('Percent',ftInteger);
  Cable_MemTable.FieldDefs.Add('Id_Missing',ftInteger);
  Cable_MemTable.Active := true;


  AllComponent_MemTable.FieldDefs.Add('ID', ftAutoInc);
  AllComponent_MemTable.FieldDefs.Add('Name',ftString, 255);
  AllComponent_MemTable.FieldDefs.Add('Articul',ftString, 50);
  AllComponent_MemTable.FieldDefs.Add('Kolvo',ftInteger);
  AllComponent_MemTable.FieldDefs.Add('Price',ftFloat);
  AllComponent_MemTable.FieldDefs.Add('Cost',ftFloat);
  AllComponent_MemTable.FieldDefs.Add('TypePoz',ftInteger);
  AllComponent_MemTable.Active := True;


  HaracterProject_MemTable.FieldDefs.Add('ID', ftAutoInc);
  HaracterProject_MemTable.FieldDefs.Add('Name',ftString, 100);
  HaracterProject_MemTable.FieldDefs.Add('Znach',ftString, 500);
  HaracterProject_MemTable.Active := True;

  PriceWorks_MemTable.FieldDefs.Add('Id',ftAutoInc);
  PriceWorks_MemTable.FieldDefs.Add('Name',ftString, 255);
  PriceWorks_MemTable.FieldDefs.Add('Price',ftFloat);
  PriceWorks_MemTable.Active := true;
  InitPriceWorks_MemTable;
end;

Procedure AddRecordInMemTable(ATable: TkbmMemTable; AName : string = ''; AArticul: string = '';
                              APrice: double = 0; AKolvo: integer = -1; APercent: integer = -1; AId_Missing: integer = -1);
begin
  with ATable do
  begin
    Append;
    FieldByName('Name').AsString := AName;
    FieldByName('Articul').AsString := AArticul;
    FieldByName('Price').AsFloat := APrice;
    if AKolvo > -1 then
      FieldByName('Kolvo').AsInteger := AKolvo;
    if APercent > -1 then
      FieldByName('Percent').AsInteger := APercent;
    if AId_Missing > -1 then
      FieldByName('id_Missing').AsInteger := AId_Missing;
    Post;
  end;
end;


procedure TKalc_DM.AddShelfForFloor(AFloor: string);
begin
  with ShelfForFloor_MemTable do
  begin
    Append;
    FieldByName('Floor').AsString := AFloor;
    FieldByName('Id_Shelf').AsString :=''; 
    Post;
  end;
end;

function TKalc_DM.CmpPerFromCostPk_MemTable(var AOstPer: integer): integer;
var
  i, Per1, Per2: integer;
begin
  per1 := 100;
  per2 := 100;
  Result := -1;
//  RecNo := CableCanalMemTable.RecNo;
  CostPK_MemTable.First;
  for i := 0 to CostPK_MemTable.RecordCount - 1 do
  begin
    if CostPK_MemTable.FieldByName('id_Missing').AsInteger =  mpkRM then
    begin
      Per1 := Per1 - CostPK_MemTable.FieldByName('Percent').AsInteger;
    end
    else
      Per2 := Per2 - CostPK_MemTable.FieldByName('Percent').AsInteger;
  CostPK_MemTable.Next;
  end;
  AOstPer := 0;
  if (per1 <= 0) and (Per2 > 0)  then
  begin
    Result := 1;
    AOstPer := Per2;
  end
  else
  if (per1 > 0) and (Per2 <= 0)  then
  begin
    Result := 2;
    AOstPer := Per1;
  end
  else
  if (per1 <= 0) and (Per2 <= 0)  then
  begin
    Result := 0;
  end
  else
  begin
    Result := 3;
    AOstPer := Per1;
  end;
end;

function TKalc_DM.CmpPerFromCCFLoor_MemTable: integer;
begin
  Result := 100;
  with CCFloor_MemTable do
  begin
    First;
    while Not Eof do
    begin
      Result := Result - FieldByName('Percent').AsInteger;
      Next;
    end;
    if Result < 0 then
      Result := 0;
  end;

end;

function TKalc_DM.CmpPerFromCCUnderCeil_MemTable: integer;
begin
  Result := 100;
  with CCUnderCeil_MemTable do
  begin
    First;
    while Not Eof do
    begin
      Result := Result - FieldByName('Percent').AsInteger;
      Next;
    end;
    if Result < 0 then
      Result := 0;
  end;
end;

function TKalc_DM.CmpPerFromElPointFix_MemTable: integer;
begin
  Result := 100;
  with ElPointFix_MemTable do
  begin
    First;
    while Not Eof do
    begin
      Result := Result - FieldByName('Percent').AsInteger;
      Next;
    end;
    if Result < 0 then
      Result := 0;
  end;
end;

procedure TKalc_DM.DisebleMasterDetail;
begin
  ElemForCabcanal_MemTable.MasterSource := Nil;
end;

procedure TKalc_DM.EnableMasterDetail;
begin
  ElemForCabcanal_MemTable.MasterSource := CableCanal_Source;
end;

procedure TKalc_DM.CostShelf_MemTableAfterPost(DataSet: TDataSet);
begin
//
end;

procedure TKalc_DM.InitPriceWorks_MemTable;
const
  MasNamesOfWorks: array [1..12] of string = (
    cKalc_DM_Msg10 ,
    cKalc_DM_Msg11,
    cKalc_DM_Msg12,
    cKalc_DM_Msg13,
    cKalc_DM_Msg14,
    cKalc_DM_Msg15,
    cKalc_DM_Msg16,
    cKalc_DM_Msg17,
    cKalc_DM_Msg18,
    cKalc_DM_Msg19,
    cKalc_DM_Msg20,
    cKalc_DM_Msg21);
var
  i: integer;
begin
  with PriceWorks_MemTable do
  begin
    Active := false;
    Active := true;
    for i := 1 to 12 do
    begin
      Append;
      FieldByName('ID').AsInteger := i;
    //  if i > 1 then
        FieldByName('Name').AsString := MasNamesOfWorks[i];
   //   else
//        FieldByName('Name').AsString := MasNamesOfWorks[i]
//                                         + strCalcCapt22  + GetNameUOM(F_NormBase.FUOM, true, false) + cKalc_DM_Msg22;
      FieldByName('Price').AsFloat := 0;
      Post;
    end;
  end
end;

end.
