| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935 |
- <?php
- namespace bizGhs\order\classes;
- use biz\ghs\classes\GhsClass;
- use biz\shop\classes\MainClass;
- use biz\shop\classes\ShopCapitalClass;
- use biz\shop\classes\ShopClass;
- use biz\shop\classes\ShopExtClass;
- use biz\sj\classes\SjClass;
- use biz\stat\classes\StatCgClass;
- use biz\stat\classes\StatCgGhsClass;
- use biz\stat\classes\StatOutClass;
- use bizGhs\base\classes\BaseClass;
- use bizGhs\custom\classes\CustomClass;
- use bizGhs\item\classes\CostChangeClass;
- use bizGhs\order\traits\OrderTrait;
- use bizGhs\shop\classes\ShopAdminClass;
- use bizGhs\stock\classes\StockRecordClass;
- use bizGhs\stock\services\StockRecordService;
- use bizGhs\product\classes\ProductClass;
- use common\components\dict;
- use common\components\imgUtil;
- use common\components\noticeUtil;
- use common\components\orderSn;
- use common\components\printUtil;
- use common\components\util;
- use Yii;
- //采购单 lqh 2021.1.20
- class PurchaseOrderClass extends BaseClass
- {
- use OrderTrait;
- public static $baseFile = '\bizGhs\order\models\PurchaseOrder';
- const PURCHASE_ORDER_STATUS_WAIT = 1; //待确认
- const PURCHASE_ORDER_STATUS_UN_SEND = 2; //待配送,待发货
- const PURCHASE_ORDER_STATUS_SENDING = 3; //配送中,待入库
- const PURCHASE_ORDER_STATUS_COMPLETE = 4; //已入库
- const PURCHASE_ORDER_STATUS_CANCEL = 5; //已取消
- public static $statusMap = [
- self::PURCHASE_ORDER_STATUS_WAIT => '待确认',
- self::PURCHASE_ORDER_STATUS_UN_SEND => '待配送',
- self::PURCHASE_ORDER_STATUS_SENDING => '待入库',
- self::PURCHASE_ORDER_STATUS_COMPLETE => '已入库',
- self::PURCHASE_ORDER_STATUS_CANCEL => '已取消',
- ];
- const DEBT_UNKNOWN = 0;
- const DEBT_YES = 1;
- const DEBT_NO = 2;
- //寄付与到付
- const YF_PAY_WAY_JF = 1;
- const YF_PAY_WAY_DF = 2;
- //稍后入库和直接入库
- const IN_TYPE_LATER = 0;
- const IN_TYPE_NOW = 1;
- public static function printGroup($current, $content)
- {
- //58mm的机器,一行打印16个汉字,32个字母
- $bigPrice = $current['bigPrice'] ?? '';
- $bigPrice = floatval($bigPrice);
- $bigUnit = $current['bigUnit'] ?? '扎';
- $name = $current['name'] ?? '';
- if (floatval($bigPrice) == 0.01) {
- $name = $name . '(送)';
- }
- $count = $current['itemNum'] ?? '';
- $content .= '<B>' . $name . ' ' . $count . $bigUnit . '</B><BR>';
- $totalPrice = bcmul($count, $bigPrice, 2);
- $totalPrice = floatval($totalPrice);
- $content .= $bigPrice . '元x' . $count . $bigUnit . '=' . $totalPrice . '元<BR>';
- if (isset($current['refundNum']) && $current['refundNum'] > 0) {
- $content .= '<B>已退' . $current['refundNum'] . $bigUnit . '</B><BR>';
- }
- $content .= '--------------------------------<BR>';
- return $content;
- }
- //打印小票 ssh 20230608
- public static function printTicket($orderInfo)
- {
- $ghsId = $orderInfo->ghsId ?? 0;
- $debtCount = PurchaseOrderClass::getCount(['ghsId' => $ghsId, 'debt' => PurchaseOrderClass::DEBT_YES]);
- $debtAmount = PurchaseOrderClass::sum(['ghsId' => $ghsId, 'debt' => PurchaseOrderClass::DEBT_YES], 'actPrice');
- $orderSn = $orderInfo->orderSn ?? '';
- $itemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
- $shopId = $orderInfo->shopId ?? 0;
- $shop = ShopClass::getById($shopId, true);
- $telephone = $shop->telephone ?? '';
- $sjId = $shop->sjId ?? 0;
- $sj = SjClass::getById($sjId, true);
- $sjName = $sj->name ?? '';
- $shopName = $shop->shopName ?? '';
- $currentName = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
- $content = "<CB>" . $currentName . "</CB>";
- $content .= "<CB>采购单</CB>";
- $content .= '--------------------------------<BR>';
- $content .= '<C>供货商</C><BR>';
- $content .= '<CB>' . $orderInfo->ghsName . '</CB><BR><BR>';
- if (isset($orderInfo->remark) && !empty($orderInfo->remark)) {
- $content .= '<BR>';
- $content .= '备注:<BR>';
- $content .= '<B>' . $orderInfo->remark . '</B><BR>';
- }
- $content .= '<BR>';
- $content .= '商品 数量/单价 金额 <BR>';
- $content .= '--------------------------------<BR>';
- if (isset($itemList) && !empty($itemList)) {
- foreach ($itemList as $current) {
- $content = self::printGroup($current, $content);
- }
- }
- $kindNum = $orderInfo->kind ?? 0;
- $bigNum = $orderInfo->itemNum ? floatval($orderInfo->itemNum) : 0;
- $content .= '<BR>********************************<BR>';
- $content .= "<CB>共{$kindNum}种";
- if ($bigNum > 0) {
- $content .= ",{$bigNum}扎";
- }
- $content .= "</CB>";
- $content .= '********************************<BR><BR>';
- if (isset($orderInfo['itemPrice']) && $orderInfo['itemPrice'] > 0) {
- $content .= '商品金额:' . floatval($orderInfo['itemPrice']) . '元<BR>';
- }
- if (isset($orderInfo['sendCost']) && $orderInfo['sendCost'] > 0) {
- $content .= ' 运费:' . floatval($orderInfo['sendCost']) . '元<BR>';
- }
- if (isset($orderInfo['packCost']) && $orderInfo['packCost'] > 0) {
- $content .= ' 打包费:' . floatval($orderInfo['packCost']) . '元<BR>';
- }
- $content .= '合计金额:' . floatval($orderInfo['prePrice']) . '元<BR>';
- if (isset($orderInfo['discountAmount']) && !empty($orderInfo['discountAmount']) && $orderInfo['discountAmount'] > 0) {
- $content .= '优惠扣除:-' . floatval($orderInfo['discountAmount']) . '元<BR>';
- }
- if (isset($orderInfo['tkPrice']) && $orderInfo['tkPrice'] > 0) {
- $content .= '退款金额:' . floatval($orderInfo['tkPrice']) . '元<BR>';
- }
- $content .= '<B>订单金额:' . floatval($orderInfo['realPrice']) . '</B><BR>';
- if (isset($orderInfo['debt']) && $orderInfo['debt'] == 1) {
- $content .= '实付金额:0元<BR>';
- } else {
- $content .= '实付金额:' . floatval($orderInfo['realPrice']) . '元<BR>';
- }
- if ($debtAmount > 0) {
- $content .= '累计赊账:' . $debtCount . '笔 ' . floatval($debtAmount) . '元<BR>';
- }
- $content .= '--------------------------------<BR>';
- $getInfo = self::getById($orderInfo['id']);
- $content .= '订单日期:' . date("Y-m-d H:i", strtotime($getInfo['addTime'])) . '<BR>';
- $content .= '订单编号:' . $orderInfo['orderSn'] . '<BR>';
- if (!empty($telephone)) {
- $content .= '联系电话:' . $telephone . '<BR>';
- }
- $content .= "<BR>";
- $content .= "<BR>";
- $ext = ShopExtClass::getByCondition(['shopId' => $shopId]);
- $printSn = $ext['printSn'] ?? '';
- if (!empty($printSn)) {
- $p = new printUtil($printSn);
- $p->printMsg($content, $shop);
- }
- }
- public static function putIn($cg, $data)
- {
- if ($cg->status == self::PURCHASE_ORDER_STATUS_COMPLETE) {
- util::fail('已经入过库了');
- }
- if ($cg->status == self::PURCHASE_ORDER_STATUS_CANCEL) {
- util::fail('已取消');
- }
- $orderSn = $cg->orderSn ?? '';
- $mainId = $cg->mainId ?? 0;
- $sjId = $cg->sjId ?? 0;
- $shopId = $cg->shopId ?? 0;
- $staffId = $data['staffId'] ?? 0;
- $staffName = $data['staffName'] ?? '';
- $adminId = $data['adminId'] ?? 0;
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到门店');
- }
- $ghsName = $cg->ghsName ?? '';
- $itemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (empty($itemList)) {
- util::fail('没有找到花材');
- }
- foreach ($itemList as $cgKey => $cgItem) {
- $productId = $cgItem['productId'] ?? 0;
- $currentNum = $cgItem['itemNum'] ?? 0;
- $ptItemId = $cgItem['itemId'] ?? 0;
- $unitCost = $cgItem['cost'] ?? 0;
- $presell = $cgItem['presell'] ?? 0;
- $product = ProductClass::getLockById($productId);
- if (empty($product)) {
- util::fail('没有找到花材哦');
- }
- if ($presell == 0) {
- //加库存和流水记录
- $stockInfo = ProductClass::addStockByItemNum($productId, $currentNum);
- $record = [];
- $record['sjId'] = $sjId;
- $record['shopId'] = $shopId;
- $record['mainId'] = $mainId;
- $record['orderSn'] = $orderSn;
- $record['productId'] = $productId;
- $record['itemId'] = $ptItemId;
- $record['relateName'] = $ghsName;
- $record['itemNum'] = $currentNum;
- $record['oldStock'] = $stockInfo['oldStock'];
- $record['newStock'] = $stockInfo['newStock'];
- StockRecordClass::addPurchaseOrderRecord($record);
- }
- $product->cost = $unitCost;
- $product->priceLabel = ProductClass::PRICE_LABEL_AUTO;
- $product->save();
- //成本变动记录
- $changeData = [
- 'ptStyle' => 2,
- 'sjId' => $sjId,
- 'mainId' => $mainId,
- 'itemId' => $productId,
- 'ptItemId' => $ptItemId,
- 'cost' => $unitCost,
- 'staffId' => $staffId,
- 'staffName' => $staffName,
- 'name' => $product->name ?? '',
- 'cover' => $product->cover ?? '',
- 'targetId' => $order->id ?? 0,
- 'event' => '采购',
- ];
- CostChangeClass::addData($changeData);
- if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
- //直营门店的成本价也要一起改掉
- $allShop = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
- if (!empty($allShop)) {
- $currentItemId = $product->itemId ?? 0;
- foreach ($allShop as $currentShop) {
- $currentId = $currentShop->mainId ?? 0;
- if ($currentId == $mainId) {
- continue;
- }
- if (isset($currentShop->join) && $currentShop->join == 1) {
- continue;
- }
- $currentProduct = ProductClass::getByCondition(['mainId' => $currentId, 'itemId' => $currentItemId], true);
- if (!empty($currentProduct)) {
- $currentProduct->cost = $unitCost;
- $currentProduct->save();
- $currentStaff = ShopAdminClass::getByCondition(['mainId' => $currentProduct->mainId, 'adminId' => $adminId], true);
- //成本变动记录
- $changeData = [
- 'ptStyle' => 2,
- 'sjId' => $currentProduct->sjId ?? 0,
- 'mainId' => $currentProduct->mainId ?? 0,
- 'itemId' => $currentProduct->id ?? 0,
- 'ptItemId' => $currentProduct->itemId ?? 0,
- 'cost' => $unitCost,
- 'staffId' => $currentStaff->id ?? 0,
- 'staffName' => $currentStaff->name ?? '',
- 'name' => $currentProduct->name ?? '',
- 'cover' => $currentProduct->cover ?? '',
- 'targetId' => $order->id ?? 0,
- 'event' => $shop->shopName . '采购',
- ];
- CostChangeClass::addData($changeData);
- }
- }
- }
- }
- }
- //门店支出增加
- $currentPrice = $cg->actPrice ?? 0;
- $mainId = $shop->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有资产信息');
- }
- //支出增加
- $currentTotalExpend = bcadd($main->totalExpend, $currentPrice, 2);
- $main->totalExpend = $currentTotalExpend;
- $main->save();
- //采购增加
- $main->cgFinish += 1;
- $main->totalPurchaseOrder += 1;
- $currentTotalPurchase = bcadd($main->totalPurchase, $currentPrice, 2);
- $main->totalPurchase = $currentTotalPurchase;
- $main->save();
- //供货商资产增加
- $ghsId = $cg->ghsId ?? 0;
- $ghs = GhsClass::getLockById($ghsId);
- if (empty($ghs)) {
- util::fail('没有找到供货商');
- }
- $ghs->debt = GhsClass::DEBT_YES;
- $ghs->debtAmount = bcadd($ghs->debtAmount, $currentPrice, 2);
- $ghs->debtNum += 1;
- $ghs->expendAmount = bcadd($ghs->expendAmount, $currentPrice, 2);
- $ghs->expendNum += 1;
- $ghs->save();
- //采购统计
- $cgNum = $order->itemNum ?? 0;
- StatCgClass::replace($main, $shop, $currentPrice, $cgNum);
- //采购按供货商统计
- StatCgGhsClass::ghsReplace($cg);
- //门店应收客户款增加
- $ghsShopId = $ghs->shopId;
- $ghsShop = ShopClass::getLockById($ghsShopId);
- if (empty($ghsShop)) {
- util::fail('没有找到门店');
- }
- $ghsMainId = $ghsShop->mainId ?? 0;
- $ghsMain = \bizGhs\shop\classes\MainClass::getLockById($ghsMainId);
- if (empty($ghsMain)) {
- util::fail('没有main信息');
- }
- $currentMayGathering = bcadd($ghsMain->mayGathering, $currentPrice, 2);
- $ghsMain->mayGathering = $currentMayGathering;
- //客户资产增加
- $customId = $ghs->customId;
- $custom = CustomClass::getLockById($customId);
- if (empty($custom)) {
- util::fail('没有找到客户信息');
- }
- $custom->isDebt = CustomClass::IS_DEBT_YES;
- $custom->debtAmount = bcadd($custom->debtAmount, $currentPrice, 2);
- if ($custom->debtNum == 0) {
- $ghsMain->mayGatheringNum += 1;
- }
- $ghsMain->save();
- $custom->debtNum += 1;
- $custom->buyNum += 1;
- $custom->buyAmount = bcadd($custom->buyAmount, $currentPrice, 2);
- $custom->save();
- //当天和当月支出统计
- StatOutClass::updateOrInsert($main, $shop, $currentPrice);
- //支出流水
- $payWay = dict::getDict('payWay', 'unknown');
- $capitalType = dict::getDict('capitalType', 'ghsPurchase', 'id');
- $sjId = $order->sjId ?? 0;
- $shopId = $order->shopId ?? 0;
- $event = '采购';
- $mainId = $shop->mainId ?? 0;
- $capitalData = [
- 'capitalType' => $capitalType,
- 'io' => 0,
- 'totalExpend' => $currentTotalExpend,
- 'payWay' => $payWay,
- 'amount' => $currentPrice,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'event' => $event,
- 'mainId' => $mainId
- ];
- ShopCapitalClass::addCapital($capitalData);
- $cg->debt = self::DEBT_YES;
- $cg->status = self::PURCHASE_ORDER_STATUS_COMPLETE;
- $cg->entryTime = date("Y-m-d H:i:s");
- $cg->save();
- return $cg;
- }
- //添加采购单 lqh 2021.1.19
- public static function addOrder($data)
- {
- $sjId = $data['sjId'] ?? 0;
- $shopId = $data['shopId'] ?? 0;
- $mainId = $data['mainId'] ?? 0;
- $ghsName = $data['ghsName'] ?? '';
- $staffId = $data['staffId'] ?? 0;
- $staffName = $data['staffName'] ?? '';
- $inType = $data['inType'] ?? self::IN_TYPE_NOW;
- $adminId = $data['adminId'] ?? 0;
- $orderSn = orderSn::getGhsPurchaseSn();
- $data['orderSn'] = $orderSn;
- $status = $inType == self::IN_TYPE_NOW ? self::PURCHASE_ORDER_STATUS_COMPLETE : self::PURCHASE_ORDER_STATUS_SENDING;
- $data['status'] = $status;
- //数据结构 [{itemId:0,bigNum:0,smallNum:0,productId:12,itemPrice:1,weight:1}]
- $ghsItemInfo = $data['itemInfo'];
- $ghsItemInfo = self::mergeItemInfo($ghsItemInfo);
- //总共多少扎
- $bigNum = 0;
- //总共多少支
- $smallNum = 0;
- //总数量
- $totalNum = 0;
- $totalItemPrice = 0;
- $totalWeight = 0;
- $ghsItemInfoMap = [];
- $productIds = array_column($ghsItemInfo, 'productId');
- $productIds = array_unique(array_filter($productIds));
- $productList = ProductClass::getByIds($productIds, null, 'id');
- foreach ($ghsItemInfo as $v) {
- if (isset($v['bigNum']) == false || $v['bigNum'] <= 0) {
- util::fail('请填写数量');
- }
- if (isset($v['itemPrice']) == false || $v['itemPrice'] <= 0) {
- util::fail('请填写价格');
- }
- $bigNum += $v['bigNum'] ?? 0;
- $smallNum += $v['smallNum'] ?? 0;
- //限制不允许小单位采购,这样大单位就量总数量 ssh 20211021
- if (isset($v['smallNum']) && $v['smallNum'] > 0) {
- util::fail('采购最小单位必须扎');
- }
- $currentBigNum = $v['bigNum'] ?? 0;
- $totalNum = bcadd($totalNum, $currentBigNum, 2);
- $currentTotalPrice = bcmul($v['bigNum'], $v['itemPrice'], 2);
- $totalItemPrice = bcadd($totalItemPrice, $currentTotalPrice, 2);
- $ghsItemInfoMap[$v['productId']] = $v;
- $currentProductId = $v['productId'] ?? 0;
- $currentWeight = $productList[$currentProductId]['weight'] ?? 0;
- $weight = bcmul($currentWeight, $v['bigNum'], 2);
- $totalWeight = bcadd($totalWeight, $weight, 2);
- }
- $data['bigNum'] = $bigNum;
- $data['itemNum'] = $totalNum;
- $data['smallNum'] = $smallNum;
- $data['price'] = $totalItemPrice;
- $data['totalWeight'] = $totalWeight;
- //计算订单总价
- $data['packingCharge'] = isset($data['packingCharge']) && $data['packingCharge'] > 0 ? $data['packingCharge'] : 0;
- $data['shortCharge'] = isset($data['shortCharge']) && $data['shortCharge'] > 0 ? $data['shortCharge'] : 0;
- $data['longCharge'] = isset($data['longCharge']) && $data['longCharge'] > 0 ? $data['longCharge'] : 0;
- $data['pickCharge'] = isset($data['pickCharge']) && $data['pickCharge'] > 0 ? $data['pickCharge'] : 0;
- $data['localCharge'] = isset($data['localCharge']) && $data['localCharge'] > 0 ? $data['localCharge'] : 0;
- $cgModel = $data['cgModel'] ?? 0;
- if ($cgModel == 1) {
- //简约采购模式
- $customAvgKgWeight = $data['avgKgWeight'] ?? 0;
- $data['longCharge'] = bcmul($totalWeight, $customAvgKgWeight, 2);
- unset($data['modifyPrice']);
- } else {
- //正常采购模式不要使用传过来的每公斤运费
- unset($data['avgKgWeight']);
- }
- //直接入库的有入库时间
- if ($inType == self::IN_TYPE_NOW) {
- $data['entryTime'] = empty($data['entryTime']) ? date('Y-m-d H:i:s') : $data['entryTime'];
- } else {
- $data['entryTime'] = '0000-00-00 00:00:00';
- }
- $packingCharge = $data['packingCharge'] ?? 0;
- $shortCharge = $data['shortCharge'] ?? 0;
- $longCharge = $data['longCharge'] ?? 0;
- $pickCharge = $data['pickCharge'] ?? 0;
- $localCharge = $data['localCharge'] ?? 0;
- //提货费和本地运费
- $paidPrice = bcadd($localCharge, $pickCharge, 2);
- $yfPayWay = $data['yfPayWay'] ?? self::YF_PAY_WAY_JF;
- //打包费
- $currentPrice = bcadd($totalItemPrice, $packingCharge, 2);
- //寄付才算长途和短途运费
- if ($yfPayWay == self::YF_PAY_WAY_JF) {
- $currentPrice = bcadd($currentPrice, $shortCharge, 2);
- $currentPrice = bcadd($currentPrice, $longCharge, 2);
- } elseif ($yfPayWay == self::YF_PAY_WAY_DF) {
- $paidPrice = bcadd($paidPrice, $shortCharge, 2);
- $paidPrice = bcadd($paidPrice, $longCharge, 2);
- } else {
- util::fail('没有找到这个运费支付方式');
- }
- $data['prePrice'] = $currentPrice;
- if (isset($data['modifyPrice']) && !empty($data['modifyPrice']) && $data['modifyPrice'] > 0) {
- //报损减免后金额
- $modifyPrice = $data['modifyPrice'] ?? 0;
- if ($modifyPrice > $currentPrice) {
- util::fail("应付金额大于总金额,应付金额:{$modifyPrice},总金额:{$currentPrice}");
- }
- if ($modifyPrice < $currentPrice) {
- $data['discountAmount'] = bcsub($currentPrice, $modifyPrice, 2);
- $data['discountType'] = dict::getDict('discountType', 'discount');
- $currentPrice = $modifyPrice;
- }
- }
- $data['orderPrice'] = $currentPrice;
- $data['actPrice'] = $currentPrice;
- $data['realPrice'] = $currentPrice;
- $data['paidPrice'] = $paidPrice;
- //总运费
- $totalFreight = bcadd($packingCharge, $shortCharge, 2);
- $totalFreight = bcadd($totalFreight, $longCharge, 2);
- $totalFreight = bcadd($totalFreight, $pickCharge, 2);
- $totalFreight = bcadd($totalFreight, $localCharge, 2);
- $data['totalFreight'] = $totalFreight;
- $connection = Yii::$app->db;
- $transaction = $connection->beginTransaction();
- try {
- //总运费/总公斤数,计算每公斤的运费
- $avgKgWeight = bcdiv($totalFreight, $totalWeight, 3);
- $avgKgWeight = round($avgKgWeight, 2);
- $data['avgKgWeight'] = $avgKgWeight;
- $data['cgStyle'] = dict::getDict('cgStyle', 'ghs');
- $order = self::add($data, true);
- $shop = ShopClass::getLockById($shopId);
- if (empty($shop)) {
- util::fail('没有找到门店');
- }
- //组装详情表数据
- $batchData = self::groupOrderItem($ghsItemInfo, $orderSn);
- //加库存
- foreach ($batchData as $currentKey => $v) {
- $itemId = $v['itemId'];
- $productId = $v['productId'];
- $presell = $productList[$productId]['presell'] ?? 0;
- //非预售增加库存,预售不增加库存 ssh 20220109
- if ($presell == 0) {
- if ($inType == self::IN_TYPE_NOW) {
- //流水记录
- $record = [];
- $record['sjId'] = $sjId;
- $record['shopId'] = $shopId;
- $record['mainId'] = $mainId;
- $record['orderSn'] = $orderSn;
- $record['productId'] = $productId;
- $record['itemId'] = $itemId;
- $record['relateName'] = $ghsName;
- $record['itemNum'] = $v['itemNum'];
- $record['oldStock'] = $v['itemStock'];
- $record['newStock'] = bcadd($v['itemStock'], $v['itemNum'], 2);
- StockRecordClass::addPurchaseOrderRecord($record);
- //加库存
- ProductClass::addStockByItemNum($productId, $v['itemNum']);
- }
- }
- //单位采购价
- $unitCgPrice = $v['bigPrice'] ?? 0;
- $productWeight = $productList[$productId]['weight'] ?? 0;
- $unitFreight = bcmul($avgKgWeight, $productWeight, 3);
- $unitCost = bcadd($unitCgPrice, $unitFreight, 3);
- $unitCost = round($unitCost, 2);
- $batchData[$currentKey]['bigFreight'] = $unitFreight;
- $batchData[$currentKey]['cost'] = $unitCost;
- $currentCover = $productList[$productId]['cover'] ?? '';
- $currentName = $productList[$productId]['name'] ?? '';
- $batchData[$currentKey]['cover'] = $currentCover;
- $batchData[$currentKey]['name'] = $currentName;
- $currentBigUnit = $productList[$productId]['bigUnit'] ?? '';
- $currentSmallUnit = $productList[$productId]['smallUnit'] ?? '';
- $currentRatio = $productList[$productId]['ratio'] ?? 0;
- $currentRatioType = $productList[$productId]['ratioType'] ?? 0;
- $batchData[$currentKey]['bigUnit'] = $currentBigUnit;
- $batchData[$currentKey]['smallUnit'] = $currentSmallUnit;
- $batchData[$currentKey]['ratio'] = $currentRatio;
- $batchData[$currentKey]['ratioType'] = $currentRatioType;
- $batchData[$currentKey]['presell'] = $presell;
- if ($inType == self::IN_TYPE_NOW) {
- $product = ProductClass::getLockById($productId);
- $product->cost = $unitCost;
- $product->priceLabel = ProductClass::PRICE_LABEL_AUTO;
- $product->save();
- //成本变动记录
- $changeData = [
- 'ptStyle' => 2,
- 'sjId' => $sjId,
- 'mainId' => $mainId,
- 'itemId' => $productId,
- 'ptItemId' => $itemId,
- 'cost' => $unitCost,
- 'staffId' => $staffId,
- 'staffName' => $staffName,
- 'name' => $product->name ?? '',
- 'cover' => $product->cover ?? '',
- 'targetId' => $order->id ?? 0,
- 'event' => '采购',
- ];
- CostChangeClass::addData($changeData);
- if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
- //直营门店的成本价也要一起改掉
- $allShop = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
- if (!empty($allShop)) {
- $currentItemId = $product->itemId ?? 0;
- foreach ($allShop as $currentShop) {
- $currentId = $currentShop->mainId ?? 0;
- if ($currentId == $mainId) {
- continue;
- }
- if (isset($currentShop->join) && $currentShop->join == 1) {
- continue;
- }
- $currentProduct = ProductClass::getByCondition(['mainId' => $currentId, 'itemId' => $currentItemId], true);
- if (!empty($currentProduct)) {
- $currentProduct->cost = $unitCost;
- $currentProduct->save();
- $currentStaff = ShopAdminClass::getByCondition(['mainId' => $currentProduct->mainId, 'adminId' => $adminId], true);
- //成本变动记录
- $changeData = [
- 'ptStyle' => 2,
- 'sjId' => $currentProduct->sjId ?? 0,
- 'mainId' => $currentProduct->mainId ?? 0,
- 'itemId' => $currentProduct->id ?? 0,
- 'ptItemId' => $currentProduct->itemId ?? 0,
- 'cost' => $unitCost,
- 'staffId' => $currentStaff->id ?? 0,
- 'staffName' => $currentStaff->name ?? '',
- 'name' => $currentProduct->name ?? '',
- 'cover' => $currentProduct->cover ?? '',
- 'targetId' => $order->id ?? 0,
- 'event' => $shop->shopName . '采购',
- ];
- CostChangeClass::addData($changeData);
- }
- }
- }
- }
- }
- }
- //写入详情表
- PurchaseOrderItemClass::batchAddOrderItem($batchData);
- if ($inType == self::IN_TYPE_NOW) {
- //门店支出增加
- $mainId = $shop->mainId ?? 0;
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有资产信息');
- }
- //支出增加
- $currentTotalExpend = bcadd($main->totalExpend, $currentPrice, 2);
- $main->totalExpend = $currentTotalExpend;
- $main->save();
- //采购增加
- $main->cgFinish += 1;
- $main->totalPurchaseOrder += 1;
- $currentTotalPurchase = bcadd($main->totalPurchase, $currentPrice, 2);
- $main->totalPurchase = $currentTotalPurchase;
- $main->save();
- //供货商资产增加
- $ghsId = $data['ghsId'] ?? 0;
- $ghs = GhsClass::getLockById($ghsId);
- if (empty($ghs)) {
- util::fail('没有找到供货商');
- }
- $ghs->debt = GhsClass::DEBT_YES;
- $ghs->debtAmount = bcadd($ghs->debtAmount, $currentPrice, 2);
- $ghs->debtNum += 1;
- $ghs->expendAmount = bcadd($ghs->expendAmount, $currentPrice, 2);
- $ghs->expendNum += 1;
- $ghs->save();
- //采购统计
- $cgNum = $order->itemNum ?? 0;
- StatCgClass::replace($main, $shop, $currentPrice, $cgNum);
- //采购按供货商统计
- StatCgGhsClass::ghsReplace($order);
- //门店应收客户款增加
- $ghsShopId = $ghs->shopId;
- $ghsShop = ShopClass::getLockById($ghsShopId);
- if (empty($ghsShop)) {
- util::fail('没有找到门店');
- }
- $ghsMainId = $ghsShop->mainId ?? 0;
- $ghsMain = \bizGhs\shop\classes\MainClass::getLockById($ghsMainId);
- if (empty($ghsMain)) {
- util::fail('没有main信息');
- }
- $currentMayGathering = bcadd($ghsMain->mayGathering, $currentPrice, 2);
- $ghsMain->mayGathering = $currentMayGathering;
- //客户资产增加
- $customId = $ghs->customId;
- $custom = CustomClass::getLockById($customId);
- if (empty($custom)) {
- util::fail('没有找到客户信息');
- }
- $custom->isDebt = CustomClass::IS_DEBT_YES;
- $custom->debtAmount = bcadd($custom->debtAmount, $currentPrice, 2);
- if ($custom->debtNum == 0) {
- $ghsMain->mayGatheringNum += 1;
- }
- $ghsMain->save();
- $custom->debtNum += 1;
- $custom->buyNum += 1;
- $custom->buyAmount = bcadd($custom->buyAmount, $currentPrice, 2);
- $custom->save();
- //当天和当月支出统计
- StatOutClass::updateOrInsert($main, $shop, $currentPrice);
- //支出流水
- $payWay = dict::getDict('payWay', 'unknown');
- $capitalType = dict::getDict('capitalType', 'ghsPurchase', 'id');
- $sjId = $order->sjId ?? 0;
- $shopId = $order->shopId ?? 0;
- $event = '采购';
- $mainId = $shop->mainId ?? 0;
- $capitalData = [
- 'capitalType' => $capitalType,
- 'io' => 0,
- 'totalExpend' => $currentTotalExpend,
- 'payWay' => $payWay,
- 'amount' => $currentPrice,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'event' => $event,
- 'mainId' => $mainId
- ];
- ShopCapitalClass::addCapital($capitalData);
- }
- $transaction->commit();
- return $order;
- } catch (\Exception $exception) {
- $transaction->rollBack();
- util::fail('保存失败:' . $exception->getMessage());
- }
- return false;
- }
- //采购单列表 lqh 2021.1.19
- public static function getOrderList($where)
- {
- $data = self::getList('*', $where, 'addTime DESC');
- $list = $data['list'] ?? [];
- $data['list'] = self::groupSupplierList($list);
- $data['list'] = self::groupAdminList($data['list']);
- $data['list'] = self::groupStatusName($data['list']);
- return $data;
- }
- //获取订单详情信息 lqh 2021.1.20
- public static function getOrderDetail($orderSn, $shopId)
- {
- $where = ['orderSn' => $orderSn];
- $orderData = self::getByCondition($where);
- if (empty($orderData)) {
- util::fail('订单不存在');
- }
- if (isset($orderData['shopId']) == false || $orderData['shopId'] != $shopId) {
- util::fail('没有权限访问');
- }
- $orderInfo = PurchaseOrderItemClass::getOrderItemDetail($orderSn);
- if ($orderInfo) {
- foreach ($orderInfo as $key => $val) {
- $shortCover = $val['cover'] ?? '';
- $orderInfo[$key]['ShortCover'] = $shortCover;
- $orderInfo[$key]['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
- $orderInfo[$key]['bigCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
- }
- }
- $orderData = self::groupAdmin($orderData);
- //供货商信息
- $ghsId = $orderData['ghsId'] ?? 0;
- $ghsInfo = GhsClass::getGhsInfo($ghsId);
- $orderData['supplierName'] = $ghsInfo['name'] ?? '';
- $orderData['supplierMobile'] = $ghsInfo['mobile'] ?? '';
- $orderData['supplierAddress'] = $ghsInfo['address'] ?? '';
- $orderData['debtAmount'] = $ghsInfo['debtAmount'] ?? 0;
- $orderData['itemInfo'] = $orderInfo;
- $orderData['statusName'] = self::getStatusName($orderData['status']);
- //1寄付 2到付
- $yfPayWay = $orderData['yfPayWay'] ?? 1;
- $actPrice = $orderData['actPrice'] ?? 0;
- $shortCharge = $orderData['shortCharge'] ?? 0;
- $longCharge = $orderData['longCharge'] ?? 0;
- $pickCharge = $orderData['pickCharge'] ?? 0;
- $localCharge = $orderData['localCharge'] ?? 0;
- $totalCgCost = $actPrice;
- if ($yfPayWay == 2) {
- $totalCgCost = bcadd($totalCgCost, $shortCharge, 2);
- $totalCgCost = bcadd($totalCgCost, $longCharge, 2);
- }
- $totalCgCost = bcadd($totalCgCost, $pickCharge, 2);
- $totalCgCost = bcadd($totalCgCost, $localCharge, 2);
- $orderData['totalCgCost'] = $totalCgCost;
- return $orderData;
- }
- // 组装 orderItem lqh 2021.1.25
- public static function groupOrderItem($ghsItemInfo, $orderSn)
- {
- $productData = self::getProductMapData($ghsItemInfo);
- $batchData = [];
- foreach ($ghsItemInfo as $v) {
- $tmp = [];
- $productId = $v['productId'];
- $itemId = $productData[$productId]['itemId'];
- $tmp['orderSn'] = $orderSn;
- $tmp['itemId'] = $itemId;
- $tmp['productId'] = $v['productId'];
- $tmp['itemStock'] = $productData[$productId]['stock'] ?? 0;//当时库存
- $tmp['itemNum'] = $v['bigNum'];
- $tmp['bigPrice'] = $v['itemPrice'];
- $tmp['totalPrice'] = bcmul($v['itemPrice'], $tmp['itemNum'], 2);
- $batchData[] = $tmp;
- }
- return $batchData;
- }
- // 组装状态码 lqh 2021.1.30
- public static function groupStatusName($list)
- {
- foreach ($list as $k => $val) {
- $status = $val['status'] ?? 0;
- $list[$k]['statusName'] = self::getStatusName($status);
- }
- return $list;
- }
- // 获取状态码名称
- public static function getStatusName($status)
- {
- return self::$statusMap[$status] ?? '';
- }
- //获取各个状态下的订单数量 allNum unPayNum unSendNum sendingNum finishNum
- public static function statNum($shopId)
- {
- $data = [
- 'allNum' => 0,
- 'unPayNum' => 0,
- 'unSendNum' => 0,
- 'sendingNum' => 0,
- 'finishNum' => 0,
- ];
- $res = self::getAllList("status", ['shopId' => $shopId]);
- foreach ($res as $v) {
- $data['allNum']++;
- $status = $v['status'];
- switch ($status) {
- case self::PURCHASE_ORDER_STATUS_WAIT:
- //待支付
- $data['unPayNum']++;
- break;
- case self::PURCHASE_ORDER_STATUS_UN_SEND:
- $data['unSendNum']++;
- break;
- case self::PURCHASE_ORDER_STATUS_SENDING:
- $data['sendingNum']++;
- break;
- case self::PURCHASE_ORDER_STATUS_COMPLETE:
- $data['finishNum']++;
- break;
- }
- }
- return $data;
- }
- //采购数,采购金额统计(根据时间,今日,昨天,7天,30天)
- public static function statOrderInfo($shopId, $statType)
- {
- $start = '';
- $end = '';
- switch ($statType) {
- case "today":
- $start = date('Y-m-d');
- $end = $start;
- break;
- case "yesterday" :
- $start = date('Y-m-d', strtotime("-1 day"));
- $end = $start;
- break;
- case "seven":
- $start = date('Y-m-d', strtotime("-6 day"));
- $end = date('Y-m-d');
- break;
- case "thirty":
- $start = date('Y-m-d', strtotime("-29 day"));
- $end = date('Y-m-d');
- break;
- default:
- }
- $where = [];
- $where['shopId'] = $shopId;
- $where['addTime'] = ['between', [$start, $end]];
- $res = self::getAllByCondition($where, null, "id,actPrice");
- $totalAmount = 0;
- $totalNum = count($res);
- foreach ($res as $v) {
- $actPrice = $v['actPrice'] ?? 0;
- $totalAmount = bcadd($totalAmount, $actPrice, 2);
- }
- return [
- 'totalAmount' => $totalAmount,
- 'totalNum' => $totalNum,
- ];
- }
- }
|