pdf-order.nut 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (C) 2013 by Domingo Alvarez Duarte <[email protected]>
  3. *
  4. * Licensed under GPLv3, see http://www.gnu.org/licenses/gpl.html.
  5. */
  6. class PDF_Order extends Sq_Fpdf
  7. {
  8. logoFileName = null;
  9. secondColX = null;
  10. cellHeight = null;
  11. cellWidth = null;
  12. fillColorBG = null;
  13. drawColor = null;
  14. static borderTop = "LRT";
  15. static borderBetween = "LR";
  16. static borderBottom = "LBR";
  17. water_mark = null;
  18. page_title = null;
  19. page_title2 = null;
  20. page_footer = null;
  21. labelNumber = null;
  22. strNumber = null;
  23. labelDate = null;
  24. strDate = null;
  25. lineHeaders = null;
  26. totalHeaders = null;
  27. strLines = null;
  28. strTotals = null;
  29. strEntity = null;
  30. constructor()
  31. {
  32. base.constructor();
  33. AliasNbPages();
  34. SetFont("Arial", "", 12);
  35. SetAuthor("DADBIZ & OurBiz");
  36. //SetCompression(false);
  37. }
  38. function do_init()
  39. {
  40. secondColX = 107;
  41. cellHeight = 7;
  42. cellWidth = 44;
  43. fillColorBG = 0xEE;
  44. drawColor = 0xCC;
  45. page_title = "OURBIZ";
  46. page_title2 = "www.dadbiz.es";
  47. page_footer = "Thanks for choose our products and services !";
  48. labelNumber = _tr("Order Number");
  49. strNumber = "20128456";
  50. labelDate = _tr("Date");
  51. strDate = "10/04/2012";
  52. strTotals = [];
  53. strEntity = [];
  54. strLines = [];
  55. lineHeaders = [];
  56. newColInfo(lineHeaders, _tr("Code"), 16, 'C', false);
  57. newColInfo(lineHeaders, _tr("Description"), 93, 'L', true),
  58. newColInfo(lineHeaders, _tr("Quantity"), 16, 'R', true),
  59. newColInfo(lineHeaders, _tr("Price"), 20, 'R', false),
  60. newColInfo(lineHeaders, _tr("Disc. %"), 10, 'R', true),
  61. newColInfo(lineHeaders, _tr("S.T. I %"), 10, 'R', true),
  62. newColInfo(lineHeaders, _tr("S.T. II %"), 10, 'R', true),
  63. newColInfo(lineHeaders, _tr("Subtotal"), 20, 'R', false);
  64. totalHeaders = [];
  65. newColInfo(totalHeaders, _tr("Page"), 20, 'C', false);
  66. newColInfo(totalHeaders, _tr("Subtotal"), 45, 'C', true);
  67. newColInfo(totalHeaders, _tr("Sales Tax I"), 42, 'C', true);
  68. newColInfo(totalHeaders, _tr("Sales Tax II"), 42, 'C', false);
  69. newColInfo(totalHeaders, _tr("Total"), 46, 'C', true);
  70. }
  71. function newColInfo(vec, str, width, dataAlign, fitScale)
  72. {
  73. vec.push({
  74. str = str,
  75. width = width,
  76. dataAlign = dataAlign,
  77. fitScale = fitScale,
  78. });
  79. }
  80. function getLogo(){
  81. if(!logoFileName || logoFileName.isempty()){
  82. logoFileName = "dadbiz_120.png";
  83. }
  84. return logoFileName;
  85. }
  86. function logoImage(px, py, pwidth){
  87. local logoImg = getLogo();
  88. if(!logoImg.isempty()) Image(logoImg, px, py, pwidth);
  89. }
  90. function orderTable(lineCount)
  91. {
  92. local norder_pages = strLines.size()/lineCount;
  93. if(strLines.len()%lineCount) ++norder_pages;
  94. if(!norder_pages) ++norder_pages;
  95. local tmp_str;
  96. local font_settings = {};
  97. AliasNbPages();
  98. for(int_t _np=0; _np < norder_pages; ++_np)
  99. {
  100. AddPage();
  101. SetFont("Arial", "0", 12);
  102. //if(getLogoImage() != null) Image(getLogoImage(), mLogoFileName, 10, 8, 30, 0, 0, 0);
  103. //Image("logo.png",10,8);
  104. LogoImage(10, 8, 30);
  105. // Header
  106. SetFontSize(12);
  107. SetFillColor(fillColorBG);
  108. SetDrawColor(drawColor);
  109. SetLineWidth(0.3);
  110. SetFont("", "B");
  111. local savedY = GetY();
  112. Cell(32);
  113. Cell(cellWidth*1.65, cellHeight, page_title, "", 0, 'C');
  114. Ln();
  115. Cell(32);
  116. GetFontSettings(font_settings);
  117. SetFont("Arial", "i", 9);
  118. MultiCell(cellWidth*1.65, cellHeight*0.6, page_title2, "", 'C');
  119. SetFontSettings(font_settings);
  120. SetY(savedY);
  121. SetFontSize(10);
  122. Cell(secondColX);
  123. Cell(cellWidth, cellHeight, labelNumber, "1", 0, 'C', true);
  124. Cell(cellWidth, cellHeight, labelDate, "1", 0, 'C', true);
  125. Ln();
  126. // Color and font restoration
  127. SetFillColor(224, 235, 255);
  128. SetFontSize(12);
  129. Cell(secondColX);
  130. Cell(cellWidth, cellHeight, strNumber, "1", 0, 'C');
  131. Cell(cellWidth, cellHeight, strDate, "1", 0, 'C');
  132. Ln();
  133. SetFontSize(10);
  134. SetY(GetY() + 2);
  135. Cell(secondColX);
  136. local savedHeight = cellHeight;
  137. cellHeight = 6;
  138. local withBorder = borderTop;
  139. local x = 0;
  140. for(local i=0, size = strEntity.size(); i< size; ++i)
  141. {
  142. local value = strEntity[i];
  143. CellFitScale(cellWidth * 2, cellHeight, value.tostring(), withBorder,
  144. 0, 'L', false, 0);
  145. if (x++ == size - 2) withBorder = borderBottom;
  146. else withBorder = borderBetween;
  147. Ln();
  148. Cell(secondColX);
  149. }
  150. cellHeight = savedHeight;
  151. SetY(GetY() + 2);
  152. SetFillColor(fillColorBG);
  153. SetDrawColor(drawColor);
  154. SetLineWidth(0.3);
  155. SetFont("", "B");
  156. local savedFontSize = GetFontSize();
  157. for(local i=0, size=lineHeaders.size(); i<size; ++i)
  158. {
  159. local header = lineHeaders[i];
  160. if(header.fitScale) CellFitScale(header.width, cellHeight, header.str, "1", 0,
  161. 'C', true);
  162. else Cell(header.width, cellHeight, header.str, "1", 0, 'C', true);
  163. }
  164. SetFontSize(savedFontSize);
  165. Ln();
  166. // Color and font restoration
  167. SetFillColor(224, 235, 255);
  168. SetTextColor(0);
  169. SetFont();
  170. local fill = false;
  171. withBorder = borderBetween;
  172. local line_count = (lineCount == 0) ? 32 : lineCount;
  173. line_count += line_count * _np;
  174. savedHeight = cellHeight;
  175. cellHeight = 6;
  176. for(local i = _np*lineCount; i < line_count; i++)
  177. {
  178. x = 0;
  179. for (local j=0, size=lineHeaders.size(); j<size; ++j)
  180. {
  181. local header = lineHeaders[j];
  182. if(header.fitScale) //clipping desciption
  183. {
  184. //ClippedCell(header.width, mCellHeight, getLineData(i, x), withBorder,
  185. // Position.RIGHTOF, header.dataAlign, fill);
  186. CellFitScale(header.width, cellHeight, getLineData(i, x), withBorder,
  187. 0, header.dataAlign, fill, 0);
  188. }
  189. else
  190. {
  191. Cell(header.width, cellHeight, getLineData(i, x).tostring(), withBorder,
  192. 0, header.dataAlign, fill);
  193. }
  194. x++;
  195. }
  196. Ln();
  197. fill = !fill;
  198. if (i > line_count - 3) withBorder = borderBottom;
  199. }
  200. cellHeight = savedHeight;
  201. SetY(GetY() + 2);
  202. SetFillColor(fillColorBG);
  203. SetDrawColor(drawColor);
  204. SetLineWidth(0.3);
  205. SetFont("", "B");
  206. for(local i=0, size=totalHeaders.size(); i<size; ++i)
  207. {
  208. local header = totalHeaders[i];
  209. Cell(header.width, cellHeight, header.str, "1", 0, 'C', true);
  210. }
  211. Ln();
  212. // Color and font restoration
  213. SetFillColor(224, 235, 255);
  214. SetTextColor(0);
  215. SetFont("", "B", 12);
  216. x = 0;
  217. for(local i=0, size=totalHeaders.size(); i<size; ++i)
  218. {
  219. local header = totalHeaders[i];
  220. if(x == 0)
  221. {
  222. tmp_str = format("%d/%s", PageNo(), GetAliasNbPages());
  223. Cell(header.width, cellHeight, tmp_str, "1", 0, header.dataAlign);
  224. }
  225. else
  226. {
  227. tmp_str = (_np == (norder_pages-1)) ? getTotalData(x) : "...";
  228. Cell(header.width, cellHeight, tmp_str, "1", 0, header.dataAlign);
  229. }
  230. x++;
  231. }
  232. if(!water_mark.isempty()){
  233. SetAlpha(0.7, "Darken");
  234. //SetAlpha(0.5);
  235. SetFont("Arial","B",50);
  236. SetTextColor(255,192,203);
  237. RotatedText(35,190, water_mark, 45);
  238. SetAlpha(1);
  239. SetTextColor(0);
  240. }
  241. //footer message
  242. Ln(10);
  243. SetFont("", "i", 8);
  244. MultiCell(0, cellHeight*0.6, page_footer, "", 'C');
  245. }
  246. }
  247. function getLineData(row, col)
  248. {
  249. if(strLines.size() && row < strLines.size()) return strLines[row][col];
  250. return "";
  251. }
  252. function getTotalData(col)
  253. {
  254. //<= and col-1 because it start with 1
  255. if(strTotals.size() && col <= strTotals.size()) return strTotals[col-1];
  256. return "";
  257. }
  258. }