InvoiceUtilities.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. using Comal.Classes;
  2. using InABox.Clients;
  3. using InABox.Core;
  4. namespace PRS.Shared
  5. {
  6. public enum InvoiceTimeCalculation
  7. {
  8. Detailed,
  9. Activity,
  10. Collapsed,
  11. }
  12. public enum InvoiceMaterialCalculation
  13. {
  14. Detailed,
  15. Product,
  16. CostCentre,
  17. Collapsed,
  18. }
  19. public static class InvoiceUtilities
  20. {
  21. private class InvoiceLineDetail
  22. {
  23. public Guid ID { get; set; }
  24. public String Description { get; set; }
  25. public TaxCodeLink TaxCode { get; set; }
  26. public double Quantity { get; set; }
  27. public double Charge { get; set; }
  28. public InvoiceLineDetail()
  29. {
  30. TaxCode = new TaxCodeLink();
  31. }
  32. }
  33. public static void GenerateInvoiceLines(Guid invoiceid, InvoiceTimeCalculation timesummary, InvoiceMaterialCalculation partsummary, IProgress<String>? progress )
  34. {
  35. CustomerActivitySummary[] activities = new CustomerActivitySummary[] { };
  36. CustomerProductSummary[] products = new CustomerProductSummary[] { };
  37. Assignment[] assignments = new Assignment[] { };
  38. var movements = Array.Empty<StockMovement>();
  39. progress?.Report("Loading Invoice");
  40. var invoice = new Client<Invoice>().Load(new Filter<Invoice>(x => x.ID).IsEqualTo(invoiceid)).FirstOrDefault();
  41. progress?.Report("Loading Detail Data");
  42. var setup = new Task[]
  43. {
  44. Task.Run(() =>
  45. {
  46. var oldlines = new Client<InvoiceLine>().Query(
  47. new Filter<InvoiceLine>(x => x.InvoiceLink.ID).IsEqualTo(invoice.ID),
  48. Columns.None<InvoiceLine>().Add(x => x.ID)
  49. ).Rows.Select(x => x.ToObject<InvoiceLine>()).ToArray();
  50. new Client<InvoiceLine>().Delete(oldlines, "");
  51. }),
  52. Task.Run(() =>
  53. {
  54. activities =
  55. new Client<CustomerActivitySummary>().Query(
  56. new Filter<CustomerActivitySummary>(x => x.Customer.ID).InList(invoice.CustomerLink.ID, Guid.Empty),
  57. Columns.None<CustomerActivitySummary>().Add(x => x.Customer.ID)
  58. .Add(x => x.Activity.ID)
  59. .Add(x => x.Activity.Code)
  60. .Add(x => x.Activity.Description)
  61. .Add(x => x.Charge.TaxCode.ID)
  62. .Add(x => x.Charge.TaxCode.Rate)
  63. .Add(x => x.Charge.Chargeable)
  64. .Add(x => x.Charge.FixedCharge)
  65. .Add(x => x.Charge.ChargeRate)
  66. .Add(x => x.Charge.ChargePeriod)
  67. .Add(x => x.Charge.MinimumCharge)
  68. ).Rows.Select(r => r.ToObject<CustomerActivitySummary>()).ToArray();
  69. }),
  70. Task.Run(() =>
  71. {
  72. assignments = new Client<Assignment>().Query(
  73. new Filter<Assignment>(x => x.Invoice.ID).IsEqualTo(invoice.ID).And(x => x.Charge.Chargeable).IsEqualTo(true),
  74. null,
  75. new SortOrder<Assignment>(x => x.Date)
  76. ).Rows.Select(x => x.ToObject<Assignment>()).ToArray();
  77. }),
  78. Task.Run(() =>
  79. {
  80. products =
  81. new Client<CustomerProductSummary>().Query(
  82. new Filter<CustomerProductSummary>(x => x.Customer.ID).InList(invoice.CustomerLink.ID, Guid.Empty),
  83. Columns.None<CustomerProductSummary>().Add(x => x.Customer.ID)
  84. .Add(x => x.Product.ID)
  85. .Add(x => x.Product.Code)
  86. .Add(x => x.Product.Name)
  87. .Add(x => x.Product.TaxCode.ID)
  88. .Add(x => x.Product.TaxCode.Rate)
  89. .Add(x => x.Charge.Chargeable)
  90. .Add(x => x.Charge.PriceType)
  91. .Add(x => x.Charge.Price)
  92. .Add(x => x.Charge.Markup)
  93. ).Rows.Select(r => r.ToObject<CustomerProductSummary>()).ToArray();
  94. }),
  95. Task.Run(() =>
  96. {
  97. movements = new Client<StockMovement>().Query(
  98. new Filter<StockMovement>(x => x.Invoice.ID).IsEqualTo(invoice.ID).And(x => x.Charge.Chargeable).IsEqualTo(true),
  99. null
  100. ).ToArray<StockMovement>();
  101. })
  102. };
  103. Task.WaitAll(setup);
  104. List<InvoiceLine> updates = new List<InvoiceLine>();
  105. progress?.Report("Calculating...");
  106. var timelines = new List<InvoiceLineDetail>();
  107. foreach (var assignment in assignments)
  108. {
  109. var id = timesummary switch
  110. {
  111. InvoiceTimeCalculation.Detailed => assignment.ID,
  112. InvoiceTimeCalculation.Activity => assignment.ActivityLink.ID,
  113. _ => Guid.Empty
  114. };
  115. var description = timesummary switch
  116. {
  117. InvoiceTimeCalculation.Detailed => string.Format("{0:dd MMM yy} - {1}", assignment.Date, assignment.Description),
  118. InvoiceTimeCalculation.Activity => assignment.ActivityLink.Description,
  119. _ => "Labour"
  120. };
  121. var quantity = assignment.Charge.OverrideQuantity
  122. ? TimeSpan.FromHours(assignment.Charge.Quantity)
  123. : assignment.Actual.Duration;
  124. var activity =
  125. activities.FirstOrDefault(x => x.Customer.ID.Equals(invoice.CustomerLink.ID) && x.Activity.ID.Equals(assignment.ActivityLink.ID))
  126. ?? activities.FirstOrDefault(x => x.Customer.ID.Equals(Guid.Empty) && x.Activity.ID.Equals(assignment.ActivityLink.ID))
  127. ?? new CustomerActivitySummary();
  128. double charge = 0.0F;
  129. if (assignment.Charge.OverrideCharge)
  130. charge = quantity.TotalHours * assignment.Charge.Charge;
  131. else
  132. {
  133. double fixedcharge = activity.Charge.FixedCharge;
  134. TimeSpan chargeperiod = !activity.Charge.ChargePeriod.Equals(TimeSpan.Zero)
  135. ? activity.Charge.ChargePeriod
  136. : TimeSpan.FromHours(1);
  137. var rounded = quantity.Ceiling(chargeperiod);
  138. double multiplier = TimeSpan.FromHours(1).TotalHours / chargeperiod.TotalHours;
  139. double rate = activity.Charge.ChargeRate * multiplier;
  140. double mincharge = activity.Charge.MinimumCharge;
  141. charge = Math.Max(fixedcharge + (rounded.TotalHours * rate), mincharge);
  142. }
  143. var timeline = timelines.FirstOrDefault(x => x.ID == id);
  144. if (timeline == null)
  145. {
  146. timeline = new InvoiceLineDetail();
  147. timeline.Description = description;
  148. timeline.TaxCode.ID = activity.Charge.TaxCode.ID;
  149. timeline.TaxCode.Synchronise(activity.Charge.TaxCode);
  150. timelines.Add(timeline);
  151. }
  152. timeline.Quantity += quantity.TotalHours;
  153. timeline.Charge += charge;
  154. }
  155. foreach (var line in timelines)
  156. {
  157. var update = new InvoiceLine();
  158. update.InvoiceLink.ID = invoice.ID;
  159. update.Description = line.Description;
  160. update.TaxCode.ID = line.TaxCode.ID;
  161. update.TaxCode.Synchronise(line.TaxCode);
  162. update.Quantity = timesummary != InvoiceTimeCalculation.Collapsed ? line.Quantity : 1;
  163. update.ExTax = line.Charge;
  164. updates.Add(update);
  165. }
  166. var partlines = new List<InvoiceLineDetail>();
  167. foreach (var item in movements)
  168. {
  169. var id = partsummary switch
  170. {
  171. InvoiceMaterialCalculation.Detailed => item.ID,
  172. InvoiceMaterialCalculation.Product => item.Product.ID,
  173. InvoiceMaterialCalculation.CostCentre => item.Product.CostCentre.ID,
  174. _ => Guid.Empty
  175. };
  176. var description = partsummary switch
  177. {
  178. InvoiceMaterialCalculation.Detailed => $"{item.Product.Name}: {item.Style.Code}; {item.Dimensions.UnitSize}",
  179. InvoiceMaterialCalculation.Product => item.Product.Name,
  180. InvoiceMaterialCalculation.CostCentre => item.Product.CostCentre.Description,
  181. _ => "Materials"
  182. };
  183. var quantity = item.Charge.OverrideQuantity
  184. ? item.Charge.Quantity
  185. : item.Qty;
  186. var product =
  187. products.FirstOrDefault(x => x.Customer.ID.Equals(invoice.CustomerLink.ID) && x.Product.ID.Equals(item.Product.ID))
  188. ?? products.FirstOrDefault(x => x.Customer.ID.Equals(Guid.Empty) && x.Product.ID.Equals(item.Product.ID))
  189. ?? new CustomerProductSummary();
  190. double charge = 0.0F;
  191. if (item.Charge.OverrideCharge)
  192. charge = quantity * item.Charge.Charge;
  193. else
  194. {
  195. charge = quantity * product.Charge.PriceType switch
  196. {
  197. ProductPriceType.CostPlus => 0.0F * product.Charge.Markup,
  198. _ => product.Charge.Price
  199. };
  200. }
  201. var partline = partlines.FirstOrDefault(x => x.ID == id);
  202. if (partline == null)
  203. {
  204. partline = new InvoiceLineDetail();
  205. partline.ID = id;
  206. partline.Description = description;
  207. partline.TaxCode.ID = product.Product.TaxCode.ID;
  208. partline.TaxCode.Synchronise(product.Product.TaxCode);
  209. partlines.Add(partline);
  210. }
  211. partline.Quantity += quantity;
  212. partline.Charge += charge;
  213. }
  214. foreach (var line in partlines)
  215. {
  216. var update = new InvoiceLine();
  217. update.InvoiceLink.ID = invoice.ID;
  218. update.Description = line.Description;
  219. update.TaxCode.ID = line.TaxCode.ID;
  220. update.TaxCode.Synchronise(line.TaxCode);
  221. update.Quantity = new[] { InvoiceMaterialCalculation.Detailed, InvoiceMaterialCalculation.Product}.Contains(partsummary) ? line.Quantity : 1.0F;
  222. update.ExTax = line.Charge;
  223. updates.Add(update);
  224. }
  225. progress?.Report("Creating Invoice Lines");
  226. if (updates.Any())
  227. new Client<InvoiceLine>().Save(updates, "Recalculating Invoice from Time and Materials");
  228. }
  229. }
  230. }