1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-17 06:52:27 +01:00

confirm check cashing api

This commit is contained in:
Sylvain 2021-02-04 17:51:16 +01:00
parent d54846c349
commit b5cdea513d
8 changed files with 61 additions and 25 deletions

View File

@ -4,6 +4,7 @@
class API::PaymentSchedulesController < API::ApiController
before_action :authenticate_user!
before_action :set_payment_schedule, only: %i[download]
before_action :set_payment_schedule_item, only: %i[cash_check]
def list
authorize PaymentSchedule
@ -25,9 +26,22 @@ class API::PaymentSchedulesController < API::ApiController
send_file File.join(Rails.root, @payment_schedule.file), type: 'application/pdf', disposition: 'attachment'
end
def cash_check
schedule = @payment_schedule_item.payment_schedule
authorize schedule
PaymentScheduleService.new.generate_invoice(@payment_schedule_item)
@payment_schedule_item.update_attributes(state: 'paid', payment_method: 'check')
render :show, status: :ok, location: schedule
end
private
def set_payment_schedule
@payment_schedule = PaymentSchedule.find(params[:id])
end
def set_payment_schedule_item
@payment_schedule_item = PaymentScheduleItem.find(params[:id])
end
end

View File

@ -53,6 +53,16 @@ const PaymentSchedulesList: React.FC = () => {
});
}
/**
* Reload from te API all the currently displayed payment schedules
*/
const handleRefreshList = (): void => {
const api = new PaymentScheduleAPI();
api.list({ query: { reference: referenceFilter, customer: customerFilter, date: dateFilter, page: 1, size: PAGE_SIZE * pageNumber }}).then((res) => {
setPaymentSchedules(res);
});
}
/**
* Check if the current collection of payment schedules is empty or not.
*/
@ -78,7 +88,7 @@ const PaymentSchedulesList: React.FC = () => {
</div>
{!hasSchedules() && <div>{t('app.admin.invoices.payment_schedules.no_payment_schedules')}</div>}
{hasSchedules() && <div className="schedules-list">
<PaymentSchedulesTable paymentSchedules={paymentSchedules} showCustomer={true} />
<PaymentSchedulesTable paymentSchedules={paymentSchedules} showCustomer={true} refreshList={handleRefreshList} />
{hasMoreSchedules() && <FabButton className="load-more" onClick={handleLoadMore}>{t('app.admin.invoices.payment_schedules.load_more')}</FabButton>}
</div>}
</div>

View File

@ -17,10 +17,11 @@ declare var Fablab: IFablab;
interface PaymentSchedulesTableProps {
paymentSchedules: Array<PaymentSchedule>,
showCustomer?: boolean
showCustomer?: boolean,
refreshList: () => void
}
const PaymentSchedulesTableComponent: React.FC<PaymentSchedulesTableProps> = ({ paymentSchedules, showCustomer }) => {
const PaymentSchedulesTableComponent: React.FC<PaymentSchedulesTableProps> = ({ paymentSchedules, showCustomer, refreshList }) => {
const { t } = useTranslation('admin');
const [showExpanded, setShowExpanded] = useState<Map<number, boolean>>(new Map());
@ -157,10 +158,10 @@ const PaymentSchedulesTableComponent: React.FC<PaymentSchedulesTableProps> = ({
const onCheckCashingConfirmed = (): void => {
const api = new PaymentScheduleAPI();
api.cashCheck(tempDeadline.id).then(res => {
// TODO refresh display
api.cashCheck(tempDeadline.id).then(() => {
refreshList();
toggleConfirmCashingModal();
});
// TODO create /api/payment_schedule/item/confirm_check endpoint and post to it
}
/**
@ -287,10 +288,10 @@ const PaymentSchedulesTableComponent: React.FC<PaymentSchedulesTableProps> = ({
PaymentSchedulesTableComponent.defaultProps = { showCustomer: false };
export const PaymentSchedulesTable: React.FC<PaymentSchedulesTableProps> = ({ paymentSchedules, showCustomer }) => {
export const PaymentSchedulesTable: React.FC<PaymentSchedulesTableProps> = ({ paymentSchedules, showCustomer, refreshList }) => {
return (
<Loader>
<PaymentSchedulesTableComponent paymentSchedules={paymentSchedules} showCustomer={showCustomer} />
<PaymentSchedulesTableComponent paymentSchedules={paymentSchedules} showCustomer={showCustomer} refreshList={refreshList} />
</Loader>
);
}

View File

@ -6,6 +6,10 @@ class PaymentSchedulePolicy < ApplicationPolicy
user.admin? || user.manager?
end
def cash_check?
user.admin? || user.manager?
end
def download?
user.admin? || user.manager? || (record.invoicing_profile.user_id == user.id)
end

View File

@ -0,0 +1,19 @@
# frozen_string_literal: true
json.extract! payment_schedule, :id, :reference, :created_at, :payment_method
json.total payment_schedule.total / 100.00
json.chained_footprint payment_schedule.check_footprint
json.user do
json.name payment_schedule.invoicing_profile.full_name
end
if payment_schedule.operator_profile
json.operator do
json.id payment_schedule.operator_profile.user_id
json.extract! payment_schedule.operator_profile, :first_name, :last_name
end
end
json.items payment_schedule.payment_schedule_items do |item|
json.extract! item, :id, :due_date, :state, :invoice_id, :payment_method
json.amount item.amount / 100.00
json.client_secret item.payment_intent.client_secret if item.stp_invoice_id && item.state == 'requires_action'
end

View File

@ -4,21 +4,5 @@ max_schedules = @payment_schedules.except(:offset, :limit, :order).count
json.array! @payment_schedules do |ps|
json.max_length max_schedules
json.extract! ps, :id, :reference, :created_at, :payment_method
json.total ps.total / 100.00
json.chained_footprint ps.check_footprint
json.user do
json.name ps.invoicing_profile.full_name
end
if ps.operator_profile
json.operator do
json.id ps.operator_profile.user_id
json.extract! ps.operator_profile, :first_name, :last_name
end
end
json.items ps.payment_schedule_items do |item|
json.extract! item, :id, :due_date, :state, :invoice_id, :payment_method
json.amount item.amount / 100.00
json.client_secret item.payment_intent.client_secret if item.stp_invoice_id && item.state == 'requires_action'
end
json.partial! 'api/payment_schedules/payment_schedule', payment_schedule: ps
end

View File

@ -0,0 +1,3 @@
# frozen_string_literal: true
json.partial! 'api/payment_schedules/payment_schedule', payment_schedule: @payment_schedule_item.payment_schedule

View File

@ -114,6 +114,7 @@ Rails.application.routes.draw do
resources :payment_schedules, only: %i[show] do
post 'list', action: 'list', on: :collection
get 'download', on: :member
post 'items/:id/cash_check', action: 'cash_check', on: :collection
end
resources :i_calendar, only: %i[index create destroy] do