documents.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. namespace App;
  3. /**
  4. * @var string $type_string
  5. */
  6. use App\Constraint\NotInConstraint;
  7. use App\Enums\AruStatuses;
  8. use App\Enums\DocumentTypes;
  9. use App\Enums\InvoiceStatuses;
  10. use RuntimeException;
  11. $core = Core::getInstance();
  12. $user = $core->getUser();
  13. $user->requireAdmin();
  14. switch($type_string) {
  15. case "invoices":
  16. $title = __("Invoices");
  17. $type = DocumentTypes::INVOICE;
  18. $singular = __("%s invoice");
  19. $plural = __("%s invoices");
  20. $status_field = "invoice_status";
  21. break;
  22. case "goods-in":
  23. $title = __("Goods-In");
  24. $type = DocumentTypes::GOODS_IN;
  25. $singular = __("%s goods-in documents");
  26. $plural = __("%s goods-in documents");
  27. $status_field = null;
  28. break;
  29. case "arus":
  30. $title = __("ARUs");
  31. $type = DocumentTypes::ADVANCE_REPLACEMENT_UNIT;
  32. $singular = __("%s ARU");
  33. $plural = __("%s ARUs");
  34. $status_field = "aru_status";
  35. break;
  36. default:
  37. throw new RuntimeException(__("Invalid type string."));
  38. }
  39. $core->addBreadcrumb("/$type_string", $title);
  40. $type_name = DocumentTypes::enumToText($type);
  41. $nvp = [
  42. "type" => $type,
  43. "dts_deleted" => null
  44. ];
  45. $request = Request::fromRequest();
  46. $supplier_id = $request->int("supplier_id");
  47. if($supplier_id) {
  48. $nvp["supplier_id"] = $supplier_id;
  49. }
  50. $customer_id = $request->int("customer_id");
  51. if($customer_id) {
  52. $customer = Customer::get($customer_id);
  53. if($customer) {
  54. $nvp["location_id"] = array_column($customer->getLocations(), "id");
  55. }
  56. else {
  57. $nvp["location_id"] = 0;
  58. }
  59. }
  60. $location_id = $request->int("location_id");
  61. if($location_id) {
  62. $nvp["location_id"] = $location_id;
  63. }
  64. $status = $request->int("status");
  65. if($status !== null && $status_field) {
  66. $nvp[$status_field] = $status;
  67. }
  68. $number = $request->int("number");
  69. if($number) {
  70. $nvp["number"] = $number;
  71. }
  72. $custom_filter = $request->string("custom_filter");
  73. switch($custom_filter) {
  74. case "open":
  75. if($type === DocumentTypes::INVOICE) {
  76. $nvp["invoice_status"] = new NotInConstraint(InvoiceStatuses::PAID, InvoiceStatuses::VOID);
  77. }
  78. if($type === DocumentTypes::ADVANCE_REPLACEMENT_UNIT) {
  79. $nvp["aru_status"] = new NotInConstraint(AruStatuses::COMPLETED, AruStatuses::CANCELLED);
  80. }
  81. break;
  82. }
  83. $count = Document::count($nvp);
  84. $pagination = new Pagination($count);
  85. $pagination->setTerms($singular, $plural);
  86. $documents = Document::query($nvp, [
  87. "sort" => "id",
  88. "order" => "desc",
  89. "offset" => $pagination->getOffset(),
  90. "limit" => $pagination->getPageSize()
  91. ]);
  92. $core->addScript("/js/new-document-modal.js");
  93. View::include("header");
  94. View::include("documents", compact("title", "type_string", "type", "documents", "pagination", "type_name", "supplier_id", "customer_id", "location_id", "status", "number", "custom_filter"));
  95. View::include("modals/new_document");
  96. View::include("footer");