unit dmAccu; interface uses SysUtils, Classes, DBXpress, DB, SqlExpr, FMTBcd, DBClient, Provider,SqlTimSt, kbmMemTable,Dialogs,Variants, DBTables,ADODB,inifiles,DateUtils; type TdmAcc = class(TDataModule) dsrVou: TDataSource; dsrVouItems: TDataSource; memTblAccItems: TkbmMemTable; DsrAccItems: TDataSource; ADOTblVou: TADOTable; ADOTblVouitems: TADOTable; ADOQueryTemp: TADOQuery; tempQuery: TADOQuery; ADOTblVouitemsORDNO: TIntegerField; ADOTblVouitemsITEMNO: TIntegerField; ADOTblVouitemsACCID: TStringField; ADOTblVouitemsPACCID: TStringField; ADOTblVouitemsACCNAME: TStringField; ADOTblVouitemsPROPERTY: TSmallintField; ADOTblVouitemsGRADE: TSmallintField; ADOTblVouitemsDC: TStringField; ADOTblVouitemsSUBLEGTYPE: TStringField; ADOTblVouitemsAMTDR: TFloatField; ADOTblVouitemsAMTCR: TFloatField; ADOTblVouitemsFCY: TStringField; ADOTblVouitemsFCYNO: TStringField; ADOTblVouitemsFCYEXRATE: TFloatField; ADOTblVouitemsFCYDR: TFloatField; ADOTblVouitemsFCYCR: TFloatField; ADOTblVouitemsQTY: TStringField; ADOTblVouitemsQTYSPECS: TStringField; ADOTblVouitemsQTYUNIT: TStringField; ADOTblVouitemsPRICEDR: TFloatField; ADOTblVouitemsPRICECR: TFloatField; ADOTblVouitemsQTYDR: TFloatField; ADOTblVouitemsQTYCR: TFloatField; ADOTblVouitemsEXPLAN: TStringField; ADOTblVouitemsVERIFYED: TStringField; ADOTblVouitemsVERIFYLASTBLC: TFloatField; ADOTblVouitemsDEPTACC: TStringField; ADOTblVouitemsEMPLACC: TStringField; ADOTblVouitemsCORPACC: TStringField; ADOTblVouitemsITEMACC: TStringField; ADOTblVouitemsDEPTID: TStringField; ADOTblVouitemsEMPLID: TStringField; ADOTblVouitemsCORPID: TStringField; ADOTblVouitemsPITEMID: TIntegerField; ADOTblVouitemsITEMID: TIntegerField; ADOTblVouitemsFIELD1: TStringField; ADOTblVouitemsFIELD2: TStringField; ADOTblVouitemsFIELD3: TStringField; ADOTblVouitemsFIELD4: TStringField; ADOTblVouitemsFIELD5: TStringField; ADOTblVouitemsFIELD6: TStringField; ADOTblVouitemsFIELD7: TStringField; ADOTblVouitemsCF: TSmallintField; ADOTblVouitemsCUSTBE: TStringField; ADOTblVouitemsCORPBF: TStringField; ADOTblVouitemsSTATUS: TSmallintField; ADOTblVouitemsBSNO: TStringField; ADOTblVouitemsMBLNO: TStringField; ADOTblVouitemsFSETTLCODE: TStringField; ADOTblVouitemsFSETTLENO: TStringField; ADOTblVouitemsEMPL: TStringField; ADOTblVouitemsDEPT: TStringField; procedure cdsVouNewRecord(DataSet: TDataSet); procedure cdsVouItemsNewRecord(DataSet: TDataSet); procedure DataModuleCreate(Sender: TObject); procedure cdsVouItemsAfterPost(DataSet: TDataSet); procedure cdsVouItemsAfterDelete(DataSet: TDataSet); procedure cdsVouItemsAfterOpen(DataSet: TDataSet); procedure tblVouItemsBeforeClose(DataSet: TDataSet); procedure tblVouItemsBeforePost(DataSet: TDataSet); procedure tblVouItemsCalcFields(DataSet: TDataSet); procedure ADOTblVouNewRecord(DataSet: TDataSet); private Plans:String; function GetVoucherOrdNo: Integer; function GetVoucherNo(const OrdNo : Integer; const sts : TSQLTimeStamp; const VKNo : String): String; function GetLineNo(OrdNo : Integer): Integer; function LoadAccounts(Reload : Boolean) : Boolean; // function LoadCorp(Reload : Boolean) : Boolean; procedure UpdateVoucherAmt; procedure AccIDFieldChange(Sender : TField); procedure FCYDRFieldChange(Sender : TField); procedure FCYCRFieldChange(Sender : TField); // procedure InitSQL(iKind : SmallInt); // procedure SetFieldsProviderFlags(DataSet : TDataSet; FieldNames : String; pfFlag : TProviderFlag); { Private declarations } public { Public declarations } end; var dmAcc:TdmAcc; VoucherDate:TDatetime; // function GetCorpID(helpmem:String):Integer; implementation uses u_main, my_sys_function, u_data_share; {$R *.dfm} function TdmAcc.GetVoucherOrdNo: Integer; begin with ADOQueryTemp do try Close;SQL.Clear; SQL.Add('Select Max(OrdNo) OrdNo from Vouchers'); Open; if not FieldByName('OrdNo').IsNull then Result:=FieldByName('OrdNo').AsInteger+1 else Result:=1; finally Close; end; end; function TdmAcc.GetVoucherNo(const OrdNo : Integer; const sts : TSQLTimeStamp; const VKNo : String): String; var iResult : Integer; function sResult : String; begin Result:=IntToStr(iResult); while Length(Result)<6 do Result:='0'+Result; end; begin with ADOQueryTemp do try Close;SQL.Clear; SQL.Add('Select VouNo from Vouchers'); SQL.Add('where Year(VouDate)=:AccYear and Month(VouDate)=:AccMonth'); SQL.Add('and VKNo=:VKNo and OrdNo<>:OrdNo'); Parameters.ParamByName('AccYear').Value:=sts.Year; Parameters.ParamByName('AccMonth').value:=sts.Month; Parameters.ParamByName('VKNo').value:=VKNo; Parameters.ParamByName('OrdNo').value:=OrdNo; SQL.Add('Order By VouNo'); Open; iResult:=1; with ADOQueryTemp do if not (Bof and Eof) then while Locate('VouNo', sResult, []) do Inc(iResult); Result:=sResult; finally Close; end; end; function TdmAcc.GetLineNo(OrdNo : Integer): Integer; begin with ADOQueryTemp do try Close;SQL.Clear; SQL.Add('Select Max(ItemNo) Itemno from VouItems'); SQL.Add(Format('where OrdNo=%d', [OrdNo])); Open; if not FieldByName('Itemno').IsNull then Result:=FieldByName('Itemno').AsInteger+1 else Result:=1; finally Close; end; end; function TdmAcc.LoadAccounts(Reload : Boolean) : Boolean; var CanLoad : Boolean; begin Result:=true; CanLoad:=Reload or (not memTblAccItems.Active); if not CanLoad then exit; with ADOQueryTemp do try Close;SQL.Clear; SQL.Add('Select * from ACCITEMS'); SQL.Add('Order By AccID'); Open; memTblAccitems.LoadFromDataSet(ADOQueryTemp, [mtcpoStructure,mtcpoProperties,mtcpoFieldIndex]); finally close; end; end; { function TdmAcc.LoadCorp(Reload : Boolean) : Boolean; var CanLoad : Boolean; aQuery:TQuery; begin Result:=true; CanLoad:=Reload or (not memTblCorp.Active); if not CanLoad then exit; aQuery:=CreateQuery; with aQuery do try Close;SQL.Clear; SQL.Add('Select * from Customer'); SQL.Add('Order By CustID'); Open; memTblCorp.LoadFromDataSet(aQuery, [mtcpoStructure,mtcpoProperties,mtcpoFieldIndex]); finally FreeAndNil(aQuery); end; end; } procedure TdmAcc.cdsVouNewRecord(DataSet: TDataSet); begin { VoucherDate.Hour:=0; VoucherDate.Minute:=0; VoucherDate.Second:=0; VoucherDate.Fractions:=0; } with DataSet do begin FieldByName('OrdNo').AsInteger:=GetVoucherOrdNo; FieldByName('VKNo').AsString:=inttostr(aVKNO); FieldByName('VouDate').AsDateTime:=VoucherDate; FieldByName('AccYear').AsInteger:=Yearof(Voucherdate); FieldByName('AccMonth').AsInteger:=monthof(Voucherdate); FieldByName('VouNo').AsString:=GetVoucherNo(FieldByName('OrdNo').AsInteger, DateTimeToSQLTimeStamp(FieldByName('VouDate').AsDateTime), FieldByName('VKNo').AsString); FieldByName('Prepared').AsString:=employee; FieldByName('ItemsCount').AsInteger:=0; end; end; procedure TdmAcc.cdsVouItemsNewRecord(DataSet: TDataSet); begin with DataSet do begin FieldByName('OrdNo').AsInteger:=AdotblVou.FieldByName('OrdNo').AsInteger; FieldByName('ItemNo').AsInteger:=GetLineNo(FieldByName('OrdNo').AsInteger); FieldByName('QTY').AsString:='1'; FieldByName('CF').AsInteger:=0; FieldByName('EXPLAN').AsString:=''; FieldByName('PRICEDR').AsFloat:=0.0; FieldByName('PRICECR').AsFloat:=0.0; FieldByName('QTYDR').AsFloat:=0.0; FieldByName('QTYCR').AsFloat:=0.0; FieldByName('VERIFYED').AsString:='F'; FieldByName('VERIFYLASTBLC').AsFloat:=0.0; FieldByName('FIELD1').AsString:=''; FieldByName('FIELD2').AsString:=''; FieldByName('FIELD3').AsString:=''; FieldByName('FIELD4').AsString:=''; FieldByName('FIELD5').AsString:=''; FieldByName('FIELD6').AsString:=''; FieldByName('FIELD7').AsString:=''; end; end; procedure TdmAcc.DataModuleCreate(Sender: TObject); begin { with SQLConnectionAcc do begin Params.LoadFromFile(ExtractFilePath(ParamStr(0))+'Account.ini'); try Connected:=true; except exit; end; end; } LoadAccounts(true); // LoadCorp(True); // QryDept.Open; // QrySaleID.Open; // tblSTLMODE.Open; // InitSQL(1); // cdsVou.Open; // cdsVouItems.Open; // CreatememTblVou; end; procedure TdmAcc.UpdateVoucherAmt; var AmtDR, AmtCR,D,C : Double; OrdNo,Counts : Integer; begin OrdNo:=AdotblVouItems.FieldByName('OrdNo').AsInteger; D:=AdotblVou.FieldByName('AmtDR').AsFloat; C:=AdotblVou.FieldByName('AmtCR').AsFloat; with ADOQueryTemp do try Close;SQL.Clear; SQL.Add('Select Sum(AmtDR) AmtDR, Sum(AmtCR) AmtCR'); SQL.Add('from VouItems'); SQL.Add(Format('where OrdNo=%d', [OrdNo])); Open; AmtDR:=StrToFloatDef(FormatFloat('0.00;-0.00;0.0',FieldByName('AmtDR').AsFloat), 0); AmtCR:=StrToFloatDef(FormatFloat('0.00;-0.00;0.0',FieldByName('AmtCR').AsFloat), 0); Close;SQL.Clear; SQL.Add('Select Count(ItemNo) itemcount from VouItems'); SQL.Add(Format('where OrdNo=%d', [OrdNo])); Open; counts:=fieldByName('itemcount').AsInteger; if (D<>AmtDR)or(C<>AmtCR) then begin Close;SQL.Clear; SQL.Add('Update VOUCHERS set AMTDR=:AmtDR,AmtCR=:AmtCR,ITEMSCOUNT=:counts'); SQL.Add(Format('where OrdNo=%d', [OrdNo])); Parameters.ParamByName('AmtDR').value:=AmtDR; Parameters.ParamByName('AmtCR').value:=AmtCR; Parameters.ParamByName('counts').value:=counts; ExecSQL; end; finally Close; end; end; procedure TdmAcc.cdsVouItemsAfterPost(DataSet: TDataSet); begin UpdateVoucherAmt; end; procedure TdmAcc.cdsVouItemsAfterDelete(DataSet: TDataSet); begin UpdateVoucherAmt; end; procedure TdmAcc.cdsVouItemsAfterOpen(DataSet: TDataSet); begin DataSet.FieldByName('AccID').OnChange:=AccIDFieldChange; DataSet.FieldByName('FCYDR').OnChange:=FCYDRFieldChange; DataSet.FieldByName('FCYCR').OnChange:=FCYCRFieldChange; end; procedure TdmAcc.AccIDFieldChange(Sender : TField); begin memTblAccitems.First; while not memTblAccItems.Eof do begin if Trim(memTblAccItems.FieldByName('AccID').AsString)=Sender.AsString then begin Sender.DataSet.Edit; Sender.DataSet.FieldByName('AccName').AsString:=memTblAccItems.fieldByName('AccName').AsString; Sender.DataSet.FieldByName('DC').asString:=memTblAccItems.fieldByName('DC').AsString; Sender.DataSet.FieldByName('FCY').asString:=memTblAccItems.fieldByName('FCY').AsString; Sender.DataSet.FieldByName('DEPTACC').asString:=memTblAccItems.fieldByName('DEPTACC').AsString; Sender.DataSet.FieldByName('EMPLACC').asString:=memTblAccItems.fieldByName('EMPLACC').AsString; Sender.DataSet.FieldByName('CORPACC').asString:=memTblAccItems.fieldByName('CORPACC').AsString; Sender.DataSet.FieldByName('ITEMACC').asString:=memTblAccItems.fieldByName('ITEMACC').AsString; if (Sender.AsString<>AccInfo.DebitAccID) and (Sender.AsString<>AccInfo.CreditAccID) then Sender.DataSet.FieldByName('EXPLAN').AsString:=Plans; exit; end; memTblAccItems.Next; end; end; procedure TdmAcc.tblVouItemsBeforeClose(DataSet: TDataSet); begin UpdateVoucherAmt; end; procedure TdmAcc.FCYCRFieldChange(Sender: TField); begin if Sender.AsFloat=0 then exit; //Sender.DataSet.FieldByName('FCYEXRATE').AsFloat:=8.32; Sender.DataSet.FieldByName('FCY').AsString:='T'; Sender.DataSet.FieldByName('AMTCR').AsFloat:=StrToFloatDef(FormatFloat('0.00;-0.00;0.0', Sender.AsFloat*Sender.DataSet.FieldByName('FCYEXRATE').AsFloat), 0); end; procedure TdmAcc.FCYDRFieldChange(Sender: TField); begin if Sender.AsFloat=0 then exit; // Sender.DataSet.FieldByName('FCYEXRATE').AsFloat:=8.32; Sender.DataSet.FieldByName('FCY').AsString:='T'; Sender.DataSet.FieldByName('AMTDR').AsFloat:=StrToFloatDef(FormatFloat('0.00;-0.00;0.0', Sender.AsFloat*Sender.DataSet.FieldByName('FCYEXRATE').AsFloat), 0); end; procedure TdmAcc.tblVouItemsBeforePost(DataSet: TDataSet); begin if (DataSet.FieldByName('AccID').AsString=AccInfo.DebitAccID) or (DataSet.FieldByName('AccID').AsString=AccInfo.CreditAccID) then Plans:=DataSet.fieldByName('EXPLAN').AsString; if DataSet.FieldByName('CorpBf').AsString<>'F' then DataSet.FieldByName('CorpBf').AsString:='T'; end; procedure TdmAcc.tblVouItemsCalcFields(DataSet: TDataSet); { function DrCorpDisplayName(cCusCode:String ) : String; var CorpNo,Corp:String; begin with ADOQuery1 do begin Close;SQL.Clear; SQL.Add('Select cCusCode, cCusName,cCusAbbName from Customer'); SQL.Add(Format('where cCusCode=%s', [cCusCode])); Open; CorpNo:=Trim(FieldByName('cCusCode').AsString); Corp:=Trim(FieldByName('cCusAbbName').AsString); if Corp='' then Corp:=Trim(FieldByName('cCusAbbName').AsString); Result:=Trim(Format('%s %s', [CorpNo, Corp])); end; end; function CrCorpDisplayName(cCusCode:String ) : String; var CorpNo,Corp:String; begin with ADOQuery1 do begin Close;SQL.Clear; SQL.Add('Select cVenCode, cVenName,cVenAbbName from Vendor'); SQL.Add(Format('where cVenCode=%s', [cCusCode])); Open; CorpNo:=Trim(FieldByName('cVenCode').AsString); Corp:=Trim(FieldByName('CName').AsString); if Corp='' then Corp:=Trim(FieldByName('SName').AsString); Result:=Trim(Format('%s %s', [CorpNo, Corp])); end; end; } { function CorpDisplayName(cCusCode:String ) : String; var CorpNo,Corp:String; aQuery:TQuery; begin aQuery:=CreateQuery; try with aQuery do begin Close;SQL.Clear; SQL.Add('Select sname from Customer where Custid=:CustID'); ParamByName('CustID').AsString:=cCusCode; Open; Corp:=Trim(FieldByName('sname').AsString); Result:=Trim(Format('%s %s', [CorpNo, Corp])); end; finally FreeAndNil(aQuery); end; end; } begin { if DataSet.FieldByName('AccID').AsString=AccInfo.DebitAccID then DataSet.FieldByName('AccNameCalc').AsString:=DataSet.FieldByName('AccName').AsString+CorpDisplayName(DataSet.FieldByName('CorpID').AsString) else DataSet.FieldByName('AccNameCalc').AsString:=DataSet.FieldByName('AccName').AsString+CorpDisplayName(DataSet.FieldByName('CorpID').AsString); } end; procedure TdmAcc.ADOTblVouNewRecord(DataSet: TDataSet); begin { VoucherDate.Hour:=0; VoucherDate.Minute:=0; VoucherDate.Second:=0; VoucherDate.Fractions:=0; Tdate } with DataSet do begin FieldByName('VouDate').AsDatetime:=dateof(VoucherDate); FieldByName('AccYear').AsInteger:=yearof(VoucherDate); FieldByName('AccMonth').AsInteger:=monthof(VoucherDate); FieldByName('VouNo').AsString:=' '; FieldByName('AMTDR').AsFloat:=0.00; FieldByName('AMTCR').AsFloat:=0.00; // FieldByName('Prepared').AsString:=employee; FieldByName('ItemsCount').AsInteger:=0; FieldByName('Status').AsInteger:=0; end; end; end.