| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042 |
- <?php
- namespace bizGhs\stat\classes;
- use biz\stat\classes\StatStockInClass;
- use biz\stat\classes\StatStockOutClass;
- use bizGhs\cg\classes\CgRefundClass;
- use bizGhs\custom\classes\CustomClass;
- use bizGhs\custom\classes\DistClass;
- use bizGhs\expend\classes\ExpendClass;
- use bizGhs\item\classes\ItemClassClass;
- use bizGhs\order\classes\OrderItemClass;
- use bizGhs\order\classes\PurchaseOrderClass;
- use bizGhs\order\classes\PurchaseOrderItemClass;
- use bizGhs\order\classes\RefundOrderClass;
- use bizGhs\order\classes\NextDayRefundClass;
- use bizGhs\order\classes\StockWastageOrderClass;
- use bizGhs\product\classes\ProductClass;
- use bizGhs\staff\classes\SalaryClass;
- use bizHd\goods\classes\PlantCgClass;
- use bizHd\order\classes\OrderClass as HdOrderClass;
- use bizHd\order\classes\OrderItemClass as HdOrderItemClass;
- use bizGhs\order\classes\OrderClass;
- use bizHd\purchase\classes\PurchaseClass;
- use bizHd\refund\classes\HdRefundClass;
- use bizHd\refund\classes\HdNextDayRefundClass;
- use common\components\arrayUtil;
- use common\components\dateUtil;
- use common\components\dict;
- use common\components\util;
- use Yii;
- use bizGhs\base\classes\BaseClass;
- class StatSaleClass extends BaseClass
- {
- public static $baseFile = '\bizGhs\stat\models\StatSale';
- public static function wastage($mainId)
- {
- $get = Yii::$app->request->get();
- $where = [];
- $where['mainId'] = $mainId;
- $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
- $start = $period['startTime'];
- $end = $period['endTime'];
- $currentStartDate = date('Y-m-d', strtotime($start));
- $currentEndDate = date('Y-m-d', strtotime($end));
- $currentStartTime = $currentStartDate . ' 00:00:00';
- $currentEndTime = $currentEndDate . ' 23:59:59';
- $where['addTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $list = StockWastageOrderClass::getAllByCondition($where, null, '*');
- $totalPrice = 0;
- $totalCost = 0;
- if (!empty($list)) {
- foreach ($list as $order) {
- $price = $order['price'] ?? 0;
- $totalPrice = bcadd($totalPrice, $price, 2);
- $cost = $order['cost'] ?? 0;
- $totalCost = bcadd($totalCost, $cost, 2);
- }
- }
- $totalPrice = floatval($totalPrice);
- return ['totalPrice' => $totalPrice, 'totalCost' => $totalCost];
- }
- //结账跟踪 ssh 20230503
- public static function OrderClear($mainId)
- {
- $get = Yii::$app->request->get();
- $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
- $timeStyle = $get['timeStyle'] ?? 0;
- if (isset($get['searchTime']) && !empty($get['searchTime'])) {
- $searchTime = $get['searchTime'];
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
- $start = $period['startTime'];
- $end = $period['endTime'];
- $currentStartDate = date('Y-m-d', strtotime($start));
- $currentEndDate = date('Y-m-d', strtotime($end));
- $currentStartTime = $currentStartDate . ' 00:00:00';
- $currentEndTime = $currentEndDate . ' 23:59:59';
- if ($timeStyle == 0) {
- $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- } else {
- $where['clearTime'] = ['between', [$currentStartTime, $currentEndTime]];
- }
- } else {
- $payTime = 'today';
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($payTime, $startTime, $endTime, true);
- $start = $period['startTime'];
- $end = $period['endTime'];
- $currentStartDate = date('Y-m-d', strtotime($start));
- $currentEndDate = date('Y-m-d', strtotime($end));
- $currentStartTime = $currentStartDate . ' 00:00:00';
- $currentEndTime = $currentEndDate . ' 23:59:59';
- $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- }
- $customId = $get['customId'] ?? 0;
- if (!empty($customId)) {
- $where['customId'] = $customId;
- }
- $unClear = $get['unClear'] ?? 0;
- if ($unClear == 1) {
- $where['clearId'] = 0;
- $where['remainDebtPrice>'] = 0;
- }
- $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
- return ['list' => $ghsOrderList];
- }
- /**
- * 客户业绩:按客户汇总笔数/扎数/金额/成本/利润,供 ghsApp customSale、/stat-kh-cg/profile 使用。
- * totalCount 为总扎数(含隔天售后扣减),与各行 count 口径一致。
- */
- public static function statCustomCg($mainId)
- {
- $get = Yii::$app->request->get();
- $where = [];
- $where['mainId'] = $mainId;
- $searchTime = !empty($get['searchTime']) ? $get['searchTime'] : 'today';
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
- $start = $period['startTime'];
- $end = $period['endTime'];
- $where['time'] = ['between', [$start, $end]];
- $currentStartDate = date('Y-m-d', strtotime($start));
- $currentEndDate = date('Y-m-d', strtotime($end));
- $currentStartTime = $currentStartDate . ' 00:00:00';
- $currentEndTime = $currentEndDate . ' 23:59:59';
- $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
- $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $customList = CustomClass::getAllByCondition(['ownMainId' => $mainId], null, 'id,name,py', 'id');
- $arr = [];
- $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
- $totalAmount = 0;
- $totalNum = 0;
- $totalCount = 0;
- $totalProfit = 0;
- $totalCost = 0;
- if (!empty($ghsOrderList)) {
- foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
- $actPrice = $ghsOrder['actPrice'] ?? 0;
- if ($actPrice <= 0) {
- continue;
- }
- $cost = $ghsOrder['remainCost'] ?? 0;
- $profit = bcsub($actPrice, $cost, 2);
- $orderPrice = $ghsOrder['orderPrice'] ?? 0;
- $tkPrice = $ghsOrder['tkPrice'] ?? 0;
- $customId = $ghsOrder['customId'] ?? 0;
- $status = $ghsOrder['status'] ?? 0;
- $book = $ghsOrder['book'] ?? 0;
- //扎数
- $count = $ghsOrder['itemNum'] ?? 0;
- if ($status == 1 || $status == 5) {
- continue;
- }
- if ($book == 1 && $status == 2) {
- continue;
- }
- $customName = $customList[$customId]['name'] ?? '';
- $py = $customList[$customId]['py'] ?? '';
- $totalNum++;
- $totalCount = bcadd($totalCount, $count, 2);
- $totalAmount = bcadd($totalAmount, $actPrice, 2);
- $totalProfit = bcadd($totalProfit, $profit, 2);
- $totalCost = bcadd($totalCost, $cost, 2);
- if (isset($arr[$customId])) {
- $arr[$customId]['num']++;
- $arr[$customId]['amount'] = bcadd($arr[$customId]['amount'], $actPrice, 2);
- $arr[$customId]['count'] = bcadd($arr[$customId]['count'], $count, 2);
- $arr[$customId]['orderPrice'] = bcadd($arr[$customId]['orderPrice'], $orderPrice, 2);
- $arr[$customId]['tkPrice'] = bcadd($arr[$customId]['tkPrice'], $tkPrice, 2);
- $arr[$customId]['cost'] = bcadd($arr[$customId]['cost'], $cost, 2);
- $arr[$customId]['profit'] = bcadd($arr[$customId]['profit'], $profit, 2);
- } else {
- $arr[$customId] = [
- 'customId' => $customId,
- 'customName' => $customName,
- 'py' => $py,
- 'source' => 'ghs',
- 'num' => 1,
- 'count' => $count,
- 'amount' => $actPrice,
- 'levelName' => '',
- 'orderPrice' => $orderPrice,
- 'tkPrice' => $tkPrice,
- 'cost' => $cost,
- 'profit' => $profit,
- ];
- }
- }
- }
- // 零售开单:与批发共用客户业绩;key 用 hd_{id},名称加「零售-」前缀
- $productCostMap = ProductClass::getAllByCondition(['mainId' => $mainId], null, 'id,cost,avCost', 'id');
- $hdWhere = [
- 'mainId' => $mainId,
- 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]],
- 'payTime' => ['between', [$currentStartTime, $currentEndTime]],
- ];
- $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
- if (!empty($hdOrderList)) {
- foreach ($hdOrderList as $hdOrder) {
- $forward = intval($hdOrder['forward'] ?? 0);
- if ($forward === 1) {
- // 售后付款属支出,不进客户业绩
- continue;
- }
- $status = intval($hdOrder['status'] ?? 0);
- if ($status === HdOrderClass::ORDER_STATUS_UN_PAY || $status === HdOrderClass::ORDER_STATUS_CANCEL) {
- continue;
- }
- $mainPay = bcadd((string)($hdOrder['mainPay'] ?? '0'), '0', 2);
- $tkPrice = bcadd((string)($hdOrder['tkPrice'] ?? '0'), '0', 2);
- $cash = bcadd((string)($hdOrder['cash'] ?? '0'), '0', 2);
- // 当天退款已从 mainPay 口径扣 tkPrice;现金另计(对齐渠道收入)
- $actPrice = bcadd(bcsub($mainPay, $tkPrice, 2), $cash, 2);
- if (bccomp($actPrice, '0', 2) <= 0) {
- continue;
- }
- $customId = intval($hdOrder['customId'] ?? 0);
- $rowKey = HdNextDayRefundClass::customStatKey($customId);
- $rawName = trim((string)($hdOrder['customName'] ?? ''));
- $orderSn = $hdOrder['orderSn'] ?? '';
- // 扎数、成本:按支付日净销量(隔天部分 nextRefundNum 留到通过日扣)
- $count = '0.00';
- $cost = '0.00';
- $orderItemList = HdOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($orderItemList)) {
- foreach ($orderItemList as $orderItem) {
- $remainNum = HdNextDayRefundClass::payDayRemainNum(
- $orderItem->num ?? 0,
- $orderItem->refundNum ?? 0,
- $orderItem->nextRefundNum ?? 0
- );
- $count = bcadd($count, $remainNum, 2);
- $productId = intval($orderItem->itemId ?? 0);
- $unitCost = $orderItem->cost ?? null;
- if ($unitCost === null || $unitCost === '') {
- $unitCost = $productCostMap[$productId]['avCost']
- ?? ($productCostMap[$productId]['cost'] ?? 0);
- }
- $cost = bcadd($cost, bcmul($remainNum, (string)$unitCost, 2), 2);
- }
- }
- $profit = bcsub($actPrice, $cost, 2);
- $totalNum++;
- $totalCount = bcadd($totalCount, $count, 2);
- $totalAmount = bcadd($totalAmount, $actPrice, 2);
- $totalProfit = bcadd($totalProfit, $profit, 2);
- $totalCost = bcadd($totalCost, $cost, 2);
- if (isset($arr[$rowKey])) {
- $arr[$rowKey]['num']++;
- $arr[$rowKey]['amount'] = bcadd($arr[$rowKey]['amount'], $actPrice, 2);
- $arr[$rowKey]['count'] = bcadd($arr[$rowKey]['count'], $count, 2);
- $arr[$rowKey]['orderPrice'] = bcadd($arr[$rowKey]['orderPrice'], $mainPay, 2);
- $arr[$rowKey]['tkPrice'] = bcadd($arr[$rowKey]['tkPrice'], $tkPrice, 2);
- $arr[$rowKey]['cost'] = bcadd($arr[$rowKey]['cost'], $cost, 2);
- $arr[$rowKey]['profit'] = bcadd($arr[$rowKey]['profit'], $profit, 2);
- } else {
- $arr[$rowKey] = [
- 'customId' => $customId,
- 'customName' => HdNextDayRefundClass::displayCustomName($rawName, $customId),
- 'py' => '',
- 'source' => 'hd',
- 'num' => 1,
- 'count' => $count,
- 'amount' => $actPrice,
- 'levelName' => '',
- 'orderPrice' => $mainPay,
- 'tkPrice' => $tkPrice,
- 'cost' => $cost,
- 'profit' => $profit,
- ];
- }
- }
- }
- // 隔天售后:按通过日扣金额/扎数;无当日开单也建负行(笔数不减)
- // ghs 批发客户用原 customId;hd 零售用 hd_{id} 隔离,避免与 xhGhsCustom 撞号
- $fwdMap = NextDayRefundClass::sumAmountGroupByCustom($mainId, $currentStartTime, $currentEndTime);
- $qtyMap = NextDayRefundClass::sumItemQtyByCustom($mainId, $currentStartTime, $currentEndTime);
- $cidKeys = array_unique(array_merge(array_keys($fwdMap), array_keys($qtyMap)));
- foreach ($cidKeys as $cid) {
- $amt = $fwdMap[$cid] ?? '0';
- $qty = $qtyMap[$cid] ?? '0';
- if (bccomp((string)$amt, '0', 2) == 0 && bccomp((string)$qty, '0', 2) == 0) {
- continue;
- }
- if (!isset($arr[$cid])) {
- $arr[$cid] = [
- 'customId' => $cid,
- 'customName' => $customList[$cid]['name'] ?? '',
- 'py' => $customList[$cid]['py'] ?? '',
- 'source' => 'ghs',
- 'num' => 0,
- 'count' => '0.00',
- 'amount' => '0.00',
- 'levelName' => '',
- 'orderPrice' => 0,
- 'tkPrice' => 0,
- 'cost' => 0,
- 'profit' => '0.00',
- ];
- }
- $arr[$cid]['amount'] = bcsub((string)$arr[$cid]['amount'], (string)$amt, 2);
- $arr[$cid]['profit'] = bcsub((string)$arr[$cid]['profit'], (string)$amt, 2);
- $arr[$cid]['count'] = bcsub((string)($arr[$cid]['count'] ?? 0), (string)$qty, 2);
- $totalAmount = bcsub((string)$totalAmount, (string)$amt, 2);
- $totalProfit = bcsub((string)$totalProfit, (string)$amt, 2);
- $totalCount = bcsub((string)$totalCount, (string)$qty, 2);
- }
- // hd 零售隔天:独立 key +「零售-」客户名
- $hdAmtMap = HdNextDayRefundClass::sumAmountGroupByCustom($mainId, $currentStartTime, $currentEndTime);
- $hdQtyMap = HdNextDayRefundClass::sumItemQtyByCustom($mainId, $currentStartTime, $currentEndTime);
- $hdCids = array_unique(array_merge(array_keys($hdAmtMap), array_keys($hdQtyMap)));
- foreach ($hdCids as $cid) {
- $amtInfo = $hdAmtMap[$cid] ?? null;
- $qtyInfo = $hdQtyMap[$cid] ?? null;
- $amt = is_array($amtInfo) ? ($amtInfo['amount'] ?? '0') : '0';
- $qty = is_array($qtyInfo) ? ($qtyInfo['num'] ?? '0') : '0';
- if (bccomp((string)$amt, '0', 2) == 0 && bccomp((string)$qty, '0', 2) == 0) {
- continue;
- }
- $rowKey = HdNextDayRefundClass::customStatKey($cid);
- $rawName = '';
- $py = '';
- if (is_array($amtInfo)) {
- $rawName = $amtInfo['customName'] ?? '';
- $py = $amtInfo['py'] ?? '';
- }
- if ($rawName === '' && is_array($qtyInfo)) {
- $rawName = $qtyInfo['customName'] ?? '';
- if ($py === '') {
- $py = $qtyInfo['py'] ?? '';
- }
- }
- if (!isset($arr[$rowKey])) {
- $arr[$rowKey] = [
- 'customId' => $cid,
- 'customName' => HdNextDayRefundClass::displayCustomName($rawName, $cid),
- 'py' => $py,
- 'source' => 'hd',
- 'num' => 0,
- 'count' => '0.00',
- 'amount' => '0.00',
- 'levelName' => '',
- 'orderPrice' => 0,
- 'tkPrice' => 0,
- 'cost' => 0,
- 'profit' => '0.00',
- ];
- }
- $arr[$rowKey]['amount'] = bcsub((string)$arr[$rowKey]['amount'], (string)$amt, 2);
- $arr[$rowKey]['profit'] = bcsub((string)$arr[$rowKey]['profit'], (string)$amt, 2);
- $arr[$rowKey]['count'] = bcsub((string)($arr[$rowKey]['count'] ?? 0), (string)$qty, 2);
- $totalAmount = bcsub((string)$totalAmount, (string)$amt, 2);
- $totalProfit = bcsub((string)$totalProfit, (string)$amt, 2);
- $totalCount = bcsub((string)$totalCount, (string)$qty, 2);
- }
- if (!empty($arr)) {
- $arr = arrayUtil::arraySort($arr, 'profit');
- }
- $totalAmount = floatval($totalAmount);
- return ['list' => $arr, 'totalAmount' => $totalAmount, 'totalNum' => $totalNum, 'totalCount' => floatval($totalCount), 'totalCost' => $totalCost, 'totalProfit' => $totalProfit];
- }
- public static function profile($mainId)
- {
- //收入:批发开单、零售开单(含门店、美团和微信客服)、出库
- //支出:采购、调拨入库、批发退款、零售退款、店租、水电、伙食
- $get = Yii::$app->request->get();
- $where = [];
- $where['mainId'] = $mainId;
- $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
- $start = $period['startTime'];
- $end = $period['endTime'];
- $where['time'] = ['between', [$start, $end]];
- $currentStartDate = date('Y-m-d', strtotime($start));
- $currentEndDate = date('Y-m-d', strtotime($end));
- $currentStartTime = $currentStartDate . ' 00:00:00';
- $currentEndTime = $currentEndDate . ' 23:59:59';
- //出库
- $stockOut = StatStockOutClass::getSum($where);
- //采购入库
- $cgWhere = ['mainId' => $mainId, 'status' => 4];
- $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
- $cg = 0;
- if (!empty($cgList)) {
- foreach ($cgList as $cgKey => $cgItem) {
- $actPrice = $cgItem['actPrice'] ?? 0;
- $paidPrice = $cgItem['paidPrice'] ?? 0;
- if ($actPrice <= 0) {
- // 如果$actPrice=0,表示这个采购单已经全部退款,不需要再统计成本,本来$paidPrice要同步清零,但没有,这个问题由徐州邂逅花坊马总,提出的需求发现
- $cg = bcadd($cg, 0, 2);
- } else {
- $total = bcadd($actPrice, $paidPrice, 2);
- //物流费用不算成本。意思是说,长短途运费+提货费+本地运费,这四项和打包费一起,参与到平摊花材运费成本的计算,但那四项不参与总的采购成本登记,因为,有另外地方会独立登记运费成本。
- //$paidPrice 字段说明,此字段表示收货方已付款金额,包括 $localCharge本地运费、 $pickCharge提货费、 $shortCharge短途运费(到付时算,如果不是到付,不算在$paidPrice里)、$longCharge长途运费(到付时算,如果不是到付,不算在$paidPrice里)
- //请搜索关键词 cg_item_cost ,有多处相似情况要考虑
- if (isset($cgItem['isCost']) && $cgItem['isCost'] == 0) {
- $shortCharge = $cgItem['shortCharge'] ?? 0;
- $longCharge = $cgItem['longCharge'] ?? 0;
- $pickCharge = $cgItem['pickCharge'] ?? 0;
- $localCharge = $cgItem['localCharge'] ?? 0;
- $a1 = bcadd($shortCharge, $longCharge, 2);
- $a2 = bcadd($pickCharge, $localCharge, 2);
- $a = bcadd($a1, $a2, 2);
- //注意下面是减号 bcsub
- $total = bcsub($total, $a, 2);
- }
- $cg = bcadd($cg, $total, 2);
- }
- }
- }
- $cg = floatval($cg);
- $lsCgWhere = ['mainId' => $mainId, 'status' => 4, 'belongCost' => 1];
- $lsCgWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $lsCgList = PurchaseClass::getAllByCondition($lsCgWhere, null, '*');
- $lsCgAmount = 0;
- if (!empty($lsCgList)) {
- foreach ($lsCgList as $lsCgItem) {
- $actPrice = $lsCgItem['actPrice'] ?? 0;
- $lsCgAmount = bcadd($lsCgAmount, $actPrice, 2);
- }
- }
- $lsCgAmount = floatval($lsCgAmount);
- $lzWhere = ['mainId' => $mainId, 'status' => 3];
- $lzWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $lzCgList = PlantCgClass::getAllByCondition($lzWhere, null, '*');
- $lzCgAmount = 0;
- if (!empty($lzCgList)) {
- foreach ($lzCgList as $lzInfo) {
- $actPrice = $lzInfo['actPrice'] ?? 0;
- $lzCgAmount = bcadd($actPrice, $lzCgAmount, 2);
- }
- }
- $lzCgAmount = floatval($lzCgAmount);
- //调拨入库
- $stockIn = StatStockInClass::getSum($where);
- $totalOrderNum = 0;
- //批发开单
- $pfIncome = 0;
- $pfSendCost = 0;
- $pfPack = 0;
- $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
- $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
- if (!empty($ghsOrderList)) {
- foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
- $actPrice = $ghsOrder['actPrice'] ?? 0;
- $sendCost = $ghsOrder['sendCost'] ?? 0;
- $refundSendCost = $ghsOrder['refundSendCost'] ?? 0;
- $remainSendCost = bcsub($sendCost, $refundSendCost, 2);
- $packCost = $ghsOrder['packCost'] ?? 0;
- $refundPackCost = $ghsOrder['refundPackCost'] ?? 0;
- $remainPackCost = bcsub($packCost, $refundPackCost, 2);
- $pfIncome = bcadd($pfIncome, $actPrice, 2);
- $pfSendCost = bcadd($pfSendCost, $remainSendCost, 2);
- $pfPack = bcadd($pfPack, $remainPackCost, 2);
- $totalOrderNum++;
- }
- }
- // 隔天售后扣减批发收入(笔数不变)
- $pfIncome = bcsub(
- (string)$pfIncome,
- NextDayRefundClass::sumAmountByMainAndTime($mainId, $currentStartTime, $currentEndTime),
- 2
- );
- //零售开单
- $payCodeIncome = 0;
- $lsAfterSale = 0;
- $lsGiveBalance = 0;
- $lsIncome = 0;
- $lsSendCost = 0;
- $lsServiceFee = 0;
- $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
- $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
- foreach ($hdOrderList as $hdKey => $hdOrder) {
- $actPrice = $hdOrder['mainPay'] ?? 0;
- $onlinePay = $hdOrder['onlinePay'] ?? 1;
- $repeat = $hdOrder['repeat'] ?? 0;
- $forward = $hdOrder['forward'] ?? 0;
- if ($hdOrder['tkPrice'] && $hdOrder['tkPrice'] > 0) {
- $actPrice = bcsub($actPrice, $hdOrder['tkPrice'], 2);
- }
- if ($forward == 1) {
- //零售售后付款
- $lsAfterSale = bcadd($lsAfterSale, $actPrice, 2);
- } else {
- $cash = $hdOrder['cash'] ?? 0;
- $serviceFee = $hdOrder['serviceFee'] ?? 0;
- $sendCost = $hdOrder['sendCost'] ?? 0;
- $lsIncome = bcadd($actPrice, $lsIncome, 2);
- $lsIncome = bcadd($lsIncome, $cash, 2);
- //如果$actPrice<=0,则退款取消先不计算成本
- if ($actPrice > 0) {
- $lsSendCost = bcadd($lsSendCost, $sendCost, 2);
- $lsServiceFee = bcadd($lsServiceFee, $serviceFee, 2);
- }
- $totalOrderNum++;
- if ($onlinePay == 2 && floatval($cash) <= 0 && $repeat == 1) {
- $payCodeIncome = bcadd($payCodeIncome, $actPrice, 2);
- $payCodeIncome = floatval($payCodeIncome);
- }
- $giveBalance = $hdOrder['giveBalance'] ?? 0;
- $giveBalanceTk = $hdOrder['giveBalanceTk'] ?? 0;
- $remainGiveBalance = bcsub($giveBalance, $giveBalanceTk, 2);
- $lsGiveBalance = bcadd($lsGiveBalance, $remainGiveBalance, 2);
- }
- }
- // 隔天售后扣减零售收入(当天退款已在 tkPrice 扣过;隔天记 nextDayTkPrice,按通过日扣)
- $lsIncome = bcsub(
- (string)$lsIncome,
- HdNextDayRefundClass::sumAmountByMainAndTime($mainId, $currentStartTime, $currentEndTime),
- 2
- );
- $condition = ['mainId' => $mainId, 'addTime' => ['between', [$currentStartTime, $currentEndTime]],];
- $salary = 0;
- $salaryList = SalaryClass::getAllByCondition($condition, null, '*', null, true);
- if (!empty($salaryList)) {
- foreach ($salaryList as $item) {
- $salary = bcadd($salary, $item->amount, 2);
- }
- }
- $condition = [
- 'mainId' => $mainId,
- 'payTime' => ['between', [$currentStartTime, $currentEndTime]],
- 'delStatus' => 0,
- ];
- $expendList = ExpendClass::getAllByCondition($condition, null, '*', null, true);
- $expendTypeMap = dict::getDict('expendTypeMap');
- $expendAmount = [];
- if (!empty($expendList)) {
- foreach ($expendList as $item) {
- $type = $item->type ?? 0;
- $amount = $item->amount ?? 0;
- $name = $expendTypeMap[$type] ?? '暂无';
- if (!isset($expendAmount[$type]['amount'])) {
- $expendAmount[$type]['amount'] = 0;
- }
- $expendAmount[$type]['name'] = $name;
- $expendAmount[$type]['amount'] = floatval(bcadd($expendAmount[$type]['amount'], $amount, 2));
- }
- }
- $income = [
- ['name' => '批发', 'id' => 'pf', 'amount' => floatval($pfIncome)],
- ['name' => '零售', 'id' => 'ls', 'amount' => floatval($lsIncome)],
- ['name' => '调拨出库', 'id' => 'allot', 'amount' => floatval($stockOut)],
- ];
- $expend = [
- ['name' => '批发运费', 'id' => 'pfSendCost', 'amount' => floatval($pfSendCost)],
- ['name' => '批发包装费', 'id' => 'pfSendCost', 'amount' => floatval($pfPack)],
- ['name' => '零售运费', 'id' => 'lsSendCost', 'amount' => floatval($lsSendCost)],
- ['name' => '零售售后付款', 'id' => 'lsSendCost', 'amount' => floatval($lsAfterSale)],
- ['name' => '美团手续费', 'id' => 'mtFee', 'amount' => floatval($lsServiceFee)],
- ['name' => '花材采购', 'id' => 'cg', 'amount' => floatval($cg)],
- ['name' => '绿植资材采购', 'id' => 'cg', 'amount' => floatval($lzCgAmount)],
- ['name' => '零售花材采购', 'id' => 'cg', 'amount' => floatval($lsCgAmount)],
- ['name' => '调拨入库', 'id' => 'allot', 'amount' => floatval($stockIn)],
- ['name' => '员工工资', 'id' => 'salary', 'amount' => floatval($salary)],
- ['name' => '收款码收入', 'id' => 'salary', 'amount' => floatval($payCodeIncome)],
- ['name' => '零售充值赠送', 'id' => 'salary', 'amount' => floatval($lsGiveBalance)],
- ];
- if (!empty($expendAmount)) {
- foreach ($expendAmount as $it) {
- $expend[] = $it;
- }
- }
- $balance = 0;
- foreach ($income as $it) {
- $amount = $it['amount'] ?? 0;
- $balance = bcadd($amount, $balance, 2);
- }
- foreach ($expend as $it) {
- $amount = $it['amount'] ?? 0;
- $balance = bcsub($balance, $amount, 2);
- }
- return ['income' => $income, 'expend' => $expend, 'balance' => floatval($balance), 'totalOrderNum' => $totalOrderNum];
- }
- //按花材统计收入 ssh 20231111
- public static function incomeOutItem($mainId)
- {
- $get = Yii::$app->request->get();
- $export = $get['export'] ?? 0;
- $where = [];
- $where['mainId'] = $mainId;
- $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
- $start = $period['startTime'];
- $end = $period['endTime'];
- $where['time'] = ['between', [$start, $end]];
- $currentStartDate = date('Y-m-d', strtotime($start));
- $currentEndDate = date('Y-m-d', strtotime($end));
- $currentStartTime = $currentStartDate . ' 00:00:00';
- $currentEndTime = $currentEndDate . ' 23:59:59';
- $s = strtotime($currentStartTime);
- $e = strtotime($currentEndTime);
- $p = bcsub($e, $s);
- $xx = bcmul(86400, 90);
- if ($p > $xx) {
- util::fail('查询时间不能超过三个月,编号666');
- }
- $productList = ProductClass::getAllByCondition(['mainId' => $mainId], null, 'id,name', 'id');
- if (empty($productList)) {
- util::fail('没有找到花材');
- }
- $arr = [];
- //打包费和运费
- $packFreight = 0;
- $onlyRefundCg = 0;
- $onlyRefund = 0;
- $totalDiscountAmount = 0;
- $totalLabourCost = 0;
- //采购入库
- $tbCgIncome = [];
- $tbCgIncomeStaff = [];
- $cgWhere = ['mainId' => $mainId, 'status' => 4];
- $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
- if (!empty($cgList)) {
- foreach ($cgList as $cgKey => $cgItem) {
- $packingCharge = $cgItem['packingCharge'] ?? 0;
- $shortCharge = $cgItem['shortCharge'] ?? 0;
- $longCharge = $cgItem['longCharge'] ?? 0;
- $pickCharge = $cgItem['pickCharge'] ?? 0;
- $localCharge = $cgItem['localCharge'] ?? 0;
- $packFreight = bcadd($packFreight, $packingCharge, 2);
- $packFreight = bcadd($packFreight, $shortCharge, 2);
- $packFreight = bcadd($packFreight, $longCharge, 2);
- $packFreight = bcadd($packFreight, $pickCharge, 2);
- $packFreight = bcadd($packFreight, $localCharge, 2);
- $tkPrice = $cgItem['tkPrice'] ?? 0;
- $orderSn = $cgItem['orderSn'] ?? '';
- $cgItemInfoList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($cgItemInfoList)) {
- foreach ($cgItemInfoList as $cgData) {
- $itemNum = $cgData->itemNum ?? 0;
- $productId = $cgData->productId ?? 0;
- $refundNum = $cgData->refundNum ?? 0;
- $remainNum = bcsub($itemNum, $refundNum);
- $bigPrice = $cgData->bigPrice ?? 0;
- $amount = bcmul($remainNum, $bigPrice, 2);
- $itemName = $productList[$productId]['name'] ?? '已删除';
- $arr[$productId]['name'] = $itemName;
- $arr[$productId]['id'] = $productId;
- if (isset($arr[$productId]['expend'])) {
- $arr[$productId]['expend'] = bcadd($amount, $arr[$productId]['expend'], 2);
- $arr[$productId]['outNum'] = bcadd($arr[$productId]['outNum'], $remainNum, 2);
- } else {
- $arr[$productId]['expend'] = $amount;
- $arr[$productId]['outNum'] = $remainNum;
- $arr[$productId]['inNum'] = 0;
- $arr[$productId]['income'] = 0;
- }
- }
- if ($tkPrice > 0) {
- $cgRefundOrderList = CgRefundClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
- if (!empty($cgRefundOrderList)) {
- foreach ($cgRefundOrderList as $cgRefundOrder) {
- if ($cgRefundOrder->refundType == 2) {
- $onlyRefundCg = bcadd($cgRefundOrder->refundPrice, $onlyRefundCg, 2);
- }
- }
- }
- }
- }
- }
- }
- //批发开单
- $pfIncome = 0;
- $tbSaleIncome = [];
- $tbSaleIncomeStaff = [];
- $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
- $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
- if (!empty($ghsOrderList)) {
- foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
- $book = $ghsOrder['book'] ?? 0;
- $status = $ghsOrder['status'] ?? 0;
- if ($book == 1 && $status == 2) {
- //预订单待发货的不算收入
- continue;
- }
- if ($status == 0 || $status == 5) {
- continue;
- }
- $shopAdminId = $ghsOrder['shopAdminId'] ?? 0;
- $shopAdminName = $ghsOrder['shopAdminName'] ?? '';
- $orderSn = $ghsOrder['orderSn'] ?? '';
- $actPrice = $ghsOrder['actPrice'] ?? 0;
- $tkPrice = $ghsOrder['tkPrice'] ?? 0;
- $pfIncome = bcadd($pfIncome, $actPrice, 2);
- $labourCost = $ghsOrder['labourCost'] ?? 0;
- $discountAmount = $ghsOrder['discountAmount'] ?? 0;
- $totalLabourCost = bcadd($labourCost, $totalLabourCost, 2);
- $totalDiscountAmount = bcadd($totalDiscountAmount, $discountAmount, 2);
- $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($orderItemList)) {
- foreach ($orderItemList as $orderItem) {
- $refundNum = $orderItem->refundNum ?? 0;
- $name = $orderItem->name ?? '';
- $productId = $orderItem->productId ?? 0;
- $num = $orderItem->xhNum ?? 0;
- // 支付日净销量:当天已退=refundNum-nextRefundNum
- $remainNum = NextDayRefundClass::payDayRemainNum(
- $num,
- $refundNum,
- $orderItem->nextRefundNum ?? 0
- );
- $xhUnitPrice = $orderItem->xhUnitPrice ?? 0;
- $amount = bcmul($remainNum, $xhUnitPrice, 2);
- $itemName = $productList[$productId]['name'] ?? '已删除';
- $arr[$productId]['name'] = $itemName;
- $arr[$productId]['id'] = $productId;
- if (isset($arr[$productId]['income'])) {
- $arr[$productId]['income'] = bcadd($amount, $arr[$productId]['income'], 2);
- $arr[$productId]['inNum'] = bcadd($arr[$productId]['inNum'], $remainNum, 2);
- } else {
- $arr[$productId]['expend'] = 0;
- $arr[$productId]['outNum'] = 0;
- $arr[$productId]['inNum'] = $remainNum;
- $arr[$productId]['income'] = $amount;
- }
- //单独统计
- $belongCost = $orderItem->belongCost ?? 0;
- if ($belongCost == 1) {
- if (isset($tbSaleIncome[$productId])) {
- $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
- $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
- } else {
- $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
- }
- if (isset($tbSaleIncomeStaff[$productId]) && isset($tbSaleIncomeStaff[$productId][$shopAdminId])) {
- $tbSaleIncomeStaff[$productId][$shopAdminId]['num'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['num'], $remainNum);
- $tbSaleIncomeStaff[$productId][$shopAdminId]['price'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['price'], $amount, 2);
- } else {
- $tbSaleIncomeStaff[$productId][$shopAdminId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount, 'staffName' => $shopAdminName];
- }
- }
- }
- if ($tkPrice > 0) {
- $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
- if (!empty($refundList)) {
- foreach ($refundList as $refund) {
- if ($refund->refundType == 1) {
- //退货退款前面已经考虑,这里不再处理 OK
- } else {
- // 隔天仅退款记在通过日,这里跳过避免与下方 sumMoneyOnly 重复
- if (intval($refund->sameDay ?? RefundOrderClass::SAME_DAY_YES) === RefundOrderClass::SAME_DAY_NO) {
- continue;
- }
- $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
- }
- }
- }
- }
- }
- }
- }
- // 隔天售后:支付日已用 nextRefundNum;通过日再扣数量/金额;仅退款并入销售仅退款
- $onlyRefund = bcadd(
- (string)$onlyRefund,
- self::applyNextDayAdjustToProductIncomeRows(
- $arr,
- $tbSaleIncome,
- $productList,
- $mainId,
- $currentStartTime,
- $currentEndTime
- ),
- 2
- );
- //零售开单
- $lsIncome = 0;
- $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
- $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
- foreach ($hdOrderList as $hdKey => $hdOrder) {
- $actPrice = $hdOrder['mainPay'] ?? 0;
- $cash = $hdOrder['cash'] ?? 0;
- $lsIncome = bcadd($actPrice, $lsIncome, 2);
- $lsIncome = bcadd($lsIncome, $cash, 2);
- $orderSn = $hdOrder['orderSn'] ?? '';
- $tkPrice = $hdOrder['tkPrice'] ?? 0;
- $orderItemList = \bizHd\order\classes\OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($orderItemList)) {
- foreach ($orderItemList as $orderItem) {
- $unitPrice = $orderItem->unitPrice ?? 0;
- $refundNum = $orderItem->refundNum ?? 0;
- $num = $orderItem->num ?? 0;
- $productId = $orderItem->itemId ?? 0;
- // 支付日净销量:隔天部分记 nextRefundNum,通过日再扣
- $remainNum = HdNextDayRefundClass::payDayRemainNum(
- $num,
- $refundNum,
- $orderItem->nextRefundNum ?? 0
- );
- $amount = bcmul($remainNum, $unitPrice, 2);
- $itemName = $productList[$productId]['name'] ?? '已删除';
- $arr[$productId]['name'] = $itemName;
- $arr[$productId]['id'] = $productId;
- if (isset($arr[$productId]['income'])) {
- $arr[$productId]['income'] = bcadd($amount, $arr[$productId]['income'], 2);
- $arr[$productId]['inNum'] = bcadd($arr[$productId]['inNum'], $remainNum, 2);
- } else {
- $arr[$productId]['expend'] = 0;
- $arr[$productId]['outNum'] = 0;
- $arr[$productId]['inNum'] = $remainNum;
- $arr[$productId]['income'] = $amount;
- }
- }
- if ($tkPrice > 0) {
- $refundList = HdRefundClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($refundList)) {
- foreach ($refundList as $refund) {
- if ($refund->refundType == 2) {
- // 隔天仅退款记在通过日,避免与 applyNextDayAdjust 重复
- if (intval($refund->sameDay ?? HdRefundClass::SAME_DAY_YES) === HdRefundClass::SAME_DAY_NO) {
- continue;
- }
- $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
- }
- }
- }
- }
- }
- }
- $totalIncome = 0;
- $totalExpend = 0;
- if (!empty($arr)) {
- foreach ($arr as $key => $val) {
- $income = $val['income'] ?? 0;
- $income = floatval($income);
- $totalIncome = bcadd($totalIncome, $income, 2);
- $expend = $val['expend'] ?? 0;
- $expend = floatval($expend);
- $totalExpend = bcadd($totalExpend, $expend, 2);
- $profit = bcsub($income, $expend, 2);
- $arr[$key]['profit'] = floatval($profit);
- $arr[$key]['income'] = floatval($income);
- $arr[$key]['expend'] = floatval($expend);
- }
- }
- //总成本算上物流打包等费用
- $totalExpend = bcadd($totalExpend, $packFreight, 2);
- //总收入算上采购仅退款
- $totalIncome = bcadd($totalIncome, $onlyRefundCg, 2);
- //总收入加上人工费
- $totalIncome = bcadd($totalIncome, $totalLabourCost, 2);
- //总收入减掉优惠金额
- $totalIncome = bcsub($totalIncome, $totalDiscountAmount, 2);
- //总成本算上销售仅退款
- $totalExpend = bcadd($totalExpend, $onlyRefund, 2);
- $totalProfit = bcsub($totalIncome, $totalExpend, 2);
- $totalIncome = floatval($totalIncome);
- $totalExpend = floatval($totalExpend);
- $totalProfit = floatval($totalProfit);
- $arr = array_values($arr);
- $arr = arrayUtil::arraySort($arr, 'profit');
- if ($export == 1) {
- $statData = [
- ['id' => 0, 'name' => '物流打包费', 'income' => 0, 'expend' => $packFreight, 'profit' => -$packFreight, 'inNum' => 0, 'outNum' => 0],
- ['id' => 0, 'name' => '采购仅退款', 'income' => $onlyRefundCg, 'expend' => 0, 'profit' => $onlyRefundCg, 'inNum' => 0, 'outNum' => 0],
- ['id' => 0, 'name' => '销售仅退款', 'income' => 0, 'expend' => $onlyRefund, 'profit' => -$onlyRefund, 'inNum' => 0, 'outNum' => 0],
- ['id' => 0, 'name' => '附加人工费', 'income' => $totalLabourCost, 'expend' => 0, 'profit' => $totalLabourCost, 'inNum' => 0, 'outNum' => 0],
- ['id' => 0, 'name' => '优惠金额', 'income' => -$totalDiscountAmount, 'expend' => 0, 'profit' => -$totalDiscountAmount, 'inNum' => 0, 'outNum' => 0],
- ['id' => 0, 'name' => '【汇总金额】', 'income' => $totalIncome, 'expend' => $totalExpend, 'profit' => $totalProfit, 'inNum' => 0, 'outNum' => 0],
- ];
- $list = array_merge($arr, $statData);
- self::exportIncomeOutItem($list, $mainId);
- }
- util::success(['list' => $arr, 'packFreight' => $packFreight, 'onlyRefundCg' => $onlyRefundCg, 'onlyRefund' => $onlyRefund,
- 'totalIncome' => $totalIncome,
- 'totalExpend' => $totalExpend,
- 'totalProfit' => $totalProfit,
- 'tbSaleIncome' => $tbSaleIncome,
- 'tbCgIncome' => $tbCgIncome,
- 'tbCgIncomeStaff' => $tbCgIncomeStaff,
- 'tbSaleIncomeStaff' => $tbSaleIncomeStaff,
- 'totalLabourCost' => $totalLabourCost,
- 'totalDiscountAmount' => $totalDiscountAmount,
- ]);
- }
- //按分类统计收入 ssh 20231111
- public static function classProfile($mainId)
- {
- $get = Yii::$app->request->get();
- $export = $get['export'] ?? 0;
- $where = [];
- $where['mainId'] = $mainId;
- $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
- $start = $period['startTime'];
- $end = $period['endTime'];
- $where['time'] = ['between', [$start, $end]];
- $currentStartDate = date('Y-m-d', strtotime($start));
- $currentEndDate = date('Y-m-d', strtotime($end));
- $currentStartTime = $currentStartDate . ' 00:00:00';
- $currentEndTime = $currentEndDate . ' 23:59:59';
- $s = strtotime($currentStartTime);
- $e = strtotime($currentEndTime);
- $p = bcsub($e, $s);
- $xx = bcmul(86400, 90);
- if ($p > $xx) {
- util::fail('查询时间不能超过三个月,编号780');
- }
- $classList = ItemClassClass::getAllByCondition(['mainId' => $mainId], null, 'id,name', 'id');
- if (empty($classList)) {
- util::fail('没有找到花材');
- }
- $arr = [];
- //打包费和运费
- $packFreight = 0;
- $onlyRefundCg = 0;
- $onlyRefund = 0;
- $totalDiscountAmount = 0;
- $totalLabourCost = 0;
- //采购入库
- $tbCgIncome = [];
- $tbCgIncomeStaff = [];
- $cgWhere = ['mainId' => $mainId, 'status' => 4];
- $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
- if (!empty($cgList)) {
- foreach ($cgList as $cgKey => $cgItem) {
- $packingCharge = $cgItem['packingCharge'] ?? 0;
- $shortCharge = $cgItem['shortCharge'] ?? 0;
- $longCharge = $cgItem['longCharge'] ?? 0;
- $pickCharge = $cgItem['pickCharge'] ?? 0;
- $localCharge = $cgItem['localCharge'] ?? 0;
- $cgStaffId = $cgItem['cgStaffId'] ?? 0;
- $cgStaffName = $cgItem['cgStaffName'] ?? '';
- $actPrice = $cgItem['actPrice'] ?? 0;
- if ($actPrice <= 0) {
- continue;
- }
- $packFreight = bcadd($packFreight, $packingCharge, 2);
- $packFreight = bcadd($packFreight, $shortCharge, 2);
- $packFreight = bcadd($packFreight, $longCharge, 2);
- $packFreight = bcadd($packFreight, $pickCharge, 2);
- $packFreight = bcadd($packFreight, $localCharge, 2);
- $tkPrice = $cgItem['tkPrice'] ?? 0;
- $orderSn = $cgItem['orderSn'] ?? '';
- $cgItemInfoList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($cgItemInfoList)) {
- foreach ($cgItemInfoList as $cgData) {
- $itemNum = $cgData->itemNum ?? 0;
- $productId = $cgData->productId ?? 0;
- $name = $cgData->name ?? '';
- $refundNum = $cgData->refundNum ?? 0;
- $remainNum = bcsub($itemNum, $refundNum);
- $bigPrice = $cgData->bigPrice ?? 0;
- $amount = bcmul($remainNum, $bigPrice, 2);
- $classId = $cgData->classId;
- $className = $classList[$classId]['name'] ?? '已删除';
- if ($className == '运费') {
- //noticeUtil::push("{$cgData->orderSn}运费项", '15280215347');
- }
- $arr[$classId]['className'] = $className;
- $arr[$classId]['classId'] = $classId;
- if (isset($arr[$classId]['expend'])) {
- $arr[$classId]['expend'] = bcadd($amount, $arr[$classId]['expend'], 2);
- $arr[$classId]['cgNum'] = bcadd($arr[$classId]['cgNum'], $remainNum);
- } else {
- $arr[$classId]['expend'] = $amount;
- $arr[$classId]['income'] = 0;
- $arr[$classId]['cgNum'] = $remainNum;
- $arr[$classId]['saleNum'] = 0;
- }
- //单独统计
- $belongCost = $cgData->belongCost ?? 0;
- if ($belongCost == 1) {
- if (isset($tbCgIncome[$productId])) {
- $tbCgIncome[$productId]['num'] = bcadd($tbCgIncome[$productId]['num'], $remainNum);
- $tbCgIncome[$productId]['price'] = bcadd($tbCgIncome[$productId]['price'], $amount, 2);
- } else {
- $tbCgIncome[$productId] = ['num' => $remainNum, 'name' => $name, 'price' => $amount];
- }
- if (isset($tbCgIncomeStaff[$productId]) && isset($tbCgIncomeStaff[$productId][$cgStaffId])) {
- $tbCgIncomeStaff[$productId][$cgStaffId]['num'] = bcadd($tbCgIncomeStaff[$productId][$cgStaffId]['num'], $remainNum);
- $tbCgIncomeStaff[$productId][$cgStaffId]['price'] = bcadd($tbCgIncomeStaff[$productId][$cgStaffId]['price'], $amount, 2);
- } else {
- $tbCgIncomeStaff[$productId][$cgStaffId] = ['num' => $remainNum, 'name' => $name, 'price' => $amount, 'staffName' => $cgStaffName,];
- }
- }
- }
- if ($tkPrice > 0) {
- $cgRefundOrderList = CgRefundClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
- if (!empty($cgRefundOrderList)) {
- foreach ($cgRefundOrderList as $cgRefundOrder) {
- if ($cgRefundOrder->refundType == 2) {
- $onlyRefundCg = bcadd($cgRefundOrder->refundPrice, $onlyRefundCg, 2);
- }
- }
- }
- }
- }
- }
- }
- //批发开单
- $pfIncome = 0;
- $tbSaleIncome = [];
- $tbSaleIncomeStaff = [];
- $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
- $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
- if (!empty($ghsOrderList)) {
- foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
- $book = $ghsOrder['book'] ?? 0;
- $status = $ghsOrder['status'] ?? 0;
- if ($book == 1 && $status == 2) {
- //预订单待发货的不算收入
- continue;
- }
- if ($status == 0 || $status == 5) {
- continue;
- }
- $shopAdminId = $ghsOrder['shopAdminId'] ?? 0;
- $shopAdminName = $ghsOrder['shopAdminName'] ?? '';
- $orderSn = $ghsOrder['orderSn'] ?? '';
- $actPrice = $ghsOrder['actPrice'] ?? 0;
- if ($actPrice <= 0) {
- continue;
- }
- $tkPrice = $ghsOrder['tkPrice'] ?? 0;
- $pfIncome = bcadd($pfIncome, $actPrice, 2);
- $labourCost = $ghsOrder['labourCost'] ?? 0;
- $discountAmount = $ghsOrder['discountAmount'] ?? 0;
- $totalLabourCost = bcadd($labourCost, $totalLabourCost, 2);
- $totalDiscountAmount = bcadd($totalDiscountAmount, $discountAmount, 2);
- $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($orderItemList)) {
- foreach ($orderItemList as $orderItem) {
- $refundNum = $orderItem->refundNum ?? 0;
- $name = $orderItem->name ?? '';
- $productId = $orderItem->productId ?? 0;
- $num = $orderItem->xhNum ?? 0;
- $remainNum = NextDayRefundClass::payDayRemainNum(
- $num,
- $refundNum,
- $orderItem->nextRefundNum ?? 0
- );
- $classId = $orderItem->classId ?? 0;
- $className = $classList[$classId]['name'] ?? '已删除';
- $xhUnitPrice = $orderItem->xhUnitPrice ?? 0;
- $amount = bcmul($remainNum, $xhUnitPrice, 2);
- $arr[$classId]['className'] = $className;
- $arr[$classId]['classId'] = $classId;
- if (isset($arr[$classId]['income'])) {
- $arr[$classId]['income'] = bcadd($amount, $arr[$classId]['income'], 2);
- $arr[$classId]['saleNum'] = bcadd($arr[$classId]['saleNum'], $remainNum);
- } else {
- $arr[$classId]['expend'] = 0;
- $arr[$classId]['income'] = $amount;
- $arr[$classId]['saleNum'] = $remainNum;
- $arr[$classId]['cgNum'] = 0;
- }
- //单独统计
- $belongCost = $orderItem->belongCost ?? 0;
- if ($belongCost == 1) {
- if (isset($tbSaleIncome[$productId])) {
- $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
- $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
- } else {
- $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
- }
- if (isset($tbSaleIncomeStaff[$productId]) && isset($tbSaleIncomeStaff[$productId][$shopAdminId])) {
- $tbSaleIncomeStaff[$productId][$shopAdminId]['num'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['num'], $remainNum);
- $tbSaleIncomeStaff[$productId][$shopAdminId]['price'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['price'], $amount, 2);
- } else {
- $tbSaleIncomeStaff[$productId][$shopAdminId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount, 'staffName' => $shopAdminName];
- }
- }
- }
- if ($tkPrice > 0) {
- $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
- if (!empty($refundList)) {
- foreach ($refundList as $refund) {
- if ($refund->refundType == 1) {
- //退货退款前面已经考虑,这里不再处理 OK
- } else {
- // 隔天仅退款记在通过日,这里跳过避免与下方 sumMoneyOnly 重复
- if (intval($refund->sameDay ?? RefundOrderClass::SAME_DAY_YES) === RefundOrderClass::SAME_DAY_NO) {
- continue;
- }
- $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
- }
- }
- }
- }
- }
- }
- }
- // 隔天售后:支付日已用 nextRefundNum;通过日再按售后单扣分类收入;仅退款进「销售仅退款」
- $productClassMap = ProductClass::getAllByCondition(['mainId' => $mainId], null, 'id,classId', 'id');
- $applyClassDelta = function ($pidMap, $sign) use (&$arr, &$classList, $productClassMap) {
- foreach ($pidMap as $pid => $row) {
- $classId = $productClassMap[$pid]['classId'] ?? 0;
- $deltaNum = bcmul((string)($row['num'] ?? '0'), (string)$sign, 2);
- $deltaAmt = bcmul((string)($row['amount'] ?? '0'), (string)$sign, 2);
- if (!isset($arr[$classId])) {
- if (bccomp($deltaAmt, '0', 2) == 0 && bccomp($deltaNum, '0', 2) == 0) {
- continue;
- }
- $arr[$classId] = [
- 'className' => $classList[$classId]['name'] ?? '已删除',
- 'classId' => $classId,
- 'expend' => 0,
- 'income' => '0.00',
- 'saleNum' => '0.00',
- 'cgNum' => 0,
- ];
- }
- $arr[$classId]['income'] = bcadd((string)($arr[$classId]['income'] ?? 0), $deltaAmt, 2);
- $arr[$classId]['saleNum'] = bcadd((string)($arr[$classId]['saleNum'] ?? 0), $deltaNum, 2);
- }
- };
- $applyClassDelta(
- NextDayRefundClass::sumItemByProduct($mainId, $currentStartTime, $currentEndTime),
- -1
- );
- // hd 零售隔天退货:同样按花材归属分类扣减
- $applyClassDelta(
- HdNextDayRefundClass::sumItemByProduct($mainId, $currentStartTime, $currentEndTime),
- -1
- );
- $onlyRefund = bcadd(
- (string)$onlyRefund,
- NextDayRefundClass::sumMoneyOnlyAmountByMainAndTime($mainId, $currentStartTime, $currentEndTime),
- 2
- );
- $onlyRefund = bcadd(
- (string)$onlyRefund,
- HdNextDayRefundClass::sumMoneyOnlyAmountByMainAndTime($mainId, $currentStartTime, $currentEndTime),
- 2
- );
- //零售开单
- $lsIncome = 0;
- $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
- $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
- foreach ($hdOrderList as $hdKey => $hdOrder) {
- $actPrice = $hdOrder['mainPay'] ?? 0;
- $cash = $hdOrder['cash'] ?? 0;
- if ($actPrice <= 0 && $cash <= 0) {
- continue;
- }
- $lsIncome = bcadd($actPrice, $lsIncome, 2);
- $lsIncome = bcadd($lsIncome, $cash, 2);
- $orderSn = $hdOrder['orderSn'] ?? '';
- $tkPrice = $hdOrder['tkPrice'] ?? 0;
- $orderItemList = \bizHd\order\classes\OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($orderItemList)) {
- foreach ($orderItemList as $orderItem) {
- $classId = $orderItem->classId ?? 0;
- $unitPrice = $orderItem->unitPrice ?? 0;
- $refundNum = $orderItem->refundNum ?? 0;
- $num = $orderItem->num ?? 0;
- $productId = $orderItem->itemId ?? 0;
- $name = $orderItem->name ?? '';
- $remainNum = HdNextDayRefundClass::payDayRemainNum(
- $num,
- $refundNum,
- $orderItem->nextRefundNum ?? 0
- );
- $amount = bcmul($remainNum, $unitPrice, 2);
- $className = $classList[$classId]['name'] ?? '已删除';
- $arr[$classId]['className'] = $className;
- $arr[$classId]['classId'] = $classId;
- if (isset($arr[$classId]['income'])) {
- $arr[$classId]['income'] = bcadd($amount, $arr[$classId]['income'], 2);
- $arr[$classId]['saleNum'] = bcadd($arr[$classId]['saleNum'], $remainNum);
- } else {
- $arr[$classId]['expend'] = 0;
- $arr[$classId]['income'] = $amount;
- $arr[$classId]['saleNum'] = $remainNum;
- $arr[$classId]['cgNum'] = 0;
- }
- $belongCost = $orderItem->belongCost ?? 0;
- if ($belongCost == 1) {
- if (isset($tbSaleIncome[$productId])) {
- $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
- $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
- } else {
- $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
- }
- }
- }
- if ($tkPrice > 0) {
- $refundList = HdRefundClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($refundList)) {
- foreach ($refundList as $refund) {
- if ($refund->refundType == 2) {
- // 隔天仅退款记在通过日
- if (intval($refund->sameDay ?? HdRefundClass::SAME_DAY_YES) === HdRefundClass::SAME_DAY_NO) {
- continue;
- }
- $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
- }
- }
- }
- }
- }
- }
- $totalIncome = 0;
- $totalExpend = 0;
- if (!empty($arr)) {
- foreach ($arr as $key => $val) {
- $income = $val['income'] ?? 0;
- $income = floatval($income);
- $totalIncome = bcadd($totalIncome, $income, 2);
- $expend = $val['expend'] ?? 0;
- $expend = floatval($expend);
- $totalExpend = bcadd($totalExpend, $expend, 2);
- $profit = bcsub($income, $expend, 2);
- $arr[$key]['profit'] = floatval($profit);
- $arr[$key]['income'] = floatval($income);
- $arr[$key]['expend'] = floatval($expend);
- }
- }
- //总成本算上物流打包等费用
- $totalExpend = bcadd($totalExpend, $packFreight, 2);
- //总收入算上采购仅退款
- $totalIncome = bcadd($totalIncome, $onlyRefundCg, 2);
- //总收入加上人工费
- $totalIncome = bcadd($totalIncome, $totalLabourCost, 2);
- //总收入减掉优惠金额
- $totalIncome = bcsub($totalIncome, $totalDiscountAmount, 2);
- //总成本算上销售仅退款
- $totalExpend = bcadd($totalExpend, $onlyRefund, 2);
- $totalProfit = bcsub($totalIncome, $totalExpend, 2);
- $totalIncome = floatval($totalIncome);
- $totalExpend = floatval($totalExpend);
- $totalProfit = floatval($totalProfit);
- $arr = array_values($arr);
- $arr = arrayUtil::arraySort($arr, 'profit');
- if ($export == 1) {
- $statData = [
- ['classId' => 0, 'className' => '物流打包费', 'income' => 0, 'expend' => $packFreight, 'profit' => -$packFreight, 'saleNum' => 0, 'cgNum' => 0],
- ['classId' => 0, 'className' => '采购仅退款', 'income' => $onlyRefundCg, 'expend' => 0, 'profit' => $onlyRefundCg, 'saleNum' => 0, 'cgNum' => 0],
- ['classId' => 0, 'className' => '销售仅退款', 'income' => 0, 'expend' => $onlyRefund, 'profit' => -$onlyRefund, 'saleNum' => 0, 'cgNum' => 0],
- ['classId' => 0, 'className' => '附加人工费', 'income' => $totalLabourCost, 'expend' => 0, 'profit' => $totalLabourCost, 'saleNum' => 0, 'cgNum' => 0],
- ['classId' => 0, 'className' => '优惠金额', 'income' => -$totalDiscountAmount, 'expend' => 0, 'profit' => -$totalDiscountAmount, 'saleNum' => 0, 'cgNum' => 0],
- ['classId' => 0, 'className' => '【汇总金额】', 'income' => $totalIncome, 'expend' => $totalExpend, 'profit' => $totalProfit, 'saleNum' => 0, 'cgNum' => 0],
- ];
- $list = array_merge($arr, $statData);
- self::exportIncomeClass($list, $mainId);
- }
- util::success(['list' => $arr, 'packFreight' => $packFreight, 'onlyRefundCg' => $onlyRefundCg, 'onlyRefund' => $onlyRefund,
- 'totalIncome' => $totalIncome,
- 'totalExpend' => $totalExpend,
- 'totalProfit' => $totalProfit,
- 'tbSaleIncome' => $tbSaleIncome,
- 'tbCgIncome' => $tbCgIncome,
- 'tbCgIncomeStaff' => $tbCgIncomeStaff,
- 'tbSaleIncomeStaff' => $tbSaleIncomeStaff,
- 'totalLabourCost' => $totalLabourCost,
- 'totalDiscountAmount' => $totalDiscountAmount,
- ]);
- }
- public static function exportIncomeOutItem($list, $mainId)
- {
- $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
- require_once($phpExcelFile . 'Classes/PHPExcel.php');
- $objPHPExcel = new \PHPExcel();
- $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
- ->setLastModifiedBy("Maarten Balliauw")
- ->setTitle("Office 2007 XLSX Document")
- ->setSubject("Office 2007 XLSX Document")
- ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
- ->setKeywords("office 2007 openxml php")
- ->setCategory("file");
- $ghsTitle = '供货商';
- $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
- $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
- $objPHPExcel->getActiveSheet()->setCellValue('B1', '销售收入');
- $objPHPExcel->getActiveSheet()->setCellValue('C1', '销售数量');
- $objPHPExcel->getActiveSheet()->setCellValue('D1', '采购成本');
- $objPHPExcel->getActiveSheet()->setCellValue('E1', '采购数量');
- $objPHPExcel->getActiveSheet()->setCellValue('F1', '利润');
- //设置宽度
- $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
- $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
- $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
- $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
- //加粗
- $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $baseRow = 2;
- foreach ($list as $key => $custom) {
- $i = $baseRow + $key;
- $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['name']);
- $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['income']);
- $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['inNum']);
- $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['expend']);
- $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['outNum']);
- $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['profit']);
- //居左
- $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- }
- $fileName = '收支花材-' . date("m-d");
- $objPHPExcel->getActiveSheet()->setTitle($fileName);
- $objPHPExcel->setActiveSheetIndex(0);
- $dir = './priceTable/' . $mainId;
- if (file_exists($dir) == false) {
- mkdir($dir, 0777, true);
- }
- $date = $fileName;
- $file = $date . '.xls';
- $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
- if (file_exists($dir . '/' . $file)) {
- unlink($dir . '/' . $file);
- }
- $objWriter->save($dir . '/' . $file);
- $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
- util::success(['file' => $fileUrl, 'shortFile' => $file]);
- }
- public static function exportIncomeClass($list, $mainId)
- {
- $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
- require_once($phpExcelFile . 'Classes/PHPExcel.php');
- $objPHPExcel = new \PHPExcel();
- $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
- ->setLastModifiedBy("Maarten Balliauw")
- ->setTitle("Office 2007 XLSX Document")
- ->setSubject("Office 2007 XLSX Document")
- ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
- ->setKeywords("office 2007 openxml php")
- ->setCategory("file");
- $ghsTitle = '供货商';
- $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
- $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
- $objPHPExcel->getActiveSheet()->setCellValue('B1', '销售数量');
- $objPHPExcel->getActiveSheet()->setCellValue('C1', '销售收入');
- $objPHPExcel->getActiveSheet()->setCellValue('D1', '采购数量');
- $objPHPExcel->getActiveSheet()->setCellValue('E1', '采购成本');
- $objPHPExcel->getActiveSheet()->setCellValue('F1', '利润');
- //设置宽度
- $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
- //加粗
- $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('F1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
- $baseRow = 2;
- foreach ($list as $key => $custom) {
- $i = $baseRow + $key;
- $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['className']);
- $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['saleNum']);
- $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['income']);
- $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['cgNum']);
- $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['expend']);
- $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['profit']);
- //居左
- $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
- }
- $fileName = '收支分类-' . time();
- $objPHPExcel->getActiveSheet()->setTitle($fileName);
- $objPHPExcel->setActiveSheetIndex(0);
- $dir = './priceTable/' . $mainId;
- if (file_exists($dir) == false) {
- mkdir($dir, 0777, true);
- }
- $date = $fileName;
- $file = $date . '.xls';
- $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
- if (file_exists($dir . '/' . $file)) {
- unlink($dir . '/' . $file);
- }
- $objWriter->save($dir . '/' . $file);
- $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
- util::success(['file' => $fileUrl, 'shortFile' => $file]);
- }
- public static function exportItemSale($list, $mainId)
- {
- $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
- require_once($phpExcelFile . 'Classes/PHPExcel.php');
- $objPHPExcel = new \PHPExcel();
- $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
- ->setLastModifiedBy("Maarten Balliauw")
- ->setTitle("Office 2007 XLSX Document")
- ->setSubject("Office 2007 XLSX Document")
- ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
- ->setKeywords("office 2007 openxml php")
- ->setCategory("file");
- $ghsTitle = '供货商';
- $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
- $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
- $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
- $objPHPExcel->getActiveSheet()->setCellValue('B1', '总数量');
- $objPHPExcel->getActiveSheet()->setCellValue('C1', '总金额');
- $objPHPExcel->getActiveSheet()->setCellValue('D1', '毛利');
- $objPHPExcel->getActiveSheet()->setCellValue('E1', '毛利率');
- //设置宽度
- $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
- $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
- $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
- $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
- $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
- //加粗
- $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
- $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $baseRow = 2;
- foreach ($list as $key => $custom) {
- $i = $baseRow + $key;
- $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['name']);
- $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['num']);
- $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['amount']);
- $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['gross']);
- $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['mll'] . '%');
- //居左
- $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
- }
- $fileName = '花材销量-' . date("m-d");
- $objPHPExcel->getActiveSheet()->setTitle($fileName);
- $objPHPExcel->setActiveSheetIndex(0);
- $dir = './priceTable/' . $mainId;
- if (file_exists($dir) == false) {
- mkdir($dir, 0777, true);
- }
- $date = $fileName;
- $file = $date . '.xls';
- $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
- if (file_exists($dir . '/' . $file)) {
- unlink($dir . '/' . $file);
- }
- $objWriter->save($dir . '/' . $file);
- $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
- util::success(['file' => $fileUrl, 'shortFile' => $file]);
- }
- public static function send($mainId)
- {
- $get = Yii::$app->request->get();
- $where = [];
- $where['mainId'] = $mainId;
- $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
- $start = $period['startTime'];
- $end = $period['endTime'];
- $where['time'] = ['between', [$start, $end]];
- $currentStartDate = date('Y-m-d', strtotime($start));
- $currentEndDate = date('Y-m-d', strtotime($end));
- $currentStartTime = $currentStartDate . ' 00:00:00';
- $currentEndTime = $currentEndDate . ' 23:59:59';
- $s = strtotime($currentStartTime);
- $e = strtotime($currentEndTime);
- $p = bcsub($e, $s);
- $xx = bcmul(86400, 90);
- if ($p > $xx) {
- util::fail('查询时间不能超过三个月,编号699');
- }
- $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
- $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
- $arr = [];
- if (!empty($ghsOrderList)) {
- foreach ($ghsOrderList as $ghsOrder) {
- $book = $ghsOrder['book'] ?? 0;
- $status = $ghsOrder['status'] ?? 0;
- if ($book == 1 && $status == 2) {
- //预订单待发货的不算收入
- continue;
- }
- if ($status == 0 || $status == 5) {
- continue;
- }
- $orderSn = $ghsOrder['orderSn'] ?? '';
- $actPrice = $ghsOrder['actPrice'] ?? 0;
- if ($actPrice <= 0) {
- continue;
- }
- $sendStaffId = $ghsOrder['sendStaffId'] ?? 0;
- $sendStaffName = isset($ghsOrder['sendStaffName']) && !empty($ghsOrder['sendStaffName']) ? $ghsOrder['sendStaffName'] : '未记名';
- if (isset($arr[$sendStaffId])) {
- $arr[$sendStaffId]['orderNum'] = bcadd($arr[$sendStaffId]['orderNum'], 1);
- $arr[$sendStaffId]['amount'] = bcadd($arr[$sendStaffId]['amount'], $actPrice, 2);
- } else {
- $arr[$sendStaffId] = ['staffName' => $sendStaffName, 'orderNum' => 1, 'itemNum' => 0, 'amount' => $actPrice, 'staffId' => $sendStaffId];
- }
- $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($orderItemList)) {
- foreach ($orderItemList as $orderItem) {
- $remainNum = NextDayRefundClass::payDayRemainNum(
- $orderItem->xhNum ?? 0,
- $orderItem->refundNum ?? 0,
- $orderItem->nextRefundNum ?? 0
- );
- $arr[$sendStaffId]['itemNum'] = bcadd($arr[$sendStaffId]['itemNum'], $remainNum);
- }
- }
- }
- }
- // 隔天售后:数量已用 nextRefundNum;通过日再扣(无当日配送也建负行)
- self::applyNextDayAdjustToSendStaffRows(
- $arr,
- $mainId,
- $currentStartTime,
- $currentEndTime
- );
- if (!empty($arr)) {
- $arr = array_values($arr);
- }
- return $arr;
- }
- /**
- * 片区业绩:按片区汇总笔数/扎数/金额/成本/利润,供 ghsApp distSale、/stat-kh-cg/dist 使用。
- * totalCount 为总扎数(含隔天售后扣减),与各行 count 口径一致。
- */
- public static function customDistStat($mainId)
- {
- $get = Yii::$app->request->get();
- $where = [];
- $where['mainId'] = $mainId;
- $searchTime = !empty($get['searchTime']) ? $get['searchTime'] : 'today';
- $startTime = $get['startTime'] ?? '';
- $endTime = $get['endTime'] ?? '';
- $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
- $start = $period['startTime'];
- $end = $period['endTime'];
- $where['time'] = ['between', [$start, $end]];
- $currentStartDate = date('Y-m-d', strtotime($start));
- $currentEndDate = date('Y-m-d', strtotime($end));
- $currentStartTime = $currentStartDate . ' 00:00:00';
- $currentEndTime = $currentEndDate . ' 23:59:59';
- $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
- $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
- $customList = CustomClass::getAllByCondition(['ownMainId' => $mainId], null, 'id,name,distId', 'id');
- $distMap = DistClass::getAllByCondition(['mainId' => $mainId], null, '*', 'id');
- $arr = [];
- $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
- $totalAmount = 0;
- $totalNum = 0;
- $totalCount = 0;
- $totalProfit = 0;
- $totalCost = 0;
- if (!empty($ghsOrderList)) {
- foreach ($ghsOrderList as $ghsOrder) {
- $actPrice = $ghsOrder['actPrice'] ?? 0;
- if ($actPrice <= 0) {
- continue;
- }
- $cost = $ghsOrder['remainCost'] ?? 0;
- $profit = bcsub($actPrice, $cost, 2);
- $orderPrice = $ghsOrder['orderPrice'] ?? 0;
- $tkPrice = $ghsOrder['tkPrice'] ?? 0;
- $customId = $ghsOrder['customId'] ?? 0;
- $status = $ghsOrder['status'] ?? 0;
- $book = $ghsOrder['book'] ?? 0;
- //扎数
- $count = $ghsOrder['itemNum'] ?? 0;
- if ($status == 1 || $status == 5) {
- continue;
- }
- if ($book == 1 && $status == 2) {
- continue;
- }
- $distId = $customList[$customId]['distId'] ?? 0;
- $distName = isset($distMap[$distId]) ? $distMap[$distId]['name'] : '未划片区';
- $totalNum++;
- $totalCount = bcadd($totalCount, $count, 2);
- $totalAmount = bcadd($totalAmount, $actPrice, 2);
- $totalProfit = bcadd($totalProfit, $profit, 2);
- $totalCost = bcadd($totalCost, $cost, 2);
- if (isset($arr[$distId])) {
- $arr[$distId]['num']++;
- $arr[$distId]['amount'] = bcadd($arr[$distId]['amount'], $actPrice, 2);
- $arr[$distId]['orderPrice'] = bcadd($arr[$distId]['orderPrice'], $orderPrice, 2);
- $arr[$distId]['count'] = bcadd($arr[$distId]['count'], $count, 2);
- $arr[$distId]['tkPrice'] = bcadd($arr[$distId]['tkPrice'], $tkPrice, 2);
- $arr[$distId]['cost'] = bcadd($arr[$distId]['cost'], $cost, 2);
- $arr[$distId]['profit'] = bcadd($arr[$distId]['profit'], $profit, 2);
- } else {
- $arr[$distId] = [
- 'distId' => $distId,
- 'distName' => $distName,
- 'num' => 1,
- 'count' => $count,
- 'amount' => $actPrice,
- 'levelName' => '',
- 'orderPrice' => $orderPrice,
- 'tkPrice' => $tkPrice,
- 'cost' => $cost,
- 'profit' => $profit,
- ];
- }
- }
- }
- // 零售客户无批发片区:全部归入虚拟片区「零售片区」
- $hdDistKey = 'hd_retail';
- $productCostMap = ProductClass::getAllByCondition(['mainId' => $mainId], null, 'id,cost,avCost', 'id');
- $hdWhere = [
- 'mainId' => $mainId,
- 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]],
- 'payTime' => ['between', [$currentStartTime, $currentEndTime]],
- ];
- $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
- if (!empty($hdOrderList)) {
- foreach ($hdOrderList as $hdOrder) {
- if (intval($hdOrder['forward'] ?? 0) === 1) {
- continue;
- }
- $status = intval($hdOrder['status'] ?? 0);
- if ($status === HdOrderClass::ORDER_STATUS_UN_PAY || $status === HdOrderClass::ORDER_STATUS_CANCEL) {
- continue;
- }
- $mainPay = bcadd((string)($hdOrder['mainPay'] ?? '0'), '0', 2);
- $tkPrice = bcadd((string)($hdOrder['tkPrice'] ?? '0'), '0', 2);
- $cash = bcadd((string)($hdOrder['cash'] ?? '0'), '0', 2);
- $actPrice = bcadd(bcsub($mainPay, $tkPrice, 2), $cash, 2);
- if (bccomp($actPrice, '0', 2) <= 0) {
- continue;
- }
- $orderSn = $hdOrder['orderSn'] ?? '';
- $count = '0.00';
- $cost = '0.00';
- $orderItemList = HdOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
- if (!empty($orderItemList)) {
- foreach ($orderItemList as $orderItem) {
- $remainNum = HdNextDayRefundClass::payDayRemainNum(
- $orderItem->num ?? 0,
- $orderItem->refundNum ?? 0,
- $orderItem->nextRefundNum ?? 0
- );
- $count = bcadd($count, $remainNum, 2);
- $productId = intval($orderItem->itemId ?? 0);
- $unitCost = $orderItem->cost ?? null;
- if ($unitCost === null || $unitCost === '') {
- $unitCost = $productCostMap[$productId]['avCost']
- ?? ($productCostMap[$productId]['cost'] ?? 0);
- }
- $cost = bcadd($cost, bcmul($remainNum, (string)$unitCost, 2), 2);
- }
- }
- $profit = bcsub($actPrice, $cost, 2);
- $totalNum++;
- $totalCount = bcadd($totalCount, $count, 2);
- $totalAmount = bcadd($totalAmount, $actPrice, 2);
- $totalProfit = bcadd($totalProfit, $profit, 2);
- $totalCost = bcadd($totalCost, $cost, 2);
- if (!isset($arr[$hdDistKey])) {
- $arr[$hdDistKey] = [
- 'distId' => 0,
- 'distName' => '零售片区',
- 'num' => 0,
- 'count' => '0.00',
- 'amount' => '0.00',
- 'levelName' => '',
- 'orderPrice' => 0,
- 'tkPrice' => 0,
- 'cost' => 0,
- 'profit' => '0.00',
- ];
- }
- $arr[$hdDistKey]['num']++;
- $arr[$hdDistKey]['amount'] = bcadd($arr[$hdDistKey]['amount'], $actPrice, 2);
- $arr[$hdDistKey]['orderPrice'] = bcadd($arr[$hdDistKey]['orderPrice'], $mainPay, 2);
- $arr[$hdDistKey]['count'] = bcadd($arr[$hdDistKey]['count'], $count, 2);
- $arr[$hdDistKey]['tkPrice'] = bcadd($arr[$hdDistKey]['tkPrice'], $tkPrice, 2);
- $arr[$hdDistKey]['cost'] = bcadd($arr[$hdDistKey]['cost'], $cost, 2);
- $arr[$hdDistKey]['profit'] = bcadd($arr[$hdDistKey]['profit'], $profit, 2);
- }
- }
- // 隔天售后按客户归到片区扣金额/扎数(批发走客户片区;零售统一扣「零售片区」)
- $fwdMap = NextDayRefundClass::sumAmountGroupByCustom($mainId, $currentStartTime, $currentEndTime);
- $qtyMap = NextDayRefundClass::sumItemQtyByCustom($mainId, $currentStartTime, $currentEndTime);
- $cidKeys = array_unique(array_merge(array_keys($fwdMap), array_keys($qtyMap)));
- foreach ($cidKeys as $cid) {
- $amt = $fwdMap[$cid] ?? '0';
- $qty = $qtyMap[$cid] ?? '0';
- if (bccomp((string)$amt, '0', 2) == 0 && bccomp((string)$qty, '0', 2) == 0) {
- continue;
- }
- $distId = $customList[$cid]['distId'] ?? 0;
- if (!isset($arr[$distId])) {
- $arr[$distId] = [
- 'distId' => $distId,
- 'distName' => isset($distMap[$distId]) ? $distMap[$distId]['name'] : '未划片区',
- 'num' => 0,
- 'count' => '0.00',
- 'amount' => '0.00',
- 'levelName' => '',
- 'orderPrice' => 0,
- 'tkPrice' => 0,
- 'cost' => 0,
- 'profit' => '0.00',
- ];
- }
- $arr[$distId]['amount'] = bcsub((string)$arr[$distId]['amount'], (string)$amt, 2);
- $arr[$distId]['profit'] = bcsub((string)$arr[$distId]['profit'], (string)$amt, 2);
- $arr[$distId]['count'] = bcsub((string)($arr[$distId]['count'] ?? 0), (string)$qty, 2);
- $totalAmount = bcsub((string)$totalAmount, (string)$amt, 2);
- $totalProfit = bcsub((string)$totalProfit, (string)$amt, 2);
- $totalCount = bcsub((string)$totalCount, (string)$qty, 2);
- }
- $hdAmtMap = HdNextDayRefundClass::sumAmountGroupByCustom($mainId, $currentStartTime, $currentEndTime);
- $hdQtyMap = HdNextDayRefundClass::sumItemQtyByCustom($mainId, $currentStartTime, $currentEndTime);
- $hdAmtTotal = '0.00';
- $hdQtyTotal = '0.00';
- foreach ($hdAmtMap as $row) {
- $hdAmtTotal = bcadd($hdAmtTotal, (string)($row['amount'] ?? '0'), 2);
- }
- foreach ($hdQtyMap as $row) {
- $hdQtyTotal = bcadd($hdQtyTotal, (string)($row['num'] ?? '0'), 2);
- }
- if (bccomp($hdAmtTotal, '0', 2) > 0 || bccomp($hdQtyTotal, '0', 2) > 0) {
- if (!isset($arr[$hdDistKey])) {
- $arr[$hdDistKey] = [
- 'distId' => 0,
- 'distName' => '零售片区',
- 'num' => 0,
- 'count' => '0.00',
- 'amount' => '0.00',
- 'levelName' => '',
- 'orderPrice' => 0,
- 'tkPrice' => 0,
- 'cost' => 0,
- 'profit' => '0.00',
- ];
- }
- $arr[$hdDistKey]['amount'] = bcsub((string)$arr[$hdDistKey]['amount'], $hdAmtTotal, 2);
- $arr[$hdDistKey]['profit'] = bcsub((string)$arr[$hdDistKey]['profit'], $hdAmtTotal, 2);
- $arr[$hdDistKey]['count'] = bcsub((string)($arr[$hdDistKey]['count'] ?? 0), $hdQtyTotal, 2);
- $totalAmount = bcsub((string)$totalAmount, $hdAmtTotal, 2);
- $totalProfit = bcsub((string)$totalProfit, $hdAmtTotal, 2);
- $totalCount = bcsub((string)$totalCount, $hdQtyTotal, 2);
- }
- if (!empty($arr)) {
- $arr = arrayUtil::arraySort($arr, 'profit');
- }
- $totalAmount = floatval($totalAmount);
- return ['list' => $arr, 'totalAmount' => $totalAmount, 'totalNum' => $totalNum, 'totalCount' => floatval($totalCount), 'totalCost' => $totalCost, 'totalProfit' => $totalProfit];
- }
- /**
- * 花材收支明细:支付日已用 nextRefundNum;此处按通过日扣退货数量/金额,并返回隔天仅退款合计。
- *
- * @param array $arr 产品收支行(引用修改 income/inNum)
- * @param array $tbSaleIncome 归属成本花材销售明细(引用同步加减)
- * @param array $productList 花材字典
- * @param int $mainId
- * @param string $startTime
- * @param string $endTime
- * @return string 通过日隔天「仅退款」合计,需加到 onlyRefund
- */
- private static function applyNextDayAdjustToProductIncomeRows(
- &$arr,
- &$tbSaleIncome,
- $productList,
- $mainId,
- $startTime,
- $endTime
- ) {
- $applyDelta = function ($pidMap, $sign) use (&$arr, &$tbSaleIncome, $productList) {
- foreach ($pidMap as $pid => $row) {
- $deltaNum = bcmul((string)($row['num'] ?? '0'), (string)$sign, 2);
- $deltaAmt = bcmul((string)($row['amount'] ?? '0'), (string)$sign, 2);
- if (bccomp($deltaAmt, '0', 2) == 0 && bccomp($deltaNum, '0', 2) == 0) {
- continue;
- }
- // 通过日扣减:当日无该花材开单也建负行(收入/数量可为负)
- if (!isset($arr[$pid])) {
- $arr[$pid] = [
- 'name' => $productList[$pid]['name'] ?? '已删除',
- 'id' => $pid,
- 'expend' => 0,
- 'outNum' => 0,
- 'inNum' => '0.00',
- 'income' => '0.00',
- ];
- }
- $arr[$pid]['income'] = bcadd((string)($arr[$pid]['income'] ?? 0), $deltaAmt, 2);
- $arr[$pid]['inNum'] = bcadd((string)($arr[$pid]['inNum'] ?? 0), $deltaNum, 2);
- if (isset($tbSaleIncome[$pid])) {
- $tbSaleIncome[$pid]['price'] = bcadd((string)($tbSaleIncome[$pid]['price'] ?? 0), $deltaAmt, 2);
- $tbSaleIncome[$pid]['num'] = bcadd((string)($tbSaleIncome[$pid]['num'] ?? 0), $deltaNum, 2);
- }
- }
- };
- // 通过日:扣退货数量+金额(ghs 批发 + hd 零售共用花材库存)
- $applyDelta(
- NextDayRefundClass::sumItemByProduct($mainId, $startTime, $endTime),
- -1
- );
- $applyDelta(
- HdNextDayRefundClass::sumItemByProduct($mainId, $startTime, $endTime),
- -1
- );
- $onlyRefund = NextDayRefundClass::sumMoneyOnlyAmountByMainAndTime($mainId, $startTime, $endTime);
- return bcadd(
- (string)$onlyRefund,
- HdNextDayRefundClass::sumMoneyOnlyAmountByMainAndTime($mainId, $startTime, $endTime),
- 2
- );
- }
- /**
- * 配送业绩:支付日数量已用 nextRefundNum;通过日再扣数量/金额(仅退款只减金额)。
- * 无当日配送单的配送员也会建负行业绩行。
- *
- * @param array $arr sendStaffId => 业绩行(引用修改 itemNum/amount)
- * @param int $mainId
- * @param string $startTime
- * @param string $endTime
- */
- private static function applyNextDayAdjustToSendStaffRows(
- &$arr,
- $mainId,
- $startTime,
- $endTime
- ) {
- $applyDelta = function ($staffMap, $sign) use (&$arr) {
- foreach ($staffMap as $sid => $row) {
- $deltaNum = bcmul((string)($row['num'] ?? '0'), (string)$sign, 2);
- $deltaAmt = bcmul((string)($row['amount'] ?? '0'), (string)$sign, 2);
- if (bccomp($deltaAmt, '0', 2) == 0 && bccomp($deltaNum, '0', 2) == 0) {
- continue;
- }
- if (!isset($arr[$sid])) {
- $arr[$sid] = [
- 'staffName' => '未记名',
- 'orderNum' => 0,
- 'itemNum' => '0.00',
- 'amount' => '0.00',
- 'staffId' => $sid,
- ];
- }
- $arr[$sid]['itemNum'] = bcadd((string)($arr[$sid]['itemNum'] ?? 0), $deltaNum, 2);
- $arr[$sid]['amount'] = bcadd((string)($arr[$sid]['amount'] ?? 0), $deltaAmt, 2);
- }
- };
- $applyDelta(
- NextDayRefundClass::sumItemBySendStaff($mainId, $startTime, $endTime),
- -1
- );
- // 仅退款只减金额
- $applyDelta(
- NextDayRefundClass::sumMoneyOnlyAmountBySendStaff($mainId, $startTime, $endTime),
- -1
- );
- }
- }
|