BGLSelectFrm.pas 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. unit BGLSelectFrm;
  2. interface
  3. uses
  4. BGLDm,
  5. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  6. Dialogs, StdCtrls, ZjGridDBA, ZJGrid, ExtCtrls, ZJCells;
  7. const
  8. sBGInfo = '当前变更清单:%s 变更数量:%s';
  9. type
  10. TBGLSelectForm = class(TForm)
  11. lblBGInfo: TLabel;
  12. lblBGLTitel: TLabel;
  13. pnlBGLBar: TPanel;
  14. zgBGL: TZJGrid;
  15. btnOk: TButton;
  16. btnCancel: TButton;
  17. cbIncludeZeroQuantity: TCheckBox;
  18. procedure zgBGLCellCanEdit(Sender: TObject; const ACoord: TPoint;
  19. var Allow: Boolean);
  20. procedure btnOkClick(Sender: TObject);
  21. procedure zgBGLCellGetColor(Sender: TObject; ACoord: TPoint;
  22. var AColor: TColor);
  23. procedure zgBGLCellTextChanging(Sender: TObject; const ACoord: TPoint;
  24. var NewValue: String; var Accept: Boolean);
  25. procedure zgBGLCellValueChanged(Sender: TObject);
  26. procedure cbIncludeZeroQuantityClick(Sender: TObject);
  27. private
  28. FOrgBGL: TBGLSelectInfo;
  29. FProjectData: TObject;
  30. FBGLData: TBGLData;
  31. FGclCode: string;
  32. FBGNum: Double;
  33. FHasBGNum: Boolean;
  34. function GetTotalBGNum: Double;
  35. procedure InitBGList;
  36. function GetIncludeZeroQuantity: Boolean;
  37. public
  38. procedure Init(AOrgBGL: TBGLSelectInfo; ProjectData: TObject; ABGNum: Double);
  39. procedure LoadCurBGL(ACurBGL: TBGLSelectInfo);
  40. property HasBGNum: Boolean read FHasBGNum write FHasBGNum;
  41. property IncludeZeroQuantity: Boolean read GetIncludeZeroQuantity;
  42. end;
  43. function SelectBGL(AOrgBGL, ACurBGL: TBGLSelectInfo; AProjectData: TObject): Boolean;
  44. function SelectBGLAndBGNum(AOrgBGL, ACurBGL: TBGLSelectInfo; AProjectData: TObject): Boolean;
  45. implementation
  46. uses
  47. Math, ProjectData, sdIDTree, UtilMethods, ZhAPI;
  48. {$R *.dfm}
  49. function SelectBGL(AOrgBGL, ACurBGL: TBGLSelectInfo; AProjectData: TObject): Boolean;
  50. var
  51. FBGLSelectForm: TBGLSelectForm;
  52. begin
  53. FBGLSelectForm := TBGLSelectForm.Create(nil);
  54. try
  55. FBGLSelectForm.HasBGNum := True;
  56. FBGLSelectForm.Init(AOrgBGL, AProjectData, ACurBGL.TotalNum);
  57. if FBGLSelectForm.ShowModal = mrOk then
  58. begin
  59. Result := True;
  60. FBGLSelectForm.LoadCurBGL(ACurBGL);
  61. end
  62. else
  63. Result := False;
  64. finally
  65. FBGLSelectForm.Free;
  66. end;
  67. end;
  68. function SelectBGLAndBGNum(AOrgBGL, ACurBGL: TBGLSelectInfo; AProjectData: TObject): Boolean;
  69. var
  70. FBGLSelectForm: TBGLSelectForm;
  71. begin
  72. FBGLSelectForm := TBGLSelectForm.Create(nil);
  73. try
  74. FBGLSelectForm.HasBGNum := False;
  75. FBGLSelectForm.Init(AOrgBGL, AProjectData, ACurBGL.TotalNum);
  76. if FBGLSelectForm.ShowModal = mrOk then
  77. begin
  78. Result := True;
  79. FBGLSelectForm.LoadCurBGL(ACurBGL);
  80. end
  81. else
  82. Result := False;
  83. finally
  84. FBGLSelectForm.Free;
  85. end;
  86. end;
  87. { TBGLSelectForm }
  88. procedure TBGLSelectForm.Init(AOrgBGL: TBGLSelectInfo;
  89. ProjectData: TObject; ABGNum: Double);
  90. begin
  91. ClientHeight := 281;
  92. ClientWidth := 475;
  93. FProjectData := ProjectData;
  94. with TProjectData(FProjectData).BillsMeasureData.BillsMeasureTree do
  95. FGclCode := Selected.Rec.ValueByName('B_Code').AsString;
  96. FBGLData := TProjectData(FProjectData).BGLData;
  97. FOrgBGL := AOrgBGL;
  98. FBGNum := ABGNum;
  99. lblBGInfo.Caption := Format(sBGInfo, [FGclCode, FloatToStr(FBGNum)]);
  100. InitBGList;
  101. end;
  102. procedure TBGLSelectForm.InitBGList;
  103. procedure InitBGListHead;
  104. begin
  105. zgBGL.ColCount := 6;
  106. zgBGL.CellClass.Cols[1] := TZjCheckBoxCell;
  107. zgBGL.Cells[1, 0].Text := '选择';
  108. zgBGL.ColWidths[1] := 35;
  109. zgBGL.Cells[2, 0].Text := '变更令号';
  110. zgBGL.ColWidths[2] := 90;
  111. zgBGL.Cells[3, 0].Text := '名称';
  112. zgBGL.ColWidths[3] := 150;
  113. zgBGL.Cells[4, 0].Text := '可变更数量';
  114. zgBGL.ColWidths[4] := 80;
  115. zgBGL.Cells[5, 0].Text := '变更数量';
  116. zgBGL.ColWidths[5] := 60;
  117. zgBGL.RowCount := 1;
  118. end;
  119. function GetOrgQuantity(const ABGLCode: string): Double;
  120. var
  121. I: Integer;
  122. begin
  123. Result := 0;
  124. for I := 0 to FOrgBGL.Count - 1 do
  125. begin
  126. if SameText(FOrgBGL.Codes[I], ABGLCode) then
  127. begin
  128. Result := StrToFloatDef(FOrgBGL.Nums[I], 0);
  129. Break;
  130. end;
  131. end;
  132. end;
  133. procedure AddBGL;
  134. var
  135. fValidQuantity: Double;
  136. begin
  137. with FBGLData do
  138. begin
  139. fValidQuantity := QuantityRoundTo(
  140. cdsBGBillsQuantity.AsFloat - cdsBGBillsUsedQuantity.AsFloat
  141. + GetOrgQuantity(cdsBGLCode.AsString));
  142. if not IncludeZeroQuantity and (fValidQuantity = 0) then Exit;
  143. zgBGL.RowCount := zgBGL.RowCount + 1;
  144. zgBGL.Cells[2, zgBGL.RowCount -1].Text := cdsBGLCode.AsString;
  145. zgBGL.Cells[3, zgBGL.RowCount -1].Text := cdsBGLName.AsString;
  146. zgBGL.Cells[4, zgBGL.RowCount -1].Text := FloatToStr(fValidQuantity);
  147. zgBGL.Rows[zgBGL.RowCount -1].Data := Pointer(cdsBGBillsID.AsInteger);
  148. end;
  149. end;
  150. begin
  151. InitBGListHead;
  152. FBGLData.cdsBGL.First;
  153. while not FBGLData.cdsBGL.Eof do
  154. begin
  155. FBGLData.cdsBGBills.Filter := 'BGID = ' + IntToStr(FBGLData.cdsBGLID.AsInteger);
  156. FBGLData.cdsBGBills.Filtered := True;
  157. try
  158. FBGLData.cdsBGBills.First;
  159. while not FBGLData.cdsBGBills.Eof do
  160. begin
  161. if SameText(FBGLData.cdsBGBillsB_Code.AsString, FOrgBGL.B_Code) and
  162. SameText(FBGLData.cdsBGBillsName.AsString, FOrgBGL.Name) and
  163. SameText(FBGLData.cdsBGBillsUnits.AsString, FOrgBGL.Units) and
  164. (PriceRoundTo(FBGLData.cdsBGBillsPrice.AsFloat - FOrgBGL.Price) = 0) then
  165. AddBGL;
  166. FBGLData.cdsBGBills.Next;
  167. end;
  168. finally
  169. FBGLData.cdsBGBills.Filtered := False;
  170. end;
  171. FBGLData.cdsBGL.Next;
  172. end;
  173. end;
  174. procedure TBGLSelectForm.LoadCurBGL(ACurBGL: TBGLSelectInfo);
  175. var
  176. iRowIndex: Integer;
  177. begin
  178. ACurBGL.TotalNum := GetTotalBGNum;
  179. for iRowIndex := 0 to zgBGL.RowCount - 1 do
  180. if zgBGL.Cells[2, iRowIndex].Text <> '' then
  181. begin
  182. if SameText(zgBGL.Cells[1, iRowIndex].Text, 'True') then
  183. begin
  184. ACurBGL.Codes.Add(zgBGL.Cells[2, iRowIndex].Text);
  185. ACurBGL.Nums.Add(zgBGL.Cells[5, iRowIndex].Text);
  186. end;
  187. end;
  188. end;
  189. procedure TBGLSelectForm.zgBGLCellCanEdit(Sender: TObject;
  190. const ACoord: TPoint; var Allow: Boolean);
  191. begin
  192. Allow := (ACoord.X = 1) or
  193. ((ACoord.X = 5) and SameText(zgBGL.Cells[1, ACoord.Y].Text, 'True'));
  194. end;
  195. procedure TBGLSelectForm.btnOkClick(Sender: TObject);
  196. function CheckSelect: Boolean;
  197. var
  198. iRowIndex: Integer;
  199. begin
  200. Result := False;
  201. for iRowIndex := 0 to zgBGL.RowCount - 1 do
  202. if zgBGL.Cells[2, iRowIndex].Text <> '' then
  203. if SameText(zgBGL.Cells[1, iRowIndex].Text, 'True') then
  204. begin
  205. Result := True;
  206. Break;
  207. end;
  208. end;
  209. begin
  210. if not CheckSelect then
  211. ErrorMessage('请选择变更令!')
  212. else if (FBGNum <> GetTotalBGNum) then
  213. ErrorMessage('所选变更令的变更数量合计必须等于清单的变更数量')
  214. else
  215. ModalResult := mrOk;
  216. end;
  217. function TBGLSelectForm.GetTotalBGNum: Double;
  218. var
  219. iRowIndex: Integer;
  220. begin
  221. Result := 0;
  222. for iRowIndex := 0 to zgBGL.RowCount - 1 do
  223. if zgBGL.Cells[2, iRowIndex].Text <> '' then
  224. if SameText(zgBGL.Cells[1, iRowIndex].Text, 'True') then
  225. Result := Result + StrToFloatDef(zgBGL.Cells[5, iRowIndex].Text, 0);
  226. end;
  227. procedure TBGLSelectForm.zgBGLCellGetColor(Sender: TObject; ACoord: TPoint;
  228. var AColor: TColor);
  229. var
  230. fValidValue, fValue: Double;
  231. begin
  232. if ACoord.X = 5 then
  233. begin
  234. fValidValue := StrToFloatDef(zgBGL.Cells[4, ACoord.Y].Text, 0);
  235. fValue := StrToFloatDef(zgBGL.Cells[5, ACoord.Y].Text, 0);
  236. if ((fValidValue > 0) and (fValue > fValidValue)) or
  237. ((fValidValue < 0) and (fValue < fValidValue)) then
  238. AColor := clRed;
  239. end;
  240. end;
  241. procedure TBGLSelectForm.zgBGLCellTextChanging(Sender: TObject;
  242. const ACoord: TPoint; var NewValue: String; var Accept: Boolean);
  243. procedure AutoGetBGNum(ARowIndex: Integer);
  244. var
  245. fAvailableNum: Double;
  246. begin
  247. if HasBGNum then
  248. fAvailableNum := FBGNum - GetTotalBGNum
  249. else
  250. fAvailableNum := StrToFloatDef(zgBGL.Cells[4, ARowIndex].Text, 0);
  251. // 当变更数量为负数时, 例如-60,但计算得到可变更数量为-80,此时应取两值中较大的-60
  252. if FBGNum < 0 then
  253. zgBGL.Cells[5, ARowIndex].Text := FloatToStr(
  254. Max(fAvailableNum, StrToFloatDef(zgBGL.Cells[4, ARowIndex].Text, 0)))
  255. else
  256. zgBGL.Cells[5, ARowIndex].Text := FloatToStr(
  257. Min(fAvailableNum, StrToFloatDef(zgBGL.Cells[4, ARowIndex].Text, 0)));
  258. end;
  259. begin
  260. if ACoord.X = 1 then
  261. if NewValue = 'True' then
  262. AutoGetBGNum(ACoord.Y)
  263. else
  264. zgBGL.Cells[5, ACoord.Y].Text := '';
  265. end;
  266. procedure TBGLSelectForm.zgBGLCellValueChanged(Sender: TObject);
  267. procedure RefreshBGNum;
  268. begin
  269. FBGNum := GetTotalBGNum;
  270. lblBGInfo.Caption := Format(sBGInfo, [FGclCode, FloatToStr(FBGNum)]);
  271. end;
  272. var
  273. fCur, fValid: Double;
  274. begin
  275. if zgBGL.CurCol = 5 then
  276. begin
  277. zgBGL.OnCellValueChanged := nil;
  278. if zgBGL.Cells[5, zgBGL.CurRow].Text <> '' then
  279. begin
  280. fCur := QuantityRoundTo(StrToFloatDef(zgBGL.Cells[5, zgBGL.CurRow].Text, 0));
  281. fValid := StrToFloatDef(zgBGL.Cells[4, zgBGL.CurRow].Text, 0);
  282. if ((fValid >= 0) and (QuantityRoundTo(fValid - fCur) >= 0))) or
  283. ((fValid < 0) and (QuantityRoundTo(fCur - fValid) >= 0))) then
  284. zgBGL.Cells[5, zgBGL.CurRow].Text := FloatToStr(fCur)
  285. else
  286. begin
  287. ErrorMessage('当前输入的变更数量大于可变更数量,请重新输入!');
  288. zgBGL.Cells[5, zgBGL.CurRow].Text := '';
  289. end;
  290. end;
  291. zgBGL.OnCellValueChanged := zgBGLCellValueChanged;
  292. end;
  293. if not HasBGNum and ((zgBGL.CurCol = 1) or (zgBGL.CurCol = 5)) then
  294. RefreshBGNum;
  295. end;
  296. procedure TBGLSelectForm.cbIncludeZeroQuantityClick(Sender: TObject);
  297. begin
  298. InitBGList;
  299. end;
  300. function TBGLSelectForm.GetIncludeZeroQuantity: Boolean;
  301. begin
  302. Result := cbIncludeZeroQuantity.Checked;
  303. end;
  304. end.