PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/parts/main.php

https://github.com/MyITCRM/myitcrm1
PHP | 387 lines | 276 code | 65 blank | 46 comment | 158 complexity | 6b62c8f5848f76196f60b81a21077741 MD5 | raw file
  1. <?php
  2. ####################################################
  3. # IN #
  4. # #
  5. # #
  6. # This program is distributed under the terms and #
  7. # conditions of the GPL #
  8. # Parts #
  9. # Version 0.0.1 Sat Nov 26 20:46:40 PST 2005 #
  10. # #
  11. ####################################################
  12. if(!xml2php("parts")) {
  13. $smarty->assign('error_msg',"Error in language file");
  14. }
  15. /* if we have work order assign it */
  16. if(isset($VAR['wo_id'])) {
  17. $smarty->assign('wo_id', $VAR['wo_id']);
  18. }
  19. /* check to see if we have an open order for this WO */
  20. $q = "SELECT count(*) as count FROM ".PRFX."ORDERS WHERE WO_ID=".$db->qstr($VAR['wo_id']);
  21. if(!$rs = $db->execute($q)) {
  22. force_page('core', 'error&error_msg=MySQL Error: '.$db->ErrorMsg().'&menu=1&type=database');
  23. exit;
  24. }
  25. $count = $rs->fields['count'];
  26. if($count > 0) {
  27. force_page('workorder', 'view&wo_id='.$VAR['wo_id'].'&error&error_msg=A parts order already exists for this Work Order. &page_title=Work%20Order%20ID%20'.$VAR['wo_id']);
  28. exit;
  29. }
  30. ##################################
  31. # Load Configs #
  32. ##################################
  33. $q = "SELECT PARTS_LO,PARTS_LOGIN,PARTS_PASSWORD,SERVICE_CODE,PARTS_MARKUP,UPS_LOGIN,UPS_PASSWORD,UPS_ACCESS_KEY FROM ".PRFX."SETUP ";
  34. if(!$rs = $db->execute($q)) {
  35. force_page('core', 'error&error_msg=MySQL Error: '.$db->ErrorMsg().'&menu=1&type=database');
  36. exit;
  37. }
  38. $local = $rs->fields['PARTS_LO'];
  39. $login = $rs->fields['PARTS_LOGIN'];
  40. $passwd = $rs->fields['PARTS_PASSWORD'];
  41. $service_code = $rs->fields['SERVICE_CODE'];
  42. $mark_up = $rs->fields['PARTS_MARKUP'];
  43. $mark_up = $mark_up * .01;
  44. $ups_login = $rs->fields['UPS_LOGIN'];
  45. $ups_password = $rs->fields['UPS_PASSWORD'];
  46. $ups_access_key = $rs->fields['UPS_ACCESS_KEY'];
  47. /* assign service coed to smarty */
  48. if($service_code == "03") {
  49. $smarty->assign('service_code','UPS Ground');
  50. } else if($service_code == "02") {
  51. $smarty->assign('service_code','UPS 2nd Day Air');
  52. } else if($service_code == "01") {
  53. $smarty->assign('service_code','UPS Next Day Air');
  54. } else if($service_code == "07") {
  55. $smarty->assign('service_code','UPS Worldwide Express');
  56. } else if($service_code == "08") {
  57. $smarty->assign('service_code','UPS Worldwide Expedited');
  58. } else if($service_code == "11") {
  59. $smarty->assign('service_code','UPS Standard');
  60. } else if($service_code == "12") {
  61. $smarty->assign('service_code','UPS 3 Day Select');
  62. } else if($service_code == "13") {
  63. $smarty->assign('service_code','UPS Next Day Air Saver');
  64. } else if($service_code == "14") {
  65. $smarty->assign('service_code','UPS Next Day Air Early');
  66. } else if($service_code == "54") {
  67. $smarty->assign('service_code','UPS Worldwide Express Plus');
  68. } else if($service_code == "59") {
  69. $smarty->assign('service_code','UPS 2nd Day Air A.M.');
  70. } else if($service_code == "65") {
  71. $smarty->assign('service_code','UPS Express Saver');
  72. }
  73. /* assign smarty wharehoues location */
  74. if($local == "AT") {
  75. $smarty->assign('location', 'Atlanta');
  76. } else if($local == "CH") {
  77. $smarty->assign('location', 'Chicago');
  78. } else if($local == "DA") {
  79. $smarty->assign('location', 'Dallas');
  80. } else if($local == "FR") {
  81. $smarty->assign('location', 'Fremont');
  82. } else if($local == "HO") {
  83. $smarty->assign('location', 'Houston');
  84. } else if($local == "KA") {
  85. $smarty->assign('location', 'Kansas');
  86. } else if($local == "LR") {
  87. $smarty->assign('location', 'Laredo');
  88. } else if($local == "LA") {
  89. $smarty->assign('location', 'Los Angeles');
  90. } else if($local == "MI") {
  91. $smarty->assign('location', 'Miami');
  92. } else if($local == "NJ") {
  93. $smarty->assign('location', 'New Jersey');
  94. } else if($local == "PO") {
  95. $smarty->assign('location', 'Portland');
  96. } else if($local == "TP") {
  97. $smarty->assign('location', 'Tampa');
  98. }
  99. ##################################
  100. # Load Category #
  101. ##################################
  102. $q = "SELECT * FROM ".PRFX."CAT";
  103. if(!$rs = $db->execute($q)) {
  104. force_page('core', 'error&error_msg=MySQL Error: '.$db->ErrorMsg().'&menu=1&type=database');
  105. exit;
  106. }
  107. $arr = $rs->GetArray();
  108. $smarty->assign( 'CAT', $arr );
  109. $q = "SELECT * FROM ".PRFX."SUB_CAT";
  110. if(!$rs = $db->execute($q)) {
  111. force_page('core', 'error&error_msg=MySQL Error: '.$db->ErrorMsg().'&menu=1&type=database');
  112. exit;
  113. }
  114. $arr = $rs->GetArray();
  115. $smarty->assign( 'SUB_CAT', $arr );
  116. ##################################
  117. # If Submit #
  118. ##################################
  119. if(isset($VAR['submit'])) {
  120. if(!isset($VAR['check_out'])) {
  121. /* get parts */
  122. $x = "<CRM_PARTS_LIST>
  123. <LOGIN>$login</LOGIN>
  124. <PASSWORD>$passwd</PASSWORD>
  125. <SUB_CATEGORY>".$VAR['CAT2']."</SUB_CATEGORY>
  126. <LOCATION>$local</LOCATION>
  127. </CRM_PARTS_LIST>";
  128. $ch = curl_init();
  129. curl_setopt($ch, CURLOPT_URL, INCITCRM);
  130. curl_setopt ($ch, CURLOPT_POST, 1);
  131. curl_setopt ($ch, CURLOPT_POSTFIELDS, "page=parts:list&xml=".$x."&escape=1");
  132. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  133. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  134. $content = curl_exec ($ch); # This returns HTML
  135. curl_close ($ch);
  136. /* return errors */
  137. if($content == 98) {
  138. $smarty->assign('crm_msg', 'Account Login Failed. Please Enter corect login information in the Control Center Under Company Edit. If you do not have an account please click here to create one <a href="https://www.incitecrm.com/?page=sign_up:main&page_title=Sign%20Up" target="new">Create Account</a>. To order parts we must have an active credit card on file. <br><br> If you feel this is an error please verify your account information on In-Cite CRM by logging in here <a href="https://www.incitecrm.com/?page=account:account" target="new">In-cite CRM Login</a>');
  139. } else if($content == 1) {
  140. $smarty->assign('crm_msg', 'Wharehouse Location Not Found. Please Select a Location in the Control Center');
  141. } else if($content == 2) {
  142. $smarty->assign('crm_msg', 'Please Select A category');
  143. } else if($content == 99) {
  144. $smarty->assign('crm_msg', 'Server Error');
  145. }
  146. /* parse Return */
  147. $parser = xml_parser_create();
  148. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  149. xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  150. xml_parse_into_struct($parser, $content, $values, $tags);
  151. xml_parser_free($parser);
  152. /* build array from returned xml */
  153. foreach($values as $xml){
  154. if($xml['tag'] == "SKU" && $xml['value'] != ""){
  155. $sku = array('SKU' => $xml['value']);
  156. }
  157. if($xml['tag'] == "PART_ID" && $xml['value'] != ""){
  158. $part_id = array('ITEMID' => $xml['value']);
  159. }
  160. if($xml['tag'] == "VENDOR" && $xml['value'] != ""){
  161. $vendor = array('VENDOR' => $xml['value']);
  162. }
  163. if($xml['tag'] == "DESCRIPTION" && $xml['value'] != ""){
  164. $description = array('DESCRIPTION' => $xml['value']);
  165. }
  166. if($xml['tag'] == "PRICE" && $xml['value'] != ""){
  167. $price = array('PRICE' => $number = number_format(($xml['value'] * $mark_up) + $xml['value'], 2,'.', '') );
  168. }
  169. if($xml['tag'] == "Weight" && $xml['value'] != ""){
  170. $weight = array('Weight' => $xml['value']);
  171. }
  172. if($xml['tag'] == "ZIPCODE" && $xml['value'] != ""){
  173. $from_zip = $xml['value'];
  174. }
  175. if($xml['tag'] == "PART" && $xml['type'] == "close" ){
  176. $parts[] = array_merge($sku,$part_id,$vendor,$description,$price,$weight);
  177. }
  178. }
  179. $smarty->assign('from_zip',$from_zip);
  180. $smarty->assign( 'parts', $parts );
  181. $smarty->assign('CAT2', $VAR['CAT2']);
  182. }
  183. ###############################
  184. # Add Part #
  185. ###############################
  186. /* if parts where added */
  187. if(isset($VAR['add_part'])) {
  188. if($VAR['AMOUNT'] == '') {
  189. $VAR['AMOUNT'] =1;
  190. }
  191. $sub = $VAR['AMOUNT'] * $VAR['PRICE'];
  192. $q = "INSERT INTO ".PRFX."CART SET
  193. SKU =". $db->qstr($VAR['SKU']) .",
  194. AMOUNT =". $db->qstr($VAR['AMOUNT']) .",
  195. DESCRIPTION =". $db->qstr($VAR['DESCRIPTION']).",
  196. VENDOR =". $db->qstr($VAR['VENDOR']).",
  197. ITEMID =". $db->qstr($VAR['ITEMID']).",
  198. Weight =". $db->qstr($VAR['Weight']).",
  199. PRICE =". $db->qstr($VAR['PRICE']) .",
  200. SUB_TOTAL =". $db->qstr($sub) .",
  201. ZIP =". $db->qstr($VAR['from_zip']) .",
  202. WO_ID =". $db->qstr($VAR['wo_id']) .",
  203. LAST =". time();
  204. if(!$rs = $db->execute($q)) {
  205. force_page('core', 'error&error_msg=MySQL Error: '.$db->ErrorMsg().'&menu=1&type=database');
  206. exit;
  207. }
  208. }
  209. ##################################
  210. # Remove part From Cart #
  211. ##################################
  212. /* if parts where removed */
  213. if(isset($VAR['update_cart'])) {
  214. foreach($VAR['remove'] as $SKU){
  215. $q = "DELETE FROM ".PRFX."CART WHERE SKU=".$db->qstr($SKU);
  216. if(!$rs = $db->execute($q)) {
  217. force_page('core', 'error&error_msg=MySQL Error: '.$db->ErrorMsg().'&menu=1&type=database');
  218. exit;
  219. }
  220. }
  221. }
  222. ##################################
  223. # Check Out #
  224. ##################################
  225. /* if checkout selected */
  226. if(isset($VAR['check_out'])) {
  227. $q = "SELECT * FROM ".PRFX."CART";
  228. if(!$rs = $db->execute($q)) {
  229. force_page('core', 'error&error_msg=MySQL Error: '.$db->ErrorMsg().'&menu=1&type=database');
  230. exit;
  231. }
  232. $arr = $rs->GetArray();
  233. foreach($arr as $key=>$val) {
  234. $sub_total = $sub_total + $val['SUB_TOTAL'];
  235. $from_zip = $val['ZIP'];
  236. $amount = $val['AMOUNT'] * $val['Weight'];
  237. $cart_weight_total = $cart_weight_total + $amount;
  238. }
  239. $q = "SELECT COMPANY_ZIP FROM ".PRFX."TABLE_COMPANY";
  240. if(!$rs = $db->execute($q)) {
  241. force_page('core', 'error&error_msg=MySQL Error: '.$db->ErrorMsg().'&menu=1&type=database');
  242. exit;
  243. }
  244. $to_zip = $rs->fields['COMPANY_ZIP'];
  245. $length = 10;
  246. $width = 10;
  247. $height = 10;
  248. if($ups_login != '') {
  249. $activity = "activity";
  250. $y = "<?xml version=\"1.0\"?><AccessRequest xml:lang=\"en-US\"><AccessLicenseNumber>$ups_access_key</AccessLicenseNumber><UserId>$ups_login</UserId><Password>$ups_password</Password></AccessRequest><?xml version=\"1.0\"?><RatingServiceSelectionRequest xml:lang=\"en-US\"><Request><TransactionReference><CustomerContext>Bare Bones Rate Request</CustomerContext><XpciVersion>1.0</XpciVersion></TransactionReference><RequestAction>Rate</RequestAction><RequestOption>Rate</RequestOption></Request><PickupType><Code>01</Code></PickupType><Shipment><Shipper><Address><PostalCode>$from_zip</PostalCode><CountryCode>US</CountryCode></Address></Shipper><ShipTo><Address><PostalCode>$to_zip</PostalCode><CountryCode>US</CountryCode></Address></ShipTo><ShipFrom><Address><PostalCode>$from_zip</PostalCode><CountryCode>US</CountryCode></Address></ShipFrom><Service><Code>$service_code</Code></Service><Package><PackagingType><Code>02</Code></PackagingType><Dimensions><UnitOfMeasurement><Code>IN</Code></UnitOfMeasurement><Length>$length</Length><Width>$width</Width><Height>$height</Height></Dimensions><PackageWeight><UnitOfMeasurement><Code>LBS</Code></UnitOfMeasurement><Weight>$cart_weight_total</Weight></PackageWeight></Package></Shipment></RatingServiceSelectionRequest>";
  251. // cURL ENGINE
  252. $ch = curl_init(); //initialize a cURL session
  253. curl_setopt ($ch, CURLOPT_URL,"https://www.ups.com/ups.app/xml/Rate");
  254. curl_setopt ($ch, CURLOPT_HEADER, 0);
  255. curl_setopt($ch, CURLOPT_POST, 1);
  256. curl_setopt($ch, CURLOPT_POSTFIELDS, "$y");
  257. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  258. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  259. $response = curl_exec ($ch);
  260. curl_close ($ch);
  261. $parser = xml_parser_create();
  262. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  263. xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  264. xml_parse_into_struct($parser, $response, $values, $tags);
  265. xml_parser_free($parser);
  266. //print_r($values);
  267. foreach($values as $xml){
  268. if($xml['tag'] == "ResponseStatusCode" && $xml['value'] != "" ){
  269. $ResponseStatusCode = array('ResponseStatusCode'=>$xml['value']);
  270. }
  271. if ($xml['tag'] == "ResponseStatusCode" && $xml['value'] != "" ) {
  272. $ResponseStatusCode = $xml['value'];
  273. }
  274. if ($xml['tag'] == "ErrorDescription" && $xml['value'] != "" ) {
  275. $ErrorDescription = $xml['value'];
  276. }
  277. if($xml['tag'] == "MonetaryValue" && $xml['value'] != "" ){
  278. $MonetaryValue = array('MonetaryValue'=>$xml['value']);
  279. }
  280. if($xml['tag'] == "GuaranteedDaysToDelivery" && $xml['value'] != "" ){
  281. $GuaranteedDaysToDelivery = array('GuaranteedDaysToDelivery'=>$xml['value']);
  282. }
  283. if($xml['tag'] == "RatedShipment" && $xml['type'] == "close" ){
  284. $rate[] = array_merge($ResponseStatusCode,$MonetaryValue,$GuaranteedDaysToDelivery);
  285. }
  286. }
  287. $shipping_charges = number_format($rate[0]['MonetaryValue'], 2, '.', '');
  288. $total_charges = $sub_total + $rate[0]['MonetaryValue'];
  289. } else {
  290. $shipping_charges = '0.00';
  291. $total_charges = $sub_total;
  292. $ResponseStatusCode = 1;
  293. $ErrorDescription = 'You have not set up UPS information';
  294. }
  295. /* get Cart Total */
  296. $smarty->assign('ResponseStatusCode',$ResponseStatusCode);
  297. $smarty->assign('ErrorDescription',$ErrorDescription);
  298. $smarty->assign('sub_total',number_format($sub_total, 2, '.', ''));
  299. $smarty->assign('shipping_charges',$shipping_charges);
  300. $smarty->assign('total_charges', $total_charges);
  301. $smarty->assign('cart_weight_total', $cart_weight_total);
  302. $smarty->assign('cart_contents', $arr);
  303. }
  304. }
  305. ##################################
  306. # Get Cart Contents #
  307. ##################################
  308. $q = "SELECT * FROM ".PRFX."CART";
  309. if(!$rs = $db->execute($q)) {
  310. force_page('core', 'error&error_msg=MySQL Error: '.$db->ErrorMsg().'&menu=1&type=database');
  311. exit;
  312. }
  313. $arr = $rs->GetArray();
  314. //print_r($arr);
  315. foreach($arr as $key=>$val) {
  316. $cart_sub_total = $cart_sub_total + $val['SUB_TOTAL'];
  317. }
  318. $smarty->assign('cart_total',number_format($cart_sub_total, 2, '.', ''));
  319. $smarty->assign('cart_count',count($arr));
  320. $smarty->assign('cart', $arr);
  321. $smarty->display('parts'.SEP.'main.tpl');
  322. ?>