/Bin/etc/scripts/OrderConfirmation.cs
C# | 78 lines | 61 code | 1 blank | 16 comment | 3 complexity | 460129ed00e9fc3fbfd83066838c65b2 MD5 | raw file
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Data;
- using System.Data.SqlClient;
- using System.IO;
- using DICK;
- using DICK.Core;
- using Antlr.StringTemplate;
-
- namespace DICK.Scripts
- {
- /*
- * ORDERCONFIRMATION SAMPLE PROJECT
- * Author: Henry Nordstrom
- * Date: February 11, 2011
- *
- * This sample demonstrates the following features of DICK:
- * - Using the StringTemplate as template engine (DICK supports both StringTemplate and NVelocity)
- * - Possibility to do select queries directly to the database
- * - Using the QueryToolsADO library to retrieve datatables based on a query
- * - Using the FileNameBody context variable to specify the body of the filename for the file to be written
- *
- * The template file used together with this script is named "OrderConfirmation.cs"
- *
- */
- public class OrderConfirmation:IDataObject
- {
- public override DICKContext GetContext(string key)
- {
- //Initialize context
- DICKContext ctx=new DICKContext();
- DataTable header=QueryToolsADO.QueryToDataTable("select DocNum, isnull(NumAtCard,'-'), DocDate, DocTotal, '-' from ordr where docentry="+key);
- DataTable rows=QueryToolsADO.QueryToDataTable("select ItemCode, Dscription, Quantity, Price, LineTotal, LineNum from RDR1 where docentry = "+key);
- DataRow hdr=header.Rows[0];
- ctx.SetAttribute("DOCNUM",hdr[0].ToString());
- ctx.SetAttribute("NUMATCARD",hdr[1].ToString());
- ctx.SetAttribute("DOCDATE",((DateTime)hdr[2]).ToString("dd.MM.yyyy"));
- ctx.SetAttribute("DOCTOTAL",((Decimal)hdr[3]).ToString("F2"));
- ctx.SetAttribute("SIGN",hdr[4].ToString());
- int rowcounter=0;
- foreach (DataRow row in rows.Rows)
- {
- SortedDictionary<string, object> record = new SortedDictionary<string, object>();
- record.Add("ItemCode", (row[0].ToString().Trim()).PadRight(16,' '));
- record.Add("ItemName", row[1].ToString());
- record.Add("Quantity", ((Decimal)row[2]).ToString("F2").Trim().PadLeft(21,' '));
- record.Add("Price", ((Decimal)row[3]).ToString("F2").Trim().PadLeft(24,' '));
- record.Add("LineTotal", ((Decimal)row[4]).ToString("F2").Trim().PadLeft(24,' '));
- DataTable textrows=QueryToolsADO.QueryToDataTable("select linetext from rdr10 where docentry="+key+" and aftlinenum="+row[5].ToString());
- if(textrows!=null && textrows.Rows.Count>0)
- {
- List<string> textlines=new List<string>();
- foreach(DataRow textrow in textrows.Rows)
- {
- textlines.Add(textrow[0].ToString());
- record.Add("TextLines",textlines);
- }
- }
- ctx.SetAttribute("RECORDS",record);
- rowcounter++;
- }
- //If there is only one record, we need to add another, empty one. Otherwise StringTemplate doesn't find the rows as specified in the template.
- if(rowcounter<2)
- {
- SortedDictionary<string, object> record = new SortedDictionary<string, object>();
- record.Add("ItemCode", "".PadRight(16,' '));
- record.Add("ItemName", "");
- record.Add("Quantity", "".PadLeft(21,' '));
- record.Add("Price", "".PadLeft(24,' '));
- record.Add("LineTotal", "".PadLeft(24,' '));
- ctx.SetAttribute("RECORDS",record);
- }
- ctx.FileNameBody=key;
- return ctx;
- }
- }
- }