SupplierBillPanel.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using Comal.Classes;
  8. using InABox.Clients;
  9. using InABox.Configuration;
  10. using InABox.Core;
  11. using InABox.DynamicGrid;
  12. using InABox.WPF;
  13. using InABox.Wpf;
  14. using System.Threading.Tasks;
  15. using System.IO;
  16. using System.Collections.ObjectModel;
  17. using System.Windows.Media;
  18. using System.Runtime.CompilerServices;
  19. namespace PRSDesktop;
  20. public class SupplierBillPanelSettings : IUserConfigurationSettings
  21. {
  22. public SupplierBillPanelSettings()
  23. {
  24. AnchorWidth = 500F;
  25. ViewType = ScreenViewType.Combined;
  26. }
  27. public ScreenViewType ViewType { get; set; }
  28. public double AnchorWidth { get; set; }
  29. }
  30. public class SupplierBillPanelProperties : BaseObject, IGlobalConfigurationSettings
  31. {
  32. [IntegerEditor]
  33. [EditorSequence(1)]
  34. public int CurrencyDecimalPlaces { get; set; } = 2;
  35. [EditorSequence(2)]
  36. [CheckBoxEditor]
  37. public bool AllowBlankBillNumbers { get; set; }
  38. }
  39. public partial class SupplierBillPanel : UserControl, IPanel<Bill>, IPropertiesPanel<SupplierBillPanelProperties, CanConfigureAccountsPanels>, INotifyPropertyChanged
  40. {
  41. private SupplierBillPanelSettings settings;
  42. private SupplierBillEditLayout EditLayout;
  43. public SupplierBillPanel()
  44. {
  45. InitializeComponent();
  46. }
  47. #region IPanel Interface
  48. public bool IsReady { get; set; }
  49. public event DataModelUpdateEvent? OnUpdateDataModel;
  50. public Dictionary<string, object[]> Selected()
  51. {
  52. return new Dictionary<string, object[]> { { typeof(Bill).EntityName(), Bills.SelectedRows } };
  53. }
  54. public void CreateToolbarButtons(IPanelHost host)
  55. {
  56. AccountsSetupActions.Standard(host);
  57. PostUtils.CreateToolbarButtons(host,
  58. () => (DataModel(Selection.Selected) as IDataModel<Bill>)!,
  59. () => Bills.Refresh(false, true),
  60. true);
  61. host.CreateSetupSeparator();
  62. host.CreateSetupActionIfCanView<BillApprovalSet>("Bill Approval Sets", PRSDesktop.Resources.bill, (action) =>
  63. {
  64. var list = new MasterList(typeof(BillApprovalSet));
  65. list.ShowDialog();
  66. });
  67. }
  68. public string SectionName => "Supplier Bills";
  69. public SupplierBillPanelProperties Properties { get; set; }
  70. public DataModel DataModel(Selection selection)
  71. {
  72. var ids = Bills.ExtractValues(x => x.ID, selection).ToArray();
  73. return new BaseDataModel<Bill>(new Filter<Bill>(x => x.ID).InList(ids));
  74. }
  75. public void Refresh()
  76. {
  77. if (CheckSaved())
  78. {
  79. Bills.Refresh(false, true);
  80. SetChanged(false);
  81. }
  82. }
  83. public void Setup()
  84. {
  85. settings = new UserConfiguration<SupplierBillPanelSettings>().Load();
  86. SplitPanel.View = settings.ViewType == ScreenViewType.Register ? DynamicSplitPanelView.Master :
  87. settings.ViewType == ScreenViewType.Details ? DynamicSplitPanelView.Detail : DynamicSplitPanelView.Combined;
  88. SplitPanel.AnchorWidth = settings.AnchorWidth;
  89. EditLayout = new();
  90. EditLayout.Approve += EditLayout_Approve;
  91. Bill.SetLayout(EditLayout);
  92. Bills.Refresh(true, false);
  93. BillPanelDocumentCache.Cache.ClearOld();
  94. }
  95. #region Approval
  96. private BillApproval? _approval = null;
  97. private SupplierBillLineGrid? _billLinePage = null;
  98. private void EditLayout_Approve()
  99. {
  100. if (_approval is null) return;
  101. if(_approval.Approved == DateTime.MinValue)
  102. {
  103. _approval.Approved = DateTime.Now;
  104. EditLayout.IsApproved = true;
  105. }
  106. else
  107. {
  108. _approval.Approved = DateTime.MinValue;
  109. EditLayout.IsApproved = false;
  110. }
  111. Bill.DoChanged();
  112. }
  113. private void SaveApproval()
  114. {
  115. if (_approval is null) return;
  116. Client.Save(_approval, "Approval updated by user.");
  117. }
  118. private void CancelApproval()
  119. {
  120. UpdateApproval();
  121. }
  122. private void UpdateApproval()
  123. {
  124. if(_bills is not null && _bills.Length == 1)
  125. {
  126. _approval = Client.Query<BillApproval>(
  127. new Filter<BillApproval>(x => x.Employee.ID).IsEqualTo(App.EmployeeID)
  128. .And(x => x.Bill.ID).IsEqualTo(_bills[0].ID),
  129. Columns.Required<BillApproval>().Add(x => x.Approved))
  130. .ToObjects<BillApproval>().FirstOrDefault();
  131. EditLayout.CanApprove = _approval is not null;
  132. EditLayout.IsApproved = _approval is not null ? _approval.Approved != DateTime.MinValue : false;
  133. if(Bill.Pages.TryGetPage(out _billLinePage))
  134. {
  135. _billLinePage.OnChanged -= BillLinePage_OnChanged;
  136. _billLinePage.OnChanged += BillLinePage_OnChanged;
  137. EditLayout.BillAmount = _billLinePage.Items.Sum(x => x.IncTax);
  138. EditLayout.POAmount = _billLinePage.Items.Sum(x =>
  139. {
  140. return x.OrderItem.IncTax + x.Consignment.IncTax;
  141. });
  142. }
  143. }
  144. else
  145. {
  146. _approval = null;
  147. EditLayout.CanApprove = false;
  148. }
  149. }
  150. private void BillLinePage_OnChanged(object? sender, EventArgs e)
  151. {
  152. if (_billLinePage is null) return;
  153. EditLayout.BillAmount = _billLinePage.Items.Sum(x => x.IncTax);
  154. EditLayout.POAmount = _billLinePage.Items.Sum(x =>
  155. {
  156. return x.OrderItem.IncTax + x.Consignment.IncTax;
  157. });
  158. }
  159. #endregion
  160. private void CheckSaved(CancelEventArgs cancel)
  161. {
  162. if (!bChanged)
  163. {
  164. return;
  165. }
  166. var result = MessageBox.Show("You have an unsaved Supplier Bill; do you wish to save these changes?", "Save Changes?", MessageBoxButton.YesNoCancel);
  167. if (result == MessageBoxResult.Yes)
  168. {
  169. Bill.SaveItem(cancel);
  170. if (!cancel.Cancel)
  171. {
  172. MessageBox.Show("Purchase Order saved.");
  173. }
  174. }
  175. else if (result == MessageBoxResult.Cancel)
  176. {
  177. cancel.Cancel = true;
  178. }
  179. }
  180. private bool CheckSaved()
  181. {
  182. var cancel = new CancelEventArgs();
  183. CheckSaved(cancel);
  184. return !cancel.Cancel;
  185. }
  186. public void Shutdown(CancelEventArgs? cancel)
  187. {
  188. if(cancel is not null)
  189. {
  190. CheckSaved(cancel);
  191. }
  192. }
  193. public void Heartbeat(TimeSpan time)
  194. {
  195. }
  196. #endregion
  197. private Bill[]? _bills = null;
  198. private CoreRow[]? _editRows = null;
  199. private void Bills_OnOnSelectItem(object sender, DynamicGridSelectionEventArgs e)
  200. {
  201. if(SplitPanel.View != DynamicSplitPanelView.Master)
  202. {
  203. ReloadBills();
  204. }
  205. }
  206. private void ReloadBills(bool force = false)
  207. {
  208. var newRows = Bills.SelectedRows;
  209. if (newRows.Length != 0)
  210. {
  211. if(force || _editRows is null || !newRows.CompareTo(_editRows))
  212. {
  213. _editRows = Bills.SelectedRows;
  214. _bills = Bills.LoadBills(_editRows);
  215. Bills.InitialiseEditorForm(Bill, _bills, null, true);
  216. Bill.Visibility = Visibility.Visible;
  217. LinkDocumentPage();
  218. }
  219. UpdateApproval();
  220. }
  221. else
  222. {
  223. _bills = null;
  224. _editRows = null;
  225. Bill.Visibility = Visibility.Hidden;
  226. ClearDocumentPage();
  227. UpdateApproval();
  228. }
  229. }
  230. private void Bill_OnOnOK()
  231. {
  232. using (new WaitCursor())
  233. {
  234. // Saving the approval must happen before saving the bill, because otherwise the BillStore's approval stuff is overwritten
  235. // by this SaveApproval function.
  236. SaveApproval();
  237. var cancel = new System.ComponentModel.CancelEventArgs();
  238. Bill.SaveItem(cancel);
  239. if (!cancel.Cancel)
  240. {
  241. if(_editRows is not null && _bills is not null)
  242. {
  243. _bills = Bills.LoadBills(_editRows);
  244. Bills.UpdateRows(_editRows, _bills);
  245. }
  246. ReloadBills();
  247. SetChanged(false);
  248. }
  249. }
  250. }
  251. private void Bill_OnOnCancel()
  252. {
  253. CancelApproval();
  254. ReloadBills();
  255. SetChanged(false);
  256. }
  257. private void SetChanged(bool changed)
  258. {
  259. bChanged = changed;
  260. Bills.IsEnabled = !changed;
  261. Bill.HideButtons = !changed;
  262. if (!changed)
  263. {
  264. bDocumentsChanged = false;
  265. DoPropertyChanged(nameof(CanRotateImage));
  266. }
  267. }
  268. private bool bChanged = false;
  269. private void Bill_OnOnChanged(object? sender, EventArgs e)
  270. {
  271. SetChanged(true);
  272. }
  273. private void SplitPanel_OnChanged(object sender, DynamicSplitPanelSettings e)
  274. {
  275. settings.ViewType = SplitPanel.View == DynamicSplitPanelView.Master ? ScreenViewType.Register :
  276. SplitPanel.View == DynamicSplitPanelView.Detail ? ScreenViewType.Details : ScreenViewType.Combined;
  277. settings.AnchorWidth = SplitPanel.AnchorWidth;
  278. new UserConfiguration<SupplierBillPanelSettings>().Save(settings);
  279. }
  280. #region Documents
  281. private DynamicDocumentGrid<BillDocument, Bill, BillLink>? DocumentPage;
  282. public bool CanRotateImage => !bDocumentsChanged;
  283. private void ClearDocumentPage()
  284. {
  285. ViewList.Documents = [];
  286. }
  287. private void LinkDocumentPage()
  288. {
  289. DocumentPage = Bill.Pages.OfType<DynamicDocumentGrid<BillDocument, Bill, BillLink>>().FirstOrDefault();
  290. if(DocumentPage is not null)
  291. {
  292. DocumentPage.AfterRefresh += (o, e) =>
  293. {
  294. if (bRefreshingDocuments)
  295. {
  296. return;
  297. }
  298. ReloadViewList(true);
  299. };
  300. DocumentPage.OnChanged += DocumentPage_OnChanged;
  301. }
  302. }
  303. private bool bRefreshingDocuments = false;
  304. private bool bDocumentsChanged = false;
  305. private void DocumentPage_OnChanged(object? sender, EventArgs e)
  306. {
  307. bDocumentsChanged = true;
  308. DoPropertyChanged(nameof(CanRotateImage));
  309. if (bRefreshingDocuments)
  310. {
  311. return;
  312. }
  313. ReloadViewList();
  314. }
  315. private void ReloadViewList(bool force = false)
  316. {
  317. if(DocumentPage is null)
  318. {
  319. return;
  320. }
  321. ViewList.UpdateViewList(DocumentPage.LoadItems(DocumentPage.Data.Rows), force);
  322. }
  323. private void ViewList_UpdateDocument(BillDocument document, Document doc)
  324. {
  325. if(DocumentPage is null)
  326. {
  327. return;
  328. }
  329. if (Path.GetExtension(doc.FileName) == ".pdf")
  330. {
  331. document.Thumbnail = ImageUtils.GetPDFThumbnail(doc.Data, 256, 256);
  332. }
  333. Client.Save(document, "");
  334. BillPanelDocumentCache.Cache.Add(new DocumentCachedDocument(doc));
  335. DocumentPage.Refresh(false, true);
  336. ViewList.UpdateViewList(DocumentPage.LoadItems(DocumentPage.Data.Rows), true);
  337. }
  338. private void Documents_DragOver(object sender, DragEventArgs e)
  339. {
  340. if(Bills.SelectedRows.Length == 0)
  341. {
  342. e.Effects = DragDropEffects.None;
  343. }
  344. else
  345. {
  346. if (e.Data.GetDataPresent(DataFormats.FileDrop) || e.Data.GetDataPresent("FileGroupDescriptor"))
  347. {
  348. e.Effects = DragDropEffects.Copy;
  349. }
  350. else
  351. {
  352. e.Effects = DragDropEffects.None;
  353. }
  354. }
  355. e.Handled = true;
  356. }
  357. private void Documents_Drop(object sender, DragEventArgs e)
  358. {
  359. if(Bills.SelectedRows.Length == 0 || DocumentPage is null)
  360. {
  361. return;
  362. }
  363. var selectedBill = Bills.SelectedRows[0].ToObject<Bill>();
  364. Task.Run(() =>
  365. {
  366. Dispatcher.Invoke(() =>
  367. {
  368. Progress.Show("Uploading documents");
  369. try
  370. {
  371. var result = DocumentUtils.HandleFileDrop(e);
  372. if (result is not null)
  373. {
  374. var docs = new List<Document>();
  375. foreach (var (filename, stream) in result)
  376. {
  377. var doc = new Document();
  378. doc.FileName = filename;
  379. if (stream is null)
  380. {
  381. doc.Data = File.ReadAllBytes(filename);
  382. doc.TimeStamp = new FileInfo(filename).LastWriteTime;
  383. }
  384. else
  385. {
  386. using var memStream = new MemoryStream();
  387. stream.CopyTo(memStream);
  388. doc.Data = memStream.ToArray();
  389. doc.TimeStamp = DateTime.Now;
  390. }
  391. doc.CRC = CoreUtils.CalculateCRC(doc.Data);
  392. docs.Add(doc);
  393. }
  394. Client.Save(docs, "Initial Upload");
  395. var billDocs = new List<BillDocument>();
  396. foreach (var doc in docs)
  397. {
  398. var billDoc = new BillDocument();
  399. billDoc.DocumentLink.CopyFrom(doc);
  400. billDoc.EntityLink.CopyFrom(selectedBill);
  401. billDocs.Add(billDoc);
  402. }
  403. DocumentPage.SaveItems(billDocs);
  404. DocumentPage.Refresh(false, true);
  405. try
  406. {
  407. bRefreshingDocuments = true;
  408. DocumentPage.DoChanged();
  409. }
  410. finally
  411. {
  412. bRefreshingDocuments = false;
  413. }
  414. }
  415. Progress.Close();
  416. }
  417. catch (Exception e)
  418. {
  419. Progress.Close();
  420. MessageWindow.ShowError("Could not upload documents.", e);
  421. }
  422. });
  423. });
  424. }
  425. #endregion
  426. public event PropertyChangedEventHandler? PropertyChanged;
  427. protected void DoPropertyChanged([CallerMemberName] string propertyName = "")
  428. {
  429. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  430. }
  431. private Column<Bill> ApprovalSetID = new(x => x.ApprovalSet.ID);
  432. private void Bills_OnChanged(object sender, EventArgs e)
  433. {
  434. ReloadBills(force: true);
  435. }
  436. private void Bills_OnCustomiseEditor(IDynamicEditorForm sender, Bill[] items, DynamicGridColumn column, BaseEditor editor)
  437. {
  438. if(column.ColumnName == "Approved" || ApprovalSetID.IsEqualTo(column.ColumnName))
  439. {
  440. editor.Editable = Editable.Hidden;
  441. }
  442. if((editor is DateEditor || editor is TimestampEditor) && !column.ColumnName.Contains("."))
  443. {
  444. if(editor.Page == "Additional")
  445. {
  446. editor.EditorSequence += 100;
  447. }
  448. editor.Page = "Dates";
  449. }
  450. }
  451. }
  452. public class BillDocumentViewList : DocumentViewList<BillDocument>
  453. {
  454. protected override IEnumerable<Document> LoadDocuments(IEnumerable<Guid> ids)
  455. {
  456. return BillPanelDocumentCache.Cache.LoadDocuments(ids, checkTimestamp: true);
  457. }
  458. protected override Guid GetID(BillDocument document)
  459. {
  460. return document.ID;
  461. }
  462. protected override Guid GetDocumentID(BillDocument document)
  463. {
  464. return document.DocumentLink.ID;
  465. }
  466. protected override string GetDocumentFileName(IEnumerable<BillDocument> documents, Document document)
  467. {
  468. return Documents.FirstOrDefault(x => x.DocumentLink.ID == document.ID)?.DocumentLink.FileName ?? "";
  469. }
  470. }
  471. public class BillPanelDocumentCache : DocumentCache
  472. {
  473. public override TimeSpan MaxAge => TimeSpan.FromHours(1);
  474. private static BillPanelDocumentCache? _cache;
  475. public static BillPanelDocumentCache Cache
  476. {
  477. get
  478. {
  479. _cache ??= DocumentCaches.GetOrRegister<BillPanelDocumentCache>();
  480. return _cache;
  481. }
  482. }
  483. public BillPanelDocumentCache() : base(nameof(BillPanelDocumentCache))
  484. {
  485. }
  486. }