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(); }
/** * Set shipment status to completed * * @param Shipment $shipment * @param string $recepient_name */ private function setJneStatusOrder(Shipment $shipment, $status, $recepient_name = '') { $event = new ShipmentEvent(); $shipment->setScenario('event'); $event->created = time(); $event->event_time = $event->created; $event->shipment_id = $shipment->id; $event->user_id = User::USER_SYSTEM; switch (strtoupper($status)) { case 'DELIVERED': $event->status = ShipmentStatus::POD; $shipment->shipping_status = ShipmentStatus::POD; $shipment->event_time = $event->event_time; $shipment->recipient_name = $recepient_name; break; case 'MANIFESTED': $event->status = ShipmentStatus::MDE; $shipment->shipping_status = ShipmentStatus::MDE; $shipment->event_time = $event->event_time; break; case 'RECEIVED ON DESTINATION': $event->status = ShipmentStatus::ARR; $shipment->shipping_status = ShipmentStatus::ARR; $shipment->event_time = $event->event_time; break; case 'ON PROCESS': $event->status = ShipmentStatus::OTW; $shipment->shipping_status = ShipmentStatus::OTW; $shipment->event_time = $event->event_time; break; } try { $trans = Yii::app()->db->beginTransaction(); if ($event->save()) { if ($shipment->save()) { $trans->commit(); $this->printf('Shipment set to %s', $status); return true; } else { print_r($shipment->getErrors()); throw new CException(); } } else { print_r($event->getErrors()); throw new CException(); } } catch (CException $e) { $trans->rollback(); throw $e; } }