BillsClipboard.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. unit BillsClipboard;
  2. interface
  3. uses
  4. BaseClipboard, Classes, BillsDm, ZhAPI, XmlDoc, XmlIntf, Windows, sdIDTree;
  5. type
  6. TBillsBlockNode = class
  7. private
  8. FID: Integer;
  9. FParentID: Integer;
  10. FNextSiblingID: Integer;
  11. FCode: string;
  12. FB_Code: string;
  13. FName: string;
  14. FUnits: string;
  15. FPrice: Double;
  16. FOrgQuantity: Double;
  17. FMisQuantity: Double;
  18. FOthQuantity: Double;
  19. FQuantity: Double;
  20. FDrawingCode: string;
  21. FAlias: string;
  22. FDgnQuantity1: Double;
  23. FDgnQuantity2: Double;
  24. FMemoStr: string;
  25. FDealCode: string;
  26. FDealCompany: string;
  27. public
  28. procedure SaveToXmlNode(AXmlNode: IXMLNode);
  29. procedure LoadFromXmlNode(AXmlNode: IXMLNode);
  30. end;
  31. TBillsClipboard = class(TBaseClipboard)
  32. private
  33. FBillsData: TBillsData;
  34. FNodeList: TList;
  35. procedure SaveNodesToXml(AXmlNode: IXMLNode);
  36. procedure LoadNodesFromXml(AXmlNode: IXMLNode);
  37. procedure CopyBillsNodeData(ANode: TsdIDTreeNode);
  38. procedure CopyBillsNode(ANode: TsdIDTreeNode);
  39. procedure CopyData(ANode: TsdIDTreeNode; ACount: Integer);
  40. function AddBillsNode(ANode: TsdIDTreeNode; APos: Integer): TsdIDTreeNode;
  41. procedure PasteBillsNodeData(ANode: TsdIDTreeNode; APasteNode: TBillsBlockNode);
  42. procedure PasteChildren(AParentNode: TsdIDTreeNode; APasteParentID: Integer);
  43. procedure PasteNextSibling(APreSibling: TsdIDTreeNode; APasteNodeID: Integer);
  44. procedure PasteBillsNode(ANode: TsdIDTreeNode; APos: Integer; APasteNode: TBillsBlockNode);
  45. procedure PasteData(ANode: TsdIDTreeNode; APos: Integer);
  46. procedure Clear;
  47. protected
  48. procedure LoadXmlData(AXmlDoc: IXmlDocument); override;
  49. procedure SaveXmlData(AXmlDoc: IXmlDocument); override;
  50. public
  51. constructor Create(ABillsData: TBillsData);
  52. destructor Destroy; override;
  53. procedure Copy(ANode: TsdIDTreeNode; ACount: Integer);
  54. // APos: 0[×ÓÏî] 1[ºóÏî] 2[ǰÏî]
  55. procedure Paste(ANode: TsdIDTreeNode; APos: Integer);
  56. end;
  57. function HasBillsBlockFormat: Boolean;
  58. implementation
  59. uses sdDB, Math, SysUtils, UtilMethods;
  60. var
  61. CF_BillsBlock: Word;
  62. function HasBillsBlockFormat: Boolean;
  63. begin
  64. Result := HasClipboardFormat(CF_BillsBlock);
  65. end;
  66. { TBillsClipboard }
  67. function TBillsClipboard.AddBillsNode(ANode: TsdIDTreeNode;
  68. APos: Integer): TsdIDTreeNode;
  69. begin
  70. // ×ÓÏî²åÈë
  71. if APos = 0 then
  72. Result := ANode.Owner.Add(ANode.ID, -1)
  73. // ºóÏî²åÈë
  74. else if APos = 1 then
  75. Result := ANode.Owner.Add(ANode.ParentID, ANode.NextSiblingID)
  76. // ǰÏî²åÈë
  77. else if APos = 2 then
  78. Result := ANode.Owner.Add(ANode.ParentID, ANode.ID);
  79. end;
  80. procedure TBillsClipboard.Clear;
  81. begin
  82. ClearObjects(FNodeList);
  83. FNodeList.Clear;
  84. end;
  85. procedure TBillsClipboard.Copy(ANode: TsdIDTreeNode; ACount: Integer);
  86. begin
  87. Clear;
  88. CopyData(ANode, ACount);
  89. SaveToStream;
  90. SaveToClipboard(CF_BillsBlock);
  91. end;
  92. procedure TBillsClipboard.CopyBillsNode(ANode: TsdIDTreeNode);
  93. var
  94. iChild: Integer;
  95. begin
  96. if Assigned(ANode) then
  97. begin
  98. CopyBillsNodeData(ANode);
  99. for iChild := 0 to ANode.ChildCount - 1 do
  100. CopyBillsNode(ANode.ChildNodes[iChild]);
  101. end;
  102. end;
  103. procedure TBillsClipboard.CopyBillsNodeData(ANode: TsdIDTreeNode);
  104. var
  105. vNode: TBillsBlockNode;
  106. begin
  107. vNode := TBillsBlockNode.Create;
  108. FNodeList.Add(vNode);
  109. vNode.FID := ANode.Rec.ValueByName('ID').AsInteger;
  110. vNode.FParentID := ANode.Rec.ValueByName('ParentID').AsInteger;
  111. vNode.FNextSiblingID := ANode.Rec.ValueByName('NextSiblingID').AsInteger;
  112. vNode.FCode := ANode.Rec.ValueByName('Code').AsString;
  113. vNode.FB_Code := ANode.Rec.ValueByName('B_Code').AsString;
  114. vNode.FName := ANode.Rec.ValueByName('Name').AsString;
  115. vNode.FUnits := ANode.Rec.ValueByName('Units').AsString;
  116. vNode.FPrice := ANode.Rec.ValueByName('Price').AsFloat;
  117. vNode.FOrgQuantity := ANode.Rec.ValueByName('OrgQuantity').AsFloat;
  118. vNode.FMisQuantity := ANode.Rec.ValueByName('MisQuantity').AsFloat;
  119. vNode.FOthQuantity := ANode.Rec.ValueByName('OthQuantity').AsFloat;
  120. vNode.FQuantity := ANode.Rec.ValueByName('Quantity').AsFloat;
  121. vNode.FDrawingCode := ANode.Rec.ValueByName('DrawingCode').AsString;
  122. vNode.FAlias := ANode.Rec.ValueByName('Alias').AsString;
  123. vNode.FDgnQuantity1 := ANode.Rec.ValueByName('DgnQuantity1').AsFloat;
  124. vNode.FDgnQuantity2 := ANode.Rec.ValueByName('DgnQuantity2').AsFloat;
  125. vNode.FMemoStr := ANode.Rec.ValueByName('MemoStr').AsString;
  126. vNode.FDealCode := ANode.Rec.ValueByName('DealCode').AsString;
  127. vNode.FDealCompany := ANode.Rec.ValueByName('DealCompany').AsString;
  128. end;
  129. procedure TBillsClipboard.CopyData(ANode: TsdIDTreeNode; ACount: Integer);
  130. begin
  131. while Assigned(ANode) and (ACount > 0) do
  132. begin
  133. CopyBillsNode(ANode);
  134. Dec(ACount, ANode.PosterityCount + 1);
  135. ANode := ANode.NextSibling;
  136. end;
  137. end;
  138. constructor TBillsClipboard.Create(ABillsData: TBillsData);
  139. begin
  140. inherited Create;
  141. FBillsData := ABillsData;
  142. FNodeList := TList.Create;
  143. end;
  144. destructor TBillsClipboard.Destroy;
  145. begin
  146. ClearObjects(FNodeList);
  147. FNodeList.Free;
  148. inherited;
  149. end;
  150. procedure TBillsClipboard.LoadNodesFromXml(AXmlNode: IXMLNode);
  151. var
  152. BillsBlockNode: TBillsBlockNode;
  153. iNum: Integer;
  154. ParentXmlNode, CurXmlNode: IXMLNode;
  155. begin
  156. ParentXmlNode := AXmlNode.ChildNodes.FindNode('BillsBlockNodes');
  157. for iNum := 0 to ParentXmlNode.ChildNodes.Count - 1 do
  158. begin
  159. CurXmlNode := ParentXmlNode.ChildNodes.Nodes[iNum];
  160. BillsBlockNode := TBillsBlockNode.Create;
  161. FNodeList.Add(BillsBlockNode);
  162. BillsBlockNode.LoadFromXmlNode(CurXmlNode);
  163. end;
  164. end;
  165. procedure TBillsClipboard.LoadXmlData(AXmlDoc: IXmlDocument);
  166. begin
  167. inherited;
  168. LoadNodesFromXml(AXmlDoc.DocumentElement);
  169. end;
  170. procedure TBillsClipboard.Paste(ANode: TsdIDTreeNode; APos: Integer);
  171. begin
  172. Clear;
  173. LoadFromClipboard(CF_BillsBlock);
  174. LoadFromStream;
  175. PasteData(ANode, APos);
  176. end;
  177. procedure TBillsClipboard.PasteBillsNode(ANode: TsdIDTreeNode;
  178. APos: Integer; APasteNode: TBillsBlockNode);
  179. var
  180. stnCurrent: TsdIDTreeNode;
  181. begin
  182. stnCurrent := AddBillsNode(ANode, APos);
  183. PasteBillsNodeData(stnCurrent, APasteNode);
  184. PasteChildren(stnCurrent, APasteNode.FID);
  185. PasteNextSibling(stnCurrent, APasteNode.FNextSiblingID);
  186. end;
  187. procedure TBillsClipboard.PasteBillsNodeData(ANode: TsdIDTreeNode;
  188. APasteNode: TBillsBlockNode);
  189. begin
  190. ANode.Rec.ValueByName('Code').AsString := APasteNode.FCode;
  191. ANode.Rec.ValueByName('B_Code').AsString := APasteNode.FB_Code;
  192. ANode.Rec.ValueByName('Name').AsString := APasteNode.FName;
  193. ANode.Rec.ValueByName('Units').AsString := APasteNode.FUnits;
  194. ANode.Rec.ValueByName('Price').AsFloat := APasteNode.FPrice;
  195. ANode.Rec.ValueByName('OrgQuantity').AsFloat := QuantityRoundTo(APasteNode.FOrgQuantity);
  196. ANode.Rec.ValueByName('MisQuantity').AsFloat := QuantityRoundTo(APasteNode.FMisQuantity);
  197. ANode.Rec.ValueByName('OthQuantity').AsFloat := QuantityRoundTo(APasteNode.FOthQuantity);
  198. ANode.Rec.ValueByName('Quantity').AsFloat := QuantityRoundTo(APasteNode.FQuantity);
  199. ANode.Rec.ValueByName('DrawingCode').AsString := APasteNode.FDrawingCode;
  200. ANode.Rec.ValueByName('Alias').AsString := APasteNode.FAlias;
  201. ANode.Rec.ValueByName('DgnQuantity1').AsFloat := QuantityRoundTo(APasteNode.FDgnQuantity1);
  202. ANode.Rec.ValueByName('DgnQuantity2').AsFloat := QuantityRoundTo(APasteNode.FDgnQuantity2);
  203. ANode.Rec.ValueByName('MemoStr').AsString := APasteNode.FMemoStr;
  204. ANode.Rec.ValueByName('DealCode').AsString := APasteNode.FDealCode;
  205. ANode.Rec.ValueByName('DealCompany').AsString := APasteNode.FDealCompany;
  206. end;
  207. procedure TBillsClipboard.PasteChildren(AParentNode: TsdIDTreeNode;
  208. APasteParentID: Integer);
  209. var
  210. iNode: Integer;
  211. begin
  212. for iNode := 0 to FNodeList.Count - 1 do
  213. if TBillsBlockNode(FNodeList.Items[iNode]).FParentID = APasteParentID then
  214. begin
  215. PasteBillsNode(AParentNode, 0, TBillsBlockNode(FNodeList.Items[iNode]));
  216. Break;
  217. end;
  218. end;
  219. procedure TBillsClipboard.PasteData(ANode: TsdIDTreeNode; APos: Integer);
  220. var
  221. I: Integer;
  222. vPasteNode: TBillsBlockNode;
  223. begin
  224. PasteBillsNode(ANode, APos, TBillsBlockNode(FNodeList.First));
  225. end;
  226. procedure TBillsClipboard.PasteNextSibling(APreSibling: TsdIDTreeNode;
  227. APasteNodeID: Integer);
  228. var
  229. iNode: Integer;
  230. begin
  231. if APasteNodeID = -1 then Exit;
  232. for iNode := 0 to FNodeList.Count - 1 do
  233. if TBillsBlockNode(FNodeList.Items[iNode]).FID = APasteNodeID then
  234. begin
  235. PasteBillsNode(APreSibling, 1, TBillsBlockNode(FNodeList.Items[iNode]));
  236. Break;
  237. end;
  238. end;
  239. procedure TBillsClipboard.SaveNodesToXml(AXmlNode: IXMLNode);
  240. var
  241. ParentXmlNode, CurXmlNode: IXMLNode;
  242. iNum: Integer;
  243. BillsBlockNode: TBillsBlockNode;
  244. begin
  245. ParentXmlNode := AXmlNode.AddChild('BillsBlockNodes');
  246. for iNum := 0 to FNodeList.Count - 1 do
  247. begin
  248. BillsBlockNode := TBillsBlockNode(FNodeList.Items[iNum]);
  249. CurXmlNode := ParentXmlNode.AddChild('BillsBlockNode');
  250. BillsBlockNode.SaveToXmlNode(CurXmlNode);
  251. end;
  252. end;
  253. procedure TBillsClipboard.SaveXmlData(AXmlDoc: IXmlDocument);
  254. begin
  255. SaveNodesToXml(AXmlDoc.DocumentElement);
  256. inherited;
  257. end;
  258. { TBillsBlockNode }
  259. procedure TBillsBlockNode.LoadFromXmlNode(AXmlNode: IXMLNode);
  260. begin
  261. FID := AXmlNode.Attributes['ID'];
  262. FParentID := AXmlNode.Attributes['ParentID'];
  263. FNextSiblingID := AXmlNode.Attributes['NextSiblingID'];
  264. FCode := AXmlNode.Attributes['Code'];
  265. FB_Code := AXmlNode.Attributes['B_Code'];
  266. FName := AXmlNode.Attributes['Name'];
  267. FUnits := AXmlNode.Attributes['Units'];
  268. FPrice := AXmlNode.Attributes['Price'];
  269. FOrgQuantity := AXmlNode.Attributes['OrgQuantity'];
  270. FMisQuantity := AXmlNode.Attributes['MisQuantity'];
  271. FOthQuantity := AXmlNode.Attributes['OthQuantity'];
  272. FQuantity := AXmlNode.Attributes['Quantity'];
  273. FDrawingCode := AXmlNode.Attributes['DrawingCode'];
  274. FAlias := AXmlNode.Attributes['Alias'];
  275. FDgnQuantity1 := AXmlNode.Attributes['DgnQuantity1'];
  276. FDgnQuantity2 := AXmlNode.Attributes['DgnQuantity2'];
  277. FMemoStr := AXmlNode.Attributes['MemoStr'];
  278. FDealCode := AXmlNode.Attributes['DealCode'];
  279. FDealCompany := AXmlNode.Attributes['DealCompany'];
  280. end;
  281. procedure TBillsBlockNode.SaveToXmlNode(AXmlNode: IXMLNode);
  282. begin
  283. AXmlNode.Attributes['ID'] := FID;
  284. AXmlNode.Attributes['ParentID'] := FParentID;
  285. AXmlNode.Attributes['NextSiblingID'] := FNextSiblingID;
  286. AXmlNode.Attributes['Code'] := FCode;
  287. AXmlNode.Attributes['B_Code'] := FB_Code;
  288. AXmlNode.Attributes['Name'] := FName;
  289. AXmlNode.Attributes['Units'] := FUnits;
  290. AXmlNode.Attributes['Price'] := FPrice;
  291. AXmlNode.Attributes['OrgQuantity'] := FOrgQuantity;
  292. AXmlNode.Attributes['MisQuantity'] := FMisQuantity;
  293. AXmlNode.Attributes['OthQuantity'] := FOthQuantity;
  294. AXmlNode.Attributes['Quantity'] := FQuantity;
  295. AXmlNode.Attributes['DrawingCode'] := FDrawingCode;
  296. AXmlNode.Attributes['Alias'] := FAlias;
  297. AXmlNode.Attributes['DgnQuantity1'] := FDgnQuantity1;
  298. AXmlNode.Attributes['DgnQuantity2'] := FDgnQuantity2;
  299. AXmlNode.Attributes['MemoStr'] := FMemoStr;
  300. AXmlNode.Attributes['DealCode'] := FDealCode;
  301. AXmlNode.Attributes['DealCompany'] := FDealCompany;
  302. end;
  303. initialization
  304. CF_BillsBlock := RegisterClipboardFormat('Zh.BillsBlock');
  305. end.