SupplierBillPanel.xaml.cs 15 KB

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