Location: PHPKode > projects > NOLA > importcustomer.php
<? include('includes/main.php'); ?>
<? include('includes/arfunctions.php'); ?>
<? //importcustomer.php - upload customer data from file
if ($filename) {
  $gfilename=IMAGE_UPLOAD_DIR."impcust".strtolower(substr($filename, strrpos($filename,'.')));
  if (!move_uploaded_file($filename, $gfilename)) retrievefile($filename,$gfilename);
  $fcontents = file ($gfilename);
  while (list ($line_num, $line) = each ($fcontents)) {
   echo '.';
   $custarray=split(",",$line);
   $creditlimit=stripquotes($custarray[0]);//$recordSet->fields[0];
   $billtoattnname=stripquotes($custarray[1]);//$recordSet->fields[1];
   $quoteattnname=stripquotes($custarray[2]);//$recordSet->fields[2];
   $salestaxnum=stripquotes($custarray[3]);//$recordSet->fields[3];
   $companyname=stripquotes($custarray[4]);//$recordSet->fields[4];
   $address1=stripquotes($custarray[5]);//$recordSet->fields[5];
   $address2=stripquotes($custarray[6]);//$recordSet->fields[6];
   $city=stripquotes($custarray[7]);//$recordSet->fields[7];
   $state=stripquotes($custarray[8]);//$recordSet->fields[8];
   $zip=stripquotes($custarray[9]);//$recordSet->fields[9];
   $phone1=stripquotes($custarray[10]);//$recordSet->fields[10];
   $phone2=stripquotes($custarray[11]);//$recordSet->fields[11];
   $email1=stripquotes($custarray[12]);//$recordSet->fields[12];
   $federalid=stripquotes($custarray[13]);//$recordSet->fields[13];
   $shipcompanyname=stripquotes($custarray[14]);//$recordSet->fields[15];
   $shipaddress1=stripquotes($custarray[15]);//$recordSet->fields[16];
   $shipaddress2=stripquotes($custarray[16]);//$recordSet->fields[17];
   $shipcity=stripquotes($custarray[17]);//$recordSet->fields[18];
   $shipstate=stripquotes($custarray[18]);//$recordSet->fields[19];
   $shipzip=stripquotes($custarray[19]);//$recordSet->fields[20];
   if ($shipcompanyname<=" ") $shipcompanyname=$companyname;
   $identical=0;
   if ($companyname>"  ") {
   if ($shipcompanyname==$companyname&&$shipaddress1==$address1&&$shipaddress2==$address2) $identical=1;
   //create bill-to entry into company and then read id
   $recordSet2=&$conn->Execute('select id from company where companyname='.sqlprep($companyname));
   $companyid=0;
   if (!$recordSet2->EOF) {
         //update company file only, do not create
         $companyid=$recordSet2->fields[0];
   } else {
         //insert into company file, then read id
         $conn->Execute('insert into company (companyname,address1,address2,city,state,zip,phone1,phone2,phone2comment,federalid,email1) values ('.sqlprep($companyname).', '.sqlprep($address1).', '.sqlprep($address2).', '.sqlprep($city).', '.sqlprep($state).', '.sqlprep($zip).', '.sqlprep($phone1).', '.sqlprep($phone2).', '.sqlprep("FAX").', '.sqlprep($email1).', '.sqlprep($federalid).')');
   };
   $recordSet2=&$conn->Execute('select id from company where companyname='.sqlprep($companyname));
   if (!$recordSet2->EOF) {
      $companyid=$recordSet2->fields[0];
      //create customer entry then read id
      $customerid=0;
      if ($companyid>0) {
         //already had a company entry, so check for customer entry
         $recordSet2=&$conn->Execute('select id from customer where gencompanyid='.sqlprep($active_company).' and companyid='.sqlprep($companyid).' and cancel=0');
         if (!$recordSet2->EOF) $customerid=$recordSet2->fields[0];
      };
      if ($customerid>0) {
           //update existing
           $conn->Execute('update customer set billtoattnname='.sqlprep($billtoattnname).', quoteattnname='.sqlprep($quoteattnname).' where customerid='.sqlprep($customerid));
      } else {
           //create new
           $conn->Execute('insert into customer (companyid,gencompanyid,billtoattnname,quoteattnname) values ('.sqlprep($companyid).','.sqlprep($active_company).','.sqlprep($billtoattnname).','.sqlprep($quoteattnname).')');
      };
      $recordSet2=&$conn->Execute('select id from customer where gencompanyid='.sqlprep($active_company).' and companyid='.sqlprep($companyid).' and cancel=0');
      if (!$recordSet2->EOF) $customerid=$recordSet2->fields[0];
      if ($customerid>0) {
          $recordSet2=&$conn->Execute('select id,shiptocompanyid from shipto where companyid='.sqlprep($companyid));
          $shiptoid=0;
          if (!$recordSet2->EOF) {
                $shiptoid=$recordSet2->fields[0];
                $shiptocompanyid=$recordSet2->fields[1];
                //update existing shipto
                if ($shiptocompanyid==$companyid&&$identical==1) {
                   //do not need to do anything
                } elseif ($identical==1&&$shiptocompanyid<>$companyid) {
                   //will need to change shiptocompanyid to be same as
                   //companyid
                   $conn->Execute('update shipto set companyid='.sqlprep($companyid).' where id='.sqlprep($shiptoid));

                } else {
                    // not identical, so simply update company pointed to
                    $conn->Execute('update company set companyname='.sqlprep($shipcompanyname).', address1='.sqlprep($shipaddress1).', address2='.sqlprep($shipaddress2).', city='.sqlprep($shipcity).', state='.sqlprep($shipstate).', zip='.sqlprep($shipzip).', phone1='.sqlprep($shipphone1).', phone2='.sqlprep($shipphone2).', phone2comment="FAX", email1='.sqlprep($shipemail1).', where companyid='.sqlprep($shiptocompanyid));
                };
          } else {
                //create ship-to entry
                //create company entry, then ship-to entry
                $recordSet2=&$conn->Execute('select id from company where companyname='.sqlprep($shipcompanyname).' and address1='.sqlprep($shipaddress1));
                if ($recordSet2->EOF) {
                     $conn->Execute('insert into company (companyname,address1,address2,city,state,zip,phone1,phone2,phone2comment,federalid,email1) values ('.sqlprep($shipcompanyname).', '.sqlprep($shipaddress1).', '.sqlprep($shipaddress2).', '.sqlprep($shipcity).', '.sqlprep($shipstate).', '.sqlprep($shipzip).', '.sqlprep($shipphone1).', '.sqlprep($shipphone2).', '.sqlprep("FAX").', '.sqlprep($shipemail1).', '.sqlprep($shipfederalid).')');
                };
                     $recordSet2=&$conn->Execute('select id from company where companyname='.sqlprep($shipcompanyname).' and address1='.sqlprep($shipaddress1));
                     if (!$recordSet2->EOF) $shiptocompanyid=$recordSet2->fields[0];
                     if ($shiptocompanyid>0) {
                        $conn->Execute('insert into shipto (companyid,shiptocompanyid) values ('.sqlprep($companyid).','.sqlprep($shiptocompanyid).')');
                     };
          };
      };
   };
   };
  };
  echo "\n";
  echo texttitle("Customers have been imported").'<br><br>';
} else {
    echo texttitle('Select Customer File');
    echo '<form name="mainform" method="post" action="importcustomer.php">';
    echo '<table><tr><td align="'.TABLE_LEFT_SIDE_ALIGN.'">URL/File for CSV Format Customer File:</td><td><input type="file" name="filename"'.INC_TEXTBOX.'></td></tr></table>';
    echo '<input type="submit" value="Select File"></form>';
};
?>
<? include('includes/footer.php'); ?>
Return current item: NOLA