|
|
@@ -0,0 +1,333 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App;
|
|
|
+
|
|
|
+use App\Enums\DocumentTypes;
|
|
|
+use App\Enums\InvoiceStatuses;
|
|
|
+use App\Enums\SaleStatuses;
|
|
|
+use RuntimeException;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @var int $number
|
|
|
+ * @var string $action
|
|
|
+ */
|
|
|
+
|
|
|
+$number = $number ?? null;
|
|
|
+$action = $action ?? null;
|
|
|
+
|
|
|
+$request = Request::fromRequest();
|
|
|
+
|
|
|
+$controller = new class($number) {
|
|
|
+
|
|
|
+ private ?int $number;
|
|
|
+
|
|
|
+ private Request $request;
|
|
|
+
|
|
|
+ private User $user;
|
|
|
+
|
|
|
+ public function __construct(?int $number) {
|
|
|
+ $core = Core::getInstance();
|
|
|
+ $this->user = $core->getUser();
|
|
|
+
|
|
|
+ $this->number = $number;
|
|
|
+ $this->request = Request::fromRequest();
|
|
|
+ }
|
|
|
+
|
|
|
+ private function getSale(): ?Sale {
|
|
|
+ if(!$this->number) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return Sale::getByNumber($this->number);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function requireSale(): Sale {
|
|
|
+ $sale = $this->getSale();
|
|
|
+ if(!$sale) {
|
|
|
+ throw new RuntimeException(__("Sale not found."));
|
|
|
+ }
|
|
|
+ return $sale;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function requireNote(?int $note_id): SaleNote {
|
|
|
+ $notes = $this->requireSale()->getNotes(["id" => $note_id]);
|
|
|
+ if(!$notes) {
|
|
|
+ throw new RuntimeException(__("Note not found."));
|
|
|
+ }
|
|
|
+ return $notes[0];
|
|
|
+ }
|
|
|
+
|
|
|
+ private function requireItem(?int $item_id): SaleItem {
|
|
|
+ $items = $this->requireSale()->getItems(["id" => $item_id]);
|
|
|
+ if(!$items) {
|
|
|
+ throw new RuntimeException(__("Item not found."));
|
|
|
+ }
|
|
|
+ return $items[0];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function view(): void {
|
|
|
+ $sale = $this->requireSale();
|
|
|
+
|
|
|
+ $core = Core::getInstance();
|
|
|
+ $this->user->requireAdmin();
|
|
|
+
|
|
|
+ $core->addBreadcrumb("/sales", __("Sales"));
|
|
|
+ $core->addBreadcrumb($sale->getUrl(), __("Sale %d", $sale->getNumber()));
|
|
|
+
|
|
|
+ $core->addScript("/js/sale-item-modal.js");
|
|
|
+ $core->addScript("/js/sale-note-modal.js");
|
|
|
+ $core->addScript("/js/sale-page.js");
|
|
|
+
|
|
|
+ View::include("header");
|
|
|
+ View::include("sale", compact("sale"));
|
|
|
+ View::include("modals/sale_item");
|
|
|
+ View::include("modals/sale_note");
|
|
|
+ View::include("footer");
|
|
|
+ }
|
|
|
+
|
|
|
+ public function createOrUpdate(): void {
|
|
|
+
|
|
|
+ $sale = $this->getSale();
|
|
|
+ if(!$sale) {
|
|
|
+
|
|
|
+ $company_id = $this->request->int("company_id");
|
|
|
+ $company = Company::get($company_id);
|
|
|
+ if(!$company) {
|
|
|
+ throw new RuntimeException(__("Company not found."));
|
|
|
+ }
|
|
|
+
|
|
|
+ $location_id = $this->request->int("location_id");
|
|
|
+ $location = Location::get($location_id);
|
|
|
+ if(!$location) {
|
|
|
+ throw new RuntimeException(__("Location not found."));
|
|
|
+ }
|
|
|
+
|
|
|
+ $sale = new Sale();
|
|
|
+ $sale->company_id = $company->id;
|
|
|
+ $sale->location_id = $location->id;
|
|
|
+ $sale->user_id = $this->user->id;
|
|
|
+ $sale->status = SaleStatuses::NEW;
|
|
|
+ $sale->currency = "GBP";
|
|
|
+ $sale->dts_created = Database::dts();
|
|
|
+
|
|
|
+ $success_message = __("Sale created successfully.");
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ $sale->status = $this->request->int("status");
|
|
|
+ $sale->currency = $this->request->string("currency");
|
|
|
+ $sale->tax_rate = $this->request->decimal("tax_rate", 2);
|
|
|
+
|
|
|
+ if(!SaleStatuses::validate($sale->status)) {
|
|
|
+ throw new RuntimeException(__("Status is invalid."));
|
|
|
+ }
|
|
|
+
|
|
|
+ $sale->dts_updated = Database::dts();
|
|
|
+
|
|
|
+ $success_message = __("Sale updated successfully.");
|
|
|
+ }
|
|
|
+
|
|
|
+ $sale->commit();
|
|
|
+
|
|
|
+ Session::addAlert("success", $success_message);
|
|
|
+
|
|
|
+ Http::redirect($sale->getUrl());
|
|
|
+ }
|
|
|
+
|
|
|
+ public function delete(): void {
|
|
|
+ $sale = $this->requireSale();
|
|
|
+ $sale->softDelete();
|
|
|
+ JSON::success();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function submitNote(): void {
|
|
|
+ $sale = $this->requireSale();
|
|
|
+
|
|
|
+ $note_id = $this->request->int("sale_note_id");
|
|
|
+ if($note_id) {
|
|
|
+ $note = $this->requireNote($note_id);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ $note = new SaleNote();
|
|
|
+ $note->sale_id = $sale->id;
|
|
|
+ $note->user_id = $this->user->id;
|
|
|
+ $note->dts_created = Database::dts();
|
|
|
+ }
|
|
|
+
|
|
|
+ $note->content = $this->request->string("content");
|
|
|
+ if(empty($note->content)) {
|
|
|
+ throw new RuntimeException(__("Content cannot be empty."));
|
|
|
+ }
|
|
|
+
|
|
|
+ $note->commit();
|
|
|
+
|
|
|
+ JSON::success([
|
|
|
+ "html" => View::getHtml("sale_notes_section", compact("sale"))
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function deleteNote(): void {
|
|
|
+ $note_id = $this->request->int("note_id");
|
|
|
+ $note = $this->requireNote($note_id);
|
|
|
+ $note->softDelete();
|
|
|
+ JSON::success();
|
|
|
+ }
|
|
|
+
|
|
|
+ public function submitItem(): void {
|
|
|
+ $sale = $this->requireSale();
|
|
|
+
|
|
|
+ $sale_item_id = $this->request->int("sale_item_id");
|
|
|
+ if($sale_item_id) {
|
|
|
+ $item = $this->requireItem($sale_item_id);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ $item = new SaleItem();
|
|
|
+ $item->sale_id = $sale->id;
|
|
|
+ }
|
|
|
+
|
|
|
+ $product_id = $this->request->int("product_id");
|
|
|
+ $product = Product::get($product_id);
|
|
|
+ if(!$product) {
|
|
|
+ throw new RuntimeException(__("Product not found."));
|
|
|
+ }
|
|
|
+
|
|
|
+ $item->product_id = $product->id;
|
|
|
+ $item->quantity = $this->request->int("quantity");
|
|
|
+ $item->unit_price = $this->request->decimal("unit_price", 2);
|
|
|
+ $item->commit();
|
|
|
+
|
|
|
+ JSON::success([
|
|
|
+ "html" => View::getHtml("sale_items_section", compact("sale"))
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function deleteItem(): void {
|
|
|
+ $item_id = $this->request->int("sale_item_id");
|
|
|
+ $item = $this->requireItem($item_id);
|
|
|
+ $sale = $item->getSale();
|
|
|
+
|
|
|
+ $item->delete();
|
|
|
+
|
|
|
+ JSON::success([
|
|
|
+ "html" => View::getHtml("sale_items_section", compact("sale"))
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param int[] $item_ids
|
|
|
+ * @return SaleItem[]
|
|
|
+ */
|
|
|
+ private function validateItemIds(array $item_ids): array {
|
|
|
+ $items = [];
|
|
|
+ foreach($item_ids as $item_id) {
|
|
|
+ $items[] = $this->requireItem($item_id);
|
|
|
+ }
|
|
|
+ return $items;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function deleteItems(): void {
|
|
|
+ $sale = $this->requireSale();
|
|
|
+ $item_ids = $this->request->int("sale_item_ids");
|
|
|
+ if(empty($item_ids) || !is_array($item_ids)) {
|
|
|
+ throw new RuntimeException(__("No items selected."));
|
|
|
+ }
|
|
|
+
|
|
|
+ $items = $this->validateItemIds($item_ids);
|
|
|
+
|
|
|
+ foreach($items as $item) {
|
|
|
+ $item->delete();
|
|
|
+ }
|
|
|
+
|
|
|
+ Session::addAlert("success", __("Items deleted successfully."));
|
|
|
+ Http::redirect($sale->getUrl());
|
|
|
+ }
|
|
|
+
|
|
|
+ public function createInvoiceFromItems(): void {
|
|
|
+ $sale = $this->requireSale();
|
|
|
+ $location = $sale->getLocation();
|
|
|
+ $address = $location->getAddress();
|
|
|
+
|
|
|
+ $item_ids = $this->request->int("sale_item_ids");
|
|
|
+ if(empty($item_ids) || !is_array($item_ids)) {
|
|
|
+ throw new RuntimeException(__("No items selected."));
|
|
|
+ }
|
|
|
+
|
|
|
+ $sale_items = $this->validateItemIds($item_ids);
|
|
|
+
|
|
|
+ $address_string = $location->name . "\n" . $address;
|
|
|
+
|
|
|
+ // OK
|
|
|
+ $invoice = new Document();
|
|
|
+ $invoice->user_id = $this->user->id;
|
|
|
+ $invoice->sale_id = $sale->id;
|
|
|
+ $invoice->location_id = $location->id;
|
|
|
+ $invoice->type = DocumentTypes::INVOICE;
|
|
|
+ $invoice->invoice_status = InvoiceStatuses::DRAFT;
|
|
|
+ $invoice->currency = $sale->currency;
|
|
|
+ $invoice->tax_rate = $sale->tax_rate;
|
|
|
+ $invoice->billing_address = $address_string;
|
|
|
+ $invoice->delivery_address = $address_string;
|
|
|
+ $invoice->number = Document::getNextNumber(DocumentTypes::INVOICE);
|
|
|
+ $invoice->date = date("Y-m-d");
|
|
|
+ $invoice->dts_created = Database::dts();
|
|
|
+ $invoice->commit();
|
|
|
+
|
|
|
+ foreach($sale_items as $sale_item) {
|
|
|
+ $product = $sale_item->getProduct();
|
|
|
+ $supplier = $product->getSupplier();
|
|
|
+
|
|
|
+ $invoice_item = new DocumentItem();
|
|
|
+ $invoice_item->document_id = $invoice->id;
|
|
|
+ $invoice_item->description = $product->name . " ($supplier->name)";
|
|
|
+ $invoice_item->quantity = $sale_item->quantity;
|
|
|
+ $invoice_item->unit_price = $sale_item->unit_price;
|
|
|
+ $invoice_item->dts_created = Database::dts();
|
|
|
+ $invoice_item->commit();
|
|
|
+ }
|
|
|
+
|
|
|
+ Session::addAlert("success", __("Invoice created successfully."));
|
|
|
+ Http::redirect($sale->getUrl() . "/invoices/" . $invoice->number);
|
|
|
+ }
|
|
|
+
|
|
|
+};
|
|
|
+
|
|
|
+switch($action) {
|
|
|
+ case "submit-note":
|
|
|
+ $controller->submitNote();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case "delete-note":
|
|
|
+ $controller->deleteNote();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case "submit-item":
|
|
|
+ $controller->submitItem();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case "delete-item":
|
|
|
+ $controller->deleteItem();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case "submit":
|
|
|
+ $controller->createOrUpdate();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case "items-batch-action":
|
|
|
+ $batch_action = $request->string("batch_action");
|
|
|
+ switch($batch_action) {
|
|
|
+ case "create-invoice":
|
|
|
+ $controller->createInvoiceFromItems();
|
|
|
+ break;
|
|
|
+
|
|
|
+ case "delete":
|
|
|
+ $controller->deleteItems();
|
|
|
+ break;
|
|
|
+
|
|
|
+ default:
|
|
|
+ throw new RuntimeException(__("Invalid batch action."));
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ default:
|
|
|
+ $controller->view();
|
|
|
+ break;
|
|
|
+}
|