1.2 Форма редактирования контрагентов – «Информация»
/****************************************************************************
Description: форма редактирования контрагентов – «Информация»
Author: Potapenko Vladimir
Version: 1.0.0 Changes info:
+ 23.03.2013 (Potapenko Vladimir): создан
****************************************************************************/ class OFormView_clients extends OFormViewRecord implements IActionListener
{ protected $SubTablesKeyId;
protected $Attributes; public function __construct($Name, $Parent)
{
parent::__construct($Name, $Parent);
$this->CreateFromXML(ProjectPath().'forms/clients/LayoutOFormView_clients.xml');
$this->DataAlias="table_clients";
$this->SubTablesKeyId=0;
$this->Attributes=array(); $this->Record=new ODBObject("", $this, "MainDBConnection");
$this->Record->SelectQueryAlias="Select_table_clients";
$this->Record->UpdateQueryAlias="Update_table_clients";
$this->Record->InsertQueryAlias="Insert_table_clients"; $this->Assigner->Add($this->Edit_NAME, "NAME", $this->Label_NAME, true);
$this->Assigner->Add($this->Edit_FULLNAME, "FULLNAME", $this->Label_FULLNAME, true);
$this->Assigner->Add($this->ListBox_ID_TYPE, "ID_TYPE");
$this->Assigner->Add($this->TextArea_ADDRESS, "ADDRESS");
$this->Assigner->Add($this->Edit_PHONE, "PHONE");
$this->Assigner->Add($this->Edit_EMAIL, "EMAIL");
$this->Assigner->Add($this->Edit_WEBSITE, "WEBSITE");
$this->Assigner->Add($this->TextArea_NOTICE, "NOTICE");
} protected function AfterAssign()
{
parent::AfterAssign();
if($this->Edit_FULLNAME->Value!="")
{
$this->Title=$this->Edit_FULLNAME->Value;
}
Fill_ListBox($this->ListBox_ID_TYPE, "table_client_types", "ID", "NAME"); $this->FormClientEmployees->Work($this->GetCurrentRecordId());
$this->FormClientHistory->Work($this->GetCurrentRecordId());
$this->FormClientPlans->Work($this->GetCurrentRecordId());
$this->FormClientProjects->Work($this->GetCurrentRecordId());
$this->FormClientBranchs->Work($this->GetCurrentRecordId());
$this->FormClientHeads->Work($this->GetCurrentRecordId()); if($this->GetCurrentRecordId()<=0)
{
$this->ListBox_ID_TYPE->Value=4;
} $this->LoadClientAttributes();
} protected function LoadClientAttributes()
{
$query="SELECT cp.ID, cp.NAME FROM table_client_params cp
LEFT JOIN table_cp_relation cpr ON cpr.ID_PARAM=cp.ID
WHERE cpr.ID_CLIENT='".$this->GetCurrentRecordId()."'";
$ds=new ODBDataSet("", $this, GetConnection("MainDBConnection"));
$ds->Open($query);
for($i=0; $i<$ds->RowCount; $i++)
{
$ds->SetRow($i);
$id=$ds->GetFieldValue("ID");
$name=$ds->GetFieldValue("NAME");
$this->Attributes[$id]=$name;
}
$this->SetClientAttributes();
} protected function GetCurrentRecordId()
{
$result=intval($this->Record->PropertyValue("ID"));
if($result<=0)
{
$result=UserId();
$result=$result-($result*2);
$this->SubTablesKeyId=$result;
}
return $result;
} protected function AfterOpen()
{
$record_id=$this->GetCurrentRecordId();
if($record_id<=0)
{
$this->AddClientHistoryLine($record_id, "Контрагент создан");
}
else
{
$this->AddMainContactInfo();
}
} protected function AddMainContactInfo()
{
$query="SELECT NAME, PHONE, EMAIL, POSITION FROM table_clients_employees WHERE
IS_MAIN_CONTACT='1' AND ID_CLIENT='".$this->GetCurrentRecordId()."' LIMIT 1";
$ds=new ODBDataSet("", $this, GetConnection("MainDBConnection"));
$ds->Open($query);
for($i=0; $i<$ds->RowCount; $i++)
{
$this->LabelContactNameValue->Text=$ds->GetFieldValue("NAME");
$this->LabelContactPositionValue->Text=
$ds->GetFieldValue("POSITION")==""?"-":$ds->GetFieldValue("POSITION");
$this->LabelContactPhoneValue->Text=
$ds->GetFieldValue("PHONE")==""?"-":$ds->GetFieldValue("PHONE");
$this->LabelContactEmailValue->Text=
$ds->GetFieldValue("EMAIL")==""?"-":" $ds->GetFieldValue("EMAIL")."'>".$ds->GetFieldValue("EMAIL")."";
}
} protected function SetClientAttributes()
{
$this->EditAttributes->Value=implode(", ", $this->Attributes);
} protected function AddClientHistoryLine($_id_client, $_action)
{
$query="INSERT INTO table_client_history(ID_CLIENT, ACTION_TYPE, ID_DONE_USER,
ACTION_DATE, ISPLANNED) VALUES('".$_id_client."', '$_action',
'".UserId()."', NOW(), 0)";
GetConnection("MainDBConnection")->sql_query($query);
$this->SendGlobalEvent("database_data_changed", "table_client_history");
} protected function AfterPost()
{
$this->AddRecentActivity("new_client");
if($this->SubTablesKeyId!=0)
{
$this->UpdateClientEmployeesKeyField();
$this->UpdateClientHistoryKeyField();
$this->SendGlobalEvent("database_data_changed", "table_client_history");
}
$this->SaveClientsAttributes();
} protected function UpdateClientEmployeesKeyField()
{
$query="UPDATE table_clients_employees SET ID_CLIENT='".$this->Record->Id().
"' WHERE ID_CLIENT='".$this->SubTablesKeyId."'";
GetConnection("MainDBConnection")->sql_query($query);
} protected function UpdateClientHistoryKeyField()
{
$query="UPDATE table_client_history SET ID_CLIENT='".$this->Record->Id().
"' WHERE ID_CLIENT='".$this->SubTablesKeyId."'";
GetConnection("MainDBConnection")->sql_query($query);
} protected function ClearAddedClientEmployees()
{
$query="DELETE FROM table_clients_employees WHERE ID_CLIENT='".
$this->GetCurrentRecordId()."'";
GetConnection("MainDBConnection")->sql_query($query);
} protected function ClearAddedClientHistory()
{
$query="DELETE FROM table_client_history WHERE ID_CLIENT='".
$this->GetCurrentRecordId()."'";
GetConnection("MainDBConnection")->sql_query($query);
} protected function SaveClientsAttributes()
{
$this->ClearClientAttributes();
$values="";
foreach(array_keys($this->Attributes) as $id)
{
$item="('$id', '".$this->Record->Id()."')";
$values .= $values==""?$item:", ".$item;
} if($values!="")
{
$query="INSERT INTO table_cp_relation(ID_PARAM, ID_CLIENT) VALUES".$values;
GetConnection("MainDBConnection")->sql_query($query);
}
} protected function ClearClientAttributes()
{
$query="DELETE FROM table_cp_relation WHERE ID_CLIENT='".$this->GetCurrentRecordId()."'";
GetConnection("MainDBConnection")->sql_query($query);
} protected function ButtonSelectAttributes_OnClick($_sender, $_event, $_event_args=null)
{
$f=new OFormDBCheckBoxSelect("FormDBCheckBoxSelect", $_SESSION["WorkSpace"]->MainForm);
$f->Title="Атрибуты";
$f->Work("ClientParamsSelected", "table_client_params", "ID", "NAME",
implode(",", array_keys($this->Attributes)));
} public function ButtonCancel_OnClick($_sender, $_event, $_event_args=null)
{
if($this->GetCurrentRecordId()<=0)
{
$this->ClearAddedClientEmployees();
$this->ClearAddedClientHistory();
}
parent::ButtonCancel_OnClick($_sender, $_event, $_event_args);
} public function CloseButton_OnClick($_sender, $_event, $_event_args=null)
{
$this->ButtonCancel_OnClick($_sender, $_event, $_event_args);
} public function ActionPerform($_event_name, $_event_args)
{
if($_event_name === "ClientEmployeeHistory" || $_event_name === "ClientProjectHistory")
{
$this->AddClientHistoryLine($_event_args["id_client"],$_event_args["add_text"]);
}
if($_event_name === "ClientParamsSelected")
{
$this->Attributes=$_event_args;
$this->SetClientAttributes();
}
if($_event_name === "CloseClientCard")
{
if($_event_args==$this->GetCurrentRecordId())
{
$this->Close();
}
}
}
} ?>
|