public function api_makeorder() { Configure::write('debug', 0); ob_start(); print_r($this->request->data); $c = ob_get_clean(); $fc = fopen('files' . DS . 'detail.txt', 'w'); fwrite($fc, $c); fclose($fc); if ($this->request->is('post')) { $d = $this->request->data; $sum = 0; $tm = null; foreach ($d as $v) { $sum += $v['Order']['price'] * $v['Order']['qty']; $tm = $v['Order']['timestamp']; } if ($this->Order->saveAll($d, array('atomic' => true))) { $x = $this->Order->find("all", array("conditions" => array("Order.sku" => $d[0]['Order']['sku']), "contain" => array("Address", "Combination", "Combination.Vendor"))); $ttl = 0; foreach ($x as $rt) { $ttl += $rt['Combination']['price'] * $rt['Order']['qty']; $ComboObj = new Combination(); $ComboObj->updateAll(array("Combination.stock_count" => "'" . ((int) $rt['Combination']['stock_count'] - (int) $rt['Order']['qty']) . "'"), array("Combination.id" => $rt['Combination']['id'])); } // Code to Deduct money from wallet $this->loadModel("Customer"); $cst = $this->Customer->find("first", array("conditions" => array("Customer.id" => $x[0]['Order']['customer_id']), "contain" => false)); $cashToPay = $x[0]['Combination']['price']; if ($cst['Customer']['cash_by_promo'] >= 0) { if ($cst['Customer']['cash_by_promo'] - $ttl <= 0) { $cashAm = 0; $cashToPay = abs($cst['Customer']['cash_by_promo'] - $ttl); } else { $cashAm = $cst['Customer']['cash_by_promo'] - $ttl; $cashToPay = 0; } $v = $this->Customer->updateAll(array("Customer.cash_by_promo" => "'" . $cashAm . "'"), array("Customer.id" => $cst['Customer']['id'])); $this->Order->updateAll(array("Order.discount_amount" => "'" . $cst['Customer']['cash_by_promo'] . "'"), array("Order.sku" => $x[0]['Order']['sku'])); // CakeLog::debug(print_r($cashToPay,true)); // CakeLog::debug(print_r($cst,true)); } // $total = 0; // foreach($x as $t){ // $total += $t['Combination']['price'] * $t['Order']['qty']; // } App::uses("CakeEmail", "Network/Email"); $fm = new CakeEmail('smtp'); $viewVars = array('id_o' => $x[0]['Order']['sku'], 'name' => $x[0]['Address']['f_name'] . " " . $x[0]['Address']['l_name'], 'mob' => $x[0]['Address']['phone_number'], 'address' => $x[0]['Address']['address'], 'orders' => $x, 'total' => $cashToPay); $fm->to("*****@*****.**")->viewVars($viewVars)->from("*****@*****.**", "PickMeals.com")->replyTo("*****@*****.**", "PickMeals.com")->subject("New Order on PickMeals.com (ID :" . $x[0]['Order']['sku'] . ")")->template("referal")->emailFormat('html'); try { $x = $fm->send(); } catch (SocketException $e) { debug($e); } // $promo = new PromoController(); // $promo->sendCashToReferal($x[0]['Order']['customer_id']); if ($d[0]['Order']['paid_via'] == 'Cash on Delivery') { $url = "CODE"; } else { $url = FULL_BASE_URL . $this->webroot . 'orders/payuweb/' . $this->Order->getLastInsertID() . "U" . $sum . "U" . $tm; } $this->set($x = array('data' => array('error' => 0, 'msg' => 'Success', 'url' => $url), '_serialize' => array('data'))); } else { $this->set($x = array('data' => array("error" => 1, "msg" => "Error Occured...", "trace" => $this->Order->validationErrors), '_serialize' => array('data'))); } Debugger::log($x); } }
public function neworder() { $this->loadModel("Combination"); $this->loadModel("Customer"); $this->loadModel("Address"); $this->loadModel("Order"); if ($this->request->is(array('post'))) { $d = $this->request->data; // print_r($d); // exit; $customer = $this->Customer->find("first", array("conditions" => array("Customer.id" => $d['Customer']['id']))); if (empty($customer)) { $this->Customer->create(); $d['Customer']['v_code'] = strtoupper($this->randomString(5)); $d['Customer']['password'] = $d['Customer']['v_code']; $d['Customer']['is_admin'] = 0; $d['Customer']['registered_on'] = time(); $d['Customer']['device_token'] = 'Added From Admin'; $d['Customer']['verified'] = 'true'; $d['Customer']['status'] = 'VERIFIED'; $d['Customer']['cash_by_promo'] = 0; $d['Customer']['referal_paid'] = 0; $d['Customer']['address'] = $d['Address']['address']; $this->Customer->save(array("Customer" => $d['Customer'])); $this->generatePromo($cid = $this->Customer->getLastInsertID()); $customer = $this->Customer->find("first", array("conditions" => array("Customer.id" => $cid))); } $old_address = $this->Address->find("first", array("conditions" => array('Address.customer_id' => $customer['Customer']['id']), "order" => "Address.id DESC")); $this->Address->create(); $d['Address']['customer_id'] = $customer['Customer']['id']; $tmp = explode(" ", $customer['Customer']['name']); $d['Address']['f_name'] = isset($tmp[0]) ? $tmp[0] : ''; $d['Address']['l_name'] = isset($tmp[1]) ? $tmp[1] : ''; $d['Address']['email'] = $customer['Customer']['email']; $d['Address']['phone_number'] = $customer['Customer']['mobile_number']; $d['Address']['status'] = 1; if (!empty($old_address)) { if (strlen($d['Address']['address']) < 4) { $d['Address']['address'] = $old_address['Address']['address']; } } $this->Address->save(array("Address" => $d['Address'])); $aid = $this->Address->getLastInsertID(); $s = rand(0, 5); $sku = time(); $sku = strtoupper(md5($sku)); $sku = substr($sku, $s, 5); $sku = "P-" . $sku; $eodrs = array(); foreach ($d['orders'] as $order) { $odr = json_decode($order, true); $combination = $this->Combination->find("first", array("conditions" => array("Combination.id" => $odr['Order']['combination_id']), "contain" => FALSE)); $odr['Order']['customer_id'] = $customer['Customer']['id']; $odr['Order']['address_id'] = $aid; $odr['Order']['recipe_names'] = $combination['Combination']['display_name']; $odr['Order']['price'] = $combination['Combination']['price']; $odr['Order']['essentials'] = 'chk_value'; $odr['Order']['paid_via'] = 'Cash on Delivery'; $odr['Order']['sku'] = $sku; $odr['Order']['status'] = 1; $odr['Order']['timestamp'] = time(); $this->Order->create(); $this->Order->save(array("Order" => $odr['Order'])); $eodrs[] = $this->Order->find("first", array("conditions" => array("Order.id" => $this->Order->getLastInsertID()), "contain" => array("Address", "Combination", "Combination.Vendor"))); } $ttl = 0; foreach ($eodrs as $rt) { $ttl += $rt['Combination']['price'] * $rt['Order']['qty']; $ComboObj = new Combination(); $ComboObj->updateAll(array("Combination.stock_count" => "'" . ((int) $rt['Combination']['stock_count'] - (int) $rt['Order']['qty']) . "'"), array("Combination.id" => $rt['Combination']['id'])); } $cashToPay = $eodrs[0]['Combination']['price']; if ($customer['Customer']['cash_by_promo'] >= 0) { if ($customer['Customer']['cash_by_promo'] - $ttl <= 0) { $cashAm = 0; $cashToPay = abs($customer['Customer']['cash_by_promo'] - $ttl); } else { $cashAm = $customer['Customer']['cash_by_promo'] - $ttl; $cashToPay = 0; } $v = $this->Customer->updateAll(array("Customer.cash_by_promo" => "'" . $cashAm . "'"), array("Customer.id" => $customer['Customer']['id'])); $this->Order->updateAll(array("Order.discount_amount" => "'" . $customer['Customer']['cash_by_promo'] . "'"), array("Order.sku" => $eodrs[0]['Order']['sku'])); // CakeLog::debug(print_r($cashToPay,true)); // CakeLog::debug(print_r($cst,true)); } App::uses("CakeEmail", "Network/Email"); $fm = new CakeEmail('smtp'); $viewVars = array('id_o' => $eodrs[0]['Order']['sku'], 'name' => $eodrs[0]['Address']['f_name'] . " " . $eodrs[0]['Address']['l_name'], 'mob' => $eodrs[0]['Address']['phone_number'], 'address' => $eodrs[0]['Address']['address'], 'orders' => $eodrs, 'total' => $cashToPay); $fm->to("*****@*****.**")->viewVars($viewVars)->from("*****@*****.**", "PickMeals.com")->replyTo("*****@*****.**", "PickMeals.com")->subject("New Order on PickMeals.com (ID :" . $eodrs[0]['Order']['sku'] . ")")->template("referal")->emailFormat('html'); try { $x = $fm->send(); } catch (SocketException $e) { debug($e); } $this->sendSms($customer['Customer']['mobile_number'], "Dear " . $customer['Customer']['name'] . ", Thanks for placing order. Your Order " . $eodrs[0]['Order']['recipe_names'] . " will be delivered within 45 minutes."); $this->autoRender = FALSE; $this->response->type('json'); $this->response->body(json_encode(array("error" => 0, "msg" => "Order Placed..."))); } $cnd = array("Combination.type" => "MAIN", "Combination.visible" => 1, "DATE(Combination.date) >= " => $this->_since); $combinations = $this->Combination->find('all', array("conditions" => $cnd, "fields" => array("get_distance_in_miles_between_geo_locations(0,0,Vendor.lat,Vendor.long) as distance", "Vendor.*", "Combination.*"), "order" => 'Combination.id DESC', "limit" => 10)); $this->set("combinations", $combinations); $customers = $this->Customer->find("all", array("conditions" => array("Customer.is_admin" => 0), "contain" => false, "order" => "Customer.mobile_number DESC")); $cs = array(); foreach ($customers as $c) { $cs[$c['Customer']['id']] = $c['Customer']['mobile_number'] . " " . $c['Customer']['name']; } // print_r($cs); exit; $this->set("customers", $cs); }