OtherMeasurePhaseDm.pas 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. private
  18. FProjectData: TObject;
  19. function GetNewID: Integer;
  20. function GetNewSerialNo: Integer;
  21. procedure SaveDetailData;
  22. procedure BeforeBatchOperation;
  23. procedure AfterBatchOperation;
  24. public
  25. constructor Create(AProjectData: TObject);
  26. destructor Destroy; override;
  27. procedure Open(AConnection: TADOConnection);
  28. procedure Save;
  29. procedure LoadCurStageData;
  30. end;
  31. implementation
  32. uses
  33. ProjectData, UtilMethods, Math;
  34. {$R *.dfm}
  35. { TOtherMeasurePhaseData }
  36. procedure TOtherMeasurePhaseData.AfterBatchOperation;
  37. begin
  38. sddPhase.EndUpdate;
  39. sddPhase.EnableControls;
  40. sddPhase.AfterValueChanged := sddPhaseAfterValueChanged;
  41. end;
  42. procedure TOtherMeasurePhaseData.BeforeBatchOperation;
  43. begin
  44. sddPhase.AfterValueChanged := nil;
  45. sddPhase.DisableControls;
  46. sddPhase.BeginUpdate;
  47. end;
  48. constructor TOtherMeasurePhaseData.Create(AProjectData: TObject);
  49. begin
  50. inherited Create(nil);
  51. FProjectData := AProjectData;
  52. end;
  53. destructor TOtherMeasurePhaseData.Destroy;
  54. begin
  55. inherited;
  56. end;
  57. function TOtherMeasurePhaseData.GetNewID: Integer;
  58. var
  59. idx: TsdIndex;
  60. begin
  61. idx := sddPhase.FindIndex('idxID');
  62. if idx.RecordCount > 0 then
  63. Result := idx.Records[idx.RecordCount - 1].ValueByName('ID').AsInteger + 1
  64. else
  65. Result := 1;
  66. end;
  67. function TOtherMeasurePhaseData.GetNewSerialNo: Integer;
  68. var
  69. idx: TsdIndex;
  70. begin
  71. idx := sddPhase.FindIndex('idxSerial');
  72. if idx.RecordCount > 0 then
  73. Result := idx.Records[idx.RecordCount - 1].ValueByName('SerialNo').AsInteger + 1
  74. else
  75. Result := 1;
  76. end;
  77. procedure TOtherMeasurePhaseData.LoadCurStageData;
  78. procedure LoadData;
  79. const
  80. sSelectSql = 'Select * From OMPhaseDetail Where (PhaseID = %d) and (StageID = %d)';
  81. var
  82. vQuery: TADOQuery;
  83. i: Integer;
  84. Rec: TOMPhaseRecord;
  85. begin
  86. try
  87. with TProjectData(FProjectData) do
  88. vQuery := QueryData(sdpPhase.Connection, Format(sSelectSql, [PhaseIndex, StageIndex]));
  89. for i := 0 to sddPhase.RecordCount - 1 do
  90. begin
  91. Rec := TOMPhaseRecord(sddPhase.Records[i]);
  92. if vQuery.Locate('ID', Rec.ValueByName('ID').AsInteger, []) then
  93. begin
  94. Rec.PreTotalPrice.AsFloat := vQuery.FieldByName('PreTotalPrice').AsFloat;
  95. Rec.CurTotalPrice.AsFloat := vQuery.FieldByName('CurTotalPrice').AsFloat;
  96. Rec.EndTotalPrice.AsFloat := vQuery.FieldByName('EndTotalPrice').AsFloat;
  97. end
  98. else
  99. begin
  100. Rec.PreTotalPrice.AsFloat := 0;
  101. Rec.CurTotalPrice.AsFloat := 0;
  102. Rec.EndTotalPrice.AsFloat := 0;
  103. end;
  104. end;
  105. finally
  106. vQuery.Free;
  107. end;
  108. end;
  109. begin
  110. BeforeBatchOperation;
  111. try
  112. LoadData;
  113. finally
  114. AfterBatchOperation;
  115. end;
  116. end;
  117. procedure TOtherMeasurePhaseData.Open(AConnection: TADOConnection);
  118. begin
  119. sdpPhase.Connection := AConnection;
  120. sddPhase.Open;
  121. if not Assigned(sddPhase.FindIndex('idxID')) then
  122. sddPhase.AddIndex('idxID', 'ID');
  123. if not Assigned(sddPhase.FindIndex('idxSerial')) then
  124. sddPhase.AddIndex('idxSerial', 'SerialNo');
  125. sdvPhase.Open;
  126. sdvPhase.IndexName := 'idxSerial';
  127. LoadCurStageData;
  128. end;
  129. procedure TOtherMeasurePhaseData.Save;
  130. begin
  131. sddPhase.Save;
  132. if not TProjectData(FProjectData).StageDataReadOnly then
  133. SaveDetailData;
  134. end;
  135. procedure TOtherMeasurePhaseData.SaveDetailData;
  136. const
  137. sClearSql = 'Delete From OMPhaseDetail where ID not in (Select distinct ID From OtherMeasurePhase)';
  138. sDeleteSql = 'Delete From OMPhaseDetail where (PhaseID = %d) and (StageID = %d)';
  139. sInsertSql = 'Insert Into OMPhaseDetail' +
  140. ' Select ID, %d As PhaseID, %d As StageID, PreTotalPrice, CurTotalPrice, EndTotalPrice' +
  141. ' From OtherMeasurePhase';
  142. var
  143. iPhase, iStage: Integer;
  144. vQuery: TADOQuery;
  145. begin
  146. if TProjectData(FProjectData).StageDataReadOnly then Exit;
  147. // 被删除的数据,也不应保留明细
  148. ExecuteSql(sdpPhase.Connection, sClearSql);
  149. iPhase := TProjectData(FProjectData).PhaseIndex;
  150. iStage := TProjectData(FProjectData).StageIndex;
  151. ExecuteSql(sdpPhase.Connection, Format(sDeleteSql, [iPhase, iStage]));
  152. ExecuteSql(sdpPhase.Connection, Format(sInsertSql, [iPhase, iStage]));
  153. ExecuteSql(sdpPhase.Connection, Format(sDeleteSql, [iPhase, -1]));
  154. ExecuteSql(sdpPhase.Connection, Format(sInsertSql, [iPhase, -1]));
  155. end;
  156. procedure TOtherMeasurePhaseData.sdvPhaseBeforeAddRecord(
  157. ARecord: TsdDataRecord; var Allow: Boolean);
  158. begin
  159. ARecord.ValueByName('ID').AsInteger := GetNewID;
  160. ARecord.ValueByName('SerialNo').AsInteger := GetNewSerialNo;
  161. ARecord.ValueByName('CreatePhaseID').AsInteger := TProjectData(FProjectData).PhaseIndex;
  162. ARecord.ValueByName('CreateStageID').AsInteger := TProjectData(FProjectData).StageIndex;
  163. end;
  164. procedure TOtherMeasurePhaseData.sdvPhaseAfterValueChanged(
  165. AValue: TsdValue);
  166. var
  167. Rec: TOMPhaseRecord;
  168. begin
  169. if SameText(AValue.FieldName, 'CurTotalPrice') then
  170. begin
  171. Rec := TOMPhaseRecord(AValue.Owner);
  172. Rec.SetFloatValue(Rec.EndTotalPrice, TotalPriceRoundTo(Rec.CurTotalPrice.AsFloat + Rec.PreTotalPrice.AsFloat));
  173. end;
  174. end;
  175. procedure TOtherMeasurePhaseData.sddPhaseGetRecordClass(
  176. var ARecordClass: TsdRecordClass);
  177. begin
  178. ARecordClass := TOMPhaseRecord;
  179. end;
  180. procedure TOtherMeasurePhaseData.sddPhaseAfterValueChanged(
  181. AValue: TsdValue);
  182. var
  183. Rec: TOMPhaseRecord;
  184. begin
  185. if SameText(AValue.FieldName, 'CurTotalPrice') then
  186. begin
  187. Rec := TOMPhaseRecord(AValue.Owner);
  188. Rec.SetFloatValue(Rec.EndTotalPrice, TotalPriceRoundTo(Rec.CurTotalPrice.AsFloat + Rec.PreTotalPrice.AsFloat));
  189. Rec.SetFloatValue(Rec.AddTotalPrice, Rec.EndTotalPrice.AsFloat);
  190. end;
  191. end;
  192. procedure TOtherMeasurePhaseData.sdvPhaseSetText(var Text: String;
  193. ARecord: TsdDataRecord; AValue: TsdValue; AColumn: TsdViewColumn;
  194. var Allow: Boolean);
  195. begin
  196. if SameText(AColumn.FieldName, 'Code') then
  197. begin
  198. if (Text = '') then
  199. begin
  200. if (ARecord.ValueByName('Name').AsString = '') then
  201. DataSetErrorMessage(Allow, '编号和名称不可同时为空。');
  202. if (ARecord.ValueByName('Code').AsString = '') and (ARecord.ValueByName('Name').AsString = '') then
  203. sddPhase.Remove(ARecord);
  204. end;
  205. end
  206. else if SameText(AColumn.FieldName, 'Name') then
  207. begin
  208. if (Text = '') then
  209. begin
  210. if (ARecord.ValueByName('Code').AsString = '') then
  211. DataSetErrorMessage(Allow, '编号和名称不可同时为空。');
  212. if(ARecord.ValueByName('Code').AsString = '') and (ARecord.ValueByName('Name').AsString = '') then
  213. sddPhase.Remove(ARecord);
  214. end;
  215. end
  216. else
  217. begin
  218. if (ARecord.ValueByName('Code').AsString = '') and (ARecord.ValueByName('Name').AsString = '') then
  219. begin
  220. DataSetErrorMessage(Allow, '编号、名称不可同时为空,请先填写编号或名称,再填写其他信息。');
  221. sddPhase.Remove(AValue.Owner);
  222. end;
  223. end;
  224. end;
  225. end.