resourceMap(); if (! isset($map[$resource])) { return $this->error('Resource not found', 404); } $def = $map[$resource]; if (isset($def['v1'])) { return $this->{$def['v1']}($request); } if (isset($def['txn_sub'])) { return $this->transactionsBySubType($request, $def['txn_sub']); } if (isset($def['txn'])) { return $this->transactions($request, $def['txn'], $def['where'] ?? []); } return $this->tableList($request, $def['table'], $def['where'] ?? [], $def['order'] ?? 'id'); } protected function expenseCategories(Request $request) { return $this->expenseController->listExpenseCategories(); } protected function transactionsBySubType(Request $request, string $sub_type) { $business_id = $this->contextService->getBusinessId($request); $query = Transaction::where('business_id', $business_id) ->where('sub_type', $sub_type) ->orderByDesc('transaction_date'); return $this->paginated($query->paginate($request->input('per_page', 25))); } protected function transactions(Request $request, string $type, array $where = []) { $business_id = $this->contextService->getBusinessId($request); $query = Transaction::where('business_id', $business_id) ->where('type', $type) ->orderByDesc('transaction_date'); foreach ($where as $col => $val) { $query->where($col, $val); } return $this->paginated($query->paginate($request->input('per_page', 25))); } protected function tableList(Request $request, string $table, array $where = [], string $order = 'id') { if (! Schema::hasTable($table)) { return $this->success([]); } $business_id = $this->contextService->getBusinessId($request); $query = DB::table($table)->where('business_id', $business_id); foreach ($where as $col => $val) { $query->where($col, $val); } if (Schema::hasColumn($table, $order)) { $query->orderByDesc($order); } $items = $query->limit($request->input('per_page', 100))->get(); return $this->success($items); } protected function resourceMap(): array { return [ 'commission-agents' => ['table' => 'users', 'where' => ['is_cmmsn_agnt' => 1]], 'customer-groups' => ['table' => 'customer_groups'], 'warranties' => ['table' => 'warranties'], 'variation-templates' => ['table' => 'variation_templates'], 'selling-price-group' => ['table' => 'selling_price_groups'], 'purchase-requisition' => ['txn' => 'purchase_requisition'], 'purchase-order' => ['txn' => 'purchase_order'], 'purchase-return' => ['txn' => 'purchase_return'], 'sales-order' => ['txn' => 'sales_order'], 'sell-return' => ['txn' => 'sell_return'], 'shipments' => ['txn' => 'sell'], 'discounts' => ['table' => 'discounts'], 'expense-categories' => ['v1' => 'expenseCategories'], 'invoice-schemes' => ['table' => 'invoice_schemes'], 'invoice-layouts' => ['table' => 'invoice_layouts'], 'printers' => ['table' => 'printers'], 'barcodes' => ['table' => 'barcodes'], 'crm-leads' => ['table' => 'crm_leads'], 'crm-followups' => ['table' => 'crm_schedules'], 'crm-campaigns' => ['table' => 'crm_campaigns'], 'crm-proposals' => ['table' => 'crm_proposals'], 'attendance' => ['table' => 'essentials_attendances'], 'leave' => ['table' => 'essentials_leaves'], 'payroll' => ['table' => 'essentials_payrolls'], 'holidays' => ['table' => 'essentials_holidays'], 'departments' => ['table' => 'categories', 'where' => ['category_type' => 'hrm_department']], 'designations' => ['table' => 'categories', 'where' => ['category_type' => 'hrm_designation']], 'todo' => ['table' => 'essentials_todos'], 'mfg-recipes' => ['table' => 'mfg_recipes'], 'mfg-production' => ['txn_sub' => 'production_purchase'], 'repair' => ['txn_sub' => 'repair'], 'repair-status' => ['table' => 'repair_statuses'], 'repair-models' => ['table' => 'repair_device_models'], 'projects' => ['table' => 'pjt_projects'], 'tasks' => ['table' => 'pjt_project_tasks'], 'time-logs' => ['table' => 'pjt_time_logs'], 'tables' => ['table' => 'res_tables'], 'bookings' => ['table' => 'bookings'], 'modifiers' => ['table' => 'res_product_modifier_sets'], 'accounting' => ['table' => 'accounts'], 'journals' => ['table' => 'accounting_acc_trans_mappings'], 'transfers' => ['table' => 'accounting_acc_trans_mappings'], 'budget' => ['table' => 'accounting_budgets'], 'assets' => ['table' => 'assets'], 'inventory' => ['table' => 'inventory_counts'], 'spreadsheets' => ['table' => 'sheet_spreadsheets'], 'packages' => ['table' => 'packages'], 'subscriptions' => ['table' => 'subscriptions'], 'accounts' => ['table' => 'accounts'], 'pos' => ['txn' => 'sell', 'where' => ['is_direct_sale' => 0]], ]; } }