123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- using Comal.Classes;
- using ControlzEx.Standard;
- using CsvHelper;
- using CsvHelper.Configuration.Attributes;
- using FastReport.DevComponents.WinForms.Drawing;
- using InABox.Core;
- using InABox.Core.Postable;
- using InABox.Poster.Timberline;
- using InABox.Scripting;
- using Microsoft.Win32;
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using static System.Windows.Forms.VisualStyles.VisualStyleElement;
- namespace PRS.Shared
- {
- public class BillTimberlineHeader
- {
- [Ignore]
- public List<BillTimberlineDistribution> Distributions { get; set; } = new();
- [Index(1)]
- public string RecordID { get; set; } = "APIF";
- [Index(2)]
- public string Vendor { get; set; }
- [Index(3)]
- public string Invoice { get; set; }
- [Index(4)]
- public string Description { get; set; }
- [Index(5)]
- public double Amount { get; set; }
- [Index(6)]
- public double Tax { get; set; }
- [Index(7)]
- public double DiscountOffered { get; set; }
- [Index(8)]
- public double MiscDeduction { get; set; }
- [Index(9)]
- public DateTime InvoiceDate { get; set; }
- [Index(10)]
- public DateTime DateReceived { get; set; }
- [Index(11)]
- public DateTime DiscountDate { get; set; }
- [Index(12)]
- public DateTime PaymentDate { get; set; }
- [Index(13)]
- public DateTime AccountingDate { get; set; }
- [Index(14)]
- public string InvoiceCode1 { get; set; }
- [Index(15)]
- public string InvoiceCode2 { get; set; }
- [Index(16)]
- public string SmryPayeeName { get; set; }
- [Index(17)]
- public string SmryPayeeAddress1 { get; set; }
- [Index(18)]
- public string SmryPayeeAddress2 { get; set; }
- [Index(19)]
- public string SmryPayeeCity { get; set; }
- [Index(20)]
- public string SmryPayeeState { get; set; }
- [Index(21)]
- public string SmryPayeeZip { get; set; }
- }
- public class BillTimberlineDistribution
- {
- [Index(1)]
- public string RecordID { get; set; } = "APDF";
- [Index(2)]
- public string Commitment { get; set; }
- [Index(3)]
- public int CommitmentLineItem { get; set; }
- [Index(4)]
- public string Equipment { get; set; }
- [Index(5)]
- public string EQCostCode { get; set; }
- [Index(6)]
- public string Job { get; set; }
- [Index(7)]
- public string Extra { get; set; }
- [Index(8)]
- public string CostCode { get; set; }
- [Index(9)]
- public string Category { get; set; }
- [Index(10)]
- public string BLStdItem { get; set; }
- [Index(11)]
- public string Reserved { get; set; }
- [Index(12)]
- public string ExpenseAccount { get; set; }
- [Index(13)]
- public string APAccount { get; set; }
- [Index(14)]
- public double TaxablePayments { get; set; }
- [Index(15)]
- public string TaxGroup { get; set; }
- [Index(16)]
- public double Units { get; set; }
- [Index(17)]
- public double UnitCost { get; set; }
- [Index(18)]
- public double Amount { get; set; }
- [Index(19)]
- public double Tax { get; set; }
- [Index(20)]
- public double TaxLiability { get; set; }
- [Index(21)]
- public double DiscountOffered { get; set; }
- [Index(22)]
- public double Retainage { get; set; }
- [Index(23)]
- public double MiscDeduction { get; set; }
- [Index(24)]
- [BooleanTrueValues("t", "T", "1", "Y", "y")]
- [BooleanFalseValues("f", "F", "0", "N", "n")]
- public bool TaxablePaymentsExempt { get; set; }
- [Index(25)]
- public string DistCode { get; set; }
- [Index(26)]
- public string MiscEntry1 { get; set; }
- [Index(27)]
- public double MiscEntryUnits1 { get; set; }
- [Index(28)]
- public string MiscEntry2 { get; set; }
- [Index(29)]
- public double MiscEntryUnits2 { get; set; }
- [Index(30)]
- public double MeterOdometer { get; set; }
- [Index(31)]
- public string Description { get; set; }
- [Index(32)]
- public string Authorization { get; set; }
- [Index(33)]
- public string JointPayee { get; set; }
- }
- public class BillTimberlineSettings : TimberlinePosterSettings<Bill>
- {
- protected override string DefaultScript()
- {
- return @"
- using PRS.Shared;
- using InABox.Core;
- using System.Collections.Generic;
- public class Module
- {
- public void BeforePost(IDataModel<Bill> model)
- {
- // Perform pre-processing
- }
- public void ProcessHeader(IDataModel<Bill> model, Bill bill, BillTimberlineHeader header)
- {
- // Do extra processing for a header line
- }
- public void ProcessLine(IDataModel<Bill> model, BillLine bill, BillTimberlineDistribution distribution)
- {
- // Do extra processing for a distribution line
- }
- public void AfterPost(IDataModel<Bill> model)
- {
- // Perform post-processing
- }
- }";
- }
- }
- public class BillTimberlinePoster : ITimberlinePoster<Bill, BillTimberlineSettings>
- {
- public ScriptDocument? Script { get; set; }
- public bool BeforePost(IDataModel<Bill> model)
- {
- model.SetIsDefault<Document>(false, alias: "CompanyLogo");
- model.SetIsDefault<CoreTable>(false, alias: "CompanyInformation");
- model.SetIsDefault<Employee>(false);
- model.SetIsDefault<BillLine>(true, "Bill_BillLine");
- model.AddChildTable<BillLine, PurchaseOrderItem>(x => x.OrderItem.ID, x => x.ID, isdefault: true, parentalias: "Bill_BillLine", childalias: "POItem");
- Script?.Execute(methodname: "BeforePost", parameters: new object[] { model });
- return true;
- }
- private void ProcessHeader(IDataModel<Bill> model, Bill bill, BillTimberlineHeader header)
- {
- Script?.Execute(methodname: "ProcessHeader", parameters: new object[] { model, bill, header });
- }
- private void ProcessLine(IDataModel<Bill> model, BillLine bill, BillTimberlineDistribution distribution)
- {
- Script?.Execute(methodname: "ProcessLine", parameters: new object[] { model, bill, distribution });
- }
- private List<BillTimberlineHeader> DoProcess(IDataModel<Bill> model)
- {
- var apifs = new List<BillTimberlineHeader>();
- foreach(var bill in model.GetTable<Bill>().ToObjects<Bill>())
- {
- var apif = new BillTimberlineHeader
- {
- Vendor = bill.SupplierLink.Code,
- Invoice = bill.Number,
- Description = "",
- Amount = bill.IncTax,
- Tax = bill.Tax,
- // DiscountOffered
- // Misc. Deduction
- InvoiceDate = bill.BillDate,
- // DateReceived doesn't exist
- DiscountDate = bill.BillDate,
- PaymentDate = bill.PaymentDate,
- AccountingDate = bill.AccountingDate,
- // InvoiceCode1
- // InvoiceCode2
- // SmryPayeeName
- // SmryPayeeAddress1
- // SmryPayeeAddress2
- // SmryPayeeCity
- // SmryPayeeState
- // SmryPayeeZip
- };
- ProcessHeader(model, bill, apif);
- var purchaseOrderItems = model.GetTable<PurchaseOrderItem>("POItem").ToObjects<PurchaseOrderItem>()
- .ToDictionary(x => x.ID, x => x);
- foreach (var billLine in model.GetTable<BillLine>("Bill_BillLine").ToObjects<BillLine>())
- {
- var apdf = new BillTimberlineDistribution
- {
- // Equipment
- // EQ Cost Code
- // Extra
- // Cost Code
- // Category
- /// BL STd Item
- // Reserved
- // Expense account
- // AP Account
- // Taxable Payments
- TaxGroup = billLine.TaxCode.Code,
- Amount = billLine.IncTax,
- Tax = billLine.Tax,
- // Tax Liability
- // Discount OFfered
- // Retainage
- // MIsc Deduction
- // Tax Payments Exempt
- // Dist Code
- // Misc Entry 1
- // Misc Units 1
- // Misc Entry 2
- // Misc Units 2
- // Meter
- Description = billLine.Description,
- // Authorization
- // Joint Payee
- };
- if(purchaseOrderItems.TryGetValue(billLine.OrderItem.ID, out var poItem))
- {
- apdf.Commitment = poItem.PONumber;
- apdf.Job = poItem.Job.JobNumber;
- // apdf.CommitmentLineItem : Leaving this blank
- apdf.Units = poItem.Qty;
- apdf.UnitCost = poItem.Cost;
- }
- ProcessLine(model, billLine, apdf);
- apif.Distributions.Add(apdf);
- }
- apifs.Add(apif);
- }
- return apifs;
- }
- public bool Process(IDataModel<Bill> model)
- {
- var apifs = DoProcess(model);
- var dlg = new SaveFileDialog()
- {
- Filter = "CSV Files (*.csv)|*.csv"
- };
- if (dlg.ShowDialog() == true)
- {
- using var writer = new StreamWriter(dlg.FileName);
- using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
- foreach(var apif in apifs)
- {
- csv.WriteRecord(apif);
- csv.NextRecord();
- foreach(var apdf in apif.Distributions)
- {
- csv.WriteRecord(apdf);
- csv.NextRecord();
- }
- }
- return true;
- }
- else
- {
- throw new PostCancelledException();
- }
- }
- public void AfterPost(IDataModel<Bill> model)
- {
- Script?.Execute(methodname: "AfterPost", parameters: new object[] { model });
- }
- }
- public class BillTimberlinePosterEngine<T> : TimberlinePosterEngine<Bill, BillTimberlineSettings>
- {
- }
- }
|