this is items + tax + ship - payments - return items - return tax + refunds. if items amount is 0 then shipping is not counted.
public $payments_amount; // note this includes sc + gc
public $refunds_amount; // note this includes sc + gc
public $pending_payments_amount;
public $pending_refunds_amount;
public $transactions;
function __Construct($order_id)
{
$this->id = $order_id;
// initialize amounts
$this->items_amount = 0;
$this->items_amount_shipped = 0;
$this->items_amount_returned = 0;
$this->tax_amount = 0;
$this->ship_amount = 0;
$this->original_total_amount = 0;
$this->total_amount = 0;
$this->balance = 0;
$this->payments_amount = 0;
$this->refunds_amount = 0;
$this->pending_payments_amount = 0;
$this->pending_refunds_amount = 0;
}
function get_data()
{
$this->data = Database::ado_get_row("SELECT * FROM orders WHERE id='".$this->id."'", "Order::get_data");
}
function get_amounts()
{
if(!isset($this->data))
{
$this->get_data();
}
if(!isset($this->items))
{
$this->get_items(); // sets items_amount, tax_amount
}
$this->ship_amount = $this->data['ship_amount'];
$this->original_total_amount = $this->original_items_amount + $this->original_tax_amount + $this->ship_amount;
$this->total_amount = $this->items_amount + $this->tax_amount;
if($this->data['order_status'] != 'canceled')
{
$this->total_amount += $this->ship_amount;
}
if(!isset($this->transactions))
{
$this->get_transactions(); // sets $this->payments_amount, $this->refunds_amount
}
$this->set_balance();
}
function set_balance()
{
//items + tax + ship - payments - return items - return tax + refunds
//order amount (items amount incl tax + shipping) - payment amounts - return amounts + refunds.
$this->balance = $this->items_amount + $this->tax_amount;
if($this->data['order_status'] != 'canceled')
{
$this->balance += $this->ship_amount;
}
$this->balance -= $this->payments_amount;
$this->balance += $this->refunds_amount;
}
function get_items()
{
// amounts issue: web and store work differently for returns - store custs return things that we can't find orig recpts for so almost all (some exceptions early on) returns are put on a new order. this sets order_items.item_action to 'return' and order_item_status.order_item_status to 'ret'. however, for the web we just set the status of orig item record to 'ret' and the item_action remains as 'purchase'. this must be accounted for. the rule is that the status field takes precedence.
$this->items = array(); // order_items record data plus an array of order_item_status data (1 status value for each qty)
$RS = Database::ado_execute_query("SELECT a.*, b.style_number FROM ".Config::$db_tables['order_items']." a, ".Config::$db_tables['inventory_items']." b WHERE a.item_id=b.id AND order_id=$this->id ORDER BY id", "Order::get_items");
while($row = $RS->FetchRow())
{
$this->items[$row['id']] = $row;
// get status(es)
$this->items[$row['id']]['status'] = array();
$sel = "SELECT * FROM ".Config::$db_tables['order_item_status']." WHERE order_item_id = '".$row['id']."' ORDER BY id";
//echo $sel;
$StatusRS = Database::ado_execute_query($sel, "Order::get_items");
// important to have a status record for each item (ie 2 records for qty 2)
// consider making this a cron job - part of a db inconsistencies job
if($StatusRS->RecordCount() != $row['item_qty'])
{
Utilities::notify(Config::$tech_email, 'Incorrect # of Order Item Status Records', "Order # ".$this->id." \nOrder Item #: ".$row['id']);
}
while($status_row = $StatusRS->FetchRow())
{
$this->items[$row['id']]['status'][] = $status_row;
if($status_row['order_item_status'] != 'cxc' && $status_row['order_item_status'] != 'cxs')
{
if($status_row['order_item_status'] == 'ret')
{
if($row['item_action'] == 'return') // mainly store returns but some web ones - see order 1867
{
$this->original_items_amount -= $row['item_price'];
$this->items_amount -= $row['item_price'];
$this->original_tax_amount -= $row['item_tax'];
$this->tax_amount -= $row['item_tax'];
}
else
{
// this was originally a purchased item
$this->original_items_amount += $row['item_price'];
$this->original_tax_amount += $row['item_tax'];
}
$this->items_amount_returned += $row['item_price'];
}
else
{
$this->original_items_amount += $row['item_price'];
$this->items_amount += $row['item_price'];
$this->original_tax_amount += $row['item_tax'];
$this->tax_amount += $row['item_tax'];
if($status_row['order_item_status'] == 'shp' || $status_row['order_item_status'] == 'snc')
{
$this->items_amount_shipped += $row['item_price'];
}
}
}
else
{
// a cancelled item
$this->original_items_amount += $row['item_price'];
$this->original_tax_amount += $row['item_tax'];
}
}
}
}
function get_ois_data($ois_id)
{
return(Database::ado_get_row("SELECT * FROM ".Config::$db_tables['order_item_status']." WHERE id='$ois_id'", "OrderAdmin::get_ois_data"));
}
function get_ois_RS($oi_id)
{
// items will have multiple ois records if qty > 1
return(Database::ado_execute_query("SELECT * FROM ".Config::$db_tables['order_item_status']." WHERE order_item_id='$oi_id'", "OrderAdmin::get_ois_RS"));
}
function items_html($type='cust', $Forms='')
{
if(!isset($this->items))
{
$this->get_items();
}
$html = '
| Thank you for your purchase! |
 |
| Your order will be shipped as soon as possible.
Please print this page for your records. If you have any questions, you can reach us at orders@'.Config::$domain.' or '.Config::$phone.'. |
 |
';
if($type == 'cust')
{
$this->receipt_html .= '
';
if(round($this->balance) != 0 && ($this->data['order_type'] == 'store' || $type == 'admin') )
{
$html .= '
| Current Balance: $'.number_format($this->balance, 2).' |
';
}
if($this->data['order_type'] == 'store')
{
$html .= '
| Payments: |
| Date |
Method |
Amount |
Balance |
';
}
else
{
if($type == 'admin') // no row for cust - just 'Paid by x4111 in transactions'
{
$html .= '
| Transactions: |
';
}
}
foreach($this->transactions as $otr_id => $tr_info)
{
$display_date = Utilities::informal_date(substr($tr_info['trans_dt'],0,10));
if($this->data['order_type'] == 'store')
{
$method_cell = ucwords($tr_info['method']);
if($tr_info['trans_type'] == 'refund')
{
$method_cell .= ' REFUND';
if($tr_info['status'] == 'pending')
{
$method_cell .= ' PENDING';
}
else
{
$display_balance += $tr_info['amount'];
}
}
else
{
if($tr_info['status'] == 'pending')
{
$method_cell .= ' PENDING';
}
else
{
$display_balance -= $tr_info['amount'];
}
}
$html .= '
| '.$display_date.' |
'.$method_cell.' |
$'.number_format($tr_info['amount'],2).' |
$'.number_format($display_balance,2).' |
';
}
else // web order
{
if($type == 'cust' && $tr_info['method'] == 'credit card')
{
$html .= '
| Paid by credit card ending in '.substr($tr_info['cc_number'], strlen($tr_info['cc_number'])-4).' |
';
}
elseif($type == 'admin')
{
//print_r($tr_info);
if($tr_info['method'] == 'credit card')
{
$method_cell =
$tr_info['cc_number'].'
Exp (mmyy): '.$tr_info['cc_exp'].'
CVV2: '.$tr_info['cc_security_code'].'
Name on Card: '.$tr_info['cc_name'].'
Address: '.$tr_info['cc_street'].'
Zip Code: '.$tr_info['cc_postal'].'
trans id: '.$tr_info['gateway_trans_id'];
if($tr_info['gateway_ref_trans_id'] != '')
{
$method_cell .= '
ref trans id: '.$tr_info['gateway_ref_trans_id'];
}
$status_cell = $tr_info['status_cctr'];
switch($tr_info['status_cctr'])
{
case 'AUTH':
$actions_cell = '
';
break;
case 'CAPT':
$status_cell .= '
'.$tr_info['capture_date'];
$actions_cell = '
';
break;
default:
$actions_cell = ' ';
}
if($_SESSION['level'] != 'owner' && $_SESSION['level'] != 'admin')
{
$actions_cell = ' ';
}
}
else
{
$method_cell = ' ';
$status_cell = ' ';
$actions_cell = ' ';
}
$html .= '
| '.$display_date.': |
'.ucwords($tr_info['trans_type']).' |
$'.number_format($tr_info['amount'],2).' |
'.$method_cell.' |
'.$status_cell.' |
'.$actions_cell.' |
';
}
}
}
if($this->data['order_type'] == 'web' && $type == 'admin' && ($_SESSION['level'] == 'owner' || $_SESSION['level'] == 'admin') )
{
// new transaction form - for a new cc charge
$html .= '
|
';
}
$html .= ' |
';
return($html);
}
function amounts_html()
{
$html = '