.
*
* You can get complete code of ProjeQtOr, other resource, help and information
* about contributors at http://www.projeqtor.org
*
* ** DO NOT REMOVE THIS NOTICE *********************************************** */
/** ============================================================================
* QuotationMain
*/
require_once(__DIR__ . '/persistence/_securityCheck.php');
class QuotationMain extends SqlElement
{
// List of fields that will be exposed in general user interface
public $_sec_description;
public $id; // redefine $id to specify its visible place
public $reference;
public $name;
public $idQuotationType;
public $idProject;
public $idUser;
public $creationDate;
public $Origin;
public $idClient;
public $idContact;
public $description;
public $additionalInfo;
public $_sec_treatment;
public $idStatus;
public $idResource;
public $sendDate;
public $idDeliveryMode;
public $validityEndDate;
public $idLikelihood;
public $handled;
public $handledDate;
public $done;
public $doneDate;
public $idle;
public $idleDate;
public $cancelled;
public $_lib_cancelled;
//public $initialWork;
//public $initialPricePerDayAmount;
//public $initialAmount;
public $initialEndDate;
public $idActivityType;
public $idPaymentDelay;
public $_tab_5_1_smallLabel = array('untaxedAmountShort', 'tax', '', 'fullAmountShort', 'estimatedWork', 'amount');
public $untaxedAmount;
public $taxPct;
public $taxAmount;
public $fullAmount;
public $plannedWork;
public $comment;
//public $_sec_BillLine;
public $_BillLine = array();
public $_BillLine_colSpan = "2";
public $_sec_Link;
public $_Link = array();
public $_Attachment = array();
public $_Note = array();
public $_nbColMax = 3;
// Define the layout that will be used for lists
private static $_layout = '
# ${id}
${idProject}
${idClient}
${idQuotationType}
${name}
${idStatus}
${responsible}
${offerValidityEndDate}
${untaxedAmount}
${fullAmount}
${handled}
${done}
${idle}
';
private static $_fieldsAttributes = array("id" => "nobr",
"idQuotationType" => "required",
"idProject" => "required",
"reference" => "readonly",
"name" => "required",
"idStatus" => "required",
"handled" => "nobr",
"done" => "nobr",
"idle" => "nobr",
"idleDate" => "nobr",
"cancelled" => "nobr",
'taxAmount' => 'calculated,readonly',
'fullAmount' => 'readonly'
);
private static $_colCaptionTransposition = array('idUser' => 'issuer',
'idResource' => 'responsible',
'validityEndDate' => 'offerValidityEndDate',
'idActivity' => 'linkActivity',
'initialEndDate' => 'actualEndDate',
//'initialWork'=>'estimatedEffort',
//'initialAmount'=>'plannedAmount',
//'initialPricePerDayAmount'=>'pricePerDay',
'description' => 'request',
'idPaymentDelay' => 'paymentDelay',
'idDeliveryMode' => 'sendMode',
'plannedWork' => 'estimatedWork');
private static $_databaseColumnName = array('taxPct' => 'tax');
// private static $_databaseColumnName = array('idResource'=>'idUser');
/** ==========================================================================
* Constructor
* @param $id the id of the object in the database (null if not stored yet)
* @return void
*/
function __construct($id = NULL, $withoutDependentObjects = false)
{
parent::__construct($id, $withoutDependentObjects);
if (count($this->_BillLine)) {
self::$_fieldsAttributes['untaxedAmount'] = 'readonly';
}
if ($this->fullAmount) {
$this->taxAmount = $this->fullAmount - $this->untaxedAmount;
}
}
/** ==========================================================================
* Destructor
* @return void
*/
function __destruct()
{
parent::__destruct();
}
// ============================================================================**********
// GET STATIC DATA FUNCTIONS
// ============================================================================**********
/** ==========================================================================
* Return the specific layout
* @return the layout
*/
protected function getStaticLayout()
{
return self::$_layout;
}
/** ==========================================================================
* Return the specific fieldsAttributes
* @return the fieldsAttributes
*/
protected function getStaticFieldsAttributes()
{
return self::$_fieldsAttributes;
}
/** ============================================================================
* Return the specific colCaptionTransposition
* @return the colCaptionTransposition
*/
protected function getStaticColCaptionTransposition($fld = null)
{
return self::$_colCaptionTransposition;
}
/** ========================================================================
* Return the specific databaseTableName
* @return the databaseTableName
*/
protected function getStaticDatabaseColumnName()
{
return self::$_databaseColumnName;
}
/** =========================================================================
* control data corresponding to Model constraints
* @param void
* @return "OK" if controls are good or an error message
* must be redefined in the inherited class
*/
public function control()
{
$result = "";
$defaultControl = parent::control();
if ($defaultControl != 'OK') {
$result.=$defaultControl;
}
if ($result == "")
$result = 'OK';
return $result;
}
/** =========================================================================
* Overrides SqlElement::deleteControl() function to add specific treatments
* @see persistence/SqlElement#deleteControl()
* @return the return message of persistence/SqlElement#deleteControl() method
*/
/* * =========================================================================
* Overrides SqlElement::save() function to add specific treatments
* @see persistence/SqlElement#save()
* @return the return message of persistence/SqlElement#save() method
*/
public function save()
{
$result = '';
if (trim($this->id) == '') {
// fill the creatin date if it's empty - creationDate is not empty for import !
if ($this->creationDate == '')
$this->creationDate = date('Y-m-d');
}
if (trim($this->idClient)) {
$client = new Client($this->idClient);
if ($client->taxPct != '' and ! $this->taxPct) {
$this->taxPct = $client->taxPct;
}
if (!trim($this->idPaymentDelay)) {
$this->idPaymentDelay = $client->idPaymentDelay;
}
}
$this->name = trim($this->name);
$billLine = new BillLine();
$crit = array("refType" => "Quotation", "refId" => $this->id);
$billLineList = $billLine->getSqlElementsFromCriteria($crit, false);
if (count($billLineList) > 0) {
$amount = 0;
foreach ($billLineList as $line) {
$amount+=$line->amount;
}
$this->untaxedAmount = $amount;
}
$this->fullAmount = $this->untaxedAmount * (1 + $this->taxPct / 100);
$result = parent::save();
return $result;
}
private function resultMessage($message, $value = 'INVALID')
{
$returnValue = '' . $message . '';
$returnValue .= '';
$returnValue .= '';
$returnValue .= '';
return $returnValue;
}
private function createActivityQuotationLine(&$line, $quotation_id, $planningelement, $description)
{
$billLine = new BillLine();
$billLine->refType = 'Quotation';
$billLine->refId = $quotation_id;
$billLine->line = $line++;
$billLine->quantity = $planningelement->validatedWork;
$billLine->description = $description . "- Activity";
$billLine->detail;
$billLine->price = $planningelement->validatedCost / $billLine->quantity;
$billLine->idMeasureUnit = 3;
$billLine->amount = $planningelement->validatedCost;
$billLine->idTerm = NULL;
$billLine->idResource;
$billLine->idActivityPrice = NULL;
$billLine->startDate = $planningelement->validatedStartDate;
$billLine->endDate = $planningelement->validatedEndDate;
$billLine->extra = 0;
$billLine->billingType = 'M';
$result = $billLine->save();
if ($billLine->id) {
$result = '';
}
return $result;
}
private function createExpenseQuotationLine(&$line, $quotation_id, $amount, $startdate, $enddate, $description)
{
if ($amount > 0) {
$billLine = new BillLine();
$billLine->refType = 'Quotation';
$billLine->refId = $quotation_id;
$billLine->line = $line++;
$billLine->quantity = 1;
$billLine->description = $description . " Expense";
$billLine->detail;
$billLine->price = $amount;
$billLine->idMeasureUnit = 1;
$billLine->amount = $amount;
$billLine->idTerm = NULL;
$billLine->idResource;
$billLine->idActivityPrice = NULL;
$billLine->startDate = $startdate;
$billLine->endDate = $enddate;
$billLine->extra = 0;
$billLine->billingType = 'M';
$result = $billLine->save();
if (!$billLine->id) {
return $result;
}
}
return '';
}
/** =========================================================================
* Overrides SqlElement::deleteControl() function to add specific treatments
* @see persistence/SqlElement#deleteControl()
* @return the return message of persistence/SqlElement#deleteControl() method
*/
/* * =========================================================================
* Overrides SqlElement::save() function to add specific treatments
* @see persistence/SqlElement#save()
* @return the return message of persistence/SqlElement#save() method
*/
public function quotationFromProject($idProject)
{
$result = '';
if (trim($idProject) == '') {
return $this->resultMessage("Manca id del Progetto");
}
$quotationFinder = new Quotation();
$crit = array("idProject" => $idProject, "cancelled" => '0');
$quotationList = $quotationFinder->getSqlElementsFromCriteria($crit, false);
if (count($quotationList) > 0) {
return $this->resultMessage("Esiste già un preventivo per questo progetto . Cancellarlo per ricrearlo");
}
$project = new Project($idProject);
$this->idProject = $idProject;
if ($project->idProjectType == ProjectType::FIXED_PRICE) {
$this->idQuotationType = QuotationType::FIXED_PRICE;
} else if ($project->idProjectType == ProjectType::TIME_AND_MATERIALS || project->idProjectType == ProjectType::CTAM) {
$this->idQuotationType = QuotationType::PER_DAY; // Da rivedere
} else {
return $this->resultMessage("Non è possibile creare un preventivo per questo tipo di progetto");
}
$this->name = $project->name;
$this->description = $project->description;
$this->creationDate = date('Y-m-d');
$this->idClient = $project->idClient;
$this->idUser = getSessionUser()->id;
$this->idStatus = Status::STATUS_RECORDED;
$this->idResource = $project->idResource;
$this->reference = ''; // Cos'è ?????
$this->Origin = ''; // Cos'è ?????
$this->idContact = $project->idContact;
$this->additionalInfo = '';
$this->sendDate = NULL;
$this->idDeliveryMode = NULL;
$this->validityEndDate = NULL;
$this->idLikelihood = NULL;
$this->handled = 0;
$this->handledDate = NULL;
$this->done = 0;
$this->doneDate = NULL;
$this->idle = 0;
$this->idleDate = NULL;
$this->cancelled = 0;
$this->initialEndDate = NULL;
$this->idActivityType = NULL; // ???
$this->idPaymentDelay = NULL; // Verrà impostato nel save o prendere dal project o no
$this->untaxedAmount = 0; // Verrà impostato nel save
$this->taxPct = 0; // Verrà impostato nel save
$this->taxAmount = 0; // ??
$this->fullAmount = 0; // Verrà impostato nel save
$this->plannedWork = 0; // ??
$this->comment = '';
$result = $this->simpleSave(); // Per avere un id
$quotation_id = $this->id;
// $quotation = SqlElement::getSingleSqlElementFromCriteria('Quotation', $crit, false);
if (!$quotation_id) {
return $result;
}
$activityFinder = new Activity();
$crit = array("idProject" => $idProject, "cancelled" => '0');
$activityList = $activityFinder->getSqlElementsFromCriteria($crit, false);
$line = 1;
if (count($activityList) > 0) {
foreach ($activityList as $activity) {
$result = $this->createActivityQuotationLine($line, $quotation_id, $activity->ActivityPlanningElement, $activity->name);
if ($result != '') {
return $result;
}
}
} else {
$result = $this->createActivityQuotationLine($line, $quotation_id, $project->ProjectPlanningElement, $project->name);
if ($result != '') {
return $result;
}
}
if ($project->ProjectPlanningElement->expenseAssignedAmount >= $project->ProjectPlanningElement->expenseValidatedAmount) {
$expenseFinder = new Expense();
$crit = array("idProject" => $idProject, "cancelled" => '0');
$expenseList = $expenseFinder->getSqlElementsFromCriteria($crit, false);
foreach ($expenseList as $expense) {
$result = $this->createExpenseQuotationLine($line, $quotation_id,
$expense->plannedAmount,
$expense->expensePlannedDate,
$expense->expensePlannedDate,
$expense->name);
if ($result != '') {
return $result;
}
}
} else {
$result = $this->createExpenseQuotationLine($line, $quotation_id,
$project->ProjectPlanningElement->expenseAssignedAmount,
$project->ProjectPlanningElement->validatedStartDate,
$project->ProjectPlanningElement->validatedEndDate,
$project->name);
}
if ($result == '') {
$result = $this->resultMessage("Quotation $quotation_id inserted", 'OK');
}
return $result;
}
public function simpleSave()
{
return parent::save();
}
/** ==========================================================================
* Return the validation sript for some fields
* @return the validation javascript (for dojo frameword)
*/
public function getValidationScript($colName)
{
$colScript = parent::getValidationScript($colName);
if ($colName == "untaxedAmount" || $colName == "taxPct") {
$colScript .= '';
} else if ($colName == "idProject") {
$colScript .= '';
} else if ($colName == "idClient") {
$colScript .= '';
}
return $colScript;
}
private function zeroIfNull($value)
{
$val = $value;
if (!$val || $val == '' || !is_numeric($val)) {
$val = 0;
} else {
$val = $val * 1;
}
return $val;
}
}
?>