ScCopyBills.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. unit ScCopyBills;
  2. interface
  3. uses
  4. Classes, SysUtils, XMLDoc, XMLIntf, Math, Controls, ScProjectManager,
  5. DB, DBClient, Contnrs, Windows, ScBillsTree, ConstVarUnit, DataBase,
  6. ConstTypeUnit;
  7. var
  8. CF_Bills: Word;
  9. CF_Rations: Word;
  10. type
  11. TScXMLSaver = class(TObject)
  12. private
  13. FMajorID: Integer;
  14. FPasteCount: Integer;
  15. FBillsData: TDMDataBase;
  16. FBillIDsList: TObjectList;
  17. procedure LoadBillsItem(ABillRec: TBillIDRecord; ANode: IXMLNode;
  18. var ANewDQID: Integer; ABillsQty, ADQQty: Boolean);
  19. procedure InternalAddBillRecd(xNode: IXMLNode; var ABillsID, ANewDQID: Integer;
  20. ABillsQty, ADQQty: Boolean);
  21. procedure RepairTreeStruct(var ALastNextNewID, ALastNextOldID: Integer;
  22. AParentID: Integer; AIsNew: Boolean);
  23. {复制方法}
  24. procedure SaveDrawingQuantity(ANode: IXMLNode; ABillID: Integer);
  25. procedure SaveExprsInXMLNode(ANode: IXMLNode);
  26. procedure SaveBillsExprs(ANode: IXMLNode; ABillsID: Integer);
  27. procedure SaveDrawingItemExprs(ANode: IXMLNode; ADrawingID: Integer);
  28. {粘贴方法}
  29. procedure LoadBillsForpaste(ANode: IXMLNode; var ABillID: Integer; AIsNew: Boolean;
  30. AItem: TScBillsItem; ABillsQty, ADQQty: Boolean);
  31. procedure LoadBillsExprs(ANode: IXMLNode; ABillsID: Integer);
  32. procedure LoadDrawingItemExprs(ANode: IXMLNode; ADrawingID: Integer);
  33. procedure LoadDrawingQuantityForPaste(ANode: IXMLNode; var ANewID: Integer;
  34. ABillID: Integer; AClearQty: Boolean; AClearBillsQty: Boolean = False);
  35. protected
  36. function CreateXMLDoc: IXMLDocument;
  37. public
  38. constructor Create(ABillsData: TDMDataBase); virtual;
  39. destructor Destroy; override;
  40. property BillsData: TDMDataBase read FBillsData;
  41. end;
  42. TScCopyType = (ctBills, ctRations);
  43. TScXMLClipboard = class(TScXMLSaver)
  44. private
  45. function SelectPastePos(var ABillsQty, ADQQty, ANew: Boolean; var ANewID, APos: Integer; var AItem: TScBillsItem): Boolean;
  46. function GetFirstLevelCount(aRoot: IXMLNode): Integer;
  47. procedure LocateNew(aPos, aIndex, aCount: Integer);
  48. procedure CollapseNew(aPos, aCount: Integer; aItem: TScBillsItem);
  49. private
  50. function SaveBillsItemForCopy(AItem: TScBillsItem; ANode: IXMLNode): IXMLNode;
  51. procedure SaveBillsForCopy(AIndex1, AIndex2: Integer; ANode: IXMLNode);
  52. procedure CopyBillsToXml(AXmlDoc: IXMLDocument; AIndex1, AIndex2: Integer);
  53. procedure PasteBillsFromXml(AXmlDoc: IXMLDocument; AIndex: Integer);
  54. protected
  55. procedure SaveXMLToClipboard(AFormat: Word; AXMLDoc: IXMLDocument);
  56. procedure LoadXMLFromClipboard(AFormat: Word; AXMLDoc: IXMLDocument);
  57. public
  58. constructor Create(aBillsData: TDMDataBase); override;
  59. {复制清单}
  60. procedure CopyBillsToClipboard(Index1, Index2: Integer);
  61. {粘贴清单}
  62. procedure PasteBillsFromClipboard(aIndex: Integer);
  63. {复制清单保存成文件}
  64. procedure CopyBillsToFile(const AFileName: string; AIndex1, AIndex2: Integer);
  65. {从文件中粘贴清单}
  66. procedure PasteBillsFromFile(const AFileName: string; AIndex: Integer);
  67. end;
  68. implementation
  69. uses
  70. Forms, Variants, Clipbrd, ZjIDTree, CheckPosForm, ConstMethodUnit, ScConfig,
  71. ScExprsDM;
  72. { TScXMLSaver }
  73. constructor TScXMLSaver.Create(ABillsData: TDMDataBase);
  74. begin
  75. FBillsData := ABillsData;
  76. FBillIDsList := TObjectList.Create;
  77. end;
  78. function TScXMLSaver.CreateXMLDoc: IXMLDocument;
  79. begin
  80. Result := TXMLDocument.Create(nil) as IXMLDocument;
  81. Result.Active := True;
  82. Result.Encoding := 'gb2312';
  83. Result.Options := Result.Options + [doNodeAutoIndent];
  84. Result.AddChild('SmartCost');
  85. end;
  86. destructor TScXMLSaver.Destroy;
  87. begin
  88. FBillIDsList.Free;
  89. inherited;
  90. end;
  91. procedure TScXMLSaver.InternalAddBillRecd(xNode: IXMLNode;
  92. var ABillsID, ANewDQID: Integer; ABillsQty, ADQQty: Boolean);
  93. var
  94. bRec: TBillIDRecord;
  95. begin
  96. if not Assigned(xNode) then Exit;
  97. if SameText(xNode.NodeName, c_BillsItem) then
  98. begin
  99. bRec := TBillIDRecord.Create;
  100. bRec.OldID := xNode.Attributes[c_ID];
  101. bRec.ParentID := xNode.Attributes[c_ParentID];
  102. bRec.NextSiblingID := xNode.Attributes[c_NextSiblingID];
  103. bRec.NewID := ABillsID;
  104. LoadBillsItem(bRec, xNode, ANewDQID, ABillsQty, ADQQty);
  105. FBillIDsList.Add(bRec);
  106. Inc(ABillsID);
  107. end;
  108. if xNode.HasChildNodes then
  109. InternalAddBillRecd(xNode.ChildNodes[0], ABillsID, ANewDQID, ABillsQty, ADQQty);
  110. if Assigned(xNode.NextSibling) then
  111. InternalAddBillRecd(xNode.NextSibling, ABillsID, ANewDQID, ABillsQty, ADQQty);
  112. end;
  113. procedure TScXMLSaver.LoadBillsExprs(ANode: IXMLNode; ABillsID: Integer);
  114. procedure LoadExprs(AXMLNode: IXMLNode);
  115. begin
  116. with FBillsData.DMExprs do
  117. begin
  118. cdsOrgExprs.Append;
  119. cdsOrgExprsMajorID.Value := AXMLNode.Attributes[c_MajorID];
  120. cdsOrgExprsMinorID.Value := AXMLNode.Attributes[c_MinorID];
  121. cdsOrgExprsRecdID.Value := ABillsID;
  122. cdsOrgExprsExprs.Value := AXMLNode.Attributes[c_Exprs];
  123. cdsOrgExprsExprs1.Value := AXMLNode.Attributes[c_Exprs1];
  124. cdsOrgExprsFlag.Value := AXMLNode.Attributes[c_Flag];
  125. cdsOrgExprsExprsValue.Value := AXMLNode.Attributes[c_ExprsValue];
  126. cdsOrgExprs.Post;
  127. end;
  128. end;
  129. var
  130. I: Integer;
  131. xmlNode: IXMLNode;
  132. begin
  133. xmlNode := ANode.ChildNodes.FindNode(c_BillsExprs);
  134. if xmlNode <> nil then
  135. begin
  136. for I := 0 to xmlNode.ChildNodes.Count - 1 do
  137. LoadExprs(xmlNode.ChildNodes[I]);
  138. end;
  139. end;
  140. procedure TScXMLSaver.LoadBillsForpaste(ANode: IXMLNode;
  141. var ABillID: Integer; AIsNew: Boolean; AItem: TScBillsItem;
  142. ABillsQty, ADQQty: Boolean);
  143. var
  144. iNewDQID: Integer;
  145. vNode: IXMLNode;
  146. iOldNextSiblingID, iParentID, iCurID: Integer;
  147. iLastNextSiblingNewID, iLastNextSiblingOldID: Integer;
  148. begin
  149. if not Assigned(AItem) then Exit;
  150. iNewDQID := FBillsData.GetMaxDrawingQuangtiyID;
  151. iOldNextSiblingID := -1;
  152. with FBillsData do
  153. begin
  154. iCurID := AItem.ID;
  155. if not ModifyNextSiblingID(iCurID, ABillID, iParentID, iOldNextSiblingID)
  156. then Exit;
  157. DisconnectBillsTree;
  158. try
  159. ANode := ANode.ChildNodes.FindNode(c_BillsList);
  160. if Assigned(ANode) and (ANode.ChildNodes.Count > 0) then
  161. begin
  162. vNode := ANode.ChildNodes[0];
  163. iLastNextSiblingOldID := vNode.Attributes[c_ID];
  164. iLastNextSiblingNewID := ABillID;
  165. InternalAddBillRecd(vNode, ABillID, iNewDQID, ABillsQty, ADQQty);
  166. end;
  167. FPasteCount := FBillIDsList.Count;
  168. RepairTreeStruct(iLastNextSiblingNewID, iLastNextSiblingOldID, iParentID, AIsNew);
  169. WriteRecIntoDB(FBillIDsList);
  170. if (iLastNextSiblingNewID <> iOldNextSiblingID) then
  171. ModifyNextSiblingID(iLastNextSiblingNewID, iOldNextSiblingID);
  172. if AIsNew then DeleteBills(iCurID);
  173. finally
  174. ConnectionBillsTree;
  175. end;
  176. end;
  177. end;
  178. procedure TScXMLSaver.LoadBillsItem(ABillRec: TBillIDRecord;
  179. ANode: IXMLNode; var ANewDQID: Integer; ABillsQty, ADQQty: Boolean);
  180. begin
  181. ABillRec.Code := VarToStr(ANode.Attributes[c_Code]);
  182. ABillRec.Name := VarToStr(ANode.Attributes[c_Name]);
  183. ABillRec.Units := VarToStr(ANode.Attributes[c_Units]);
  184. if ABillsQty then
  185. begin
  186. ABillRec.Quantity := 0;
  187. ABillRec.DesignQuantity := 0;
  188. ABillRec.DesignQuantity2 := 0;
  189. ABillRec.DesignPrice := 0;
  190. ABillRec.UnitPrice := 0;
  191. ABillRec.TotalPrice := 0;
  192. end
  193. else
  194. begin
  195. ABillRec.Quantity := ANode.Attributes[c_Quantity];
  196. ABillRec.DesignQuantity := ANode.Attributes[c_DesignQuantity1];
  197. ABillRec.DesignQuantity2 := ANode.Attributes[c_DesignQuantity2];
  198. ABillRec.DesignPrice := ANode.Attributes[c_DesignPrice];
  199. ABillRec.UnitPrice := ANode.Attributes[c_UnitPrice];
  200. ABillRec.TotalPrice := ANode.Attributes[c_TotalPrice];
  201. end;
  202. ABillRec.B_Code := ANode.Attributes[c_BCode];
  203. ABillRec.MemoStr := ANode.Attributes[c_MemoString];
  204. LoadDrawingQuantityForPaste(ANode, ANewDQID, ABillRec.NewID, ADQQty, ABillsQty);
  205. LoadBillsExprs(ANode, ABillRec.NewID);
  206. end;
  207. procedure TScXMLSaver.LoadDrawingItemExprs(ANode: IXMLNode;
  208. ADrawingID: Integer);
  209. procedure LoadExprs(AXMLNode: IXMLNode);
  210. begin
  211. with FBillsData.DMExprs do
  212. begin
  213. cdsOrgExprs.Append;
  214. cdsOrgExprsMajorID.Value := AXMLNode.Attributes[c_MajorID];
  215. cdsOrgExprsMinorID.Value := AXMLNode.Attributes[c_MinorID];
  216. cdsOrgExprsRecdID.Value := ADrawingID;
  217. cdsOrgExprsExprs.Value := AXMLNode.Attributes[c_Exprs];
  218. cdsOrgExprsExprs1.Value := AXMLNode.Attributes[c_Exprs1];
  219. cdsOrgExprsFlag.Value := AXMLNode.Attributes[c_Flag];
  220. cdsOrgExprsExprsValue.Value := AXMLNode.Attributes[c_ExprsValue];
  221. cdsOrgExprs.Post;
  222. end;
  223. end;
  224. var
  225. I: Integer;
  226. xmlNode: IXMLNode;
  227. begin
  228. xmlNode := ANode.ChildNodes.FindNode(c_DrawingExprs);
  229. if xmlNode <> nil then
  230. begin
  231. for I := 0 to xmlNode.ChildNodes.Count - 1 do
  232. LoadExprs(xmlNode.ChildNodes[I]);
  233. end;
  234. end;
  235. procedure TScXMLSaver.LoadDrawingQuantityForPaste(ANode: IXMLNode;
  236. var ANewID: Integer; ABillID: Integer; AClearQty: Boolean; AClearBillsQty: Boolean);
  237. var
  238. I, iSerinalNo: Integer;
  239. vNode: IXMLNode;
  240. begin
  241. ANode := ANode.ChildNodes.FindNode(c_DrawQList);
  242. if ANode = nil then Exit;
  243. with FBillsData do
  244. begin
  245. iSerinalNo := 1;
  246. for I := 0 to ANode.ChildNodes.Count - 1 do
  247. begin
  248. vNode := ANode.ChildNodes[I];
  249. cdsDrawingQuantity.Insert;
  250. cdsDrawingQuantityID.Value := ANewID;
  251. cdsDrawingQuantityBillsID.Value := ABillID;
  252. cdsDrawingQuantitySerinalNo.Value := iSerinalNo;
  253. cdsDrawingQuantityName.Value := vNode.Attributes[c_Name];
  254. cdsDrawingQuantityUnits.Value := vNode.Attributes[c_Units];
  255. if AClearBillsQty then
  256. cdsDrawingQuantityIsGatherQ.AsBoolean := False
  257. else
  258. cdsDrawingQuantityIsGatherQ.Value := vNode.Attributes[c_IsGatherQty];
  259. if AClearQty then
  260. begin
  261. cdsDrawingQuantityDQuantity1.Value := 0;
  262. cdsDrawingQuantityDQuantity2.Value := 0;
  263. end
  264. else
  265. begin
  266. cdsDrawingQuantityDQuantity1.Value := vNode.Attributes[c_DesignQuantity1];
  267. cdsDrawingQuantityDQuantity2.Value := vNode.Attributes[c_DesignQuantity2];
  268. end;
  269. cdsDrawingQuantityMemoContext.Value := vNode.Attributes[c_MemoString];
  270. cdsDrawingQuantity.Post;
  271. LoadDrawingItemExprs(vNode, ANewID);
  272. Inc(ANewID);
  273. Inc(iSerinalNo);
  274. end;
  275. end;
  276. end;
  277. procedure TScXMLSaver.RepairTreeStruct(var ALastNextNewID, ALastNextOldID: Integer;
  278. AParentID: Integer; AIsNew: Boolean);
  279. var
  280. I, J: Integer;
  281. billIDRecd, billRec: TBillIDRecord;
  282. begin
  283. for I := 0 to FBillIDsList.Count - 1 do
  284. begin
  285. billIDRecd := TBillIDRecord(FBillIDsList[I]);
  286. if (billIDRecd.OldID = ALastNextOldID) then
  287. begin
  288. if (billIDRecd.NextSiblingID <> -1) or AIsNew then
  289. begin
  290. ALastNextOldID := billIDRecd.NextSiblingID;
  291. end;
  292. ALastNextNewID := billIDRecd.NewID;
  293. billIDRecd.ParentID := AParentID;
  294. billIDRecd.ParentChanged := True;
  295. end;
  296. for J := 0 to FBillIDsList.Count - 1 do
  297. begin
  298. billRec := TBillIDRecord(FBillIDsList[J]);
  299. if (billRec.ParentID = billIDRecd.OldID) and (not billRec.ParentChanged) then
  300. begin
  301. billRec.ParentID := billIDRecd.NewID;
  302. billRec.ParentChanged := True;
  303. end
  304. else if (billRec.NextSiblingID = billIDRecd.OldID) and (not billRec.NextSiblingChanged) then
  305. begin
  306. billRec.NextSiblingID := billIDRecd.NewID;
  307. billRec.NextSiblingChanged := True;
  308. end;
  309. end;
  310. end;
  311. end;
  312. procedure TScXMLSaver.SaveBillsExprs(ANode: IXMLNode; ABillsID: Integer);
  313. var
  314. xmlExprs: IXMLNode;
  315. begin
  316. with FBillsData.DMExprs do
  317. begin
  318. xmlExprs := ANode.AddChild(c_BillsExprs);
  319. cdsOrgExprs.SetRange([1, ABillsID], [1, ABillsID]);
  320. cdsOrgExprs.First;
  321. while not cdsOrgExprs.Eof do
  322. begin
  323. SaveExprsInXMLNode(xmlExprs.AddChild(c_BillsExprsItem));
  324. cdsOrgExprs.Next;
  325. end;
  326. cdsOrgExprs.CancelRange;
  327. end;
  328. end;
  329. procedure TScXMLSaver.SaveDrawingItemExprs(ANode: IXMLNode;
  330. ADrawingID: Integer);
  331. var
  332. xmlExprs: IXMLNode;
  333. begin
  334. with FBillsData.DMExprs do
  335. begin
  336. xmlExprs := ANode.AddChild(c_DrawingExprs);
  337. cdsOrgExprs.SetRange([2, ADrawingID], [2, ADrawingID]);
  338. cdsOrgExprs.First;
  339. while not cdsOrgExprs.Eof do
  340. begin
  341. SaveExprsInXMLNode(xmlExprs.AddChild(c_DrawingExprsItem));
  342. cdsOrgExprs.Next;
  343. end;
  344. cdsOrgExprs.CancelRange;
  345. end;
  346. end;
  347. procedure TScXMLSaver.SaveDrawingQuantity(ANode: IXMLNode;
  348. ABillID: Integer);
  349. var
  350. xmlDrawItem: IXMLNode;
  351. begin
  352. with FBillsData do
  353. begin
  354. cdsDQForLocate.SetRange([ABillID], [ABillID]);
  355. if cdsDQForLocate.RecordCount > 0 then
  356. ANode := ANode.AddChild(c_DrawQList);
  357. cdsDQForLocate.First;
  358. while not cdsDQForLocate.Eof do
  359. begin
  360. xmlDrawItem := ANode.AddChild(c_DQItem);
  361. xmlDrawItem.Attributes[c_BillsID] := cdsDQForLocateBillsID.AsInteger;
  362. xmlDrawItem.Attributes[c_Name] := cdsDQForLocateName.AsString;
  363. xmlDrawItem.Attributes[c_Units] := cdsDQForLocateUnits.AsString;
  364. xmlDrawItem.Attributes[c_DesignQuantity1] := cdsDQForLocateDQuantity1.AsFloat;
  365. xmlDrawItem.Attributes[c_DesignQuantity2] := cdsDQForLocateDQuantity2.AsFloat;
  366. xmlDrawItem.Attributes[c_MemoString] := cdsDQForLocateMemoContext.AsString;
  367. xmlDrawItem.Attributes[c_IsGatherQty] := cdsDQForLocateIsGatherQ.AsBoolean;
  368. SaveDrawingItemExprs(xmlDrawItem, cdsDQForLocateID.AsInteger);
  369. cdsDQForLocate.Next;
  370. end;
  371. cdsDQForLocate.CancelRange;
  372. end;
  373. end;
  374. procedure TScXMLSaver.SaveExprsInXMLNode(ANode: IXMLNode);
  375. begin
  376. with FBillsData.DMExprs do
  377. begin
  378. ANode.Attributes[c_MajorID] := cdsOrgExprsMajorID.AsInteger;
  379. ANode.Attributes[c_MinorID] := cdsOrgExprsMinorID.AsInteger;
  380. ANode.Attributes[c_RecdID] := cdsOrgExprsRecdID.AsInteger;
  381. ANode.Attributes[c_Exprs] := cdsOrgExprsExprs.AsString;
  382. ANode.Attributes[c_Exprs1] := cdsOrgExprsExprs1.AsString;
  383. ANode.Attributes[c_Flag] := cdsOrgExprsFlag.AsInteger;
  384. ANode.Attributes[c_ExprsValue] := cdsOrgExprsExprsValue.AsFloat;
  385. end;
  386. end;
  387. { TScXMLClipboard }
  388. procedure TScXMLClipboard.CollapseNew(aPos, aCount: Integer; aItem: TScBillsItem);
  389. begin
  390. case aPos of
  391. cp_Next:
  392. begin
  393. while Assigned(aItem) and (aCount > 0) do
  394. begin
  395. aItem.Collapse;
  396. aItem := TScBillsItem(aItem.NextSibling);
  397. Dec(aCount);
  398. end;
  399. end;
  400. cp_Font, cp_Child:
  401. begin
  402. while Assigned(aItem) and (aCount > 0) do
  403. begin
  404. aItem.Collapse;
  405. aItem := TScBillsItem(aItem.PrevSibling);
  406. Dec(aCount);
  407. end;
  408. end;
  409. end;
  410. end;
  411. procedure TScXMLClipboard.CopyBillsToClipboard(Index1, Index2: Integer);
  412. var
  413. xmlDoc: IXMLDocument;
  414. begin
  415. xmlDoc := CreateXMLDoc;
  416. try
  417. CopyBillsToXml(xmlDoc, Index1, Index2);
  418. // 将XML文件流按照CF_Rations格式保存到剪贴板中
  419. SaveXMLToClipboard(CF_Bills, xmlDoc);
  420. finally
  421. xmlDoc := nil;
  422. end;
  423. end;
  424. procedure TScXMLClipboard.CopyBillsToFile(const AFileName: string; AIndex1,
  425. AIndex2: Integer);
  426. var
  427. xmlDoc: IXMLDocument;
  428. begin
  429. xmlDoc := CreateXMLDoc;
  430. try
  431. CopyBillsToXml(xmlDoc, AIndex1, AIndex2);
  432. if not DirectoryExists(ExtractFilePath(AFileName)) then
  433. ForceDirectories(ExtractFilePath(AFileName));
  434. xmlDoc.SaveToFile(AFileName);
  435. finally
  436. xmlDoc := nil;
  437. end;
  438. end;
  439. procedure TScXMLClipboard.CopyBillsToXml(AXmlDoc: IXMLDocument; AIndex1,
  440. AIndex2: Integer);
  441. var
  442. xmlBillsList, xmlRoot: IXMLNode;
  443. begin
  444. if AIndex1 > AIndex2 then Exit;
  445. xmlRoot := AXmlDoc.DocumentElement;
  446. // 增加清单列表接点
  447. xmlBillsList := xmlRoot.AddChild(c_BillsList);
  448. AIndex1 := Max(0, AIndex1);
  449. AIndex2 := Min(FBillsData.BillsTree.Count - 1, AIndex2);
  450. SaveBillsForCopy(AIndex1, AIndex2, xmlBillsList);
  451. end;
  452. constructor TScXMLClipboard.Create(aBillsData: TDMDataBase);
  453. begin
  454. inherited Create(aBillsData);
  455. end;
  456. function TScXMLClipboard.GetFirstLevelCount(aRoot: IXMLNode): Integer;
  457. var
  458. cNode: IXMLNode;
  459. begin
  460. cNode := aRoot.ChildNodes.FindNode(c_BillsList);
  461. Result := cNode.ChildNodes.Count;
  462. end;
  463. procedure TScXMLClipboard.LoadXMLFromClipboard(AFormat: Word;
  464. AXMLDoc: IXMLDocument);
  465. var
  466. MemStrm: TMemoryStream;
  467. Data: THandle;
  468. DataPtr: Pointer;
  469. begin
  470. with Clipboard do
  471. begin
  472. Open;
  473. try
  474. Data := GetClipboardData(AFormat);
  475. if Data = 0 then Exit;
  476. DataPtr := GlobalLock(Data);
  477. try
  478. MemStrm := TMemoryStream.Create;
  479. try
  480. MemStrm.WriteBuffer(DataPtr^, GlobalSize(Data));
  481. MemStrm.Position := 0;
  482. AXMLDoc.LoadFromStream(MemStrm);
  483. finally
  484. MemStrm.Free;
  485. end;
  486. finally
  487. GlobalUnlock(Data);
  488. end;
  489. finally
  490. Close;
  491. end;
  492. end;
  493. end;
  494. procedure TScXMLClipboard.LocateNew(aPos, aIndex, aCount: Integer);
  495. var
  496. cItem: TScBillsItem;
  497. begin
  498. cItem := FBillsData.BillsTree[aIndex];
  499. case aPos of
  500. cp_Next:
  501. begin
  502. cItem := TScBillsItem(cItem.NextSibling);
  503. if Assigned(cItem) then
  504. cItem.LocateDBRecord;
  505. end;
  506. cp_Font:
  507. begin
  508. cItem := TScBillsItem(cItem.PrevSibling);
  509. if Assigned(cItem) then
  510. cItem.LocateDBRecord;
  511. end;
  512. cp_Child:
  513. begin
  514. cItem := TScBillsItem(cItem.LastChild);
  515. if Assigned(cItem) then
  516. cItem.LocateDBRecord;
  517. end;
  518. end;
  519. if ScConfigInfo.AutoCollapse then
  520. CollapseNew(aPos, aCount, cItem);
  521. end;
  522. procedure TScXMLClipboard.PasteBillsFromClipboard(aIndex: Integer);
  523. var
  524. xmlDoc: IXMLDocument;
  525. begin
  526. if (aIndex >= FBillsData.BillsTree.Count) or (aIndex < -1) then Exit;
  527. xmlDoc := CreateXMLDoc;
  528. try
  529. LoadXMLFromClipboard(CF_Bills, xmlDoc);
  530. PasteBillsFromXml(xmlDoc, aIndex);
  531. finally
  532. xmlDoc := nil;
  533. end;
  534. end;
  535. procedure TScXMLClipboard.PasteBillsFromFile(const AFileName: string;
  536. AIndex: Integer);
  537. var
  538. xmlDoc: IXMLDocument;
  539. begin
  540. if (AIndex >= FBillsData.BillsTree.Count) or (AIndex < -1) then Exit;
  541. xmlDoc := CreateXMLDoc;
  542. try
  543. xmlDoc.LoadFromFile(AFileName);
  544. PasteBillsFromXml(xmlDoc, AIndex);
  545. finally
  546. xmlDoc := nil;
  547. end;
  548. end;
  549. procedure TScXMLClipboard.PasteBillsFromXml(AXmlDoc: IXMLDocument;
  550. AIndex: Integer);
  551. var
  552. vRoot: IXMLNode;
  553. vItem: TScBillsItem;
  554. bBillsQty: Boolean;
  555. bDQQty: Boolean;
  556. bNew: Boolean;
  557. bIsPPBills: Boolean;
  558. iNewBillID, iPos, iCount: Integer;
  559. begin
  560. bNew := False;
  561. bBillsQty := False;
  562. bDQQty := False;
  563. bIsPPBills := False;
  564. vItem := FBillsData.BillsTree.Items[aIndex];
  565. if vItem = nil then Exit;
  566. vRoot := AXmlDoc.DocumentElement;
  567. if vRoot = nil then Exit;
  568. iCount := GetFirstLevelCount(vRoot);
  569. iNewBillID := FBillsData.GetMaxBillsID;
  570. aIndex := vItem.ID;
  571. FBillsData.SaveStatus;
  572. if not SelectPastePos(bBillsQty, bDQQty, bNew, iNewBillID, iPos, vItem) then Exit;
  573. Screen.Cursor := crHourGlass;
  574. if FBillsData.IsProjectBills then
  575. begin
  576. bIsPPBills := True;
  577. FBillsData.IsProjectBills := False;
  578. end;
  579. FBillsData.EnabledUITreeEvt(False, False);
  580. try
  581. LoadBillsForpaste(vRoot, iNewBillID, bNew, vItem, bBillsQty, bDQQty);
  582. finally
  583. FBillsData.ReadStatus(FMajorID, FPasteCount);
  584. FBillsData.EnabledUITreeEvt(True, False);
  585. LocateNew(iPos, aIndex, iCount);
  586. Screen.Cursor := crDefault;
  587. if bIsPPBills then
  588. FBillsData.IsProjectBills := bIsPPBills;
  589. end;
  590. end;
  591. procedure TScXMLClipboard.SaveBillsForCopy(AIndex1, AIndex2: Integer;
  592. ANode: IXMLNode);
  593. var
  594. vChildNode: IXMLNode;
  595. vItem: TScBillsItem;
  596. iIndex: Integer;
  597. begin
  598. if AIndex1 > AIndex2 then Exit;
  599. vItem := FBillsData.BillsTree.Items[AIndex1];
  600. while Assigned(vItem) and (vItem.MajorIndex <= AIndex2) do
  601. begin
  602. vChildNode := SaveBillsItemForCopy(vItem, ANode);
  603. if vItem.HasChildren then
  604. begin
  605. iIndex := vItem.FirstChild.MajorIndex;
  606. // 用AIndex2则只复制到选择范围内,用MaxInt表示复制所有子项,即使不在选择范围内
  607. SaveBillsForCopy(iIndex, MaxInt, vChildNode);
  608. end;
  609. vItem := TScBillsItem(vItem.NextSibling);
  610. end;
  611. end;
  612. function TScXMLClipboard.SaveBillsItemForCopy(AItem: TScBillsItem;
  613. ANode: IXMLNode): IXMLNode;
  614. procedure SaveXMLBillsItem(AXMLNode: IXMLNode);
  615. begin
  616. with FBillsData do
  617. begin
  618. AXMLNode.Attributes[c_ID] := cdsBillsID.Value;
  619. AXMLNode.Attributes[c_ParentID] := cdsBillsParentID.Value;
  620. AXMLNode.Attributes[c_NextSiblingID] := cdsBillsNextSiblingID.Value;
  621. AXMLNode.Attributes[c_Code] := cdsBillsCode.AsString;
  622. AXMLNode.Attributes[c_Name] := cdsBillsName.AsString;
  623. AXMLNode.Attributes[c_Units] := cdsBillsUnits.AsString;
  624. AXMLNode.Attributes[c_BCode] := cdsBillsB_Code.AsString;
  625. AXMLNode.Attributes[c_DesignQuantity1] := cdsBillsDesignQuantity.AsFloat;
  626. AXMLNode.Attributes[c_DesignQuantity2] := cdsBillsDesignQuantity2.AsFloat;
  627. AXMLNode.Attributes[c_DesignPrice] := cdsBillsDesignPrice.AsFloat;
  628. AXMLNode.Attributes[c_Quantity] := cdsBillsQuantity.AsFloat;
  629. AXMLNode.Attributes[c_UnitPrice] := cdsBillsUnitPrice.AsFloat;
  630. AXMLNode.Attributes[c_TotalPrice] := cdsBillsTotalPrice.AsFloat;
  631. AXMLNode.Attributes[c_MemoString] := cdsBillsMemoStr.AsString;
  632. end;
  633. end;
  634. begin
  635. Result := nil;
  636. if FBillsData.cdsBills.FindKey([AItem.ID]) then
  637. begin
  638. Result := ANode.AddChild(c_BillsItem);
  639. SaveXMLBillsItem(Result);
  640. SaveDrawingQuantity(Result, AItem.ID);
  641. SaveBillsExprs(Result, AItem.ID);
  642. end;
  643. end;
  644. Type
  645. TClipboardAccess = class(TClipboard);
  646. procedure TScXMLClipboard.SaveXMLToClipboard(AFormat: Word;
  647. AXMLDoc: IXMLDocument);
  648. var
  649. MemStrm: TMemoryStream;
  650. begin
  651. MemStrm := TMemoryStream.Create;
  652. try
  653. AxmlDoc.SaveToStream(MemStrm);
  654. MemStrm.Position := 0;
  655. TClipboardAccess(Clipboard).SetBuffer(AFormat, MemStrm.Memory^, MemStrm.Size);
  656. finally
  657. MemStrm.Free;
  658. end;
  659. end;
  660. function TScXMLClipboard.SelectPastePos(var ABillsQty, ADQQty,
  661. ANew: Boolean; var ANewID, APos: Integer; var AItem: TScBillsItem): Boolean;
  662. begin
  663. Result := False;
  664. APos := CheckBillsPastePosition(ABillsQty, ADQQty);
  665. case APos of
  666. -1: Exit;
  667. // 后兄弟无需处理,始终插在选中节点的后兄弟位置。
  668. cp_Next: FMajorID := AItem.MajorIndex;
  669. cp_Font:
  670. begin
  671. if AItem.PrevSibling = nil then
  672. begin
  673. FMajorID := AItem.Parent.MajorIndex;
  674. AItem := FBillsData.BillsTree.AddBillsItem(ANewID, AItem.ParentID, AItem.ID);
  675. Inc(ANewID);
  676. ANew := True;
  677. end
  678. else
  679. begin
  680. FMajorID := AItem.PrevNode.MajorIndex;
  681. AItem := FBillsData.BillsTree[AItem.PrevSiblingID];
  682. end;
  683. end;
  684. cp_Child:
  685. begin
  686. {if AItem.HasDrawingQuantity then
  687. begin
  688. MessageHint(0, '该清单下有图纸工程量,不允许插入子项。');
  689. Exit;
  690. end; }
  691. if AItem.ChildCount = 0 then
  692. begin
  693. FMajorID := AItem.MajorIndex;
  694. AItem := FBillsData.BillsTree.AddBillsItem(ANewID, AItem.ID, -1);
  695. Inc(ANewID);
  696. ANew := True;
  697. end
  698. else
  699. begin
  700. AItem := TScBillsItem(AItem.LastChild);
  701. FMajorID := AItem.MajorIndex;
  702. end;
  703. end;
  704. end;
  705. Result := True;
  706. end;
  707. initialization
  708. { The following strings should not be localized }
  709. CF_Bills := RegisterClipboardFormat('SmartCost Bills');
  710. CF_Rations := RegisterClipboardFormat('SmartCost Rations');
  711. finalization
  712. end.