namespace yii_app\actions\motivation;
-use PhpOffice\PhpSpreadsheet\IOFactory;
-use PhpOffice\PhpSpreadsheet\Spreadsheet;
use Yii;
use yii\base\Action;
use yii\base\DynamicModel;
use yii\web\UploadedFile;
use yii_app\records\CityStore;
use yii_app\records\Motivation;
-use yii_app\records\MotivationCostsItem;
use yii_app\services\MotivationService;
class IndexAction extends Action
$model->month = intval($model->month);
}
+ // Подсчитываем стоимость брака
+ MotivationService::calculateDefectCost($model->store_id, $model->year, $model->month);
+
$showTable = false;
$motivationData = [];
$daysInMonth = null;
const DATA_TYPE_FLOAT = 'float';
const DATA_TYPE_STRING = 'string';
+ const ITEM_WRITE_OFF_OF_ILLIQUID_GOODS_SPOILAGE_EXPIRATION_OF_SHELF_LIFE = 'Списание неликвидного товара: порча, истечение срока годности';
+ const ITEM_DEFECTIVE_DELIVERY = 'Брак с поставки';
+ const ITEM_DEFECT_DUE_TO_EQUIPMENT_FAILURE = 'Брак из-за поломки оборудования';
+ const ITEM_REGRADING = 'Пересорт';
+
+ public static function writeOffsToMotivationItemMap($itemType) {
+ switch ($itemType) {
+ case WriteOffsErp::WRITE_OFFS_TYPE_BRAK: return self::ITEM_WRITE_OFF_OF_ILLIQUID_GOODS_SPOILAGE_EXPIRATION_OF_SHELF_LIFE;
+ case WriteOffsErp::WRITE_OFFS_TYPE_DELIVERY_BRAK: return self::ITEM_DEFECTIVE_DELIVERY;
+ case WriteOffsErp::WRITE_OFFS_TYPE_DUE_TO_EQUIPMENT_FAILURE_BRAK: return self::ITEM_DEFECT_DUE_TO_EQUIPMENT_FAILURE;
+ case WriteOffsErp::WRITE_OFFS_TYPE_RESORTING_DOES_NOT_COUNT_TOWARDS_COST: return self::ITEM_REGRADING;
+ default: return self::ITEM_WRITE_OFF_OF_ILLIQUID_GOODS_SPOILAGE_EXPIRATION_OF_SHELF_LIFE;
+ }
+ }
+
/**
* {@inheritdoc}
*/
namespace yii_app\services;
use PhpOffice\PhpSpreadsheet\IOFactory;
+use yii_app\records\ExportImportTable;
use yii_app\records\Motivation;
use yii_app\records\MotivationValue;
use yii_app\records\MotivationValueGroup;
use yii_app\records\CityStore;
use yii_app\records\MotivationCostsItem;
+use yii_app\records\WriteOffs;
class MotivationService
return compact('errors');
}
+
+ public static function calculateDefectCost($store_id, $year, $month) {
+ $monthStart = date("Y-m-d 00:00:00", strtotime($year . '-' . $month . '-1'));
+ $monthEnd = date("Y-m-t 23:59:59", strtotime($year . '-' . $month . '-1'));
+
+ $motivation = Motivation::find()->where(['store_id' => $store_id, 'year' => $year, 'month' => $month])->one();
+
+ foreach (range(1, 5) as $ind) {
+ $weekStart = date("Y-m-d 00:00:00", strtotime("+" . (($ind - 1) * 7) . ' days', strtotime($monthStart)));
+ $weekEnd = date("Y-m-d 23:50:50", strtotime("+" . ($ind * 7 - 1) . ' days', strtotime($monthStart)));
+ if ($weekEnd > $monthEnd) {
+ $weekEnd = $monthEnd;
+ }
+
+ $motivationValueGroup = MotivationValueGroup::find()->where(['alias' => 'week' . $ind])->one();
+
+ $eit = ExportImportTable::find()->select(['export_val'])->where(['entity' => 'city_store', 'entity_id' => $store_id, 'export_id' => 1])->one();
+ /** @var $eit ExportImportTable */
+ if ($eit) {
+ $writeOffs = WriteOffs::find()->select(['sum(summ) as total', 'type'])
+ ->where(['between', 'date', $weekStart, $weekEnd])
+ ->andWhere(['store_id' => $eit->export_val])
+ ->groupBy(['type'])
+ ->indexBy('type')
+ ->asArray()->all();
+
+ foreach ($writeOffs as $key => $data) {
+ $motivationItemType = MotivationCostsItem::writeOffsToMotivationItemMap($key);
+ $motivationCostsItem = MotivationCostsItem::find()->where(['name' => $motivationItemType])->one();
+ /** @var $motivationCostsItem MotivationCostsItem */
+ if ($motivation) {
+ $motivationValue = MotivationValue::find()->where(['motivation_id' => $motivation->id,
+ 'motivation_group_id' => $motivationValueGroup->id, 'value_id' => $motivationCostsItem->code])->one();
+ if (!$motivationValue) {
+ $motivationValue = new MotivationValue;
+ $motivationValue->motivation_id = $motivation->id;
+ $motivationValue->motivation_group_id = $motivationValueGroup->id;
+ $motivationValue->value_id = $motivationCostsItem->code;
+ $motivationValue->value_type = $motivationCostsItem->data_type;
+ }
+ $motivationValue->value_float = $data['total'];
+ $motivationValue->save();
+ if ($motivationValue->getErrors()) {
+ var_dump($motivationValue->getErrors());
+ die;
+ }
+ }
+ }
+ }
+ }
+ }
}