437 lines
20 KiB
PHP
437 lines
20 KiB
PHP
<?php
|
||
|
||
namespace admin\models\forms\drug;
|
||
|
||
use common\core\BaseModel;
|
||
use common\helpers\ArrayHelper;
|
||
use common\models\Drug;
|
||
use common\models\DrugStoreDrug;
|
||
use common\models\DrugStoreRelations;
|
||
use common\models\Platform;
|
||
use Overtrue\Pinyin\Pinyin;
|
||
use yii\db\Exception;
|
||
|
||
class ImportForm extends BaseModel
|
||
{
|
||
public $file;
|
||
public $status;
|
||
|
||
public function rules()
|
||
{
|
||
return [
|
||
['status', 'integer'],
|
||
[['file'], 'file', 'skipOnEmpty' => false, 'extensions' => 'xls,xlsx'],
|
||
];
|
||
}
|
||
|
||
public function attributeLabels()
|
||
{
|
||
return [
|
||
'file' => '文件上传'
|
||
];
|
||
}
|
||
|
||
//首拼
|
||
public function shoupin($name)
|
||
{
|
||
return ArrayHelper::shouzimu($name);
|
||
}
|
||
|
||
//西药导入
|
||
public function WestImport($data)
|
||
{
|
||
if (empty($data)) {
|
||
throw new Exception('导入数据为空');
|
||
} else {
|
||
$t = \Yii::$app->db->beginTransaction();
|
||
try {
|
||
foreach ($data as $item) {
|
||
if (empty($item['药名'])) {
|
||
throw new Exception('药名不能为空,导入失败');
|
||
}
|
||
$drug = Drug::find()->where([
|
||
'or',
|
||
['bar_code' => $item['条形码']],
|
||
['guozi_no' => $item['国字准号']]
|
||
])->one();
|
||
if (!$drug) {
|
||
$model = new Drug();
|
||
$model->setAttribute('drug_name', trim($item['药名']));
|
||
$model->setAttribute('drug_alias', trim($item['商品名']));
|
||
$model->setAttribute('pinyin_simple', (new Pinyin())->abbr(trim($item['药名'])));
|
||
$model->setAttribute('drug_number', trim($item['编号']));
|
||
$model->setAttribute('bar_code', trim($item['条形码']));
|
||
$model->setAttribute('specification', trim($item['规格']));
|
||
$model->setAttribute('guozi_no', trim($item['国字准号']));
|
||
$model->setAttribute('status', trim($item['药品状态']));
|
||
$model->setAttribute('function', trim($item['功能主治']));
|
||
$model->setAttribute('source', trim($item['厂家']));
|
||
$model->setAttribute('status', trim($item['状态']));
|
||
$model->setAttribute('is_otc', trim($item['是否处方药']));
|
||
$model->setAttribute('time_id', trim($item['服用时间']));
|
||
$model->setAttribute('type_id', trim($item['服用方法']));
|
||
$model->setAttribute('frequency_id', trim($item['使用频率']));
|
||
$model->setAttribute('unit_id', trim($item['用量单位']));
|
||
$model->setAttribute('usage', trim($item['用法']));
|
||
$model->setAttribute('type', '2');
|
||
$model->setAttribute('created_at', time());
|
||
if (!$model->save()) {
|
||
$errorsMsg = array_values($model->getFirstErrors());
|
||
$errors[] = '条形码:' . $item['条形码'] . ',国子准号:' . $item['国字准号'] . '的西药导入失败,原因:保存失败;' . $errorsMsg[0];
|
||
} else {
|
||
$success[] = '条形码:' . $item['条形码'] . ',国子准号:' . $item['国字准号'] . '的西药导入成功';
|
||
}
|
||
} else {
|
||
$errors[] = '条形码:' . $item['条形码'] . ',国子准号:' . $item['国字准号'] . '的西药导入失败,原因:重复导入';
|
||
}
|
||
}
|
||
|
||
$t->commit();
|
||
} catch (\Exception $e) {
|
||
$t->rollBack();
|
||
$errors[] = $e->getMessage();
|
||
|
||
}
|
||
return ['success' => $success, 'error' => $errors];
|
||
}
|
||
}
|
||
|
||
//导入中药
|
||
public function ChineseImport($data)
|
||
{
|
||
if (empty($data)) {
|
||
throw new Exception('导入数据为空');
|
||
} else {
|
||
$t = \Yii::$app->db->beginTransaction();
|
||
try {
|
||
foreach ($data as $item) {
|
||
if (empty($item['药名'])) {
|
||
throw new Exception('药名不能为空,导入失败');
|
||
}
|
||
$drug = Drug::find()->where(['drug_number' => trim($item['编号'])])->one();
|
||
|
||
if (!$drug) {
|
||
$model = new Drug();
|
||
$model->setAttribute('drug_name', trim($item['药名']));
|
||
$model->setAttribute('pinyin_simple', (new Pinyin())->abbr(trim($item['药名'])));
|
||
$model->setAttribute('drug_number', trim($item['编号']));
|
||
$model->setAttribute('drug_alias', trim($item['别名']));
|
||
$model->setAttribute('unit_id', trim($item['单位']));
|
||
$model->setAttribute('status', trim($item['状态']));
|
||
$model->setAttribute('type', '1');
|
||
$model->setAttribute('created_at', time());
|
||
$model->setAttribute('updated_at', time());
|
||
if (!$model->saveOrFail()) {
|
||
$errorsMsg = array_values($model->getFirstErrors());
|
||
$errors[] = '编号:' . $item['编号'] . '的导入失败,原因:保存失败,' . $errorsMsg[0];
|
||
} else {
|
||
$success[] = '编号:' . $item['编号'] . '的中药导入成功';
|
||
}
|
||
} else {
|
||
$errors[] = '编号:' . $item['编号'] . '的中药导入失败,原因:重复导入';
|
||
}
|
||
}
|
||
|
||
$t->commit();
|
||
} catch (\Exception $e) {
|
||
$t->rollBack();
|
||
$errors[] = $e->getMessage();
|
||
}
|
||
return ['success' => $success, 'error' => $errors];
|
||
}
|
||
}
|
||
|
||
//颗粒配方
|
||
public function GranularImport($data)
|
||
{
|
||
if (empty($data)) {
|
||
throw new Exception('导入数据为空');
|
||
} else {
|
||
$t = \Yii::$app->db->beginTransaction();
|
||
try {
|
||
foreach ($data as $item) {
|
||
if (empty($item['药名'])) {
|
||
throw new Exception('药名不能为空,导入失败');
|
||
}
|
||
|
||
$drug = Drug::find()->where(['drug_number' => trim($item['编号'])])->one();
|
||
if (!$drug) {
|
||
$model = new Drug();
|
||
$model->setAttribute('drug_name', trim($item['药名']));
|
||
$model->setAttribute('pinyin_simple', (new Pinyin())->abbr(trim($item['药名'])));
|
||
$model->setAttribute('drug_number', trim($item['编号']));
|
||
$model->setAttribute('drug_alias', trim($item['别名']));
|
||
$model->setAttribute('place', trim($item['产地']));
|
||
$model->setAttribute('source', trim($item['厂家']));
|
||
$model->setAttribute('status', trim($item['状态']));
|
||
$model->setAttribute('is_otc', trim($item['是否处方药']));
|
||
$model->setAttribute('type', '3');
|
||
$model->setAttribute('created_at', time());
|
||
if (!$model->save()) {
|
||
$errorsMsg = array_values($model->getFirstErrors());
|
||
$errors[] = '编号:' . $item['编号'] . '导入失败,原因:保存失败;' . $errorsMsg[0];
|
||
} else {
|
||
$success[] = '编号:' . $item['编号'] . '的颗粒配方导入成功';
|
||
}
|
||
} else {
|
||
$errors[] = '编号:' . $item['编号'] . '的颗粒配方导入失败,原因:重复导入';
|
||
}
|
||
}
|
||
|
||
$t->commit();
|
||
} catch (\Exception $e) {
|
||
$t->rollBack();
|
||
$errors[] = $e->getMessage();
|
||
}
|
||
return ['success' => $success, 'error' => $errors];
|
||
}
|
||
}
|
||
|
||
|
||
//中成药导入
|
||
public function ImportZhongcheng($data)
|
||
{
|
||
if (empty($data)) {
|
||
throw new Exception('导入数据为空');
|
||
} else {
|
||
$t = \Yii::$app->db->beginTransaction();
|
||
try {
|
||
foreach ($data as $item) {
|
||
if (empty($item['药名'])) {
|
||
throw new Exception('药名不能为空,导入失败');
|
||
}
|
||
|
||
$drug = Drug::find()->where([
|
||
'or',
|
||
['bar_code' => trim($item['条形码'])],
|
||
['guozi_no' => trim($item['国字准号'])]
|
||
])->one();
|
||
if (!$drug) {
|
||
$model = new Drug();
|
||
$model->setAttribute('drug_name', trim($item['药名']));
|
||
$model->setAttribute('drug_alias', trim($item['别名']));
|
||
$model->setAttribute('pinyin_simple', (new Pinyin())->abbr(trim($item['药名'])));
|
||
$model->setAttribute('drug_number', trim($item['编号']));
|
||
$model->setAttribute('bar_code', trim($item['条形码']));
|
||
$model->setAttribute('specification', trim($item['规格']));
|
||
$model->setAttribute('guozi_no', trim($item['国字准号']));
|
||
$model->setAttribute('function', trim($item['功能主治']));
|
||
$model->setAttribute('source', trim($item['厂家']));
|
||
$model->setAttribute('status', trim($item['状态']));
|
||
$model->setAttribute('is_otc', trim($item['是否处方药']));
|
||
$model->setAttribute('time_id', trim($item['服用时间']));
|
||
$model->setAttribute('type_id', trim($item['服用方法']));
|
||
$model->setAttribute('frequency_id', trim($item['使用频率']));
|
||
$model->setAttribute('unit_id', trim($item['用量单位']));
|
||
$model->setAttribute('usage', trim($item['用法']));
|
||
$model->setAttribute('type', '4');
|
||
$model->setAttribute('created_at', time());
|
||
if (!$model->save()) {
|
||
$errorsMsg = array_values($model->getFirstErrors());
|
||
$errors[] = '条形码:' . $item['条形码'] . ',国子准号:' . $item['国字准号'] . '的中成药导入失败,原因:保存失败;' . $errorsMsg[0];
|
||
} else {
|
||
$success[] = '条形码:' . $item['条形码'] . ',国子准号:' . $item['国字准号'] . '的中成药导入成功';
|
||
}
|
||
} else {
|
||
$errors[] = '条形码:' . $item['条形码'] . ',国子准号:' . $item['国字准号'] . '的中成药导入失败,原因:重复导入';
|
||
}
|
||
}
|
||
$t->commit();
|
||
} catch (\Exception $e) {
|
||
$t->rollBack();
|
||
$errors[] = $e->getMessage();
|
||
}
|
||
return ['success' => $success, 'error' => $errors];
|
||
}
|
||
}
|
||
|
||
public function DivisionImport($data)
|
||
{
|
||
$user = \Yii::$app->user->identity;
|
||
$Platform_id = Platform::find()->select('id')->where([
|
||
'drugstore_id' => $user->drugstore,
|
||
])->one();
|
||
if (empty($data)) {
|
||
throw new Exception('导入数据为空');
|
||
} else {
|
||
foreach ($data as $item) {
|
||
$t = \Yii::$app->db->beginTransaction();
|
||
try {
|
||
$Is_PlatformDrug = PlatformDrug::find()->where([
|
||
'platform_id' => $Platform_id->id,
|
||
'drug_id' => $item['药品ID'],
|
||
'platform_drug_id' => $item['平台药品ID'],
|
||
'is_deleted' => 0
|
||
])->one();
|
||
|
||
if (!$Is_PlatformDrug) {
|
||
$PlatformDrug = new PlatformDrug();
|
||
$PlatformDrug->setAttribute('platform_drug_id', trim($item['平台药品ID']));
|
||
$PlatformDrug->setAttribute('drug_id', trim($item['药品ID']));
|
||
$PlatformDrug->setAttribute('platform_id', $Platform_id->id);
|
||
if (!$PlatformDrug->save()) {
|
||
$errorsMsg = array_values($PlatformDrug->getFirstErrors());
|
||
return [$errorsMsg[0]];
|
||
}
|
||
}
|
||
|
||
$Drug = Drug::find()->where([
|
||
'id' => $item['药品ID'],
|
||
'is_delete' => 0
|
||
])->one();
|
||
if (!$Drug) throw new Exception('该药品不存在');
|
||
|
||
$Is_DrugStoreDrug = DrugStoreDrug::find()->where([
|
||
'drug_id' => $item['药品ID'],
|
||
'drugstore_id' => $user->drugstore,
|
||
'is_delete' => 0
|
||
])->one();
|
||
if (!$Is_DrugStoreDrug) {
|
||
$model = new DrugStoreDrug();
|
||
$model->setAttribute('drugstore_id', $user->drugstore);
|
||
$model->setAttribute('drug_id', trim($item['药品ID']));
|
||
$model->setAttribute('type', $Drug->type);
|
||
$model->setAttribute('stock', $item['库存']);
|
||
$model->setAttribute('price', $item['价格']);
|
||
if (!$model->save()) {
|
||
$errorsMsg = array_values($model->getFirstErrors());
|
||
return [$errorsMsg[0]];
|
||
}
|
||
}
|
||
|
||
$t->commit();
|
||
$success[] = '分仓药品导入成功!';
|
||
|
||
} catch (\Exception $e) {
|
||
$t->rollBack();
|
||
$errors[] = $e->getMessage();
|
||
continue;
|
||
}
|
||
}
|
||
return ['success' => $success, 'error' => $errors];
|
||
}
|
||
}
|
||
|
||
public function ExportRealDrug($data)
|
||
{
|
||
if (empty($data)) {
|
||
throw new Exception('导入数据为空');
|
||
} else {
|
||
$t = \Yii::$app->db->beginTransaction();
|
||
try {
|
||
foreach ($data as $item) {
|
||
$DrugStoreDrug = DrugStoreDrug::find()->where(['drug_id' => trim($item['药品ID']), 'type' => trim($item['类别'])])->one();
|
||
|
||
if (!$DrugStoreDrug) {
|
||
$model = new DrugStoreDrug();
|
||
$model->setAttribute('drugstore_id', 1);
|
||
$model->setAttribute('drug_id', trim($item['药品ID']));
|
||
$model->setAttribute('type', trim($item['类别']));
|
||
$model->setAttribute('price', trim($item['销售价格']));
|
||
$model->setAttribute('market_price', trim($item['市场(指导价)价']) ?? '');
|
||
$model->setAttribute('stock', trim($item['库存']));
|
||
if (!$model->save()) {
|
||
$errorsMsg = array_values($model->getFirstErrors());
|
||
$errors[] = $item['药品ID'] . '导入失败,原因:保存失败;' . $errorsMsg[0];
|
||
} else {
|
||
$success[] = $item['药品ID'] . '导入成功!';
|
||
}
|
||
} else {
|
||
$errors[] = $item['药品ID'] . '导入失败,原因:重复导入';
|
||
}
|
||
}
|
||
$t->commit();
|
||
} catch (\Exception $e) {
|
||
$t->rollBack();
|
||
$errors[] = $e->getMessage();
|
||
}
|
||
return ['success' => $success, 'error' => $errors];
|
||
}
|
||
}
|
||
|
||
public function ExportUpdateDrugPrice($data)
|
||
{
|
||
if (empty($data)) {
|
||
throw new Exception('导入数据为空');
|
||
} else {
|
||
$t = \Yii::$app->db->beginTransaction();
|
||
try {
|
||
foreach ($data as $item) {
|
||
$item['药品ID'] = trim($item['药品ID']);
|
||
if (empty(trim($item['药品ID']))) {
|
||
$errors[] = $item['药品ID'] . '导入失败,原因:药品ID为空';
|
||
continue;
|
||
}
|
||
if (empty(trim($item['销售价格']))) {
|
||
$errors[] = $item['药品ID'] . '导入失败,原因:药品价格为空【' . $item['销售价格'] . '】';
|
||
continue;
|
||
}
|
||
$DrugStoreDrug = DrugStoreDrug::find()->where(['drug_id' => trim($item['药品ID'])])->one();
|
||
|
||
if ($DrugStoreDrug) {
|
||
if ($DrugStoreDrug->price == trim($item['销售价格']) && $DrugStoreDrug->market_price == trim($item['供货价'])) {
|
||
$errors[] = $item['药品ID'] . '导入失败,原因:价格未改变';
|
||
} else {
|
||
$DrugStoreDrug->setAttribute('market_price', trim($item['供货价']));
|
||
$DrugStoreDrug->setAttribute('price', trim($item['建议零售价']));
|
||
$DrugStoreDrug->updated_at = time();
|
||
|
||
if (!$DrugStoreDrug->save()) {
|
||
$errorsMsg = array_values($DrugStoreDrug->getFirstErrors());
|
||
$errors[] = $item['药品ID'] . '导入失败,原因:保存失败;' . $errorsMsg[0];
|
||
} else {
|
||
$success[] = $item['药品ID'] . '导入成功!';
|
||
}
|
||
}
|
||
|
||
} else {
|
||
$errors[] = $item['药品ID'] . '导入失败,原因:未找到对应药品';
|
||
}
|
||
}
|
||
$t->commit();
|
||
} catch (\Exception $e) {
|
||
$t->rollBack();
|
||
$errors[] = $e->getMessage();
|
||
}
|
||
return ['success' => $success, 'error' => $errors];
|
||
}
|
||
}
|
||
|
||
public function ExportUpdateDrugPriceByStore($data, $storeId)
|
||
{
|
||
if (empty($data)) {
|
||
throw new Exception('导入数据为空');
|
||
} else {
|
||
$t = \Yii::$app->db->beginTransaction();
|
||
try {
|
||
foreach ($data as $item) {
|
||
$item['药品ID'] = trim($item['药品ID']);
|
||
$DrugStoreDrug = DrugStoreRelations::find()->where(['drug_id' => trim($item['药品ID']), 'store_id' => $storeId])->one();
|
||
|
||
if ($DrugStoreDrug) {
|
||
if ($DrugStoreDrug->price == trim($item['销售价格'])) {
|
||
$errors[] = $item['药品ID'] . '导入失败,原因:价格未改变';
|
||
} else {
|
||
$DrugStoreDrug->setAttribute('price', trim($item['销售价格']));
|
||
$DrugStoreDrug->updated_at = date('Y-m-d H:i:s');
|
||
|
||
if (!$DrugStoreDrug->save()) {
|
||
$errorsMsg = array_values($DrugStoreDrug->getFirstErrors());
|
||
$errors[] = $item['药品ID'] . '导入失败,原因:保存失败;' . $errorsMsg[0];
|
||
} else {
|
||
$success[] = $item['药品ID'] . '导入成功!';
|
||
}
|
||
}
|
||
} else {
|
||
$errors[] = $item['药品ID'] . '导入失败,原因:未找到对应药品 '. json_encode($item);
|
||
}
|
||
}
|
||
$t->commit();
|
||
} catch (\Exception $e) {
|
||
$t->rollBack();
|
||
$errors[] = $e->getMessage();
|
||
}
|
||
return ['success' => $success, 'error' => $errors];
|
||
}
|
||
}
|
||
} |