123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516 |
- {
- ***************************************
- Note: Add the Third Part Bills when Export .smb File
- **************************************
- }
- unit ExportDecorateUnit;
- interface
- uses
- Classes,
- ADODB,
- DataBase,
- ConstTypeUnit,
- Provider,
- ConstMethodUnit,
- DBClient,
- DB,
- ConstVarUnit,
- ScFileArchiver;
- type
- TBillsConfig = class
- private
- FBillsStrings: TStrings;
- FRecordList: TList;
- public
- constructor Create(const aFileName: string);
- destructor Destroy; override;
- procedure ResolveStrings;
- end;
- TDecorator = class
- private
- FBillsConfig: TBillsConfig;
- public
- constructor Create(const aCfgFileName: string);
- destructor Destroy; override;
- procedure Decorate; virtual; abstract;
- end;
- TCreateDecorator = class(TDecorator)
- private
- FBillsData: TDMDataBase;
- procedure ClearExprs;
- procedure Save;
- procedure WriteBillsAndExprs;
- public
- constructor Create(aBillsData: TDMDataBase; const aCfgFileName: string);
- procedure Decorate; override;
- end;
- TBillsDecorator = class(TDecorator)
- private
- FArchiver : TScProjectFileArchiver;
- FBillsTable : TADOTable;
- FBillsDsp : TDataSetProvider;
- FBillsCds : TClientDataSet;
- FDrawQtyTable : TADOTable;
- FDrawQtyDsp : TDataSetProvider;
- FDrawQtyCds : TClientDataSet;
- FProjProperty : TADOTable;
- FProjPropertyDsp : TDataSetProvider;
- FProjPropertyCds : TClientDataSet;
- function CanDecorate: Boolean;
- function MaxBillsID: Integer;
- function GetMaxProjPropertyID: Integer;
- procedure ModifyNextID;
- procedure ModifyItemIDs;
- procedure AppendBills;
- procedure ModifyExprs;
- procedure ModifyProjProperty;
- procedure ModifyIsCreatePriceAnalysis;
- public
- constructor Create(const aArFileName, aCfgFileName: string); overload;
- destructor Destroy; override;
- procedure Decorate; override;
- end;
- implementation
- uses SysUtils, ScExprsDM;
- { TBillsConfig }
- constructor TBillsConfig.Create(const aFileName: string);
- begin
- FBillsStrings := TStringList.Create;
- FRecordList := TList.Create;
- FBillsStrings.LoadFromFile(aFileName);
- end;
- destructor TBillsConfig.Destroy;
- begin
- FBillsStrings.Free;
- ClearPointerList(FRecordList);
- FRecordList.Free;
- inherited;
- end;
- procedure TBillsConfig.ResolveStrings;
- var
- iLoop : Integer;
- iID : Integer;
- iErrCode: Integer;
- sBills: string;
- sChain: string;
- rdBillsConfig: PBillsConfigRecord;
- begin
- for iLoop := 0 to FBillsStrings.Count - 1 do
- begin
- sBills := FBillsStrings[iLoop];
- if sBills = '' then Continue;
-
- New(rdBillsConfig);
- sChain := Copy(sBills, 1, Pos(',', sBills) - 1);
- sBills := Copy(sBills, Pos(',', sBills) + 1, Length(sBills));
- Val(sChain, iID, iErrCode);
- rdBillsConfig.ID := iID;
- sChain := Copy(sBills, 1, Pos(',', sBills) - 1);
- sBills := Copy(sBills, Pos(',', sBills) + 1, Length(sBills));
- Val(sChain, iID, iErrCode);
- rdBillsConfig.ParentID := iID;
- sChain := Copy(sBills, 1, Pos(',', sBills) - 1);
- sBills := Copy(sBills, Pos(',', sBills) + 1, Length(sBills));
- Val(sChain, iID, iErrCode);
- rdBillsConfig.NextID := iID;
- sChain := Copy(sBills, 1, Pos(',', sBills) - 1);
- sBills := Copy(sBills, Pos(',', sBills) + 1, Length(sBills));
- rdBillsConfig.Code := Trim(sChain);
- sChain := Copy(sBills, 1, Pos(',', sBills) - 1);
- sBills := Copy(sBills, Pos(',', sBills) + 1, Length(sBills));
- rdBillsConfig.BCode := sChain;
- sChain := Copy(sBills, 1, Pos(',', sBills) - 1);
- sBills := Copy(sBills, Pos(',', sBills) + 1, Length(sBills));
- rdBillsConfig.Name := sChain;
- sChain := Copy(sBills, 1, Pos(',', sBills) - 1);
- sBills := Copy(sBills, Pos(',', sBills) + 1, Length(sBills));
- rdBillsConfig.Units := sChain;
- sChain := Copy(sBills, 1, Pos(',', sBills) - 1);
- sBills := Copy(sBills, Pos(',', sBills) + 1, Length(sBills));
- rdBillsConfig.IsPreDefine := UpperCase(sChain) = 'TRUE';
- rdBillsConfig.Exprs := Trim(sBills);
- rdBillsConfig.ParentModified := False;
- rdBillsConfig.NextModified := False;
- FRecordList.Add(rdBillsConfig);
- end;
- end;
- { TBillsDecorator }
- procedure TBillsDecorator.AppendBills;
- var
- I: Integer;
- rdBillsConfig: PBillsConfigRecord;
- begin
- for I := 0 to FBillsConfig.FRecordList.Count - 1 do
- begin
- rdBillsConfig := FBillsConfig.FRecordList.List^[I];
- if rdBillsConfig.ID = 0 then Continue;
- FBillsCds.Append;
- FBillsCds.FieldByName(SID).AsInteger := rdBillsConfig.ID;
- FBillsCds.FieldByName(sParentID).AsInteger := rdBillsConfig.ParentID;
- FBillsCds.FieldByName(sNextSiblingID).AsInteger := rdBillsConfig.NextID;
- FBillsCds.FieldByName(sCode).AsString := rdBillsConfig.Code;
- FBillsCds.FieldByName(sB_Code).AsString := rdBillsConfig.BCode;
- FBillsCds.FieldByName(sName).AsString := rdBillsConfig.Name;
- FBillsCds.FieldByName(sUnits).AsString := rdBillsConfig.Units;
- FBillsCds.FieldByName(sIsPreDefine).AsBoolean := rdBillsConfig.IsPreDefine;
- FBillsCds.FieldByName('IsCreatePriceAnalysis').AsBoolean := True;
- FBillsCds.Post;
- end;
- end;
- function TBillsDecorator.CanDecorate: Boolean;
- begin
- FBillsCds.EditKey;
- FBillsCds.FieldByName(SID).AsInteger := 3;
- Result := not FBillsCds.GotoKey;
- end;
- constructor TBillsDecorator.Create(const aArFileName, aCfgFileName: string);
- begin
- FArchiver := TScProjectFileArchiver.Create;
- FBillsTable := TADOTable.Create(nil);
- FBillsDsp := TDataSetProvider.Create(nil);
- FBillsCds := TClientDataSet.Create(nil);
- FDrawQtyTable := TADOTable.Create(nil);
- FDrawQtyDsp := TDataSetProvider.Create(nil);
- FDrawQtyCds := TClientDataSet.Create(nil);
- FProjProperty := TADOTable.Create(nil);
- FProjPropertyDsp := TDataSetProvider.Create(nil);
- FProjPropertyCds := TClientDataSet.Create(nil);
- FBillsDsp.UpdateMode := upWhereKeyOnly;
- FDrawQtyDsp.UpdateMode := upWhereKeyOnly;
- FProjPropertyDsp.UpdateMode := upWhereKeyOnly;
- FArchiver.FileName := aArFileName;
- if FArchiver.OpenFile then
- begin
- FBillsTable.Connection := FArchiver.Connection;
- FBillsTable.TableName := 'Bills';
- FBillsDsp.DataSet := FBillsTable;
- FBillsCds.SetProvider(FBillsDsp);
- FBillsCds.Active := True;
- FBillsCds.IndexFieldNames := SID;
- FDrawQtyTable.Connection := FArchiver.Connection;
- FDrawQtyTable.TableName := 'Exprs';
- FDrawQtyDsp.DataSet := FDrawQtyTable;
- FDrawQtyCds.SetProvider(FDrawQtyDsp);
- FDrawQtyCds.IndexFieldNames := 'RecdID';
- FDrawQtyCds.Active := True;
- FProjProperty.Connection := FArchiver.Connection;
- FProjProperty.TableName := 'ProjProperty';
- FProjPropertyDsp.DataSet := FProjProperty;
- FProjPropertyCds.SetProvider(FProjPropertyDsp);
- FProjPropertyCds.Open;
- FProjPropertyCds.IndexFieldNames := 'ID';
- end;
- inherited Create(aCfgFileName);
- end;
- procedure TBillsDecorator.Decorate;
- begin
- if FDrawQtyCds.Active then
- { TODO : Ð޸Ĺ«Ê½ }
- ModifyExprs;
- // Ð޸ļÆËãģʽ
- if FProjPropertyCds.Active then
- ModifyProjProperty;
- if FBillsCds.Active then
- begin
- ModifyIsCreatePriceAnalysis;
- FBillsCds.ApplyUpdates(0);
- end;
- if FBillsCds.Active and CanDecorate then
- begin
- { TODO : Read txt }
- FBillsConfig.ResolveStrings;
- { TODO : Modify NextID }
- ModifyNextID;
- ModifyItemIDs;
- { TODO : Append Bills }
- AppendBills;
- { TODO : Save }
- FBillsCds.ApplyUpdates(0);
- end;
- FArchiver.Save;
- end;
- destructor TBillsDecorator.Destroy;
- begin
- FArchiver.Free;
- FBillsTable.Free;
- FBillsDsp.Free;
- FBillsCds.Free;
- FDrawQtyTable.Free;
- FDrawQtyDsp.Free;
- FDrawQtyCds.Free;
- FProjProperty.Free;
- FProjPropertyDsp.Free;
- FProjPropertyCds.Free;
-
- inherited;
- end;
- function TBillsDecorator.MaxBillsID: Integer;
- begin
- FBillsCds.Last;
- Result := FBillsCds.FieldByName(SID).AsInteger;
- end;
- function TBillsDecorator.GetMaxProjPropertyID: Integer;
- begin
- FProjPropertyCds.Last;
- Result := FProjPropertyCds.FieldByName('ID').AsInteger + 1;
- end;
- procedure TBillsDecorator.ModifyExprs;
- begin
- FDrawQtyCds.First;
- while not FDrawQtyCds.Eof do
- begin
- if FDrawQtyCds.FieldByName('MajorID').AsInteger = 2 then
- begin
- FDrawQtyCds.Edit;
- FDrawQtyCds.FieldByName('MajorID').AsInteger := 4;
- FDrawQtyCds.FieldByName('MinorID').AsInteger := 1;
- FDrawQtyCds.Post;
- end;
- FDrawQtyCds.Next;
- end;
- FDrawQtyCds.ApplyUpdates(0);
- end;
- procedure TBillsDecorator.ModifyItemIDs;
- var
- I: Integer;
- J: Integer;
- iMaxID: Integer;
- rdBillsConfig: PBillsConfigRecord;
- rdTemConfig: PBillsConfigRecord;
- begin
- iMaxID := MaxBillsID + 1;
- for I := 0 to FBillsConfig.FRecordList.Count - 1 do
- begin
- rdBillsConfig := FBillsConfig.FRecordList.List^[I];
- if rdBillsConfig.ID >= 100 then
- begin
- for J := 0 to FBillsConfig.FRecordList.Count - 1 do
- begin
- rdTemConfig := FBillsConfig.FRecordList.List^[J];
- if rdTemConfig <> rdBillsConfig then
- begin
- if (not rdTemConfig.ParentModified) and (rdTemConfig.ParentID = rdBillsConfig.ID) then
- begin
- rdTemConfig.ParentID := iMaxID;
- rdTemConfig.ParentModified := True;
- end
- else if (not rdTemConfig.NextModified) and (rdTemConfig.NextID = rdBillsConfig.ID) then
- begin
- rdTemConfig.NextID := iMaxID;
- rdTemConfig.NextModified := True;
- end;
- end;
- end;
- rdBillsConfig.ID := iMaxID;
- Inc(iMaxID);
- end;
- end;
- end;
- procedure TBillsDecorator.ModifyNextID;
- begin
- FBillsCds.EditKey;
- FBillsCds.FieldByName(SID).AsInteger := 2;
- if FBillsCds.GotoKey then
- begin
- FBillsCds.Edit;
- FBillsCds.FieldByName(sNextSiblingID).AsInteger := 3;
- FBillsCds.Post;
- end;
- end;
- procedure TBillsDecorator.ModifyProjProperty;
- var
- iMaxID: Integer;
- begin
- if FProjPropertyCds.Locate('Name', 'ExpressMode', []) then
- begin
- FProjPropertyCds.Edit;
- FProjPropertyCds.FieldByName('ItemValue').Value := '1';
- FProjPropertyCds.Post;
- end
- else
- begin
- iMaxID := GetMaxProjPropertyID;
- FProjPropertyCds.Append;
- FProjPropertyCds.FieldByName('ID').Value := iMaxID;
- FProjPropertyCds.FieldByName('Name').Value := 'ExpressMode';
- FProjPropertyCds.FieldByName('ItemValue').Value := '1';
- FProjPropertyCds.Post;
- end;
- FProjPropertyCds.ApplyUpdates(0);
- end;
- procedure TBillsDecorator.ModifyIsCreatePriceAnalysis;
- begin
- FBillsCds.First;
- while not FBillsCds.Eof do
- begin
- if not FBillsCds.FieldByName('IsCreatePriceAnalysis').AsBoolean then
- begin
- FBillsCds.Edit;
- FBillsCds.FieldByName('IsCreatePriceAnalysis').AsBoolean := True;
- FBillsCds.Post;
- end;
- FBillsCds.Next;
- end;
- end;
- { TDecorator }
- constructor TDecorator.Create(const aCfgFileName: string);
- begin
- FBillsConfig := TBillsConfig.Create(aCfgFileName);
- end;
- destructor TDecorator.Destroy;
- begin
- FBillsConfig.Free;
- inherited;
- end;
- { TCreateDecorator }
- procedure TCreateDecorator.ClearExprs;
- begin
- with FBillsData.DMExprs do
- begin
- cdsOrgExprs.First;
- while not cdsOrgExprs.Eof do
- cdsOrgExprs.Delete;
- end;
- end;
- constructor TCreateDecorator.Create(aBillsData: TDMDataBase;
- const aCfgFileName: string);
- begin
- FBillsData := aBillsData;
- inherited Create(aCfgFileName);
- end;
- procedure TCreateDecorator.Decorate;
- begin
- { Read Txt }
- FBillsConfig.ResolveStrings;
- ClearExprs;
- { write Bills }
- WriteBillsAndExprs;
- Save;
- end;
- procedure TCreateDecorator.Save;
- begin
- FBillsData.cdsBills.ApplyUpdates(0);
- FBillsData.DMExprs.Save;
- end;
- procedure TCreateDecorator.WriteBillsAndExprs;
- procedure WriteBills(ABills: PBillsConfigRecord);
- begin
- with FBillsData do
- begin
- cdsBills.Append;
- cdsBillsID.Value := ABills.ID;
- cdsBillsParentID.Value := ABills.ParentID;
- cdsBillsNextSiblingID.Value := ABills.NextID;
- cdsBillsCode.Value := ABills.Code;
- cdsBillsB_Code.Value := ABills.BCode;
- cdsBillsName.Value := ABills.Name;
- cdsBillsUnits.Value := ABills.Units;
- cdsBillsIsPreDefine.Value := ABills.IsPreDefine;
- cdsBills.Post;
- end;
- end;
- procedure WriteExprs(ABills: PBillsConfigRecord);
- begin
- if ABills.Exprs <> '' then
- FBillsData.DMExprs.AddExprs(1, 3, ABills.ID, ABills.Exprs, 0, 0);
- end;
- var
- I: Integer;
- rdBillsConfig: PBillsConfigRecord;
- begin
- for I := 0 to FBillsConfig.FRecordList.Count - 1 do
- begin
- rdBillsConfig := FBillsConfig.FRecordList.List^[I];
- if rdBillsConfig.ID > 0 then
- begin
- WriteBills(rdBillsConfig);
- WriteExprs(rdBillsConfig);
- end;
- end;
- end;
- end.
|