Create($crm_db,$crm_user,$crm_host,$crm_pass); $gc_sql=$crm; // session handling $cdc = new MySQL_class; $cdc->Create($cdc_db,$cdc_user,$cdc_host,$cdc_pass); $brnd = new MySQL_class; $brnd->Create($db,$user,$host,$pass); unset($u_type,$u_id,$session_email); // start session start_session($php_sessid); // test login if($u_id=="") { header("location: /crm2/login.php"); exit(); } // get all the people that have a billing date of yesterday $yesterday = date("Y-m-d",mktime(0, 0, 0, date("m"), date("d")-1, date("Y"))); $anniversary = explode("-",$yesterday); // $anniversary[2] = 1; if(!$relation_id) { $query = "select company_id, contact_id, product_id, package_id, price, discount_price, discount_billing_period, brand, relation_id, billing_method from $ORDER_PACKAGE where billing_period = 'MONTHLY' and enabled = 'Y' and brand = '$code' and original_creation_date not like '$yesterday %' "; $data = $crm->mydata($query); //and billing_method = 'CHECK_MO' } else { $data[relation_id][0] = $relation_id; $data[brand][0] = $code; } $pdf = new cezpdf(); $pdf->selectfont('../commonfiles/fonts/Helvetica.afm'); for($a=0;$aeznewpage(); } $pdf_invoice2 = aggregate_bills($data[relation_id][$a],$cdc,$crm,$brnd,$data[brand][$a]); if($pdf_invoice2) { $pdf_invoice = $pdf_invoice2; } } if($pdf_invoice) { header('HTTP/1.1 200 OK'); header('Status: 200 OK'); header('Accept-Ranges: bytes'); header('Content-Transfer-Encoding: Binary'); header('Content-Type: application/force-download'); header("Content-Disposition: inline; filename=invoice".date("Y-m-d").".pdf"); echo"$pdf_invoice"; exit(); } else { echo"email NOT sent"; } function aggregate_bills($relation_id,$cdc,$crm,$brnd,$code) { global $pdf,$anniversary,$contact,$BRAND,$CDC,$ORDER_HEADER,$ORDER_DETAIL,$ORDER_CONTACTS; $next_month = date("Y-m-d",mktime(0, 0, 0, $anniversary[1]+1, $anniversary[2], $anniversary[0])); $yesterday = implode("-",$anniversary); // get the companies address $query = "select their_cdc_id from $BRAND where code = '$code' "; $data = $brnd->mydata($query); // get the clients information image $new_array = get_cdc($data[their_cdc_id][0],$code,$cdc); $pdf->ezsety(810); $pdf->ezText("\n$new_array[company_name]"); $pdf->ezText("$new_array[address_1_1]"); if($new_array[address_1_2]!="") { $pdf->ezText("$new_array[address_1_2]"); $address = "$new_array[address_1_1], $new_array[address_1_2]"; } else { $address = $new_array[address_1_1]; } $pdf->ezText("$new_array[city_1] $new_array[province_1], $new_array[zipcode_1]"); $pdf->ezText("$new_array[country_1]"); $pdf->ezText("$new_array[telephone_1]"); $footer = "Please enclose payment for the total amount due. \nWrite your check to $new_array[company_name] and send payment to: \n\n$new_array[company_name] \n$address \n$new_array[city_1] $new_array[province_1], $new_array[zipcode_1] \n $new_array[country_1]"; // get the order information $query = "select order_header_id, contact_id, company_id, order_date from $ORDER_HEADER where status = 'PACKAGE_PENDING' and relation_id = '$relation_id' order by order_date desc "; $o_data = $crm->mydata($query); // and payment_method = 'CHECK_MO' $order_date = explode(" ", $o_data[order_date][0]); $company_id = $o_data[company_id][0]; $contact_id = $o_data[contact_id][0]; // el("$company_id $contact_id"); // if nothing found break out .. if(!$company_id && !$contact_id) { return(0); } if($company_id) { $company = get_cdc($company_id,$code,$cdc); } else { $company = get_cdc($contact_id,$code,$cdc); } $pdf->addtext(445,795,20,"INVOICE"); $pdf->addtext(450,770,10,"Invoice #: ".substr($o_data[order_header_id][0],0,10).""); // add 15 days to order date $date = explode("-",$order_date[0]); $year = $date[0]; $month = $date[1]; $day = $date[2]; $issue_date = date("Y-M-d",mktime(0,0,0,date($month),date($day),date($year))); $end_date = date("Y-M-d",mktime(0,0,0,date($month),date($day)+15,date($year))); $pdf->addtext(470,760,10,"Date: $issue_date "); $pdf->addtext(448,750,10,"Due Date: $end_date"); $pdf->addtext(461,740,10,"Status: Due"); // get the clients address $data = get_cdc($contact_id,$code,$cdc); $contact = $data; $first_name = $data[first_name]; $last_name = $data[last_name]; $address_1 = $data[address_1_1]; $address_2 = $data[address_1_2]; $city = $data[city_1]; $province = $data[province_1]; $zip = $data[zipcode_1]; $country = $data[country_1]; $phone = $data[telephone_1]; if($company_id) { $billto = "$company[company_name] \n$first_name $last_name \n$address_1"; } else { $billto = "$first_name $last_name \n$address_1"; } if($address_2) { $billto .= "\n$address_2"; } $billto .= "\n$city, $province $zip"; if($country) { $billto .= "\n$country"; } $billto .= "\n\n\nCustomer ID: $data[external_id]\n"; $pdf->eztext("\n\n\n\n$billto"); unset($voodoo,$address_table,$billto,$shipto); // get the order details $query = "select * from $ORDER_DETAIL where order_header_id = '".$o_data[order_header_id][0]."' "; $order_detail_data = $crm->mydata($query); for($b=0;$bProduct Name" => "$b_date[0] - ".$order_detail_data[product_name][$b], "Quantity" => $order_detail_data[quantity][$b], "Price" => "$".money_format('%i',$order_detail_data[price][$b])."", "Subtotal" => "$".money_format('%i',$subtotal).""); $lists[] = $foo; } // get unpaid bills ... for($a=1;$amydata($query); if(abs($b_data[total][0])) { $b_date = explode(" ",$o_data[order_date][$a]); $foo = array( "Product Name" => "Service through $b_date[0] (past due)", "Quantity" => 'na', "Price" => 'na', "Subtotal" => "$".money_format('%i',$b_data[total][0]).""); $lists[] = $foo; $total = $b_data[total][0] + $total; } } $pdf->eztable($lists,'','',array('width'=>'525')); unset($lists,$foo); // get the total $query = "select sum(quantity*price) as total from $ORDER_DETAIL where order_header_id = '".$o_data[order_header_id][0]."' "; $order_detail_data = $crm->mydata($query); $total = $order_detail_data[total][0] + $total; $total = "\nTotal: $".$total." \n"; $total .= "\nThank you for your business."; $pdf->eztext("$total \n\n$footer"); ob_start(); $pdf->ezstream(); $pdf_invoice = ob_get_contents(); ob_end_clean(); return($pdf_invoice); } ?>