<?php
class RetailItem extends AppModel {
	var $name = 'RetailItem';
	var $validate = array(
		'article_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				'allowEmpty' => false,
				'required' => true,
			),
		),
		'amount' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				'allowEmpty' => false,
				'required' => true,
			),
		),
		'base_price' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				'allowEmpty' => true,
			),
		),
		'discount_percent' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				'allowEmpty' => true,
			),
		),
		'price' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				'allowEmpty' => true,
			),
		),
	);

	var $belongsTo = array(
		'Sale' => array(
			'className' => 'Sale',
			'foreignKey' => 'sale_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Article' => array(
			'className' => 'Article',
			'foreignKey' => 'article_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

	// function afterSave($created) {
		// parent::afterSave($created);
		// if ($created) {
			// $this->Product->contain('ProductItem');
			// $product = $this->Product->read(null, $this->data['SaleItem']['product_id']);

			// foreach($product['ProductItem'] as $i) {
				// $amount = $i['amount'] * $this->data['SaleItem']['amount'];
				// $this->Product->ProductItem->Article->updateAll(array('amount' => "`amount`-{$amount}"), array('Article.id' => $i['article_id']));
			// }
		// }
	// }

	// function cancelItem($item) {
		// foreach($item['Product']['ProductItem'] as $i) {
			// $amount = $i['amount'] * $item['amount'];
			// $this->Product->ProductItem->Article->updateAll(array('amount' => "`amount`+{$amount}"), array('Article.id' => $i['article_id']));
		// }
	// }

}
