'notes',
'email_id' => 'emails'
// 'account_id' => 'account',
// 'contact_id' => 'contact',
);
var $main_sale_id;
var $main_sale_no;
var $stock_id;
function EcmSale() {
parent::SugarBean ();
$this->setupCustomFields ( 'EcmSales' );
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 .= "ecmsales.*
,users.user_name as assigned_user_name";
if ($custom_join) {
$query .= $custom_join ['select'];
}
$query .= " FROM ecmsales
LEFT JOIN users
ON ecmsales.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 ecmsales.name";
return $query;
}
function create_export_query($order_by, $where) {
$custom_join = $this->custom_fields->getJOIN ();
$query = "SELECT
ecmsales.*,
users.user_name assigned_user_name";
if ($custom_join) {
$query .= $custom_join ['select'];
}
$query .= " FROM ecmsales ";
$query .= " LEFT JOIN users
ON ecmsales.assigned_user_id=users.id";
if ($custom_join) {
$query .= $custom_join ['join'];
}
$query .= "";
$where_auto = " ecmsales.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 ecmsales.name";
return $query;
}
function fill_in_additional_list_fields() {
}
function check_mail_addresses(){
$sea = new SugarEmailAddress;
$addresses = $sea->getAddressesByGUID($this->parent_id, 'Accounts');
$check=0;
foreach ( $addresses as $address ) {
if($address['email_address']!='' && $address['opt_out']==1){
$check=1;
}
}
return $check;
}
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 );
/*
$result = $this->db->query ( "SELECT status, parent_id, contact_id, template_id, assigned_user_id FROM ecmsales WHERE id='$this->id'" );
if (is_resource ( $result )) {
$row = $this->db->fetchByAssoc ( $result );
if ($row) {
$this->status = $row ['status'];
$this->parent_id = $row ['parent_id'];
$this->contact_id = $row ['contact_id'];
$this->template_id = $row ['template_id'];
$this->assigned_user_id = $row ['assigned_user_id'];
}
}
*/
/*
* $query = "select `number`, `template_id` from `ecmsales` where `id`='$this->id' limit 1"; $r = $this->db->query($query); if($r) { $r = $this->db->fetchByAssoc($r); $this->template_id = $r['template_id']; $this->number = $r['number']; $this->setTemplate(); $this->document_no = $this->formatNumber(); }
*/
}
function get_list_view_data() {
global $current_language,$current_user;
$this->fill_in_additional_detail_fields ();
$app_list_strings = return_app_list_strings_language ( $current_language );
$mod_strings = return_module_language ( $current_language, 'EcmSales' );
$the_array = parent::get_list_view_data ();
// THE NEW LISTVIEW CODE ONLY FETCHES COLUMNS THAT WE'RE DISPLAYING AND NOT ALL
// THE COLUMNS SO WE NEED THESE CHECKS.
if($this->ecmproduct_id!=''){
//add subpanel data
$tmp=$this->getPriceFromDocument($this->ecmproduct_id);
$the_array ['QUANTITY'] = format_number ( $tmp['quantity'] );
$the_array ['PRICE_NETTO'] = format_number ( $tmp['price_netto'] );
$the_array ['TOTAL_NETTO'] = ( $tmp['total_netto'] );
}
$this->retrieve($this->id);
$the_array ['PARENT_NAME']='' . $this->parent_name . '';
$the_array ['NAME'] = (($this->name == "") ? "blank" : $this->name);
$a = new Account();
$a->retrieve($this->parent_id);
$the_array ['SHOP_NUMBER'] = $a->shop_number;
$a = new EcmStockDocOut();
$a->retrieve_by_string_fields(['so_id'=>$this->id]);
$the_array ['WZ'] = "".$a->document_no."";
$a = new EcmInvoiceOut();
$a->retrieve_by_string_fields(['so_id'=>$this->id]);
$the_array ['FV'] = "".$a->document_no."";
$the_array ['ZS'] = "".$this->edi_zs_document_no."";
$the_array ['TOTAL'] = format_number ( $this->total_netto );
//create image options
$the_array['DOCUMENT_AUTOINCREMENT']=date('d.m.Y',strtotime($this->register_date));
$op = '
';
$the_array['OPTIONS'] = $op;
return $the_array;
}
function getPriceFromDocument($product){
$zap=$this->db->query("select quantity,price_netto,total_netto from ecmsaleitems where ecmsale_id='".$this->id."' and ecmproduct_id='".$product."'");
$dane=$this->db->fetchByAssoc($zap);
return $dane;
}
function getStatusMenuInfo($inJSON = true) {
global $app_list_strings;
$arr = array ();
foreach ( $app_list_strings ['ecmsales_status_dom'] as $key => $value ) {
$arr [$key] = array (
'enabled' => '
' . $value . '
' . $value . '
';
}
/**
* 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, "ecmsales.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 ( "DESCRIPTION", $simplemodule->description );
return $xtpl;
}
function bean_implements($interface) {
switch ($interface) {
case 'ACL' :
return true;
}
return false;
}
function save($check_notify = FALSE) {
//add mz
//get number format 2015-04-23
require_once('modules/EcmSysInfos/EcmSysInfo.php');
$EcmSysInfo = new EcmSysInfo();
$template = $EcmSysInfo->getFormatNumberForModule('EcmSales');
unset($EcmSysInfo);
global $current_user;
if (!$this->id || $this->id=='') {
if ($template != "") {
require_once('include/ECM/EcmDocumentNumberGenerator/EcmDocumentNumberGenerator.inc.php');
$dng = new EcmDocumentNumberGenerator('EcmSales',$this->register_date);
$this->document_no= $dng->parseSaleNumber($template, $this->stock_id, $this->order_source);
}
}
if ($this->order_source == null || strlen($this->order_source_id) == 0) {
$this->order_source = 'standard';
}
$this->vats_summary = htmlspecialchars($this->vats_summary);
$aa=explode('/',$this->document_no);
$this->document_autoincrement=date("Ymd",strtotime($this->register_date)).''.sprintf('%05d',(string)((int)$aa[0]));
$return_id = parent::save ( $check_notify );
//remove res
// include_once("modules/EcmStockOperations/EcmStockOperation.php");
//$op=new EcmStockOperation();
//$op->removeDocumentReservations($return_id);
$this->savePositions($return_id);
if($this->total_brutto!=0){
$this->db->query("update ecmsales set total_brutto='".round($this->total_brutto,2)."' where id ='".$this->id."'");
}
return $return_id;
}
// ***************************Start Managing Positions*************************//
function constructInsertQuery($data, $table = '') {
if ($table == '' && isset ( $this->object_name ) && $this->object_name != '') {
$table = strtolower ( $this->object_name ) . "items";
} else
return "";
$keys = array ();
$values = array ();
foreach ( $data as $key => $value ) {
$keys [] = $key;
if (is_array ( $value ))
$values [] = $value [1] . str_replace ( "'", "\'", $value [0] ) . $value [1];
else
$values [] = "'" . str_replace ( "'", "\'", $value ) . "'";
}
$q = "insert into `$table` (`";
$q .= implode ( '`, `', $keys );
$q .= "`) values (";
$q .= implode ( ", ", $values );
$q .= ");";
return $q;
}
function savePositions($id = null, $position_list = null) {
global $current_user, $timedate;
if ($position_list == null)
$position_list = $this->position_list;
if ($id == null)
$id = $this->id;
if (count ( $position_list ) > 0)
$this->deleteAssignedPositions ( $id );
$arr = array ();
$position = 0;
$this->total_brutto=0;
for($i = 0; $i < count ( $position_list ); $i ++) {
if ($position_list [$i] ['name'] == '' || $position_list [$i]['product_id']=='')
continue;
$p = $position_list [$i];
if (! empty ( $p ['category_id'] ) || $p ['category_id'] == '') {
require_once ("modules/EcmProducts/EcmProduct.php");
$pr = new EcmProduct ();
$pr->retrieve ( $p ['id'] );
$p ['category_id'] = $pr->product_category_id;
}
$item_id = create_guid();
$arr = array (
'id' => $item_id,
'date_entered' => array (
'NOW()',
''
),
'date_modified' => array (
'NOW()',
''
),
'modified_user_id' => $current_user->id,
'assigned_user_id' => $current_user->id,
'created_by' => $current_user->id,
'deleted' => '0',
strtolower ( $this->object_name ) . '_id' => $id,
'ecmproduct_id' => $p ['product_id'],
'position' => $position,
'code' => $p ['product_code'],
'name' => $p ['name'],
'quantity' => ((empty ( $p ['quantity'] ) || $p ['quantity'] == '') ? 0 : $p ['quantity']),
'price_start' => ((empty ( $p ['price_start'] ) || $p ['price_start'] == '') ? 0 : $p ['price_start']),
'price_netto' => ((empty ( $p ['price_netto'] ) || $p ['price_netto'] == '') ? 0 : $p ['price_netto']),
'price_brutto'=> round($p['price_netto']+($p['price_netto']*($p ['ecmvat_name']/100)),2),
'total_brutto'=> round($p['total_netto']+($p['total_netto']*($p ['ecmvat_name']/100)),2),
'discount' => ((empty ( $p ['discount'] ) || $p ['discount'] == '') ? 0 : $p ['discount']),
'total_netto' => ((empty ( $p ['total_netto'] ) || $p ['total_netto'] == '') ? 0 : $p ['total_netto']),
'dd_unit_id' => $p ['unit_id'],
'dd_unit_name' => $p['unit_name'],
'ecmvat_id' => $p ['ecmvat_id'],
'ecmvat_name' => $p ['ecmvat_name'],
'ecmvat_value' => $p ['ecmvat_value'],
'ecmproductcategory_id' => $p ['category_id'],
'recipient_code' => (empty($p['recipient_code']) ? $p ['product_recipient_code'] : $p['recipient_code']),
);
$this->total_brutto=$this->total_brutto+($p['total_netto']+($p['total_netto']*($p ['ecmvat_name']/100)));
$this->db->query ( $this->constructInsertQuery ( $arr ) );
//add mz 11-05-2015
//save reservation
include_once("modules/EcmStockOperations/EcmStockOperation.php");
$op=new EcmStockOperation();
$q = ((empty ( $p ['quantity'] ) || $p ['quantity'] == '') ? 0 : $p ['quantity']);
//$op->saveParentReservation($this->stock_id, $p ['product_id'], $id, $item_id, $q, 'EcmSale');
$position ++;
}
}
function getPosition($position,$module) {
if (! is_array ( $position ))
return '';
global $timedate,$app_list_strings;
$return_array = array ();
$p=new EcmProduct();
$p->retrieve($position ['ecmproduct_id']);
$return_array ['product_id'] = $position ['ecmproduct_id'];
$return_array ['position'] = $position ['position'];
$return_array ['product_code'] = $position ['code'];
$return_array ['name'] = $position ['name'];
$return_array ['quantity'] = $position ['quantity'];
if($module=='EcmReceipts'){
$return_array ['price_start'] = $position ['price_brutto'];
} else {
$return_array ['price_start'] = $position ['price_start'];
}
$query = "select sum(it.quantity) as quantity,it.ecmproduct_id as product_id from ecminvoiceoutitems it
left join ecminvoiceouts f on f.id=it.ecminvoiceout_id
where f.so_id='$this->id' and it.ecmproduct_id='".$position ['ecmproduct_id']."' group by it.id";
//echo $query;
$r = $this->db->query ( $query );
$r2=$this->db->fetchByAssoc($r);
$return_array ['quantity_used']=$r2['quantity'];
$return_array ['product_ks_group'] = $p->ks_group;
$return_array ['price_netto'] = $position ['price_netto'];
$return_array ['price_brutto'] = $position ['price_brutto'];
$return_array ['price_total_netto'] = $position ['total_netto'];
$return_array ['price_total_brutto'] = $position ['total_brutto'];
$return_array ['price_vat'] = $position ['price_brutto']-$position ['price_netto'];
$return_array ['price_total_vat'] = $position ['total_brutto']-$position ['total_netto'];
$return_array ['price_ecmvat_id'] = $position ['ecmvat_id'];
$return_array ['price_ecmvat_name'] = $position ['ecmvat_name'];
$return_array ['price_ecmvat_value'] = $position ['ecmvat_value'];
$return_array ['discount'] = $position ['discount'];
$return_array ['total_netto'] = $position ['total_netto'];
$return_array ['unit_id'] = $position ['dd_unit_id'];
$return_array ['unit_name'] = $position ['dd_unit_name'];
$return_array ['ecmvat_id'] = $position ['ecmvat_id'];
$return_array ['ecmvat_name'] = $position ['ecmvat_name'];
$return_array ['ecmvat_value'] = $position ['ecmvat_value'];
$return_array ['recipient_code'] = $position ['recipient_code'];
$return_array ['product_recipient_code'] = $position ['recipient_code'];
$return_array['unit_precision'] = $app_list_strings['ecmproducts_unit_dom_precision'][$position ['dd_unit_id']];
$return_array['product_precision'] = $app_list_strings['ecmproducts_unit_dom_precision'][$position ['dd_unit_id']];
$return_array['product_category_id'] = $p->product_category_id;
$return_array['real_vat_id'] = $p->vat_id;
$return_array['real_vat_name'] = $p->vat_name;
$return_array['real_vat_value'] = $p->vat_value;
// for WZ
$return_array['product_pieces_per_carton'] = $p->pieces_per_carton;
$return_array['un_code'] = $p->un_code;
//get reservatiaons
//add mz 2015-05-11
include_once("modules/EcmStockOperations/EcmStockOperation.php");
$op=new EcmStockOperation();
$return_array['quantity_res'] = format_number($op->getProductReservations($this->id, $position['id']),0,0);
unset($p);
return $return_array;
}
function getPositionList($array = false,$module) {
if (isset ( $this->id ) && $this->id != '') {
$query = "SELECT * FROM `" . strtolower ( $this->object_name ) . "items` WHERE `" . strtolower ( $this->object_name ) . "_id`='" . $this->id . "' order by position asc";
$r = $this->db->query ( $query );
$return_array = array ();
if ($r) {
while ( $w = $this->db->fetchByAssoc ( $r ) ) {
$return_array [] = $this->getPosition ( $w,$module);
}
$json = getJSONobj ();
return $array ? $return_array : $json->encode ( $return_array );
}
}
return $array ? false : '[]';
}
function deleteAssignedPositions() {
if (isset ( $this->id ) && $this->id != '') {
$query = "DELETE FROM `" . strtolower ( $this->object_name ) . "items` WHERE `" . strtolower ( $this->object_name ) . "_id`='" . $this->id . "'";
$r = $this->db->query ( $query );
if ($r)
return true;
}
return false;
}
function formatPositions($position_list) {
return;
if (! is_array ( $position_list ) || count ( $position_list ) == 0)
return false;
global $app_list_strings;
require_once ('modules/Currencies/Currency.php');
foreach ( $position_list as $key => $value ) {
$position_list [$key] ['unit_id'] = $app_list_strings ['ecmproducts_unit_dom'] [$value ['unit_id']];
$position_list [$key] ['category_id'] = $app_list_strings ['ecmproducts_category_dom'] [$value ['category_id']];
// $position_list[$key]['vat_id'] = format_number($value['vat_id']).'%';
$position_list [$key] ['discount'] = format_number ( $value ['discount'] ) . '%';
if (isset ( $currency ) && ($currency->id == $this->currency_id)) {
} else {
$currency = new Currency ();
$currency->retrieve ( $this->currency_id );
}
$position_list [$key] ['price'] = format_number ( $value ['price'] ) . ' ' . $currency->iso4217;
$position_list [$key] ['total'] = format_number ( $value ['total'] ) . ' ' . $currency->iso4217;
// $position_list[$key]['price'] = format_number($value['price']).' PLN';
// $position_list[$key]['total'] = format_number($value['total']).' PLN';
$this->currency_symbol = $currency->iso4217;
}
return $position_list;
}
// ***************************End Managing Positions*************************//
function ACLAccess($view, $is_owner = 'not_set') {
global $current_user;
$file = 'modules/EcmGroupSales/EcmGroupSale.php';
if (file_exists ( $file )) {
require_once ($file);
$cc = EcmGroupSale::loadSettings ( true );
} else
return true;
if (is_admin ( $current_user )) {
switch ($view) {
case 'confirming' :
return ($this->status == "s20");
case 'send_to_confirm' :
return ($this->status == "s10");
case 'pdf_toolbar' :
return ($this->status != "s10" && $this->status != "s20" && $this->status != "s40");
}
return true;
}
if ($is_owner == 'not_set') {
$is_owner = $this->isOwner ( $current_user->id );
}
// if we don't implent acls return true
if (! $this->bean_implements ( 'ACL' ))
return true;
$view = strtolower ( $view );
if ($cc ['user_manager_role']) { // Manager
switch ($view) {
case 'list' :
case 'index' :
case 'listview' :
return ACLController::checkAccess ( $this->module_dir, 'list', true );
case 'edit' :
case 'save' :
case 'popupeditview' :
case 'editview' :
return (ACLController::checkAccess ( $this->module_dir, 'edit', $is_owner ));
case 'view' :
case 'detail' :
case 'detailview' :
return ACLController::checkAccess ( $this->module_dir, 'view', $is_owner );
case 'delete' :
return ACLController::checkAccess ( $this->module_dir, 'delete', $is_owner );
case 'export' :
return ACLController::checkAccess ( $this->module_dir, 'export', $is_owner );
case 'import' :
return ACLController::checkAccess ( $this->module_dir, 'import', true );
case 'send_to_confirm' :
return ($this->status == "s10");
case 'can_commit' :
return true;
case 'confirming' :
return ($this->status == "s20");
case 'auto_commiting' :
return true;
case 'pdf_toolbar' :
return ($this->status != "s10" && $this->status != "s20" && $this->status != "s40");
}
} else if ($cc ['user_representative_extra_role']) { // Representative Extra
switch ($view) {
case 'list' :
case 'index' :
case 'listview' :
return ACLController::checkAccess ( $this->module_dir, 'list', true );
case 'edit' :
return (ACLController::checkAccess ( $this->module_dir, 'edit', $is_owner ) && ($this->status == "s10" || $this->status == "s40"));
case 'save' :
case 'popupeditview' :
case 'editview' :
return (ACLController::checkAccess ( $this->module_dir, 'edit', $is_owner ));
case 'view' :
case 'detail' :
case 'detailview' :
return ACLController::checkAccess ( $this->module_dir, 'view', $is_owner );
case 'delete' :
return (ACLController::checkAccess ( $this->module_dir, 'delete', $is_owner ) && $this->status == "s10");
case 'export' :
return ACLController::checkAccess ( $this->module_dir, 'export', $is_owner );
case 'import' :
return ACLController::checkAccess ( $this->module_dir, 'import', true );
case 'quote_to_sale' :
case 'quote_to_invoice' :
return ($this->status != "s10" && $this->status != "s20" && $this->status != "s40" && $this->status != "s60");
case 'send_to_confirm' :
return ($this->status == "s10");
case 'send_email' :
return ($this->status != "s10" && $this->status != "s20" && $this->status != "s40");
case 'can_commit' :
return true;
case 'confirming' :
return false;
case 'auto_commiting' :
return true;
case 'pdf_toolbar' :
return ($this->status != "s10" && $this->status != "s20" && $this->status != "s40");
}
} else if ($cc ['user_representative_role']) { // Representative
switch ($view) {
case 'list' :
case 'index' :
case 'listview' :
return ACLController::checkAccess ( $this->module_dir, 'list', true );
case 'edit' :
return (ACLController::checkAccess ( $this->module_dir, 'edit', $is_owner ) && ($this->status == "s10" || $this->status == "s40"));
case 'save' :
case 'popupeditview' :
case 'editview' :
return (ACLController::checkAccess ( $this->module_dir, 'edit', $is_owner ));
case 'view' :
case 'detail' :
case 'detailview' :
return ACLController::checkAccess ( $this->module_dir, 'view', $is_owner );
case 'delete' :
return (ACLController::checkAccess ( $this->module_dir, 'delete', $is_owner ) && $this->status == "s10");
case 'export' :
return ACLController::checkAccess ( $this->module_dir, 'export', $is_owner );
case 'import' :
return ACLController::checkAccess ( $this->module_dir, 'import', true );
case 'quote_to_sale' :
case 'quote_to_invoice' :
return ($this->status != "s10" && $this->status != "s20" && $this->status != "s40" && $this->status != "s60");
case 'send_to_confirm' :
return ($this->status == "s10");
case 'send_email' :
return ($this->status != "s10" && $this->status != "s20" && $this->status != "s40");
case 'can_commit' :
return false;
case 'confirming' :
return false;
case 'auto_commiting' :
return false;
case 'pdf_toolbar' :
return ($this->status != "s10" && $this->status != "s20" && $this->status != "s40");
}
}
// if it is not one of the above views then it should be implemented on the page level
return true;
}
function getPositionListForInvoice($array = false,$module) {
$query = "select sum(it.quantity) as quantity,it.ecmproduct_id as product_id from ecminvoiceoutitems it
left join ecminvoiceouts f on f.id=it.ecminvoiceout_id
where f.so_id='$this->id' group by it.ecmproduct_id";
$r = $this->db->query ( $query );
if($r->num_rows>0){
$used_array=array();
while($res=$this->db->fetchByAssoc($r)){
$used_array[]=$res;
}
$query = "SELECT * FROM `" . strtolower ( $this->object_name ) . "items` WHERE `" . strtolower ( $this->object_name ) . "_id`='" . $this->id . "' order by position asc";
$r = $this->db->query ( $query );
$return_array = array ();
if ($r) {
while ( $w = $this->db->fetchByAssoc ( $r ) ) {
$return_array [] = $this->getPosition ( $w,$module);
}
}
foreach ($return_array as $k=>$v){
foreach ($used_array as $k2=>$v2){
if($v['product_id']==$v2['product_id']){
$return_array[$k]['quantity']=$v['quantity']-$v2['quantity'];
}
}
}
// remove used ...
foreach ($return_array as $k=>$v){
if($v['quantity']==0){
unset($return_array[$k]);
}
}
// reindexing ....
$return_array = array_merge($return_array);
for($i=0;$i