Advertisement
Guest User

localdelivery.php

a guest
Mar 18th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.13 KB | None | 0 0
  1. <?php
  2. class extension_Mpperproductshipping_Model_Carrier_LocalDelivery extends Mage_Shipping_Model_Carrier_Abstract
  3. {
  4. /* Use group alias */
  5. protected $_code = 'mpperproductshipping';
  6.  
  7.  
  8. // Prateek code start
  9.  
  10. public function collectRates(Mage_Shipping_Model_Rate_Request $request) {
  11. $storeId = $request->getStoreId();
  12. if (!$request->getOrig()) {
  13. $request
  14. ->setCountryId(Mage::getStoreConfig(self::XML_PATH_STORE_COUNTRY_ID, $request->getStore()))
  15. ->setRegionId(Mage::getStoreConfig(self::XML_PATH_STORE_REGION_ID, $request->getStore()))
  16. ->setCity(Mage::getStoreConfig(self::XML_PATH_STORE_CITY, $request->getStore()))
  17. ->setPostcode(Mage::getStoreConfig(self::XML_PATH_STORE_ZIP, $request->getStore()));
  18. }
  19.  
  20. $postCode = $request->getDestPostcode();
  21. $restrictedCodes = array(
  22. 110001,
  23. 110002
  24. );
  25. //restricted values. they can come from anywhere
  26.  
  27. if (!in_array($postCode, $restrictedCodes)) {
  28. return $this;
  29. }
  30.  
  31. $limitCarrier = $request->getLimitCarrier();
  32. if (!$limitCarrier) {
  33. $carriers = Mage::getStoreConfig('carriers', $storeId);
  34.  
  35. foreach ($carriers as $carrierCode => $carrierConfig) {
  36. $this->collectCarrierRates($carrierCode, $request);
  37. }
  38. } else {
  39. if (!is_array($limitCarrier)) {
  40. $limitCarrier = array($limitCarrier);
  41. }
  42. foreach ($limitCarrier as $carrierCode) {
  43. $carrierConfig = Mage::getStoreConfig('carriers/' . $carrierCode, $storeId);
  44. if (!$carrierConfig) {
  45. continue;
  46. }
  47. $this->collectCarrierRates($carrierCode, $request);
  48. }
  49. }
  50.  
  51. return $this;
  52. }
  53.  
  54. // Prateek code end
  55.  
  56. $result = Mage::getModel('shipping/rate_result');
  57.  
  58. /* Edited by vikas_mageworx */
  59. $postcode=$request->getDestPostcode();
  60. $countrycode=$request->getDestCountry();
  61. $items=$request->getAllItems();
  62. /* End Editing by vikas_mageworx */
  63.  
  64. $postcode=str_replace('-', '', $postcode);
  65. $shippingdetail=array();
  66.  
  67. /* one start */
  68. $shippostaldetail=array('countrycode'=>$countrycode,'postalcode'=>$postcode,'items'=>$items);
  69. /* one end */
  70.  
  71.  
  72.  
  73. foreach($items as $item) {
  74. $proid=$item->getProductId();
  75. $options=$item->getProductOptions();
  76. $mpassignproductId=$options['info_buyRequest']['mpassignproduct_id'];
  77. if(!$mpassignproductId) {
  78. foreach($item->getOptions() as $option) {
  79. $temp=unserialize($option['value']);
  80. if($temp['mpassignproduct_id']) {
  81. $mpassignproductId=$temp['mpassignproduct_id'];
  82. }
  83. }
  84. }
  85. if($mpassignproductId) {
  86. $mpassignModel = Mage::getModel('mpassignproduct/mpassignproduct')->load($mpassignproductId);
  87. $partner = $mpassignModel->getSellerId();
  88. } else {
  89. $collection=Mage::getModel('marketplace/product')
  90. ->getCollection()->addFieldToFilter('mageproductid',array('eq'=>$proid));
  91. foreach($collection as $temp) {
  92. $partner=$temp->getUserid();
  93. }
  94. }
  95.  
  96. $product=Mage::getModel('catalog/product')->load($proid)->getWeight();
  97. $weight=$product*$item->getQty();
  98. if(count($shippingdetail)==0){
  99. array_push($shippingdetail,array('seller_id'=>$partner,'items_weight'=>$weight,'product_name'=>$item->getName(),'qty'=>$item->getQty(),'item_id'=>$item->getId()));
  100. }else{
  101. $shipinfoflag=true;
  102. $index=0;
  103. foreach($shippingdetail as $itemship){
  104. if($itemship['seller_id']==$partner){
  105. $itemship['items_weight']=$itemship['items_weight']+$weight;
  106. $itemship['product_name']=$itemship['product_name'].",".$item->getName();
  107. $itemship['item_id']=$itemship['item_id'].",".$item->getId();
  108. $itemship['qty']=$itemship['qty']+$item->getQty();
  109. $shippingdetail[$index]=$itemship;
  110. $shipinfoflag=false;
  111. }
  112. $index++;
  113. }
  114. if($shipinfoflag==true){
  115. array_push($shippingdetail,array('seller_id'=>$partner,'items_weight'=>$weight,'product_name'=>$item->getName(),'qty'=>$item->getQty(),'item_id'=>$item->getId()));
  116. }
  117. }
  118. }
  119. $shippingpricedetail=$this->getShippingPricedetail($shippingdetail,$shippostaldetail);
  120.  
  121. if($shippingpricedetail['errormsg']!==""){
  122. Mage::getSingleton('core/session')->setShippingCustomError($shippingpricedetail['errormsg']);
  123. return $result;
  124. }
  125. /*store shipping in session*/
  126. $shippingAll=Mage::getSingleton('core/session')->getData('shippinginfo');
  127. $shippingAll[$this->_code]=$shippingpricedetail['shippinginfo'];
  128. Mage::getSingleton('core/session')->setData('shippinginfo',$shippingAll);
  129.  
  130. $method = Mage::getModel('shipping/rate_result_method');
  131. $method->setCarrier($this->_code);
  132. $method->setCarrierTitle(Mage::getStoreConfig('carriers/'.$this->_code.'/title'));
  133. /* Use method name */
  134. $method->setMethod($this->_code);
  135. $method->setMethodTitle(Mage::getStoreConfig('carriers/'.$this->_code.'/name'));
  136. $method->setCost($shippingpricedetail['handlingfee']);
  137. $method->setPrice($shippingpricedetail['handlingfee']);
  138. $result->append($method);
  139. return $result;
  140. }
  141.  
  142. public function getShippingPricedetail($shippingdetail,$shippostaldetail) {
  143. $shippinginfo=array();
  144. $handling=0;
  145. $session = Mage::getSingleton('checkout/session');
  146. $customerAddress = $session->getQuote()->getShippingAddress();
  147.  
  148.  
  149. /* Edited by vikas_boy */
  150. $customerPostCode = $shippostaldetail['postalcode'];
  151. $items = $shippostaldetail['items'];
  152. /* End Editing by vikas_boy */
  153.  
  154.  
  155. /* one */
  156.  
  157. foreach($shippingdetail as $shipdetail) {
  158. $seller = Mage::getModel("customer/customer")->load($shipdetail['seller_id']);
  159. $sellerAddress = $seller->getPrimaryShippingAddress();
  160. $distance = $this->getDistanse($sellerAddress->getPostcode(),$customerPostCode);
  161. // echo "distance ".$distance;die;
  162. $price = 0;
  163. $itemsarray=explode(',',$shipdetail['item_id']);
  164. foreach($items as $item) {
  165. $proid=$item->getProductId();
  166. $options=$item->getProductOptions();
  167. $mpassignproductId=$options['info_buyRequest']['mpassignproduct_id'];
  168. if(!$mpassignproductId) {
  169. foreach($item->getOptions() as $option) {
  170. $temp=unserialize($option['value']);
  171. if($temp['mpassignproduct_id']) {
  172. $mpassignproductId=$temp['mpassignproduct_id'];
  173. }
  174. }
  175. }
  176. if (Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($proid))
  177. {
  178. continue;
  179. }
  180. $mpshippingcharge = 0;
  181. $localDistance = Mage::getStoreConfig('marketplace/mpperproductshipping/local_shipping_distance');
  182. $regionalDistance = Mage::getStoreConfig('marketplace/mpperproductshipping/regional_shipping_distance');
  183. $stateDistance = Mage::getStoreConfig('marketplace/mpperproductshipping/state_shipping_distance');
  184. if(in_array($item->getId(),$itemsarray)) {
  185. if($mpassignproductId) {
  186. if($distance < $localDistance) {
  187. $mpshippingcharge=Mage::getModel('mpassignproduct/mpassignproduct')->load($mpassignproductId)->getLocalShippingCharge();
  188. } elseif($distance > $localDistance && $distance < $regionalDistance) {
  189. $mpshippingcharge=Mage::getModel('mpassignproduct/mpassignproduct')->load($mpassignproductId)->getRegionalShippingCharge();
  190. } elseif($distance > $regionalDistance) {
  191. $mpshippingcharge=Mage::getModel('mpassignproduct/mpassignproduct')->load($mpassignproductId)->getStateShippingCharge();
  192. }
  193. } else {
  194. // echo "imte ".$item->getProductId();
  195. if($distance < $localDistance) {
  196. $mpshippingcharge=Mage::getModel('catalog/product')->load($item->getProductId())->getMpLocalShippingCharge();
  197. // echo "imte ".$item->getProductId();
  198. // echo "ship ".$mpshippingcharge;
  199. } elseif($distance > $localDistance && $distance < $regionalDistance) {
  200. $mpshippingcharge=Mage::getModel('catalog/product')->load($item->getProductId())->getMpRegionalShippingCharge();
  201. } elseif($distance > $regionalDistance) {
  202. $mpshippingcharge=Mage::getModel('catalog/product')->load($item->getProductId())->getMpStateShippingCharge();
  203. }
  204. }
  205.  
  206. /* tt */
  207. // echo "test ".$mpshippingcharge;die;
  208. if(!is_numeric($mpshippingcharge)){
  209. $price=$price+floatval($this->getConfigData('defalt_ship_amount')* floatval($item->getQty()));
  210. }else{
  211. $price=$price+($mpshippingcharge * floatval($item->getQty()));
  212. }
  213.  
  214. }
  215. }
  216.  
  217. $handling = $handling+$price;
  218. $submethod = array(array('method'=>Mage::getStoreConfig('carriers/'.$this->_code.'/title'),'cost'=>$price,'error'=>0));
  219. array_push($shippinginfo,array('seller_id'=>$shipdetail['seller_id'],'methodcode'=>$this->_code,'shipping_ammount'=>$price,'product_name'=>$shipdetail['product_name'],'submethod'=>$submethod,'item_ids'=>$shipdetail['item_id']));
  220. }
  221. $msg="";
  222. return array('handlingfee'=>$handling,'shippinginfo'=>$shippinginfo,'errormsg'=>$msg);
  223. }
  224.  
  225.  
  226. /* one end */
  227.  
  228. /* tt start */
  229.  
  230. private function getDistanse($origin,$destination) {
  231. $url = "http://maps.googleapis.com/maps/api/distancematrix/json?origins=".$origin.",india&destinations=".$destination.",india&mode=driving&language=en-EN&sensor=false";
  232. $ch = curl_init();
  233. curl_setopt($ch, CURLOPT_URL, $url);
  234. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  235. curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
  236. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  237. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  238. $response = curl_exec($ch);
  239. curl_close($ch);
  240. $response_all = json_decode($response);
  241. $distance = $response_all->rows[0]->elements[0]->distance->value / 1000;
  242. if($distance==0){
  243. $zips = array(
  244. $origin,$destination
  245. // ... etc ...
  246. );
  247.  
  248. $geocoded = array();
  249. $serviceUrl = "http://maps.googleapis.com/maps/api/geocode/json?components=postal_code:%s&sensor=false";
  250. $curl = curl_init();
  251. foreach ($zips as $zip) {
  252. curl_setopt($curl, CURLOPT_URL, sprintf($serviceUrl, urlencode($zip)));
  253. curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  254. $data = json_decode(curl_exec($curl));
  255. $info = curl_getinfo($curl);
  256. if ($info['http_code'] != 200) {
  257. // Request failed
  258. } else if ($data->status !== 'OK') {
  259. // Something happened, or there are no results
  260. } else {
  261. $geocoded[$zip] =$data->results[0]->geometry->location;
  262. }
  263. }
  264. $distance=$this->DistAB($geocoded[$zips[0]]->lat,$geocoded[$zips[0]]->lng,$geocoded[$zips[1]]->lat,$geocoded[$zips[1]]->lng);
  265.  
  266. }
  267. return $distance;
  268. }
  269.  
  270.  
  271. public function DistAB($lat_a,$lon_a,$lat_b,$lon_b)
  272.  
  273. {
  274.  
  275. $measure_unit = 'kilometers';
  276.  
  277. $measure_state = false;
  278.  
  279. $measure = 0;
  280.  
  281. $error = '';
  282.  
  283. $delta_lat = $lat_b - $lat_a ;
  284. $delta_lon = $lon_b - $lon_a ;
  285. $earth_radius = 6372.795477598;
  286.  
  287. $alpha = $delta_lat/2;
  288. $beta = $delta_lon/2;
  289. $a = sin(deg2rad($alpha)) * sin(deg2rad($alpha)) + cos(deg2rad($this->lat_a)) * cos(deg2rad($this->lat_b)) * sin(deg2rad($beta)) * sin(deg2rad($beta)) ;
  290. $c = asin(min(1, sqrt($a)));
  291. $distance = 2*$earth_radius * $c;
  292. $distance = round($distance, 4);
  293.  
  294. $measure = $distance;
  295. return $measure;
  296.  
  297. }
  298.  
  299. }
  300.  
  301. /* tt end */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement