Skip to content

Commit

Permalink
extend invoice api (#88)
Browse files Browse the repository at this point in the history
  • Loading branch information
Memurame authored Apr 5, 2024
1 parent 44decfd commit 59c610c
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 7 deletions.
8 changes: 4 additions & 4 deletions app/Config/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,10 +508,10 @@
);

$routes->delete("invoice/(:num)", 'apiInvoiceController::delete/$1');
$routes->delete(
"invoice/position/(:num)",
'apiInvoicePosController::delete/$1',
);
$routes->get("invoice/(:num)", 'apiInvoiceController::show/$1');


$routes->delete("invoice/position/(:num)",'apiInvoicePosController::delete/$1');
$routes->post(
"invoice/position/(:num)/copy",
'apiInvoicePosController::saveAsTemplate/$1'
Expand Down
29 changes: 29 additions & 0 deletions app/Controllers/apiInvoiceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,35 @@ class apiInvoiceController extends BaseController
{
use ResponseTrait;


public function show(int $invoiceId = null){

$invoice = model('InvoiceModel')
->find($invoiceId);

if(!$invoice){
return $this->failNotFound("No invoice found");
}

$customer = model('CustomerModel')->find($invoice->customer_id);
$positions = model('InvoicePositionModel')
->where('invoice_id', $invoice->id)
->orderBy('ord', 'asc')
->findAll();

$return = $invoice->prepareForReturn();
$return['customer'] = $customer->prepareForReturn();
if($positions){
foreach($positions as $pos){
$return['positions'][] = $pos->prepareForReturn();
}
}else{
$return['positions'] = [];
}

return $this->respond($return, 200);
}

public function delete($invoiceId = null){
$invoice = model('InvoiceModel')->find($invoiceId);

Expand Down
6 changes: 3 additions & 3 deletions app/Controllers/apiInvoicePosController.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ public function moveUp($invoicePosId){
$invoicePos = model('InvoicePositionModel')->find($invoicePosId);
$invoicePosList = model('InvoicePositionModel')
->where('invoice_id', $invoicePos->invoice_id)
->where('ord <', $invoicePos->ord)
->where('ord <=', $invoicePos->ord)
->orderBy('ord', 'desc')
->findAll();

foreach($invoicePosList as $pos){
if($pos->ord < $invoicePos->ord){
$prevPos = $pos->ord;
Expand All @@ -135,7 +135,7 @@ public function moveDown($invoicePosId){
$invoicePos = model('InvoicePositionModel')->find($invoicePosId);
$invoicePosList = model('InvoicePositionModel')
->where('invoice_id', $invoicePos->invoice_id)
->where('ord >', $invoicePos->ord)
->where('ord >=', $invoicePos->ord)
->orderBy('ord')
->findAll();

Expand Down
24 changes: 24 additions & 0 deletions app/Entities/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,28 @@ public function getBillingAddress(){

return $this->address;
}

/* ########################################################
* Ab hier sind Funktionen für die API
#########################################################*/
public function prepareForReturn(){
$r = model('InvoiceModel')
->find($this->id);

$return = [];
$return['id'] = $r->id;
$return['date_invoice'] = $r->invoice;
$return['paid'] = $r->paid;
$return['renew'] = $r->renew;
$return['intervall'] = $r->renew_intervall;
$return['renewed'] = $r->renewed;
$return['description'] = $r->description;
$return['note']['top'] = $r->notes_top;
$return['note']['bottom'] = $r->notes_bottom;
$return['contact']['name'] = $r->contact_name;
$return['contact']['phone'] = $r->contact_phone;
$return['contact']['mail'] = $r->contact_mail;

return $return ?? null;
}
}
23 changes: 23 additions & 0 deletions app/Entities/InvoicePosition.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,27 @@ public function getPositionUnitprice($inkl = true){

return round($total,2);
}

/* ########################################################
* Ab hier sind Funktionen für die API
#########################################################*/
public function prepareForReturn(){
$r = model('InvoicePositionModel')
->find($this->id);

$return = [];
$return['id'] = $r->id;
$return['position'] = $r->position;
$return['title'] = $r->title;
$return['description'] = $r->description;
$return['price'] = $r->price;
$return['price_inkl'] = $r->price_inkl;
$return['amount'] = $r->amount;
$return['mwst'] = $r->mwst;
$return['notes'] = $r->notes;
$return['type'] = $r->type;
$return['order'] = $r->ord;

return $return ?? null;
}
}

0 comments on commit 59c610c

Please sign in to comment.