dcodegroup/laravel-xero-leave

此包提供了向Xero提交请假申请的标准Xero功能

0.1.7 2023-12-11 05:04 UTC

This package is auto-updated.

Last update: 2024-09-09 00:48:31 UTC


README

此包提供了将请假申请发送到Xero的标准xero功能。

此包提供

  • 请假表格
  • 依赖于dcodegroup/laravel-xero-employee
  • 审批事件
  • 审批或否的配置
  • 请假类型配置
  • 更新设置的命令。
  • 重新同步按钮

工作原理

按照安装说明进行操作。如果您想审批请求,请选择设置。以下是触发的事件

事件

  • 请假申请创建
  • 需要审批的请假
  • 请假已批准 - 触发发送到Xero的事件监听器

安装

您可以通过composer安装此包

composer require dcodegroup/laravel-xero-leave

然后运行安装命令。

php artsian laravel-xero-leave:install

这将发布配置文件和迁移。

然后运行迁移

php artsian migrate

以下表将被添加到您的项目中

leaves
---
id bigint(20) PK IDENTITY
leavable_type varchar(255)
leaveable_id unsignedbigint
xero_leave_application_id varchar(50) NULL # The identifier returned from xero
xero_employee_id varchar(50) NULL # may be redundant becuase its on the user that should be the polymporphic field. But saves a lookup
xero_leave_type_id varchar(50) # The identifier returned from xero stored in the configuration
start_date date
end_date date
units double(8,2) # in case the duration is less than a day
title varchar(50)
description varchar(200) NULL
xero_periods json NULL
xero_exception_message text NULL
approved_at timestamp NULL
declined_at timestamp NULL
decline_reason text NULL
xero_synced_at timestamp NULL
deleted_at timestamp NULL
created_at timestamp NULL
updated_at timestamp NULL

配置

大多数配置都已设置为合理的默认值。但是,您可以审查位于config/laravel-xero-leave.php的配置文件并根据需要进行调整

用法

您应该为该包实现自己的前端。但是,模型和事件等已经为您准备好了。以下是您需要执行的步骤。

创建自己的Leave::class模型类,该类扩展了包的模型。

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Dcodegroup\LaravelXeroLeave\Models\Leave as BaseLeave;

class Leave extends BaseLeave
{
    use HasFactory;
    ...

确保将以下特质添加到您将使用的模型中。例如 App\Models\User::class

class User extends Authenticatable 
{
    use HasLeave;
    ...

示例Vue组件,用于更新需要审批的请假状态

<template>
 <td>
  <menu>
   <ul>
    <li>
     <button class="button primary active">
      {{ currentStatus }}
     </button>
     <ul class="right">
      <li>
       <a @click="submit('approve')">
        {{ $t('leave.status.approve') }}
       </a>
      </li>
      <li>
       <a @click="showReason = true;">
        {{ $t('leave.status.decline') }}
       </a>
       <div v-show="showReason">
        <input
                v-model="declineReason"
                type="text"
                :placeholder="$t('leave.words.reason')"
        >
        <button @click="submit('decline')">
         {{ $t('leave.buttons.save') }}
        </button>
       </div>
      </li>
      <li>
       <a @click="submit('pending')">
        {{ $t('leave.status.pending') }}
       </a>
      </li>
     </ul>
    </li>
   </ul>
  </menu>

  <div v-show="hasSyncError">
   <header class="alert danger">
    <div>
     <span>&#8505;</span>
     <small>{{ rowData.xero_exception_message }}</small>
    </div>
    <button>&#9747;</button>
   </header>
   <button
           class="button right"
           @click="retrySync"
   >
    <icon
            name="xero-red"
            :width="50"
            :height="50"
    />
   </button>
  </div>
 </td>
</template>

<script>
export default {
 name: "UpdateLeaveStatus",

 props: {
  rowData: {
   type: Object,
   required: true
  },
  rowIndex: {
   type: Number
  },
  rowField: {
   type: [String, Object]
  },
 },

 data() {
  return {
   currentStatus: this.rowData.status,
   hasSyncError: this.rowData.has_xero_sync_error,
   showReason: false,
   declineReason: '',
  }
 },

 methods: {
  submit(value) {
   // this.showReason = false;

   axios.patch(this.rowData.update_status_url, {
    action: value,
    reason: this.declineReason,
   }).then(({data}) => {
    this.currentStatus = data.status;
   }).catch((errors) => {
    if ('reason' in errors.response.data.errors) {
     this.$notify({
      group: 'general',
      title: 'Error!',
      type: 'error',
      text: errors.response.data.errors.reason[0]
     })
    }

    console.error(errors);
   });
  },
  retrySync() {
   axios.get(this.rowData.retry_sync_url).then(({data}) => {
    this.hasSyncError = false;
   }).catch((errors) => {
    console.error(errors);
   });
  }
 }
}
</script>

<style scoped>

</style>

路由

以下列出了此包内可用的路由。建议您检查使用的中间件并相应地更新配置。

xero_leave.update-status 在需要审批时更新用于发送到Xero的状态。 xero_leave.retry-sync 是一个端点,可以在发送请假请求到Xero时出现问题时使用。

+--------+----------+----------------------------------+--------------------------+---------------------------------------------------------------------+----------------------------------+
| Domain | Method   | URI                              | Name                     | Action                                                              | Middleware                       |
+--------+----------+----------------------------------+--------------------------+---------------------------------------------------------------------+----------------------------------+
|        | GET|HEAD | xero-leave/retry-sync/{leave}    | xero_leave.retry-sync    | Dcodegroup\LaravelXeroLeave\Http\Controllers\RetrySyncController    | web                              |
|        |          |                                  |                          |                                                                     | App\Http\Middleware\Authenticate |
|        | PATCH    | xero-leave/update-status/{leave} | xero_leave.update-status | Dcodegroup\LaravelXeroLeave\Http\Controllers\UpdateStatusController | web                              |
|        |          |                                  |                          |                                                                     | App\Http\Middleware\Authenticate |
+--------+----------+----------------------------------+--------------------------+---------------------------------------------------------------------+----------------------------------+

HTTP资源

当返回Leave的数据时,您很可能会使用资源。您应该在请假资源中添加一些元素。下面是示例。

<?php

namespace App\Http\Resources\Admin;

use Dcodegroup\LaravelConfiguration\Models\Configuration;
use Illuminate\Http\Resources\Json\JsonResource;

class Leave extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     *
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        $data = parent::toArray($request);

       
        $data['status'] = $this->status;
        $data['user'] = optional($this->leaveable)->preferred_name ?? optional($this->leaveable)->name;
        $data['leave_type'] = data_get(Configuration::byKey('xero_leave_types')->pluck('value')->flatten(1)->where('LeaveTypeID', $this->xero_leave_type_id)->first(), 'Name');
        $data['update_status_url'] = route('xero_leave.update-status', $this);
        $data['has_xero_sync_error'] = (bool) $this->hasSyncError();
        
        /**
         *  Optional
         */
        $data['start_date'] = $this->start_date->format(config('gradcon.date_format'));
        $data['end_date'] = $this->end_date->format(config('gradcon.date_format'));
        
        ...

事件

与Xero通信是通过触发事件来完成的。这些事件有监听器,它们会触发监听器并调度作业。

所有事件都接受Leave::class作为唯一参数。

SendLeaveToXero::class 将请假记录发送到Xero。这有一个监听器,然后调度作业SendToXero LeaveApproved::class 当请假变为批准时,将触发此事件。这将触发将请假发送到Xero LeaveDeclined::class 当请假变为未批准时,将触发此事件。这将触发将请假发送到Xero(通常是当前申请的更新) RequestLeaveApproval::class 如果配置参数laravel-xero-leave.applications_require_approval为true,并且创建了新的请假请求,则将触发此事件。您可以在自己的应用程序中监听它,触发通知或电子邮件,告知谁需要采取行动。

此包中已存在请求类。您可以在自己的控制器中更新并使用下面的代码来更新store和update类。

use App\Http\Controllers\Controller;
use Dcodegroup\LaravelXeroLeave\BaseXeroLeaveService;
use Dcodegroup\LaravelXeroLeave\Http\Requests\StoreLeave;

class LeaveController extends Controller
{
    protected BaseXeroLeaveService $service;

    public function __construct()
    {
        $this->service = resolve(BaseXeroLeaveService::class);
    }

    public function store(StoreLeave $request)
    {
        $this->authorize('create', Leave::class);

        $this->service->save($request);
        
        ...        
    }

    public function update(StoreLeave $request, Model $leave)
    {
        $this->authorize('update', $leave);

        $this->service->save($request, $leave);
        
        ....
    }

    ...

工作

命令

laravel-xero-leave:install 发布配置文件和迁移文件

laravel-xero-leave:update-xero-configuration-data 将从数据库中检索并存储请假类型。您可以使用 `--force` 参数确保立即执行。

您应该将其添加到 app/Console/Kernel.php 文件中,以便每天运行一次。如果需要,可以使用 --force 标志更频繁地运行。

    /**
     * Define the application's command schedule.
     *
     * @param Schedule $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('laravel-xero-leave:update-xero-configuration-data')->daily();    
        ...
    }

资源

通过 Xero API 没有删除请假申请的能力。更多信息请参阅这里