1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060 |
- <?php
- namespace Ppb\Db\Table\Row;
- use Ppb\Db\Table\SalesListings,
- Ppb\Db\Table\Vouchers as VouchersTable,
- Cube\Db\Expr,
- Ppb\Service;
- class Sale extends AbstractRow
- {
-
- const PAYMENT_UNPAID = 0;
- const PAYMENT_PAID = 1;
- const PAYMENT_PAID_DIRECT_PAYMENT = 2;
- const PAYMENT_PAY_ARRIVAL = 3;
-
- const SHIPPING_PROCESSING = 0;
- const SHIPPING_SENT = 1;
- const SHIPPING_PROBLEM = 2;
- const SHIPPING_NA = -1;
-
- public static $paymentStatuses = array(
- self::PAYMENT_UNPAID => 'Unpaid',
- self::PAYMENT_PAID => 'Paid',
- self::PAYMENT_PAID_DIRECT_PAYMENT => 'Paid (Direct Payment)',
- self::PAYMENT_PAY_ARRIVAL => 'Payment on Arrival',
- );
-
- public static $shippingStatuses = array(
- self::SHIPPING_PROCESSING => 'Processing',
- self::SHIPPING_SENT => 'Posted/Sent',
- self::SHIPPING_PROBLEM => 'Problem',
- self::SHIPPING_NA => 'N/A',
- );
-
- protected static $saleDataKeys = array(
- 'currency',
- 'country',
- 'state',
- 'address',
- 'pickup_options'
- );
-
- protected $_salesListings = array();
-
- protected $_sales;
-
- protected $_serializable = array('sale_data');
-
- protected $_taxAmount = false;
-
- protected $_voucher = false;
-
- public function getSalesListings()
- {
- if (empty($this->_salesListings)) {
- $this->setSalesListings(
- $this->findDependentRowset('\Ppb\Db\Table\SalesListings'));
- }
- return $this->_salesListings;
- }
-
- public function setSalesListings($salesListings)
- {
- $this->clearSalesListings();
- foreach ($salesListings as $saleListing) {
- $this->addSaleListing($saleListing);
- }
- return $this;
- }
-
- public function clearSalesListings()
- {
- $this->_salesListings = array();
- return $this;
- }
-
- public function addSaleListing($saleListing)
- {
- if ($saleListing instanceof SaleListing) {
- $this->_salesListings[] = $saleListing;
- }
- else {
- if (!array_key_exists('price', $saleListing) ||
- !array_key_exists('quantity', $saleListing)
- ) {
- throw new \InvalidArgumentException("The sale listing array must include the 'price' and 'quantity' keys");
- }
- $this->_salesListings[] = new SaleListing(array(
- 'data' => $saleListing,
- 'table' => new SalesListings(),
- ));
- }
- return $this;
- }
-
- public function setTaxAmount($taxAmount)
- {
- $this->_taxAmount = floatval($taxAmount);
- return $this;
- }
-
- public function getTaxAmount($force = false)
- {
- if ($this->_taxAmount === false || $force === true) {
- $this->calculateTotal();
- }
- return $this->_taxAmount;
- }
-
- public function setVoucher($voucher)
- {
- $this->_voucher = $voucher;
- }
-
- public function getVoucher()
- {
- if ($this->_voucher === false) {
- if (($data = \Ppb\Utility::unserialize($this->getData('voucher_details'))) !== false) {
- $voucher = new Voucher(array(
- 'table' => new VouchersTable(),
- 'data' => $data,
- ));
- }
- else {
- $voucher = null;
- }
- $this->setVoucher($voucher);
- }
- return $this->_voucher;
- }
-
- public function calculateTotal($simple = false, $applyVoucher = true)
- {
- $result = 0;
- $taxAmount = null;
- $salesListings = $this->getSalesListings();
- $applyVoucher = ($this->getData('pending') && $applyVoucher) ? true : false;
- foreach ($salesListings as $saleListing) {
-
- $result += $saleListing->calculateTotal($applyVoucher);
- }
- if ($simple === false) {
- if ($this->hasPostage()) {
- $result += $this->getData('postage_amount');
- if ($this->getInsuranceAmount()) {
- $result += $this->getData('insurance_amount');
- }
- }
- if ($this->getData('tax_rate') > 0) {
- $taxAmount = $result * $this->getData('tax_rate') / 100;
- $result += $taxAmount;
- }
- }
- $this->setTaxAmount($taxAmount);
- return $result;
- }
-
- public function isActive($expiresAt = true)
- {
- $active = ($this->getData('active') && !$this->getData('pending')) ? true : false;
- return ($expiresAt && $this->getData('expires_at')) ? false : $active;
- }
-
- public function isPaid()
- {
- return ($this->getData('flag_payment') == self::PAYMENT_UNPAID) ? false : true;
- }
-
- public function canEdit()
- {
- if (!count($this)) {
- return false;
- }
- $canEdit = (
- $this->isActive() &&
- !$this->isPaid() &&
- $this->getData('buyer_id') &&
- $this->getData('seller_id')
- ) ? true : false;
- $user = $this->getUser();
- if ($user['id'] === $this->getData('buyer_id') && $this->getData('edit_locked')) {
- $canEdit = false;
- }
- return $canEdit;
- }
-
- public function canDelete()
- {
- if (!count($this)) {
- return false;
- }
- $canDelete = (
- !$this->getData('expires_at')
- ) ? true : false;
- return $canDelete;
- }
-
- public function canCombinePurchases()
- {
- if (!$this->getData('voucher_details')) {
- $settings = $this->getSettings();
- if ($this->isSeller() || $settings['buyer_create_invoices']) {
- return true;
- }
- }
- return false;
- }
-
- public function isSeller($admin = false)
- {
- $user = $this->getUser();
- $result = false;
- if ($this->getData('seller_id') == $user['id']) {
- $result = true;
- }
- else if ($admin == true && $user['role'] == 'Admin') {
- $result = true;
- }
- return $result;
- }
-
- public function isBuyer()
- {
- $user = $this->getUser();
- if ($this->getData('buyer_id') == $user['id']) {
- return true;
- }
- return false;
- }
-
- public function getPickupOptions()
- {
-
- $salesListings = $this->findDependentRowset('\Ppb\Db\Table\SalesListings');
-
- foreach ($salesListings as $saleListing) {
-
- $listing = $saleListing->findParentRow('\Ppb\Db\Table\Listings');
- return $listing[\Ppb\Model\Shipping::FLD_PICKUP_OPTIONS];
- }
- return null;
- }
-
- public function getPaymentMethods($type = null)
- {
- $paymentMethods = null;
-
- $salesListings = $this->findDependentRowset('\Ppb\Db\Table\SalesListings');
-
- foreach ($salesListings as $saleListing) {
-
- $listing = $saleListing->findParentRow('\Ppb\Db\Table\Listings');
- if ($listing instanceof Listing) {
- $listingPaymentMethods = $listing->getPaymentMethods($type);
- if ($paymentMethods === null) {
- $paymentMethods = $listingPaymentMethods;
- }
- else {
- $paymentMethods = array_uintersect($paymentMethods, $listingPaymentMethods,
- function ($a, $b) {
- return strcmp($a['name'] . $a['type'], $b['name'] . $b['type']);
- });
- }
- }
- }
- return $paymentMethods;
- }
-
- public function canPayDirectPayment()
- {
- $user = $this->getUser();
- if ($this->isActive(false) &&
- $user['id'] == $this->getData('buyer_id') &&
- !$this->isPaid()
- ) {
- $paymentMethods = $this->getPaymentMethods('direct');
- if (count($paymentMethods) > 0) {
- return $paymentMethods;
- }
- }
- return false;
- }
-
- public function setExpiresFlag($reset = false)
- {
- $settings = $this->getSettings();
- $salesListings = $this->findDependentRowset('\Ppb\Db\Table\SalesListings');
-
- foreach ($salesListings as $saleListing) {
-
- $listing = $saleListing->findParentRow('\Ppb\Db\Table\Listings');
- if ($listing->getData('listing_type') != 'product') {
- $reset = true;
- break;
- }
- }
- if ($reset) {
- $flag = new Expr('null');
- }
- else {
- $flag = new Expr('now() + interval ' . $settings['force_payment_limit'] . ' minute');
- }
- $this->save(array(
- 'expires_at' => $flag,
- ));
- $this->_data['expires_at'] = ($reset) ? null : date('Y-m-d H:i:s', time() + $settings['force_payment_limit'] * 60);
- return $this;
- }
-
- public function getInsuranceAmount()
- {
- if ($this->getData('apply_insurance')) {
- return $this->getData('insurance_amount');
- }
- return null;
- }
-
- public function hasPostage()
- {
- if ($this->getData('enable_shipping')) {
- return true;
- }
- if ($this->getData('pending')) {
- $salesListings = $this->getSalesListings();
-
- foreach ($salesListings as $saleListing) {
-
- $listing = $saleListing->findParentRow('\Ppb\Db\Table\Listings');
- if (!$listing->isShipping()) {
- return false;
- }
- }
- return true;
- }
- return false;
- }
-
- public function getPostageMethod()
- {
- if (isset($this->_data['postage']['method'])) {
- return $this->_data['postage']['method'];
- }
- return 'N/A';
- }
-
- public function getPaymentStatusDescription()
- {
- if (array_key_exists($this->_data['flag_payment'], self::$paymentStatuses)) {
- return self::$paymentStatuses[$this->_data['flag_payment']];
- }
- return 'N/A';
- }
-
- public function getSaleTransaction()
- {
- $select = $this->getTable()->select()
- ->where('paid = ?', 1);
- $transaction = $this->findDependentRowset('\Ppb\Db\Table\Transactions', null, $select)->getRow(0);
- return $transaction;
- }
-
- public function canView()
- {
- if (!count($this) || !$this->isActive()) {
- return false;
- }
- $user = $this->getUser();
- if (in_array($user['id'], array($this->getData('seller_id'), $this->getData('buyer_id')))) {
- return true;
- }
- return false;
- }
-
- public function messagingEnabled()
- {
- $settings = $this->getSettings();
- if ($settings['enable_messaging']) {
- return true;
- }
- return false;
- }
-
- public function messagingLink()
- {
- $action = ($this->getData('messaging_topic_id')) ? 'topic' : 'create';
- $array = array(
- 'module' => 'members',
- 'controller' => 'messaging',
- 'action' => $action,
- );
- if ($action == 'topic') {
- $array['id'] = $this->getData('messaging_topic_id');
- }
- else {
- $array['sale_id'] = $this->getData('id');
- $array['topic_type'] = Service\Messaging::SALE_TRANSACTION;
- }
- return $array;
- }
-
- public function reputationLink($posterId)
- {
- $array = array();
- $reputationService = new Service\Reputation();
- $salesListings = $this->findDependentRowset('\Ppb\Db\Table\SalesListings');
- $salesListingsIds = array(0);
- foreach ($salesListings as $saleListing) {
- $salesListingsIds[] = $saleListing['id'];
- }
- $select = $reputationService->getTable()->select()
- ->where('poster_id = ?', $posterId)
- ->where('posted = ?', 0)
- ->where('sale_listing_id IN (?)', $salesListingsIds);
- $reputations = $reputationService->fetchAll($select);
- $ids = array();
- foreach ($reputations as $reputation) {
- $ids[] = $reputation['id'];
- }
- if (count($ids) > 0) {
- $array = array(
- 'module' => 'members',
- 'controller' => 'reputation',
- 'action' => 'post',
- 'id' => $ids,
- );
- }
- return $array;
- }
-
- public function canCheckout()
- {
- $translate = $this->getTranslate();
- $user = $this->getUser();
- $userId = (!empty($user['id'])) ? $user['id'] : null;
- if (!$this->getData('pending')) {
- return sprintf($translate->_('Invalid shopping cart selected.'));
- }
- else if ($userId == $this->getData('seller_id')) {
- return sprintf($translate->_('You cannot purchase your own products.'));
- }
- return true;
- }
-
- public function canPayFee()
- {
- if ($this->isActive(false)) {
- return false;
- }
- $settings = $this->getSettings();
- $user = $this->getUser();
- switch ($settings['sale_fee_payer']) {
- case 'buyer':
- if ($user['id'] == $this->getData('buyer_id')) {
- return true;
- }
- break;
- case 'seller':
- if ($user['id'] == $this->getData('seller_id')) {
- return true;
- }
- break;
- }
- return false;
- }
-
- public function updateActive($active = 1)
- {
- $this->save(array(
- 'active' => (int)$active,
- ));
- return $this;
- }
-
- public function updateStatus($post)
- {
- $saleData = (array)$this->getData('sale_data');
- $saleData['tracking_link'] = $post['tracking_link'];
- $this->saveSaleData(array(
- 'tracking_link' => $post['tracking_link'],
- ));
- $data = array(
- 'flag_shipping' => $post['flag_shipping'],
- 'flag_payment' => $post['flag_payment'],
- );
- if ($post['flag_payment'] != self::PAYMENT_UNPAID) {
- $this->setExpiresFlag(true);
- }
- parent::save($data);
- return $this;
- }
-
- public function saveSaleData($post)
- {
- $saleData = array_merge((array)\Ppb\Utility::unserialize($this->getData('sale_data')), $post);
- foreach (self::$saleDataKeys as $key) {
- if (!array_key_exists($key, $saleData)) {
- $saleData[$key] = null;
- }
- }
- $salesListings = $this->findDependentRowset('\Ppb\Db\Table\SalesListings');
- $enableShipping = false;
- foreach ($salesListings as $saleListing) {
-
- $listing = $saleListing->findParentRow('\Ppb\Db\Table\Listings');
- if ($listing->isShipping()) {
- $enableShipping = true;
- break;
- }
- }
- $saleData['enable_shipping'] = $enableShipping;
- $data = array(
- 'sale_data' => serialize($saleData),
- );
- parent::save($data);
- return $this;
- }
-
- public function saveVoucherDetails($voucher)
- {
- if (!$voucher instanceof Voucher) {
- $vouchersService = new Service\Vouchers();
- $voucher = $vouchersService->findBy($voucher, $this->getData('seller_id'));
- }
- if ($voucher instanceof Voucher) {
- $voucher = ($voucher->isValid()) ? serialize($voucher->getData()) : null;
- }
- $this->save(array(
- 'voucher_details' => $voucher
- ));
- return $this;
- }
-
- public function updateQuantities(array $quantities)
- {
- $salesListings = $this->getSalesListings();
-
- foreach ($salesListings as $saleListing) {
- $key = $saleListing['id'];
- if (array_key_exists($key, $quantities)) {
- $quantity = ($quantities[$key] > 1) ? $quantities[$key] : 1;
- $saleListing->save(array(
- 'quantity' => $quantity,
- ));
- }
- }
- return $this;
- }
-
- public function revert()
- {
-
- $salesListings = $this->getSalesListings();
-
- foreach ($salesListings as $saleListing) {
-
- $listing = $saleListing->findParentRow('\Ppb\Db\Table\Listings');
- if ($listing instanceof Listing) {
- $listing->updateQuantity($saleListing->getData('quantity'), $saleListing['product_attributes'], Listing::ADD);
- }
-
- $saleListing->findDependentRowset('\Ppb\Db\Table\Reputation')->delete();
- }
-
- $accountingRowset = $this->findDependentRowset('\Ppb\Db\Table\Accounting');
-
- foreach ($accountingRowset as $accounting) {
- $accounting->acceptRefundRequest(true);
- }
- $this->delete(true);
- return $this;
- }
-
- public function delete($admin = false)
- {
- if ($admin === true) {
- return parent::delete();
- }
- $user = $this->getUser();
- if ($user['id'] == $this->getData('seller_id')) {
- $this->save(array(
- 'seller_deleted' => 1
- ));
- return true;
- }
- else if ($user['id'] == $this->getData('buyer_id')) {
- $this->save(array(
- 'buyer_deleted' => 1
- ));
- return true;
- }
- return false;
- }
- }
|