Sindbad~EG File Manager
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Promo_lib {
public function __construct()
{
$this->ci = &get_instance();
$this->ci->load->model('promo');
}
public function checkPromo($item_id) {
$e = $this->checkTebusMurah();
if ($e) return 0;
$e = $this->watchProduct($item_id);
$e = $this->watchTebusMurah($item_id);
if ($e) return 0;
$promos = $this->ci->promo->getPromo($item_id);
if (!isset($promos[0])) return 0;
foreach($promos as $promo) {
switch ($promo->promo_type) {
case 'Purchase With Purchase' :
// add watch
$this->addOrUpdateWatchProduct($item_id, $promo);
break;
case 'Gift With Purchase':
// print('Gift');
$this->addCart($item_id, $promo);
break;
case 'Diskon':
if ($promo->promo_get_persentase_discount) {
$value = $promo->promo_get_persentase_discount;
$type = 0;
} else {
$value = $promo->promo_get_price_discount;
$type = 1;
}
$this->updateCart($item_id, [
'discount' => $value,
'discount_type' => $type,
]);
break;
}
}
// if ($e) return 0;
}
public function checkTebusMurah()
{
$promos = $this->ci->promo->checkTebusMurah();
$tebus_murah = $this->ci->session->userdata('tebus_murah');
$tebus_murah_sess = [];
if(!is_array($tebus_murah)) $tebus_murah = [];
else $tebus_murah_sess = $tebus_murah;
// print('<pre>');print_r($promos);print('</pre>');
// die;
foreach($promos as $promo) {
$tebus_murah_items = json_decode($promo->promo_tebus_murah_items);
foreach($tebus_murah_items as $item_id) {
$key = array_search($item_id, array_column($tebus_murah, 'tebus_murah_item_id'));
if ($key === false ) {
$tebus_murah_sess[] = [
'tebus_murah_item_id' => $item_id,
'tebus_murah_min_buy' => $promo->condition_min_purchase,
'tebus_murah_price' => $promo->promo_tebus_murah_price,
];
}
}
}
$this->ci->session->set_userdata(['tebus_murah' => $tebus_murah_sess]);
return 0;
}
public function watchTebusMurah($item_id)
{
// get subtotal
$cart = $this->ci->session->userdata('sales_cart');
$subtotal = 0;
if (is_array($cart)) {
foreach($cart as $item) {
$subtotal += $item['discounted_total'];
}
}
// print('Sub Total: ' . $subtotal);
$tebus_murah = $this->ci->session->userdata('tebus_murah');
if (is_array($tebus_murah)) {
$key = array_search($item_id, array_column($tebus_murah, 'tebus_murah_item_id'));
if($key !== false) {
$min = $tebus_murah[$key]['tebus_murah_min_buy'];
$price = $tebus_murah[$key]['tebus_murah_price'];
if($subtotal >= $min) {
$this->updateCart($item_id, [
'discount' => 0,
'discount_type' => 0,
'price' => $price,
]);
}
}
}
return 0;
}
public function addCart($item_id, $promo)
{
$cart = $this->ci->session->userdata('sales_cart');
if (is_array($cart)) {
$key = array_search($promo->promo_get_gift_product_id, array_column($cart, 'item_id'));
if ($key === false) {
// get info item
$this->ci->load->model('item');
$info = $this->ci->item->get_info($promo->promo_get_gift_product_id);
// print('<pre>');print_r($info);print('</pre>');
// get last key
$lastkey = array_key_last($cart);
$key = $lastkey + 1;
// $keys = array_keys($cart);
// $key = $keys[$key];
// print('key ' . $key . '=> ' . $item_id );
$cart[$key]['price'] = 0;
$cart[$key]['quantity'] = 1;
$cart[$key]['total'] = 0 ;
$cart[$key]['discounted_total'] = 0;
$cart[$key] = [
'item_id' => $info->item_id,
'item_location' => 2,
'stock_name' => '',
'line' => $key,
'name' => $info->name,
'item_number' => $info->item_number,
'attribute_values' => '',
'attribute_dtvalues' => '',
'description' => $info->description,
'serialnumber' => '',
'allow_alt_description' => 0,
'is_serialized' => 0,
'quantity' => 1,
'discount' => 0,
'discount_type' => 0,
'in_stock' => 1,
'price' => 0,
'cost_price' => 0,
'total' => 0,
'discounted_total' => 0,
'print_option' => 0,
'stock_type' => $info->stock_type,
'item_type' => 0,
'hsn_code' => $info->hsn_code,
'tax_category_id' => 0,
];
// update new cart
$this->ci->session->set_userdata(['sales_cart' => $cart]);
} else {
$keys = array_keys($cart);
$key = $keys[$key];
$cart[$key]['quantity'] += 1;
// update new cart
$this->ci->session->set_userdata(['sales_cart' => $cart]);
}
} else {
print('No Cart');
}
}
public function removePromo($line)
{
$cart = $this->ci->session->userdata('sales_cart');
if ($cart !== null) {
$item_id = $cart[$line]['item_id'];
}
// TODO
// find promo in watch product, if exists remove promo discount from register
// and remove it on watch product
$discounted_item_id = -1;
$watch = $this->ci->session->userdata('watch_product');
if (isset($watch[0])) {
$key = array_search($item_id, array_column($watch, 'item_id'));
$discounted_item_id = $watch[$key]['discounted_item_id'];
if ($key !== false) {
unset($watch[$key]);
$this->ci->session->set_userdata(['watch_product' => $watch]);
}
}
$this->updateCart($discounted_item_id, [
'discount' => 0,
'discount_type' => 0
]);
}
public function debug($s=1)
{
if ($s) {
print('<pre>');
print_r($this->ci->session->userdata());
print('</pre>');
}
}
protected function watchProduct($item_id) {
$watch = $this->ci->session->userdata('watch_product');
if (isset($watch[0])) {
$key = array_search($item_id, array_column($watch, 'discounted_item_id'));
if ($key !== false) {
$value = $watch[$key]['value'];
$discount_type = $watch[$key]['discount_type'];
$this->updateCart($item_id, [
'discount' => $value,
'discount_type' => $discount_type,
]);
return 1;
}
}
return 0;
}
protected function addOrUpdateWatchProduct($item_id, $promo)
{
$watch = $this->ci->session->userdata('watch_product');
if(!is_array($watch)) $watch = [];
$key = $key = array_search($item_id, array_column($watch, 'item_id'));
if($key === false) {
$watch_this_item = [];
if (isset($watch[0])) $watch_this_item = $watch;
if ($promo->promo_get_price_discount) {
$value = $promo->promo_get_price_discount;
$discount_type = 1;
} else {
$value = $promo->promo_get_persentase_discount;
$discount_type = 0;
}
$watch_this = [
'discounted_item_id' => $promo->promo_product_get_discount,
'item_id' => $item_id,
'discount_type' => $discount_type,
'value' => $value,
'tebus_murah' => $promo->promo_tebus_murah_price,
];
$watch_this_item[] = $watch_this;
$this->ci->session->set_userdata(['watch_product' => $watch_this_item]);
// check discounted product is exists
$this->updateCart($promo->promo_product_get_discount, [
'discount' => $value,
'discount_type' => $discount_type,
]);
}
// } else {
// $key = array_search($item_id, array_column($watch, 'item_id'));
// if ($key !== false) {
// // update watch product
// }
// }
}
protected function addWatchProduct($item_id, $promo)
{
$watch = $this->ci->session->userdata('watch_product');
if (is_array($watch)) {
}
}
protected function updateWatchProduct($item_id)
{
$watch = $this->ci->session->userdata('watch_product');
$key = array_search($item_id, array_column($watch, 'promo_item_id'));
}
protected function updateCart($item_id, $data = [])
{
$cart = $this->ci->session->userdata('sales_cart');
if (is_array($cart)) {
$key = array_search($item_id, array_column($cart, 'item_id'));
if ($key !== false) {
$keys = array_keys($cart);
$key = $keys[$key];
// print('key ' . $key . '=> ' . $item_id );
foreach($data as $k => $v) {
$cart[$key][$k] = $v;
}
$price = $cart[$key]['price'];
$quantity = $cart[$key]['quantity'];
$item_price = $price * $quantity;
$total_persentase = $item_price * $data['discount']/100;
$total_price = $data['discount'];
$discount = ($data['discount_type']) ? $total_price : $total_persentase ;
$cart[$key]['total'] = $item_price ;
$cart[$key]['discounted_total'] = $item_price - $discount;
// update new cart
$this->ci->session->set_userdata(['sales_cart' => $cart]);
}
}
}
public function totalSales()
{
// check total harga keseluruhan
$cart = $this->ci->session->userdata('sales_cart');
$total = 0;
foreach($cart as $item) {
$total += $item['price'];
}
return $total;
}
public function _checkTebusMurah()
{
// check total harga keseluruhan
$cart = $this->ci->session->userdata('sales_cart');
$total = 0;
foreach($cart as $item) {
$total += $item['price'];
}
// print($total);
// reset tebus murah
$tebusmurahsess = $this->ci->session->userdata('tebus_murah');
$tebus_murah = $this->ci->promo->checkTebusMurah($total);
//print_r($tebus_murah);
// $tebusmurahsess = is_array($tebusmurahsess) ? $tebusmurahsess : [];
$data['tebus_murah'] = [];
if (count($tebus_murah) > 0) {
foreach($tebus_murah as $tbs_murah) {
$data['tebus_murah'][] = [
'product_id' => $tbs_murah->promo_product_get_discount,
'price' => $tbs_murah->promo_tebus_murah_price
];
}
$this->ci->session->set_userdata($data);
$this->ci->session->set_userdata(['tebus_murah_on' => 1]);
} else {
$this->ci->session->set_userdata(['tebus_murah' => []]);
$this->ci->session->set_userdata(['tebus_murah_on' => 0]);
}
}
public function checkItem($item_id) {
if( $this->ci->session->userdata('tebus_murah_on') !== null) {
if ($this->ci->session->userdata('tebus_murah_on')) {
$tebus_murah = $this->ci->session->userdata('tebus_murah') !== null ? $this->ci->session->userdata('tebus_murah') : [];
$pkey = array_search($item_id, array_column($tebus_murah, 'product_id'));
if ($key) {
$cart = $this->ci->session->userdata('sales_cart');
$key = array_search($item_id, array_column($cart, 'item_id'));
// print('key ' . $key . '=> ' . $item_id );
$keys = array_keys($cart);
$key = $keys[$key];
$price = $cart[$key]['price'];
$quantity = $cart[$key]['quantity'];
$tebus_murah_price = $tebus_murah[$pkey]['price'];
$price_sub = $price - $tebus_murah_price;
$promo_diskon = $this->ci->session->userdata('promo_diskon');
$dpromo = [];
if ( is_array($promo_diskon) ) {
$dpromo = $promo_diskon;
}
$dkey = array_search($item_id, array_column($promo_diskon, 'item_id'));
if ( $dkey === false ) {
$dpromo [] = [
'total_price_before' => $price,
'item_id' => $item_id,
'price' => $price_sub
];
}
}
}
}
}
public function checkDiscountMember()
{
$customer_id = $this->ci->session->userdata('sales_customer');
if ($customer_id !== null) {
$discount = $this->ci->promo->checkMemberDiscount($customer_id);
if ($this->ci->promo->num_rows > 0) {
$pdiscount = $this->ci->session->userdata('promo_diskon');
$promo_discount = 0;
if (is_array($pdiscount)) {
foreach($pdiscount as $disc) {
$promo_discount+= $disc['price'];
}
}
$total_sales = $this->totalSales() - $promo_discount;
if ($discount->promo_get_persentase_discount) {
$sub = $total_sales * $discount->promo_get_persentase_discount/100;
} else {
$sub = $total_sales - $discount->promo_get_price_discount;
}
// return $sub;
$member_diskon_data[] = [
'total_price_before' => 0,
'item_id' => -1,
'price' => $sub,
];
$promo['member_diskon'] = $member_diskon_data;
$this->ci->session->set_userdata($promo);
}
}
return 0;
}
public function updatePromo($line)
{
$cart = $this->ci->session->userdata('sales_cart');
if (is_array($cart)) {
$item_id = $cart[$line]['item_id'];
$this->checkPromo($item_id);
}
}
}
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists