SALESREP.MOD 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. IMPLEMENTATION MODULE SalesReps;
  2. (*
  3. * ModBase
  4. * Release 3.0
  5. * (c) Copyright 1986 - 1991 PMI
  6. * P.O. Box 8402
  7. * Green Bay Wi 53308
  8. * All Rights Reserved
  9. * by Ed Ross
  10. *)
  11. FROM DBFInvoice IMPORT OpenInvoiceDBF,CloseInvoiceDBF,MoveInvoiceFromDBF,
  12. InvoiceRec,InvoiceDBF;
  13. FROM GenLists IMPORT NewList,GenList,ListLength,ListInsert,GetElmt,
  14. DisposeList,GetElmtAdr,ListDelete,ListInsertAdr,SortList;
  15. FROM ListUtils IMPORT TextFileToList,TextListToFile;
  16. FROM StrConv IMPORT RealToStr,CardinalToStr;
  17. FROM StrEdit IMPORT CAPstr,DeleteRightJustified,
  18. CrunchBlanks,Center,Append;
  19. FROM Customer IMPORT GetCustName,GetCustSalesRepCom,GetCustSalesRep;
  20. FROM DBFCustomer IMPORT OpenCustomerDBF,FindCustomerByCin,CustomerRec,
  21. MoveCustomerFromDBF;
  22. FROM DBFInvoice IMPORT OpenInvoiceDBF;
  23. FROM PMIGlobals IMPORT Config,PMIScreens,Today,TodayStr;
  24. FROM DspFiles IMPORT OpenDisplayFile;
  25. FROM DBUtils IMPORT GetDateRange;
  26. FROM DBStuff IMPORT MakeKey;
  27. FROM HandleIO IMPORT OpenFile,FileExists,CloseHandle;
  28. FROM PrinterUtils IMPORT SetupDisplayLine,DisplayLine,RemoveDisplayLine,
  29. AddPrintElemt,PrintDisplayLine,ClearDisplayLine;
  30. FROM PosUtils IMPORT Equal;
  31. FROM DateFunctions IMPORT Date,DateToStr,CompareDates,Comparison;
  32. FROM ModBase3 IMPORT ReadDBRec,Deleted,NumberRecords;
  33. FROM StringIO IMPORT WriteStr,WriteEol,PrintMessage;
  34. FROM SYSTEM IMPORT ADDRESS,ADR;
  35. FROM NumTypes IMPORT Real8;
  36. FROM ScrnUtl2 IMPORT CloseDisplayFrame,GetEdField;
  37. FROM VWindows IMPORT ClearPart, CurrentWindow, SetCursorHeight;
  38. FROM ScrnTypes IMPORT DisplayFrame,InitDisplayFrame,AFrameName,DisplayFile;
  39. FROM FramePainter IMPORT ShowDisplayFrame,RedrawField;
  40. FROM ControlUtils IMPORT Control,AddEditorField;
  41. FROM VEditor IMPORT AnEdControlRec,InitEdRec;
  42. FROM LowLevel IMPORT Fill;
  43. FROM M2Strings IMPORT CompareStr,Length,Assign;
  44. VAR
  45. MainList : GenList;
  46. SalesRepList : GenList;
  47. PrtHand : CARDINAL;
  48. Environ : ARRAY[0..60] OF CHAR;
  49. PROCEDURE SortBySalesRep(Inv1 : ADDRESS; Size1 : CARDINAL;
  50. Inv2 : ADDRESS; Size2 : CARDINAL): INTEGER;
  51. (* sort list of invoices by sales rep, by customer, by invdate*)
  52. VAR
  53. I1,I2 : POINTER TO InvoiceRec;
  54. Comp : Comparison;
  55. BEGIN
  56. I1 := Inv1;
  57. I2 := Inv2;
  58. RETURN CompareStr(I1^.SALESMAN,I2^.SALESMAN);
  59. END SortBySalesRep;
  60. PROCEDURE SortByInvNbr(Inv1 : ADDRESS; Size1 : CARDINAL;
  61. Inv2 : ADDRESS; Size2 : CARDINAL): INTEGER;
  62. (* sort list of invoices by sales rep, by customer, by invdate*)
  63. VAR
  64. I1,I2 : POINTER TO InvoiceRec;
  65. Comp : Comparison;
  66. BEGIN
  67. I1 := Inv1;
  68. I2 := Inv2;
  69. RETURN CompareStr(I1^.INVOICE,I2^.INVOICE);
  70. END SortByInvNbr;
  71. PROCEDURE SortByCustNbr(Inv1 : ADDRESS; Size1 : CARDINAL;
  72. Inv2 : ADDRESS; Size2 : CARDINAL): INTEGER;
  73. (* sort list of invoices by sales rep, by customer, by invdate*)
  74. VAR
  75. I1,I2 : POINTER TO InvoiceRec;
  76. Comp : Comparison;
  77. BEGIN
  78. I1 := Inv1;
  79. I2 := Inv2;
  80. RETURN CompareStr(I1^.CUSTID,I2^.CUSTID);
  81. END SortByCustNbr;
  82. PROCEDURE GetMainList();
  83. (* read through the invoices and get those that are closed and within the
  84. specified date range*)
  85. VAR
  86. BeginDate,EndDate : Date;
  87. Invoice : InvoiceRec;
  88. L,NumOfRecs : LONGINT;
  89. BEGIN
  90. BeginDate := Today;
  91. BeginDate.day := 1;
  92. EndDate := Today;
  93. GetDateRange(BeginDate,EndDate,PMIScreens);
  94. OpenInvoiceDBF(FALSE);
  95. NewList(MainList);
  96. NumOfRecs := NumberRecords(InvoiceDBF);
  97. FOR L := 1 TO NumOfRecs DO
  98. ReadDBRec(InvoiceDBF,L);
  99. IF NOT Deleted(InvoiceDBF)
  100. THEN
  101. MoveInvoiceFromDBF(Invoice);
  102. CrunchBlanks(Invoice.SALESMAN);
  103. IF Length(Invoice.SALESMAN) > 1 (* only grab the ones with salesrep*)
  104. THEN
  105. IF (CompareDates(Invoice.INVDATE,BeginDate) = DateGreater) AND
  106. (CompareDates(Invoice.INVDATE,EndDate) = DateLess)
  107. THEN
  108. ListInsert(Invoice,2,MainList,1);
  109. END;
  110. END;
  111. END; (* if deleted *)
  112. END; (* end of loop *)
  113. SortList(MainList,SortBySalesRep);
  114. END GetMainList;
  115. PROCEDURE MakeSubList(SalesRep: ARRAY OF CHAR);
  116. (* Return a list of invoices for this sales rep -
  117. make the list from main list *)
  118. VAR
  119. N : CARDINAL;
  120. Inv : POINTER TO InvoiceRec;
  121. Size,Code : CARDINAL;
  122. SalesMan : ARRAY[0..10] OF CHAR;
  123. Cust : CustomerRec;
  124. BEGIN
  125. NewList(SalesRepList);
  126. MakeKey(SalesRep);
  127. FOR N := 1 TO ListLength(MainList) DO
  128. GetElmtAdr(MainList,N,Inv,Size,Code);
  129. MakeKey(Inv^.SALESMAN);
  130. MakeKey(Inv^.INVOICE);
  131. IF Equal(SalesRep,Inv^.SALESMAN)
  132. THEN
  133. ListInsert(Inv^,2,SalesRepList,1); (* insert in sublist*)
  134. END;
  135. END; (* end for *)
  136. SortList(SalesRepList,SortByInvNbr);
  137. END MakeSubList;
  138. PROCEDURE PrintHeader(SalesRep : ARRAY OF CHAR; Page : CARDINAL);
  139. VAR
  140. Line : ARRAY[0..80] OF CHAR;
  141. Str: ARRAY[0..10] OF CHAR;
  142. BEGIN
  143. WriteStr(PrtHand,12C);
  144. Line := 'Sales Commission Report';
  145. Center(Line,80);
  146. WriteEol(PrtHand,Line);
  147. Assign( SalesRep,Line );
  148. Center(Line,80);
  149. WriteEol(PrtHand,Line);
  150. Assign( TodayStr,Line);
  151. Append(Line,' Page ');
  152. CardinalToStr(Page,4,Str);
  153. Append(Line,Str);
  154. Center(Line,80);
  155. WriteEol(PrtHand,Line);
  156. WriteEol(PrtHand,'');
  157. WriteEol(PrtHand,'');
  158. WriteEol(PrtHand,'');
  159. WriteEol(PrtHand,
  160. ' Cust Invoice Date Net Com % Commission');
  161. WriteEol(PrtHand,
  162. '----------------------------------------------------------------');
  163. END PrintHeader;
  164. PROCEDURE ReportSalesRep(SalesRep: ARRAY OF CHAR);
  165. VAR
  166. CurrentCustNbr : ARRAY[0..15] OF CHAR;
  167. CurrentCustName ,
  168. LastCust : ARRAY[0..60] OF CHAR;
  169. Inv : POINTER TO InvoiceRec;
  170. J, Size,Code : CARDINAL;
  171. CommPerCent : Real8;
  172. CustomerTotal : Real8;
  173. RepTotal : Real8;
  174. LineNbr : CARDINAL;
  175. Line : DisplayLine;
  176. Page : CARDINAL;
  177. B : BOOLEAN;
  178. Commision : Real8;
  179. Str : ARRAY[0..10] OF CHAR;
  180. BEGIN
  181. (* * * * * * * print output should look like this * * * * *** * * *
  182. 123456789012345678901234567890123456789012345678901234567890123456789012345
  183. 1 2 3 4 5 6 7
  184. Customer Name Number Commission Rate xx.xx%
  185. Cust Nbr Invoice Date Net Amount Comm % Commission
  186. xxxxxxxxx xxxxxxxxxx xx-xx-xx xxxxxxx.xx xx.xx xxxxxx.xx
  187. *)
  188. MakeSubList(SalesRep); (* a list of all invoices sorted by client id*)
  189. IF ListLength(SalesRepList) = 0
  190. THEN DisposeList(SalesRepList);
  191. RETURN;
  192. END;
  193. GetElmtAdr(SalesRepList,1,Inv,Size,Code);
  194. RepTotal := 0.0;
  195. Page := 1;
  196. LineNbr := 0;
  197. PrintHeader(SalesRep,Page);
  198. CustomerTotal := 0.0;
  199. RepTotal := 0.0;
  200. SetupDisplayLine(Line,90);
  201. FOR J := 1 TO ListLength(SalesRepList) DO
  202. INC(LineNbr);
  203. IF LineNbr > 50
  204. THEN
  205. LineNbr := 1;
  206. INC(Page);
  207. PrintHeader(SalesRep,Page);
  208. END;
  209. GetElmtAdr(SalesRepList,J,Inv,Size,Code);
  210. AddPrintElemt(Line,Inv^.CUSTID,'S',2,8,'L');
  211. AddPrintElemt(Line,Inv^.INVOICE,'S',10,8,'L');
  212. AddPrintElemt(Line,Inv^.INVDATE,'D',20,8,'L');
  213. AddPrintElemt(Line,Inv^.INVNET,'R',30,10,'L');
  214. GetCustName(Inv^.CUSTID,CurrentCustName);
  215. GetCustSalesRepCom(CommPerCent);
  216. CommPerCent := CommPerCent / 100.0;
  217. AddPrintElemt(Line,CommPerCent,'%',45,5,'L');
  218. AddPrintElemt(Line,CommPerCent * Inv^.INVNET,'R',60,10,'L');
  219. PrintDisplayLine(Line,PrtHand);
  220. ClearDisplayLine(Line);
  221. RepTotal := RepTotal + (CommPerCent * Inv^.INVNET);
  222. END; (* end of sales rep invoice list *)
  223. WriteEol(PrtHand,'');
  224. WriteEol(PrtHand,'');
  225. WriteStr(PrtHand,'Total commission for Sales Rep ');
  226. WriteStr(PrtHand,SalesRep);
  227. WriteStr(PrtHand,': $');
  228. RealToStr(RepTotal,2,10,Str);
  229. WriteEol(PrtHand, Str);
  230. (* write a list of the customers for the sales rep *)
  231. SortList(SalesRepList,SortByCustNbr);
  232. WriteStr(PrtHand,12C);
  233. WriteStr(PrtHand,' Customers for ');
  234. WriteStr(PrtHand,SalesRep);
  235. WriteEol(PrtHand,'');
  236. LineNbr := 1;
  237. LastCust := '';
  238. FOR J := 1 TO ListLength(SalesRepList) DO
  239. GetElmtAdr(SalesRepList,J,Inv,Size,Code);
  240. GetCustName(Inv^.CUSTID,CurrentCustName);
  241. IF NOT Equal(LastCust,CurrentCustName)
  242. THEN
  243. WriteStr(PrtHand,Inv^.CUSTID);
  244. WriteStr(PrtHand,' ');
  245. WriteEol(PrtHand,CurrentCustName);
  246. LastCust := CurrentCustName;
  247. END;
  248. END;
  249. RemoveDisplayLine(Line);
  250. END ReportSalesRep;
  251. PROCEDURE ParseName(VAR Name : ARRAY OF CHAR; Str: ARRAY OF CHAR);
  252. VAR
  253. J : CARDINAL;
  254. BEGIN
  255. Fill(ADR(Name),SIZE(Name),0);
  256. J := 0;
  257. LOOP
  258. IF J > Length(Str)
  259. THEN EXIT;
  260. END;
  261. IF J > SIZE(Name)
  262. THEN EXIT;
  263. END;
  264. IF Str[J] = ' '
  265. THEN EXIT;
  266. END;
  267. Name[J] := Str[J];
  268. INC(J);
  269. END; (* end loop *)
  270. CAPstr(Name);
  271. END ParseName;
  272. PROCEDURE GetSalesReps();
  273. (* read a list of the sales reps into the linked sales rep list *)
  274. VAR J : CARDINAL;
  275. Code : CARDINAL;
  276. L : LONGINT;
  277. TheList : GenList;
  278. Str : ARRAY[0..80] OF CHAR;
  279. SalesRep : ARRAY[0..15] OF CHAR;
  280. EM : CARDINAL;
  281. BEGIN
  282. EM := OpenFile(PrtHand,Config.ReportPrt);
  283. SalesRep := 'SalesRep.txt'; (* temp till file open *)
  284. NewList(TheList);
  285. EM := TextFileToList(SalesRep,TheList);
  286. GetMainList();
  287. FOR J := 1 TO ListLength(TheList) DO
  288. GetElmt(TheList,J,Str,Code); (* get the string*)
  289. ParseName(SalesRep,Str);
  290. ReportSalesRep(SalesRep);
  291. END;
  292. DisposeList(TheList);
  293. EM := CloseHandle(PrtHand);
  294. END GetSalesReps;
  295. PROCEDURE CreateEditSalesReps();
  296. (* sales reps are stored in a simple text file, one sales rep per line
  297. Any information after the reps invoice name is kept but ignored
  298. in the processing *)
  299. VAR
  300. TheList : GenList;
  301. DF : DisplayFrame;
  302. FileHandle : CARDINAL;
  303. EditorRec : AnEdControlRec;
  304. Str : ARRAY[0..15] OF CHAR;
  305. EM : CARDINAL;
  306. BEGIN
  307. NewList(TheList);
  308. Str := 'SalesRep.Txt';
  309. IF FileExists(Str)
  310. THEN
  311. EM := TextFileToList(Str,TheList);
  312. END;
  313. InitEdRec(EditorRec);
  314. InitDisplayFrame(DF,CurrentWindow);
  315. AddEditorField(DF,TheList,EditorRec);
  316. Control(DF);
  317. GetEdField(DF,'',TheList);
  318. EM := TextListToFile(TheList,Str);
  319. END CreateEditSalesReps;
  320. PROCEDURE ProduceCommissions();
  321. BEGIN
  322. OpenCustomerDBF(TRUE);
  323. GetSalesReps();
  324. END ProduceCommissions;
  325. END SalesReps.
  326.