-
-
Save bizstation/21e7a638eac5b822dd779d3730566d15 to your computer and use it in GitHub Desktop.
Example with Transactd PHP ORM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once(__DIR__ . "/vendor/autoload.php"); | |
use BizStation\Transactd\Transactd; | |
use BizStation\Transactd\Database; | |
use BizStation\Transactd\Tabledef; | |
use BizStation\Transactd\Benchmark; | |
use BizStation\Transactd\Nstable; | |
use Transactd\QueryExecuter; | |
use Transactd\IOException; | |
use Transactd\Model; | |
use Transactd\Collection; | |
use Transactd\JsonSerializable; | |
class_alias('Transactd\DatabaseManager', 'DB'); | |
CONST masterUri = 'tdap://localhost/salesdb'; | |
CONST slaveUri = 'tdap://localhost/salesdb'; | |
class Customer extends Model | |
{ | |
protected static $guarded = ['id']; | |
public $id = 0; | |
public function invoices() | |
{ | |
return $this->hasMany('Invoice'); | |
} | |
public function transactions($startDate, $endDate) | |
{ | |
return Invoice::index(1)->keyValue($this->id, $startDate) | |
->where('customer_id', $this->id)->where('date', '<=', $endDate)->get(); | |
} | |
} | |
class Product extends Model | |
{ | |
protected static $guarded = ['id']; | |
public function stock() | |
{ | |
return $this->hasOne('Stock', 0, 'code'); | |
} | |
} | |
class Stock extends Model | |
{ | |
protected static $guarded = []; | |
public function product() | |
{ | |
return $this->belongsTo('Product', 'code', 1); | |
} | |
} | |
Trait AmountTrait | |
{ | |
public $sales = 0; | |
public $tax = 0; | |
public $payment = 0; | |
public $balance = 0; | |
public function reset() | |
{ | |
$this->sales = 0; | |
$this->tax = 0; | |
$this->payment = 0; | |
} | |
public function sum($amount) | |
{ | |
$this->sales += $amount->sales; | |
$this->tax += $amount->tax; | |
$this->payment += $amount->payment; | |
} | |
public function total() | |
{ | |
return $this->sales + $this->tax - $this->payment; | |
} | |
} | |
class InvoiceAmount | |
{ | |
use AmountTrait; | |
use JsonSerializable; | |
public function __construct() | |
{ | |
$this->className = get_class($this); | |
} | |
} | |
class Invoice extends Model | |
{ | |
public static $aliases = ['sales_amount' => 'sales', 'tax_amount' => 'tax', 'payment_amount' => 'payment', 'balance_amount' => 'balance']; | |
public static $transferMap = ['sales' => 'amount', 'tax' => 'amount', 'payment' => 'amount', 'balance' => 'amount']; | |
public static $guarded = ['id']; | |
public $id = 0; | |
public $amount = null; | |
public $date; | |
public function __construct() | |
{ | |
parent::__construct(); | |
$this->amount = new InvoiceAmount; | |
$this->date = date("Y/m/d"); | |
} | |
public function items() | |
{ | |
return $this->hasMany('InvoiceItem'); | |
} | |
public function customer() | |
{ | |
return $this->belongsTo('Customer'); | |
} | |
public function addSalesLine($code, $qty) | |
{ | |
$prod = Product::index(1)->findOrFail($code); | |
$item = new InvoiceItem; | |
$item->assignSales($prod, $qty); | |
$this->items->add($item); | |
return $item; | |
} | |
public function addPaymentLine($amount, $desc) | |
{ | |
$item = new InvoiceItem; | |
$item->assignPayment($amount, $desc); | |
$this->items->add($item); | |
return $item; | |
} | |
private function prepareTables() | |
{ | |
Stock::prepareTable(); | |
DailySummary::prepareTable(); | |
InvoiceItem::prepareTable(); | |
} | |
public function saveWithTransaction() | |
{ | |
try { | |
$this->prepareTables(); | |
DB::beginTrn(Transactd::MULTILOCK_GAP); | |
$this->save(); | |
DB::commit(); | |
} catch (Exception $e) { | |
DB::rollBack(); | |
throw $e; | |
} | |
} | |
public function deleteWithTransaction() | |
{ | |
try { | |
$this->prepareTables(); | |
DB::beginTrn(Transactd::MULTILOCK_GAP); | |
$this->delete(); | |
DB::commit(); | |
} catch (Exception $e) { | |
DB::rollBack(); | |
throw $e; | |
} | |
} | |
} | |
class InvoiceItem extends Model | |
{ | |
const SALES = 0; | |
const PAYMENT = 1; | |
const TAX_RATE = 0.08; | |
protected static $table = 'invoice_items'; | |
protected static $guarded = []; | |
public function invoice() | |
{ | |
return $this->belongsTo('Invoice'); | |
} | |
public function stock() | |
{ | |
return $this->hasOne('Stock', 0, 'product_code'); | |
} | |
public function product() | |
{ | |
return $this->hasOne('Product', 1, 'product_code'); | |
} | |
public function assignSales($prod, $qty) | |
{ | |
$this->product_code = $prod->code; | |
$this->product_description = $prod->description; | |
$this->line_type = self::SALES; | |
$this->price = $prod->price; | |
$this->quantity = $qty; | |
$this->amount = $prod->price * $qty; | |
$this->tax = (int)(($this->amount * self::TAX_RATE) + 0.5); | |
} | |
public function assignPayment($amount, $desc) | |
{ | |
$this->product_code = 'PAYMENT'; | |
$this->product_description = $desc; | |
$this->line_type = self::PAYMENT; | |
$this->amount = $amount; | |
} | |
} | |
class DailySummary extends Model | |
{ | |
use AmountTrait; | |
protected static $guarded = []; | |
protected static $table = 'daily_summaries'; | |
static protected $aliases = ['slaes_amount' => 'sales', 'tax_amount' => 'tax', 'payment_amount' => 'payment']; | |
public function invoices() | |
{ | |
return $this->hasMany('Invoice', 2, 'date'); | |
} | |
} | |
// Please `connect` at the beginning of the application. | |
// DB::connect(URI, URI); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment