connect(); class EcmDocumentTemplate extends SugarBean { var $field_name_map = array(); // STANDARD FIELDS var $id; var $date_entered; var $date_modified; var $modified_user_id; var $assigned_user_id; var $name; //TABLE COLUMNS var $place_of_register; var $logo_path; var $logo_id; var $logo_name; var $account_name; var $account_id; var $account_number; //footer var $footer_account; var $footer_address; var $footer_phone; var $footer_fax; var $footer_krs; var $footer_nip; var $footer_regon; var $footer_capital; var $footer_management; var $footer_swift; var $footer_bankname; var $footer_vatid; var $document_number_format_id; var $documents_per_day_id; var $documents_per; // var $document_number_letter; // var $correct_number_letter; var $ecmlanguage; var $footer_text; var $header_text; var $show_header_on_all_pages; var $show_footer_on_all_pages; var $template_files_defs; var $docs=array("EcmInsideOrders","EcmQuotes","EcmSales","EcmPurchaseOrders","EcmDeliveryNotes","EcmInvoiceOuts","EcmReceiptOuts","EcmStockDocIns","EcmStockDocOuts", "EcmStockDocInsideIns","EcmStockDocInsideOuts", "EcmStockDocMoves", "EcmTemplates", "EcmDocuments","EcmStockDocCorrects"); var $pdf; var $account; var $mfp; // RELATED FIELDS var $created_by; var $created_by_name; var $modified_by_name; var $assigned_user_name; // SUBPANELS RELATED // MODULE OBJECT DETAILS var $module_dir = 'EcmDocumentTemplates'; var $table_name = "ecmdocumenttemplates"; var $object_name = "EcmDocumentTemplate"; //RELATED TABLE NAMES // USED TO RETRIEVE RELATED FIELDS FROM FORM POSTS. var $additional_column_fields = Array( 'assigned_user_name', 'assigned_user_id', 'modified_user_id', 'created_by', ); var $relationship_fields = Array( //RELATIONSHIP FIELDS ); function EcmDocumentTemplate() { parent::SugarBean(); $this->setupCustomFields('EcmDocumentTemplates'); foreach ($this->field_defs as $field) { $this->field_name_map[$field['name']] = $field; } } var $new_schema = true; function get_summary_text(){ return "$this->name"; } function create_list_query($order_by, $where, $show_deleted = 0){ // Fill in the assigned_user_name $custom_join = $this->custom_fields->getJOIN(); $query = "SELECT "; $query .= " ecmdocumenttemplates.* ,users.user_name as assigned_user_name"; if($custom_join){ $query .= $custom_join['select']; } $query .= " FROM ecmdocumenttemplates LEFT JOIN users ON ecmdocumenttemplates.assigned_user_id=users.id"; $query .= " "; if($custom_join){ $query .= $custom_join['join']; } $where_auto = '1=1'; if($show_deleted == 0){ $where_auto = " $this->table_name.deleted=0 "; }else if($show_deleted == 1){ $where_auto = " $this->table_name.deleted=1 "; } if($where != "") $query .= "where $where AND ".$where_auto; else $query .= "where ".$where_auto; if(substr_count($order_by, '.') > 0){ $query .= " ORDER BY $order_by"; } else if($order_by != "") $query .= " ORDER BY $order_by"; else $query .= " ORDER BY ecmdocumenttemplates.name"; return $query; } function create_export_query($order_by, $where){ $custom_join = $this->custom_fields->getJOIN(); $query = "SELECT ecmdocumenttemplates.*, users.user_name assigned_user_name"; if($custom_join){ $query .= $custom_join['select']; } $query .= " FROM ecmdocumenttemplates "; $query .= " LEFT JOIN users ON ecmdocumenttemplates.assigned_user_id=users.id"; if($custom_join){ $query .= $custom_join['join']; } $query .= ""; $where_auto = " ecmdocumenttemplates.deleted=0 "; if($where != "") $query .= " where $where AND ".$where_auto; else $query .= " where ".$where_auto; if($order_by != "") $query .= " ORDER BY $order_by"; else $query .= " ORDER BY ecmdocumenttemplates.name"; return $query; } function fill_in_additional_list_fields(){ } function fill_in_additional_detail_fields(){ // FILL IN THE ASSIGNED_USER_NAME $this->assigned_user_name = get_assigned_user_name($this->assigned_user_id); $this->created_by_name = get_assigned_user_name($this->created_by); $this->modified_by_name = get_assigned_user_name($this->modified_user_id); } function get_list_view_data(){ global $current_language; $the_array = parent::get_list_view_data(); $app_list_strings = return_app_list_strings_language($current_language); $mod_strings = return_module_language($current_language, 'EcmDocumentTemplates'); // THE NEW LISTVIEW CODE ONLY FETCHES COLUMNS THAT WE'RE DISPLAYING AND NOT ALL // THE COLUMNS SO WE NEED THESE CHECKS. $the_array['NAME'] = (($this->name == "") ? "blank" : $this->name); $the_array['ENCODED_NAME'] = $this->name; return $the_array; } /** BUILDS A GENERIC SEARCH BASED ON THE QUERY STRING USING OR. DO NOT INCLUDE ANY $THIS-> BECAUSE THIS IS CALLED ON WITHOUT HAVING THE CLASS INSTANTIATED. */ function build_generic_where_clause ($the_query_string) { $where_clauses = Array(); $the_query_string = PearDatabase::quote(from_html($the_query_string)); array_push($where_clauses, "ecmdocumenttemplates.name like '$the_query_string%'"); $the_where = ""; foreach($where_clauses as $clause){ if($the_where != "") $the_where .= " or "; $the_where .= $clause; } return $the_where; } function set_notification_body($xtpl, $simplemodule){ global $mod_strings, $app_list_strings; $xtpl->assign("NAME", $simplemodule->name); $xtpl->assign("ECMDOCUMENTTEMPLATES_DESCRIPTION", $ecmdocumenttemplates->description); return $xtpl; } function bean_implements($interface){ switch($interface){ case 'ACL':return true; } return false; } function save($check_notify = FALSE){ if($this->polish_correct_format == 'on') $this->polish_correct_format = 1; else $this->polish_correct_format = 0; if($this->change_all_old_records == 1) { $this->UpdateFormatNumber("EcmQuotes", "ecmquotes"); $this->UpdateFormatNumber("EcmSales", "ecmsales"); $this->UpdateFormatNumber("EcmInvoiceOuts", "ecminvoiceouts"); $this->UpdateFormatNumber("EcmWorkReports", "ecmworkreports"); $this->UpdateFormatNumber("EcmDeliveryNotes", "ecmdeliverynotes"); } $arr=array(); foreach($this->docs as $d){ $arr[$d]=$_REQUEST[$d."_file"]; } $tfd=base64_encode(serialize($arr)); $this->template_files_defs=$tfd; return parent::save($check_notify); } public function UpdateFormatNumber($module, $table_name, $field_number = 'number', $field_formatted_number = 'document_no', $field_template_id = 'template_id') { $query = "SELECT `id`, `$field_number` as `number` FROM `$table_name` WHERE `$field_template_id`='$this->id' ORDER BY `date_entered` ASC"; $results = $GLOBALS['db']->query($query); if(is_resource($results)) { $numberPrefix = $this->getNumberPrefix($module); while($row = $GLOBALS['db']->fetchByAssoc($results)) { $new_number = $this->formatNumber($row['number'], $module, $table_name, $field_number, $numberPrefix); $query = "UPDATE `$table_name` SET `$field_formatted_number`='$new_number' WHERE `id`='".$row['id']."'"; $this->db->query($query); } } } public function retrieveByAccountId($id) { $query = "select id from ecmdocumenttemplates where account_id='".$id."' AND deleted=0 limit 1"; $result = mysql_query($query); if(!$result) return ''; $idtmp = mysql_fetch_array($result); $this->retrieve($idtmp[0]); $this->unformat_all_fields(); } function setPDF($id) { $this->retrieve($id); if($this->id == '') return; $this->format_all_fields(); $this->setAccount(); $this->loadParser(); $this->header_text = $this->mfp->parseText($this->header_text); $this->footer_text = $this->mfp->parseText($this->footer_text); $address = $this->account->name; if($this->account->billing_address_street) $address .= " - ".$this->account->billing_address_street; if($this->account->billing_address_postalcode) $address .= " - ".$this->account->billing_address_postalcode; if($this->account->billing_address_city) $address .= " ".$this->account->billing_address_city; $this->address = $address; require_once('modules/EcmDocumentTemplates/DocumentTemplatePDF.php'); $this->pdf = new DocumentTemplatePDF(); $this->pdf->edt = $this; $this->pdf->SetXY(25,25); $this->pdf->SetLineWidth(0); $this->pdf->SetDrawColor(0,0,0); $this->pdf->SetFont('Arial', '', 8); $this->pdf->AddPage(); } function outputPDF($name='auto_pdf.pdf',$method = 'I') { if($method == "S") echo $this->pdf->Output($name,$method); else $this->pdf->Output($name,$method); } function getPDF($id,$name='auto_pdf.pdf',$method = 'I') { $this->setPDF($id); $this->outputPDF($name,$method); } function setAccount() { if(isset($this->account_id) && $this->account_id != '') { require_once('modules/Accounts/Account.php'); $this->account = new Account(); $this->account->retrieve($this->account_id); } } function loadParser() { if(isset($this->mfp) && $this->mfp != '') return true; $path = 'modules/EcmDocumentTemplates/ModuleFieldsParser/ModuleFieldsParser.php'; if(file_exists($path)) { require_once($path); $this->mfp = new ModuleFieldsParser(); $this->mfp->modules['EcmDocumentTemplates']['focus'] = $this; if(!isset($this->account->id) || $this->account->id == '') { if(isset($this->account_id) && $this->account_id != '') { $this->setAccount(); $this->mfp->modules['Accounts']['focus'] = $this->account; } } else $this->mfp->modules['Accounts']['focus'] = $this->account; require_once('modules/Users/User.php'); $us = new User(); global $current_user; $us->retrieve(($this->assigned_user_id)?$this->assigned_user_id:$current_user->id); if(isset($us->id) && $us->id != '') { $this->mfp->modules['Users']['focus'] = $us; } return true; } else return false; } function generateNumber($table_name, $field_name = 'number') { $number = date("Y"); if($table_name=="ecminvoiceouts") $query = "SELECT COUNT(*) as `count` FROM `$table_name` WHERE `ecminvoiceout_id` is null and `".$field_name."` LIKE '".$number."%' AND deleted='0' ORDER BY `".$field_name."` DESC LIMIT 1"; else $query = "SELECT COUNT(*) as `count` FROM `$table_name` WHERE correct_id is null and `".$field_name."` LIKE '".$number."%' ORDER BY `".$field_name."` DESC LIMIT 1"; $GLOBALS['db'] = new MysqlManager(); $GLOBALS['db']->connect(); $result = $GLOBALS['db']->query($query); if(is_resource($result)) { $count = $GLOBALS['db']->fetchByAssoc($result); $count = intval($count['count']); //różnice między systemami - skasować po nowym roku: if ($table_name=='ecminvoiceouts') $count = $count - 14; } else $count = 1; $number = date("Ymd"); $number .= sprintf("%d", ++$count); return $number; } function getNumberPrefix($module) { $mod_strings2 = array(); require_once('modules/EcmTexts/EcmText.php'); if(isset($this->ecmlanguage) && $this->ecmlanguage != '') { $data = EcmText::LoadText(null,null,$module,$this->ecmlanguage); if(isset($data[0]) && isset($data[0]['data']) && isset($data[0]['data']['labels'])) { $data = $data[0]['data']['labels']; foreach($data as $key => $value) { $mod_strings2[$value['label']] = $value['translation']; } } } return $mod_strings2['LBL_PDF_CODE']; } function formatNumber($number, $module, $table_name = '', $field_name = 'number', $numberPrefix = null) { if($table_name == '') $table_name = strtolower($module); if($this->documents_per != "year") { $from_number = strval(substr($number,0,4)); $GLOBALS['db'] = new MysqlManager(); $GLOBALS['db']->connect(); $query = "SELECT COUNT(*) as `count`"; $query .= " FROM `".$table_name."`"; $query .= " WHERE "; if($table_name=="ecminvoiceouts")$query .= " ecminvoiceout_id is null and "; else $query .= " correct_id is null and "; $query .= " SUBSTRING(`number`, 1, 4) = '".$from_number."'"; if($this->documents_per == "month") { $query .= " AND"; $query .= " SUBSTRING(`number`,1,6) = '".substr($number,0,6)."'"; $query .= " AND"; $query .= " CAST(`number` AS UNSIGNED) < ".$number; } else if($this->documents_per == "day") { $query .= " AND"; $query .= " SUBSTRING(`number`,1,8) = '".substr($number,0,8)."'"; $query .= " AND"; $query .= " CAST(`number` AS UNSIGNED) < ".$number; } $query .= ";"; $result = $GLOBALS['db']->query($query); if(is_resource($result)) { $tmp = $GLOBALS['db']->fetchByAssoc($result); $count = intval($tmp['count']); $count ++; } $number = substr($number,0,8).strval($count); } if($numberPrefix == null) $numberPrefix = $this->getNumberPrefix($module); if($this->id=="57d2b950-00c8-62b7-16bb-4d678a086f05" || $this->id=="d48b9458-bf48-d30f-7f7c-4bb074833504"){ $a1=array("001/E","002/E","003/E","004/E","005/E","006/E","007/E","008/E","009/E","010/E","011/E","012/E","013/E","014/E","015/E","016/E","017/E","018/E","019/E"); $a2=array("1/E","2/E","3/E","4/E","5/E","6/E","7/E","8/E","9/E","10/E","11/E","12/E","13/E","14/E","15/E","16/E","17/E","18/E","19/E"); $nnoo=str_replace($a1,$a2,$this->formatDocumentNumber($number, $numberPrefix)); $t = explode("/", $nnoo); if (substr($t[0],0,1) == "0") { //return $t[0]; $t[0] = substr($t[0], 1, strlen($t[0])); $nnoo = implode("/",$t); } if ($this->id=="57d2b950-00c8-62b7-16bb-4d678a086f05") return str_replace("E", "U", $nnoo); return $nnoo; } else $nnoo=$this->formatDocumentNumber($number, $numberPrefix); return $nnoo; } function generateNumberCorrect($table_name, $field_name = 'number') { $number = date("Y"); if($table_name=="ecminvoiceouts"){ $query="SELECT COUNT(*) as count FROM ecminvoiceouts WHERE deleted='0' and (ecminvoiceout_id is not null or ecminvoiceout_id!='') and type='correct' and number LIKE '".$number."%' ORDER BY number DESC limit 1"; } else $query = "SELECT COUNT(*) as `count` FROM `$table_name` WHERE `correct_id` is not null and `".$field_name."` LIKE '".$number."%' ORDER BY `".$field_name."` DESC LIMIT 1"; $GLOBALS['db'] = new MysqlManager(); $GLOBALS['db']->connect(); $result = $GLOBALS['db']->query($query); if(is_resource($result)) { $count = $GLOBALS['db']->fetchByAssoc($result); $count = intval($count['count']); //różnice między systemami - skasować po nowym roku: if ($table_name=='ecminvoiceouts') $count = $count - 6; } else $count = 1; $number = date("Ymd"); $number .=$count; return $number; } function getNumberPrefixCorrect($module) { $mod_strings2 = array(); require_once('modules/EcmTexts/EcmText.php'); if(isset($this->ecmlanguage) && $this->ecmlanguage != '') { $data = EcmText::LoadText(null,null,$module,$this->ecmlanguage); if(isset($data[0]) && isset($data[0]['data']) && isset($data[0]['data']['labels'])) { $data = $data[0]['data']['labels']; foreach($data as $key => $value) { $mod_strings2[$value['label']] = $value['translation']; } } } return $mod_strings2['LBL_PDF_CODE_CORRECT']; } function formatNumberCorrect($number, $module, $table_name = '', $field_name = 'number', $numberPrefix = null) { if($table_name == '') $table_name = strtolower($module); if($this->documents_per != "year") { $from_number = strval(substr($number,0,4)); $GLOBALS['db'] = new MysqlManager(); $GLOBALS['db']->connect(); $query = "SELECT COUNT(*) as `count`"; $query .= " FROM `".$table_name."`"; $query .= " WHERE "; if($table_name=="ecminvoiceouts")$query .= " ecminvoiceout_id is not null and "; else $query .= " correct_id is not null and "; $query .= " SUBSTRING(`number`, 1, 4) = '".$from_number."'"; if($this->documents_per == "month") { $query .= " AND"; $query .= " SUBSTRING(`number`,1,6) = '".substr($number,0,6)."'"; $query .= " AND"; $query .= " CAST(`number` AS UNSIGNED) < ".$number; } else if($this->documents_per == "day") { $query .= " AND"; $query .= " SUBSTRING(`number`,1,8) = '".substr($number,0,8)."'"; $query .= " AND"; $query .= " CAST(`number` AS UNSIGNED) < ".$number; } $query .= ";"; $result = $GLOBALS['db']->query($query); if(is_resource($result)) { $tmp = $GLOBALS['db']->fetchByAssoc($result); $count = intval($tmp['count']); $count ++; } $number = substr($number,0,8).strval($count); } if($numberPrefix == null) $numberPrefix = $this->getNumberPrefixCorrect($module); return $this->formatDocumentNumber($number, $numberPrefix); } function formatDocumentNumber($documentNumber = '', $code = '') { $cf = ''; $format = $this->document_number_format_id; $format = str_replace('CODE', '[cOdE]', $format); $ARR1 = array ('y', 'Y', 'M', 'D', 'NR', '[cOdE]'); $ARR2 = array (substr($documentNumber,2,2), substr($documentNumber,0,4), substr($documentNumber,4,2), substr($documentNumber,6,2), substr($documentNumber,8)); $ARR2[4] = sprintf("%0".$this->documents_per_day_id."d",$ARR2[4]); $ARR2[5] = $code; $nno=str_replace($ARR1,$ARR2,$format).$cf; $exp=explode(" ",$nno); if($exp[0]=="")$nno=$exp[1]; return $nno;; } function getListOfTemplateFiles($path,$module){ if(!file_exists($path.$module))$module="Other"; if($handle=opendir($path.$module)) { while (false !== ($file = readdir($handle))) { if($file!="." && $file!=".." && is_dir($path.$module."/".$file))$arr[]=$file; } closedir($handle); } return $arr; } function selectTemplateFile($module,$files,$value){ $html=''; return $html; } function getTemplateFile($module, $type = null){ $arr=unserialize(base64_decode($this->template_files_defs)); if( file_exists("modules/EcmDocumentTemplates/templates/".$module."/".$arr[$module]."/header.php") && file_exists("modules/EcmDocumentTemplates/templates/".$module."/".$arr[$module]."/footer.php") && file_exists("modules/EcmDocumentTemplates/templates/".$module."/".$arr[$module]."/subheader.php") && file_exists("modules/EcmDocumentTemplates/templates/".$module."/".$arr[$module]."/subfooter.php") )return array($module,$arr[$module]); else return array("Other",$arr[$module]); } } ?>