documents.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\SaleOrderStatuses;
  10. use RuntimeException;
  11. $core = Core::getInstance();
  12. $user = $core->getUser();
  13. $user->requireAdmin();
  14. switch($type_string) {
  15. case "sale-orders":
  16. $title = __("Sale Orders");
  17. $type = DocumentTypes::SALE_ORDER;
  18. $singular = __("%s sale order");
  19. $plural = __("%s sale orders");
  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. case "delivery-notes":
  37. $title = __("Delivery Notes");
  38. $type = DocumentTypes::DELIVERY_NOTE;
  39. $singular = __("%s delivery note");
  40. $plural = __("%s delivery notes");
  41. $status_field = null;
  42. break;
  43. default:
  44. throw new RuntimeException(__("Invalid type string."));
  45. }
  46. $core->addBreadcrumb("/$type_string", $title);
  47. $type_name = DocumentTypes::enumToText($type);
  48. $nvp = [
  49. "type" => $type,
  50. "dts_deleted" => null
  51. ];
  52. $request = Request::fromRequest();
  53. $supplier_id = $request->int("supplier_id");
  54. if($supplier_id) {
  55. $nvp["supplier_id"] = $supplier_id;
  56. }
  57. $customer_id = $request->int("customer_id");
  58. if($customer_id) {
  59. $customer = Customer::get($customer_id);
  60. if($customer) {
  61. $nvp["location_id"] = array_column($customer->getLocations(), "id");
  62. }
  63. else {
  64. $nvp["location_id"] = 0;
  65. }
  66. }
  67. $location_id = $request->int("location_id");
  68. if($location_id) {
  69. $nvp["location_id"] = $location_id;
  70. }
  71. $status = $request->int("status");
  72. if($status !== null && $status_field) {
  73. $nvp[$status_field] = $status;
  74. }
  75. $number = $request->int("number");
  76. if($number) {
  77. $nvp["number"] = $number;
  78. }
  79. $custom_filter = $request->string("custom_filter");
  80. switch($custom_filter) {
  81. case "open":
  82. if($type === DocumentTypes::SALE_ORDER) {
  83. $nvp["invoice_status"] = new NotInConstraint(SaleOrderStatuses::PAID, SaleOrderStatuses::VOID);
  84. }
  85. if($type === DocumentTypes::ADVANCE_REPLACEMENT_UNIT) {
  86. $nvp["aru_status"] = new NotInConstraint(AruStatuses::COMPLETED, AruStatuses::CANCELLED);
  87. }
  88. break;
  89. }
  90. $count = Document::count($nvp);
  91. $pagination = new Pagination($count);
  92. $pagination->setTerms($singular, $plural);
  93. $documents = Document::query($nvp, [
  94. "sort" => "id",
  95. "order" => "desc",
  96. "offset" => $pagination->getOffset(),
  97. "limit" => $pagination->getPageSize()
  98. ]);
  99. $core->addScript("/js/new-document-modal.js");
  100. View::include("header");
  101. View::include("documents", compact("title", "type_string", "type", "documents", "pagination", "type_name", "supplier_id", "customer_id", "location_id", "status", "number", "custom_filter"));
  102. View::include("modals/new_document");
  103. View::include("footer");