first(); } public function all(FilterAllAttr $transactionAllAttr) { $expense = Expense::with('expense_account', 'payment_account', 'tax', 'supplier', 'customer', 'project'); if ($fromDate = $transactionAllAttr->getFromDate() and $toDate = $transactionAllAttr->getToDate()) { $expense = $expense->whereBetween('expense_date', [formatDate($fromDate), formatDate($toDate)]); } return $expense->get(); } public function sumAllTaxRate(ReportCoaFilterAttr $reportCoaFilterAttr) { $expense = lessAndBetweenDateQuery( Expense::query(), $reportCoaFilterAttr->getLessDate(), $reportCoaFilterAttr->getBetweenTwoDates(), 'expense_date' ); return $expense->sumAllTaxRate($reportCoaFilterAttr)->first()->all_tax_rate; } public function getRandomId($limit = 10) { $expense = Expense::limit($limit)->inRandomOrder(); return $expense->first()->id ?? null; } public function paginate(PaginateBuilderAttr $paginateBuilderAttr): PaginateCollectionAttr { $query = Expense::with('expense_account', 'payment_account', 'service_center', 'supplier', 'customer', 'project'); if (request()->for == 'external') { $query->whereNotnull('project_id'); } else { $query->whereNull('project_id'); } return (new PaginateCollectionAttrBuilder) ->build( $query, $paginateBuilderAttr, new PaginateSchemaExpense ); } public function fetch($id) { return Expense::with('tax', 'taxDeducated', 'expense_account', 'payment_account', 'supplier', 'service_center', 'customer', 'project.deal')->findOrFail($id); } public function create(ExpenseAttr $expenseAttr, $noExpense) { $expense = Expense::create([ 'no_expense' => $noExpense, 'expense_date' => formatDate($expenseAttr->getExpenseDate()), 'expense_account_id' => $expenseAttr->getExpenseAccountId(), 'amount' => numberOnly($expenseAttr->getAmount()), 'payment_account_id' => $expenseAttr->getPaymentAccountId(), 'tax_id' => $expenseAttr->getTaxId(), 'tax_rate' => $expenseAttr->getTaxRate(), 'supplier_id' => $expenseAttr->getSupplierId(), 'service_center_id' => $expenseAttr->getServiceCenterId(), 'customer_id' => $expenseAttr->getCustomerId(), 'notes' => $expenseAttr->getNotes(), 'type' => $expenseAttr->getType(), 'project_id' => $expenseAttr->getProjectId(), 'project_value' => $expenseAttr->getProjectValue(), 'rap_value' => $expenseAttr->getRapValue(), 'paid_amount' => $expenseAttr->getPaidAmount(), 'subcon_id' => $expenseAttr->getSubconId(), 'image' => $expenseAttr->getImage() ]); return $this->fetch($expense->id); } public function update(ExpenseAttr $expenseAttr, $id) { $expense = $this->fetch($id); $expense->update([ 'expense_date' => formatDate($expenseAttr->getExpenseDate()), 'expense_account_id' => $expenseAttr->getExpenseAccountId(), 'amount' => numberOnly($expenseAttr->getAmount()), 'payment_account_id' => $expenseAttr->getPaymentAccountId(), 'tax_id' => $expenseAttr->getTaxId(), 'tax_rate' => $expenseAttr->getTaxRate(), 'supplier_id' => $expenseAttr->getSupplierId(), 'service_center_id' => $expenseAttr->getServiceCenterId(), 'customer_id' => $expenseAttr->getCustomerId(), 'notes' => $expenseAttr->getNotes(), 'type' => $expenseAttr->getType(), 'project_id' => $expenseAttr->getProjectId(), 'project_value' => $expenseAttr->getProjectValue(), 'rap_value' => $expenseAttr->getRapValue(), 'paid_amount' => $expenseAttr->getPaidAmount(), 'subcon_id' => $expenseAttr->getSubconId(), 'image' => $expenseAttr->getImage() ]); return $this->fetch($id); } public function delete($id) { $expense = $this->fetch($id); $expense->delete(); return $expense; } }