Пример #1
0
 public function order_save($indata, $api_key, $transaction_id)
 {
     date_default_timezone_set('Asia/Jakarta');
     $args = '';
     //$api_key = $this->get('key');
     //$transaction_id = $this->get('trx');
     if (is_null($api_key)) {
         $result = json_encode(array('status' => 'ERR:NOKEY', 'timestamp' => now()));
         return $result;
     } else {
         $app = \Prefs::get_key_info(trim($api_key));
         if ($app == false) {
             $result = json_encode(array('status' => 'ERR:INVALIDKEY', 'timestamp' => now()));
             return $result;
         } else {
             //$in = $this->input->post('transaction_detail');
             //$in = file_get_contents('php://input');
             $in = $indata;
             //print $in;
             $buyer_id = 1;
             $args = 'p=' . $in;
             $in = json_decode($in);
             //print "order input to save: \r\n";
             //print_r($in);
             $is_new = false;
             $in->phone = isset($in->phone) && $in->phone != '' ? \Prefs::normalphone($in->phone) : '';
             $in->mobile1 = isset($in->mobile1) && $in->mobile1 != '' ? \Prefs::normalphone($in->mobile1) : '';
             $in->mobile2 = isset($in->mobile2) && $in->mobile2 != '' ? \Prefs::normalphone($in->mobile2) : '';
             if (isset($in->buyer_id) && $in->buyer_id != '' && $in->buyer_id > 1) {
                 $buyer_id = $in->buyer_id;
                 $is_new = false;
             } else {
                 if ($in->email == '' || $in->email == '-' || !isset($in->email) || $in->email == 'noemail') {
                     $in->email = 'noemail';
                     $is_new = true;
                     if (trim($in->phone . $in->mobile1 . $in->mobile2) != '') {
                         if ($buyer = \Prefs::check_phone($in->phone, $in->mobile1, $in->mobile2)) {
                             $buyer_id = $buyer['id'];
                             $is_new = false;
                         }
                     }
                 } else {
                     if ($buyer = \Prefs::check_email($in->email)) {
                         $buyer_id = $buyer['id'];
                         $is_new = false;
                     } else {
                         if ($buyer = \Prefs::check_phone($in->phone, $in->mobile1, $in->mobile2)) {
                             $buyer_id = $buyer['id'];
                             $is_new = false;
                         }
                     }
                 }
             }
             if (isset($in->merchant_trans_id) && $in->merchant_trans_id != "") {
                 $transaction_id = $in->merchant_trans_id;
             }
             if ($is_new) {
                 $random_string = str_random(5);
                 $buyer_username = substr(strtolower(str_replace(' ', '', $in->buyer_name)), 0, 6) . $random_string;
                 $dataset['username'] = $buyer_username;
                 $dataset['email'] = $in->email;
                 $dataset['phone'] = $in->phone;
                 $dataset['mobile1'] = $in->mobile1;
                 $dataset['mobile2'] = $in->mobile2;
                 $dataset['fullname'] = $in->buyer_name;
                 $password = str_random(8);
                 $dataset['password'] = $password;
                 $dataset['created'] = date('Y-m-d H:i:s', time());
                 /*
                 $dataset['province'] =
                 $dataset['mobile']
                 */
                 $dataset['street'] = $in->shipping_address;
                 $dataset['district'] = $in->buyerdeliveryzone;
                 $dataset['city'] = $in->buyerdeliverycity;
                 $dataset['country'] = 'Indonesia';
                 $dataset['zip'] = isset($in->zip) ? $in->zip : '';
                 //$buyer_id = $this->register_buyer($dataset);
                 $is_new = true;
             }
             $order['created'] = date('Y-m-d H:i:s', time());
             $order['ordertime'] = date('Y-m-d H:i:s', time());
             $order['application_id'] = $app->id;
             $order['application_key'] = $app->key;
             $order['buyer_id'] = $buyer_id;
             $order['merchant_id'] = $app->merchant_id;
             $order['merchant_trans_id'] = trim($transaction_id);
             $order['buyer_name'] = $in->buyer_name;
             $order['recipient_name'] = $in->recipient_name;
             $order['email'] = $in->email;
             $order['directions'] = $in->directions;
             //$order['dir_lat'] = $in->dir_lat;
             //$order['dir_lon'] = $in->dir_lon;
             $order['buyerdeliverytime'] = $in->buyerdeliverytime;
             $order['buyerdeliveryslot'] = $in->buyerdeliveryslot;
             $order['buyerdeliveryzone'] = $in->buyerdeliveryzone;
             $order['buyerdeliverycity'] = is_null($in->buyerdeliverycity) || $in->buyerdeliverycity == '' ? 'Jakarta' : $in->buyerdeliverycity;
             $order['currency'] = $in->currency;
             $order['total_price'] = isset($in->total_price) ? $in->total_price : 0;
             $order['total_discount'] = isset($in->total_discount) ? $in->total_discount : 0;
             $order['total_tax'] = isset($in->total_tax) ? $in->total_tax : 0;
             if (in_array(strtoupper(trim($in->delivery_type)), array('COD', 'CCOD', 'PS', 'DO', 'DELIVERY ONLY'))) {
                 $in->delivery_type = 'COD';
             }
             $order['delivery_type'] = $in->delivery_type;
             if ($in->delivery_type == 'DO' || $in->delivery_type == 'Delivery Only') {
                 $order['cod_cost'] = 0;
             } else {
                 $order['cod_cost'] = \Prefs::get_cod_tariff($order['total_price'], $app->id);
             }
             $order['box_count'] = isset($in->box_count) ? $in->box_count : 1;
             $order['pending_count'] = isset($in->pending_count) ? $in->pending_count : 0;
             $order['delivery_note'] = isset($in->delivery_note) ? $in->delivery_note : '';
             $order['shipping_address'] = $in->shipping_address;
             $order['shipping_zip'] = $in->shipping_zip;
             $order['phone'] = $in->phone;
             $order['mobile1'] = $in->mobile1;
             $order['mobile2'] = $in->mobile2;
             $order['status'] = $in->status;
             $order['width'] = $in->width;
             $order['height'] = $in->height;
             $order['length'] = $in->length;
             $order['weight'] = isset($in->weight) ? $in->weight : 0;
             $order['actual_weight'] = isset($in->actual_weight) ? $in->actual_weight : 0;
             $order['delivery_cost'] = $order['weight'];
             $order['cod_bearer'] = isset($in->cod_bearer) ? $in->cod_bearer : 'merchant';
             $order['delivery_bearer'] = isset($in->delivery_bearer) ? $in->delivery_bearer : 'merchant';
             $order['cod_method'] = isset($in->cod_method) ? $in->cod_method : 'cash';
             $order['ccod_method'] = isset($in->ccod_method) ? $in->ccod_method : 'full';
             $order['fulfillment_code'] = isset($in->fulfillment_code) ? $in->fulfillment_code : '';
             // check out who is bearing the cost
             if ($order['delivery_type'] == 'COD' || $order['delivery_type'] == 'CCOD') {
                 if ($order['delivery_bearer'] == 'merchant') {
                     $dcost = 0;
                 } else {
                     $dcost = $order['delivery_cost'];
                 }
                 if ($order['cod_bearer'] == 'merchant') {
                     $codcost = 0;
                 } else {
                     $codcost = $order['cod_cost'];
                 }
                 $order['chargeable_amount'] = $order['total_price'] + $dcost + $codcost;
             } else {
                 if ($order['delivery_bearer'] == 'merchant') {
                     $dcost = 0;
                 } else {
                     $dcost = $order['delivery_cost'];
                 }
                 $order['chargeable_amount'] = $dcost;
             }
             if (isset($in->show_shop)) {
                 $order['show_shop'] = $in->show_shop;
             }
             if (isset($in->show_merchant)) {
                 $order['show_merchant'] = $in->show_merchant;
             }
             $order['is_api'] = 1;
             $ship = new \Shipment();
             foreach ($order as $k => $v) {
                 $ship->{$k} = $v;
             }
             $ship->save();
             $sequence = $ship->id;
             if (isset($in->delivery_id)) {
                 if (is_null($in->delivery_id) || $in->delivery_id == '') {
                     $delivery_id = \Prefs::get_delivery_id($sequence, $app->merchant_id);
                 } else {
                     $delivery_id = \Prefs::get_delivery_id($sequence, $app->merchant_id, $in->delivery_id);
                 }
             } else {
                 $delivery_id = \Prefs::get_delivery_id($sequence, $app->merchant_id);
             }
             $ship->delivery_id = $delivery_id;
             //print_r($ship);
             $ship->save();
             //die();
             if (isset($in->box_count)) {
                 $box_count = $in->box_count;
             } else {
                 $box_count = 1;
             }
             \Prefs::save_box($delivery_id, trim($transaction_id), $order['fulfillment_code'], $box_count);
             $nedata['fullname'] = $in->buyer_name;
             $nedata['merchant_trx_id'] = trim($transaction_id);
             $nedata['delivery_id'] = $delivery_id;
             $nedata['merchantname'] = $app->application_name;
             $nedata['app'] = $app;
             $order['delivery_id'] = $delivery_id;
             $buyer_id = \Prefs::save_buyer($order);
             /*
             $this->db->where('id',$sequence)->update($this->config->item('incoming_delivery_table'),array('delivery_id'=>$delivery_id));
             */
             /*
                 $this->table_tpl = array(
                     'table_open' => '<table border="0" cellpadding="4" cellspacing="0" class="dataTable">'
                 );
                 $this->table->set_template($this->table_tpl);
             
             
                 $this->table->set_heading(
                     'No.',
                     'Description',
                     'Quantity',
                     'Total'
                     ); // Setting headings for the table
             */
             $d = 0;
             $gt = 0;
             if ($in->trx_detail) {
                 $seq = 0;
                 foreach ($in->trx_detail as $it) {
                     $item = new \Deliverydetail();
                     $item->ordertime = $order['ordertime'];
                     $item->delivery_id = $delivery_id;
                     $item->unit_sequence = $seq++;
                     $item->unit_description = $it->unit_description;
                     $item->unit_price = $it->unit_price;
                     $item->unit_quantity = $it->unit_quantity;
                     $item->unit_total = $it->unit_total;
                     $item->unit_discount = $it->unit_discount;
                     $item->save();
                     /*
                     $this->table->add_row(
                         (int)$item['unit_sequence'] + 1,
                         $item['unit_description'],
                         $item['unit_quantity'],
                         $item['unit_total']
                     );
                     
                     $u_total = str_replace(array(',','.'), '', $item['unit_total']);
                     $u_discount = str_replace(array(',','.'), '', $item['unit_discount']);
                     $gt += (int)$u_total;
                     $d += (int)$u_discount;
                     */
                 }
                 $total = isset($in->total_price) && $in->total_price > 0 ? $in->total_price : 0;
                 $total = str_replace(array(',', '.'), '', $total);
                 $total = (int) $total;
                 $gt = $total < $gt ? $gt : $total;
                 $disc = isset($in->total_discount) ? $in->total_discount : 0;
                 $tax = isset($in->total_tax) ? $in->total_tax : 0;
                 $cod = isset($in->cod_cost) ? $in->cod_cost : 'Paid by merchant';
                 $disc = str_replace(array(',', '.'), '', $disc);
                 $tax = str_replace(array(',', '.'), '', $tax);
                 $cod = str_replace(array(',', '.'), '', $cod);
                 $disc = (int) $disc;
                 $tax = (int) $tax;
                 $cod = (int) $cod;
                 $chg = $gt - $disc + $tax + $cod;
                 /*
                 $this->table->add_row(
                     '',
                     '',
                     'Total Price',
                     number_format($gt,2,',','.')
                 );
                 
                 $this->table->add_row(
                     '',
                     '',
                     'Total Discount',
                     number_format($disc,2,',','.')
                 );
                 
                 $this->table->add_row(
                     '',
                     '',
                     'Total Tax',
                     number_format($tax,2,',','.')
                 );
                 
                 
                 if($cod == 0){
                     $this->table->add_row(
                         '',
                         '',
                         'COD Charges',
                         'Paid by Merchant'
                     );
                 }else{
                     $this->table->add_row(
                         '',
                         '',
                         'COD Charges',
                         number_format($cod,2,',','.')
                     );
                 }
                 
                 
                 $this->table->add_row(
                     '',
                     '',
                     'Total Charges',
                     number_format($chg,2,',','.')
                 );
                 
                 $nedata['detail'] = $this->table;
                 
                 
                 $result = json_encode(array('status'=>'OK:ORDERPOSTED','timestamp'=>now(),'delivery_id'=>$delivery_id,'buyer_id'=>$buyer_id));
                 */
                 //return $ship->toArray();
             } else {
                 //$nedata['detail'] = false;
                 //$result = json_encode(array('status'=>'OK:ORDERPOSTEDNODETAIL','timestamp'=>now(),'delivery_id'=>$delivery_id));
                 //return $order;
             }
             return $ship->toArray();
             //print_r($app);
             /*
             
                             if($app->notify_on_new_order == 1){
                                 send_notification('New Delivery Order - Jayon Express COD Service',$in->email,$app->cc_to,$app->reply_to,'order_submit',$nedata,null);
                             }
             
                             if($is_new == true){
                                 $edata['fullname'] = $dataset['fullname'];
                                 $edata['username'] = $buyer_username;
                                 $edata['password'] = $password;
                                 if($app->notify_on_new_member == 1 && $in->email != 'noemail'){
                                     send_notification('New Member Registration - Jayon Express COD Service',$in->email,null,null,'new_member',$edata,null);
                                 }
             
                             }*/
         }
     }
     //$this->log_access($api_key, __METHOD__ ,$result,$args);
 }