Advertisement
FidoDidoo100

ExportToXML

Nov 22nd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 33.51 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.Practices.Prism.Commands;
  6. using System.Windows;
  7. using ImportExcel.Services;
  8. using ImportExcel.Models;
  9. using System;
  10. using System.Threading.Tasks;
  11.  
  12. namespace ImportExcel.ViewModels
  13. {
  14. public class MainWindowViewModel : BaseViewModel
  15. {
  16. #region Declarations
  17.  
  18. private string filesToBeReadFolderPath;
  19. private string folderPathForGeneratedFile;
  20.  
  21. private DelegateCommand<object> openFolderBrowserCommand;
  22. private DelegateCommand<object> generateXmlFileCommand;
  23. private DelegateCommand<object> clearCommand;
  24. #endregion
  25.  
  26. #region Initialization
  27. public MainWindowViewModel()
  28. {
  29. filesToBeReadFolderPath = string.Empty;
  30. folderPathForGeneratedFile = string.Empty;
  31. }
  32. #endregion
  33.  
  34. #region Properties
  35. public string FilesToBeReadFolderPath
  36. {
  37. get
  38. {
  39. return filesToBeReadFolderPath;
  40. }
  41. set
  42. {
  43. if (value == filesToBeReadFolderPath)
  44. return;
  45.  
  46. filesToBeReadFolderPath = value;
  47. OnPropertyChanged("FilesToBeReadFolderPath");
  48. RefreshCommands();
  49. }
  50. }
  51.  
  52. public string FolderPathForGeneratedFile
  53. {
  54. get
  55. {
  56. return folderPathForGeneratedFile;
  57. }
  58. set
  59. {
  60. if (value == folderPathForGeneratedFile)
  61. return;
  62.  
  63. folderPathForGeneratedFile = value;
  64. OnPropertyChanged("FolderPathForGeneratedFile");
  65. RefreshCommands();
  66. }
  67. }
  68. #endregion
  69.  
  70. #region Commands
  71. public DelegateCommand<object> OpenFolderBrowserCommand
  72. {
  73. get
  74. {
  75. if (openFolderBrowserCommand == null)
  76. openFolderBrowserCommand = new DelegateCommand<object>(OpenFolderBrowser);
  77.  
  78. return openFolderBrowserCommand;
  79. }
  80. }
  81.  
  82. public DelegateCommand<object> GenerateXmlFileCommand
  83. {
  84. get
  85. {
  86. if (generateXmlFileCommand == null)
  87. generateXmlFileCommand = new DelegateCommand<object>(GenerateXmlFile, CanGenerateXmlFile);
  88.  
  89. return generateXmlFileCommand;
  90. }
  91. }
  92.  
  93. public DelegateCommand<object> ClearCommand
  94. {
  95. get
  96. {
  97. if (clearCommand == null)
  98. clearCommand = new DelegateCommand<object>(Clear, CanClear);
  99.  
  100. return clearCommand;
  101. }
  102. }
  103. #endregion
  104.  
  105. #region Methods
  106. private void RefreshCommands()
  107. {
  108. GenerateXmlFileCommand.RaiseCanExecuteChanged();
  109. ClearCommand.RaiseCanExecuteChanged();
  110. }
  111.  
  112. private void OpenFolderBrowser(object folderBrowserPurpose)
  113. {
  114. System.Windows.Forms.FolderBrowserDialog d = new System.Windows.Forms.FolderBrowserDialog();
  115. if ((FolderBrowserPurpose)folderBrowserPurpose == FolderBrowserPurpose.Get)
  116. {
  117. d.Description = "Избертете папка в която се намират файловете";
  118. d.ShowNewFolderButton = false;
  119. }
  120. else if ((FolderBrowserPurpose)folderBrowserPurpose == FolderBrowserPurpose.Save)
  121. {
  122. d.Description = "Изберете място където да се запише генерирания файл";
  123. d.ShowNewFolderButton = true;
  124. }
  125.  
  126. if (d.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  127. {
  128. if ((FolderBrowserPurpose)folderBrowserPurpose == FolderBrowserPurpose.Get)
  129. FilesToBeReadFolderPath = d.SelectedPath;
  130. else if ((FolderBrowserPurpose)folderBrowserPurpose == FolderBrowserPurpose.Save)
  131. FolderPathForGeneratedFile = d.SelectedPath;
  132. }
  133. }
  134.  
  135. private void GenerateXmlFile(object obj)
  136. {
  137. string fullFilePath = FolderPathForGeneratedFile + "\\TransferData.xml";
  138. List<string> filesInDirectory = Directory.GetFiles(FolderPathForGeneratedFile).ToList();
  139. if (filesInDirectory.Contains(fullFilePath))
  140. {
  141. if (MessageBox.Show("В избраната директория съществува файл със същото име! Желаете ли да бъде презаместен?", "Внимание!", MessageBoxButton.YesNo, MessageBoxImage.Information) != MessageBoxResult.Yes)
  142. return;
  143. }
  144.  
  145. ShellSystemService.OnMouseBusy(true);
  146.  
  147. #region Multithreading
  148.  
  149. //IEnumerable<ImportedAccounting> importedAccountings = null;
  150. //Task importedAccountingTask = Task.Run(() => importedAccountings = ImportFromExcel.AccountingReports(FilesToBeReadFolderPath));
  151.  
  152. //IEnumerable<Provider> providers = null;
  153. //Task providersTask = Task.Run(() => providers = ImportFromExcel.Providers(FilesToBeReadFolderPath));
  154.  
  155. //IEnumerable<Client> clients = null;
  156. //Task clientsTask = Task.Run(() => clients = ImportFromExcel.Clients(FilesToBeReadFolderPath));
  157.  
  158. //IEnumerable<Purchase> purchases = null;
  159. //Task purchasesTask = Task.Run(() => purchases = ImportFromExcel.VATPurchases(FilesToBeReadFolderPath));
  160.  
  161. //IEnumerable<Sale> sales = null;
  162. //Task salesTask = Task.Run(() => sales = ImportFromExcel.VATSales(FilesToBeReadFolderPath));
  163.  
  164. //IEnumerable<PurchaseCode> purchaseCodes = null;
  165. //Task purchaseCodesTask = Task.Run(() => purchaseCodes = ImportFromExcel.PurchaseCodes(FilesToBeReadFolderPath));
  166.  
  167. //IEnumerable<SaleCode> saleCodes = null;
  168. //Task saleCodesTask = Task.Run(() => saleCodes = ImportFromExcel.SaleCodes(FilesToBeReadFolderPath));
  169.  
  170. //IEnumerable<TransactionProviders> transactionProviders = null;
  171. //Task transactionProvidersTask = Task.Run(() => transactionProviders = ImportFromExcel.TransactionProviders(FilesToBeReadFolderPath));
  172.  
  173. //IEnumerable<TransactionClients> transactionClients = null;
  174. //Task transactionClientsTask = Task.Run(() => transactionClients = ImportFromExcel.TranscationClients(FilesToBeReadFolderPath));
  175.  
  176. //IDictionary<string, AccountPayroll> payrolls = null;
  177. //Task payrollsTask = Task.Run(() => payrolls = ImportFromExcel.Payrolls(FilesToBeReadFolderPath));
  178.  
  179. //Task.WaitAll(new Task[] { importedAccountingTask, providersTask, clientsTask, purchasesTask, salesTask, purchaseCodesTask,
  180. //saleCodesTask, transactionProvidersTask, transactionClientsTask, payrollsTask});
  181.  
  182. #endregion //Multithreading
  183.  
  184. #region OneThread
  185.  
  186. IEnumerable<ImportedAccounting> importedAccountings = ImportFromExcel.AccountingReports(FilesToBeReadFolderPath);
  187. IEnumerable<Provider> providers = ImportFromExcel.Providers(FilesToBeReadFolderPath);
  188. IEnumerable<Client> clients = ImportFromExcel.Clients(FilesToBeReadFolderPath);
  189. IEnumerable<Purchase> purchases = ImportFromExcel.VATPurchases(FilesToBeReadFolderPath);
  190. IEnumerable<Sale> sales = ImportFromExcel.VATSales(FilesToBeReadFolderPath);
  191. IEnumerable<PurchaseCode> purchaseCodes = ImportFromExcel.PurchaseCodes(FilesToBeReadFolderPath);
  192. IEnumerable<SaleCode> saleCodes = ImportFromExcel.SaleCodes(FilesToBeReadFolderPath);
  193. IEnumerable<TransactionProviders> transactionProviders = ImportFromExcel.TransactionProviders(FilesToBeReadFolderPath);
  194. IEnumerable<TransactionClients> transactionClients = ImportFromExcel.TranscationClients(FilesToBeReadFolderPath);
  195. IDictionary<string, AccountPayroll> payrolls = ImportFromExcel.Payrolls(FilesToBeReadFolderPath);
  196.  
  197. #endregion //OneThread
  198.  
  199. List<Company> importedCompanies = ImportedCompanies(providers, clients);
  200.  
  201. TransferData data = DataToExport(importedAccountings, providers, clients, purchases, sales, purchaseCodes, saleCodes,
  202. transactionProviders, transactionClients, payrolls);
  203.  
  204. ExportToXmlService.Export(data, fullFilePath);
  205.  
  206. ShellSystemService.OnMouseBusy(false);
  207. }
  208.  
  209. private bool ValidateAccountings(List<Accounting> solidAccountings)
  210. {
  211. if (solidAccountings == null || solidAccountings.Count == 0)
  212. {
  213.  
  214. }
  215.  
  216. List<string> invalidAccountingsNums = new List<string>();
  217. foreach (Accounting acc in solidAccountings)
  218. {
  219.  
  220. }
  221.  
  222. if (invalidAccountingsNums.Count > 0)
  223. {
  224. StringBuilder invalidAcc = new StringBuilder();
  225.  
  226. for (int i = 0; i <= invalidAccountingsNums.Count; i++)
  227. {
  228. invalidAcc.Append(i + ". " + invalidAccountingsNums[i] + "\n");
  229. }
  230.  
  231. return MessageBox.Show("Операции с номера: \n" + invalidAcc + "\n" + "са с невалидни суми! Желаете ли да продължи експорта?"
  232. , "Внимание!"
  233. , MessageBoxButton.YesNo
  234. , MessageBoxImage.Question) == MessageBoxResult.Yes;
  235. }
  236.  
  237. return true;
  238. }
  239.  
  240. private void ShowInvalidFilesPrompt()
  241. {
  242.  
  243. }
  244.  
  245. private bool CanGenerateXmlFile(object arg)
  246. {
  247. return FolderPathForGeneratedFile != string.Empty && FilesToBeReadFolderPath != string.Empty;
  248. }
  249.  
  250. private void Clear(object obj)
  251. {
  252. FilesToBeReadFolderPath = string.Empty;
  253. FolderPathForGeneratedFile = string.Empty;
  254. }
  255.  
  256. private bool CanClear(object arg)
  257. {
  258. return true;
  259. }
  260.  
  261. private List<Company> ImportedCompanies(IEnumerable<Provider> providers, IEnumerable<Client> clients)
  262. {
  263. List<Company> importedCompanies = new List<Company>();
  264.  
  265. foreach (Provider provider in providers)
  266. {
  267. Company currentCompany = new Company();
  268. currentCompany.Identificator = provider.ProviderNumber;
  269. currentCompany.Name = provider.Name;
  270.  
  271. Address currentCompanyAddress = new Address();
  272. currentCompanyAddress.Location = provider.Address;
  273.  
  274. currentCompany.Addresses = new List<Address>();
  275. currentCompany.Addresses.Add(currentCompanyAddress);
  276.  
  277. currentCompany.Bulstat = provider.UIC;
  278. currentCompany.VatNumber = provider.INbyVAT;
  279. currentCompany.Email = provider.Email;
  280. currentCompany.Type = CompanyType.Supplier;
  281.  
  282. importedCompanies.Add(currentCompany);
  283. }
  284.  
  285. foreach (Client client in clients)
  286. {
  287. Company currentCompany = new Company();
  288. currentCompany.Identificator = client.ClientNumber;
  289. currentCompany.Name = client.Name;
  290.  
  291. Address currentCompanyAddress = new Address();
  292. currentCompanyAddress.City = client.Address;
  293.  
  294. currentCompany.Addresses = new List<Address>();
  295. currentCompany.Addresses.Add(currentCompanyAddress);
  296.  
  297. currentCompany.Bulstat = client.UIC;
  298. currentCompany.VatNumber = client.INbyVAT;
  299. currentCompany.Email = client.Email;
  300. currentCompany.Type = CompanyType.Customer;
  301.  
  302. importedCompanies.Add(currentCompany);
  303. }
  304.  
  305. return importedCompanies;
  306. }
  307.  
  308. private TransferData DataToExport(IEnumerable<ImportedAccounting> importedAccountings, IEnumerable<Provider> providers,
  309. IEnumerable<Client> clients, IEnumerable<Purchase> purchases, IEnumerable<Sale> sales, IEnumerable<PurchaseCode> purchaseCodes,
  310. IEnumerable<SaleCode> saleCodes, IEnumerable<TransactionProviders> transactionProviders, IEnumerable<TransactionClients> transactionClients,
  311. IDictionary<string, AccountPayroll> payrolls)
  312. {
  313. TransferData data = new TransferData();
  314. data.Accountings = new List<Accounting>();
  315.  
  316. List<Accounting> allAccountings = new List<Accounting>();
  317.  
  318. string currentAccountingNumber = string.Empty;
  319. string currentCompanyBulstat = string.Empty;
  320. List<Company> allComapnies = ImportedCompanies(providers, clients);
  321. Accounting currentAccounting = new Accounting();
  322. List<AccountingDetail> currentDetails = new List<AccountingDetail>();
  323. var watch = System.Diagnostics.Stopwatch.StartNew();
  324. for (int i = 0; i < 10000; i++)
  325. {
  326. ImportedAccounting currentElement = importedAccountings.ElementAt(i);
  327.  
  328. try
  329. {
  330. if (currentAccountingNumber == currentElement.Number)
  331. {
  332. allAccountings.Remove(currentAccounting);
  333. AccountingDetail currentDetail = new AccountingDetail();
  334. Account currentAccount = new Account();
  335.  
  336. if (payrolls.ContainsKey(currentElement.FinancialAccount))
  337. {
  338. currentAccount.Number = payrolls[currentElement.FinancialAccount]?.Code;
  339. currentAccount.Description = payrolls[currentElement.FinancialAccount]?.Name;
  340. }
  341.  
  342.  
  343.  
  344. if (currentDetails.Any(x => x.Account.Number == currentAccount.Number))
  345. {
  346. currentDetail = currentAccounting.AccountingDetails.FirstOrDefault(x => x.AccountNumber == currentAccount.Number);
  347. if (currentElement.Currency != "BGN")
  348. {
  349. AccountingCurrencyDetail currentCurrencyDetail = new AccountingCurrencyDetail();
  350. currentCurrencyDetail.Currency = currentElement.Currency;
  351. currentCurrencyDetail.CurrencyAmount = Math.Abs(currentElement.CurrencyAmount);
  352. if (currentElement.CurrencyAmount != 0)
  353. {
  354. currentCurrencyDetail.ExchangeRate = Math.Round(currentElement.Amount / currentElement.CurrencyAmount, 5);
  355. }
  356. else
  357. {
  358. currentCurrencyDetail.ExchangeRate = 1;
  359. }
  360. currentCurrencyDetail.FixedRate = Math.Round(currentElement.FixedRate,5);
  361. if (currentDetail.AccountingCurrencyDetail != null)
  362. currentDetail.AccountingCurrencyDetail.CurrencyAmount += currentCurrencyDetail.CurrencyAmount;
  363. else
  364. currentDetail.AccountingCurrencyDetail = currentCurrencyDetail;
  365. }
  366. currentDetails.FirstOrDefault(x => x.Account.Number == currentAccount.Number).Amount += Math.Abs(currentElement.Amount);
  367. }
  368. else
  369. {
  370. currentDetail.Account = currentAccount;
  371. currentDetail.AccountNumber = currentDetail.Account.Number;
  372. currentDetail.AccountDescription = currentDetail.Account.Description;
  373. currentDetail.Direction = currentElement.Direction;
  374.  
  375. if (currentElement.Currency != "BGN")
  376. {
  377. AccountingCurrencyDetail currentCurrencyDetail = new AccountingCurrencyDetail();
  378. currentCurrencyDetail.Currency = currentElement.Currency;
  379. currentCurrencyDetail.CurrencyAmount = Math.Abs(currentElement.CurrencyAmount);
  380. if (currentElement.CurrencyAmount != 0)
  381. {
  382. currentCurrencyDetail.ExchangeRate = Math.Round(currentElement.Amount / currentElement.CurrencyAmount, 5);
  383. }
  384. else
  385. {
  386. currentCurrencyDetail.ExchangeRate = 1;
  387. }
  388. currentCurrencyDetail.FixedRate = Math.Round(currentElement.FixedRate, 5);
  389. if (currentDetail.AccountingCurrencyDetail != null)
  390. currentDetail.AccountingCurrencyDetail.CurrencyAmount += currentCurrencyDetail.CurrencyAmount;
  391. else
  392. currentDetail.AccountingCurrencyDetail = currentCurrencyDetail;
  393. }
  394. currentDetail.Amount = Math.Abs(currentElement.Amount);
  395. //TODO should add currentDetail.VatTerm
  396. currentDetails.Add(currentDetail);
  397. currentAccounting.AccountingDetails = currentDetails;
  398. }
  399. }
  400. else
  401. {
  402. currentDetails = new List<AccountingDetail>();
  403. currentAccountingNumber = currentElement.Number;
  404. currentAccounting = new Accounting();
  405. currentAccounting.Number = importedAccountings.ElementAt(i).Number;
  406. currentAccounting.AccountingDate = currentElement.AccountingDate;
  407.  
  408. Document currentDocument = new Document();
  409.  
  410. TransactionClients currentTransactionClient = transactionClients.FirstOrDefault(x => x.AccountingOperationNumber == currentAccounting.Number);
  411. TransactionProviders currentTransactionProvider = transactionProviders.FirstOrDefault(x => x.AccountingOperationNumber == currentAccounting.Number);
  412.  
  413. DateTime currentDocumentDate = new DateTime();
  414. string currentViesMonthCode = string.Empty;
  415.  
  416. if (currentTransactionClient != null)
  417. {
  418. Sale currentSale = sales.FirstOrDefault(x => x.DocumentNumber == currentTransactionClient.Invoice);
  419. if (currentSale != null)
  420. {
  421. if (DateTime.TryParse(currentSale.DocumentDate, out currentDocumentDate))
  422. {
  423. if (currentDocumentDate == DateTime.MinValue)
  424. {
  425. currentDocument.Date = currentAccounting.AccountingDate;
  426. }
  427. else
  428. {
  429. currentDocument.Date = currentDocumentDate;
  430. }
  431. }
  432.  
  433. currentDocument.Number = currentSale.DocumentNumber;
  434.  
  435. currentCompanyBulstat = currentSale.PartnerBulstat;
  436. currentViesMonthCode = currentSale.Code;
  437. }
  438.  
  439. string currentViewMonth = saleCodes.FirstOrDefault(x => x.Code == currentViesMonthCode)?.Name;
  440. DateTime currentViewMonthTemp = new DateTime();
  441. DateTime.TryParse(currentViewMonth, out currentViewMonthTemp);
  442. if (currentViewMonthTemp != DateTime.MinValue)
  443. currentAccounting.ViesMonth = currentViewMonthTemp;
  444. else
  445. currentAccounting.ViesMonth = currentAccounting.AccountingDate;
  446.  
  447. Company currentCompany = allComapnies.FirstOrDefault(x => x.VatNumber == currentCompanyBulstat);
  448.  
  449. if (currentCompany != null)
  450. {
  451. currentAccounting.Company = currentCompany;
  452. }
  453. currentAccounting.Term = currentElement.Description;
  454.  
  455. AccountingDetail currentDetail = new AccountingDetail();
  456. Account currentAccount = new Account();
  457. currentAccount.Number = payrolls.FirstOrDefault(x => x.Key == currentElement.FinancialAccount).Value.Code;
  458. currentAccount.Description = payrolls.FirstOrDefault(x => x.Key == currentElement.FinancialAccount).Value.Name;
  459.  
  460.  
  461. currentDocument.DocumentType = DocumentType(currentAccounting.Number, currentDetail.Direction);
  462.  
  463. currentAccounting.Document = currentDocument;
  464. currentDetail.Account = currentAccount;
  465. currentDetail.AccountNumber = currentDetail.Account.Number;
  466. currentDetail.AccountDescription = currentDetail.Account.Description;
  467. currentDetail.Direction = currentElement.Direction;
  468.  
  469. if (currentElement.Currency != "BGN")
  470. {
  471. AccountingCurrencyDetail currentCurrencyDetail = new AccountingCurrencyDetail();
  472. currentCurrencyDetail.Currency = currentElement.Currency;
  473. currentCurrencyDetail.CurrencyAmount = Math.Abs(currentElement.CurrencyAmount);
  474. if (currentElement.CurrencyAmount != 0)
  475. {
  476. currentCurrencyDetail.ExchangeRate = Math.Round(currentElement.Amount / currentElement.CurrencyAmount, 5);
  477. }
  478. else
  479. {
  480. currentCurrencyDetail.ExchangeRate = 1;
  481. }
  482.  
  483. currentCurrencyDetail.FixedRate = Math.Round(currentElement.FixedRate, 5);
  484. currentDetail.AccountingCurrencyDetail = currentCurrencyDetail;
  485. }
  486. currentDetail.Amount = Math.Abs(currentElement.Amount);
  487.  
  488. //TODO should add currentDetail.VatTerm
  489. if (currentDetail != null)
  490. {
  491. currentDetails.Add(currentDetail);
  492. }
  493. if (currentAccounting.AccountingDetails == null)
  494. {
  495. currentAccounting.AccountingDetails = new List<AccountingDetail>();
  496. }
  497. currentAccounting.AccountingDetails = currentDetails;
  498. }
  499. else if (currentTransactionProvider != null)
  500. {
  501. string currentTransactionProviders = transactionProviders.FirstOrDefault(x => x.AccountingOperationNumber == currentAccounting.Number).Invoice;
  502. Purchase currentPurchase = purchases.FirstOrDefault(x => x.DocumentNumber == currentTransactionProviders);
  503.  
  504. if (currentPurchase != null)
  505. {
  506. if (DateTime.TryParse(currentPurchase.DocumentDate, out currentDocumentDate))
  507. {
  508. if (currentDocumentDate == DateTime.MinValue)
  509. {
  510. currentDocument.Date = currentAccounting.AccountingDate;
  511. }
  512. else
  513. {
  514. currentDocument.Date = currentDocumentDate;
  515. }
  516. }
  517.  
  518. currentDocument.Number = currentPurchase.DocumentNumber;
  519.  
  520. int currentDocumentType = 0;
  521. int.TryParse(currentPurchase.DocumentType, out currentDocumentType);
  522. currentDocument.DocumentType = currentDocumentType;
  523.  
  524. currentCompanyBulstat = currentPurchase.PartnerBulstat;
  525. currentViesMonthCode = currentPurchase.Code;
  526.  
  527. }
  528.  
  529. string currentViewMonth = saleCodes.FirstOrDefault(x => x.Code == currentViesMonthCode)?.Name;
  530.  
  531. DateTime currentViewMonthTemp = new DateTime();
  532. DateTime.TryParse(currentViewMonth, out currentViewMonthTemp);
  533. if (currentViewMonthTemp != DateTime.MinValue)
  534. currentAccounting.ViesMonth = currentViewMonthTemp;
  535. else
  536. currentAccounting.ViesMonth = currentAccounting.AccountingDate;
  537.  
  538. Company currentCompany = allComapnies.FirstOrDefault(x => x.VatNumber == currentCompanyBulstat);
  539.  
  540. if (currentCompany != null)
  541. {
  542. currentAccounting.Company = currentCompany;
  543. }
  544. currentAccounting.Term = currentElement.Description;
  545.  
  546. AccountingDetail currentDetail = new AccountingDetail();
  547. Account currentAccount = new Account();
  548. currentAccount.Number = payrolls.FirstOrDefault(x => x.Key == currentElement.FinancialAccount).Value.Code;
  549. currentAccount.Description = payrolls.FirstOrDefault(x => x.Key == currentElement.FinancialAccount).Value.Name;
  550.  
  551. currentDocument.DocumentType = DocumentType(currentAccounting.Number, currentDetail.Direction);
  552.  
  553. currentAccounting.Document = currentDocument;
  554. currentDetail.Account = currentAccount;
  555. currentDetail.AccountNumber = currentDetail.Account.Number;
  556. currentDetail.AccountDescription = currentDetail.Account.Description;
  557. currentDetail.Direction = currentElement.Direction;
  558.  
  559. if (currentElement.Currency != "BGN")
  560. {
  561. AccountingCurrencyDetail currentCurrencyDetail = new AccountingCurrencyDetail();
  562. currentCurrencyDetail.Currency = currentElement.Currency;
  563. currentCurrencyDetail.CurrencyAmount = Math.Abs(currentElement.CurrencyAmount);
  564.  
  565. if (currentElement.CurrencyAmount != 0)
  566. {
  567. currentCurrencyDetail.ExchangeRate = Math.Round(currentElement.Amount / currentElement.CurrencyAmount, 5);
  568. }
  569. else
  570. {
  571. currentCurrencyDetail.ExchangeRate = 1;
  572. }
  573.  
  574. currentCurrencyDetail.FixedRate = Math.Round(currentElement.FixedRate, 5);
  575. currentDetail.AccountingCurrencyDetail = currentCurrencyDetail;
  576. }
  577. currentDetail.Amount = Math.Abs(currentElement.Amount);
  578.  
  579. //TODO should add currentDetail.VatTerm
  580. if (currentDetail != null)
  581. {
  582. currentDetails.Add(currentDetail);
  583. }
  584. if (currentAccounting.AccountingDetails == null)
  585. {
  586. currentAccounting.AccountingDetails = new List<AccountingDetail>();
  587. }
  588. currentAccounting.AccountingDetails = currentDetails;
  589. }
  590. else
  591. {
  592. string currentViewMonth = saleCodes.FirstOrDefault(x => x.Code == currentViesMonthCode)?.Name;
  593.  
  594. DateTime currentViewMonthTemp = new DateTime();
  595. DateTime.TryParse(currentViewMonth, out currentViewMonthTemp);
  596. if (currentViewMonthTemp != DateTime.MinValue)
  597. currentAccounting.ViesMonth = currentViewMonthTemp;
  598. else
  599. currentAccounting.ViesMonth = currentAccounting.AccountingDate;
  600. currentAccounting.Term = currentElement.Description;
  601.  
  602. AccountingDetail currentDetail = new AccountingDetail();
  603. Account currentAccount = new Account();
  604. currentAccount.Number = payrolls.FirstOrDefault(x => x.Key == currentElement.FinancialAccount).Value.Code;
  605. currentAccount.Description = payrolls.FirstOrDefault(x => x.Key == currentElement.FinancialAccount).Value.Name;
  606.  
  607. currentDocument.Date = currentAccounting.AccountingDate;
  608. currentDocument.DocumentType = DocumentType(currentAccounting.Number, currentDetail.Direction);
  609.  
  610. currentAccounting.Document = currentDocument;
  611. currentDetail.Account = currentAccount;
  612. currentDetail.AccountNumber = currentDetail.Account.Number;
  613. currentDetail.AccountDescription = currentDetail.Account.Description;
  614. currentDetail.Direction = currentElement.Direction;
  615. currentDetail.Amount = Math.Abs(currentElement.CurrencyAmount);
  616. //TODO should add currentDetail.VatTerm
  617. if (currentDetail != null)
  618. {
  619. currentDetails.Add(currentDetail);
  620. }
  621. if (currentAccounting.AccountingDetails == null)
  622. {
  623. currentAccounting.AccountingDetails = new List<AccountingDetail>();
  624. }
  625. currentAccounting.AccountingDetails = currentDetails;
  626. }
  627. }
  628. }
  629. catch (NullReferenceException ex)
  630. {
  631.  
  632. }
  633. finally
  634. {
  635. if (currentAccounting != null)
  636. allAccountings.Add(currentAccounting);
  637. }
  638.  
  639. }
  640.  
  641. watch.Stop();
  642. var elapsedMs = watch.ElapsedMilliseconds;
  643.  
  644. data.Accountings = allAccountings;
  645. return data;
  646. }
  647.  
  648. private int DocumentType(string number, OperationDirection operationDirection)
  649. {
  650. string code = number.Substring(0, 2);
  651.  
  652. switch (code)
  653. {
  654. case "IR":
  655. return 1;
  656. case "RE":
  657. return 10;
  658. case "ПО":
  659. return 15;
  660. case "CV":
  661. if (operationDirection == OperationDirection.Credit)
  662. {
  663. return 9;
  664. }
  665. else
  666. {
  667. return 8;
  668. }
  669. case "BO":
  670. return 11;
  671. case "GL":
  672. return 10;
  673. case "SI":
  674. return 1;
  675. case "FI":
  676. return 1;
  677. case "SC":
  678. if (operationDirection == OperationDirection.Credit)
  679. {
  680. return 3;
  681. }
  682. else
  683. {
  684. return 2;
  685. }
  686. case "PR":
  687. return 10;
  688. case "FA":
  689. return 10;
  690. default:
  691. return -1;
  692.  
  693. }
  694. }
  695.  
  696. #endregion
  697. }
  698. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement