How to mark a deal with "New customer" or "Existing customer"

How to mark a deal with "New customer" or "Existing customer"

Hi all!

We have developed an API which imports all our deals from another system. When the deals gets imported, it checks if the linked customer already made a purchase with us before, or not. It is working somehow - but not entirely.

We have two types of cases; Quotes and Deals. A customer can have multiple quotes, but is not a former customer. To become a former customer they need to purchase something, which makes the quote as a deal(same module - just different stages on the deal).

So what we are doing right now, is checking if the customer have more deals attached, and if yes, then it checks the dates for the deals. This is because we are importing historic data - and one invoiced deal per customer HAS to be a NEW CUSTOMER deal, as they were not existing customer at that time.

So as I wrote, we check the current deal, with the other related deals the customer have. However, it is not always working, and the sorting mechanism is not working properly either.

Sooooo(ho), could anyone here take a look at our code, and figure out if there's an easier way of doing this? There must be, as this is somehow a bit complex code.

  1. // current deal
  2.     $deal_data = getZohoRecord("Deals", $deal_id);
  3.     $book_date_for_cond = $deal_data->data[0]->Tilbudsdato;
  4.     if($contact_id != ""){
  5.         // get existing deals
  6.         $get_related_deals = getZohoRecord("Contacts", $contact_id."/Deals?fields=Case_ID,Stage,Booket_Dato,Tilbudsdato,Oprettet");
  7.         $resp = usort($get_related_deals->data, function($a, $b) {
  8.             return strtotime($b->Oprettet) - strtotime($a->Oprettet);
  9.         });
  10.         
  11.         if(count($get_related_deals->data) == 1 && $get_related_deals->data[0]->Case_ID != $case_ws->case->caseId && $deal_data->data[0]->Stage != "Tilbud") {
  12.             $type = "Eksisterende kunde";
  13.         } elseif (count($get_related_deals->data) > 1){
  14.             // loop
  15.             // $type = "Eksisterende kunde";
  16.             $tilbud_check = false;
  17.             foreach($get_related_deals->data as $rel_deal){
  18.                 if($rel_deal->Stage != "Tilbud"){
  19.                     $tilbud_check = true;
  20.                     break;
  21.                 }
  22.             }
  23.             if($tilbud_check){
  24.                 foreach($get_related_deals->data as $rel_deal){
  25.                     
  26.                     if($book_date_for_cond <= $rel_deal->Booket_Dato || $book_date_for_cond <= $rel_deal->Tilbudsdato){
  27.                         $type = "Ny kunde";
  28.                     } else {
  29.                         $type = "Eksisterende kunde";
  30.                     }
  31.                 }
  32.             } else {
  33.                 $type = "Ny kunde";
  34.             }
  35.         } else {
  36.             $type = "Ny kunde";
  37.         }