PageRenderTime 62ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/admin/shippingprocess2.php

https://bitbucket.org/chadsaun/ifrogz-panel
PHP | 774 lines | 555 code | 88 blank | 131 comment | 95 complexity | 9c4f485458c293c9f2d9554adad98271 MD5 | raw file
  1. <?php
  2. // Checking change
  3. include('init.php');
  4. include(APPPATH.'views/partials/admin/dbconnection.php');
  5. include(APPPATH.'views/partials/admin/functions.php');
  6. session_register('error_order');
  7. session_register('error_msg');
  8. session_register('error_msg2');
  9. session_register('error_status');
  10. // BARCODE SCANNER LOOP AND THEN MANUAL RECEIVE LOOP
  11. if(!empty($_POST['recOrdersScan'])) {
  12. $_SESSION['error_order'] = array();
  13. if($_POST['recLocationScan']=='usps'){
  14. $nextStatus = 7;
  15. }elseif($_POST['recLocationScan']=='fedex') {
  16. $nextStatus = 8;
  17. }else{
  18. $nextStatus = 9;
  19. }
  20. $_SESSION['error_status'] = strtoupper($_POST['recLocationScan']);
  21. $arr=explode("\r\n",trim($_POST['receive_barcode']));
  22. foreach($arr as $key => $value) {
  23. $thisStatus = $nextStatus;
  24. $ordStatus = getShipStatus($value);
  25. if($ordStatus == 'USPS' && $thisStatus != 7) {
  26. array_push($_SESSION['error_order'],array('ordID' => $value, 'shipType' => 'USPS', 'statusNum' => $thisStatus));
  27. }elseif($ordStatus == 'FedEx' && $thisStatus != 8) {
  28. array_push($_SESSION['error_order'],array('ordID' => $value, 'shipType' => 'FedEx Express', 'statusNum' => $thisStatus));
  29. }elseif($ordStatus == 'INTL' && $thisStatus != 9) {
  30. array_push($_SESSION['error_order'],array('ordID' => $value, 'shipType' => 'International', 'statusNum' => $thisStatus));
  31. }else{
  32. $qry = "UPDATE orders
  33. SET ordStatus = '$thisStatus', ordStatusDate = '".date("Y-m-d H:i:s", time())."'
  34. WHERE ordID = '$value'";
  35. $res = mysql_query($qry) or print(mysql_error());
  36. if($res) {
  37. if(!setNewLocation( $thisStatus , $value )) print("Unable to record location.");
  38. }
  39. }
  40. }
  41. if(!empty($_SESSION['error_order'])) {
  42. header("Location: /admin/shippingerror.php");
  43. exit();
  44. }else{
  45. $_SESSION['error_order'] = '';
  46. $_SESSION['error_status'] = '';
  47. }
  48. }
  49. elseif(!empty($_POST['recOrders'])) { // START OF MANUAL RECEIVE LOOP
  50. $_SESSION['error_order'] = array();
  51. $_SESSION['error_status'] = strtoupper($_POST['recLocation']);
  52. foreach($_POST as $key => $value) {
  53. if($_POST['recLocation']=='usps'){
  54. $nextStatus = 7;
  55. }elseif($_POST['recLocation']=='fedex') {
  56. $nextStatus = 8;
  57. }else{
  58. $nextStatus = 6;
  59. }
  60. if(substr($key,0,7)=='receive') {
  61. if($value=='yes') {
  62. $order = substr($key,7);
  63. $ordStatus = getShipStatus($order);
  64. if($ordStatus == 'USPS' && $nextStatus != 7) {
  65. array_push($_SESSION['error_order'],array('ordID' => $order, 'shipType' => 'USPS', 'statusNum' => $nextStatus));
  66. }elseif($ordStatus == 'FedEx' && $nextStatus != 8) {
  67. array_push($_SESSION['error_order'],array('ordID' => $order, 'shipType' => 'FedEx Express', 'statusNum' => $nextStatus));
  68. }elseif($ordStatus == 'INTL' && $nextStatus != 9) {
  69. array_push($_SESSION['error_order'],array('ordID' => $order, 'shipType' => 'International', 'statusNum' => $nextStatus));
  70. }else{
  71. $qry = "UPDATE orders
  72. SET ordStatus = '$nextStatus', ordStatusDate = '".date("Y-m-d H:i:s", time())."'
  73. WHERE ordID = '$order'";
  74. $res = mysql_query($qry) or print(mysql_error());
  75. if($res) {
  76. if(!setNewLocation( $nextStatus , $order )) print("Unable to record location.");
  77. }
  78. }
  79. }
  80. }
  81. }
  82. if(!empty($_SESSION['error_order'])) {
  83. header("Location: /admin/shippingerror.php");
  84. exit();
  85. }else{
  86. $_SESSION['error_order'] = '';
  87. $_SESSION['error_status'] = '';
  88. }
  89. }elseif(!empty($_POST['create'])) {
  90. if($_POST['status']=='7') { // Shipping USPS
  91. // Validate the BOL#s
  92. /*if(!ereg("^[[:digit:]|[:space:]]+$",stripslashes($_POST['exBOL']))) {
  93. $_SESSION['error_msg2'] = 'The Express BOL# can only contain numbers and spaces and must not be empty.';
  94. header("Location: /admin/shipping2.php");
  95. exit();
  96. }
  97. if(!ereg("^[[:digit:]|[:space:]]+$",stripslashes($_POST['smBOL']))) {
  98. $_SESSION['error_msg2'] = 'The Smart Mail BOL# can only contain numbers and spaces and must not be empty.';
  99. header("Location: /admin/shipping2.php");
  100. exit();
  101. }*/
  102. // GET ALL THE ORDERS FOR THE SELECTED STATUS
  103. $qry = "SELECT * FROM orders WHERE ordStatus = '".$_POST['status']."'";
  104. $res = mysql_query($qry) or print(mysql_error());
  105. if(mysql_num_rows($res)>0) {
  106. $shipAddress = '';
  107. $shipAddress2 = '';
  108. $shipCity = '';
  109. $shipState = '';
  110. $shipZip = '';
  111. $shipCountry = '';
  112. // Create the new file name
  113. chdir($_SERVER['DOCUMENT_ROOT'] . '/batches/usps');
  114. $file_not_found=true;
  115. $temp = rightnow();
  116. $file_ext = 0;
  117. do{
  118. $file_new = 'ifrogz'.$file_ext.'_'.date('ymdH').'.csv';
  119. //$inv_ref = substr($temp,5,2).substr($temp,8,2).$file_ext;
  120. if(is_file($file_new)) $file_ext++;
  121. else $file_not_found=false;
  122. }while($file_not_found);
  123. $totWeight = 0;
  124. $totQuantity = 0;
  125. $csv_str='"","","ordID","","ordName","ordName","shipAddress","shipAddress2","shipCity","shipState","shipZip","weight","","","","",""'."\r\n";
  126. while($row=mysql_fetch_assoc($res)) {
  127. // Insert the BOL#s into the database for the order
  128. /* $sql3 = "INSERT INTO bol ( ordID , exBOL , smBOL )
  129. VALUES ( ".$row['ordID']." , '".mysql_real_escape_string($_POST['exBOL'])."' , ".mysql_real_escape_string($_POST['smBOL'])." )";
  130. $res3 = mysql_query($sql3) or print(mysql_error());
  131. */
  132. // Get product information from the order so that we can calculate the total weight of the order and total quantity
  133. $qry2 = "SELECT cart.cartProdID, p.pWeight, cart.cartQuantity, p.pPricing_group, p.pBinQty
  134. FROM cart, products p, orders o
  135. WHERE o.ordID = cart.cartOrderID
  136. AND cart.cartProdID = p.pID
  137. AND o.ordID = ".$row['ordID'];
  138. $res2 = mysql_query($qry2) or print(mysql_error());
  139. while($row2 = mysql_fetch_assoc($res2)) {
  140. $weight += $row2['cartQuantity'] * $row2['pWeight'];
  141. // IF THE PRODUCT IS A SET, MULTIPLY BY 3 TO GET 3 FOR THE QUANTITY INSTEAD OF 1
  142. if($row2['cartProdID']=='AHX' || $row2['cartProdID']=='BJZ' || $row2['cartProdID']=='AHX') {
  143. $quantity += ($row2['cartQuantity'])*3;
  144. }else {
  145. $quantity += $row2['cartQuantity'];
  146. }
  147. // subtract inventory from any orders with shieldzone products
  148. if($row2['pPricing_group']=='3'){
  149. $sql_update="UPDATE products SET pBinQty=pBinQty-".$row2['cartQuantity']." WHERE pID='".$row2['cartProdID']."'";
  150. $result_plot=mysql_query($sql_update) or print(mysql_error());
  151. updateplotterhistory($row2['cartProdID'],$row2['pBinQty'],($row2['pBinQty']-$row2['cartQuantity']),$_SESSION['employee']['id'],$file_new);
  152. }
  153. }
  154. $totWeight += $weight;
  155. $totQuantity += $quantity;
  156. if(!empty($row['ordShipAddress']) && !empty($row['ordShipZip'])) { // Use Shipping Address
  157. $shipAddress = $row['ordShipAddress'];
  158. $shipAddress2 = $row['ordShipAddress2'];
  159. $shipCity = $row['ordShipCity'];
  160. $shipState = $row['ordShipState'];
  161. $shipZip = $row['ordShipZip'];
  162. $shipCountry = $row['ordShipCountry'];
  163. }else{ // Use Billing Address
  164. $shipAddress = $row['ordAddress'];
  165. $shipAddress2 = $row['ordAddress2'];
  166. $shipCity = $row['ordCity'];
  167. $shipState = $row['ordState'];
  168. $shipZip = $row['ordZip'];
  169. $shipCountry = $row['ordCountry'];
  170. }
  171. $csv_str.='"'.$row['ordShipType'].'","","'.$row['ordID'].'","","'.$row['ordName'].'.","'.$row['ordName'].'","'.$shipAddress.'","'.$shipAddress2.'","'.$shipCity.'","'.$shipState.'","'.$shipZip.'","'.$weight.'","","","","",""'."\r\n";
  172. // Update the order's status to 'Shipping'
  173. $qryUpdate = "UPDATE orders
  174. SET ordStatus = '11', ordStatusDate = '".date("Y-m-d H:i:s")."'
  175. WHERE ordID = '".$row['ordID']."'";
  176. $resUpdate = mysql_query($qryUpdate) or print(mysql_error());
  177. if($resUpdate) {
  178. //google sent cust status change
  179. $goid=$row['ordID'];
  180. $gship='ship';
  181. include(DOCROOT.'admin/ajaxservice.php');
  182. if(!setNewLocation( 11 , $row['ordID'] )) print("Unbale to record location.");
  183. // SEND CUSTOMER AN EMAIL NOTIFYING THEM THAT THEIR ORDER HAS SHIPPED
  184. if(!$isgoogleorder) notifyCustShipped($row['ordID']);
  185. }
  186. }
  187. // Create the dat file
  188. if (!($fp = fopen($file_new, "w+")))
  189. exit("Unable to open the input file.");
  190. fwrite($fp,$csv_str);
  191. fclose($fp);
  192. chdir($_SERVER['DOCUMENT_ROOT']);
  193. // FTP the dat file to DHL
  194. /*$connID = ftpLogin("ftp.smartmail.com","ftpuserrband",'5g$Xpr!4');
  195. if($connID) {
  196. if(ftpUpload($connID,$file_new,"batches/dhl/".$file_new)) {
  197. // Record that the file has been uploaded to DHL
  198. $sql = "INSERT INTO dhl_files ( filename , status , date )
  199. VALUES ( '".mysql_real_escape_string($file_new)."' , 'uploaded' , '".date("Y-m-d H:i:s")."' )";
  200. $res = mysql_query($sql);
  201. }
  202. }*/
  203. $success = 'Batch '.$file_new.' created';
  204. header('Location: /admin/shipping2.php?success='.$success);
  205. exit();
  206. }
  207. }elseif($_POST['status']=='8') { // FedEx Express
  208. // GET ALL THE ORDERS FOR THE SELECTED STATUS
  209. $qry1 = "SELECT * FROM orders WHERE ordStatus = '8'";
  210. $res1 = mysql_query($qry1) or print(mysql_error());
  211. /*$qry2 = "SELECT * FROM orders WHERE ordStatus = '9'";
  212. $res2 = mysql_query($qry2) or print(mysql_error());*/
  213. // Create the new file name
  214. chdir($_SERVER['DOCUMENT_ROOT'] . '/batches/fedex');
  215. $file_not_found=true;
  216. $temp = rightnow();
  217. $file_ext = 0;
  218. do{
  219. $file_new = 'ifrogz-'.substr($temp,5,2).'-'.substr($temp,8,2).'-'.$file_ext.'.in';
  220. $inv_ref = substr($temp,5,2).substr($temp,8,2).$file_ext;
  221. if(is_file($file_new)) $file_ext++;
  222. else $file_not_found=false;
  223. }while($file_not_found);
  224. if(mysql_num_rows($res1)>0 || mysql_num_rows($res2)>0) {
  225. $shipAddress = '';
  226. $shipAddress2 = '';
  227. $shipCity = '';
  228. $shipState = '';
  229. $shipZip = '';
  230. $shipCountry = '';
  231. $produce_date = date("Ymd",mktime(date("H")+14, date("i"), date("s"), date("m"), date("d"), date("Y")));
  232. // MASTER RECORD
  233. $fileBuffer = '';
  234. /* $fileBuffer .= "\r\n";
  235. $fileBuffer .= '0,"020"'."\r\n";
  236. $fileBuffer .= '1,"MASTER"'."\r\n";
  237. $fileBuffer .= '19,"IPD-IND"'."\r\n";
  238. $fileBuffer .= '20,"339676500"'."\r\n";
  239. $fileBuffer .= '23,"3"'."\r\n";
  240. $fileBuffer .= '24,"'.$produce_date.'"'."\r\n";
  241. $fileBuffer .= '31,"New Fortune"'."\r\n";
  242. $fileBuffer .= '68,"USD"'."\r\n";
  243. $fileBuffer .= '70,"3"'."\r\n";
  244. $fileBuffer .= '71,"339676500"'."\r\n";
  245. $fileBuffer .= '75,"KGS"'."\r\n";
  246. $fileBuffer .= '79,"Accessory (HS Code 4202190000)"'."\r\n";
  247. $fileBuffer .= '541,"YNNNNNNNN"'."\r\n";
  248. $fileBuffer .= '1273,"01"'."\r\n";
  249. $fileBuffer .= '1274,"18"'."\r\n";
  250. $fileBuffer .= '1586,"Y"'."\r\n";
  251. $fileBuffer .= '1486,"ifrogz.com"'."\r\n";
  252. $fileBuffer .= '1487,"919 N 1000 W"'."\r\n";
  253. $fileBuffer .= '1488,""'."\r\n";
  254. $fileBuffer .= '1489,"Logan"'."\r\n";
  255. $fileBuffer .= '1490,"UT"'."\r\n";
  256. $fileBuffer .= '1491,"84321"'."\r\n";
  257. $fileBuffer .= '1492,"1-877-443-7641"'."\r\n";
  258. $fileBuffer .= '1585,"US"'."\r\n";
  259. $fileBuffer .= '99,""'."\r\n";*/
  260. while($row1=mysql_fetch_assoc($res1)) {
  261. if(!empty($row1['ordShipAddress']) && !empty($row1['ordShipZip'])) { // Use Shipping Address
  262. $name = $row1['ordShipName'];
  263. $shipAddress = $row1['ordShipAddress'];
  264. $shipAddress2 = $row1['ordShipAddress2'];
  265. $shipCity = $row1['ordShipCity'];
  266. $shipState = $row1['ordShipState'];
  267. $shipZip = $row1['ordShipZip'];
  268. $shipCountry = $row1['ordShipCountry'];
  269. }else{ // Use Billing Address
  270. $name = $row1['ordName'];
  271. $shipAddress = $row1['ordAddress'];
  272. $shipAddress2 = $row1['ordAddress2'];
  273. $shipCity = $row1['ordCity'];
  274. $shipState = $row1['ordState'];
  275. $shipZip = $row1['ordZip'];
  276. $shipCountry = $row1['ordCountry'];
  277. }
  278. // GET COUNTRY CODE
  279. $qry_cc = "SELECT * FROM countries
  280. WHERE countryName = '$shipCountry'";
  281. $res_cc = mysql_query($qry_cc);
  282. $row_cc = mysql_fetch_assoc($res_cc);
  283. $shipCountryCode = $row_cc['countryCode'];
  284. // Get product information from the order so that we can calculate the total weight of the order and total quantity
  285. $qry2 = "SELECT p.pWeight, cart.cartQuantity, cart.cartProdID, p.isSet, p.pPricing_group, p.pBinQty, p.pCustomsvalue
  286. FROM cart, products p, orders o
  287. WHERE o.ordID = cart.cartOrderID
  288. AND cart.cartProdID = p.pID
  289. AND o.ordID = ".$row1['ordID'];
  290. $res2 = mysql_query($qry2) or print(mysql_error());
  291. $totWeight = 0;
  292. $totQuantity = 0;
  293. $totPrice = 0;
  294. $cost = 0;
  295. while($row2 = mysql_fetch_assoc($res2)) {
  296. $totWeight += $row2['cartQuantity'] * $row2['pWeight'];
  297. $totQuantity += $row2['cartQuantity'];
  298. $totPrice += $row2['cartProdPrice'];
  299. $cost += $row2['pCustomsvalue'];
  300. // subtract inventory from any orders with shieldzone products
  301. if($row2['pPricing_group']=='3'){
  302. $sql_update="UPDATE products SET pBinQty=pBinQty-".$row2['cartQuantity']." WHERE pID='".$row2['cartProdID']."'";
  303. $result_plot=mysql_query($sql_update) or print(mysql_error());
  304. updateplotterhistory($row2['cartProdID'],$row2['pBinQty'],($row2['pBinQty']-$row2['cartQuantity']),$_SESSION['employee']['id'],$file_new);
  305. }
  306. }
  307. $totWeight += .014;
  308. $totWeight *= 10;
  309. $duties = round($totPrice,2);
  310. $duties = str_replace('.','',$totPrice);
  311. if($totQuantity==0) $avg_cost = 0;
  312. else $avg_cost = $cost / $totQuantity;
  313. // Format for FedEx
  314. $avg_cost = round($avg_cost,2);
  315. $avg_cost *= 1000000;
  316. $avg_cost = str_replace('.','',$avg_cost);
  317. // Update the order's status to 'Shipping'
  318. $qryUpdate = "UPDATE orders
  319. SET ordStatus = '11', ordStatusDate = '".date("Y-m-d H:i:s")."'
  320. WHERE ordID = '".$row1['ordID']."'";
  321. $resUpdate = mysql_query($qryUpdate) or print(mysql_error());
  322. if($resUpdate) {
  323. //google sent cust status change
  324. $goid=$row1['ordID'];
  325. $gship='ship';
  326. include(DOCROOT.'admin/ajaxservice.php');
  327. if(!setNewLocation( 11 , $row1['ordID'] )) print("Unable to record location.");
  328. }
  329. // SEND CUSTOMER AN EMAIL NOTIFYING THEM THAT THEIR ORDER HAS SHIPPED
  330. if(!$isgoogleorder) notifyCustShipped($row1['ordID']);
  331. $fileBuffer .= "";
  332. $fileBuffer .= '0,"021"'."\r\n";
  333. $fileBuffer .= '1,"'.$row1['ordID'].'"'."\r\n";
  334. $fileBuffer .= '10,"311882961"'."\r\n";//Sender FedEx Account Number
  335. $fileBuffer .= '498,"236475"'."\r\n";//Meter Number
  336. $fileBuffer .= '4,"reminderband.com"'."\r\n";//Sender Company
  337. $fileBuffer .= '5,"919 N 1000 W"'."\r\n";//Sender Address Line 1
  338. $fileBuffer .= '6,"Suite 107"'."\r\n";//Sender Address Line 2
  339. $fileBuffer .= '7,"Logan"'."\r\n";//Sender City
  340. $fileBuffer .= '8,"UT"'."\r\n";//Sender State
  341. $fileBuffer .= '9,"84321"'."\r\n";//Sender Postal Code
  342. $fileBuffer .= '117,"US"'."\r\n";//Sender Country Code
  343. $fileBuffer .= '183,"8774437641"'."\r\n";//Sender Phone Number
  344. $fileBuffer .= '12,"'.substr($name,0,35).'"'."\r\n";
  345. $fileBuffer .= '13,"'.$shipAddress.'"'."\r\n";
  346. $fileBuffer .= '14,"'.$shipAddress2.'"'."\r\n";
  347. $fileBuffer .= '15,"'.$shipCity.'"'."\r\n";
  348. $fileBuffer .= '16,"'.$shipState.'"'."\r\n";
  349. $fileBuffer .= '17,"'.$shipZip.'"'."\r\n";
  350. $fileBuffer .= '18,"'.$row1['ordPhone'].'"'."\r\n";
  351. $fileBuffer .= '50,"'.$shipCountryCode.'"'."\r\n";
  352. $fileBuffer .= '23,"1"'."\r\n";//Pay Type
  353. $fileBuffer .= '24,"'.$produce_date.'"'."\r\n";//Ship Date
  354. $fileBuffer .= '25,"Order:'.$row1['ordID'].'"'."\r\n";//Reference Information
  355. //$fileBuffer .= '116,"1"'."\r\n";//Package Total(Total Number of Pieces)
  356. $fileBuffer .= '75,"KGS"'."\r\n";//weight units
  357. //$fileBuffer .= '1273,"01"'."\r\n";//package type
  358. $fileBuffer .= '3025,"FDXE"'."\r\n";//Carrier Code
  359. $fileBuffer .= '99,""'."\r\n";
  360. //$fileBuffer .= '1274,"18"'."\r\n";//service type
  361. //$fileBuffer .= '21,"'.$totWeight.'"'."\r\n";//weight
  362. //$fileBuffer .= '1030,"'.$avg_cost.'"'."\r\n";
  363. //$fileBuffer .= '82,"'.$totQuantity.'"'."\r\n";//unit quantity
  364. /*$fileBuffer .= "\r\n";
  365. $fileBuffer .= '0,"020"'."\r\n";
  366. $fileBuffer .= '1,"'.$row1['ordID'].'"'."\r\n";
  367. $fileBuffer .= '11,""'."\r\n";
  368. $fileBuffer .= '12,"'.substr($name,0,35).'"'."\r\n";
  369. $fileBuffer .= '13,"'.$shipAddress.'"'."\r\n";
  370. $fileBuffer .= '14,"'.$shipAddress2.'"'."\r\n";
  371. $fileBuffer .= '15,"'.$shipCity.'"'."\r\n";
  372. $fileBuffer .= '16,"'.$shipState.'"'."\r\n";
  373. $fileBuffer .= '17,"'.$shipZip.'"'."\r\n";
  374. $fileBuffer .= '18,"'.$row1['ordPhone'].'"'."\r\n";
  375. $fileBuffer .= '50,"'.$shipCountryCode.'"'."\r\n";
  376. $fileBuffer .= '24,"'.$produce_date.'"'."\r\n";
  377. $fileBuffer .= '25,"Order:'.$row1['ordID'].'"'."\r\n";//Reference Information
  378. $fileBuffer .= '81,"4202190000"'."\r\n";//Harmonized Code
  379. $fileBuffer .= '541,"NNNYNNNNN"'."\r\n";
  380. $fileBuffer .= '1273,"01"'."\r\n";//package type
  381. $fileBuffer .= '1274,"18"'."\r\n";//service type
  382. $fileBuffer .= '3025,"FDXE"'."\r\n";
  383. $fileBuffer .= '116,"1"'."\r\n";//Package Total(Total Number of Pieces)
  384. $fileBuffer .= '21,"'.$totWeight.'"'."\r\n";//weight
  385. $fileBuffer .= '1030,"'.$avg_cost.'"'."\r\n";
  386. $fileBuffer .= '82,"'.$totQuantity.'"'."\r\n";//unit quantity
  387. $fileBuffer .= '79,"Accessory (HS Code 4202190000)"'."\r\n";
  388. $fileBuffer .= '80,"CN"'."\r\n";
  389. $fileBuffer .= '99,""'."\r\n";*/
  390. }
  391. // CREATE FEDEX INTERNATIONAL RECORD
  392. $sql_intl = "SELECT * FROM orders WHERE ordStatus = 9";
  393. $res_intl = mysql_query($sql_intl) or print(mysql_error());
  394. $totWeight = 0;
  395. $totQuantity = 0;
  396. $totPrice = 0;
  397. $cost = 0;
  398. while($row_intl = mysql_fetch_assoc($res_intl)) {
  399. // Get product information from the order so that we can calculate the total weight of the order and total quantity
  400. $qry2 = "SELECT p.pWeight, cart.cartQuantity, cart.cartProdID, p.isSet, p.pPricing_group, p.pBinQty, p.pCustomsvalue
  401. FROM cart, products p, orders o
  402. WHERE o.ordID = cart.cartOrderID
  403. AND cart.cartProdID = p.pID
  404. AND o.ordID = ".$row_intl['ordID'];
  405. $res2 = mysql_query($qry2) or print(mysql_error());
  406. while($row2 = mysql_fetch_assoc($res2)) {
  407. $totWeight += $row2['cartQuantity'] * $row2['pWeight'];
  408. $totQuantity += $row2['cartQuantity'];
  409. $totPrice += $row2['cartProdPrice'];
  410. $cost += $row2['pCustomsvalue'];
  411. // subtract inventory from any orders with shieldzone products
  412. if($row2['pPricing_group']=='3'){
  413. $sql_update="UPDATE products SET pBinQty=pBinQty-".$row2['cartQuantity']." WHERE pID='".$row2['cartProdID']."'";
  414. $result_plot=mysql_query($sql_update) or print(mysql_error());
  415. updateplotterhistory($row2['cartProdID'],$row2['pBinQty'],($row2['pBinQty']-$row2['cartQuantity']),$_SESSION['employee']['id'],$file_new);
  416. }
  417. }
  418. // Update the order's status to 'Shipping'
  419. $qryUpdate = "UPDATE orders
  420. SET ordStatus = '11', ordStatusDate = '".date("Y-m-d H:i:s")."'
  421. WHERE ordID = '".$row_intl['ordID']."'";
  422. $resUpdate = mysql_query($qryUpdate) or print(mysql_error());
  423. if($resUpdate) {
  424. //google sent cust status change
  425. $goid=$row_intl['ordID'];
  426. $gship='ship';
  427. include(DOCROOT.'admin/ajaxservice.php');
  428. if(!setNewLocation( 11 , $row_intl['ordID'] )) print("Unable to record location.");
  429. }
  430. // SEND CUSTOMER AN EMAIL NOTIFYING THEM THAT THEIR ORDER HAS SHIPPED
  431. if(!$isgoogleorder)notifyCustShipped($row_intl['ordID']);
  432. }
  433. $totWeight += .014;
  434. $totWeight *= 10;
  435. if($totQuantity==0) $avg_cost = 0;
  436. else $avg_cost = $cost / $totQuantity;
  437. // Format for FedEx
  438. $avg_cost = round($avg_cost,2);
  439. $avg_cost *= 1000000;
  440. $avg_cost = str_replace('.','',$avg_cost);
  441. if(mysql_num_rows($res_intl) > 0) {
  442. $fileBuffer .= "\r\n";
  443. $fileBuffer .= '0,"020"'."\r\n";
  444. $fileBuffer .= '1,"'.date("ndHis").'.in"'."\r\n";
  445. $fileBuffer .= '11,""'."\r\n";
  446. $fileBuffer .= '12,"Martin Cordero"'."\r\n";
  447. $fileBuffer .= '13,"FIMS Mail Center"'."\r\n";
  448. $fileBuffer .= '14,"700 Dowd Avenue"'."\r\n";
  449. $fileBuffer .= '15,"Elizabeth"'."\r\n";
  450. $fileBuffer .= '16,"New Jersey"'."\r\n";
  451. $fileBuffer .= '17,"07201"'."\r\n";
  452. $fileBuffer .= '18,""'."\r\n";
  453. $fileBuffer .= '50,"US"'."\r\n";
  454. $fileBuffer .= '24,"'.$produce_date.'"'."\r\n";
  455. $fileBuffer .= '81,"4202190000"'."\r\n";
  456. $fileBuffer .= '541,"NNNYNNNNN"'."\r\n";
  457. $fileBuffer .= '1273,"01"'."\r\n";
  458. $fileBuffer .= '1274,"18"'."\r\n";
  459. $fileBuffer .= '1355,"IPD"'."\r\n";
  460. $fileBuffer .= '116,"1"'."\r\n";
  461. $fileBuffer .= '21,"'.$totWeight.'"'."\r\n";
  462. $fileBuffer .= '1030,"'.$avg_cost.'"'."\r\n";
  463. $fileBuffer .= '82,"'.$totQuantity.'"'."\r\n";
  464. $fileBuffer .= '79,"Accessory (HS Code 4202190000)"'."\r\n";
  465. $fileBuffer .= '80,"CN"'."\r\n";
  466. $fileBuffer .= '99,""'."\r\n";
  467. }
  468. // Create the trans.in file
  469. if (!($fp = fopen($file_new, "w+")))
  470. exit("Unable to open the input file.");
  471. fwrite($fp,$fileBuffer);
  472. fclose($fp);
  473. chdir($_SERVER['DOCUMENT_ROOT']);
  474. $success = 'Batch '.$file_new.' created';
  475. header('Location: /admin/shipping2.php?status=FedEx&success='.$success);
  476. exit();
  477. }
  478. }else{ // INTL
  479. // Validate the BOL#s
  480. if(!ereg("^[[:digit:]|[:space:]]+$",$_POST['exBOL'])) {
  481. $_SESSION['error_msg2'] = 'The Express BOL# can only contain numbers and spaces and must not be empty.';
  482. header("Location: /admin/shipping2.php");
  483. exit();
  484. }
  485. if(!ereg("^[[:digit:]|[:space:]]+$",$_POST['gmBOL'])) {
  486. $_SESSION['error_msg2'] = 'The Global Mail BOL# can only contain numbers and spaces and must not be empty.';
  487. header("Location: /admin/shipping2.php");
  488. exit();
  489. }
  490. // GET ALL THE ORDERS FOR THE SELECTED STATUS
  491. $qry = "SELECT * FROM orders WHERE ordStatus = '".$_POST['status']."'";
  492. $res = mysql_query($qry) or print(mysql_error());
  493. if(mysql_num_rows($res)>0) {
  494. $shipAddress = '';
  495. $shipAddress2 = '';
  496. $shipCity = '';
  497. $shipState = '';
  498. $shipZip = '';
  499. $shipCountry = '';
  500. $GMAcct = '12345678';
  501. // Create the new file name
  502. chdir($_SERVER['DOCUMENT_ROOT'] . '/batches/intl');
  503. $file_not_found=true;
  504. $temp = rightnow();
  505. $file_ext = 0;
  506. do{
  507. $file_new = $GMAcct.'_'.date('Ymd').'_parcel'.$file_ext.'.dat';
  508. //$inv_ref = substr($temp,5,2).substr($temp,8,2).$file_ext;
  509. if(is_file($file_new)) $file_ext++;
  510. else $file_not_found=false;
  511. }while($file_not_found);
  512. $strCSV = '';
  513. while($row=mysql_fetch_assoc($res)) {
  514. // Insert the BOL#s into the database for the order
  515. $sql3 = "INSERT INTO bol ( ordID , exBOL , gmBOL )
  516. VALUES ( ".$row['ordID']." , '".mysql_real_escape_string($_POST['exBOL'])."' , ".mysql_real_escape_string($_POST['gmBOL'])." )";
  517. $res3 = mysql_query($sql3) or print(mysql_error());
  518. if(!empty($row['ordShipAddress']) && !empty($row['ordShipZip'])) { // Use Shipping Address
  519. $shipAddress = $row['ordShipAddress'];
  520. $shipAddress2 = $row['ordShipAddress2'];
  521. $shipCity = $row['ordShipCity'];
  522. $shipState = $row['ordShipState'];
  523. $shipZip = $row['ordShipZip'];
  524. $shipCountry = $row['ordShipCountry'];
  525. }else{ // Use Billing Address
  526. $shipAddress = $row['ordAddress'];
  527. $shipAddress2 = $row['ordAddress2'];
  528. $shipCity = $row['ordCity'];
  529. $shipState = $row['ordState'];
  530. $shipZip = $row['ordZip'];
  531. $shipCountry = $row['ordCountry'];
  532. }
  533. // Get the country code
  534. $sql = "SELECT * FROM countries WHERE countryName = '".$shipCountry."'";
  535. $ctry_res = mysql_query($sql) or print(mysql_error());
  536. $ctry_row = mysql_fetch_assoc($ctry_res);
  537. $ctryCode = $ctry_row['countryCode'];
  538. // Get product information from the order so that we can calculate the total weight of the order
  539. $qry2 = "SELECT p.pWeight, p.pPrice, cart.cartQuantity, cart.cartProdID, p.isSet, p.pPricing_group, p.pBinQty
  540. FROM cart, products p, orders o
  541. WHERE o.ordID = cart.cartOrderID
  542. AND cart.cartProdID = p.pID
  543. AND o.ordID = ".$row['ordID'];
  544. $res2 = mysql_query($qry2) or print(mysql_error());
  545. $totWeight = 0;
  546. $totQuantity = 0;
  547. while($row2 = mysql_fetch_assoc($res2)) {
  548. // IF THE PRODUCT IS A SET, MULTIPLY BY 3 TO GET 3 FOR THE QUANTITY INSTEAD OF 1
  549. if($row2['isSet']=='yes') {
  550. $totQuantity += $row2['cartQuantity']*3;
  551. }else {
  552. $totQuantity += $row2['cartQuantity'];
  553. }
  554. $totWeight += $row2['cartQuantity'] * $row2['pWeight'];
  555. // subtract inventory from any orders with shieldzone products
  556. if($row2['pPricing_group']=='3'){
  557. $sql_update="UPDATE products SET pBinQty=pBinQty-".$row2['cartQuantity']." WHERE pID='".$row2['cartProdID']."'";
  558. $result_plot=mysql_query($sql_update) or print(mysql_error());
  559. updateplotterhistory($row2['cartProdID'],$row2['pBinQty'],($row2['pBinQty']-$row2['cartQuantity']),$_SESSION['employee']['id'],$file_new);
  560. }
  561. }
  562. $qry3 = "SELECT p.pWeight, cart.cartProdName, cart.cartQuantity, cart.cartProdID, p.isSet
  563. FROM cart, products p, orders o
  564. WHERE o.ordID = cart.cartOrderID
  565. AND cart.cartProdID = p.pID
  566. AND o.ordID = ".$row['ordID'];
  567. $res3 = mysql_query($qry3) or print(mysql_error());
  568. // LOOP THROUGH EACH ITEM OF THE ORDER
  569. while($row3=mysql_fetch_assoc($res3)) {
  570. $strCSV .= $GMAcct."|".$row['ordID']."|".$row['ordName']."|".$shipAddress."|".$shipAddress2."||".$shipCity."|".$shipState."|".$shipZip."|".$row['ordPhone']."|".$row['ordEmail']."|".$ctryCode."|".$shipCountry."|".$shipCountry."|MP3 Carrying Case|".$row['ordTotal']."|".$totWeight."|0|".$row3['cartQuantity']."|".$row3['cartProdID']."|".$row3['cartProdName']."|".$row3['pPrice']."||\r\n";
  571. }
  572. // Update the order's status to 'Shipping'
  573. $qryUpdate = "UPDATE orders
  574. SET ordStatus = '11', ordStatusDate = '".date("Y-m-d H:i:s")."'
  575. WHERE ordID = '".$row['ordID']."'";
  576. $resUpdate = mysql_query($qryUpdate) or print(mysql_error());
  577. if($resUpdate) {
  578. //google sent cust status change
  579. $goid=$row['ordID'];
  580. $gship='ship';
  581. include(DOCROOT.'admin/ajaxservice.php');
  582. if(!setNewLocation( 11 , $row['ordID'] )) print("Unable to record location.");
  583. }
  584. // SEND CUSTOMER AN EMAIL NOTIFYING THEM THAT THEIR ORDER HAS SHIPPED
  585. if(!$isgoogleorder)notifyCustShipped($row['ordID']);
  586. }
  587. // Create the dat file
  588. if (!($fp = fopen($file_new, "w+")))
  589. exit("Unable to open the input file.");
  590. fwrite($fp,$strCSV);
  591. fclose($fp);
  592. chdir($_SERVER['DOCUMENT_ROOT']);
  593. $success = 'Batch '.$file_new.' created';
  594. header('Location: /admin/shipping2.php?success='.$success);
  595. exit();
  596. }
  597. }
  598. } else if(!empty($_POST['viewStatus'])) {
  599. if($_POST['viewStatus']=='View USPS') $status = 'USPS';
  600. if($_POST['viewStatus']=='View FedEx') $status = 'FedEx';
  601. if($_POST['viewStatus']=='View INTL') $status = 'INTL';
  602. header('Location: /admin/shipping2.php?status='.$status);
  603. exit();
  604. }
  605. header('Location: /admin/shipping2.php');
  606. exit();
  607. function getShipStatus($ordID) {
  608. $apo = false;
  609. $aAPOStates = array('AE','AA','AP');
  610. $country = '';
  611. $qry = "SELECT ordAddress, ordShipAddress, ordCountry, ordShipCountry, ordState, ordShipState, ordShipType, ordPoAPO, ordShipPoApo
  612. FROM orders
  613. WHERE ordID = $ordID";
  614. $res = mysql_query($qry) or print(mysql_error());
  615. $row = mysql_fetch_assoc($res);
  616. if(!empty($row['ordShipAddress'])) {
  617. $state = $row['ordShipState'];
  618. $country = $row['ordShipCountry'];
  619. $apo = $row['ordShipPoApo'];
  620. if(in_array($state,$aAPOStates)) { // IF A MILITARY STATE
  621. $apo = true;
  622. }
  623. }else{
  624. $state = $row['ordState'];
  625. $country = $row['ordCountry'];
  626. $apo = $row['ordPoApo'];
  627. if(in_array($state,$aAPOStates)) { // IF A MILITARY STATE
  628. $apo = true;
  629. }
  630. }
  631. // Get Postal Zone information
  632. $sql2 = "SELECT pz.pzName
  633. FROM countries c, postalzones pz
  634. WHERE c.countryZone = pz.pzID
  635. AND c.countryName = '" . $country . "'";
  636. $res2 = mysql_query($sql2) or print(mysql_error());
  637. $row2 = mysql_fetch_assoc($res2);
  638. if($apo) {
  639. return 'USPS';
  640. }elseif($row2['countryZone'] == 'United States') {
  641. if($row['ordShipType']=='FedEx Express') {
  642. return 'FedEx';
  643. }elseif($row['ordShipType']=='FedEx Overnight') {
  644. return 'FedEx';
  645. }else{
  646. return 'USPS';
  647. }
  648. }elseif($row2['countryZone'] == 'Canada') {
  649. return 'USPS';
  650. }elseif($row2['countryZone'] == 'International') {
  651. return 'USPS';
  652. }elseif($row2['countryZone'] == 'US Territories') {
  653. return 'USPS';
  654. }
  655. }
  656. function rightnow() {
  657. return date("Y-m-d H:i:s",mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y")));
  658. }
  659. function ftpLogin($host,$user,$pass) {
  660. $ftp = ftp_connect($host);
  661. if ($ftp) {
  662. if (ftp_login($ftp,$user,$pass)) {
  663. return $ftp;
  664. }else{
  665. ftp_quit($connID);
  666. return false;
  667. }
  668. }else{
  669. ftp_quit($connID);
  670. return false;
  671. }
  672. }
  673. function ftpUpload($connID,$destFile,$srcFile) {
  674. if(ftp_put($connID, $destFile, $srcFile, FTP_ASCII)) {
  675. ftp_quit($connID);
  676. return true;
  677. }else{
  678. ftp_quit($connID);
  679. return false;
  680. }
  681. }
  682. function cTrim(&$value) {
  683. $value = trim($value); // Trim line breaks
  684. $value = trim($value,'"'); // Trim off quotes
  685. }
  686. ?>