public function getTotalChargesNDiscount($service_type, $service_id, $customer_id)
 {
     $discount = CustomerDiscount::model()->findByAttributes(array('service_type' => $service_type, 'service_id' => $service_id, 'customer_id' => $customer_id));
     if (!$discount instanceof CustomerDiscount) {
         return array($this->charges, 0);
     } else {
         return array($this->charges - $this->charges * $discount->discount / 100, $discount->discount);
     }
 }
 protected function renderDataCellContent($row, $data)
 {
     $model = CustomerDiscount::model()->findByAttributes(array('customer_id' => $this->customer_id, 'service_id' => $data->id));
     if (!$model instanceof CustomerDiscount) {
         $model = new CustomerDiscount();
     }
     $options = $this->checkBoxHtmlOptions;
     $options['name'] = str_replace('CustomerDiscount', 'CustomerDiscount[' . $row . ']', CHtml::activeName($model, $this->forAttribute));
     $options['class'] = $this->id;
     echo CHtml::activeCheckBox($model, $this->forAttribute, $options);
 }
 public function getCurrentDiscountAttribute()
 {
     $customer_discount = CustomerDiscount::where('customer_id', $this->membership_id)->join('discounts', 'discounts.id', '=', 'discounts_customers.discount_id')->get();
     if (count($customer_discount) > 0) {
         foreach ($customer_discount as $c) {
             $dt = Carbon::parse($c->expiration);
             if ($c->expiration->diffInDays() > 0) {
                 return $c;
             }
         }
     }
     //return $customer_discount;
 }
Beispiel #4
0
<h4 class="ui-box-header ui-corner-all">
	Discount List for customer <?php 
echo $customer->name;
?>
</h4>
<br />
<?php 
$form = $this->beginWidget('CActiveForm', array('id' => 'customer-discount--form', 'enableAjaxValidation' => false, 'enableClientValidation' => true, 'clientOptions' => array('validateOnSubmit' => true)));
?>
<div class="grid">
	<?php 
$this->widget('zii.widgets.grid.CGridView', array('id' => 'discount-grid', 'filter' => $services, 'dataProvider' => $services->search(), 'htmlOptions' => array('class' => 'hastable'), 'columns' => array(array('header' => 'Product Service Code', 'type' => 'raw', 'value' => '$data->productService->code', 'name' => 'search_code'), array('header' => 'Carrier', 'type' => 'raw', 'value' => '$data->rateCompanyService->rateCompany->name'), array('header' => 'Company Service Name', 'type' => 'raw', 'value' => '$data->rateCompanyService->name'), array('header' => 'Vendor Discount (%)', 'type' => 'raw', 'value' => function ($data, $row) use($customer) {
    return CustomerDiscount::model()->getTextField($customer->id, $data->id, $row, 'vendor_discount', $data->rateCompanyService->rateCompany->id, TRUE, 3);
}), array('header' => 'discount invoice (%)', 'type' => 'raw', 'value' => function ($data, $row) use($customer) {
    return CustomerDiscount::model()->getDiscountTextField($customer->id, $data->id, $row);
}), array('header' => 'Harga invoice', 'type' => 'raw', 'value' => function ($data, $row) use($customer) {
    return CustomerDiscount::model()->getTextField($customer->id, $data->id, $row, 'harga_invoice', $data->rateCompanyService->rateCompany->id);
}), array('header' => 'discount api (%)', 'type' => 'raw', 'value' => function ($data, $row) use($customer) {
    return CustomerDiscount::model()->getTextField($customer->id, $data->id, $row, 'discount_api', $data->rateCompanyService->rateCompany->id, TRUE, 3);
}), array('header' => 'Harga api', 'type' => 'raw', 'value' => function ($data, $row) use($customer) {
    return CustomerDiscount::model()->getTextField($customer->id, $data->id, $row, 'harga_api', $data->rateCompanyService->rateCompany->id);
}), array('header' => 'Use rate', 'class' => 'CFCheckBoxColumn', 'selectableRows' => 2, 'customer_id' => $customer->id, 'checkBoxHtmlOptions' => array('name' => 'CustomerDiscount'), 'forAttribute' => 'use_rate'), array('header' => 'Show in API', 'class' => 'CFCheckBoxColumn', 'selectableRows' => 2, 'customer_id' => $customer->id, 'checkBoxHtmlOptions' => array('name' => 'CustomerDiscount'), 'forAttribute' => 'show_in_api'))));
?>
</div>
<?php 
echo CHtml::submitButton('Save');
?>

<?php 
$this->endWidget();
 public function actionRequestOrder()
 {
     if (!isset($_POST['Shipment'])) {
         echo CJSON::encode($this->statusError('Must be in POST method'));
         Yii::app()->end();
     }
     $shipment = new Shipment('api-requestpickup');
     $shipment->attributes = $_POST['Shipment'];
     $shipment->created = time();
     $routing_code = IntraCityRouting::model()->findByAttributes(array('postcode' => $shipment->shipper_postal));
     if ($routing_code instanceof IntraCityRouting) {
         $shipment->origin_code = $routing_code->code;
     }
     $price = 0;
     $price_vendor = 0;
     $trans = Yii::app()->db->beginTransaction();
     try {
         if ($this->token instanceof Token) {
             $customer = Customer::model()->findByPk($this->token->customer_id);
             if (!$customer instanceof Customer) {
                 throw new ServiceControllerException('You have to login first');
             }
             if (!!$customer->accountnr) {
                 $shipment->awb = '70' . rand(10000000, 99999999);
                 while (!$shipment->validate()) {
                     $shipment->awb = '70' . rand(10000000, 99999999);
                 }
             }
             $shipment->customer_id = $this->token->customer_id;
         } else {
             $email = '';
             $shipment->setScenario('cekemail');
             if ($shipment->validate()) {
                 if ($shipment->payer == 'shipper' && $shipment->shipper_email) {
                     $email = Contact::model()->findByAttributes(array('email' => $shipment->shipper_email));
                 } elseif ($shipment->payer == 'consignee' && $shipment->receiver_email) {
                     $email = Contact::model()->findByAttributes(array('email' => $shipment->receiver_email));
                 }
             } else {
                 throw new ServiceControllerException($shipment->getErrors());
             }
             $shipment->setScenario('api-requestpickup');
             if (!$email instanceof Contact) {
                 $customer = new Customer();
                 if ($shipment->payer == 'shipper') {
                     $customer->name = $shipment->shipper_name;
                 } elseif ($shipment->payer == 'consignee') {
                     $customer->name = $shipment->receiver_name;
                 }
                 $customer->type = 'personal';
                 $customer->accountnr = 'WEB' . time();
                 if ($customer->save()) {
                     $contact = new Contact();
                     $contact->parent_id = $customer->id;
                     $contact->parent_model = 'Customer';
                     if ($shipment->payer == 'shipper') {
                         $contact->full_name = $shipment->shipper_name;
                         $contact->address = $shipment->shipper_address;
                         $contact->country = $shipment->shipper_country;
                         $contact->city = $shipment->shipper_city;
                         $contact->postal = $shipment->shipper_postal;
                         $contact->email = $shipment->shipper_email;
                     } elseif ($shipment->payer == 'consignee') {
                         $contact->full_name = $shipment->receiver_name;
                         $contact->address = $shipment->receiver_address;
                         $contact->country = $shipment->receiver_country;
                         $contact->city = $shipment->receiver_city;
                         $contact->postal = $shipment->receiver_postal;
                         $contact->email = $shipment->receiver_email;
                     }
                     if ($contact->save()) {
                         $shipment->customer_id = $customer->id;
                     } else {
                         throw new ServiceControllerException($contact->getErrors());
                     }
                 } else {
                     throw new ServiceControllerException($customer->getErrors());
                 }
             } else {
                 throw new ServiceControllerException('Your email is currently registered as a member, please login to create order');
             }
         }
         if ($shipment->validate()) {
             $customer_rate = CustomerDiscount::getCustomerDiscountRate($shipment->service_id, $shipment->customer_id);
             switch ($shipment->service_type) {
                 case 'City Courier':
                     $rate = RateCity::model()->findByAttributes(array('service_id' => $shipment->service_id));
                     if ($rate instanceof RateCity) {
                         if ($customer_rate['harga_invoice'] != 0) {
                             $price = $customer_rate['harga_invoice'] * RateCity::increment($shipment->package_weight, $rate->weight_inc);
                         } else {
                             $price = ($rate->price - $rate->price * ($customer_rate['discount'] / 100)) * RateCity::increment($shipment->package_weight, $rate->weight_inc);
                         }
                         $price_vendor = ($rate->price - $rate->price * ($customer_rate['vendor_discount'] / 100)) * RateCity::increment($shipment->package_weight, $rate->weight_inc);
                     } else {
                         $area = Area::getZoneID($shipment->receiver_postal);
                         if (!$area) {
                             throw new ServiceControllerException('No services available');
                         }
                         $price = RateDomestic::getRatePrice($shipment->service_id, 1, $area['district_id'], $area['zone_id'], $shipment->package_weight, $customer_rate['discount']);
                         $price_vendor = RateDomestic::getRatePrice($shipment->service_id, 1, $area['district_id'], $area['zone_id'], $shipment->package_weight, $customer_rate['vendor_discount']);
                     }
                     break;
                 case 'Domestic':
                     $area = Area::getZoneID($shipment->receiver_postal);
                     if (!$area) {
                         throw new ServiceControllerException('No services available');
                     }
                     $price = RateDomestic::getRatePrice($shipment->service_id, 1, $area['district_id'], $area['zone_id'], $shipment->package_weight, $customer_rate['discount']);
                     $price_vendor = RateDomestic::getRatePrice($shipment->service_id, 1, $area['district_id'], $area['zone_id'], $shipment->package_weight, $customer_rate['vendor_discount']);
                     break;
                 case 'International':
                     $zone = FALSE;
                     if ($shipment->service_code == 'IEX') {
                         $country = $shipment->receiver_country;
                     } else {
                         if ($shipment->service_code == 'IMX') {
                             $country = $shipment->shipper_country;
                         }
                     }
                     $zone = ZoneInternational::getZoneCountryData($country);
                     if (!!$zone) {
                         $price = RateInternational::getRatePrice($shipment->service_id, $shipment->package_weight, $shipment->type, $zone, $customer_rate['discount']);
                         $price_vendor = RateInternational::getRatePrice($shipment->service_id, $shipment->package_weight, $shipment->type, $zone, $customer_rate['vendor_discount']);
                     }
                     break;
                 case '':
                     continue;
             }
             $shipment->shipping_charges = $price;
             $shipment->vendor_charge = $price_vendor;
             if (!$shipment->save()) {
                 throw new ServiceControllerException($shipment->getErrors());
             }
             $sum_add_cost = ShipmentAdditionalCharge::initSurcharges($shipment);
             /**
              * temporary disabling additional cost
              */
             $sum_add_cost = 0;
             $shipment->charges = $shipment->shipping_charges + $sum_add_cost;
             $list_add_cost = $shipment->getAdditionalCharges();
             if (!$shipment->save()) {
                 throw new ServiceControllerException($shipment->getErrors());
             }
         } else {
             throw new ServiceControllerException($shipment->getErrors());
         }
         if (isset($_GET['confirm']) && $_GET['confirm'] == 1) {
             $trans->commit();
             $data = array('shipment_id' => $shipment->id, 'status' => $shipment->shipping_status, 'time' => date('Y-m-d H:i:s', $shipment->created), 'charges' => $shipment->charges, 'awb' => $shipment->awb);
         } elseif (isset($_GET['confirm']) && $_GET['confirm'] == 0 || !isset($_GET['confirm'])) {
             $data = array('confirm' => 0, 'additional_cost' => $list_add_cost, 'shipping_charges' => $shipment->shipping_charges, 'total' => $shipment->charges, 'shipper_name' => $shipment->shipper_name, 'shipper_address' => $shipment->shipper_address, 'shipper_city' => $shipment->shipper_city, 'shipper_postal' => $shipment->shipper_postal, 'shipper_country' => $shipment->shipper_country, 'receiver_name' => $shipment->receiver_name, 'receiver_address' => $shipment->receiver_address, 'receiver_city' => $shipment->receiver_city, 'receiver_postal' => $shipment->receiver_postal, 'receiver_country' => $shipment->receiver_country, 'goods_desc' => $shipment->goods_desc, 'shipment_value' => $shipment->package_value, 'weight' => $shipment->package_weight, 'pieces' => $shipment->pieces, 'pay_bay' => $shipment->pay_by, 'payer' => $shipment->payer, 'customer_id' => $customer->id);
             $trans->rollback();
         }
     } catch (ServiceControllerException $e) {
         $errors = $e->errors;
         $trans->rollback();
         echo CJSON::encode($this->statusError($errors));
         Yii::app()->end();
     } catch (CDbException $e) {
         $trans->rollback();
         echo CJSON::encode($this->statusError($e));
         Yii::app()->end();
     }
     echo CJSON::encode($this->statusSuccess($data));
     Yii::app()->end();
 }
Beispiel #6
0
 public static function bulkOrder($rawdatas, $customer, $contact, $routing_code)
 {
     $failed = array();
     $success = array();
     $line_error = array();
     $counter = 0;
     $column = array();
     foreach ($rawdatas as $data) {
         $valid_area = true;
         if ($counter++ == 0) {
             continue;
         }
         $column = explode(',', $data);
         $shipment = new Shipment();
         if (count($column) == 21) {
             //account detail
             $shipment->setAttribute('customer_id', $customer->id);
             $shipment->setAttribute('origin_code', $routing_code);
             //shipper_detail
             $shipment->setAttribute('shipper_name', trim($column[2]));
             $shipment->setAttribute('shipper_company_name', trim($column[1]));
             $shipment->setAttribute('shipper_address', trim($column[3]));
             $shipment->setAttribute('shipper_city', trim($column[5]));
             $shipment->setAttribute('shipper_country', trim($column[6]));
             $shipment->setAttribute('shipper_postal', trim($column[7]));
             $shipment->setAttribute('shipper_phone', trim($column[4]));
             //receiver_detail
             $shipment->setAttribute('receiver_name', trim($column[9]));
             $shipment->setAttribute('receiver_company_name', trim($column[8]));
             $shipment->setAttribute('receiver_address', trim($column[10]));
             $shipment->setAttribute('receiver_city', trim($column[12]));
             $shipment->setAttribute('receiver_country', trim($column[13]));
             $shipment->setAttribute('receiver_postal', trim($column[14]));
             $shipment->setAttribute('receiver_phone', trim($column[11]));
             //shipment_detail
             $shipment->setAttribute('type', 'document');
             $shipment->setAttribute('payer', 'shipper');
             $shipment->setAttribute('payby', 'account');
             $shipment->setAttribute('pieces', trim($column[15]));
             $shipment->setAttribute('package_weight', trim($column[16]));
             $shipment->setAttribute('package_value', trim($column[17]));
             $shipment->setAttribute('service_type', trim($column[18]));
             $shipment->setAttribute('service_id', trim($column[19]));
             $shipment->setAttribute('service_code', trim($column[20]));
             $shipment->setAttribute('destination_code', trim($column[0]));
             $customer_rate = CustomerDiscount::getCustomerDiscountRate($shipment->service_id, $shipment->customer_id);
             if (!!$customer_rate) {
                 if ($customer_rate['discount'] == null) {
                     $customer_rate['discount'] = 0;
                 }
                 switch ($shipment->service_type) {
                     case 'City Courier':
                         $rate = RateCity::model()->findByAttributes(array('service_id' => $shipment->service_id));
                         if ($rate instanceof RateCity) {
                             if ($customer_rate['harga_invoice'] != 0) {
                                 $price = $customer_rate['harga_invoice'] * RateCity::increment($shipment->package_weight, $rate->weight_inc);
                             } else {
                                 $price = ($rate->price - $rate->price * ($customer_rate['discount'] / 100)) * RateCity::increment($shipment->package_weight, $rate->weight_inc);
                             }
                             $price_vendor = ($rate->price - $rate->price * ($customer_rate['vendor_discount'] / 100)) * RateCity::increment($shipment->package_weight, $rate->weight_inc);
                         } else {
                             $area = Area::getZoneID($shipment->receiver_postal);
                             if (!$area) {
                                 $price = 0;
                                 $price_vendor = 0;
                                 $valid_area = false;
                             } else {
                                 $price = RateDomestic::getRatePrice($shipment->service_id, 1, $area['district_id'], $area['zone_id'], $shipment->package_weight, $customer_rate['discount']);
                                 $price_vendor = RateDomestic::getRatePrice($shipment->service_id, 1, $area['district_id'], $area['zone_id'], $shipment->package_weight, $customer_rate['vendor_discount']);
                             }
                         }
                         break;
                     case 'Domestic':
                         $area = Area::getZoneID($shipment->receiver_postal);
                         if (!$area) {
                             $price = 0;
                             $price_vendor = 0;
                             $valid_area = false;
                         } else {
                             $price = RateDomestic::getRatePrice($shipment->service_id, 1, $area['district_id'], $area['zone_id'], $shipment->package_weight, $customer_rate['discount']);
                             $price_vendor = RateDomestic::getRatePrice($shipment->service_id, 1, $area['district_id'], $area['zone_id'], $shipment->package_weight, $customer_rate['vendor_discount']);
                         }
                         break;
                     case 'International':
                         $zone = FALSE;
                         if ($shipment->service_code == 'IEX') {
                             $country = $shipment->receiver_country;
                         } else {
                             if ($shipment->service_code == 'IMX') {
                                 $country = $shipment->shipper_country;
                             }
                         }
                         $zone = ZoneInternational::getZoneCountryData($country);
                         if (!!$zone) {
                             $price = RateInternational::getRatePrice($shipment->service_id, $shipment->package_weight, $shipment->type, $zone, $customer_rate['discount']);
                             $price_vendor = RateInternational::getRatePrice($shipment->service_id, $shipment->package_weight, $shipment->type, $zone, $customer_rate['vendor_discount']);
                         } else {
                             $price = 0;
                             $price_vendor = 0;
                             $valid_area = false;
                         }
                         break;
                     case '':
                         continue;
                 }
                 $shipment->shipping_charges = $price;
                 $shipment->vendor_charge = $price_vendor;
                 $shipment->awb = '90' . rand(10000000, 99999999);
                 while (!$shipment->validate(array('awb'))) {
                     $shipment->awb = '90' . rand(10000000, 99999999);
                 }
                 if ($shipment->save()) {
                     $additional_costs = ShipmentAdditionalCharge::initSurcharges($shipment);
                     $shipment->charges = $shipment->shipping_charges + $additional_costs;
                     $shipment->save();
                     array_push($success, $counter);
                 } else {
                     CVarDumper::dump($shipment->attributes, 10, true);
                     CVarDumper::dump($shipment->getErrors(), 10, true);
                     array_push($failed, array('counter' => $counter, 'message' => $shipment->getErrors()));
                 }
             } else {
                 array_push($failed, array('counter' => $counter, 'message' => array('this service is not available')));
             }
         } else {
             array_push($failed, array('counter' => $counter, 'message' => array('wrong delimiter format')));
         }
     }
     return array('success' => $success, 'failed' => $failed);
 }
 public function actionDiscount($id)
 {
     $customer = $this->loadModel($id);
     $services = new ServiceDetail('search');
     $services->unsetAttributes();
     // clear any default values
     if (isset($_GET['ServiceDetail'])) {
         $services->attributes = $_GET['ServiceDetail'];
     }
     if (isset($_POST['CustomerDiscount'])) {
         foreach ($_POST['CustomerDiscount'] as $key => $data) {
             if (isset($data['id'])) {
                 $model = CustomerDiscount::model()->findByPk($data['id']);
             } else {
                 $model = new CustomerDiscount();
             }
             $model->setAttributes($data);
             $model->save();
         }
     }
     $this->render('discount', array('services' => $services, 'customer' => $customer));
 }
 public function actionGetRates()
 {
     if (Yii::app()->request->isAjaxRequest && isset($_GET['Cek'])) {
         $inquiry = new InquiryForm('api-rate');
         $inquiry->setAttributes($_GET['Cek']);
         $customer_id = null;
         $use_rate = array();
         $rates = array();
         if (isset($_GET['customer_id']) && is_numeric($_GET['customer_id'])) {
             $customer_id = $_GET['customer_id'];
             $customer = Customer::model()->findByPk($customer_id);
             if ($customer instanceof Customer) {
                 $customer_id = $customer->id;
                 $customer_rates = CustomerDiscount::model()->findAllByAttributes(array('customer_id' => $customer->id, 'use_rate' => 1));
                 foreach ($customer_rates as $cus_rate) {
                     array_push($use_rate, $cus_rate->service_id);
                 }
             }
         }
         if (strtolower($inquiry->receiver_country) != 'indonesia') {
             $country = ZoneInternational::model()->findByAttributes(array('country' => strtolower($inquiry->receiver_country)));
             if ($country instanceof ZoneInternational) {
                 $rates = RateInternational::getServices($inquiry->package_weight, $country->zone, $country->transit_time, $customer_id, $use_rate);
                 $product = 'International';
             }
         } else {
             $routing = IntraCityRouting::model()->findByAttributes(array('postcode' => $inquiry->receiver_postal));
             if ($routing instanceof IntraCityRouting) {
                 $rates1 = RateCity::getCityRate(ProductService::ProductCityCourier, $routing->code, $inquiry->package_weight, $customer_id, $use_rate);
                 $rates2 = array();
                 $area = Area::getZoneID($inquiry->receiver_postal, 'postcode');
                 if ($area) {
                     $rates2 = RateDomestic::getServiceList(1, $area['district_id'], $area['zone_id'], $inquiry->package_weight, ProductService::ProductCityCourier, $customer_id, $use_rate);
                     $rates = array_merge($rates1, $rates2);
                 } else {
                     $rates = $rates1;
                 }
                 $product = 'City Courier';
             } else {
                 $area = Area::getZoneID($inquiry->receiver_postal, 'postcode');
                 if ($area) {
                     $rates = RateDomestic::getServiceList(1, $area['district_id'], $area['zone_id'], $inquiry->package_weight, ProductService::ProductDomestic, $customer_id, $use_rate);
                 }
                 $product = 'Domestic';
             }
         }
         $data = new CArrayDataProvider($rates);
         //			CVarDumper::dump($rates,10,true);exit;
         $this->renderPartial('_rates', array('rates' => $data, 'product' => $product, 'customer_id' => $customer_id, 'postal' => $inquiry->receiver_postal, 'country' => $inquiry->receiver_country, 'product' => $product, 'package_weight' => $inquiry->package_weight), false, true);
         Yii::app()->end();
     }
 }
 public function deleteCustomerDiscounts($id)
 {
     $cd = CustomerDiscount::where('id', $id)->first();
     if ($cd) {
         $cd->delete();
         return 'success';
     }
 }