OtherMeasurePhaseDm.pas 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. unit OtherMeasurePhaseDm;
  2. interface
  3. uses
  4. SysUtils, Classes, sdDB, sdProvider, ADODB, OMPhaseRecord;
  5. type
  6. TOtherMeasurePhaseData = class(TDataModule)
  7. sdpPhase: TsdADOProvider;
  8. sddPhase: TsdDataSet;
  9. sdvPhase: TsdDataView;
  10. procedure sdvPhaseBeforeAddRecord(ARecord: TsdDataRecord;
  11. var Allow: Boolean);
  12. procedure sdvPhaseAfterValueChanged(AValue: TsdValue);
  13. procedure sddPhaseGetRecordClass(var ARecordClass: TsdRecordClass);
  14. procedure sddPhaseAfterValueChanged(AValue: TsdValue);
  15. procedure sdvPhaseSetText(var Text: String; ARecord: TsdDataRecord;
  16. AValue: TsdValue; AColumn: TsdViewColumn; var Allow: Boolean);
  17. procedure sdvPhaseBeforeDeleteRecord(ARecord: TsdDataRecord;
  18. var Allow: Boolean);
  19. procedure sdvPhaseFilterRecord(ARecord: TsdDataRecord;
  20. var Allow: Boolean);
  21. procedure sdvPhaseGetText(var Text: String; ARecord: TsdDataRecord;
  22. AValue: TsdValue; AColumn: TsdViewColumn; DisplayText: Boolean);
  23. private
  24. FProjectData: TObject;
  25. FFilterDeletedRec: Boolean;
  26. function GetNewID: Integer;
  27. function GetNewSerialNo: Integer;
  28. procedure SaveDetailData;
  29. procedure BeforeBatchOperation;
  30. procedure AfterBatchOperation;
  31. procedure SetFilterDeletedRec(const Value: Boolean);
  32. public
  33. constructor Create(AProjectData: TObject);
  34. destructor Destroy; override;
  35. procedure Open(AConnection: TADOConnection);
  36. procedure Save;
  37. procedure LoadCurStageData;
  38. property FilterDeletedRec: Boolean read FFilterDeletedRec write SetFilterDeletedRec;
  39. end;
  40. implementation
  41. uses
  42. ProjectData, UtilMethods, Math, DB;
  43. {$R *.dfm}
  44. { TOtherMeasurePhaseData }
  45. procedure TOtherMeasurePhaseData.AfterBatchOperation;
  46. begin
  47. sddPhase.EndUpdate;
  48. sddPhase.EnableControls;
  49. sddPhase.AfterValueChanged := sddPhaseAfterValueChanged;
  50. end;
  51. procedure TOtherMeasurePhaseData.BeforeBatchOperation;
  52. begin
  53. sddPhase.AfterValueChanged := nil;
  54. sddPhase.DisableControls;
  55. sddPhase.BeginUpdate;
  56. end;
  57. constructor TOtherMeasurePhaseData.Create(AProjectData: TObject);
  58. begin
  59. inherited Create(nil);
  60. FProjectData := AProjectData;
  61. FFilterDeletedRec := True;
  62. end;
  63. destructor TOtherMeasurePhaseData.Destroy;
  64. begin
  65. inherited;
  66. end;
  67. function TOtherMeasurePhaseData.GetNewID: Integer;
  68. var
  69. idx: TsdIndex;
  70. begin
  71. idx := sddPhase.FindIndex('idxID');
  72. if idx.RecordCount > 0 then
  73. Result := idx.Records[idx.RecordCount - 1].ValueByName('ID').AsInteger + 1
  74. else
  75. Result := 1;
  76. end;
  77. function TOtherMeasurePhaseData.GetNewSerialNo: Integer;
  78. var
  79. idx: TsdIndex;
  80. begin
  81. idx := sddPhase.FindIndex('idxSerial');
  82. if idx.RecordCount > 0 then
  83. Result := idx.Records[idx.RecordCount - 1].ValueByName('SerialNo').AsInteger + 1
  84. else
  85. Result := 1;
  86. end;
  87. procedure TOtherMeasurePhaseData.LoadCurStageData;
  88. procedure LoadData;
  89. const
  90. sSelectSql = 'Select * From OMPhaseDetail Where (PhaseID = %d) and (StageID = %d)';
  91. var
  92. vQuery: TADOQuery;
  93. i: Integer;
  94. Rec: TOMPhaseRecord;
  95. begin
  96. try
  97. with TProjectData(FProjectData) do
  98. vQuery := QueryData(sdpPhase.Connection, Format(sSelectSql, [PhaseIndex, StageIndex]));
  99. for i := 0 to sddPhase.RecordCount - 1 do
  100. begin
  101. Rec := TOMPhaseRecord(sddPhase.Records[i]);
  102. if vQuery.Locate('ID', Rec.ValueByName('ID').AsInteger, []) then
  103. begin
  104. Rec.PreTotalPrice.AsFloat := vQuery.FieldByName('PreTotalPrice').AsFloat;
  105. Rec.CurTotalPrice.AsFloat := vQuery.FieldByName('CurTotalPrice').AsFloat;
  106. Rec.EndTotalPrice.AsFloat := vQuery.FieldByName('EndTotalPrice').AsFloat;
  107. end
  108. else
  109. begin
  110. Rec.PreTotalPrice.AsFloat := 0;
  111. Rec.CurTotalPrice.AsFloat := 0;
  112. Rec.EndTotalPrice.AsFloat := 0;
  113. end;
  114. end;
  115. finally
  116. vQuery.Free;
  117. end;
  118. end;
  119. begin
  120. BeforeBatchOperation;
  121. try
  122. LoadData;
  123. finally
  124. AfterBatchOperation;
  125. end;
  126. end;
  127. procedure TOtherMeasurePhaseData.Open(AConnection: TADOConnection);
  128. begin
  129. sdpPhase.Connection := AConnection;
  130. sddPhase.Open;
  131. if not Assigned(sddPhase.FindIndex('idxID')) then
  132. sddPhase.AddIndex('idxID', 'ID');
  133. if not Assigned(sddPhase.FindIndex('idxSerial')) then
  134. sddPhase.AddIndex('idxSerial', 'SerialNo');
  135. sdvPhase.Open;
  136. sdvPhase.IndexName := 'idxSerial';
  137. sdvPhase.Filtered := True;
  138. LoadCurStageData;
  139. end;
  140. procedure TOtherMeasurePhaseData.Save;
  141. begin
  142. sddPhase.Save;
  143. if not TProjectData(FProjectData).StageDataReadOnly then
  144. SaveDetailData;
  145. end;
  146. procedure TOtherMeasurePhaseData.SaveDetailData;
  147. const
  148. sClearSql = 'Delete From OMPhaseDetail where ID not in (Select distinct ID From OtherMeasurePhase)';
  149. sDeleteSql = 'Delete From OMPhaseDetail where (PhaseID = %d) and (StageID = %d)';
  150. sInsertSql = 'Insert Into OMPhaseDetail' +
  151. ' Select ID, %d As PhaseID, %d As StageID, PreTotalPrice, CurTotalPrice, EndTotalPrice' +
  152. ' From OtherMeasurePhase Where not Deleted';
  153. var
  154. iPhase, iStage: Integer;
  155. vQuery: TADOQuery;
  156. begin
  157. if TProjectData(FProjectData).StageDataReadOnly then Exit;
  158. // 被删除的数据,也不应保留明细
  159. ExecuteSql(sdpPhase.Connection, sClearSql);
  160. iPhase := TProjectData(FProjectData).PhaseIndex;
  161. iStage := TProjectData(FProjectData).StageIndex;
  162. ExecuteSql(sdpPhase.Connection, Format(sDeleteSql, [iPhase, iStage]));
  163. ExecuteSql(sdpPhase.Connection, Format(sInsertSql, [iPhase, iStage]));
  164. ExecuteSql(sdpPhase.Connection, Format(sDeleteSql, [iPhase, -1]));
  165. ExecuteSql(sdpPhase.Connection, Format(sInsertSql, [iPhase, -1]));
  166. end;
  167. procedure TOtherMeasurePhaseData.sdvPhaseBeforeAddRecord(
  168. ARecord: TsdDataRecord; var Allow: Boolean);
  169. begin
  170. ARecord.ValueByName('ID').AsInteger := GetNewID;
  171. ARecord.ValueByName('SerialNo').AsInteger := GetNewSerialNo;
  172. ARecord.ValueByName('CreatePhaseID').AsInteger := TProjectData(FProjectData).PhaseIndex;
  173. ARecord.ValueByName('CreateStageID').AsInteger := TProjectData(FProjectData).StageIndex;
  174. end;
  175. procedure TOtherMeasurePhaseData.sdvPhaseAfterValueChanged(
  176. AValue: TsdValue);
  177. var
  178. Rec: TOMPhaseRecord;
  179. begin
  180. if SameText(AValue.FieldName, 'CurTotalPrice') then
  181. begin
  182. Rec := TOMPhaseRecord(AValue.Owner);
  183. Rec.SetFloatValue(Rec.EndTotalPrice, TotalPriceRoundTo(Rec.CurTotalPrice.AsFloat + Rec.PreTotalPrice.AsFloat));
  184. end;
  185. end;
  186. procedure TOtherMeasurePhaseData.sddPhaseGetRecordClass(
  187. var ARecordClass: TsdRecordClass);
  188. begin
  189. ARecordClass := TOMPhaseRecord;
  190. end;
  191. procedure TOtherMeasurePhaseData.sddPhaseAfterValueChanged(
  192. AValue: TsdValue);
  193. var
  194. Rec: TOMPhaseRecord;
  195. begin
  196. if SameText(AValue.FieldName, 'CurTotalPrice') then
  197. begin
  198. Rec := TOMPhaseRecord(AValue.Owner);
  199. Rec.SetFloatValue(Rec.EndTotalPrice, TotalPriceRoundTo(Rec.CurTotalPrice.AsFloat + Rec.PreTotalPrice.AsFloat));
  200. Rec.SetFloatValue(Rec.AddTotalPrice, Rec.EndTotalPrice.AsFloat);
  201. end;
  202. end;
  203. procedure TOtherMeasurePhaseData.sdvPhaseSetText(var Text: String;
  204. ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
  205. var Allow: Boolean);
  206. begin
  207. if SameText(AColumn.FieldName, 'Code') then
  208. begin
  209. if (Text = '') then
  210. begin
  211. if (ARecord.ValueByName('Name').AsString = '') then
  212. DataSetErrorMessage(Allow, '编号和名称不可同时为空。');
  213. if (ARecord.ValueByName('Code').AsString = '') and (ARecord.ValueByName('Name').AsString = '') then
  214. sddPhase.Remove(ARecord);
  215. end;
  216. end
  217. else if SameText(AColumn.FieldName, 'Name') then
  218. begin
  219. if (Text = '') then
  220. begin
  221. if (ARecord.ValueByName('Code').AsString = '') then
  222. DataSetErrorMessage(Allow, '编号和名称不可同时为空。');
  223. if(ARecord.ValueByName('Code').AsString = '') and (ARecord.ValueByName('Name').AsString = '') then
  224. sddPhase.Remove(ARecord);
  225. end;
  226. end
  227. else
  228. begin
  229. if (ARecord.ValueByName('Code').AsString = '') and (ARecord.ValueByName('Name').AsString = '') then
  230. begin
  231. DataSetErrorMessage(Allow, '编号、名称不可同时为空,请先填写编号或名称,再填写其他信息。');
  232. sddPhase.Remove(AValue.Owner);
  233. end;
  234. end;
  235. end;
  236. procedure TOtherMeasurePhaseData.sdvPhaseBeforeDeleteRecord(
  237. ARecord: TsdDataRecord; var Allow: Boolean);
  238. function CanDelete(ARec: TsdDataRecord): Boolean;
  239. begin
  240. Result := (ARec.ValueByName('CreatePhaseID').AsInteger = TProjectData(FProjectData).PhaseIndex) and
  241. (ARec.ValueByName('CreateStageID').AsInteger = TProjectData(FProjectData).StageIndex);
  242. end;
  243. begin
  244. Allow := CanDelete(ARecord);
  245. if not Allow then
  246. begin
  247. ARecord.ValueByName('Deleted').AsBoolean := True;
  248. ARecord.ValueByName('DeletePhaseID').AsInteger := TProjectData(FProjectData).PhaseIndex;
  249. ARecord.ValueByName('DeleteStageID').AsInteger := TProjectData(FProjectData).StageIndex;
  250. sdvPhase.RefreshFilter;
  251. end;
  252. end;
  253. procedure TOtherMeasurePhaseData.sdvPhaseFilterRecord(
  254. ARecord: TsdDataRecord; var Allow: Boolean);
  255. begin
  256. if Assigned(ARecord) then
  257. Allow := not ARecord.ValueByName('Deleted').AsBoolean;
  258. end;
  259. procedure TOtherMeasurePhaseData.SetFilterDeletedRec(const Value: Boolean);
  260. begin
  261. FFilterDeletedRec := Value;
  262. sdvPhase.Filtered := FFilterDeletedRec;
  263. end;
  264. procedure TOtherMeasurePhaseData.sdvPhaseGetText(var Text: String;
  265. ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
  266. DisplayText: Boolean);
  267. procedure GetDisplayText;
  268. begin
  269. if AValue.DataType = ftFloat then
  270. if AValue.AsFloat = 0 then
  271. Text := '';
  272. end;
  273. begin
  274. if DisplayText then
  275. GetDisplayText;
  276. end;
  277. end.